9
$\begingroup$

I want to combine an Image3D and a Graphics3D using Show. Here is the code:

data = Table[
   Abs[((3 - 3 z) (.5 Sign[3 - 3 z] + .5)) (x Sin[x] + y Cos[y])],
   {x, 0, 15, .1}, {y, 0, 10, .1}, {z, 0, 3, .1}
   ];
Show[Image3D[data/Max@data], 
 Graphics3D[{AbsoluteThickness[5], Blue, 
   Line[{{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14, 9, 2}, {15, 
      10, 0}}]}]]

While the coordinates are aligned, the output is not.

3d path overlap

See the blue line on the right corner. How can I fix the problem?

Edit: The issue is resolved. There are two answers below. Both are helpful but neither of them is complete. Once any of them is competed, I'll mark it as accepted. I write one possible answer to the question here (I added {#[[3]], #[[2]], #[[1]]}/delta & /@ pointList to my code):

With[{delta = .1},
 data = Table[
   Abs[((3 - 3 z) (.5 Sign[3 - 3 z] + .5)) (x Sin[x] + y Cos[y])],
   {x, 0, 15, delta}, {y, 0, 10, delta}, {z, 0, 3, delta}
   ];
 Show[Graphics3D[{AbsoluteThickness[5], Blue, 
    Line[{#[[3]], #[[2]], #[[1]]}/
       delta & /@ {{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14, 9,
         2}, {15, 10, 0}}]}], Image3D[data/Max@data]]
 ]
$\endgroup$
1
  • $\begingroup$ Maybe you'll have better luck with the alignment if you use Raster3D[] instead... $\endgroup$ Apr 22, 2013 at 10:45

3 Answers 3

12
$\begingroup$

While the coordinates are aligned

Unfortunately, they are not because the moment you create a Image3D, the coordinate system is different. Usually, if your image has the dimensions $n_x \times n_y \times n_z$ the coordinate system of your volume ranges from {{1,nx},{1,ny},{1,nz}} depending on the implementation.

If you want to combine an Image3D and some other stuff, you have to rescale your coordinates. This can be done using RescalingTransform:

data = Reverse@Table[
   Abs[((3 - 3 z) (.5 Sign[3 - 3 z] + .5)) (x Sin[x] + y Cos[y])], {z,
     0, 3, .1}, {y, 0, 10, .1}, {x, 0, 15, .1}];
img3d = Image3D[data/Max@data];
rescale = 
  RescalingTransform[{{0, 15}, {0, 10}, {0, 3}}, 
   Transpose[{{1, 1, 1}, ImageDimensions[img3d]}]];
Show[img3d, 
 Graphics3D[{AbsoluteThickness[5], Blue, 
   Line[rescale /@ {{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14, 
       9, 2}, {15, 10, 0}}]}], Axes -> True]

enter image description here

Additionally, I'm pretty sure you think your Table call does something different. Please note the following:

Table[0, {x, 5}, {y, 6}, {z, 7}] // Dimensions

This gives a volume having dimension z=5, y=6 and x=7! If you look at my Table call in the code, you see that I reversed the iterators.

At last, please note that data in Image and Image3D is usually in reverse order compared to the usual coordinate system.

$\endgroup$
5
  • $\begingroup$ You beat me! but there still axes problem as described by cormullion. I used Line[1/0.1 {#[[3]], 10 - #[[2]], 15 - #[[1]]} & /@ lineNodes] to transform them. $\endgroup$
    – Silvia
    Apr 22, 2013 at 10:41
  • $\begingroup$ @Silvia Look at my Table call. What people forget is, that Table[0, {x, 5}, {y, 6}, {z, 7}] does not the expected thing. Dimension is reversed! Maybe I should add this to the post. $\endgroup$
    – halirutan
    Apr 22, 2013 at 10:45
  • $\begingroup$ You are right Silvia. I realized the same thing. See my comment under cormullion's answer. $\endgroup$
    – Helium
    Apr 22, 2013 at 10:45
  • $\begingroup$ @halirutan Sorry I didn't notice it. But I think it should be {z, 3, 0, -.1}? Compare the composed result with that composed with say ContourPlot3D and the line graphics. $\endgroup$
    – Silvia
    Apr 22, 2013 at 10:49
  • $\begingroup$ @Silvia Yes, this is maybe true. I didn't know what to OP wanted but the first dimension in Image/Image3D is reversed. $\endgroup$
    – halirutan
    Apr 22, 2013 at 10:54
3
$\begingroup$

This may not be the problem (and this may not be an answer), but I noticed the axes were different:

Graphics3D[{AbsoluteThickness[5], Blue, 
  Line[{{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14, 9, 2}, 
  {15, 10, 0}}]},  Axes -> True, AxesLabel -> {"x", "y", "z"}]

plot 1

Image3D[data/Max@ data, Axes -> True, AxesLabel -> {"x", "y", "z"}]

plot 2

$\endgroup$
3
  • $\begingroup$ Thanks cormullion. I noticed the same issue. A simple transform on the line points resolves the issue: {#[[3]], #[[2]], #[[1]]}/delta & /@ pointList. But I wonder if it is a bug? $\endgroup$
    – Helium
    Apr 22, 2013 at 10:37
  • $\begingroup$ The docs say 'slices, rows, and columns' correspond to x, y, and z..., so it may be deliberate. @halirutan as usual knows what's going on. $\endgroup$
    – cormullion
    Apr 22, 2013 at 10:43
  • 2
    $\begingroup$ @Mohsen I don't consider it as a bug. IMO Image3D, like Image, uses coordinates like Array/Matrix, which is different from Graphics by convention. $\endgroup$
    – Silvia
    Apr 22, 2013 at 10:44
3
$\begingroup$

Everybody seems happy already at the time of this answer. But the following resources will help a newcomer develop good habits:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.