31 #include "util/base/exception.h"
32 #include "model/structures/instance.h"
33 #include "util/structures/rect.h"
35 #include "instancetree.h"
40 InstanceTree::InstanceTree(): FifeClass() {
43 InstanceTree::~InstanceTree() {
46 void InstanceTree::addInstance(Instance* instance) {
47 ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
48 InstanceTreeNode * node = m_tree.find_container(coords.x,coords.y,0,0);
49 InstanceList& list = node->data();
50 list.push_back(instance);
51 if( m_reverse.find(instance) != m_reverse.end() )
52 throw new InconsistencyDetected(
"Duplicate Instance.");
53 m_reverse[instance] = node;
56 void InstanceTree::removeInstance(Instance* instance) {
57 ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
58 InstanceTreeNode * node = m_reverse[instance];
60 throw new InconsistencyDetected(
"Removing Ghost Instance.");
61 m_reverse.erase(instance);
62 InstanceList& list = node->data();
63 for(InstanceList::iterator i = list.begin(); i != list.end(); ++i) {
64 if((*i) == instance) {
69 throw new InconsistencyDetected(
"Removing Ghost Instance (not in list?).");
72 class InstanceListCollector {
74 InstanceTree::InstanceList& instanceList;
76 InstanceListCollector(InstanceTree::InstanceList& a_instanceList,
const Rect& rect)
77 : instanceList(a_instanceList), searchRect(rect) {
79 bool visit(InstanceTree::InstanceTreeNode* node,
int d);
82 bool InstanceListCollector::visit(InstanceTree::InstanceTreeNode* node,
int d) {
83 InstanceTree::InstanceList& list = node->data();
84 for(InstanceTree::InstanceList::const_iterator it(list.begin()); it != list.end(); ++it) {
85 ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
86 if( searchRect.contains(Point(coords.x,coords.y)) ) {
87 instanceList.push_back(*it);
93 void InstanceTree::findInstances(
const ModelCoordinate& point,
int w,
int h, InstanceTree::InstanceList& list) {
94 InstanceTreeNode * node = m_tree.find_container(point.x, point.y, w, h);
95 Rect rect(point.x, point.y, w, h);
96 InstanceListCollector collector(list,rect);
98 node->apply_visitor(collector);
100 node = node->parent();
102 for(InstanceList::const_iterator it(node->data().begin()); it != node->data().end(); ++it) {
103 ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
104 if( rect.contains(Point(coords.x,coords.y)) ) {
108 node = node->parent();