TriangleBSPCoreT.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #define TRIANGLEBSPCORET_C
00053
00054
00055
00056
00057 #include "TriangleBSPCoreT.hh"
00058
00059
00060
00061
00062
00063 template <class BSPTraits>
00064 TriangleBSPCoreT<BSPTraits>::Node::
00065 Node(const Handles& _handles, Node* _parent)
00066 : handles_(_handles),
00067 parent_(_parent), left_child_(0), right_child_(0)
00068 {}
00069
00070
00071
00072
00073
00074 template <class BSPTraits>
00075 TriangleBSPCoreT<BSPTraits>::Node::
00076 ~Node()
00077 {
00078 delete left_child_;
00079 delete right_child_;
00080
00081 if (parent_)
00082 {
00083 if (this == parent_->left_child_)
00084 parent_->left_child_ = 0;
00085 else
00086 parent_->right_child_ = 0;
00087 }
00088 }
00089
00090
00091
00092
00093
00094 template <class BSPTraits>
00095 void
00096 TriangleBSPCoreT<BSPTraits>::
00097 build(unsigned int _max_handles, unsigned int _max_depth)
00098 {
00099
00100 delete root_;
00101 root_ = new Node(handles_, 0);
00102
00103
00104 handles_ = Handles();
00105
00106
00107 _build(root_, _max_handles, _max_depth);
00108 }
00109
00110
00111
00112
00113
00114 template <class BSPTraits>
00115 void
00116 TriangleBSPCoreT<BSPTraits>::
00117 _build(Node* _node,
00118 unsigned int _max_handles,
00119 unsigned int _depth)
00120 {
00121
00122 if ((_depth == 0) || ((_node->end()-_node->begin()) <= (int)_max_handles))
00123 return;
00124
00125
00126
00127
00128 HandleIter it;
00129 Point p0, p1, p2;
00130 Point bb_min; bb_min.vectorize(FLT_MAX);
00131 Point bb_max; bb_max.vectorize(-FLT_MAX);
00132 for (it=_node->begin(); it!=_node->end(); ++it)
00133 {
00134 traits_.points(*it, p0, p1, p2);
00135 bb_min.minimize(p0);
00136 bb_min.minimize(p1);
00137 bb_min.minimize(p2);
00138 bb_max.maximize(p0);
00139 bb_max.maximize(p1);
00140 bb_max.maximize(p2);
00141 }
00142
00143
00144
00145 Point bb = bb_max - bb_min;
00146 Scalar length = bb[0];
00147 int axis = 0;
00148 if (bb[1] > length) length = bb[ (axis=1) ];
00149 if (bb[2] > length) length = bb[ (axis=2) ];
00150
00151
00152
00153 const Point XYZ[3] = { Point(1,0,0), Point(0,1,0), Point(0,0,1) };
00154 _node->plane_ = Plane((bb_min+bb_max)*0.5, XYZ[axis]);
00155
00156
00157
00158 Handles lhandles, rhandles;
00159 lhandles.reserve(_node->handles_.size()/2);
00160 rhandles.reserve(_node->handles_.size()/2);
00161
00162 bool left, right;
00163 for (it=_node->begin(); it!=_node->end(); ++it)
00164 {
00165 traits_.points(*it, p0, p1, p2);
00166 left = right = false;
00167
00168 if (_node->plane_(p0)) left = true;
00169 else right = true;
00170 if (_node->plane_(p1)) left = true;
00171 else right = true;
00172 if (_node->plane_(p2)) left = true;
00173 else right = true;
00174
00175 if (left) lhandles.push_back(*it);
00176 if (right) rhandles.push_back(*it);
00177 }
00178
00179
00180
00181 if (lhandles.size() == _node->handles_.size() ||
00182 rhandles.size() == _node->handles_.size())
00183 return;
00184 else
00185 _node->handles_ = Handles();
00186
00187
00188
00189 _node->left_child_ = new Node(lhandles, _node); lhandles = Handles();
00190 _node->right_child_ = new Node(rhandles, _node); rhandles = Handles();
00191
00192
00193
00194
00195 _build(_node->left_child_, _max_handles, _depth-1);
00196 _build(_node->right_child_, _max_handles, _depth-1);
00197 }
00198
00199
00200