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 octree.cpp  -  description
00003 -------------------
00004 begin                : Mon Sep 30 2002
00005 copyright            : (C) 2002 by Jon Anderson
00006 email                : janders@users.sf.net
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010 *                                                                         *
00011 *   This program is free software; you can redistribute it and/or modify  *
00012 *   it under the terms of the GNU Lesser General Public License as        *
00013 *   published by the Free Software Foundation; either version 2 of the    * 
00014 *   License, or (at your option) any later version.                       *
00015 *                                                                         *
00016 ***************************************************************************/
00017 
00018 #include <OgreOctree.h>
00019 #include <OgreOctreeNode.h>
00020 
00021 namespace Ogre
00022 {
00023 
00026 bool Octree::_isTwiceSize( AxisAlignedBox &box )
00027 {
00028     const Vector3 * pts1 = mBox.getAllCorners();
00029     const Vector3 * pts2 = box.getAllCorners();
00030 
00031     return ( ( pts2[ 4 ].x -pts2[ 0 ].x ) <= ( pts1[ 4 ].x - pts1[ 0 ].x ) / 2 ) &&
00032            ( ( pts2[ 4 ].y - pts2[ 0 ].y ) <= ( pts1[ 4 ].y - pts1[ 0 ].y ) / 2 ) &&
00033            ( ( pts2[ 4 ].z - pts2[ 0 ].z ) <= ( pts1[ 4 ].z - pts1[ 0 ].z ) / 2 ) ;
00034 
00035 }
00036 
00041 void Octree::_getChildIndexes( AxisAlignedBox &box, int *x, int *y, int *z )
00042 {
00043     Vector3 max = mBox.getMaximum();
00044     Vector3 min = box.getMinimum();
00045 
00046     Vector3 center = mBox.getMaximum().midPoint( mBox.getMinimum() );
00047 
00048     Vector3 ncenter = box.getMaximum().midPoint( box.getMinimum() );
00049 
00050     if ( ncenter.x > center.x )
00051         * x = 1;
00052     else
00053         *x = 0;
00054 
00055     if ( ncenter.y > center.y )
00056         * y = 1;
00057     else
00058         *y = 0;
00059 
00060     if ( ncenter.z > center.z )
00061         * z = 1;
00062     else
00063         *z = 0;
00064 
00065 }
00066 
00067 Octree::Octree( Octree * parent ) 
00068     : mWireBoundingBox(0),
00069       mHalfSize( 0, 0, 0 )
00070 {
00071     //initialize all children to null.
00072     for ( int i = 0; i < 2; i++ )
00073     {
00074         for ( int j = 0; j < 2; j++ )
00075         {
00076             for ( int k = 0; k < 2; k++ )
00077             {
00078                 mChildren[ i ][ j ][ k ] = 0;
00079             }
00080         }
00081     }
00082 
00083     mParent = parent;
00084     mNumNodes = 0;
00085 }
00086 
00087 Octree::~Octree()
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                 if ( mChildren[ i ][ j ][ k ] != 0 )
00097                     delete mChildren[ i ][ j ][ k ];
00098             }
00099         }
00100     }
00101 
00102     if(mWireBoundingBox)
00103         delete mWireBoundingBox;
00104 
00105     mParent = 0;
00106 }
00107 
00108 void Octree::_addNode( OctreeNode * n )
00109 {
00110     mNodes.push_back( n );
00111     n -> setOctant( this );
00112 
00113     //update total counts.
00114     _ref();
00115 
00116 }
00117 
00118 void Octree::_removeNode( OctreeNode * n )
00119 {
00120     mNodes.erase( std::find( mNodes.begin(), mNodes.end(), n ) );
00121     n -> setOctant( 0 );
00122 
00123     //update total counts.
00124     _unref();
00125 }
00126 
00127 void Octree::_getCullBounds( AxisAlignedBox *b )
00128 {
00129     const Vector3 * corners = mBox.getAllCorners();
00130     b -> setExtents( corners[ 0 ] - mHalfSize, corners[ 4 ] + mHalfSize );
00131 }
00132 
00133 WireBoundingBox* Octree::getWireBoundingBox()
00134 {
00135     // Create a WireBoundingBox if needed
00136     if(mWireBoundingBox == 0)
00137         mWireBoundingBox = new WireBoundingBox();
00138 
00139     mWireBoundingBox->setupBoundingBox(mBox);
00140     return mWireBoundingBox;
00141 }
00142 
00143 }

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:28 2004