Skip to content

Commit 7fdf286

Browse files
authored
Compute raylength in world coordinates (#328)
1 parent be96ec9 commit 7fdf286

4 files changed

Lines changed: 17 additions & 15 deletions

File tree

diffdrr/drr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ def forward(
173173
)
174174
img.append(partial)
175175
img = torch.cat(img, dim=-1)
176+
177+
# Multiply by the raylength (in world coordinate units)
178+
img *= self.affine(target - source).norm(dim=-1).unsqueeze(1)
179+
176180
return self.reshape_transform(img, batch_size=len(pose))
177181

178182
# %% ../notebooks/api/00_drr.ipynb 11

diffdrr/renderers.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ def forward(
8282
.scatter_add_(1, channels.transpose(-1, -2), img.transpose(-1, -2))
8383
)
8484

85-
# Multiply by ray length such that the proportion of attenuated energy is unitless
86-
raylength = (target - source + self.eps).norm(dim=-1)
87-
img *= raylength.unsqueeze(1)
8885
return img
8986

9087
# %% ../notebooks/api/01_renderers.ipynb 8
@@ -202,6 +199,10 @@ def forward(
202199
# Sample the volume with trilinear interpolation
203200
img = _get_voxel(volume, xyzs, self.mode, align_corners=align_corners)
204201

202+
# Multiply by the step size to compute the rectangular rule for integration
203+
step_size = (alphamax - alphamin) / (n_points - 1)
204+
img = img * step_size
205+
205206
# Handle optional masking
206207
if mask is None:
207208
img = img.sum(dim=-1).unsqueeze(1)
@@ -217,8 +218,4 @@ def forward(
217218
.scatter_add_(1, channels.transpose(-1, -2), img.transpose(-1, -2))
218219
)
219220

220-
# Compute the line integral with the rectangular rule and return the DRR
221-
raylength = (target - source + self.eps).norm(dim=-1).unsqueeze(1)
222-
step_size = (alphamax - alphamin) / (n_points - 1)
223-
img *= raylength * step_size
224221
return img

notebooks/api/00_drr.ipynb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
" self.patch_size = patch_size\n",
196196
" if self.patch_size is not None:\n",
197197
" self.n_patches = (height * width) // (self.patch_size**2)\n",
198-
"\n",
198+
" \n",
199199
" def reshape_transform(self, img, batch_size):\n",
200200
" if self.reshape:\n",
201201
" if self.detector.n_subsample is None:\n",
@@ -292,6 +292,10 @@
292292
" )\n",
293293
" img.append(partial)\n",
294294
" img = torch.cat(img, dim=-1)\n",
295+
" \n",
296+
" # Multiply by the raylength (in world coordinate units)\n",
297+
" img *= self.affine(target - source).norm(dim=-1).unsqueeze(1)\n",
298+
"\n",
295299
" return self.reshape_transform(img, batch_size=len(pose))"
296300
]
297301
},

notebooks/api/01_renderers.ipynb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@
184184
" .scatter_add_(1, channels.transpose(-1, -2), img.transpose(-1, -2))\n",
185185
" )\n",
186186
"\n",
187-
" # Multiply by ray length such that the proportion of attenuated energy is unitless\n",
188-
" raylength = (target - source + self.eps).norm(dim=-1)\n",
189-
" img *= raylength.unsqueeze(1)\n",
190187
" return img"
191188
]
192189
},
@@ -332,6 +329,10 @@
332329
"\n",
333330
" # Sample the volume with trilinear interpolation\n",
334331
" img = _get_voxel(volume, xyzs, self.mode, align_corners=align_corners)\n",
332+
" \n",
333+
" # Multiply by the step size to compute the rectangular rule for integration\n",
334+
" step_size = (alphamax - alphamin) / (n_points - 1)\n",
335+
" img = img * step_size\n",
335336
"\n",
336337
" # Handle optional masking\n",
337338
" if mask is None:\n",
@@ -348,10 +349,6 @@
348349
" .scatter_add_(1, channels.transpose(-1, -2), img.transpose(-1, -2))\n",
349350
" )\n",
350351
"\n",
351-
" # Compute the line integral with the rectangular rule and return the DRR\n",
352-
" raylength = (target - source + self.eps).norm(dim=-1).unsqueeze(1)\n",
353-
" step_size = (alphamax - alphamin) / (n_points - 1)\n",
354-
" img *= raylength * step_size\n",
355352
" return img"
356353
]
357354
},

0 commit comments

Comments
 (0)