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 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | |
---|
26 | /** |
---|
27 | * standard constructor |
---|
28 | @todo this constructor is not jet implemented - do it |
---|
29 | */ |
---|
30 | Quadtree::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->revertHashTable(this->nodes); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | /** |
---|
64 | * standard deconstructor |
---|
65 | |
---|
66 | */ |
---|
67 | Quadtree::~Quadtree () |
---|
68 | { |
---|
69 | // delete what has to be deleted here |
---|
70 | delete [] this->nodes; |
---|
71 | delete this->rootNode; |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | |
---|
77 | since the original matrix is counted from the right upper edge to the right lower one, we have to reorganize the elements |
---|
78 | to be able to easily correlate the hashtable array indexes with the coorindates. |
---|
79 | */ |
---|
80 | void Quadtree::revertHashTable(QuadtreeNode** nodes) |
---|
81 | { |
---|
82 | int len = (int)pow(2, this->treeDepth); //!< the length of a quadtree side |
---|
83 | int iterator = 0; //!< iterator used for mapping |
---|
84 | QuadtreeNode* tmpNode = NULL; //!< temp saving place |
---|
85 | int offset = 0; //!< offset used in the calc |
---|
86 | |
---|
87 | for(int i = len - 1; i >= 0; --i) |
---|
88 | { |
---|
89 | for(int j = len - 1; j >= 0; --j) |
---|
90 | { |
---|
91 | offset = j * len + i; |
---|
92 | /* only swap the elements in one direction */ |
---|
93 | if( offset > iterator) |
---|
94 | { |
---|
95 | tmpNode = nodes[offset]; |
---|
96 | nodes[offset] = nodes[iterator]; |
---|
97 | nodes[iterator] = tmpNode; |
---|
98 | } |
---|
99 | ++iterator; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | QuadtreeNode* Quadtree::getQuadtreeFromPosition(const Vector& position) |
---|
106 | { |
---|
107 | Vector v = position; |
---|
108 | Rectangle* r = this->rootNode->getDimension(); |
---|
109 | float len = this->nodes[0]->getDimension()->getAxis() * 2.0f; |
---|
110 | float xOff = r->getCenter()->x - r->getAxis(); |
---|
111 | float yOff = r->getCenter()->z - r->getAxis(); |
---|
112 | |
---|
113 | int i = (int)(( position.x - xOff) / len) + 1; |
---|
114 | int j = (int)(( position.z - yOff) / len) + 1; |
---|
115 | |
---|
116 | printf("the array coordinates are: %i, %i\n", i, j); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | /** |
---|
121 | * draws the debug quadtree boxes around the model |
---|
122 | */ |
---|
123 | void Quadtree::drawTree(int depth, int drawMode) const |
---|
124 | { |
---|
125 | //this->rootNode->drawTree(depth, drawMode); |
---|
126 | for(int i = 0; i < (int)pow(4, this->treeDepth); ++i) |
---|
127 | { |
---|
128 | this->nodes[i]->drawTree(0, 0); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | } |
---|