fw4spl
MeshFunctions.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWMATH_MESHFUNCTIONS_HPP__
8 #define __FWMATH_MESHFUNCTIONS_HPP__
9 
10 #include "fwMath/IntrasecTypes.hpp"
11 #include "fwMath/config.hpp"
12 
13 #include <fwCore/base.hpp>
14 
15 #include <boost/unordered_map.hpp>
16 #include <cmath>
17 #include <utility>
18 #include <vector>
19 
20 namespace fwMath
21 {
22 
26 FWMATH_API bool IsInclosedVolume(const fwVertexPosition &_vertex, const fwVertexIndex &_vertexIndex,
27  const fwVec3d &_p );
28 
38 FWMATH_API bool intersect_triangle(fwVec3d _orig, fwVec3d _dir, fwVec3d _vert0, fwVec3d _vert1, fwVec3d _vert2,
39  double &_t, double &_u, double &_v);
40 
42 FWMATH_API bool isBorderlessSurface( const fwVertexIndex &_vertexIndex);
43 
44 FWMATH_API void findBorderEdges( const fwVertexIndex &_vertexIndex, std::vector< std::vector< std::pair< int,
45  int > > > &contours);
46 
51 FWMATH_API bool closeSurface( fwVertexPosition &_vertex, fwVertexIndex &_vertexIndex );
52 
56 FWMATH_API bool removeOrphanVertices( fwVertexPosition &_vertex, fwVertexIndex &_vertexIndex );
57 
58 //-----------------------------------------------------------------------------
59 
60 template <typename T, typename U>
61 std::pair< T, U > makeOrderedPair(const T first, const U second)
62 {
63  if (first < second)
64  {
65  return std::pair< T, U >(first, second);
66  }
67  else
68  {
69  return std::pair< T, U >(second, first);
70  }
71 }
72 
73 //-----------------------------------------------------------------------------
74 
75 template <typename T, typename U, typename V>
76 bool isBorderlessSurface(T* cellDataBegin, T* cellDataEnd, U* cellDataOffsetsBegin, U* cellDataOffsetsEnd,
77  V* cellTypesBegin)
78 {
79  typedef std::pair< T, T > Edge; // always Edge.first < Edge.second !!
80  typedef ::boost::unordered_map< Edge, int > EdgeHistogram;
81  EdgeHistogram edgesHistogram;
82  bool isBorderless = true;
83 
84  size_t dataLen = 0;
85  U* iter = cellDataOffsetsBegin;
86  U* iter2 = cellDataOffsetsBegin + 1;
87  const U* iterEnd = cellDataOffsetsEnd - 1;
88  V* iterTypes = cellTypesBegin;
89 
90  dataLen = *iter2 - *iter;
91  for (
92  ;
93  iter < iterEnd || ( iter < cellDataOffsetsEnd && (dataLen = (cellDataEnd - cellDataBegin) - *iter) );
94  dataLen = *++iter2 - *++iter, ++iterTypes
95  )
96  {
97  if(*iterTypes == 0)
98  {
99  continue;
100  }
101  T* iterCell = cellDataBegin + *iter;
102  T* iterCell2 = iterCell + 1;
103  T* beginCell = iterCell;
104  const T* iterCellEnd = beginCell + dataLen - 1;
105  for (
106  ;
107  iterCell < iterCellEnd || ( iterCell < (beginCell + dataLen) && (iterCell2 = beginCell) );
108  ++iterCell, ++iterCell2
109  )
110  {
111  ++edgesHistogram[makeOrderedPair(*iterCell, *(iterCell2))];
112  }
113  }
114 
115  for(const typename EdgeHistogram::value_type &histo : edgesHistogram)
116  {
117  if (histo.second != 2)
118  {
119  isBorderless = false;
120  break;
121  }
122  }
123 
124  return isBorderless;
125 }
126 
127 }
128 
129 #endif /* __FWMATH_MESHFUNCTIONS_HPP__ */
FWMATH_API bool removeOrphanVertices(fwVertexPosition &_vertex, fwVertexIndex &_vertexIndex)
FWMATH_API bool closeSurface(fwVertexPosition &_vertex, fwVertexIndex &_vertexIndex)
Closes the surface if necessary.
FWMATH_API bool intersect_triangle(fwVec3d _orig, fwVec3d _dir, fwVec3d _vert0, fwVec3d _vert1, fwVec3d _vert2, double &_t, double &_u, double &_v)
Compute the intersection between triangle(define by threes vertex vert1, vert2, vert3) and the Oz par...
The namespace fwMath contains classes which provide the implementation of several mathematic function...
Definition: Compare.hpp:12
FWMATH_API bool isBorderlessSurface(const fwVertexIndex &_vertexIndex)
test whatever a vertex is duplicated or not