Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc @ 4922

Last change on this file since 4922 was 4922, checked in by patrick, 20 years ago

orxonox/trunk: started cleaning up the code a little and making the comments

File size: 5.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SPATIAL_SEPARATION
17
18#include "quadtree.h"
19#include "quadtree_node.h"
20#include "material.h"
21#include "vector.h"
22
23using namespace std;
24
25
26/**
27 *  standard constructor
28   @todo this constructor is not jet implemented - do it
29*/
30Quadtree::Quadtree (modelInfo* pModelInfo, const int treeDepth)
31{
32   this->setClassID(CL_QUADTREE, "Quadtree");
33   this->pModelInfo = pModelInfo;
34   this->treeDepth = treeDepth;
35
36   /* initialize the materials for debug draw */
37   this->materials = new Material*[4];
38   for(int i = 0; i < 4; ++i)
39   {
40     materials[i] = new Material();
41     materials[i]->setIllum(3);
42   }
43   materials[0]->setAmbient(0.0, 0.3, 0.0);
44   materials[1]->setAmbient(0.4, 0.0, 0.2);
45   materials[2]->setAmbient(1.0, 0.0, 0.0);
46   materials[3]->setAmbient(5.0, 3.0, 1.0);
47
48   /* build the tree */
49   this->rootNode = new QuadtreeNode(this->pModelInfo, this, this->treeDepth);
50
51   /* make an array with access to the leafs of the Quad-Tree */
52   this->nodes = new QuadtreeNode*[(int)pow(4, treeDepth)];
53   int* index = new int; *index = 0;
54   for(int i = 0; i < (int)pow(2, treeDepth); ++i)
55   {
56     printf("============================\n");
57     this->rootNode->buildHashTable(this->nodes, index);
58   }
59   this->sortHashTable(this->nodes);
60   this->revertHashTable(this->nodes);
61
62   for(int i = 0; i < (int)pow(4, treeDepth); ++i)
63   {
64     printf("node %i, %f, %f \n", i, this->nodes[i]->getDimension()->getCenter()->x, this->nodes[i]->getDimension()->getCenter()->z);
65   }
66
67   this->quadLength = this->nodes[0]->getDimension()->getAxis() * 2.0f;
68   Rectangle* r = this->rootNode->getDimension();
69
70   Vector* offset = new Vector();
71   float xOff = r->getCenter()->x - r->getAxis();
72   float yOff = r->getCenter()->z - r->getAxis();
73   this->offset->x = xOff;
74   this->offset->z = yOff;
75
76   this->maxIndex = (int)pow(2, this->treeDepth);
77}
78
79
80/**
81 *  standard deconstructor
82
83*/
84Quadtree::~Quadtree ()
85{
86  // delete what has to be deleted here
87  delete [] this->nodes;
88  delete this->rootNode;
89  delete offset;
90}
91
92
93/**
94  \brief this function rotates the array and mirrors it in the middle
95  \param nodes: the nodes to translate
96
97  since the original matrix is counted from the right upper edge to the right lower one, we have to reorganize the elements
98  to be able to easily correlate the hashtable array indexes with the coorindates.
99 */
100void Quadtree::revertHashTable(QuadtreeNode** nodes)
101{
102  int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
103  int                  iterator    = 0;                                     //!< iterator used for mapping
104  QuadtreeNode*        tmpNode     = NULL;                                  //!< temp saving place
105  int                  offset      = 0;                                     //!< offset used in the calc
106
107  for(int i = len - 1; i >= 0; --i)
108  {
109    for(int j = len - 1; j >= 0; --j)
110    {
111      offset = j * len + i;
112      /* only swap the elements in one direction */
113      if( offset > iterator)
114      {
115        tmpNode = nodes[offset];
116        nodes[offset] = nodes[iterator];
117        nodes[iterator] = tmpNode;
118      }
119      ++iterator;
120    }
121  }
122}
123
124/**
125  \brief sorts the hash table using their positions
126  \param nodes the nodes to use
127
128 */
129void Quadtree::sortHashTable(QuadtreeNode** nodes)
130{
131  int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
132  float                a;                                                   //!< temp place for float a
133  float                b;                                                   //!< temp place for float b
134  QuadtreeNode*        tmpNode;                                             //!< tmp place for a QuadtreeNode
135
136  for( int i = 0; i < len; ++i)
137  {
138    for( int j = 0; j < len; ++j)
139    {
140      for( int k = j + 1; k < len; ++k)
141      {
142        a = this->nodes[i * len + j]->getDimension()->getCenter()->z;
143        b = this->nodes[i * len + k]->getDimension()->getCenter()->z;
144
145        if( b > a)
146        {
147          tmpNode = this->nodes[i * len + j];
148          this->nodes[i * len + j] = this->nodes[i * len + k];
149          this->nodes[i * len + k] = tmpNode;
150        }
151      }
152    }
153  }
154
155}
156
157
158/**
159  \brief maps a position to a quadtree
160  \param position the position so look for
161  \return the quadtree
162
163  this function will return the quadtree that contains the position
164 */
165QuadtreeNode* Quadtree::getQuadtreeFromPosition(const Vector& position)
166{
167  /* shift into model space */
168  Vector v = position - *this->offset;
169  /* map */
170  int i = (int)(v.x / quadLength);
171  int j = (int)(v.z / quadLength);
172
173  /* check if object is still inside the terrain */
174  if( i < this->maxIndex && j < this->maxIndex)
175    this->nodes[i + j * this->maxIndex]->includesPoint(position);
176  else
177    PRINTF(0)("Object has left terrain\n");
178}
179
180
181/**
182 *  draws the debug quadtree boxes around the model
183 */
184void Quadtree::drawTree() const
185{
186  this->rootNode->drawTree();
187//   for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
188//   {
189//     this->nodes[i]->draw();
190//   }
191}
Note: See TracBrowser for help on using the repository browser.