IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
MeshSimplification.h
1 // MeshSimplification.h:
3 // ---------
4 //
14 
15 #ifndef __IPSDKGEOMETRY_MESHSIMPLIFICATION_H__
16 #define __IPSDKGEOMETRY_MESHSIMPLIFICATION_H__
17 
18 // suppression warnings
19 // warning C4251: 'ipsdk::geom::Mesh3d<T>::_mesh3dType': class 'ipsdk::geom::eMesh3dType' needs to have dll-interface to be used by clients of class 'ipsdk::geom::Mesh3d<T>'
20 #pragma warning (push)
21 #pragma warning (disable : 4251)
22 
23 #include <IPSDKGeometry/Entity/3d/BaseTypedGeometryEntity3d.h>
24 #include <IPSDKGeometry/Entity/3d/Mesh/Mesh3d.h>
25 #include <IPSDKGeometry/Entity/3d/Mesh/SymmetricMatrix.h>
26 
27 namespace ipsdk {
28 namespace geom {
29 
30 
33 
34 template <typename T>
36 {
37 
38 
39 public:
45 
46  // Types
47  typedef ipUInt64* FaceData;
48 
49  typedef FaceData* FaceDataArray;
50 
51  typedef ipReal64* VertexData;
52 
53  typedef VertexData* VertexDataArray;
54 
55 
56 // Structures
57 protected:
58 
59  struct vector3
60  {
61  double x, y, z;
62  };
63 
64  struct vec3f
65  {
66  double x, y, z;
67 
68  inline vec3f(void) {}
69 
70  //inline vec3f operator =( vector3 a )
71  // { vec3f b ; b.x = a.x; b.y = a.y; b.z = a.z; return b;}
72 
73  inline vec3f(vector3 a)
74  {
75  x = a.x; y = a.y; z = a.z;
76  }
77 
78  inline vec3f(const double X, const double Y, const double Z)
79  {
80  x = X; y = Y; z = Z;
81  }
82 
83  inline vec3f operator + (const vec3f& a) const
84  {
85  return vec3f(x + a.x, y + a.y, z + a.z);
86  }
87 
88  inline vec3f operator += (const vec3f& a) const
89  {
90  return vec3f(x + a.x, y + a.y, z + a.z);
91  }
92 
93  inline vec3f operator * (const double a) const
94  {
95  return vec3f(x * a, y * a, z * a);
96  }
97 
98  inline vec3f operator * (const vec3f a) const
99  {
100  return vec3f(x * a.x, y * a.y, z * a.z);
101  }
102 
103  inline vec3f v3() const
104  {
105  return vec3f(x, y, z);
106  }
107 
108  inline vec3f operator = (const vector3 a)
109  {
110  x = a.x; y = a.y; z = a.z; return *this;
111  }
112 
113  inline vec3f operator = (const vec3f a)
114  {
115  x = a.x; y = a.y; z = a.z; return *this;
116  }
117 
118  inline vec3f operator / (const vec3f a) const
119  {
120  return vec3f(x / a.x, y / a.y, z / a.z);
121  }
122 
123  inline vec3f operator - (const vec3f& a) const
124  {
125  return vec3f(x - a.x, y - a.y, z - a.z);
126  }
127 
128  inline vec3f operator / (const double a) const
129  {
130  return vec3f(x / a, y / a, z / a);
131  }
132 
133  inline double dot(const vec3f& a) const
134  {
135  return a.x * x + a.y * y + a.z * z;
136  }
137 
138  inline vec3f cross(const vec3f& a, const vec3f& b)
139  {
140  x = a.y * b.z - a.z * b.y;
141  y = a.z * b.x - a.x * b.z;
142  z = a.x * b.y - a.y * b.x;
143  return *this;
144  }
145 
146  inline double angle(const vec3f& v)
147  {
148  vec3f a = v, b = *this;
149  double dot = v.x * x + v.y * y + v.z * z;
150  double len = a.length() * b.length();
151  if (len == 0)len = 0.00001f;
152  double input = dot / len;
153  if (input < -1) input = -1;
154  if (input > 1) input = 1;
155  return (double)acos(input);
156  }
157 
158  inline double angle2(const vec3f& v, const vec3f& w)
159  {
160  vec3f a = v, b = *this;
161  double dot = a.x * b.x + a.y * b.y + a.z * b.z;
162  double len = a.length() * b.length();
163  if (len == 0)len = 1;
164 
165  vec3f plane; plane.cross(b, w);
166 
167  if (plane.x * a.x + plane.y * a.y + plane.z * a.z > 0)
168  return (double)-acos(dot / len);
169 
170  return (double)acos(dot / len);
171  }
172 
173  inline vec3f rot_x(double a)
174  {
175  double yy = cos(a) * y + sin(a) * z;
176  double zz = cos(a) * z - sin(a) * y;
177  y = yy; z = zz;
178  return *this;
179  }
180  inline vec3f rot_y(double a)
181  {
182  double xx = cos(-a) * x + sin(-a) * z;
183  double zz = cos(-a) * z - sin(-a) * x;
184  x = xx; z = zz;
185  return *this;
186  }
187  inline void clamp(double min, double max)
188  {
189  if (x < min) x = min;
190  if (y < min) y = min;
191  if (z < min) z = min;
192  if (x > max) x = max;
193  if (y > max) y = max;
194  if (z > max) z = max;
195  }
196  inline vec3f rot_z(double a)
197  {
198  double yy = cos(a) * y + sin(a) * x;
199  double xx = cos(a) * x - sin(a) * y;
200  y = yy; x = xx;
201  return *this;
202  }
203  inline vec3f invert()
204  {
205  x = -x; y = -y; z = -z; return *this;
206  }
207  inline vec3f frac()
208  {
209  return vec3f(
210  x - double(int(x)),
211  y - double(int(y)),
212  z - double(int(z))
213  );
214  }
215 
216  inline vec3f integer()
217  {
218  return vec3f(
219  double(int(x)),
220  double(int(y)),
221  double(int(z))
222  );
223  }
224 
225  inline double length() const
226  {
227  return (double)sqrt(x * x + y * y + z * z);
228  }
229 
230  inline vec3f normalize(double desired_length = 1)
231  {
232  double square = sqrt(x * x + y * y + z * z);
233 
234  x /= square; y /= square; z /= square;
235 
236  return *this;
237  }
238 
239  //static vec3f normalize(vec3f a);
240 
241  //static vec3f random();
242 
243  //static void random_init();
244 
245  //static double random_double();
246 
247  //static int random_number;
248 
249  double random_double_01(double a) {
250  double rnf = a * 14.434252 + a * 364.2343 + a * 4213.45352 + a * 2341.43255 + a * 254341.43535 + a * 223454341.3523534245 + 23453.423412;
251  int rni = ((int)rnf) % 100000;
252  return double(rni) / (100000.0f - 1.0f);
253  }
254 
255  vec3f random01_fxyz() {
256  x = (double)random_double_01(x);
257  y = (double)random_double_01(y);
258  z = (double)random_double_01(z);
259  return *this;
260  }
261  };
262 
263 
264  struct Triangle {
265 
266  ipUInt32 _v[3]; //Triangle indices
267 
268  ipReal64 _err[4]; //Edge error and triangle vertices errors
269 
270  ipUInt32 _deleted;
271 
272  ipUInt32 _dirty; //An edge is marked dirty if one of his neighbors edge is collapsed,
273  //And put in a queue to be processed when at the top of the queue.
274 
275  vec3f _n; //Face normal
276  };
277 
278  struct Vertex {
279 
280  vec3f _p;
281 
282  ipUInt64 _tstart;
283 
284  ipUInt64 _tcount; //Number of neighbouring triangles to the vertex
285 
286  SymmetricMatrix _q; //Quadric matrix error
287 
288  ipUInt64 _border;
289  };
290 
291  struct Ref {
292 
293  ipUInt64 _tid; //index of triangle
294 
295  ipUInt64 _tvertex; //index of one of the bounding vertices of the triangle
296  };
297 
298 
299 // methods
300 protected:
301 
303  ipReal64 min(const ipReal64 v1,
304  const ipReal64 v2);
305 
307  void simplification(const ipUInt64 target_count,
308  const ipReal64 agressiveness = 7,
309  const ipBool bVerbose = false);
310 
312  const ipBool flipped(const vec3f& p,
313  const ipUInt32 i0,
314  const ipUInt32 i1,
315  const Vertex& v0,
316  const Vertex& v1,
317  std::vector<ipUInt32>& deleted);
318 
320  void update_triangles(const ipUInt32 i0,
321  const Vertex& v,
322  const std::vector<ipUInt32>& deleted,
323  ipUInt32& deleted_triangles);
324 
326  void update_mesh(const ipUInt32 iteration);
327 
329  void compact_data();
330 
332  void compact_mesh(Mesh3d<T>& mesh);
333 
335  void compact_arrays(const ipReal32* pVertexArray,
336  const ipUInt32* pFaceArray,
337  std::vector<ipReal32>& vVerticesArray,
338  std::vector<ipUInt32>& vFacesArray);
339 
341  ipReal64 vertex_error(const SymmetricMatrix& q,
342  const ipReal64 x,
343  const ipReal64 y,
344  const ipReal64 z);
345 
347  ipReal64 calculate_error(const ipUInt32 id_v1,
348  const ipUInt32 id_v2,
349  vec3f& pResult);
350 
351 //methods
352 public:
353 
355  void simplify_mesh_inSitu(Mesh3d<T>& mesh,
356  const ipUInt64 target_count,
357  const ipReal64 agressiveness = 7,
358  const ipBool bVerbose = false);
359 
361  boost::shared_ptr<Mesh3d<T>> simplify_mesh(const Mesh3d<T>& mesh,
362  const ipUInt64 target_count,
363  const ipReal64 agressiveness = 7,
364  const ipBool bVerbose = false);
365 
366 
368  void simplify_arrays(const ipReal32* pVerticesArray,
369  const ipUInt32* pTrianglesArray,
370  const ipUInt64 nbVertices,
371  const ipUInt64 nbTriangles,
372  const ipUInt64 target_count,
373  const ipReal64 agressiveness,
374  const ipBool bVerbose,
375  std::vector<ipReal32>& vVerticesArray,
376  std::vector<ipUInt32>& vFacesArray);
377 
379  void clear();
380 
381 
382 // attributes
383 protected:
384 
385  //triangles list
386  std::vector<Triangle> _triangles;
387 
388  //vertices list
389  std::vector<Vertex> _vertices;
390 
391  //references list
392  std::vector<Ref> _refs;
393 
394  //edge collapsed list
395  std::vector<std::vector<ipBool>> _collapses;
396 };
397 
400 
403 
404  } // end of namespace geom
405 } // end of namespace ipsdk
406 
407 //#pragma warning (pop)
408 
409 #endif // __IPSDKGEOMETRY_MESHSIMPLIFICATION_H__
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
uint64_t ipUInt64
Base types definition.
Definition: BaseTypes.h:55
IPSDKGEOMETRY_API Point2d< T > operator*(const ipReal64 value, const Point2d< T > &pt)
arithmetic operators on point
IPSDK_FORCEINLINE PackT sqrt(const PackT &in)
returns the square root of a pack
Definition: sqrt.h:40
double ipReal64
Base types definition.
Definition: BaseTypes.h:57
IPSDK_FORCEINLINE PackT max(const PackT &in1, const PackT &in2)
returns the maximum of 2 packs
Definition: max.h:40
Definition: MeshSimplification.h:264
IPSDK_FORCEINLINE PackT min(const PackT &in1, const PackT &in2)
returns the minimum of 2 packs
Definition: min.h:40
#define IPSDKGEOMETRY_API
Import/Export macro for library IPSDKGeometry.
Definition: IPSDKGeometryExports.h:25
Closed mesh associated to a triangulated boundary.
Definition: MeshSimplification.h:35
Definition: MeshSimplification.h:291
bool ipBool
Base types definition.
Definition: BaseTypes.h:47
Definition: MeshSimplification.h:64
IPSDKBASESHAPESEGMENTATION_API void normalize(ipsdk::geom::Shape2dPolygonWithHoles &polygonWithHoles)
function allowing to normalize a polygon to ensure that :
Closed mesh associated to a triangulated boundary.
Definition: Mesh3d.h:43
Definition: MeshSimplification.h:278
Definition: MeshSimplification.h:59
Closed mesh associated to a triangulated boundary.
Definition: SymmetricMatrix.h:51
float ipReal32
Base types definition.
Definition: BaseTypes.h:56
uint32_t ipUInt32
Base types definition.
Definition: BaseTypes.h:53