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