HeTriang.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 //
00003 // File: HeTriang.h
00004 //
00005 // Created: March 1 2001
00006 //
00007 // Author: Øyvind Hjelle <oyvind.hjelle@math.sintef.no>
00008 //
00009 // Revision: $Id: HeTriang.h,v 1.2 2006/07/26 12:08:44 oyvindhj Exp $
00010 //
00011 // Description:
00012 //
00013 //==================================================================================================
00014 // Copyright (C) 2000-2003 SINTEF Applied Mathematics.  All rights reserved.
00015 //
00016 // This file may be distributed under the terms of the Q Public License
00017 // as defined by Trolltech AS of Norway and appearing in the file
00018 // LICENSE.QPL included in the packaging of this file.
00019 // 
00020 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00021 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00022 //
00023 //==================================================================================================
00024 
00025 
00026 #ifndef _HE_TRIANG_H_
00027 #define _HE_TRIANG_H_
00028 
00029 
00030 #define TTL_USE_NODE_ID   // Each node gets it's own unique id
00031 #define TTL_USE_NODE_FLAG // Each node gets a flag (can be set to true or false)
00032 
00033 
00034 #include <list>
00035 #include <vector>
00036 #include <iostream>
00037 #include <fstream>
00038 #include <ttl/ttl.h>
00039 #include <ttl/ttl_util.h>
00040 #include <ttl/utils/HandleId.h>
00041 #include <ttl/utils/Handle.h>
00042 
00043 
00044 using namespace std;
00045 
00046 //--------------------------------------------------------------------------------------------------
00047 // The half-edge data structure
00048 //--------------------------------------------------------------------------------------------------
00049 
00050 namespace hed {
00051 
00052   //------------------------------------------------------------------------------------------------
00053   // Node class for data structures
00054   //------------------------------------------------------------------------------------------------
00055 
00066   class Node : public virtual HandleId {
00067 
00068 #ifdef TTL_USE_NODE_ID
00070     static int id_count;
00071 #endif
00072 
00073     double x_, y_, z_;
00074 
00075 #ifdef TTL_USE_NODE_ID
00077     int id_;
00078 #endif
00079 
00080 #ifdef TTL_USE_NODE_FLAG
00082     bool flag_;
00083 #endif
00084 
00085 public:
00087     Node() { init(0, 0, 0); }
00088 
00090     Node(double x, double y, double z = 0.0) { init(x, y, z); }
00091 
00093     ~Node() {}
00094 
00096     void init(double x, double y, double z) { 
00097 
00098 #ifdef TTL_USE_NODE_ID
00099       id_ = id_count++;
00100 #endif
00101 
00102       x_ = x; y_ = y; z_ = z; 
00103     }
00104 
00106     const double& x() const { return x_; }
00107 
00109     const double& y() const { return y_; }
00110 
00112     const double& z() const { return z_; }
00113 
00114 #ifdef TTL_USE_NODE_ID
00116     const int& id() const { return id_; }
00117 #endif
00118 
00119 #ifdef TTL_USE_NODE_FLAG
00121     void setFlag(bool flag) { flag_ = flag; }
00122 
00124     const bool& getFlag() const { return flag_; }
00125 #endif
00126 
00127   }; // End og class Node
00128 
00129   
00130   //------------------------------------------------------------------------------------------------
00131   // Edge class in the half-edge data structure
00132   //------------------------------------------------------------------------------------------------
00133 
00138   class Edge {
00139     
00140     Handle<Node> sourceNode_;
00141     Edge*        twinEdge_;
00142     Edge*        nextEdgeInFace_;
00143 
00144     struct {
00145       unsigned char isLeadingEdge_ : 1;
00146       unsigned char isConstrained_ : 1;
00147     } flags_;
00148 
00149   public:
00151     Edge() { sourceNode_ = NULL; twinEdge_ = NULL; nextEdgeInFace_ = NULL; 
00152       flags_.isLeadingEdge_ = 0; flags_.isConstrained_ = 0; }
00153 
00155     ~Edge() { if(twinEdge_) twinEdge_->setTwinEdge(NULL); }
00156 
00158     void setSourceNode(Node* node) { sourceNode_ = node; }
00159 
00161     void setNextEdgeInFace(Edge* edge) { nextEdgeInFace_ = edge; }
00162 
00164     void setTwinEdge(Edge* edge) { twinEdge_ = edge; }
00165 
00167     void setAsLeadingEdge(bool val=true) { flags_.isLeadingEdge_ = (val == true ? 1 : 0); }
00168 
00170     bool isLeadingEdge() const { return flags_.isLeadingEdge_ == 1 ? true : false; }
00171 
00173    void setConstrained(bool val=true) { unsigned char cstr = (val == true ? 1 : 0); 
00174       flags_.isConstrained_ = cstr; if (twinEdge_) twinEdge_->flags_.isConstrained_ = cstr; }
00175 
00177     bool isConstrained() const { return flags_.isConstrained_ == 1 ? true : false; }
00178 
00180     Edge* getTwinEdge() const { return twinEdge_; };
00181 
00183     Edge* getNextEdgeInFace() const { return nextEdgeInFace_; }
00184 
00186     Node* getSourceNode() { return sourceNode_.getPtr(); }
00187 
00189     Node* getTargetNode() { return getNextEdgeInFace()->getSourceNode(); }
00190 
00191   }; // End of class Edge
00192 
00193 
00194   //------------------------------------------------------------------------------------------------
00195   class Dart; // Forward declaration (class in this namespace)
00196 
00197 
00198   //------------------------------------------------------------------------------------------------
00199   // Triangulation class in the half-edge data structure
00200   //------------------------------------------------------------------------------------------------
00201 
00206   class Triangulation {
00207 
00208   protected:
00209     list<Edge*> leadingEdges_; // one half-edge for each arc
00210     void addLeadingEdge(Edge* edge) { edge->setAsLeadingEdge(); leadingEdges_.push_front(edge); }
00211     bool removeLeadingEdgeFromList(Edge* leadingEdge);
00212     void cleanAll();
00213     
00214   public:
00216     Triangulation() {}
00217     
00219     Triangulation(const Triangulation& tr) { 
00220       cout << "Triangulation: Copy constructor not present - EXIT."; 
00221       exit(-1);
00222     }
00223 
00225     ~Triangulation() { cleanAll(); }
00226     
00228     void createDelaunay(vector<Node*>::iterator first,
00229                         vector<Node*>::iterator last);
00230 
00232     //  When using rectangular boundary - loop through all points and expand.
00233     //  (Called from createDelaunay(...) when starting)
00234     Edge* initTwoEnclosingTriangles(vector<Node*>::iterator first,
00235                                     vector<Node*>::iterator last);
00236 
00237 
00238     // These two functions are required by TTL for Delaunay triangulation
00239     
00241     void swapEdge(Edge& diagonal);
00242 
00244     Edge* splitTriangle(Edge& edge, Node& point);
00245 
00246 
00247     // Functions required by TTL for removing nodes in a Delaunay triangulation
00248     
00250     void removeTriangle(Edge& edge); // boundary triangle required    
00251 
00253     void reverse_splitTriangle(Edge& edge);
00254 
00255 
00257     Dart createDart();
00258 
00260     const list<Edge*>& getLeadingEdges() const { return leadingEdges_; }
00261 
00263     int noTriangles() const { return leadingEdges_.size(); }
00264     
00266     list<Edge*>* getEdges(bool skip_boundary_edges = false) const;
00267 
00268 #ifdef TTL_USE_NODE_FLAG
00270     void flagNodes(bool flag) const;
00271 
00273     list<Node*>* getNodes() const;
00274 #endif
00275 
00277     void optimizeDelaunay();
00278 
00280     bool checkDelaunay() const;    
00281 
00283     Edge* getInteriorNode() const;
00284 
00286     Edge* getBoundaryEdge() const;
00287 
00289     void printEdges(ofstream& os) const;
00290 
00291   }; // End of class Triangulation
00292 
00293 
00294 }; // End of hed namespace
00295 
00296 #endif

Generated on Wed Nov 17 17:44:27 2010 for TTL by  doxygen 1.6.1