26 #ifndef __TERRALIB_SAM_RTREE_INTERNAL_INDEX_H
27 #define __TERRALIB_SAM_RTREE_INTERNAL_INDEX_H
56 template<
class DATATYPE,
int MAXNODES = 8,
int MINNODES = MAXNODES / 2>
class Index
84 std::size_t
size(
void)
const;
170 const DATATYPE& data,
180 std::vector<DATATYPE>& report,
181 int& foundObjs)
const;
260 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
262 : m_root(0), m_size(0)
269 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
271 : m_root(0), m_mbr(mbr), m_size(0)
279 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
286 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
292 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
295 return (m_root->m_count == 0);
298 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
312 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
315 insert(mbr, data, &m_root, 0);
319 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
322 return remove(mbr, data, &m_root);
325 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
331 search(mbr, m_root, report, foundObjs);
336 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
342 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
348 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
354 if(chooseLeaf(mbr, data, *root, &newNode, level))
362 newRoot->
m_level = (*root)->m_level + 1;
366 branch.
m_mbr = nodeCover(*root);
368 addBranch(&branch, newRoot, 0);
371 branch.
m_mbr = nodeCover(newNode);
373 addBranch(&branch, newRoot, 0);
383 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
387 std::vector<NodeType*> reInsertList;
389 if(remove2(mbr, data, *root, reInsertList))
394 while(!reInsertList.empty())
398 for(
int i = 0; i < t->
m_count; ++i)
402 reInsertList.erase(reInsertList.begin());
410 if(((*root)->m_count == 1) && ((*root)->isInternalNode()))
426 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
433 for(i = 0; i < n->
m_count; ++i)
457 for(i = 0; i < n->
m_count; ++i)
470 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
479 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
485 int i = pickBranch(mbr, node);
502 b.
m_mbr = nodeCover(n2);
504 return addBranch(&b, node, newNode);
507 else if (node->
m_level == level)
516 return addBranch(&b, node, newNode);
526 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
533 for(i = 0; i < node->
m_count; ++i)
542 for(i = 0; i < node->
m_count; ++i)
547 report.push_back(
id);
557 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
564 for(
int i = 0; i < n->
m_count; ++i)
578 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
586 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
597 splitNode(node, branch, newNode);
602 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
607 double bestIncr = -1.0;
608 double bestArea = 0.;
611 for(
int i = 0; i < node->
m_count; ++i)
616 rr = combineRect(mbr, rr);
618 double increase = rr.
getArea() - area;
620 if((increase < bestIncr) || flag)
627 else if((increase == bestIncr) && (area < bestArea))
638 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
646 getBranches(node, branch, partitions);
649 methodZero(partitions);
655 (*newNode)->m_level = node->
m_level = level;
657 loadNodes(node, *newNode, partitions);
660 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
664 for(
int i = 0; i < MAXNODES; ++i)
674 for(
int i = 1; i <= MAXNODES; ++i)
680 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
688 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
696 int greatestLower[2];
702 greatestLower[0] = leastUpper[0] = 0;
704 for(i = 1; i <= MAXNODES; ++i)
709 greatestLower[0] = i;
721 greatestLower[1] = leastUpper[1] = 0;
723 for(i = 1; i <= MAXNODES; ++i)
728 greatestLower[1] = i;
753 seed0 = leastUpper[0];
754 seed1 = greatestLower[0];
770 if(separation > bestSep)
772 seed0 = leastUpper[1];
773 seed1 = greatestLower[1];
775 bestSep = separation;
780 classify(seed0, 0, p);
781 classify(seed1, 1, p);
785 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
801 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
809 for(
int i = 0; i <= MAXNODES; ++i)
814 if(p.
m_count[0] >= (MAXNODES + 1 - MINNODES))
819 else if(p.
m_count[1] >= (MAXNODES + 1 - MINNODES))
826 for(
int group = 0; group < 2; ++group)
837 newArea[group] = newCover[group].
getArea();
838 increase[group] = newArea[group] - p.
m_area[group];
842 if(increase[0] < increase[1])
844 else if(increase[1] < increase[0])
860 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
863 for(
int i = 0; i <= MAXNODES; ++i)
874 template<
class DATATYPE,
int MAXNODES,
int MINNODES>
inline
883 for(
int i = 0u; i < node->
m_count; ++i)
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
An Envelope defines a 2D rectangular region.
bool isValid() const
It tells if the rectangle is valid or not.
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
double m_llx
Lower left corner x-coordinate.
double getArea() const
It returns the area of this envelope as measured in the spatial reference system of it.
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
double m_urx
Upper right corner x-coordinate.
double m_ury
Upper right corner y-coordinate.
double m_lly
Lower left corner y-coordinate.
A class that represents an R-tree.
void pigeonhole(PartitionVarsType &p) const
te::gm::Envelope combineRect(const te::gm::Envelope &mbrA, const te::gm::Envelope &mbrB) const
Combine two rectangles into larger one containing both.
void getBranches(NodeType *n, BranchType *b, PartitionVarsType &p) const
bool insert(const te::gm::Envelope &mbr, const DATATYPE &data, NodeType **root, int level)
It inserts a data rectangle into an index structure.
bool isEmpty(void) const
It returns true if the tree is empty.
bool remove(const te::gm::Envelope &mbr, const DATATYPE &data)
It removes an item from the tree.
void disconBranch(NodeType *n, int i)
It disconnects a dependent node.
void search(const te::gm::Envelope &mbr, NodeType *node, std::vector< DATATYPE > &report, int &foundObjs) const
Recursive range query.
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
bool remove2(const te::gm::Envelope &mbr, const DATATYPE &data, NodeType *n, std::vector< NodeType * > &ee)
It deletes a rectangle from non-root part of an index structure.
std::size_t m_size
The size of R-Tree (number of nodes).
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
void methodZero(PartitionVarsType &p) const
Index(const te::gm::Envelope &mbr)
Constructor.
void setMBR(const te::gm::Envelope &mbr)
It sets the bounding box of all elements in the tree.
void loadNodes(NodeType *n, NodeType *q, PartitionVarsType &p) const
bool chooseLeaf(const te::gm::Envelope &mbr, const DATATYPE &data, NodeType *node, NodeType **newNode, int level)
It inserts a new data rectangle into the index structure.
Index(const Index &rhs)
No copy constructor allowed.
NodeType * m_root
Pointer to the root node.
void splitNode(NodeType *node, BranchType *branch, NodeType **newNode) const
Split a node.
Index & operator=(const Index &rhs)
No assignment operator allowed.
std::size_t size(void) const
It returns the number of elements in the tree.
NodeType::BranchType BranchType
te::sam::rtree::PartitionVars< BranchType, MAXNODES > PartitionVarsType
void classify(int i, int group, PartitionVarsType &p) const
Node< DATATYPE, MAXNODES, MINNODES > NodeType
te::gm::Envelope nodeCover(NodeType *n) const
Find the smallest rectangle that includes all rectangles in branches of a node.
bool remove(const te::gm::Envelope &mbr, const DATATYPE &data, NodeType **root)
It deletes a data rectangle from an index structure.
int pickBranch(const te::gm::Envelope &mbr, NodeType *node) const
Pick a branch.
void erase(NodeType *node)
Erases a node from the tree and all nodes below it.
void pickSeeds(PartitionVarsType &p) const
te::gm::Envelope m_mbr
Bounding box of the tree.
const te::gm::Envelope & getMBR(void) const
It returns the bounding box of all elements in the tree.
bool addBranch(BranchType *branch, NodeType *node, NodeType **newNode) const
Add a branch to a node.
A class that represents an R-tree node.
BranchType m_branch[MAXNODES]
Branches.
bool isInternalNode() const
It returns true if this is a internal node.
void init()
This method is used during split when a node retained and used again (beeing re-filled).
int m_count
The number of elements in the node (count).
bool isLeaf() const
It returns true if this is a leaf node.
int m_level
Leaf is zero, others positive.
NodeType
The type of node read by XML reader.
A class that represents an R-tree node.
NODE * m_child
A pointer to the child node.
DATATYPE m_data
An object-identifier (oid).
te::gm::Envelope m_mbr
Bounding box containing all the objects under the branch or an object bounding box.
Auxiliary structure when searching for a split partition.
te::gm::Envelope m_cover[2]
Auxiliary box of each new page.
int m_count[2]
Number of entries in each new page.
te::gm::Envelope m_coverSplit
Auxiliary box covering branchBuf.
int m_taken[MAXNODES+1]
Flag to indicate that entry is ok.
void init()
Initializes partition vars.
double m_area[2]
Auxiliary area of each new page.
int m_partition[MAXNODES+1]
Auxiliary partition vector.
BRANCH m_branchBuf[MAXNODES+1]
Auxiliary branch buffer.