UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
u3d_triangled_abstract_surface.hpp
1 // u3d_triangled_abstract_surface.hpp
2 // $Id: u3d_triangled_abstract_surface.hpp,v 1.3 2008/06/04 12:22:37 sad Exp $
3 
4 #ifndef U3D_TRIANGLED_ABSTRACT_SURFACE_HPP_
5 #define U3D_TRIANGLED_ABSTRACT_SURFACE_HPP_
6 
7 #include <mth/u3d_point.hpp>
8 #include <mth/u3d_triangle.hpp>
9 #include "mth/u3d_math_abstract_surface.hpp"
10 #include "mth/u3d_triangled_abstract_surface.hpp"
11 
12 #include <list>
13 #include <map>
14 #include <stdexcept>
15 #include <set>
16 
17 
18 
19 #define U3D_MIN_TRIANGLE_SIZE 50 //U3D_EPS_LENGTH * 10.;
20 
21 
22 enum U3D_COLOR {WHITE, BLACK};
23 
24 
25 
26 
27 //============================================================================
32 {
33 public:
34 
36 
38 
39  virtual ~U3dTriangledAbstractSurface();
40 
41  virtual void addPoint(U3dPoint*) = 0;
42  virtual bool removePoint(int pointId) = 0;
43  virtual void movePoint(int pointId, double x, double y, double z) = 0;
44 
45  virtual class U3dBox getBox() const = 0;
46 
47 
49 #if 0
50  U3dTriangle* getTriangle(int id) { return ((id < 0) || (id >= getTrianglesNum())) ? (NULL) : (m_triangles[id]); }
51 #else
52  const U3dTriangle* getTriangle(int id) const throw(std::out_of_range) { return m_triangles.at(id); }
53 #endif
54 
55 
56 
57 #if 0
58  bool getTriangle(int id, U3dTriangle *&triangle)
59  {
60  if((id < 0) || (id >= getTrianglesNum())) return false;
61  else { triangle = m_triangles[id]; return true; }
62  }
63 #endif
64 
65 
66 
68  int getTrianglesNum() const { return m_triangles.size(); }
69 
71  void severance(U3dTriangledAbstractSurface* other, const U3D_COLOR&);
72 
73  void severance_old(const U3dTriangledAbstractSurface* other, const U3D_COLOR&);
74 
76  void setWhitePoint(const U3dPoint& p) { m_white_point = p; }
77 
79  U3dPoint getWhitePoint() const { return m_white_point; }
80 
81 
84 #if 0
85  bool getPoint(int id, U3dPoint &point)
86  {
87  if (id < 0 || id >= getPointsNum()) return false;
88  else { point = U3dPoint(*m_tps[id]); return true; }
89  }
90 #else
91  const U3dPoint* getPoint(int id) const throw(std::out_of_range) { return m_tps.at(id); }
92  U3dPoint* getPoint(int id) throw(std::out_of_range) { return m_tps.at(id); }
93 #endif
94 
97  bool getPoint(int id, double &x, double &y, double &z)
98  {
99  if (id < 0 || id >= getPointsNum()) return false;
100  else {
101  x = m_tps[id]->getX();
102  y = m_tps[id]->getY();
103  z = m_tps[id]->getZ();
104  return true;
105  }
106  }
107 
109  int getPointsNum() { return m_tps.size(); }
110 
112  void getPoints(U3dPointsAr &ps)
113  { ps = m_tps; }
114 
115 
116 
118  void getInitialPoints(U3dPointsAr &ps) {
119  ps = m_initPoints;
120  }
121 
123  const U3dPointsAr& getInitialPoints() const {
124  return m_initPoints;
125  }
126 
127 
133  virtual bool getSideColor(const U3dPoint& p, U3D_COLOR& u3dcolor) const;
134 
136  virtual void getIntersections(U3dPoint p0, U3dPoint p1, std::vector<int> &int_trIds, U3dPointsAr &int_points);
137 
140 
141 
143  const std::set<int>& getActTrNums() { return m_actTrNums; }
144 
145  U3dMathAbstractSurface* getMathSurf() { return m_math_surf; }
146 
147 protected:
148 
149  // virtual void triangulate() { assert(); };
150  virtual void calculateTrPlanes();
151 
152  U3dMathAbstractSurface* m_math_surf;
153 
154  U3dPointsAr m_tps;
155  U3dTrianglesAr m_triangles;
156 
157  U3dPointsAr m_initPoints;
158 
159  std::set<int> m_actTrNums;
160 
161 
164 
165  // Point for white/black side determination
166  U3dPoint m_white_point;
167 
168 
169 
170  // Class for triangulation of set of points in one plane.
171  // (Used only in U3dTriangledAbstractSurface::severance()).
173 
174  private:
175  void triangulate();
176 
177  class My2DPoint {
178  public:
179  My2DPoint() {};
180  My2DPoint(double ix, double iy) : x(ix), y(iy) {};
181  double distance(const My2DPoint& other) const;
182  double getX() const { return x; };
183  double getY() const { return y; };
184  private:
185  double x, y;
186  };
187 
188  class MyHVector {
189  public:
190  MyHVector() {};
191  MyHVector(double aa, double bb, My2DPoint point) : a(aa), b(bb), p(point) {};
192  My2DPoint intersect(const MyHVector& v) const throw(std::overflow_error);
193  double getA() const { return a; };
194  double getB() const { return b; };
195  double getX() const { return p.getX(); };
196  double getY() const { return p.getY(); };
197  const My2DPoint& getP() const { return p; };
198  private:
199  double a, b;
200  My2DPoint p;
201  };
202 
203 
204  class Edge {
205  public:
206  Edge(My2DPoint* s, My2DPoint* d);
207  // Edge(const Edge&);
208  bool isPointOnRight(const My2DPoint& p) const;
209  const MyHVector& getPerpendicular() const { return hvec; };
210  bool operator==(const Edge& other) const;
211  My2DPoint* source() const { return sorc; };
212  My2DPoint* dest() const { return dst; };
213  double getLength() { return sorc->distance(*dst); };
214  private:
215  My2DPoint *sorc, *dst;
216  MyHVector hvec;
217  double a1, b1;
218  };
219 
220 
221  double vectOP[3], vectOQ[3];
222  double lengthOP, lengthOQ;
223  double pzero[3]; // begining of 2d cartezians coordinates
224  typedef std::map< My2DPoint*, unsigned> T_2d_3d_Assigns;
225  T_2d_3d_Assigns assigns;
226  std::vector<U3dTriangle*> my_triangles;
227 
228  typedef std::list<Edge> Front;
229  Front front;
230 
231  void addNewEdge(const Edge& edge);
232  const Edge* getNextEdge();
233  Front::iterator itrNextEdge;
234 
235  My2DPoint* vertexes[3];
236 
237  void init(const U3dPoint& p0, const U3dPoint& p1, double A, double B, double C);
238 
239  bool checkVertexes;
240 
241  public:
242  Delone2DTriangulator(double A, double B, double C, int ind0, const U3dPoint* p0, int ind1, const U3dPoint* p1, int ind2, const U3dPoint* p2);
243  Delone2DTriangulator(const U3dPoint& p0, const U3dPoint& p1, double A, double B, double C);
244  My2DPoint* add3DPoint(unsigned index, const U3dPoint* p);
245  const std::vector<U3dTriangle*>& generateU3dTriangles() { triangulate(); return my_triangles; }
247  };
248 
249 
250 
251 private:
252  // Special struct for getSideColor(). Keeping information about intersection.
253  friend class Intersect;
254  class Intersect {
255  public:
256  Intersect(const U3dTriangledAbstractSurface* ps,
257  const int& tr_num /*Index of triangle*/,
258  const double& mx, const double& my, const double& mz);
259 
260  /* Include information about intersection with other triangle if point is the same. */
261  bool incIfEqual(Intersect& other);
262  const std::vector<int>& triangles() const { return m_triangs; };
263  const double& getX() const { return x; };
264  const double& getY() const { return y; };
265  const double& getZ() const { return z; };
266  const int& ind1() const { return index1; };
267  const int& ind2() const { return ind2vec.front(); };
268  const int& ind3() const { return ind3vec.front(); };
269  /* Returns other vertex from given (but not vertex with intersection) */
270  bool find(const double& xin, const double& yin, const double& zin,
271  double& xother, double& yother, double& zother);
272  private:
273  double x, y, z; // Coordinates of intersection
274  int index1; // Index of vertex (For cases, when we shot in vertex of triangle)
275  const U3dTriangledAbstractSurface* surf;
276  std::list<int> ind2vec, ind3vec; // indexes of 2 and 3 other vertexes.
277  std::vector<int> m_triangs; // Indexes of all triangles of this intersections
278  };
279 
280 
281 
282 
283  // Struct for keeping number of segment that was checked already
284  // Placed here because of using in set<>
285  struct SegmentChecked {
286  int ind1, ind2; // indexes of points
287  bool operator< (const SegmentChecked& other) const {
288  if (ind1 < other.ind1) return true;
289  if (ind1 > other.ind1) return false;
290  return ind2 < other.ind2;
291  };
292 
293  mutable U3dPoint* point;
294  };
295 
296 
297 
298 
299  /* If point [x,y,z] is near to one of trianlge's vertexes,
300  this function gives index of this vertex in global points array and
301  returns number of this point in triangle (1, 2 or 3).
302  If [x,y,z] is not near to any vertex, index sets to -1, and function returns 0*/
303  int getTrPointIndex(const int& trnum, const double& x, const double& y,
304  const double& z, int& index) const;
305 
306 
307 };
308 
309 
310 
311 
312 
313 #endif // U3D_TRIANGLED_ABSTRACT_SURFACE_HPP_
const U3dPoint * getPoint(int id) const
Definition: u3d_triangled_abstract_surface.hpp:91
const U3dTriangle * getTriangle(int id) const
Definition: u3d_triangled_abstract_surface.hpp:52
Definition: u3d_math_abstract_surface.hpp:11
void getInitialPoints(U3dPointsAr &ps)
Definition: u3d_triangled_abstract_surface.hpp:118
Definition: u3d_triangled_abstract_surface.hpp:31
virtual void getIntersections(U3dPoint p0, U3dPoint p1, std::vector< int > &int_trIds, U3dPointsAr &int_points)
bool getPoint(int id, double &x, double &y, double &z)
Definition: u3d_triangled_abstract_surface.hpp:97
Definition: u3d_box.hpp:24
int getTrianglesNum() const
Definition: u3d_triangled_abstract_surface.hpp:68
Definition: u3d_triangle.hpp:18
Definition: geometry.H:16
bool isTriangulated()
Definition: u3d_triangled_abstract_surface.hpp:139
const U3dPointsAr & getInitialPoints() const
Definition: u3d_triangled_abstract_surface.hpp:123
void severance(U3dTriangledAbstractSurface *other, const U3D_COLOR &)
virtual bool getSideColor(const U3dPoint &p, U3D_COLOR &u3dcolor) const
void setWhitePoint(const U3dPoint &p)
Definition: u3d_triangled_abstract_surface.hpp:76
Definition: u3d_point.hpp:16
int getPointsNum()
Definition: u3d_triangled_abstract_surface.hpp:109
U3dPoint getWhitePoint() const
Definition: u3d_triangled_abstract_surface.hpp:79
bool m_is_triangulated
Definition: u3d_triangled_abstract_surface.hpp:163
void getPoints(U3dPointsAr &ps)
Definition: u3d_triangled_abstract_surface.hpp:112
Definition: u3d_triangled_abstract_surface.hpp:172
const std::set< int > & getActTrNums()
Definition: u3d_triangled_abstract_surface.hpp:143