/Users/craigcornelius/Projects/SPRING Mac Release 0.2/boundingsphere.h

Go to the documentation of this file.
00001 // $Id: boundingsphere.h,v 1.23 2006/05/25 22:08:39 craig Exp $
00002 // $Copyright: (c)2001 National Biocomputation Center, Stanford University $
00003 
00004 #ifndef BoundingSphere_Header
00005 #define BoundingSphere_Header
00006 
00007 #ifdef _WIN32
00008 #pragma warning ( push )
00009 #pragma warning ( disable : 4786 )
00010 #endif 
00011 
00012 
00013 #include <assert.h>
00014 
00015 #include "list.h"
00016 #include "set.h"
00017 
00018 // uses comparison function to keep in order
00019 template <class T> class Priority_queue {
00020 #ifdef NOT_YET
00021     void push(T& item) {}
00022     void pop() {}
00023     T& top() {}
00024     int empty() { return(1); }
00025 #endif
00026 };
00027 
00028 #include "point3d.h"
00029 
00030 const double SMALL = 1e-10;
00031 const double LARGE = 1e10;
00032 
00033 class BoundingSphere;
00034 class BoundingSphereLeaf;
00035 class Tetra;
00036 class Face;
00037 class Edge;
00038 class Node;
00039 class Object;
00040 #ifndef _WIN32
00041 //template <class T, class Sequence, class Compare> class priority_queue;
00042 #endif
00043 
00044 struct scompare {
00045     bool operator() (const BoundingSphere* s1, const BoundingSphere* s2) const;
00046 };
00047 
00048 
00049 // used to store instances of sphere leaf objects
00050 typedef List<BoundingSphere*> BSlist;
00051 
00052 // used to put sphere leafs in a set
00053 typedef Set<BoundingSphereLeaf*> BSLset;
00054 
00055 // used to put sphere leafs in a queue
00056 typedef Priority_queue<BoundingSphere *> BSqueue;
00057 
00058 typedef enum {tetra_ptr, face_ptr, edge_ptr, node_ptr} Feature_type;
00059 
00060 typedef union {
00061     Tetra* tetra;
00062     Face* face;
00063     Edge* edge;
00064     Node* node;
00065 } Feature_ptr;
00066 
00067 class BoundingSphereNode;
00068 class BoundingSphereLeaf;
00069 
00070 // generic class 
00071 class BoundingSphere {
00072     friend class BoundingSphereNode;
00073     friend class BoundingSphereLeaf;
00074     friend struct scompare;
00075 public:
00076     BoundingSphere(BoundingSphere* parent);
00077     virtual ~BoundingSphere();
00078     
00079     static const char* rcsid;
00080     static int debug;
00081     static int return_first_collision_only;             // defaults to 0
00082     
00083     // handy helper functions
00084     inline Point3D getCenter() {return(center);}
00085     inline double getRadius() {return(radius);}
00086  
00087     inline  BoundingSphere* getParent() { return parent;}
00088     inline  void setParent(BoundingSphere* p) { parent = p;}
00089     inline int getLevel() { return (level); }
00090     int numLeafs();
00091     int getDepth();
00092     int hasAncestor(BoundingSphere* b);
00093     BoundingSphereNode* findCommonAncestor(BoundingSphere* b[], int num);
00094     
00095     bool remainsValid(Point3D newCenter, double newRadius, double threshold);
00096     virtual bool isLeaf() = 0;
00097     
00098     // for collision detection
00099     static Node* findClosestNode(BoundingSphere *s, Point3D pt,
00100         double *thresh);
00101     
00102     static int dist(BoundingSphere *sa, BoundingSphere *sb, double error = 0,
00103         double init = LARGE);
00104     
00105     // for dynamically adding to tree
00106     static int addLeaf(BoundingSphereLeaf* sa, BoundingSphere* sb);
00107     static int removeLeaf(BoundingSphereLeaf* sa);
00108     
00109     // for increasing performance
00110     int enabled;
00111     int numEnabledLeafs();
00112     void markTreeEnabled(int enabled);
00113     void propEnabled(int enabled);
00114     
00115     // for updating the tree
00116     virtual bool procChange() = 0;
00117     inline virtual void propChange()
00118     {if (procChange() && (parent != NULL)) parent->propChange();}
00119     
00120 protected:
00121     BoundingSphere* parent;
00122     Point3D        center;
00123     double         radius;
00124     int            level;
00125     bool           inQueue;
00126 private:
00127     static BSqueue spherePQ;
00128 };
00129 
00130 
00131 // root of a tree 
00132 class BoundingSphereNode : public BoundingSphere {
00133 public:
00134     BoundingSphereNode(BoundingSphere *left_ = NULL, 
00135         BoundingSphere *right_ = NULL, 
00136         BoundingSphere *parent_ = NULL);
00137     BoundingSphereNode(BSlist &sphereList, BoundingSphere *parent_ = NULL);
00138     virtual ~BoundingSphereNode();
00139     
00140     bool isLeaf() {return(false);}
00141     void drawSubtree();
00142     
00143     Node *findClosestNode(Point3D pt, double *thresh);
00144     static int dist(BoundingSphereNode *sa, BoundingSphere *sb, 
00145         double error = 0, double init = LARGE);
00146     
00147     bool procChange();
00148     
00149     BoundingSphere* getLeft() {return left;}
00150     BoundingSphere* getRight() {return right;}
00151     
00152     void setLeft(BoundingSphere* lt) {left = lt;}
00153     void setRight(BoundingSphere* rt) {right = rt;}
00154     
00155 private: 
00156     BoundingSphere *left, *right;
00157 };
00158 
00159 
00160 // leaf nodes in a tree 
00161 class BoundingSphereLeaf : public BoundingSphere {
00162 public:
00163     // note: you could make another contructor that taken in edges,
00164     // for edge tests, steve had it read in primatives
00165     BoundingSphereLeaf(Tetra *tetra_, BoundingSphere *parent_ = NULL);
00166     BoundingSphereLeaf(Face *face_, BoundingSphere *parent_ = NULL);
00167     BoundingSphereLeaf(Edge *edge_, BoundingSphere *parent_ = NULL);
00168     BoundingSphereLeaf(Node *node_, BoundingSphere *parent_ = NULL);
00169     virtual ~BoundingSphereLeaf();
00170     
00171     bool isLeaf()       {return(true);}
00172     void draw();
00173     
00174     Node *findClosestNode(Point3D pt, double *thresh);
00175     //int dist(BoundingSphere *other, double error = 0, double init = LARGE);
00176     static int dist(BoundingSphereLeaf *sa, BoundingSphereLeaf *sb, 
00177         double error = 0, double init = LARGE);
00178     
00179     void insertInUpdateList();
00180     
00181     bool procChange();
00182     static void processUpdate();
00183     
00184     inline Feature_type getFeatureType() {return(feature_type);}
00185     inline Feature_ptr getFeaturePtr() {return(feature_ptr);}
00186     Object* getFeatureObject();
00187     int getFeatureIndex();
00188     
00189     inline Tetra* getTetra() {return(feature_ptr.tetra);}               
00190     inline Face* getFace() {return(feature_ptr.face);}
00191     inline Edge* getEdge() {return(feature_ptr.edge);}
00192     inline Node* getNode() {return(feature_ptr.node);}
00193     
00194     void setRadiusAndCenterFromFeature();
00195     
00196 private:
00197     Feature_type feature_type;
00198     Feature_ptr feature_ptr;
00199     
00200     static BSLset sphereLeafSet; // each leaf knows about the set
00201 };
00202 
00203 #ifdef _WIN32
00204 #pragma warning (pop)
00205 #endif
00206 
00207 #endif

Generated on Thu Aug 30 11:03:13 2007 for SPRING Mac by  doxygen 1.5.3