glc_octree.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023  *****************************************************************************/
00025 
00026 #include "glc_octree.h"
00027 #include "glc_octreenode.h"
00028 #include "glc_3dviewcollection.h"
00029 #include "../glc_factory.h"
00030 
00031 int GLC_Octree::m_DefaultOctreeDepth= 3;
00032 
00033 GLC_Octree::GLC_Octree(GLC_3DViewCollection* pCollection)
00034 : GLC_SpacePartitioning(pCollection)
00035 , m_pRootNode(NULL)
00036 , m_OctreeDepth(m_DefaultOctreeDepth)
00037 {
00038 
00039 
00040 }
00041 
00042 GLC_Octree::GLC_Octree(const GLC_Octree& octree)
00043 : GLC_SpacePartitioning(octree)
00044 , m_pRootNode(NULL)
00045 , m_OctreeDepth(octree.m_OctreeDepth)
00046 {
00047         if (NULL != octree.m_pRootNode)
00048         {
00049                 m_pRootNode= new GLC_OctreeNode(*(octree.m_pRootNode));
00050         }
00051 }
00052 
00053 GLC_Octree::~GLC_Octree()
00054 {
00055         delete m_pRootNode;
00056 }
00057 
00058 int  GLC_Octree::defaultDepth()
00059 {
00060         return m_DefaultOctreeDepth;
00061 }
00062 
00063 QList<GLC_3DViewInstance*> GLC_Octree::listOfIntersectedInstances(const GLC_BoundingBox& bBox)
00064 {
00065         if (NULL == m_pRootNode)
00066         {
00067                 updateSpacePartitioning();
00068         }
00069         return m_pRootNode->setOfIntersectedInstances(bBox).toList();
00070 }
00071 
00072 void GLC_Octree::updateViewableInstances(const GLC_Frustum& frustum)
00073 {
00074         if (NULL == m_pRootNode)
00075         {
00076                 updateSpacePartitioning();
00077         }
00078         m_pRootNode->updateViewableInstances(frustum);
00079 }
00080 
00081 void GLC_Octree::updateSpacePartitioning()
00082 {
00083         qDebug() << "Update space partitioning";
00084         delete m_pRootNode;
00085         m_pRootNode= new GLC_OctreeNode(m_pCollection->boundingBox());
00086         // fill the octree
00087         QList<GLC_3DViewInstance*> instanceList(m_pCollection->instancesHandle());
00088         const int size= instanceList.size();
00089         for (int i= 0; i < size; ++i)
00090         {
00091                 m_pRootNode->addInstance(instanceList.at(i), m_OctreeDepth);
00092         }
00093         m_pRootNode->removeEmptyChildren();
00094         qDebug() << "Update space partitioning DONE";
00095 }
00096 
00097 void GLC_Octree::setDepth(int depth)
00098 {
00099         m_OctreeDepth= depth;
00100         if (NULL != m_pRootNode)
00101         {
00102                 updateSpacePartitioning();
00103         }
00104 }
00105 
00106 void GLC_Octree::createBox(GLC_Material* pMat, GLC_3DViewCollection* pCol)
00107 {
00108         if (NULL == m_pRootNode)
00109         {
00110                 updateSpacePartitioning();
00111         }
00112 
00113         if (NULL == pCol) pCol= m_pCollection;
00114         if (!m_pRootNode->isEmpty())
00115         {
00116                 createBoxWithMaterial(pCol, m_pRootNode, pMat);
00117         }
00118 }
00119 
00120 void GLC_Octree::setDefaultDepth(int depth)
00121 {
00122         m_DefaultOctreeDepth= depth;
00123 }
00124 
00125 void GLC_Octree::createBoxWithMaterial(GLC_3DViewCollection* pCol, GLC_OctreeNode* pNode, GLC_Material* pMat)
00126 {
00127         if (!pNode->isEmpty())
00128         {
00129                 if (pNode->hasGeometry())
00130                 {
00131                         GLC_3DViewInstance box= GLC_Factory::instance()->createBox(pNode->boundingBox());
00132                         box.geomAt(0)->replaceMasterMaterial(pMat);
00133                         pCol->add(box);
00134                 }
00135 
00136                 if (pNode->hasChild())
00137                 {
00138                         const int size= pNode->childCount();
00139                         for (int i= 0; i < size; ++i)
00140                         {
00141                                 createBoxWithMaterial(pCol, pNode->childAt(i), pMat);
00142                         }
00143                 }
00144 
00145         }
00146 
00147 }
00148 

SourceForge.net Logo

©2005 Laurent Ribon