/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SPATIAL_SEPARATION #include "quadtree_node.h" #include "quadtree.h" #include "material.h" #include "abstract_model.h" #include "list.h" #include "vector.h" using namespace std; /** * standard constructor */ QuadtreeNode::QuadtreeNode (sTriangleExt** triangles, int numTriangles, const float* pVertices, int numVertices, Quadtree* quadtree, QuadtreeNode* parent, Rectangle* rect, int treeDepth, const int maxDepth, int index ) { this->pTriangles = triangles; this->numTriangles = numTriangles; this->pVertices = pVertices; this->numVertices = numVertices; this->quadtree = quadtree; this->parent = parent; this->pDimension = rect; this->treeDepth = treeDepth; this->maxDepth = maxDepth; this->indexNode = index; /* debug output */ for( int i = 0; i < this->treeDepth; ++i) PRINT(3)(" |"); PRINT(3)(" | +-| (Event) Building Node Nr. %i Depth: %i/%i, pointer: %p\n", this->indexNode, treeDepth, maxDepth, this); for( int i = 0; i < this->treeDepth; ++i) PRINT(3)(" |"); PRINT(3)(" | +-| (II) Rectangle Center (%5.2f, %5.2f), half axis length: %5.2f\n", this->pDimension->getCenter()->x, this->pDimension->getCenter()->z, this->pDimension->getAxis()); this->init(); } /** * standard constructor */ QuadtreeNode::QuadtreeNode(modelInfo* pModelInfo, Quadtree* quadtree, const int maxDepth) { this->pModelInfo = pModelInfo; this->quadtree = quadtree; /* create an array of triangle references */ this->pTriangles = new sTriangleExt*[this->pModelInfo->numTriangles]; this->numTriangles = this->pModelInfo->numTriangles; this->pVertices = this->pModelInfo->pVertices; this->numVertices = this->pModelInfo->numVertices; for( int i = 0; i < this->pModelInfo->numTriangles; ++i) this->pTriangles[i] = &this->pModelInfo->pTriangles[i]; this->treeDepth = 0; this->maxDepth = maxDepth; this->indexNode = 0; /* debug output */ for( int i = 0; i < this->treeDepth; ++i) PRINT(3)(" |"); PRINT(3)(" | +-| (Event) Building Node Nr. %i Depth: %i/%i, pointer: %p\n", this->indexNode, treeDepth, maxDepth, this); this->pDimension = this->getDimFromModel(); this->init(); } /** * takes the rest of the initialisation process */ void QuadtreeNode::init() { this->setClassID(CL_QUADTREE_NODE, "QuadtreeNode"); this->offset = 0.0f; this->nodeIter = -1; this->parent = NULL; this->nodeA = NULL; this->nodeB = NULL; this->nodeC = NULL; this->nodeD = NULL; this->nodes = new QuadtreeNode*[4]; for(int i = 0; i < 4; ++i) this->nodes[i] = NULL; if( this->treeDepth < this->maxDepth) this->separateNode(); } /** * standard deconstructor */ QuadtreeNode::~QuadtreeNode () { if( this->nodeA != NULL) delete this->nodeA; if( this->nodeB != NULL) delete this->nodeB; if( this->nodeC != NULL) delete this->nodeC; if( this->nodeD != NULL) delete this->nodeD; } void QuadtreeNode::buildHashTable(QuadtreeNode** nodeList, int* index) { if( this->nodeIter == -1) this->nodeIter = *index; /* offset #of elements in a row #of rows in a quadtree */ int threshold = this->nodeIter + (int)pow(2, this->maxDepth) * (int)pow(2, maxDepth - treeDepth - 1); int loopLimit = (*index < threshold)?2:4; /* is it a leaf? */ if( this->treeDepth < this->maxDepth) for(int i = (*index < threshold)?0:2; i < loopLimit; ++i) this->nodes[i]->buildHashTable(nodeList, index); else nodeList[(*index)++] = this; } /** * gives the signal to separate the model into a quadtree * @param treeDepth the max depth, the steps to go if treeDept == 0 leaf reached */ void QuadtreeNode::separateNode(float minLength) { /* dimension calculation & limit checking */ if( minLength <= this->pDimension->getAxis()) return; /* node separation */ // this->separateNode(); // this->nodeA->separateNode(minLength); // this->nodeB->separateNode(minLength); // this->nodeC->separateNode(minLength); // this->nodeD->separateNode(minLength); } /** * gives the signal to separate the model into a quadtree * @param treeDepth the max depth, the steps to go if treeDept == 0 leaf reached */ void QuadtreeNode::separateNode() { tList* listA = new tList(); //!< triangle list of nodeA tList* listB = new tList(); //!< triangle list of nodeB tList* listC = new tList(); //!< triangle list of nodeC tList* listD = new tList(); //!< triangle list of nodeD const float* pVert; //!< pointer to the vertices const Vector* rectCenter; //!< vector to the center of the rect rectCenter = this->pDimension->getCenter(); for( int i = 0; i < this->numTriangles; ++i) { for( int j = 0; j < 3; ++j) { pVert = &this->pVertices[this->pTriangles[i]->indexToVertices[j]]; if( pVert[0] > rectCenter->x + this->offset && pVert[2] > rectCenter->z + this->offset) listA->add(&this->pTriangles[i]); if( pVert[0] < rectCenter->x + this->offset && pVert[2] > rectCenter->z + this->offset) listB->add(&this->pTriangles[i]); if( pVert[0] < rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset) listC->add(&this->pTriangles[i]); if( pVert[0] > rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset) listD->add(&this->pTriangles[i]); } } for( int i = 0; i < treeDepth; ++i) PRINT(3)(" |"); PRINT(3)(" | +-| (II) Quadtree Counts - separating: A: %i, B: %i, C: %i, D: %i\n", listA->getSize(), listB->getSize(), listC->getSize(), listD->getSize()); /* Separating into to the triangle lists */ sTriangleExt** pTriA; //!< Triangle array A sTriangleExt** pTriB; //!< Triangle array B sTriangleExt** pTriC; //!< Triangle array C sTriangleExt** pTriD; //!< Triangle array D int lenA; //!< length array A int lenB; //!< length array B int lenC; //!< length array C int lenD; //!< length array D tIterator* iterator; //!< iterator for the list iterations sTriangleExt** tempTri; //!< temp save place for triangle pointer int counter; //!< counter for the while loops lenA = listA->getSize(); lenB = listB->getSize(); lenC = listC->getSize(); lenD = listD->getSize(); pTriA = new sTriangleExt*[listA->getSize()]; pTriB = new sTriangleExt*[listB->getSize()]; pTriC = new sTriangleExt*[listC->getSize()]; pTriD = new sTriangleExt*[listD->getSize()]; counter = 0; iterator = listA->getIterator(); tempTri = iterator->nextElement(); while( tempTri) { pTriA[counter] = *tempTri; tempTri = iterator->nextElement(); ++counter; } counter = 0; iterator = listB->getIterator(); tempTri = iterator->nextElement(); while( tempTri) { pTriB[counter] = *tempTri; tempTri = iterator->nextElement(); ++counter; } counter = 0; iterator = listC->getIterator(); tempTri = iterator->nextElement(); while( tempTri) { pTriC[counter] = *tempTri; tempTri = iterator->nextElement(); ++counter; } counter = 0; iterator = listD->getIterator(); tempTri = iterator->nextElement(); while( tempTri) { pTriD[counter] = *tempTri; tempTri = iterator->nextElement(); ++counter; } /* now do cleanup */ delete listA; delete listB; delete listC; delete listD; delete iterator; /* now create the rectangle dimensions */ Vector v; v.x = this->pDimension->getCenter()->x + this->pDimension->getAxis() / 2.0f; v.y = 0.0; v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f; Rectangle* rA = new Rectangle(v, this->pDimension->getAxis() / 2.0f); v.z = this->pDimension->getCenter()->z - this->pDimension->getAxis() / 2.0f; Rectangle* rB = new Rectangle(v, this->pDimension->getAxis() / 2.0f); v.x = this->pDimension->getCenter()->x - this->pDimension->getAxis() / 2.0f; Rectangle* rC = new Rectangle(v, this->pDimension->getAxis() / 2.0f); v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f; Rectangle* rD = new Rectangle(v, this->pDimension->getAxis() / 2.0f); /* now propagate */ this->nodeA = new QuadtreeNode(pTriA, lenA, this->pVertices, this->numVertices, this->quadtree, this, rA, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 0); this->nodeB = new QuadtreeNode(pTriB, lenB, this->pVertices, this->numVertices, this->quadtree, this, rB, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 1); this->nodeC = new QuadtreeNode(pTriC, lenC, this->pVertices, this->numVertices, this->quadtree, this, rC, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 2); this->nodeD = new QuadtreeNode(pTriD, lenD, this->pVertices, this->numVertices, this->quadtree, this, rD, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 3); /* map the array references, this is for faster and automatical interfacing \todo: use only array */ this->nodes[0] = this->nodeA; this->nodes[1] = this->nodeB; this->nodes[2] = this->nodeC; this->nodes[3] = this->nodeD; } /** \brief gets the maximal dimension of a model \return the dimension of the AbstractModel as a Rectangle The rectangle is x-z axis aligned. ATTENTION: if there are any vertices in the model, that exceed the size of 999999.0, there probably will be some errors in the dimensions calculations. Write an email to patrick@orxonox.ethz.ch if you will ever encounter a model bigger than 999999.0 units, I will realy be happy to see orxonox used to extensivly :) */ Rectangle* QuadtreeNode::getDimFromModel() { float maxX, maxY; //!< the maximal coordinates axis float minX, minY; //!< minimal axis coorindates const float* pVertices; //!< pointer to the current vertices maxX = -999999; maxY = -999999; minX = 999999; minY = 999999; /* get maximal/minimal x/y */ for( int i = 0; i < this->numTriangles; ++i) { for( int j = 0; j < 3; ++j) { pVertices = &this->pVertices[this->pTriangles[i]->indexToVertices[j]]; if( pVertices[0] > maxX) maxX = pVertices[0]; if( pVertices[2] > maxY) maxY = pVertices[2]; if( pVertices[0] < minX) minX = pVertices[0]; if( pVertices[2] < minY) minY = pVertices[2]; } } Rectangle* rect = new Rectangle(); rect->setCenter((maxX + minX) / 2.0f, 0.0f, (maxY + minY) / 2.0f); /* this is little strange, since y is in opengl the up vector */ rect->setAxis(fmax(((fabs(maxX) + fabs(minX)) / 2.0f), ((fabs(maxY) + fabs(minY)) / 2.0f))); for( int i = 0; i < this->treeDepth; ++i) PRINT(3)(" |"); PRINT(3)(" | +-| (II) Rectangle Dimension (%5.2f, %5.2f) to (%5.2f, %5.2f), Center (%5.2f, %5.2f)\n", minX, minY, maxX, maxY, rect->getCenter()->x, rect->getCenter()->z, rect->getAxis()); return rect; } /** * draws the debug quadtree boxes around the model */ void QuadtreeNode::drawTree(int depth, int drawMode) const { printf("this = %p\n", this); if( this->treeDepth == this->maxDepth) { Vector t1 = *this->pDimension->getCenter(); float ax = this->pDimension->getAxis(); float h = 50.0f; glBegin(GL_QUADS); this->quadtree->getMaterial(this->indexNode)->select(); glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z + ax); glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z + ax); glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z - ax); glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z - ax); glEnd(); } if( this->nodeA != NULL) this->nodeA->drawTree(depth - 1, drawMode); if( this->nodeB != NULL) this->nodeB->drawTree(depth - 1, drawMode); if( this->nodeC != NULL) this->nodeC->drawTree(depth - 1, drawMode); if( this->nodeD != NULL) this->nodeD->drawTree(depth - 1, drawMode); }