Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreOctree.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2004 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 /***************************************************************************
00026 octree.cpp  -  description
00027 -------------------
00028 begin                : Mon Sep 30 2002
00029 copyright            : (C) 2002 by Jon Anderson
00030 email                : janders@users.sf.net
00031 
00032 Enhancements 2003 - 2004 (C) The OGRE Team
00033 
00034 ***************************************************************************/
00035 
00036 #include <OgreOctree.h>
00037 #include <OgreOctreeNode.h>
00038 
00039 namespace Ogre
00040 {
00041 
00044 bool Octree::_isTwiceSize( AxisAlignedBox &box )
00045 {
00046     const Vector3 * pts1 = mBox.getAllCorners();
00047     const Vector3 * pts2 = box.getAllCorners();
00048 
00049     return ( ( pts2[ 4 ].x -pts2[ 0 ].x ) <= ( pts1[ 4 ].x - pts1[ 0 ].x ) / 2 ) &&
00050            ( ( pts2[ 4 ].y - pts2[ 0 ].y ) <= ( pts1[ 4 ].y - pts1[ 0 ].y ) / 2 ) &&
00051            ( ( pts2[ 4 ].z - pts2[ 0 ].z ) <= ( pts1[ 4 ].z - pts1[ 0 ].z ) / 2 ) ;
00052 
00053 }
00054 
00059 void Octree::_getChildIndexes( AxisAlignedBox &box, int *x, int *y, int *z )
00060 {
00061     Vector3 max = mBox.getMaximum();
00062     Vector3 min = box.getMinimum();
00063 
00064     Vector3 center = mBox.getMaximum().midPoint( mBox.getMinimum() );
00065 
00066     Vector3 ncenter = box.getMaximum().midPoint( box.getMinimum() );
00067 
00068     if ( ncenter.x > center.x )
00069         * x = 1;
00070     else
00071         *x = 0;
00072 
00073     if ( ncenter.y > center.y )
00074         * y = 1;
00075     else
00076         *y = 0;
00077 
00078     if ( ncenter.z > center.z )
00079         * z = 1;
00080     else
00081         *z = 0;
00082 
00083 }
00084 
00085 Octree::Octree( Octree * parent ) 
00086     : mWireBoundingBox(0),
00087       mHalfSize( 0, 0, 0 )
00088 {
00089     //initialize all children to null.
00090     for ( int i = 0; i < 2; i++ )
00091     {
00092         for ( int j = 0; j < 2; j++ )
00093         {
00094             for ( int k = 0; k < 2; k++ )
00095             {
00096                 mChildren[ i ][ j ][ k ] = 0;
00097             }
00098         }
00099     }
00100 
00101     mParent = parent;
00102     mNumNodes = 0;
00103 }
00104 
00105 Octree::~Octree()
00106 {
00107     //initialize all children to null.
00108     for ( int i = 0; i < 2; i++ )
00109     {
00110         for ( int j = 0; j < 2; j++ )
00111         {
00112             for ( int k = 0; k < 2; k++ )
00113             {
00114                 if ( mChildren[ i ][ j ][ k ] != 0 )
00115                     delete mChildren[ i ][ j ][ k ];
00116             }
00117         }
00118     }
00119 
00120     if(mWireBoundingBox)
00121         delete mWireBoundingBox;
00122 
00123     mParent = 0;
00124 }
00125 
00126 void Octree::_addNode( OctreeNode * n )
00127 {
00128     mNodes.push_back( n );
00129     n -> setOctant( this );
00130 
00131     //update total counts.
00132     _ref();
00133 
00134 }
00135 
00136 void Octree::_removeNode( OctreeNode * n )
00137 {
00138     mNodes.erase( std::find( mNodes.begin(), mNodes.end(), n ) );
00139     n -> setOctant( 0 );
00140 
00141     //update total counts.
00142     _unref();
00143 }
00144 
00145 void Octree::_getCullBounds( AxisAlignedBox *b )
00146 {
00147     const Vector3 * corners = mBox.getAllCorners();
00148     b -> setExtents( corners[ 0 ] - mHalfSize, corners[ 4 ] + mHalfSize );
00149 }
00150 
00151 WireBoundingBox* Octree::getWireBoundingBox()
00152 {
00153     // Create a WireBoundingBox if needed
00154     if(mWireBoundingBox == 0)
00155         mWireBoundingBox = new WireBoundingBox();
00156 
00157     mWireBoundingBox->setupBoundingBox(mBox);
00158     return mWireBoundingBox;
00159 }
00160 
00161 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:36 2004