Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more cleanup, and more cleanup to come

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