TriangleBSPCoreT.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6727 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 08:03:50 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS TriangleBSPCoreT
00049 //
00050 //=============================================================================
00051 
00052 #define TRIANGLEBSPCORET_C
00053 
00054 //== INCLUDES =================================================================
00055 
00056 
00057 #include "TriangleBSPCoreT.hh"
00058 
00059 
00060 //== CLASS DEFINITION =========================================================
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   // init
00100   delete root_;
00101   root_ = new Node(handles_, 0);
00102 
00103   // delete own handles (don't store them twice)
00104   handles_ = Handles();
00105 
00106   // call recursive helper
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   // should we stop at this level ?
00122   if ((_depth == 0) || ((_node->end()-_node->begin()) <= (int)_max_handles))
00123     return;
00124 
00125 
00126 
00127   // compute bounding box
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   // split longest side of bounding box
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   // construct splitting plane
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   // partition for left and right child
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   // check it
00181   if (lhandles.size() == _node->handles_.size() ||
00182       rhandles.size() == _node->handles_.size())
00183     return;
00184   else
00185     _node->handles_ = Handles();
00186 
00187 
00188   // create children
00189   _node->left_child_  = new Node(lhandles, _node);  lhandles = Handles();
00190   _node->right_child_ = new Node(rhandles, _node);  rhandles = Handles();
00191 
00192 
00193 
00194   // recurse to childen
00195   _build(_node->left_child_,  _max_handles, _depth-1);
00196   _build(_node->right_child_, _max_handles, _depth-1);
00197 }
00198 
00199 
00200 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .