Seg3D  2.4
Seg3D is a free volume segmentation and processing tool developed by the NIH Center for Integrative Biomedical Computing at the University of Utah Scientific Computing and Imaging (SCI) Institute.
the_grid_transform.hxx
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2016 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 // File : the_grid_transform.hxx
30 // Author : Pavel A. Koshevoy
31 // Created : Thu Nov 30 10:45:47 MST 2006
32 // Copyright : (C) 2004-2008 University of Utah
33 // Description : A discontinuous transform -- a uniform grid of vertices is
34 // mapped to an image. At each vertex, in addition to image
35 // space coordinates, a second set of coordinates is stored.
36 // This is similar to texture mapped OpenGL triangle meshes,
37 // where the texture coordinates correspond to the image space
38 // vertex coordinates.
39 
40 #ifndef THE_GRID_TRANSFORM_HXX_
41 #define THE_GRID_TRANSFORM_HXX_
42 
43 // local includes:
44 #include <Core/ITKCommon/common.hxx>
45 
46 // system includes:
47 #include <vector>
48 #include <list>
49 
50 
51 //----------------------------------------------------------------
52 // vertex_t
53 //
54 class vertex_t
55 {
56 public:
57  // normalized tile space coordinates, typically [0, 1] x [0, 1]:
58  pnt2d_t uv_;
59 
60  // physical space coordinates:
61  pnt2d_t xy_;
62 };
63 
64 //----------------------------------------------------------------
65 // triangle_t
66 //
67 class triangle_t
68 {
69 public:
70  triangle_t();
71 
72  // check whether a given xy-point falls within this triangle,
73  // return corresponding uv-point (not triangle barycentric coordinates):
74  bool xy_intersect(const vertex_t * v_arr,
75  const pnt2d_t & xy,
76  pnt2d_t & uv) const;
77 
78  // check whether a given uv-point falls within this triangle,
79  // return corresponding xy-point (not triangle barycentric coordinates):
80  bool uv_intersect(const vertex_t * v_arr,
81  const pnt2d_t & uv,
82  pnt2d_t & xy) const;
83 
84  // triangle vertex indices, counterclockwise winding:
85  unsigned int vertex_[3];
86 
87  // precomputed fast barycentric coordinate calculation coefficients:
88 
89  // for intersection calculation in xy-space:
90  double xy_pwb[3];
91  double xy_pwc[3];
92 
93  // for intersection calculation in uv-space:
94  double uv_pwb[3];
95  double uv_pwc[3];
96 };
97 
98 //----------------------------------------------------------------
99 // the_acceleration_grid_t
100 //
101 // The bounding grid triangle/point intersection acceleration
102 // structure used to speed up grid transform and mesh transform:
103 //
105 {
106 public:
108 
109  // find the grid cell containing a given xy-point:
110  unsigned int xy_cell(const pnt2d_t & xy) const;
111 
112  // find the triangle containing a given xy-point,
113  // and calculate corresponding uv-point:
114  unsigned int xy_triangle(const pnt2d_t & xy, pnt2d_t & uv) const;
115 
116  // find the grid cell containing a given uv-point:
117  unsigned int uv_cell(const pnt2d_t & uv) const;
118 
119  // find the triangle containing a given uv-point,
120  // and calculate corresponding xy-point:
121  unsigned int uv_triangle(const pnt2d_t & uv, pnt2d_t & xy) const;
122 
123  // update the vertex xy coordinates and rebuild the grid
124  void update(const vec2d_t * xy_shift);
125  void shift(const vec2d_t & xy_shift);
126 
127  // resize the grid:
128  void resize(unsigned int rows,
129  unsigned int cols);
130 
131  // rebuild the acceleration grid:
132  void rebuild();
133 
134 private:
135  // helper used to rebuild the grid:
136  void update_grid(unsigned int t_idx);
137 
138 public:
139  // the acceleration structure:
140  std::vector<std::list<unsigned int> > xy_;
141  std::vector<std::list<unsigned int> > uv_;
142  unsigned int rows_;
143  unsigned int cols_;
144 
145  // the grid bounding box (in xy-space):
146  pnt2d_t xy_min_;
147  vec2d_t xy_ext_;
148 
149  // the triangle mesh:
150  std::vector<vertex_t> mesh_;
151  std::vector<triangle_t> tri_;
152 };
153 
154 
155 //----------------------------------------------------------------
156 // the_base_triangle_transform_t
157 //
159 {
160 public:
161  // transform the point:
162  bool transform(const pnt2d_t & xy, pnt2d_t & uv) const;
163 
164  // inverse transform the point:
165  bool transform_inv(const pnt2d_t & uv, pnt2d_t & xy) const;
166 
167  // calculate the derivatives of the transforms with respect to
168  // transform parameters:
169  bool jacobian(const pnt2d_t & xy, unsigned int * idx, double * jac) const;
170 
171 public:
172  // tile bounding box:
173  pnt2d_t tile_min_;
174  vec2d_t tile_ext_;
175 
176  // the acceleration grid (stores triangle vertices, and triangles):
178 };
179 
180 
181 //----------------------------------------------------------------
182 // the_grid_transform_t
183 //
185 {
186 public:
188 
189  // check to see whether the transform has already been setup:
190  bool is_ready() const;
191 
192  // vertex accessors:
193  inline const vertex_t & vertex(size_t row, size_t col) const
194  { return grid_.mesh_[row * (cols_ + 1) + col]; }
195 
196  inline vertex_t & vertex(size_t row, size_t col)
197  { return grid_.mesh_[row * (cols_ + 1) + col]; }
198 
199  // inverse transform the point:
200  bool transform_inv(const pnt2d_t & uv, pnt2d_t & xy) const;
201 
202  // setup the transform:
203  void setup(unsigned int rows,
204  unsigned int cols,
205  const pnt2d_t & tile_min,
206  const pnt2d_t & tile_max,
207  const std::vector<pnt2d_t> & xy);
208 
209 private:
210  // helper used to setup the triangle mesh:
211  void setup_mesh();
212 
213 public:
214  // number of rows and columns of quads in the mesh
215  // (each quad is made up of 2 triangles):
216  size_t rows_;
217  size_t cols_;
218 };
219 
220 
221 //----------------------------------------------------------------
222 // the_mesh_transform_t
223 //
225 {
226 public:
227  // check to see whether the transform has already been setup:
228  bool is_ready() const;
229 
230  // setup the transform:
231  bool setup(const pnt2d_t & tile_min,
232  const pnt2d_t & tile_max,
233  const std::vector<pnt2d_t> & uv,
234  const std::vector<pnt2d_t> & xy,
235  unsigned int accel_grid_rows = 16,
236  unsigned int accel_grid_cols = 16);
237 
238  // insert a point into the mesh, and re-triangulate
239  // using Delaunay triangulation:
240  bool insert_point(const pnt2d_t & uv,
241  const pnt2d_t & xy,
242  const bool delay_setup = false);
243 
244  // insert a point into the mesh (xy-point is extrapolated),
245  // and re-triangulate using Delaunay triangulation:
246  bool insert_point(const pnt2d_t & uv);
247 
248 private:
249  // helper used to setup the triangle mesh:
250  bool setup_mesh();
251 };
252 
253 
254 #endif // THE_GRID_TRANSFORM_HXX_
Definition: itkDiscontinuousTransform.h:106
Definition: itkDiscontinuousTransform.h:160
Definition: itkDiscontinuousTransform.h:69
Definition: itkDiscontinuousTransform.h:186
Definition: itkDiscontinuousTransform.h:226
Definition: itkDiscontinuousTransform.h:56