<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-09-07 18:58 GMT+02:00 David Gobbi <span dir="ltr"><<a target="_blank" href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>></span>:<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div dir="ltr">Hi Elvis,<div><br></div><div>Let's break this down into two parts:</div><div><br></div><div>1) Going from VTK world coords to VTK data coords is achieved via the prop matrix (where a "prop" is a vtkActor, a vtkVolume, a vtkImageSlice, etc):</div><div><br></div><div>For data-to-world, apply the matrix to the point.</div><div>For world-to-data, apply the inverse of the matrix.</div><div><br></div><div>2) Going from VTK data coords (x,y,z) to point indices (i,j,k) is done with these equations:</div><div><br></div><div>x = origin[0] + i * spacing[0]</div><div>y = origin[1] + j * spacing[1]<br></div><div>z = origin[2] + k * spacing[2]<br></div><div><br></div><div>i = round((x - origin[0])/spacing[0])</div><div>j = round((y - origin[1])/spacing[1])<br></div><div>k = round((z - origin[2])/spacing[2])<br></div><div><br></div><div>Personally, I avoid using any VTK APIs to do these computations, for two reasons:</div><div><br></div><div>a) I prefer mathematical abstractions over programatic abstractions. If I have the equations, then I can combine them and simplify them and often achieve nice performance gains as a result.</div><div><br></div><div>b) When I look at the available APIs, sometimes I find they were written for a subtly different purpose than my own. For example, consider vtkImageData::<wbr>ComputeStructuredPoints:</div><div><br></div><div><div>int ComputeStructuredCoordinates(<wbr>const double x[3], int ijk[3], double pcoords[3]);</div></div><div><br></div><div>It looks like it might do what you want, right? But in fact it was written to work with VTK's abstract concept of a "cell" where, for vtkImageData, a cell is a box with a sample point (i.e. what we typically think of as a voxel) at each corner. So the above method gives us indices to tell us which cell we are in, but we'd have to check the pcoords to find out what point we are closest to. At that point, we might as well just ignore this API and use the equations given in (2).</div></div></blockquote><div><br></div><div>David, you provide an excellent answer as always :)<br><br></div><div>Of course I always knew I could do these transformations myself, but for some reason (well, see below) I was looking for an API that would fit like a glove. Your reasons above are compelling.<br> <br></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div dir="ltr"><div><br></div><div>As an aside, whenever you're dealing with VTK data, you should always keep in mind that the APIs were written according to the way that VTK's defines its "data set" abstraction, i.e. points and cells and attributes thereof. It's necessary to have a firm grasp of this abstraction in order to use VTK to its utmost. However, it's equally necessary to understand that not all problems fit nicely into this abstraction, and sometimes VTK's API's aren't going to nicely match the problem that one is trying to solve. In those cases, it can be useful to (a) read the VTK source code to understand exactly what VTK is doing, and (b) figure out exactly how you might have to do things differently.</div></div></blockquote><div><br></div><div>Indeed, I think that as a newcomer to VTK, due to the richness of its API, you sometimes become a little too used to there being a canned solution for your problem available. At least I know I'm guilty of it at times..<br><br></div><div>Also thanks for clarifying what "cells" are (or well, at least in the context of vtkImageData). It explains the the API of ComputeStructuredCoordinates, which I thought a bit strange at first, and which gives me more info than I need.<br><br></div><div>Cheers,<br></div><div>Elvis<br></div><div> <br></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div dir="ltr"><span class="gmail-HOEnZb"><font color="#888888"><div><br></div><div> - David </div></font></span><div><div class="gmail-h5"><div><br></div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 7, 2016 at 9:55 AM, Elvis Stansvik <span dir="ltr"><<a target="_blank" href="mailto:elvis.stansvik@orexplore.com">elvis.stansvik@orexplore.com</a>></span> wrote:<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><span><p dir="ltr">Den 7 sep. 2016 3:32 em skrev "Cory Quammen" <<a target="_blank" href="mailto:cory.quammen@kitware.com">cory.quammen@kitware.com</a>>:<br>
><br>
> Elvis,<br>
><br>
> Have a look at vtkImageData::FindPoint(double x, double y, double z).<br>
><br>
> It takes into account the image spacing and origin, but will not<br>
> correct for any modifications to the position of the vtkVolume you<br>
> seem to be using to represent it in the scene.</p>
</span><p dir="ltr">Thanks Cory, </p>
<p dir="ltr">I did initially find FindPoint, but I was unsure how I can translate the vtkIdType it returns (some sort of index?) into i,j,k voxel coordinate. Do you know?</p>
<p dir="ltr">Since then I've also found vtkImageData::ComputeStructur<wbr>edCoordinates, which looks similar, but does seem to produce a ijk voxel coordinate. Perhaps that is the function I'm looking for? It's doubly interesting since it has a static variant where I can specify the origin, spacing etc manually, which I might have a need for actually, if I want to do the same computation but don't have access to an actual vtkImageData instance, only its parameters so to speak.</p><span><font color="#888888">
<p dir="ltr">Elvis</p></font></span><div><div>
<p dir="ltr">><br>
> HTH,<br>
> Cory<br>
><br>
><br>
><br>
> On Wed, Sep 7, 2016 at 3:10 AM, Elvis Stansvik<br>
> <<a target="_blank" href="mailto:elvis.stansvik@orexplore.com">elvis.stansvik@orexplore.com</a>> wrote:<br>
> > Den 4 sep. 2016 2:25 em skrev "Elvis Stansvik"<br>
> > <<a target="_blank" href="mailto:elvis.stansvik@orexplore.com">elvis.stansvik@orexplore.com</a>><wbr>:<br>
> >><br>
> >> Hi all,<br>
> >><br>
> >> I have multiple volumes in a renderer, positioned above/below each other<br>
> >> (like a "string" of volumes) along the Z axis, none of them are<br>
> >> intersecting. As the user clicks+drags the mouse in the viewport, I need to<br>
> >> capture the mouse position at the start/end positions of the drag operation<br>
> >> and convert them to world coordinates. I think I have these parts figured<br>
> >> out.<br>
> >><br>
> >> Then, I need to search through the list of my volumes (sorted by their end<br>
> >> positions in world coordinates), and construct a series of vtkExtractVOI<br>
> >> that will extract the selected segments. For volumes that are completely<br>
> >> inside the selected area, the vtkExtractVOI will extract the full volumes,<br>
> >> but for the start/end volume (which may be the same volume), it will only<br>
> >> extract a portion of it.<br>
> >><br>
> >> I looked at the vtkDataSet::FindPoint, which I think will give me a linear<br>
> >> point ID (not familiar with that from before) for a point given in world<br>
> >> coordinates. But how can I convert that to a structured point position that<br>
> >> I can use when setting up the extent to use for my vtkExtractVOI?<br>
> >><br>
> >> Is the static vtkStructuredData::ComputePoin<wbr>tStructuredCoords helper the<br>
> >> function I'm looking for? Or is there some other easier way to extract an<br>
> >> area of a volume when the area is expressed in world coordinates?<br>
> >> (specifically, as a start/end Z position in world coordinates).<br>
> >><br>
> >> Here's an ASCII drawing that perhaps explains better what I want to do,<br>
> >> the rectangular areas are my volumes:<br>
> >><br>
> >> ^ +-----+<br>
> >> | | |<br>
> >> | | P1 | <--- User clicks here (world coordinate X, Y, Z)..<br>
> >> | | |<br>
> >> Z +-----+<br>
> >> +-----+<br>
> >> | |<br>
> >> | |<br>
> >> +-----+<br>
> >> +-----+<br>
> >> | |<br>
> >> | |<br>
> >> | P2 | <--- ..and drags to here (world coordinate A, B, C).<br>
> >> | |<br>
> >> | |<br>
> >> | |<br>
> >> +-----+<br>
> >><br>
> >> From this I want to create three vtkExtractVOI, that when combined gives a<br>
> >> list of vtkImageData that represents the selected area along the Z axis (in<br>
> >> the X and Y direction, everything from the volumes should be included).<br>
> >><br>
> >> Thanks in advance,<br>
> >> Elvis<br>
> ><br>
> > I realized my mail above is a little long-winded and unclear. Sorry about<br>
> > that. In essence, what I'm asking is how to map an x,y,x (double) point in<br>
> > world coordinates to a i,j,k (int) grid position (voxel coordinate) of a<br>
> > given volume. I don't want the nearest point, but to return "null" or<br>
> > equivalent if the world point does not fall inside any voxel.<br>
> ><br>
> > I'd also be interested how to do the same but against a plain vtkImageData<br>
> > instead of a vtkVolume. For this I assume I'd have to provide some info<br>
> > about where in the world the vtkImageData belongs (where/how I intend to map<br>
> > it into the scene).<br>
> ><br>
> > Anyone know what the VTK API for this is?<br>
> ><br>
> > Thanks in advance,<br>
> > Elvis<br></p></div></div></blockquote></div></div></div></div></div></div>
</blockquote></div><br></div></div>