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.
itkDiscontinuousTransform.h
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 
30 // File : the_grid_transform.hxx
31 // Author : Pavel A. Koshevoy
32 // Created : Thu Nov 30 10:45:47 MST 2006
33 // Copyright : (C) 2004-2008 University of Utah
34 // Description : A discontinuous transform -- a uniform grid of vertices is
35 // mapped to an image. At each vertex, in addition to image
36 // space coordinates, a second set of coordinates is stored.
37 // This is similar to texture mapped OpenGL triangle meshes,
38 // where the texture coordinates correspond to the image space
39 // vertex coordinates.
40 
41 #ifndef THE_GRID_TRANSFORM_HXX_
42 #define THE_GRID_TRANSFORM_HXX_
43 
44 
45 // local includes:
46 #include <Core/ITKCommon/common.hxx>
47 
48 // system includes:
49 #include <vector>
50 #include <list>
51 
52 
53 //----------------------------------------------------------------
54 // vertex_t
55 //
56 class vertex_t
57 {
58 public:
59  // normalized tile space coordinates, typically [0, 1] x [0, 1]:
60  pnt2d_t uv_;
61 
62  // physical space coordinates:
63  pnt2d_t xy_;
64 };
65 
66 //----------------------------------------------------------------
67 // triangle_t
68 //
70 {
71 public:
72  triangle_t();
73 
74  // check whether a given xy-point falls within this triangle,
75  // return corresponding uv-point (not triangle barycentric coordinates):
76  bool xy_intersect(const vertex_t * v_arr,
77  const pnt2d_t & xy,
78  pnt2d_t & uv) const;
79 
80  // check whether a given uv-point falls within this triangle,
81  // return corresponding xy-point (not triangle barycentric coordinates):
82  bool uv_intersect(const vertex_t * v_arr,
83  const pnt2d_t & uv,
84  pnt2d_t & xy) const;
85 
86  // triangle vertex indices, counterclockwise winding:
87  unsigned int vertex_[3];
88 
89  // precomputed fast barycentric coordinate calculation coefficients:
90 
91  // for intersection calculation in xy-space:
92  double xy_pwb[3];
93  double xy_pwc[3];
94 
95  // for intersection calculation in uv-space:
96  double uv_pwb[3];
97  double uv_pwc[3];
98 };
99 
100 //----------------------------------------------------------------
101 // the_acceleration_grid_t
102 //
103 // The bounding grid triangle/point intersection acceleration
104 // structure used to speed up grid transform and mesh transform:
105 //
107 {
108 public:
110 
111  // find the grid cell containing a given xy-point:
112  unsigned int xy_cell(const pnt2d_t & xy) const;
113 
114  // find the triangle containing a given xy-point,
115  // and calculate corresponding uv-point:
116  unsigned int xy_triangle(const pnt2d_t & xy, pnt2d_t & uv) const;
117 
118  // find the grid cell containing a given uv-point:
119  unsigned int uv_cell(const pnt2d_t & uv) const;
120 
121  // find the triangle containing a given uv-point,
122  // and calculate corresponding xy-point:
123  unsigned int uv_triangle(const pnt2d_t & uv, pnt2d_t & xy) const;
124 
125  // update the vertex xy coordinates and rebuild the grid
126  void update(const vec2d_t * xy_shift);
127  void shift(const vec2d_t & xy_shift);
128 
129  // resize the grid:
130  void resize(unsigned int rows,
131  unsigned int cols);
132 
133  // rebuild the acceleration grid:
134  void rebuild();
135 
136 private:
137  // helper used to rebuild the grid:
138  void update_grid(unsigned int t_idx);
139 
140 public:
141  // the acceleration structure:
142  std::vector<std::list<unsigned int> > xy_;
143  std::vector<std::list<unsigned int> > uv_;
144  unsigned int rows_;
145  unsigned int cols_;
146 
147  // the grid bounding box (in xy-space):
148  pnt2d_t xy_min_;
149  vec2d_t xy_ext_;
150 
151  // the triangle mesh:
152  std::vector<vertex_t> mesh_;
153  std::vector<triangle_t> tri_;
154 };
155 
156 
157 //----------------------------------------------------------------
158 // the_base_triangle_transform_t
159 //
161 {
162 public:
163  // transform the point:
164  bool transform(const pnt2d_t & xy, pnt2d_t & uv) const;
165 
166  // inverse transform the point:
167  bool transform_inv(const pnt2d_t & uv, pnt2d_t & xy) const;
168 
169  // calculate the derivatives of the transforms with respect to
170  // transform parameters:
171  bool jacobian(const pnt2d_t & xy, unsigned int * idx, double * jac) const;
172 
173 public:
174  // tile bounding box:
175  pnt2d_t tile_min_;
176  vec2d_t tile_ext_;
177 
178  // the acceleration grid (stores triangle vertices, and triangles):
180 };
181 
182 
183 //----------------------------------------------------------------
184 // the_grid_transform_t
185 //
187 {
188 public:
190 
191  // check to see whether the transform has already been setup:
192  bool is_ready() const;
193 
194  // vertex accessors:
195  inline const vertex_t & vertex(size_t row, size_t col) const
196  { return grid_.mesh_[row * (cols_ + 1) + col]; }
197 
198  inline vertex_t & vertex(size_t row, size_t col)
199  { return grid_.mesh_[row * (cols_ + 1) + col]; }
200 
201  // inverse transform the point:
202  bool transform_inv(const pnt2d_t & uv, pnt2d_t & xy) const;
203 
204  // setup the transform:
205  void setup(unsigned int rows,
206  unsigned int cols,
207  const pnt2d_t & tile_min,
208  const pnt2d_t & tile_max,
209  const std::vector<pnt2d_t> & xy);
210 
211 private:
212  // helper used to setup the triangle mesh:
213  void setup_mesh();
214 
215 public:
216  // number of rows and columns of quads in the mesh
217  // (each quad is made up of 2 triangles):
218  size_t rows_;
219  size_t cols_;
220 };
221 
222 
223 //----------------------------------------------------------------
224 // the_mesh_transform_t
225 //
227 {
228 public:
229  // check to see whether the transform has already been setup:
230  bool is_ready() const;
231 
232  // setup the transform:
233  bool setup(const pnt2d_t & tile_min,
234  const pnt2d_t & tile_max,
235  const std::vector<pnt2d_t> & uv,
236  const std::vector<pnt2d_t> & xy,
237  unsigned int accel_grid_rows = 16,
238  unsigned int accel_grid_cols = 16);
239 
240  // insert a point into the mesh, and re-triangulate
241  // using Delaunay triangulation:
242  bool insert_point(const pnt2d_t & uv,
243  const pnt2d_t & xy,
244  const bool delay_setup = false);
245 
246  // insert a point into the mesh (xy-point is extrapolated),
247  // and re-triangulate using Delaunay triangulation:
248  bool insert_point(const pnt2d_t & uv);
249 
250 private:
251  // helper used to setup the triangle mesh:
252  bool setup_mesh();
253 };
254 
255 
256 #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