glc_octree.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00023
00024 #include "glc_octree.h"
00025 #include "glc_octreenode.h"
00026 #include "glc_3dviewcollection.h"
00027 #include "../glc_factory.h"
00028
00029 int GLC_Octree::m_DefaultOctreeDepth= 3;
00030
00031 GLC_Octree::GLC_Octree(GLC_3DViewCollection* pCollection)
00032 : GLC_SpacePartitioning(pCollection)
00033 , m_pRootNode(NULL)
00034 , m_OctreeDepth(m_DefaultOctreeDepth)
00035 {
00036
00037
00038 }
00039
00040 GLC_Octree::GLC_Octree(const GLC_Octree& octree)
00041 : GLC_SpacePartitioning(octree)
00042 , m_pRootNode(NULL)
00043 , m_OctreeDepth(octree.m_OctreeDepth)
00044 {
00045 if (NULL != octree.m_pRootNode)
00046 {
00047 m_pRootNode= new GLC_OctreeNode(*(octree.m_pRootNode));
00048 }
00049 }
00050
00051 GLC_Octree::~GLC_Octree()
00052 {
00053 delete m_pRootNode;
00054 }
00055
00056 int GLC_Octree::defaultDepth()
00057 {
00058 return m_DefaultOctreeDepth;
00059 }
00060
00061 QList<GLC_3DViewInstance*> GLC_Octree::listOfIntersectedInstances(const GLC_BoundingBox& bBox)
00062 {
00063 if (NULL == m_pRootNode)
00064 {
00065 updateSpacePartitioning();
00066 }
00067 return m_pRootNode->setOfIntersectedInstances(bBox).toList();
00068 }
00069
00070 void GLC_Octree::updateViewableInstances(const GLC_Frustum& frustum)
00071 {
00072 if (NULL == m_pRootNode)
00073 {
00074 updateSpacePartitioning();
00075 }
00076 m_pRootNode->updateViewableInstances(frustum);
00077 }
00078
00079 void GLC_Octree::updateSpacePartitioning()
00080 {
00081 delete m_pRootNode;
00082 m_pRootNode= new GLC_OctreeNode(m_pCollection->boundingBox(true));
00083
00084 QList<GLC_3DViewInstance*> instanceList(m_pCollection->instancesHandle());
00085 const int size= instanceList.size();
00086 for (int i= 0; i < size; ++i)
00087 {
00088 m_pRootNode->addInstance(instanceList.at(i), m_OctreeDepth);
00089 }
00090 m_pRootNode->removeEmptyChildren();
00091 }
00092
00093 void GLC_Octree::clear()
00094 {
00095 delete m_pRootNode;
00096 m_pRootNode= NULL;
00097 }
00098
00099 void GLC_Octree::setDepth(int depth)
00100 {
00101 m_OctreeDepth= depth;
00102 if (NULL != m_pRootNode)
00103 {
00104 updateSpacePartitioning();
00105 }
00106 }
00107
00108 void GLC_Octree::createBox(GLC_Material* pMat, GLC_3DViewCollection* pCol)
00109 {
00110 if (NULL == m_pRootNode)
00111 {
00112 updateSpacePartitioning();
00113 }
00114
00115 if (NULL == pCol) pCol= m_pCollection;
00116 if (!m_pRootNode->isEmpty())
00117 {
00118 createBoxWithMaterial(pCol, m_pRootNode, pMat);
00119 }
00120 }
00121
00122 void GLC_Octree::setDefaultDepth(int depth)
00123 {
00124 m_DefaultOctreeDepth= depth;
00125 }
00126
00127 void GLC_Octree::createBoxWithMaterial(GLC_3DViewCollection* pCol, GLC_OctreeNode* pNode, GLC_Material* pMat)
00128 {
00129 if (!pNode->isEmpty())
00130 {
00131 if (pNode->hasGeometry())
00132 {
00133 GLC_3DViewInstance box= GLC_Factory::instance()->createBox(pNode->boundingBox());
00134 box.geomAt(0)->replaceMasterMaterial(pMat);
00135 pCol->add(box);
00136 }
00137
00138 if (pNode->hasChild())
00139 {
00140 const int size= pNode->childCount();
00141 for (int i= 0; i < size; ++i)
00142 {
00143 createBoxWithMaterial(pCol, pNode->childAt(i), pMat);
00144 }
00145 }
00146
00147 }
00148
00149 }
00150