/********************************************************************** * $Id: MonotoneChainBuilder.cpp,v 1.12 2004/12/08 13:54:43 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 #include #define DEBUG 0 namespace geos { vector* MonotoneChainBuilder::getChains(CoordinateSequence *pts) { return getChains(pts,NULL); } /** * Return a list of the MonotoneChain * for the given list of coordinates. */ vector* MonotoneChainBuilder::getChains(CoordinateSequence *pts, void* context) { vector *mcList=new vector(); vector *startIndex=getChainStartIndices(pts); for(int i=0;i<(int)startIndex->size()-1;i++) { indexMonotoneChain *mc=new indexMonotoneChain(pts, (*startIndex)[i], (*startIndex)[i + 1], context); mcList->push_back(mc); } delete startIndex; return mcList; } /** * Return an array containing lists of start/end indexes of the monotone chains * for the given list of coordinates. * The last entry in the array points to the end point of the point array, * for use as a sentinel. */ vector* MonotoneChainBuilder::getChainStartIndices(CoordinateSequence *pts) { // find the startpoint (and endpoints) of all monotone chains // in this edge int start=0; vector *startIndexList=new vector(); startIndexList->push_back(start); do { int last=findChainEnd(pts, start); startIndexList->push_back(last); start=last; } while(startgetSize()-1); // copy list to an array of ints, for efficiency //int[] startIndex = toIntArray(startIndexList); return startIndexList; } /** * @return the index of the last point in the monotone chain starting * at start. */ int MonotoneChainBuilder::findChainEnd(CoordinateSequence *pts, int start) { // determine quadrant for chain int chainQuad=Quadrant::quadrant(pts->getAt(start),pts->getAt(start + 1)); int last=start+1; while (lastgetSize()) { // compute quadrant for next possible segment in chain int quad=Quadrant::quadrant(pts->getAt(last-1),pts->getAt(last)); if (quad!=chainQuad) break; last++; } #if DEBUG cerr<<"MonotoneChainBuilder::findChainEnd() returning"<