Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/PlugIns/OctreeSceneManager/src/OgreOctree.cpp @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

File size: 4.3 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29/***************************************************************************
30octree.cpp  -  description
31-------------------
32begin                : Mon Sep 30 2002
33copyright            : (C) 2002 by Jon Anderson
34email                : janders@users.sf.net
35
36Enhancements 2003 - 2004 (C) The OGRE Team
37
38***************************************************************************/
39
40#include <OgreOctree.h>
41#include <OgreOctreeNode.h>
42
43namespace Ogre
44{
45
46/** Returns true is the box will fit in a child.
47*/
48bool Octree::_isTwiceSize( const AxisAlignedBox &box ) const
49{
50        // infinite boxes never fit in a child - always root node
51        if (box.isInfinite())
52                return false;
53
54    Vector3 halfMBoxSize = mBox.getHalfSize();
55    Vector3 boxSize = box.getSize();
56    return ((boxSize.x <= halfMBoxSize.x) && (boxSize.y <= halfMBoxSize.y) && (boxSize.z <= halfMBoxSize.z));
57
58}
59
60/** It's assumed the the given box has already been proven to fit into
61* a child.  Since it's a loose octree, only the centers need to be
62* compared to find the appropriate node.
63*/
64void Octree::_getChildIndexes( const AxisAlignedBox &box, int *x, int *y, int *z ) const
65{
66    Vector3 max = mBox.getMaximum();
67    Vector3 min = box.getMinimum();
68
69    Vector3 center = mBox.getMaximum().midPoint( mBox.getMinimum() );
70
71    Vector3 ncenter = box.getMaximum().midPoint( box.getMinimum() );
72
73    if ( ncenter.x > center.x )
74        * x = 1;
75    else
76        *x = 0;
77
78    if ( ncenter.y > center.y )
79        * y = 1;
80    else
81        *y = 0;
82
83    if ( ncenter.z > center.z )
84        * z = 1;
85    else
86        *z = 0;
87
88}
89
90Octree::Octree( Octree * parent ) 
91    : mWireBoundingBox(0),
92      mHalfSize( 0, 0, 0 )
93{
94    //initialize all children to null.
95    for ( int i = 0; i < 2; i++ )
96    {
97        for ( int j = 0; j < 2; j++ )
98        {
99            for ( int k = 0; k < 2; k++ )
100            {
101                mChildren[ i ][ j ][ k ] = 0;
102            }
103        }
104    }
105
106    mParent = parent;
107    mNumNodes = 0;
108}
109
110Octree::~Octree()
111{
112    //initialize all children to null.
113    for ( int i = 0; i < 2; i++ )
114    {
115        for ( int j = 0; j < 2; j++ )
116        {
117            for ( int k = 0; k < 2; k++ )
118            {
119                if ( mChildren[ i ][ j ][ k ] != 0 )
120                    delete mChildren[ i ][ j ][ k ];
121            }
122        }
123    }
124
125    if(mWireBoundingBox)
126        delete mWireBoundingBox;
127
128    mParent = 0;
129}
130
131void Octree::_addNode( OctreeNode * n )
132{
133    mNodes.push_back( n );
134    n -> setOctant( this );
135
136    //update total counts.
137    _ref();
138
139}
140
141void Octree::_removeNode( OctreeNode * n )
142{
143    mNodes.erase( std::find( mNodes.begin(), mNodes.end(), n ) );
144    n -> setOctant( 0 );
145
146    //update total counts.
147    _unref();
148}
149
150void Octree::_getCullBounds( AxisAlignedBox *b ) const
151{
152    b -> setExtents( mBox.getMinimum() - mHalfSize, mBox.getMaximum() + mHalfSize );
153}
154
155WireBoundingBox* Octree::getWireBoundingBox()
156{
157    // Create a WireBoundingBox if needed
158    if(mWireBoundingBox == 0)
159        mWireBoundingBox = new WireBoundingBox();
160
161    mWireBoundingBox->setupBoundingBox(mBox);
162    return mWireBoundingBox;
163}
164
165}
Note: See TracBrowser for help on using the repository browser.