Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
BSPTreeNode.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision: 10745 $ *
45 * $LastChangedBy: moebius $ *
46 * $Date: 2011-01-26 10:23:50 +0100 (Wed, 26 Jan 2011) $ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS TreeNode
56 //
57 //=============================================================================
58 
59 #ifndef MB_BSPTREENODE_HH
60 #define MB_BSPTREENODE_HH
61 
62 //== INCLUDES =================================================================
63 
64 #include <ACG/Geometry/Types/PlaneT.hh>
65 #include <ACG/Geometry/Algorithms.hh>
66 #include <ostream>
67 
68 //== CLASS DEFINITION =========================================================
69 
70 // Node of the tree: contains parent, children and splitting plane
71 template <class BSPTraits>
72 struct TreeNode
73 {
74  typedef typename BSPTraits::Handle Handle;
75  typedef typename BSPTraits::Point Point;
76  typedef typename BSPTraits::VertexHandle VertexHandle;
77  typedef std::vector<Handle> Handles;
78  typedef typename Handles::iterator HandleIter;
79  typedef typename Handles::const_iterator HandleConstIter;
80  typedef typename Point::value_type Scalar;
82 
83  TreeNode(const Handles& _handles, TreeNode* _parent)
84  : handles_(_handles),
85  parent_(_parent), left_child_(0), right_child_(0) {}
86  ~TreeNode()
87  {
88  delete left_child_;
89  delete right_child_;
90 
91  if (parent_)
92  {
93  if (this == parent_->left_child_)
94  parent_->left_child_ = 0;
95  else
96  parent_->right_child_ = 0;
97  }
98  }
99 
100  HandleIter begin() {
101  return handles_.begin();
102  }
103 
104  HandleIter end() {
105  return handles_.end();
106  }
107 
108  HandleConstIter begin() const {
109  return handles_.begin();
110  }
111 
112  HandleConstIter end() const {
113  return handles_.end();
114  }
115 
116  size_t size() const {
117  return handles_.size();
118  }
119 
120  Handles handles_;
121  TreeNode *parent_, *left_child_, *right_child_;
122  Plane plane_;
123  Point bb_min, bb_max;
124 
126  template< typename MeshT >
127  void visualizeTree(MeshT *_object, int _max_depth)
128  {
129  if (_max_depth > 0 && (left_child_ || right_child_) )
130  {
131  if (left_child_)
132  left_child_->visualizeTree(_object, _max_depth-1);
133  if (right_child_)
134  right_child_->visualizeTree(_object, _max_depth-1);
135  }
136  else
137  {
138  Point size_ = bb_max - bb_min;
139 
140  std::vector<VertexHandle> vhandle(8);
141  vhandle[0] = _object->add_vertex(bb_min+Point(0.0,0.0,size_[2]));
142  vhandle[1] = _object->add_vertex(bb_min+Point(size_[0],0.0,size_[2]));
143  vhandle[2] = _object->add_vertex(bb_min+Point(size_[0],size_[1],size_[2]));
144  vhandle[3] = _object->add_vertex(bb_min+Point(0.0,size_[1],size_[2]));
145  vhandle[4] = _object->add_vertex(bb_min+Point(0.0,0.0,0.0));
146  vhandle[5] = _object->add_vertex(bb_min+Point(size_[0],0.0,0.0));
147  vhandle[6] = _object->add_vertex(bb_min+Point(size_[0],size_[1],0.0));
148  vhandle[7] = _object->add_vertex(bb_min+Point(0.0,size_[1],0.0));
149 
150 
151  // generate (quadrilateral) faces
152  std::vector<VertexHandle> face_vhandles;
153 
154  face_vhandles.clear();
155  face_vhandles.push_back(vhandle[0]);
156  face_vhandles.push_back(vhandle[1]);
157  face_vhandles.push_back(vhandle[2]);
158  face_vhandles.push_back(vhandle[3]);
159  _object->add_face(face_vhandles);
160 
161  face_vhandles.clear();
162  face_vhandles.push_back(vhandle[7]);
163  face_vhandles.push_back(vhandle[6]);
164  face_vhandles.push_back(vhandle[5]);
165  face_vhandles.push_back(vhandle[4]);
166  _object->add_face(face_vhandles);
167 
168  face_vhandles.clear();
169  face_vhandles.push_back(vhandle[1]);
170  face_vhandles.push_back(vhandle[0]);
171  face_vhandles.push_back(vhandle[4]);
172  face_vhandles.push_back(vhandle[5]);
173  _object->add_face(face_vhandles);
174 
175  face_vhandles.clear();
176  face_vhandles.push_back(vhandle[2]);
177  face_vhandles.push_back(vhandle[1]);
178  face_vhandles.push_back(vhandle[5]);
179  face_vhandles.push_back(vhandle[6]);
180  _object->add_face(face_vhandles);
181 
182  face_vhandles.clear();
183  face_vhandles.push_back(vhandle[3]);
184  face_vhandles.push_back(vhandle[2]);
185  face_vhandles.push_back(vhandle[6]);
186  face_vhandles.push_back(vhandle[7]);
187  _object->add_face(face_vhandles);
188 
189  face_vhandles.clear();
190  face_vhandles.push_back(vhandle[0]);
191  face_vhandles.push_back(vhandle[3]);
192  face_vhandles.push_back(vhandle[7]);
193  face_vhandles.push_back(vhandle[4]);
194  _object->add_face(face_vhandles);
195  }
196  }
197 
198  private:
199  /*
200  * Noncopyable because of root_.
201  */
202  TreeNode(const TreeNode &rhs);
203  TreeNode &operator=(const TreeNode &rhs);
204 
205 };
206 
207 template<class BSPTraits>
208 std::ostream &operator<< (std::ostream &stream, const TreeNode<BSPTraits> &node) {
209  stream << "[TreeNode instance. Handles: ";
210  for (typename TreeNode<BSPTraits>::Handles::const_iterator it = node.handles_.begin(), it_end = node.handles_.end();
211  it != it_end; ++it) {
212  stream << it->idx();
213  if (it < it_end-1) stream << ", ";
214  }
215  stream << ", parent: " << node.parent_ << ", left_child_: " << node.left_child_
216  << ", right_child_: " << node.right_child_ << ", plane_: <not implemented>, bb_min: "
217  << node.bb_min << ", bb_max: " << node.bb_max << ", size(): " << node.size() << "]";
218  return stream;
219 }
220 
221 //=============================================================================
222 #endif // MB_BSPTREENODE_HH defined
223 //=============================================================================
void visualizeTree(MeshT *_object, int _max_depth)
This visualizes the bounding boxes.
Definition: BSPTreeNode.hh:127
BaseNode * parent_
pointer to parent node
Definition: BaseNode.hh:741