Developer Documentation
TriangleBSPCoreT_impl.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 //=============================================================================
45 //
46 // CLASS TriangleBSPCoreT
47 //
48 //=============================================================================
49 
50 #define TRIANGLEBSPCORET_C
51 
52 //== INCLUDES =================================================================
53 
54 
55 #include "TriangleBSPCoreT.hh"
56 
57 
58 //== CLASS DEFINITION =========================================================
59 
60 template <class BSPTraits>
61 void
63 build(unsigned int _max_handles, unsigned int _max_depth)
64 {
65  // init
66  delete root_;
67  root_ = new Node(handles_, 0);
68 
69  // delete own handles (don't store them twice)
70  handles_ = Handles();
71 
72  nodes=1;
73  traits_.calculateBoundingBoxRoot (root_);
74  // call recursive helper
75  _build(root_, _max_handles, _max_depth);
76 
77 }
78 
79 
80 //-----------------------------------------------------------------------------
81 
82 
83 template <class BSPTraits>
84 void
86 _build(Node* _node,
87  unsigned int _max_handles,
88  unsigned int _depth)
89 {
90  // should we stop at this level ?
91  if ((_depth == 0) || ((_node->end()-_node->begin()) <= (int)_max_handles))
92  return;
93 
94  Point median;
95  int axis;
96  // compute bounding boxes for children
97  traits_.calculateBoundingBox (_node, median, axis);
98 
99  // construct splitting plane
100  const Point XYZ[3] = { Point(1,0,0), Point(0,1,0), Point(0,0,1) };
101  _node->plane_ = Plane(median, XYZ[axis]);
102 
103  // partition for left and right child
104  Handles lhandles, rhandles;
105  lhandles.reserve(_node->handles_.size()/2);
106  rhandles.reserve(_node->handles_.size()/2);
107 
108  HandleIter it;
109  Point p0, p1, p2;
110  for (it=_node->begin(); it!=_node->end(); ++it)
111  {
112  traits_.points(*it, p0, p1, p2);
113 
114  /* @TODO Remove this comment block. Replaced by the clause below
115 
116  if (_node->plane_(p0)) left = true;
117  else right = true;
118  if (_node->plane_(p1)) left = true;
119  else right = true;
120  if (_node->plane_(p2)) left = true;
121  else right = true;
122 
123  if (left) lhandles.push_back(*it);
124  if (right) rhandles.push_back(*it);
125  */
126 
127  if ( _node->plane_(p0) || _node->plane_(p1) || _node->plane_(p2) ) lhandles.push_back(*it);
128  if ( !_node->plane_(p0) || !_node->plane_(p1) || !_node->plane_(p2) ) rhandles.push_back(*it);
129 
130  }
131 
132  // check it
133  if (lhandles.size() == _node->handles_.size() ||
134  rhandles.size() == _node->handles_.size())
135  return;
136  else
137  _node->handles_ = Handles();
138 
139 
140  // create children
141  _node->left_child_ = new Node(lhandles, _node); lhandles = Handles();
142  _node->right_child_ = new Node(rhandles, _node); rhandles = Handles();
143  nodes+=2;
144 
145  //save bounding boxes for children
146  /*
147  _node->left_child_->bb_min = _node->bb_min;
148  _node->left_child_->bb_max = _node->bb_max;
149  _node->left_child_->bb_max[axis] = median [axis];
150 
151  _node->right_child_->bb_min = _node->bb_min;
152  _node->right_child_->bb_min[axis] = median [axis];
153  _node->right_child_->bb_max = _node->bb_max;
154  */
155  _node->right_child_->bb_min = _node->bb_min;
156  _node->right_child_->bb_max = _node->bb_max;
157  _node->right_child_->bb_max[axis] = median [axis];
158 
159  _node->left_child_->bb_min = _node->bb_min;
160  _node->left_child_->bb_min[axis] = median [axis];
161  _node->left_child_->bb_max = _node->bb_max;
162 
163  // recurse to childen
164  _build(_node->left_child_, _max_handles, _depth-1);
165  _build(_node->right_child_, _max_handles, _depth-1);
166 }
167 
168 //=============================================================================
169 
170 
void build(unsigned int _max_handles, unsigned int _max_depth)