Changeset 1526 for GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx
- Timestamp:
- 09/28/06 17:49:37 (18 years ago)
- Location:
- GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/geom/3D.cxx
r1025 r1526 85 85 } 86 86 87 88 89 90 87 //////////////////////////////////////////////////////////////////////// 91 88 // -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/geom/3D.h
r1025 r1526 63 63 }; 64 64 65 66 65 // 67 66 // A triangular face in 3D (ie. a 2-simplex in E3) … … 94 93 } 95 94 96 97 98 95 // GFXGEOM_3D_INCLUDED 99 96 #endif -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Array.h
r1025 r1526 8 8 { 9 9 template<class T> 10 class array { 11 protected: 12 T *data; 13 int len; 14 public: 15 array() { data=NULL; len=0; } 16 array(int l) { init(l); } 17 ~array() { f_r_e_e(); } 10 class array 11 { 12 protected: 13 T *data; 14 int len; 15 public: 16 array() { data=NULL; len=0; } 17 array(int l) { init(l); } 18 ~array() { f_r_e_e(); } 18 19 20 inline void init(int l); 21 inline void f_r_e_e(); 22 inline void resize(int l); 19 23 20 inline void init(int l);21 inline void f_r_e_e();22 inline void resize(int l);24 inline T& ref(int i); 25 inline T& operator[](int i) { return data[i]; } 26 inline T& operator()(int i) { return ref(i); } 23 27 24 inline T& ref(int i);25 inline T& operator[](int i){ return data[i]; }26 inline T& operator()(int i){ return ref(i); }28 inline const T& ref(int i) const; 29 inline const T& operator[](int i) const { return data[i]; } 30 inline const T& operator()(int i) const { return ref(i); } 27 31 28 inline const T& ref(int i) const; 29 inline const T& operator[](int i) const { return data[i]; } 30 inline const T& operator()(int i) const { return ref(i); } 31 32 33 inline int length() const { return len; } 34 inline int maxLength() const { return len; } 35 }; 32 inline int length() const { return len; } 33 inline int maxLength() const { return len; } 34 }; 36 35 37 36 template<class T> 38 inline void array<T>::init(int l)39 {40 data = new T[l];41 len = l;42 }37 inline void array<T>::init(int l) 38 { 39 data = new T[l]; 40 len = l; 41 } 43 42 44 43 template<class T> 45 inline void array<T>::f_r_e_e() 46 { 47 if( data ) 44 inline void array<T>::f_r_e_e() 48 45 { 49 delete[] data; 50 data = NULL; 46 if( data ) 47 { 48 delete[] data; 49 data = NULL; 50 } 51 51 } 52 }53 52 54 53 template<class T> 55 inline T& array<T>::ref(int i)56 {57 58 assert( data );59 assert( i>=0 && i<len );60 61 return data[i];62 }54 inline T& array<T>::ref(int i) 55 { 56 #ifdef SAFETY 57 assert( data ); 58 assert( i>=0 && i<len ); 59 #endif 60 return data[i]; 61 } 63 62 64 63 template<class T> 65 inline const T& array<T>::ref(int i) const66 {67 68 assert( data );69 assert( i>=0 && i<len );70 71 return data[i];72 }64 inline const T& array<T>::ref(int i) const 65 { 66 #ifdef SAFETY 67 assert( data ); 68 assert( i>=0 && i<len ); 69 #endif 70 return data[i]; 71 } 73 72 74 73 template<class T> 75 inline void array<T>::resize(int l)76 {77 T *old = data;78 data = new T[l];79 data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T));80 len = l;81 delete[] old;82 }74 inline void array<T>::resize(int l) 75 { 76 T *old = data; 77 data = new T[l]; 78 data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T)); 79 len = l; 80 delete[] old; 81 } 83 82 } 84 83 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Buffer.h
r1025 r1526 25 25 inline int length() const { return fill; } 26 26 inline int maxLength() const { return len; } 27 inline void setFill(int l){fill = l;} 27 28 }; 28 29 29 30 30 template<class T> -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Heap.h
r1025 r1526 16 16 class Heapable 17 17 { 18 private: 19 int token; 18 private: 20 19 21 public: 22 Heapable() { notInHeap(); } 20 int token; 23 21 24 inline int isInHeap() { return token!=NOT_IN_HEAP; } 25 inline void notInHeap() { token = NOT_IN_HEAP; } 26 inline int getHeapPos() { return token; } 27 inline void setHeapPos(int t) { token=t; } 22 public: 23 24 Heapable() { notInHeap(); } 25 26 inline int isInHeap() { return token!=NOT_IN_HEAP; } 27 inline void notInHeap() { token = NOT_IN_HEAP; } 28 inline int getHeapPos() { return token; } 29 inline void setHeapPos(int t) { token=t; } 28 30 }; 29 31 32 class heap_node 33 { 34 public: 35 float import; 36 Heapable *obj; 30 37 31 class heap_node { 32 public: 33 float import; 34 Heapable *obj; 35 36 heap_node() { obj=NULL; import=0.0; } 37 heap_node(Heapable *t, float i=0.0) { obj=t; import=i; } 38 heap_node(const heap_node& h) { import=h.import; obj=h.obj; } 38 heap_node() { obj=NULL; import=0.0; } 39 heap_node(Heapable *t, float i=0.0) { obj=t; import=i; } 40 heap_node(const heap_node& h) { import=h.import; obj=h.obj; } 39 41 }; 40 42 41 42 43 class Heap : public array<heap_node> { 44 43 class Heap : public array<heap_node> 44 { 45 45 // 46 46 // The actual size of the heap. array::length() … … 57 57 void downheap(int i); 58 58 59 public:59 public: 60 60 61 61 Heap() { size=0; } 62 62 Heap(int s) : array<heap_node>(s) { size=0; } 63 64 63 65 64 void insert(Heapable *, float); … … 67 66 68 67 heap_node *extract(); 68 69 // Overhead method. 70 heap_node *extract(int i); 71 69 72 heap_node *top() { return size<1 ? (heap_node *)NULL : &ref(0); } 70 73 heap_node *kill(int i); 74 75 // Gets heap size. 76 int getSize(){return size;} 77 78 // Gets element at position i. 79 heap_node *getElement(int i){return &ref(i);} 80 71 81 }; 72 82 73 83 } 74 84 75 76 85 // GFXTOOLS_HEAP_INCLUDED 77 86 #endif 87 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/heap.cxx
r1025 r1526 7 7 void Heap::swap(int i,int j) 8 8 { 9 9 heap_node tmp = ref(i); 10 10 11 12 11 ref(i) = ref(j); 12 ref(j) = tmp; 13 13 14 15 14 ref(i).obj->setHeapPos(i); 15 ref(j).obj->setHeapPos(j); 16 16 } 17 17 18 18 void Heap::upheap(int i) 19 19 { 20 20 if( i==0 ) return; 21 21 22 if( ref(i).import > ref(parent(i)).import ) { 23 swap(i,parent(i)); 24 upheap(parent(i)); 25 } 22 if( ref(i).import > ref(parent(i)).import ) 23 { 24 swap(i,parent(i)); 25 upheap(parent(i)); 26 } 26 27 } 27 28 28 29 void Heap::downheap(int i) 29 30 { 30 31 if (i>=size) return; // perhaps just extracted the last 31 32 32 33 34 33 int largest = i, 34 l = left(i), 35 r = right(i); 35 36 36 37 37 if( l<size && ref(l).import > ref(largest).import ) largest = l; 38 if( r<size && ref(r).import > ref(largest).import ) largest = r; 38 39 39 40 41 42 40 if( largest != i ) { 41 swap(i,largest); 42 downheap(largest); 43 } 43 44 } 44 45 46 45 47 46 void Heap::insert(Heapable *t,float v) 48 47 { 49 50 51 std::cerr << "NOTE: Growing heap from " << size << " to " << 2*size << std::endl;52 resize(2*size);53 48 if( size == maxLength() ) 49 { 50 std::cerr << "NOTE: Growing heap from " << size << " to " << 2*size << std::endl; 51 resize(2*size); 52 } 54 53 55 54 int i = size++; 56 55 57 58 56 ref(i).obj = t; 57 ref(i).import = v; 59 58 60 59 ref(i).obj->setHeapPos(i); 61 60 62 61 upheap(i); 63 62 } 64 63 65 64 void Heap::update(Heapable *t,float v) 66 65 { 67 66 int i = t->getHeapPos(); 68 67 69 70 68 if( i >= size ) 69 { 71 70 std::cerr << "WARNING: Attempting to update past end of heap!" << std::endl; 72 71 return; 73 74 75 72 } 73 else if( i == NOT_IN_HEAP ) 74 { 76 75 std::cerr << "WARNING: Attempting to update object not in heap!" << std::endl; 77 76 return; 78 77 } 79 78 80 81 79 float old=ref(i).import; 80 ref(i).import = v; 82 81 83 84 85 86 82 if( v<old ) 83 downheap(i); 84 else 85 upheap(i); 87 86 } 88 89 90 87 91 88 heap_node *Heap::extract() 92 89 { 93 90 if( size<1 ) return 0; 94 91 95 96 92 swap(0,size-1); 93 size--; 97 94 98 95 downheap(0); 99 96 100 97 ref(size).obj->notInHeap(); 101 98 102 return &ref(size); 99 return &ref(size); 100 } 101 102 // Overhead method. 103 heap_node *Heap::extract(int i) 104 { 105 if( size<1 ) return 0; 106 107 swap(i,size-1); 108 size--; 109 110 downheap(i); 111 112 ref(size).obj->notInHeap(); 113 114 return &ref(size); 103 115 } 104 116 105 117 heap_node *Heap::kill(int i) 106 118 { 107 119 if( i>=size ) 108 120 std::cerr << "WARNING: Attempt to delete invalid heap node." << std::endl; 109 121 110 111 112 122 swap(i, size-1); 123 size--; 124 ref(size).obj->notInHeap(); 113 125 114 126 if( ref(i).import < ref(size).import ) 115 127 downheap(i); 116 128 else 117 129 upheap(i); 118 130 131 return &ref(size); 132 } 119 133 120 return &ref(size);121 }
Note: See TracChangeset
for help on using the changeset viewer.