source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/IMGNodeBsp.cpp @ 731

Revision 731, 2.2 KB checked in by igarcia, 18 years ago (diff)
Line 
1
2#include <IMGNodeBsp.h>
3
4namespace IMG
5{
6
7NodeBsp::NodeBsp(): references(0) // initialize references to 0
8{
9        id = -1;
10        fit = false;
11}
12
13NodeBsp::~NodeBsp()
14{
15}
16
17void NodeBsp::setBound(Box2d * box)
18{
19        bound = (*box);
20}
21
22NodeBspPtr NodeBsp::getChild(int i)
23{
24        return child[i];
25}
26
27void NodeBsp::setId(int id_)
28{
29        id = id_;
30}
31
32unsigned int NodeBsp::getId() const
33{
34        return id;
35}
36
37void NodeBsp::setChild(NodeBspPtr node, int i)
38{
39        child[i] = node;
40}
41
42NodeBspPtr NodeBsp::get(NodeBspPtr node, int i)
43{
44    if (!node)
45        {
46                return NULL;
47        }
48
49    if (node->getId() == i )
50        {
51                return node;
52        }
53   
54    NodeBspPtr nod;
55    if (nod = node->getChild(0)->get(node->getChild(0), i))
56        {
57                return nod;
58        }
59   
60    return node->getChild(0)->get(node->getChild(1), i);
61}
62
63
64Box2d * NodeBsp::getBound()
65{
66        return &bound;   
67}
68
69void NodeBsp::print()
70{
71        Ogre::LogManager::getSingleton().logMessage("Node : " + Ogre::StringConverter::toString(id) + ", fit: " + Ogre::StringConverter::toString(fit));
72
73        bound.print();
74}
75
76NodeBspPtr NodeBsp::insert(int w, int h)
77{
78        //Ogre::LogManager::getSingleton().logMessage("\nInsert del node: :" + Ogre::StringConverter::toString(id) + " (" + Ogre::StringConverter::toString(w) + ", " + Ogre::StringConverter::toString(h));
79       
80        if (this->fit == true)
81        {
82                return NULL;
83        }
84       
85        if (this->child[0] != NULL && this->child[1] != NULL)
86        {
87                NodeBspPtr new_node = child[0]->insert(w, h);
88               
89                if (new_node) return new_node;
90               
91                return child[1]->insert(w,h);
92        }
93       
94        if (!bound.in(w,h))
95        {
96                return NULL;
97        }
98       
99        if (bound.fitPerfect(w,h))
100        {
101                fit = true;  return this;
102        }
103       
104        child[0] = NodeBspPtr(new NodeBsp);
105        child[1] = NodeBspPtr(new NodeBsp);
106       
107        int dw, dh;
108        Ogre::Vector2 min, max;
109        float width, height;
110       
111        bound.print();
112        min = bound.getMinimum();
113        max = bound.getMaximum();
114       
115        width = max.x - min.x;
116        height = max.y - min.y;
117       
118        dw = (int) width - w;
119        dh = (int) height - h;
120       
121        if (dw > dh)
122        {       
123                child[0]->bound.setBoundBox(min.x, min.y, min.x + w, max.y);           
124                child[1]->bound.setBoundBox(min.x + w, min.y, max.x, max.y);                                                                           
125        }
126        else
127        {               
128                child[0]->bound.setBoundBox(min.x, max.y - h, max.x, max.y);
129                child[1]->bound.setBoundBox(min.x, min.y, max.x, max.y - h);
130        }
131       
132        return child[0]->insert(w, h);
133}
134
135unsigned int NodeBsp::ID = 0;
136
137}
Note: See TracBrowser for help on using the repository browser.