1 /**********************************************************************
4 * GEOS - Geometry Engine Open Source
5 * http://geos.refractions.net
7 * Copyright (C) 2006 Refractions Research Inc.
8 * Copyright (C) 2001-2002 Vivid Solutions Inc.
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
15 **********************************************************************/
20 #include <geos/index/bintree/Node.h>
21 #include <geos/index/bintree/Key.h>
22 #include <geos/index/bintree/Interval.h>
25 namespace index
{ // geos.index
26 namespace bintree
{ // geos.index.bintree
29 Node::createNode(Interval
*itemInterval
)
31 Key
*key
=new Key(itemInterval
);
32 //System.out.println("input: " + env + " binaryEnv: " + key.getEnvelope());
33 Node
* node
=new Node(new Interval(key
->getInterval()),key
->getLevel());
39 Node::createExpanded(Node
*node
,Interval
*addInterval
)
41 Interval
*expandInt
=new Interval(addInterval
);
42 if (node
!=NULL
) expandInt
->expandToInclude(node
->interval
);
43 Node
*largerNode
=createNode(expandInt
);
44 if (node
!=NULL
) largerNode
->insert(node
);
49 Node::Node(Interval
*newInterval
,int newLevel
)
53 centre
=(interval
->getMin()+interval
->getMax())/2;
68 Node::isSearchMatch(Interval
*itemInterval
)
70 return itemInterval
->overlaps(interval
);
74 * Returns the subnode containing the envelope.
76 * it does not already exist.
79 Node::getNode(Interval
*searchInterval
)
81 int subnodeIndex
=getSubnodeIndex(searchInterval
,centre
);
82 // if index is -1 searchEnv is not contained in a subnode
83 if (subnodeIndex
!=-1) {
84 // create the node if it does not exist
85 Node
* node
=getSubnode(subnodeIndex
);
86 // recursively search the found/created node
87 return node
->getNode(searchInterval
);
94 * Returns the smallest <i>existing</i>
95 * node containing the envelope.
98 Node::find(Interval
*searchInterval
)
100 int subnodeIndex
=getSubnodeIndex(searchInterval
,centre
);
101 if (subnodeIndex
==-1)
103 if (subnode
[subnodeIndex
]!=NULL
) {
104 // query lies in subnode, so search it
105 Node
*node
=subnode
[subnodeIndex
];
106 return node
->find(searchInterval
);
108 // no existing subnode, so return this one anyway
113 Node::insert(Node
*node
)
115 assert(interval
==NULL
|| interval
->contains(node
->interval
));
116 int index
=getSubnodeIndex(node
->interval
,centre
);
118 if (node
->level
==level
-1) {
121 // the node is not a direct child, so make a new child node to contain it
122 // and recursively insert the node
123 Node
* childNode
=createSubnode(index
);
124 childNode
->insert(node
);
125 subnode
[index
]=childNode
;
130 * get the subnode for the index.
131 * If it doesn't exist, create it
134 Node::getSubnode(int index
)
136 if (subnode
[index
]==NULL
) {
137 subnode
[index
]=createSubnode(index
);
139 return subnode
[index
];
143 Node::createSubnode(int index
)
145 // create a new subnode in the appropriate interval
150 min
=interval
->getMin();
155 max
=interval
->getMax();
158 Interval
* subInt
=new Interval(min
,max
);
159 Node
*node
=new Node(subInt
,level
-1);
163 } // namespace geos.index.bintree
164 } // namespace geos.index
167 /**********************************************************************
169 * Revision 1.3 2006/03/22 16:01:33 strk
170 * indexBintree.h header split, classes renamed to match JTS
172 **********************************************************************/