library of assembled shared sources

http://lass.cocamware.com

split_heuristics.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_SPLIT_HEURISTICS_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_SPAT_SPLIT_HEURISTICS_H
00045 
00046 #include "spat_common.h"
00047 
00048 namespace lass
00049 {
00050 namespace spat
00051 {
00052 
00053 template <typename ObjectTraits>
00054 struct SplitInfo
00055 {
00056     typedef typename ObjectTraits::TAabb TAabb;
00057     typedef typename ObjectTraits::TValue TValue;
00058     typedef int TAxis;
00059 
00060     SplitInfo(const TAabb& aabb, TValue x, TAxis axis):
00061         aabb(aabb), x(x), axis(axis)
00062     {
00063     }
00064 
00065     TAabb aabb;
00066     TValue x;
00067     TAxis axis;
00068 };
00069 
00070 template <int numObjectsPerLeaf = 1>
00071 struct DefaultSplitHeuristics
00072 {
00073     LASS_META_ASSERT(numObjectsPerLeaf > 0, numObjectsPerLeaf_must_be_strict_positive);
00074     
00075     template <typename ObjectTraits, typename RandomIterator>
00076     static SplitInfo<ObjectTraits> split(RandomIterator first, RandomIterator last)
00077     {
00078         typedef typename ObjectTraits::TAabb TAabb;
00079         typedef typename ObjectTraits::TPoint TPoint;
00080         typedef typename ObjectTraits::TValue TValue;
00081 
00082         LASS_ASSERT(numObjectsPerLeaf > 0);
00083 
00084         TAabb aabb = ObjectTraits::aabbEmpty();
00085         for (RandomIterator i = first; i != last; ++i)
00086         {
00087             aabb = ObjectTraits::aabbJoin(aabb, i->aabb);
00088         }
00089 
00090         const int n = static_cast<int>(last - first);
00091         if (n <= numObjectsPerLeaf)
00092         {
00093             return SplitInfo<ObjectTraits>(aabb, 0, -1);
00094         }
00095         
00096         const TPoint min = ObjectTraits::aabbMin(aabb);
00097         const TPoint max = ObjectTraits::aabbMax(aabb);
00098         int axis = 0;
00099         TValue maxDistance = ObjectTraits::coord(max, 0) - ObjectTraits::coord(min, 0);
00100         for (int k = 1; k < ObjectTraits::dimension; ++k)
00101         {
00102             const TValue distance = ObjectTraits::coord(max, k) - ObjectTraits::coord(min, k);
00103             if (distance > maxDistance)
00104             {
00105                 axis = k;
00106                 maxDistance = distance;
00107             }
00108         }
00109 
00110         const TValue x = (ObjectTraits::coord(min, axis) + ObjectTraits::coord(max, axis)) / 2;
00111         return SplitInfo<ObjectTraits>(aabb, x, axis);
00112     }
00113 };
00114 
00115 namespace impl
00116 {
00117 
00118 template <typename ObjectTraits>
00119 class Splitter
00120 {
00121 public:
00122     Splitter(const SplitInfo<ObjectTraits>& split): split_(split) {}
00123     template <typename Input> bool operator()(const Input& input) const
00124     {
00125         typedef ObjectTraits OT;
00126         const typename OT::TValue x = 
00127             (OT::coord(OT::aabbMin(input.aabb), split_.axis) + OT::coord(OT::aabbMax(input.aabb), split_.axis)) / 2;
00128         return x < split_.x;
00129     }           
00130 private:
00131     SplitInfo<ObjectTraits> split_;
00132 };
00133 
00134 template <typename ObjectTraits>
00135 class LessAxis
00136 {
00137 public:
00138     LessAxis(int iAxis): axis_(iAxis) {}
00139     template <typename Input> bool operator()(const Input& a, const Input& b) const
00140     {
00141         typedef ObjectTraits OT;
00142         const typename OT::TValue xa = 
00143             (OT::coord(OT::aabbMin(a.aabb), axis_) + OT::coord(OT::aabbMax(a.aabb), axis_)) / 2;
00144         const typename OT::TValue xb = 
00145             (OT::coord(OT::aabbMin(b.aabb), axis_) + OT::coord(OT::aabbMax(b.aabb), axis_)) / 2;
00146         return xa < xb;
00147     }           
00148 private:
00149     int axis_;
00150 };
00151 
00152 }
00153 
00154 }
00155 
00156 }
00157 
00158 #endif
00159 
00160 // EOF

Generated on Mon Nov 10 14:21:40 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo