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