Skip to content

Commit

Permalink
Refine rendering equation
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenvivek committed Feb 9, 2024
1 parent ebce52a commit 190745c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
6 changes: 3 additions & 3 deletions diffdrr/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ def __init__(
self.mode = mode

def dims(self, volume):
return torch.tensor(volume.shape).to(volume) + 1
return torch.tensor(volume.shape).to(volume) - 1

def forward(
self, volume, spacing, source, target, n_points=100, align_corners=True
self, volume, spacing, source, target, n_points=250, align_corners=True
):
# Get the raylength and reshape sources
raylength = (source - target + self.eps).norm(dim=-1)
Expand All @@ -151,5 +151,5 @@ def forward(
vol = volume[None, None, :, :, :].expand(batch_size, -1, -1, -1, -1)
drr = grid_sample(vol, rays, mode=self.mode, align_corners=align_corners)
drr = drr[:, 0, 0].sum(dim=-1)
drr *= raylength
drr *= raylength / n_points
return drr
14 changes: 10 additions & 4 deletions notebooks/api/01_renderers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@
"source": [
"## Trilinear interpolation\n",
"\n",
"Instead of computing the exact line integral over the voxel grid (i.e., Siddon's method), we can sample colors at points along the each ray using trilinear interpolation."
"Instead of computing the exact line integral over the voxel grid (i.e., Siddon's method), we can sample colors at points along the each ray using trilinear interpolation.\n",
"\n",
"Now, the rendering equation is\n",
"\\begin{equation}\n",
" E(R) = \\frac{\\|\\mathbf p - \\mathbf s\\|_2}{M} \\sum_{m=1}^{M} \\mathbf V \\left[ \\mathbf s + \\alpha_m (\\mathbf p - \\mathbf s) \\right] \\,,\n",
"\\end{equation}\n",
"where $\\mathbf V[\\cdot]$ is the trilinear interpolation function and $M$ is the number of points sampled per ray."
]
},
{
Expand Down Expand Up @@ -254,9 +260,9 @@
" self.mode = mode\n",
"\n",
" def dims(self, volume):\n",
" return torch.tensor(volume.shape).to(volume) + 1\n",
" return torch.tensor(volume.shape).to(volume) - 1\n",
"\n",
" def forward(self, volume, spacing, source, target, n_points=100, align_corners=True): \n",
" def forward(self, volume, spacing, source, target, n_points=250, align_corners=True): \n",
" # Get the raylength and reshape sources\n",
" raylength = (source - target + self.eps).norm(dim=-1)\n",
" source = source[:, None, :, None, :]\n",
Expand All @@ -276,7 +282,7 @@
" vol = volume[None, None, :, :, :].expand(batch_size, -1, -1, -1, -1)\n",
" drr = grid_sample(vol, rays, mode=self.mode, align_corners=align_corners)\n",
" drr = drr[:, 0, 0].sum(dim=-1)\n",
" drr *= raylength\n",
" drr *= raylength / n_points\n",
" return drr"
]
},
Expand Down
104 changes: 88 additions & 16 deletions notebooks/tutorials/trilinear.ipynb

Large diffs are not rendered by default.

0 comments on commit 190745c

Please sign in to comment.