#include namespace IMG { NodeBsp::NodeBsp(): references(0) // initialize references to 0 { id = -1; fit = false; } NodeBsp::~NodeBsp() { } void NodeBsp::setBound(Box2d * box) { bound = (*box); } NodeBspPtr NodeBsp::getChild(int i) { return child[i]; } void NodeBsp::setId(int id_) { id = id_; } unsigned int NodeBsp::getId() const { return id; } void NodeBsp::setChild(NodeBspPtr node, int i) { child[i] = node; } NodeBspPtr NodeBsp::get(NodeBspPtr node, int i) { if (!node) { return NULL; } if (node->getId() == i ) { return node; } NodeBspPtr nod; if (nod = node->getChild(0)->get(node->getChild(0), i)) { return nod; } return node->getChild(0)->get(node->getChild(1), i); } Box2d * NodeBsp::getBound() { return &bound; } void NodeBsp::print() { Ogre::LogManager::getSingleton().logMessage("Node : " + Ogre::StringConverter::toString(id) + ", fit: " + Ogre::StringConverter::toString(fit)); bound.print(); } NodeBspPtr NodeBsp::insert(int w, int h) { //Ogre::LogManager::getSingleton().logMessage("\nInsert del node: :" + Ogre::StringConverter::toString(id) + " (" + Ogre::StringConverter::toString(w) + ", " + Ogre::StringConverter::toString(h)); if (this->fit == true) { return NULL; } if (this->child[0] != NULL && this->child[1] != NULL) { NodeBspPtr new_node = child[0]->insert(w, h); if (new_node) return new_node; return child[1]->insert(w,h); } if (!bound.in(w,h)) { return NULL; } if (bound.fitPerfect(w,h)) { fit = true; return this; } child[0] = NodeBspPtr(new NodeBsp); child[1] = NodeBspPtr(new NodeBsp); int dw, dh; Ogre::Vector2 min, max; float width, height; bound.print(); min = bound.getMinimum(); max = bound.getMaximum(); width = max.x - min.x; height = max.y - min.y; dw = (int) width - w; dh = (int) height - h; if (dw > dh) { child[0]->bound.setBoundBox(min.x, min.y, min.x + w, max.y); child[1]->bound.setBoundBox(min.x + w, min.y, max.x, max.y); } else { child[0]->bound.setBoundBox(min.x, max.y - h, max.x, max.y); child[1]->bound.setBoundBox(min.x, min.y, max.x, max.y - h); } return child[0]->insert(w, h); } unsigned int NodeBsp::ID = 0; }