/********************************************************************** * $Id: QuadTreeNodeBase.cpp,v 1.10.2.1 2005/05/23 18:41:51 strk Exp $ * * GEOS - Geometry Engine Open Source * http://geos.refractions.net * * Copyright (C) 2001-2002 Vivid Solutions Inc. * * This is free software; you can redistribute and/or modify it under * the terms of the GNU Lesser General Public Licence as published * by the Free Software Foundation. * See the COPYING file for more information. * **********************************************************************/ #include #include #ifndef DEBUG #define DEBUG 0 #endif namespace geos { /** * Returns the index of the subquad that wholly contains the given envelope. * If none does, returns -1. */ int QuadTreeNodeBase::getSubnodeIndex(const Envelope *env, const Coordinate *centre) { int subnodeIndex=-1; if (env->getMinX()>=centre->x) { if (env->getMinY()>=centre->y) subnodeIndex=3; if (env->getMaxY()<=centre->y) subnodeIndex=1; } if (env->getMaxX()<=centre->x) { if (env->getMinY()>=centre->y) subnodeIndex=2; if (env->getMaxY()<=centre->y) subnodeIndex=0; } #if DEBUG cerr<<"getSubNodeIndex("<toString()<<", "<toString()<<") returning "<(); subnode[0]=NULL; subnode[1]=NULL; subnode[2]=NULL; subnode[3]=NULL; } QuadTreeNodeBase::~QuadTreeNodeBase() { delete subnode[0]; delete subnode[1]; delete subnode[2]; delete subnode[3]; subnode[0]=NULL; subnode[1]=NULL; subnode[2]=NULL; subnode[3]=NULL; delete items; } vector* QuadTreeNodeBase::getItems() { return items; } void QuadTreeNodeBase::add(void* item) { items->push_back(item); //DEBUG itemCount++; //DEBUG System.out.print(itemCount); } vector* QuadTreeNodeBase::addAllItems(vector *resultItems) { //<> Can we assert that this node cannot have both items //and subnodes? [Jon Aquino] resultItems->insert(resultItems->end(),items->begin(),items->end()); for(int i=0;i<4;i++) { if (subnode[i]!=NULL) { subnode[i]->addAllItems(resultItems); } } return resultItems; } void QuadTreeNodeBase::addAllItemsFromOverlapping(const Envelope *searchEnv, vector *resultItems) { if (!isSearchMatch(searchEnv)) return; //<> Can we assert that this node cannot have both items //and subnodes? [Jon Aquino] resultItems->insert(resultItems->end(),items->begin(),items->end()); for(int i=0;i<4;i++) { if (subnode[i]!=NULL) { subnode[i]->addAllItemsFromOverlapping(searchEnv,resultItems); } } } //<> In Samet's terminology, I think what we're returning here is //actually level+1 rather than depth. (See p. 4 of his book) [Jon Aquino] int QuadTreeNodeBase::depth() { int maxSubDepth=0; for(int i=0;i<4;i++) { if (subnode[i]!=NULL) { int sqd=subnode[i]->depth(); if (sqd>maxSubDepth) maxSubDepth=sqd; } } return maxSubDepth+1; } //<> "size" is a bit generic. How about "itemCount"? [Jon Aquino] int QuadTreeNodeBase::size() { int subSize=0; for(int i=0;i<4;i++) { if (subnode[i]!=NULL) { subSize+=subnode[i]->size(); } } return subSize+(int)items->size(); } //<> The Java Language Specification recommends that "Methods to //get and set an attribute that might be thought of as a variable V should be //named getV and setV" (6.8.3). Perhaps this and other methods should be //renamed to "get..."? [Jon Aquino] int QuadTreeNodeBase::nodeCount() { int subSize=0; for(int i=0;i<4;i++) { if (subnode[i]!=NULL) { subSize+=subnode[i]->size(); } } return subSize+1; } string QuadTreeNodeBase::toString() const { ostringstream s; s<<"ITEMS:"<size()<toString(); s<