00001
00027 #ifndef CELL_H
00028 #define CELL_H
00029
00030 #include <iostream>
00031 #include <list>
00032
00033 #include "hash_set.h"
00034 #include "hash_classes.h"
00035 #include "iterator_wrapper.h"
00036 #include "globals.h"
00037 #include "persistance.h"
00038 #include "util.h"
00039
00040
00041 class Cell;
00042
00043 class VertexCell;
00044 class EdgeCell;
00045 class FaceCell;
00046
00047 class BoundaryMesh;
00048 class BoundaryVertex;
00049 class BoundaryEdge;
00050 class BoundaryFace;
00051
00052 class BezierMesh;
00053 class BezierVertex;
00054 class BezierEdge;
00055 class BezierTriangle;
00056
00057 class DataStore;
00058 class QBSpline;
00059
00060
00061 extern std::ostream& operator<< (std::ostream &stream, const BoundaryVertex&);
00062 extern std::ostream& operator<< (std::ostream &stream, const BoundaryEdge&);
00063 extern std::ostream& operator<< (std::ostream &stream, const BoundaryFace&);
00064
00065 extern std::ostream& operator<< (std::ostream &stream, const BezierVertex&);
00066 extern std::ostream& operator<< (std::ostream &stream, const BezierEdge&);
00067 extern std::ostream& operator<< (std::ostream &stream, const BezierTriangle&);
00068
00069 #ifndef SWIG
00070
00071 namespace hashers {
00072 template <> class hash<Cell*> : public CastHash<Cell*> { };
00073 template <> class hash<VertexCell*> : public CastHash<VertexCell*> { };
00074 template <> class hash<EdgeCell*> : public CastHash<EdgeCell*> { };
00075 template <> class hash<FaceCell*> : public CastHash<FaceCell*> { };
00076 template <> class hash<BoundaryVertex*> : public CastHash<BoundaryVertex*> { };
00077 template <> class hash<BoundaryEdge*> : public CastHash<BoundaryEdge*> { };
00078 template <> class hash<BoundaryFace*> : public CastHash<BoundaryFace*> { };
00079 template <> class hash<BezierVertex*> : public CastHash<BezierVertex*> { };
00080 template <> class hash<BezierEdge*> : public CastHash<BezierEdge*> { };
00081 template <> class hash<BezierTriangle*> : public CastHash<BezierTriangle*> { };
00082
00083
00084 template <> class hash<const Cell*> : public CastHash<const Cell*> { };
00085 template <> class hash<const VertexCell*> : public CastHash<const VertexCell*> { };
00086 template <> class hash<const EdgeCell*> : public CastHash<const EdgeCell*> { };
00087 template <> class hash<const FaceCell*> : public CastHash<const FaceCell*> { };
00088 template <> class hash<const BoundaryVertex*> : public CastHash<const BoundaryVertex*> { };
00089 template <> class hash<const BoundaryEdge*> : public CastHash<const BoundaryEdge*> { };
00090 template <> class hash<const BoundaryFace*> : public CastHash<const BoundaryFace*> { };
00091 template <> class hash<const BezierVertex*> : public CastHash<const BezierVertex*> { };
00092 template <> class hash<const BezierEdge*> : public CastHash<const BezierEdge*> { };
00093 template <> class hash<const BezierTriangle*> : public CastHash<const BezierTriangle*> { };
00094 };
00095 #endif
00096
00097
00105 enum Movement
00106 {
00107 FixedNone,
00108 FixedHorizontal,
00109 FixedVertical,
00110 FixedAll
00111 };
00112
00113
00120 class Cell {
00121 public:
00122 const unsigned long id;
00123 Cell();
00124 virtual ~Cell() { }
00125 };
00126
00134 class VertexCell : public Cell {
00135 private:
00136 typedef PersistantVector<EdgeCell*> vector;
00137 vector edges_;
00138
00139 public:
00140
00141 VertexCell(PersistantStore&);
00142
00143
00144 virtual ~VertexCell();
00145
00146
00147 void add_edge(EdgeCell *e);
00148 void delete_edge(EdgeCell *e);
00149 bool has_edge(const EdgeCell *e) const;
00150 bool has_face(const FaceCell *f) const;
00151 int num_edges() const { return edges_.size(); }
00152
00153 typedef vector::iterator edge_iterator;
00154 typedef vector::const_iterator edge_const_iterator;
00155
00156 edge_iterator begin_edges() { return edges_.begin(); }
00157 edge_const_iterator begin_edges() const { return edges_.begin(); }
00158
00159 edge_iterator end_edges() { return edges_.end(); }
00160 edge_const_iterator end_edges() const { return edges_.end(); }
00161
00162 EdgeCell *get_any_edge();
00163 const EdgeCell *get_any_edge() const;
00164
00165 private:
00166
00167 VertexCell(const VertexCell &o);
00168 VertexCell& operator=(const VertexCell &o);
00169 };
00170
00177 class EdgeCell : public Cell {
00178 private:
00179 PersistantArray<VertexCell*, 2> vertices_;
00180 PersistantArray<FaceCell*, 2> faces_;
00181
00182 public:
00183
00184 EdgeCell(PersistantStore&, VertexCell *v1, VertexCell *v2);
00185
00186
00187 virtual ~EdgeCell() {}
00188
00189
00190 void add_face(FaceCell *f);
00191 void delete_face(FaceCell *f);
00192 bool has_vertex(const VertexCell *v) const;
00193 bool has_face(const FaceCell *f) const;
00194 int num_faces() const;
00195
00196 typedef PersistantArray<VertexCell*,2>::iterator vertex_iterator;
00197 typedef PersistantArray<VertexCell*,2>::const_iterator vertex_const_iterator;
00198 typedef PersistantArray<FaceCell*,2>::iterator face_iterator;
00199 typedef PersistantArray<FaceCell*,2>::const_iterator face_const_iterator;
00200
00201 vertex_iterator begin_vertices() { return vertices_; }
00202 vertex_const_iterator begin_vertices() const { return vertices_; }
00203
00204 vertex_iterator end_vertices() { return vertices_ + 2; }
00205 vertex_const_iterator end_vertices() const { return vertices_ + 2; }
00206
00207 VertexCell *get_canon_vertex();
00208 const VertexCell *get_canon_vertex() const;
00209
00210
00211 VertexCell *get_vertex(int i) {
00212 assert(i >= 0); assert(i < 2);
00213 return vertices_[i];
00214 }
00215 const VertexCell *get_vertex(int i) const {
00216 assert(i >= 0); assert(i < 2);
00217 return vertices_[i];
00218 }
00219
00220 face_iterator begin_faces() { return faces_; }
00221 face_const_iterator begin_faces() const { return faces_; }
00222
00223 face_iterator end_faces() { return faces_ + num_faces(); }
00224 face_const_iterator end_faces() const { return faces_ + num_faces(); }
00225
00226 FaceCell *get_any_face();
00227 const FaceCell *get_any_face() const;
00228
00229 private:
00230
00231 EdgeCell(const EdgeCell &o);
00232 EdgeCell& operator=(const EdgeCell &o);
00233 };
00234
00235
00242 class FaceCell : public Cell {
00243 private:
00244 typedef PersistantVector<EdgeCell*> vector;
00245 vector edges_;
00246
00247 public:
00248
00249 FaceCell(PersistantStore&);
00250
00251
00252 virtual ~FaceCell();
00253
00254
00255 void add_edge(EdgeCell *e);
00256 void delete_edge(EdgeCell *e);
00257
00258
00259 bool has_vertex(const VertexCell *v) const;
00260 bool has_edge(const EdgeCell *e) const;
00261 int num_edges() const { return edges_.size(); }
00262
00263 typedef vector::iterator edge_iterator;
00264 typedef vector::const_iterator edge_const_iterator;
00265
00266 edge_iterator begin_edges() { return edges_.begin(); }
00267 edge_const_iterator begin_edges() const { return edges_.begin(); }
00268
00269 edge_iterator end_edges() { return edges_.end(); }
00270 edge_const_iterator end_edges() const { return edges_.end(); }
00271
00272 EdgeCell *get_canon_edge();
00273 const EdgeCell *get_canon_edge() const;
00274
00275 private:
00276
00277 FaceCell(const FaceCell&);
00278 FaceCell& operator=(const FaceCell &o);
00279 };
00280
00290 #define declare_iterators_any(cell, plural, out) \
00291 typedef Tumble::iterators::caster<super::cell##_iterator, out*, out*> \
00292 cell##_iterator; \
00293 typedef Tumble::iterators::caster<super::cell##_const_iterator, const out*, const out*> \
00294 cell##_const_iterator; \
00295 cell##_iterator begin_##plural(); \
00296 cell##_const_iterator begin_##plural() const; \
00297 cell##_iterator end_##plural(); \
00298 cell##_const_iterator end_##plural() const; \
00299 out *get_any_##cell(); \
00300 const out *get_any_##cell() const;
00301
00302 #define declare_iterators_canon(cell, plural, out) \
00303 typedef Tumble::iterators::caster<super::cell##_iterator, out*, out*> \
00304 cell##_iterator; \
00305 typedef Tumble::iterators::caster<super::cell##_const_iterator, const out*, const out*> \
00306 cell##_const_iterator; \
00307 cell##_iterator begin_##plural(); \
00308 cell##_const_iterator begin_##plural() const; \
00309 cell##_iterator end_##plural(); \
00310 cell##_const_iterator end_##plural() const; \
00311 out *get_canon_##cell(); \
00312 const out *get_canon_##cell() const;
00313
00323 class BoundaryVertex : public VertexCell {
00324 private:
00325 typedef VertexCell super;
00326
00327 public:
00328
00329 BoundaryVertex(PersistantStore&, const ControlPoint&);
00330
00331
00332 virtual ~BoundaryVertex() {}
00334
00335 void set_bezier(BezierVertex *bv);
00336 void increase_fixed(Movement);
00337 void set_cp(const Point2D &p);
00338
00339
00340 const Point2D& get_cp() const;
00341 Point2D& access_cp();
00342 const ControlPoint& get_control_point() const;
00343 const BezierVertex *get_bezier() const;
00344 BezierVertex *get_bezier();
00345 Movement get_fixed() const;
00346 void print() const;
00347
00348 declare_iterators_any(edge, edges, BoundaryEdge);
00349
00350 private:
00351
00352 BezierVertex* bezier_vertex_;
00353 ControlPoint cp_;
00354 Movement fixed_;
00357
00358 BoundaryVertex(const BoundaryVertex &other);
00359 BoundaryVertex& operator=(const BoundaryVertex &o);
00360 };
00361
00362
00376 class BoundaryEdge : public EdgeCell {
00377 public:
00378 typedef EdgeCell super;
00379
00380
00381
00382 BoundaryEdge(PersistantStore&, BoundaryVertex *v0, BoundaryVertex *v1,
00383 const std::vector<ControlPoint> &deboor,
00384 const std::vector<double> &knots,
00385 DataStore *_data_store);
00386
00387
00388 virtual ~BoundaryEdge();
00389
00390
00391 void set_fixed(Movement fixed);
00392 void set_color(int _color);
00393 void set_restlength(double _restlength);
00394
00395
00396 const BoundaryVertex* get_vertex(int i) const;
00397 BoundaryVertex* get_vertex(int i);
00398 const BezierVertex* get_d0_vertex(int i) const;
00399 BezierVertex* get_d0_vertex(int i);
00400 const BezierVertex* get_d1_vertex(int i) const;
00401 BezierVertex* get_d1_vertex(int i);
00402 const BezierVertex* get_bezier_vertex(int i) const;
00403 BezierVertex* get_bezier_vertex(int i);
00404 int get_num_d1_vertexs() const;
00405
00406 const QBSpline *get_spline() const;
00407 QBSpline *get_spline();
00408 Movement get_fixed() const;
00409 int get_color() const;
00410
00411
00412 void print() const;
00413
00414
00415 declare_iterators_canon(vertex, vertices, BoundaryVertex);
00416 declare_iterators_any(face, faces, BoundaryFace);
00417
00418 private:
00419
00420 QBSpline *spline_;
00421 Movement fixed_;
00422 int color_;
00425
00426 BoundaryEdge(const BoundaryEdge &o);
00427 BoundaryEdge& operator=(const BoundaryEdge &o);
00428 };
00429
00439 class BoundaryFace : public FaceCell {
00440 public:
00441 typedef FaceCell super;
00442
00443
00444 BoundaryFace(PersistantStore&, double min_angle = 0, int color = 0);
00445
00446
00447 virtual ~BoundaryFace() {}
00449
00450 void set_min_angle(double);
00451 void set_color(int);
00452
00453 double get_min_angle() const;
00454 int get_color() const;
00455
00456
00457 void print() const;
00458 friend std::ostream &operator<<( std::ostream &stream, const BoundaryFace& v);
00459
00460
00461 declare_iterators_canon(edge, edges, BoundaryEdge);
00462
00463 private:
00464
00465 double min_angle_;
00466 int color_;
00468 private:
00469
00470 BoundaryFace(const BoundaryFace &other);
00471 BoundaryFace& operator=(const BoundaryFace &o);
00472 };
00473
00494 class BezierVertex : public VertexCell {
00495 private:
00496 typedef VertexCell super;
00497 public:
00498
00499
00500
00501 BezierVertex(PersistantStore&, const ControlPoint&, const DataPoint&);
00502
00503
00504 virtual ~BezierVertex() {}
00506
00507 void set_bdry(BoundaryEdge *e, double alpha);
00508 void set_bdry(BoundaryEdge *e, int idx);
00509 void set_bdry(BoundaryVertex *v);
00510 void set_cp(const Point2D &p );
00511 void set_dp(const LinearData &d );
00512 void set_data(int pos, double data);
00513 double get_data(int pos) const;
00514
00515
00516 const Point2D& get_cp() const;
00517 Point2D& access_cp();
00518 ControlPoint get_control_point() const;
00519
00520 const LinearData& get_dp() const;
00521 LinearData& access_dp();
00522 DataPoint get_data_point() const;
00523
00524 int containment_dimension() const;
00525 bool is_boundary() const;
00526
00527 const BoundaryEdge *get_bdry_edge() const;
00528 BoundaryEdge *get_bdry_edge();
00529 const BoundaryVertex *get_bdry_vertex() const;
00530 BoundaryVertex *get_bdry_vertex();
00531
00532 double get_u(const BoundaryEdge *be, const BezierVertex *opposite) const;
00533 double get_u(const BezierEdge *e) const;
00534 double get_u() const;
00535 void print() const;
00536
00537 declare_iterators_any(edge, edges, BezierEdge);
00538
00539 private:
00540
00541 friend class BezierMesh;
00542 void replace_cp(const ControlPoint& newp);
00543
00544 private:
00545
00546 ControlPoint cp_;
00547 DataPoint dp_;
00548 BoundaryEdge *bdry_edge_;
00549 BoundaryVertex *bdry_vertex_;
00550 double u_;
00552 private:
00553
00554 BezierVertex(const BezierVertex &o);
00555 BezierVertex& operator=(const BezierVertex &o);
00556 };
00557
00593 class BezierEdge : public EdgeCell {
00594 public:
00595 typedef EdgeCell super;
00596 friend class BezierMesh;
00597
00598
00599 BezierEdge(PersistantStore&, BezierVertex *v1, BezierVertex *v2,
00600 const ControlPoint&, const DataStore&);
00601
00602
00603 virtual ~BezierEdge() {}
00605
00606 void set_bdry(BoundaryEdge *be,double u0,double u1);
00607 void set_cp(const Point2D &p, int i);
00608 void set_dp(const LinearData &d, int i);
00609 void set_data(int i, int pos, double data);
00610
00611
00612 int get_num_cp() const { return 3; }
00613
00614 const Point2D& get_cp(int i) const;
00615 Point2D& access_cp(int i);
00616 ControlPoint get_control_point(int i) const;
00617
00618 const LinearData& get_dp(int i) const;
00619 LinearData& access_dp(int i);
00620 DataPoint get_data_point(int i) const;
00621
00622 const BezierVertex* get_vertex(int i) const;
00623 BezierVertex* get_vertex(int i);
00624
00625 bool is_boundary() const;
00626 const BoundaryEdge *get_bdry_edge() const;
00627 BoundaryEdge *get_bdry_edge();
00628
00629
00630 double get_u(int i) const;
00631
00632 Point2D eval(double u) const;
00633 LinearData dataeval(double u) const;
00634 void evalIntermediate(double u,Point2D &p,Point2D &pc0,Point2D &pc1) const;
00635 void dataevalIntermediate(double u,LinearData &d,LinearData &dc0,LinearData &dc1) const;
00636 bool is_encroached(const Point2D&) const;
00637 template <typename iterator> static bool encroaches(Point2D p,
00638 const iterator& bounding_start, const iterator& bounding_end);
00639 int left_of_curve(Point2D&,int,double&) const;
00640 double curvature_angle() const;
00641 void print() const;
00642
00643
00644 friend std::ostream &operator<<( std::ostream &stream, const BezierEdge& e);
00645
00646 declare_iterators_canon(vertex, vertices, BezierVertex);
00647 declare_iterators_any(face, faces, BezierTriangle);
00648
00649 private:
00650 void replace_cp(const ControlPoint& oldp, const ControlPoint& newp);
00651
00652 private:
00653
00654 ControlPoint cp_[3];
00655 DataPoint dp_[3];
00656 BoundaryEdge *bdry_edge_;
00657 double u_[2];
00659 private:
00660
00661 BezierEdge(const BezierEdge &o);
00662 BezierEdge& operator=(const BezierEdge &o);
00663 };
00664
00671 class CurvedTriangle {
00672 public:
00673 bool newton_find(const Point2D &p, double &u, double &v, int &niter, int &edge_num) const;
00674 bool newton_find(const Point2D &p, double &u, double &v) const;
00675 virtual void jacobian_matrix(double u,double v,Point2D &pdu,Point2D &pdv) const =0;
00676 virtual Point2D eval(double u,double v) const =0;
00677 CurvedTriangle() {}
00678 virtual ~CurvedTriangle() {}
00679 };
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00719 class BezierTriangle : public FaceCell, public CurvedTriangle
00720 {
00721 public:
00722 typedef FaceCell super;
00723 friend class BezierMesh;
00724
00725
00726 BezierTriangle(PersistantStore&,
00727 BezierEdge * const es[3], const DataStore&, bool inverse_handed=false);
00728 void set_boundary_face(BoundaryFace *f);
00729
00730
00731 virtual ~BezierTriangle() {}
00733
00734 int get_num_cp() const { return 6; }
00735 const Point2D& get_cp(int i) const;
00736 Point2D& access_cp(int i);
00737 void set_cp(int i, const Point2D &p);
00738 ControlPoint get_control_point(int i) const;
00739
00740
00741 int get_num_dp() const { return get_num_cp(); }
00742 const LinearData& get_dp(int i) const;
00743 LinearData& access_dp(int i);
00744 void set_dp(int i, const LinearData &p);
00745 DataPoint get_data_point(int i) const;
00746
00747
00748
00749 int get_num_edges() const { return 3; }
00750 BezierEdge *get_edge(int i);
00751 const BezierEdge *get_edge(int i) const;
00752
00753 bool has_bdry_face() const;
00754 const BoundaryFace *get_bdry_face() const;
00755 BoundaryFace *get_bdry_face();
00756 BoundaryFace *get_bdry_face_or_null();
00757
00758 double get_min_angle() const;
00759 int get_color() const;
00760
00761
00762 const BezierEdge* get_edge(int p0, int p1) const;
00763 BezierEdge* get_edge(int p0, int p1);
00764 Point2D eval(double alpha, double beta) const;
00765 LinearData dataeval(double alpha, double beta) const;
00766 void evalIntermediate(double alpha, double beta, Point2D &p, Point2D &pc0, Point2D &pc1, Point2D &pc2) const;
00767 void dataevalIntermediate(double alpha, double beta, LinearData &d, LinearData &dc0, LinearData &dc1, LinearData &dc2) const;
00768 double jacobian(double alpha ,double beta) const;
00769 void jacobian_matrix(double alpha, double beta, Point2D &pdu, Point2D &pdv) const;
00770 void bound_jacobian(double &min, double &max, double &area) const;
00771 bool newton_find(const Point2D &p, double &u, double &v, int &niter, int &edge_num);
00772 double area() const;
00773
00774 bool small_angle() const;
00775 bool is_linear_inverted() const;
00776 int is_inverted() const;
00777 bool is_definitely_inverted() const;
00778 bool is_definitely_not_inverted() const;
00779
00780 Point2D circumcenter() const;
00781 Point2D offcenter() const;
00782 Point2D offcenter(double beta) const;
00783 void print() const;
00784 void print_mathematica() const;
00785
00786
00787 friend std::ostream &operator<<( std::ostream &stream, const BezierTriangle& t);
00788
00789 declare_iterators_canon(edge, edges, BezierEdge);
00790
00791 private:
00792 int get_edge_index_by_cps(int p0, int p1) const;
00793 void replace_cp(const ControlPoint& oldp, const ControlPoint& newp);
00794
00795 private:
00796
00797 ControlPoint cp_[6];
00798 DataPoint dp_[6];
00799 BoundaryFace *bdry_face_;
00801 private:
00802
00803 BezierTriangle(const BezierTriangle &o);
00804 BezierTriangle& operator=(const BezierTriangle &o);
00805 };
00806
00807
00827 class GhostTriangle : public CurvedTriangle {
00828 public:
00829
00830 Point2D cp[6];
00831 LinearData dp[6];
00833
00834 GhostTriangle(const BezierTriangle &bt);
00835 GhostTriangle();
00836
00837
00838 virtual ~GhostTriangle() {}
00840 void set_cp(int i,Point2D p);
00841 Point2D get_cp(int i) const;
00842
00843
00844 void jacobian_matrix(double u,double v,Point2D &pdu,Point2D &pdv) const;
00845 double jacobian(double alpha ,double beta) const;
00846 LinearData dataeval(double u, double v) const;
00847 Point2D eval(double u, double v) const;
00848 void print() const;
00849 void print_mathematica() const;
00850
00851
00852 friend std::ostream &operator<<( std::ostream &stream, const GhostTriangle& t);
00853
00854 private:
00855
00856 GhostTriangle(const GhostTriangle&);
00857 GhostTriangle& operator=(const GhostTriangle&);
00858 };
00859
00860
00861
00862
00863
00864
00865
00866
00881 template <typename iterator>
00882 inline bool BezierEdge::encroaches(Point2D p,
00883 const iterator& bounding_start, const iterator& bounding_end)
00884 {
00885 for(iterator outer = bounding_start; outer != bounding_end; ++outer) {
00886 iterator inner = outer;
00887 ++inner;
00888 for(; inner != bounding_end; ++inner) {
00889
00890
00891 double dotproduct = (*outer - p) .dot (*inner - p);
00892 if(dotproduct < 0.0) {
00893 return true;
00894 }
00895 }
00896 }
00897 return false;
00898 }
00899
00900 #undef declare_iterators_any
00901 #undef declare_iterators_canon
00902 #endif