Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TriangleBSPT.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 TriangleBSPT
56 //
57 //=============================================================================
58 
59 #ifndef MB_TRIANGLEBSP_HH
60 #define MB_TRIANGLEBSP_HH
61 
62 
63 //== INCLUDES =================================================================
64 
65 #include "BSPTreeNode.hh"
66 #include "TriangleBSPCoreT.hh"
67 #include "BSPImplT.hh"
68 
69 //== CLASS DEFINITION =========================================================
70 #include <list>
71 
72 template <class BSPTraits>
73 class TriangleBSPT : public BSPImplT< TriangleBSPCoreT<BSPTraits> >
74 {
75 public:
77  typedef typename Base::Scalar Scalar;
78  TriangleBSPT(const BSPTraits& _traits,
79  const Scalar& _infinity = std::numeric_limits<Scalar>::infinity()) : Base(_traits, _infinity) {}
80 };
81 
82 //== CLASS DEFINITION =========================================================
83 
84 template <class Mesh>
86 {
87 public:
88 
89  typedef typename Mesh::Scalar Scalar;
90  typedef typename Mesh::Point Point;
91  typedef typename Mesh::FaceHandle Handle;
92  typedef std::vector<Handle> Handles;
93  typedef typename Handles::iterator HandleIter;
94  typedef TreeNode<Mesh> Node;
95 
96  OpenMeshTriangleBSPTraits(const Mesh& _mesh) : mesh_(_mesh) {}
97 
99  inline void points(const Handle _h, Point& _p0, Point& _p1, Point& _p2) const
100  {
101  typename Mesh::CFVIter fv_it(mesh_.cfv_iter(_h));
102  _p0 = mesh_.point(*fv_it);
103  ++fv_it;
104  _p1 = mesh_.point(*fv_it);
105  ++fv_it;
106  _p2 = mesh_.point(*fv_it);
107  }
108 
109  Scalar sqrdist(const Handle _h, const Point& _p) const
110  {
111  Point p0, p1, p2, q;
112  points(_h, p0, p1, p2);
113  return ACG::Geometry::distPointTriangleSquaredStable(_p, p0, p1, p2, q);
114  }
115 
116  void calculateBoundingBox(Node* _node, Point& median, int& axis)
117  {
118  //determine splitting axis
119  HandleIter it_h;
120  Point p0, p1, p2, bb_min, bb_max;
121  bb_min.vectorize(std::numeric_limits<typename Point::value_type>::infinity());
122  bb_max.vectorize(-std::numeric_limits<typename Point::value_type>::infinity());
123  std::list<Point> vertices;
124 
125  for (it_h = _node->begin(); it_h != _node->end(); ++it_h) {
126  points(*it_h, p0, p1, p2);
127  /*
128  bb_min.minimize(p0);
129  bb_min.minimize(p1);
130  bb_min.minimize(p2);
131  bb_max.maximize(p0);
132  bb_max.maximize(p1);
133  bb_max.maximize(p2);*/
134 
135  vertices.push_back(p0);
136  vertices.push_back(p1);
137  vertices.push_back(p2);
138  }
139  bb_min = _node->bb_min;
140  bb_max = _node->bb_max;
141 
142  // split longest side of bounding box
143  Point bb = bb_max - bb_min;
144  Scalar length = bb[0];
145  axis = 0;
146  if (bb[1] > length)
147  length = bb[(axis = 1)];
148  if (bb[2] > length)
149  length = bb[(axis = 2)];
150 
151  //calculate the median value in axis-direction
152  switch (axis) {
153  case 0:
154  vertices.sort(x_sort());
155  break;
156  case 1:
157  vertices.sort(y_sort());
158  break;
159  case 2:
160  vertices.sort(z_sort());
161  break;
162  }
163  vertices.unique();
164 
165  size_t size = vertices.size();
166  typename std::list<Point>::iterator it_v;
167  it_v = vertices.begin();
168  std::advance(it_v, size / 2);
169  median = *it_v;
170 
171  }
172 
173  void calculateBoundingBoxRoot(Node* _node)
174  {
175  HandleIter it;
176  Point p0, p1, p2, bb_min, bb_max;
177  bb_min.vectorize(FLT_MAX);
178  bb_max.vectorize(-FLT_MAX);
179  for (it = _node->begin(); it != _node->end(); ++it) {
180  points(*it, p0, p1, p2);
181  bb_min.minimize(p0);
182  bb_min.minimize(p1);
183  bb_min.minimize(p2);
184  bb_max.maximize(p0);
185  bb_max.maximize(p1);
186  bb_max.maximize(p2);
187  }
188  _node->bb_min = bb_min;
189  _node->bb_max = bb_max;
190  }
191 
192 private:
193 
194  const Mesh& mesh_;
195  //functors for sorting in different directions
196  struct x_sort { bool operator()(const Point& first, const Point& second) { return (first[0] < second[0]); } };
197  struct y_sort { bool operator()(const Point& first, const Point& second) { return (first[1] < second[1]); } };
198  struct z_sort { bool operator()(const Point& first, const Point& second) { return (first[2] < second[2]); } };
199 };
200 
201 
202 //== CLASS DEFINITION =========================================================
203 
204 
205 template <class Mesh>
207  : public TriangleBSPT<OpenMeshTriangleBSPTraits<Mesh> >
208 {
209 public:
211  typedef TriangleBSPT<Traits> Base;
212  typedef typename Traits::Scalar Scalar;
213  OpenMeshTriangleBSPT(const Mesh& _mesh,
214  const Scalar& _infinity = std::numeric_limits<Scalar>::infinity()) : Base(Traits(_mesh), _infinity) {}
215 };
216 
217 //=============================================================================
218 #endif // MB_TRIANGLEBSP_HH defined
219 //=============================================================================
void points(const Handle _h, Point &_p0, Point &_p1, Point &_p2) const
Returns the points belonging to the face handle _h.
Definition: TriangleBSPT.hh:99
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
void calculateBoundingBox(Node *_node, Point &median, int &axis)
Vec::value_type distPointTriangleSquaredStable(const Vec &_p, const Vec &_v0, const Vec &_v1, const Vec &_v2, Vec &_nearestPoint)
squared distance from point _p to triangle (_v0, _v1, _v2)
Definition: Algorithms.cc:476