Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4921 was 4921, checked in by patrick, 19 years ago

orxonox/trunk: the way, where the fighter flights though is now drawn in quadtree partitions. now you can see where the space craft has been :)

File size: 4.9 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
68
69/**
70 *  standard deconstructor
71
72*/
73Quadtree::~Quadtree ()
74{
75  // delete what has to be deleted here
76  delete [] this->nodes;
77  delete this->rootNode;
78}
79
80
81/**
82
83  since the original matrix is counted from the right upper edge to the right lower one, we have to reorganize the elements
84  to be able to easily correlate the hashtable array indexes with the coorindates.
85 */
86void Quadtree::revertHashTable(QuadtreeNode** nodes)
87{
88  int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
89  int                  iterator    = 0;                                     //!< iterator used for mapping
90  QuadtreeNode*        tmpNode     = NULL;                                  //!< temp saving place
91  int                  offset      = 0;                                     //!< offset used in the calc
92
93  for(int i = len - 1; i >= 0; --i)
94  {
95    for(int j = len - 1; j >= 0; --j)
96    {
97      offset = j * len + i;
98      /* only swap the elements in one direction */
99      if( offset > iterator)
100      {
101        tmpNode = nodes[offset];
102        nodes[offset] = nodes[iterator];
103        nodes[iterator] = tmpNode;
104      }
105      ++iterator;
106    }
107  }
108}
109
110
111void Quadtree::sortHashTable(QuadtreeNode** nodes)
112{
113  int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
114  float a,b;
115  QuadtreeNode* tmpNode;
116
117  for(int i = 0; i < len; ++i)
118  {
119    for(int j = 0; j < len; ++j)
120    {
121      for(int k = j + 1; k < len; ++k)
122      {
123        a = this->nodes[i * len + j]->getDimension()->getCenter()->z;
124        b = this->nodes[i * len + k]->getDimension()->getCenter()->z;
125
126        if( b > a)
127        {
128          tmpNode = this->nodes[i * len + j];
129          this->nodes[i * len + j] = this->nodes[i * len + k];
130          this->nodes[i * len + k] = tmpNode;
131        }
132      }
133
134    }
135
136  }
137}
138
139
140
141QuadtreeNode* Quadtree::getQuadtreeFromPosition(const Vector& position)
142{
143  Vector v = position;
144  Rectangle* r = this->rootNode->getDimension();
145  float len = this->nodes[0]->getDimension()->getAxis() * 2.0f;
146
147  Vector edge;
148  float xOff = r->getCenter()->x - r->getAxis();
149  float yOff = r->getCenter()->z - r->getAxis();
150  edge.x = xOff, edge.z = yOff;
151
152  /* shift into model space */
153  v = position - edge;
154  /* map */
155  int i = (int)(v.x / len);
156  int j = (int)(v.z / len);
157
158  int max = (int)pow(2, this->treeDepth);
159
160  if( i < max && j < max)
161  {
162    printf("-----------\nthe array coordinates are: %i, %i\n", i, j);
163    printf("position: %f,%f, center %f, %f\n", position.x, position.z, this->nodes[i + j * max]->getDimension()->getCenter()->x, this->nodes[i + j * max]->getDimension()->getCenter()->z);
164
165    //this->nodes[i + j * max]->draw();
166    this->nodes[i + j * max]->includesPoint(position);
167  }
168  else
169    printf("object has left terrain\n");
170}
171
172
173/**
174 *  draws the debug quadtree boxes around the model
175 */
176void Quadtree::drawTree(int depth, int drawMode) const
177{
178  //this->rootNode->drawTree(depth, drawMode);
179  for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
180  {
181    this->nodes[i]->draw();
182  }
183
184
185}
Note: See TracBrowser for help on using the repository browser.