<html> <head> <title>OgreOctree.cpp Source File - OGRE Documentation</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link type="text/css" rel="stylesheet" href="style.css"> </head> <body> <!-- Generated by Doxygen 1.2.16 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="namespacemembers.html">Namespace Members</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><h1>OgreOctree.cpp</h1><a href="OgreOctree_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">/***************************************************************************</font> 00002 <font class="comment">octree.cpp - description</font> 00003 <font class="comment">-------------------</font> 00004 <font class="comment">begin : Mon Sep 30 2002</font> 00005 <font class="comment">copyright : (C) 2002 by Jon Anderson</font> 00006 <font class="comment">email : janders@users.sf.net</font> 00007 <font class="comment">***************************************************************************/</font> 00008 00009 <font class="comment">/***************************************************************************</font> 00010 <font class="comment">* *</font> 00011 <font class="comment">* This program is free software; you can redistribute it and/or modify *</font> 00012 <font class="comment">* it under the terms of the GNU Lesser General Public License as *</font> 00013 <font class="comment">* published by the Free Software Foundation; either version 2 of the * </font> 00014 <font class="comment">* License, or (at your option) any later version. *</font> 00015 <font class="comment">* *</font> 00016 <font class="comment">***************************************************************************/</font> 00017 00018 <font class="preprocessor">#include <<a class="code" href="OgreOctree_8h.html">OgreOctree.h</a>></font> 00019 <font class="preprocessor">#include <<a class="code" href="OgreOctreeNode_8h.html">OgreOctreeNode.h</a>></font> 00020 00021 <font class="keyword">namespace </font>Ogre 00022 { 00023 <a name="l00026"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea6">00026</a> <font class="keywordtype">bool</font> Octree::_isTwiceSize( <a class="code" href="classOgre_1_1AxisAlignedBox.html">AxisAlignedBox</a> &box ) 00027 { 00028 <font class="keyword">const</font> <a class="code" href="classOgre_1_1Vector3.html">Vector3</a> * pts1 = <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem0">mBox</a>.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa11">getAllCorners</a>(); 00029 <font class="keyword">const</font> <a class="code" href="classOgre_1_1Vector3.html">Vector3</a> * pts2 = box.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa11">getAllCorners</a>(); 00030 00031 <font class="keywordflow">return</font> ( ( pts2[ 4 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m0">x</a> -pts2[ 0 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m0">x</a> ) <= ( pts1[ 4 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m0">x</a> - pts1[ 0 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m0">x</a> ) / 2 ) && 00032 ( ( pts2[ 4 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m1">y</a> - pts2[ 0 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m1">y</a> ) <= ( pts1[ 4 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m1">y</a> - pts1[ 0 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m1">y</a> ) / 2 ) && 00033 ( ( pts2[ 4 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m2">z</a> - pts2[ 0 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m2">z</a> ) <= ( pts1[ 4 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m2">z</a> - pts1[ 0 ].<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m2">z</a> ) / 2 ) ; 00034 00035 } 00036 <a name="l00041"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea7">00041</a> <font class="keywordtype">void</font> Octree::_getChildIndexes( <a class="code" href="classOgre_1_1AxisAlignedBox.html">AxisAlignedBox</a> &box, <font class="keywordtype">int</font> *x, <font class="keywordtype">int</font> *y, <font class="keywordtype">int</font> *z ) 00042 { 00043 <a class="code" href="classOgre_1_1Vector3.html">Vector3</a> max = <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem0">mBox</a>.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa4">getMaximum</a>(); 00044 <a class="code" href="classOgre_1_1Vector3.html">Vector3</a> min = box.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa3">getMinimum</a>(); 00045 00046 <a class="code" href="classOgre_1_1Vector3.html">Vector3</a> center = <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem0">mBox</a>.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa4">getMaximum</a>().<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3a26">midPoint</a>( <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem0">mBox</a>.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa3">getMinimum</a>() ); 00047 00048 <a class="code" href="classOgre_1_1Vector3.html">Vector3</a> ncenter = box.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa4">getMaximum</a>().<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3a26">midPoint</a>( box.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa3">getMinimum</a>() ); 00049 00050 <font class="keywordflow">if</font> ( ncenter.<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m0">x</a> > center.<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m0">x</a> ) 00051 * x = 1; 00052 <font class="keywordflow">else</font> 00053 *x = 0; 00054 00055 <font class="keywordflow">if</font> ( ncenter.<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m1">y</a> > center.<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m1">y</a> ) 00056 * y = 1; 00057 <font class="keywordflow">else</font> 00058 *y = 0; 00059 00060 <font class="keywordflow">if</font> ( ncenter.<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m2">z</a> > center.<a class="code" href="classOgre_1_1Vector3.html#Ogre_1_1Vector3m2">z</a> ) 00061 * z = 1; 00062 <font class="keywordflow">else</font> 00063 *z = 0; 00064 00065 } 00066 <a name="l00067"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea0">00067</a> Octree::Octree( <a class="code" href="classOgre_1_1Octree.html">Octree</a> * parent ) 00068 : mWireBoundingBox(0), 00069 mHalfSize( 0, 0, 0 ) 00070 { 00071 <font class="comment">//initialize all children to null.</font> 00072 <font class="keywordflow">for</font> ( <font class="keywordtype">int</font> i = 0; i < 2; i++ ) 00073 { 00074 <font class="keywordflow">for</font> ( <font class="keywordtype">int</font> j = 0; j < 2; j++ ) 00075 { 00076 <font class="keywordflow">for</font> ( <font class="keywordtype">int</font> k = 0; k < 2; k++ ) 00077 { 00078 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem3">mChildren</a>[ i ][ j ][ k ] = 0; 00079 } 00080 } 00081 } 00082 00083 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreen1">mParent</a> = parent; 00084 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreen0">mNumNodes</a> = 0; 00085 } 00086 <a name="l00087"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea1">00087</a> Octree::~Octree() 00088 { 00089 <font class="comment">//initialize all children to null.</font> 00090 <font class="keywordflow">for</font> ( <font class="keywordtype">int</font> i = 0; i < 2; i++ ) 00091 { 00092 <font class="keywordflow">for</font> ( <font class="keywordtype">int</font> j = 0; j < 2; j++ ) 00093 { 00094 <font class="keywordflow">for</font> ( <font class="keywordtype">int</font> k = 0; k < 2; k++ ) 00095 { 00096 <font class="keywordflow">if</font> ( <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem3">mChildren</a>[ i ][ j ][ k ] != 0 ) 00097 <font class="keyword">delete</font> <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem3">mChildren</a>[ i ][ j ][ k ]; 00098 } 00099 } 00100 } 00101 00102 <font class="keywordflow">if</font>(mWireBoundingBox) 00103 <font class="keyword">delete</font> <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem1">mWireBoundingBox</a>; 00104 00105 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreen1">mParent</a> = 0; 00106 } 00107 <a name="l00108"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea2">00108</a> <font class="keywordtype">void</font> Octree::_addNode( <a class="code" href="classOgre_1_1OctreeNode.html">OctreeNode</a> * n ) 00109 { 00110 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem4">mNodes</a>.push_back( n ); 00111 n -> setOctant( <font class="keyword">this</font> ); 00112 00113 <font class="comment">//update total counts.</font> 00114 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreeb0">_ref</a>(); 00115 00116 } 00117 <a name="l00118"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea3">00118</a> <font class="keywordtype">void</font> Octree::_removeNode( <a class="code" href="classOgre_1_1OctreeNode.html">OctreeNode</a> * n ) 00119 { 00120 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem4">mNodes</a>.erase( std::find( <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem4">mNodes</a>.begin(), <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem4">mNodes</a>.end(), n ) ); 00121 n -> setOctant( 0 ); 00122 00123 <font class="comment">//update total counts.</font> 00124 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreeb1">_unref</a>(); 00125 } 00126 <a name="l00127"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea8">00127</a> <font class="keywordtype">void</font> Octree::_getCullBounds( <a class="code" href="classOgre_1_1AxisAlignedBox.html">AxisAlignedBox</a> *b ) 00128 { 00129 <font class="keyword">const</font> <a class="code" href="classOgre_1_1Vector3.html">Vector3</a> * corners = <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem0">mBox</a>.<a class="code" href="classOgre_1_1AxisAlignedBox.html#Ogre_1_1AxisAlignedBoxa11">getAllCorners</a>(); 00130 b -> setExtents( corners[ 0 ] - <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem2">mHalfSize</a>, corners[ 4 ] + <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem2">mHalfSize</a> ); 00131 } 00132 <a name="l00133"></a><a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreea5">00133</a> <a class="code" href="classOgre_1_1WireBoundingBox.html">WireBoundingBox</a>* Octree::getWireBoundingBox() 00134 { 00135 <font class="comment">// Create a WireBoundingBox if needed</font> 00136 <font class="keywordflow">if</font>(<a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem1">mWireBoundingBox</a> == 0) 00137 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem1">mWireBoundingBox</a> = <font class="keyword">new</font> <a class="code" href="classOgre_1_1WireBoundingBox.html">WireBoundingBox</a>(); 00138 00139 <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem1">mWireBoundingBox</a>-><a class="code" href="classOgre_1_1WireBoundingBox.html#Ogre_1_1WireBoundingBoxa2">setupBoundingBox</a>(<a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem0">mBox</a>); 00140 <font class="keywordflow">return</font> <a class="code" href="classOgre_1_1Octree.html#Ogre_1_1Octreem1">mWireBoundingBox</a>; 00141 } 00142 00143 } </pre></div><p> Copyright © 2002-2003 by The OGRE Team<br /> Last modified Wed Jan 21 00:10:19 2004 </p> </body> </html>