Files Classes Functions Hierarchy
00001 #ifndef TRIANGLEUNIFORMSUBDIVISION_H 00002 #define TRIANGLEUNIFORMSUBDIVISION_H 00003 00004 #include <vector> 00005 using namespace std; 00006 00007 #include <print.h> 00008 #include <point.h> 00009 00021 template <typename T> 00022 class triangleuniformsubdivision 00023 { 00024 public: 00025 00027 vector< point3<uint> > & vi; 00029 vector< T > & pts; 00030 00032 triangleuniformsubdivision 00033 ( 00034 vector< point3<uint> > & vi_, 00035 vector< T > & pts_ 00036 ) : vi(vi_), pts(pts_) {} 00037 00040 void divide( point3<uint> const & ti ); 00041 00044 void divide( vector< point3<uint> > const & v2 ) 00045 { uintc sz(v2.size()); for (uint i=0; i<sz; ++i) divide(v2[i]); } 00046 00049 void divide( point3<uint> const & ti, uintc depth ); 00050 00053 void divide( vector< point3<uint> > const & v2, uintc depth ) 00054 { uintc sz(v2.size()); for (uint i=0; i<sz; ++i) divide(v2[i],depth); } 00055 00056 }; 00057 00058 00059 //------------------------------------------------------------- 00060 // Implementation. 00061 00062 template <typename T> 00063 void triangleuniformsubdivision<T>::divide 00064 ( 00065 point3<uint> const & ti, 00066 uintc depth 00067 ) 00068 { 00069 static vector< point3<uint> > v0; 00070 static vector< point3<uint> > v1; 00071 static vector< point3<uint> >* pv[2]; 00072 00073 v0.clear(); 00074 v1.clear(); 00075 00076 pv[0] = & v0; 00077 pv[1] = & v1; 00078 00079 static triangleuniformsubdivision<T> v0d(v0,pts); 00080 static triangleuniformsubdivision<T> v1d(v1,pts); 00081 00082 static triangleuniformsubdivision<T>* vd[2]; 00083 vd[0] = & v0d; 00084 vd[1] = & v1d; 00085 00086 uint current=0; 00087 pv[1]->push_back(ti); 00088 00089 uint c2; 00090 for (uint i=0; i<depth; ++i) 00091 { 00092 c2 = (current+1)%2; 00093 pv[current]->clear(); 00094 vd[current]->divide(*pv[c2]); 00095 current = (current+1)%2; 00096 } 00097 00098 // Point to where the current geometry is. 00099 current = (current+1)%2; 00100 uintc sz = pv[current]->size(); 00101 for (uint i=0; i<sz; ++i) 00102 vi.push_back( (*pv[current])[i] ); 00103 } 00104 00105 template <typename T> 00106 void triangleuniformsubdivision<T>::divide( point3<uint> const & ti ) 00107 { 00108 uint i[6]; 00109 i[0] = ti.x; 00110 i[1] = ti.y; 00111 i[2] = ti.z; 00112 i[3] = pts.size(); 00113 i[4] = i[3]+1; 00114 i[5] = i[4]+1; 00115 00116 vi.push_back( point3<uint>(i[3],i[1],i[4]) ); 00117 vi.push_back( point3<uint>(i[5],i[3],i[4]) ); 00118 vi.push_back( point3<uint>(i[5],i[4],i[2]) ); 00119 vi.push_back( point3<uint>(i[0],i[3],i[5]) ); 00120 00121 T const & p0( pts[i[0]] ); 00122 T const & p1( pts[i[1]] ); 00123 T const & p2( pts[i[2]] ); 00124 00125 // Problem with temporaries, explicit code because 00126 // compiler generated crap. 00127 00128 T q0(p0); 00129 q0 += p1; 00130 q0 *= 0.5; 00131 T q1(p1); 00132 q1 += p2; 00133 q1 *= 0.5; 00134 T q2(p2); 00135 q2 += p0; 00136 q2 *= 0.5; 00137 00138 pts.push_back(q0); 00139 pts.push_back(q1); 00140 pts.push_back(q2); 00141 00142 /* 00143 cout << SHOW(q0) << endl; 00144 cout << SHOW(q1) << endl; 00145 cout << SHOW(q2) << endl; 00146 */ 00147 00148 00149 /* 00150 cout << SHOW(p0) << endl; 00151 cout << SHOW(p1) << endl; 00152 cout << SHOW(p2) << endl; 00153 00154 pts.push_back((p0+p1)*0.5); 00155 pts.push_back((p1+p2)*0.5); 00156 pts.push_back((p2+p0)*0.5); 00157 00158 cout << SHOW(p0+p1) << endl; 00159 cout << SHOW(p1+p2) << endl; 00160 cout << SHOW(p2+p0) << endl; 00161 00162 cout << SHOW(p0) << endl; 00163 cout << SHOW(p1) << endl; 00164 cout << SHOW(p2) << endl; 00165 */ 00166 00167 } 00168 00169 00170 00171 00172 00173 00174 00175 00176 #endif 00177 00178
1.5.8