Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

oxonox/trunk: now swapping the coordinates

File size: 3.1 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
22using namespace std;
23
24
25/**
26 *  standard constructor
27   @todo this constructor is not jet implemented - do it
28*/
29Quadtree::Quadtree (modelInfo* pModelInfo, const int treeDepth)
30{
31   this->setClassID(CL_QUADTREE, "Quadtree");
32   this->pModelInfo = pModelInfo;
33   this->treeDepth = treeDepth;
34
35   /* initialize the materials for debug draw */
36   this->materials = new Material*[4];
37   for(int i = 0; i < 4; ++i)
38   {
39     materials[i] = new Material();
40     materials[i]->setIllum(3);
41   }
42   materials[0]->setAmbient(0.0, 0.3, 0.0);
43   materials[1]->setAmbient(0.4, 0.0, 0.2);
44   materials[2]->setAmbient(1.0, 0.0, 0.0);
45   materials[3]->setAmbient(5.0, 3.0, 1.0);
46
47   /* build the tree */
48   this->rootNode = new QuadtreeNode(this->pModelInfo, this, this->treeDepth);
49
50   /* make an array with access to the leafs of the Quad-Tree */
51   this->nodes = new QuadtreeNode*[(int)pow(4, treeDepth)];
52   int* index = new int; *index = 0;
53   for(int i = 0; i < (int)pow(2, treeDepth); ++i)
54   {
55     printf("============================\n");
56     this->rootNode->buildHashTable(this->nodes, index);
57   }
58   this->revertHashTable(this->nodes);
59}
60
61
62/**
63 *  standard deconstructor
64
65*/
66Quadtree::~Quadtree ()
67{
68  // delete what has to be deleted here
69  delete [] this->nodes;
70  delete this->rootNode;
71}
72
73
74/**
75
76  since the original matrix is counted from the right upper edge to the right lower one, we have to reorganize the elements
77  to be able to easily correlate the hashtable array indexes with the coorindates.
78 */
79void Quadtree::revertHashTable(QuadtreeNode** nodes)
80{
81  int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
82  int                  iterator    = 0;                                     //!< iterator used for mapping
83  QuadtreeNode*        tmpNode     = NULL;                                  //!< temp saving place
84  int                  offset      = 0;                                     //!< offset used in the calc
85
86  for(int i = len - 1; i >= 0; --i)
87  {
88    for(int j = len - 1; j >= 0; --j)
89    {
90      offset = j * len + i;
91      /* only swap the elements in one direction */
92      if( offset > iterator)
93      {
94        tmpNode = nodes[offset];
95        nodes[offset] = nodes[iterator];
96        nodes[iterator] = tmpNode;
97      }
98      ++iterator;
99    }
100  }
101}
102
103
104/**
105 *  draws the debug quadtree boxes around the model
106 */
107void Quadtree::drawTree(int depth, int drawMode) const
108{
109  //this->rootNode->drawTree(depth, drawMode);
110  for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
111  {
112    this->nodes[i]->drawTree(0, 0);
113  }
114
115
116}
Note: See TracBrowser for help on using the repository browser.