GEOS  3.3.8
LineSequencer.h
00001 /**********************************************************************
00002  * $Id: LineSequencer.h 3309 2011-04-27 15:47:14Z strk $
00003  *
00004  * GEOS - Geometry Engine Open Source
00005  * http://geos.refractions.net
00006  *
00007  * Copyright (C) 2011 Sandro Santilli <strk@keybit.net>
00008  * Copyright (C) 2006 Refractions Research Inc.
00009  *
00010  * This is free software; you can redistribute and/or modify it under
00011  * the terms of the GNU Lesser General Public Licence as published
00012  * by the Free Software Foundation. 
00013  * See the COPYING file for more information.
00014  *
00015  **********************************************************************
00016  *
00017  * Last port: operation/linemerge/LineSequencer.java r378 (JTS-1.12)
00018  *
00019  **********************************************************************/
00020 
00021 #ifndef GEOS_OP_LINEMERGE_LINESEQUENCER_H
00022 #define GEOS_OP_LINEMERGE_LINESEQUENCER_H
00023 
00024 #include <geos/export.h>
00025 
00026 #include <geos/operation/linemerge/LineMergeGraph.h> // for composition
00027 #include <geos/geom/Geometry.h> // for inlines
00028 #include <geos/geom/LineString.h> // for inlines
00029 
00030 #include <vector>
00031 #include <list>
00032 #include <memory> // for auto_ptr
00033 
00034 #ifdef _MSC_VER
00035 #pragma warning(push)
00036 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
00037 #endif
00038 
00039 // Forward declarations 
00040 namespace geos {
00041         namespace geom { 
00042                 class GeometryFactory;
00043                 class Geometry;
00044                 class LineString;
00045         }
00046         namespace planargraph { 
00047                 class DirectedEdge;
00048                 class Subgraph;
00049                 class Node;
00050         }
00051 }
00052 
00053 
00054 namespace geos {
00055 namespace operation { // geos::operation
00056 namespace linemerge { // geos::operation::linemerge
00057 
00100 class GEOS_DLL LineSequencer {
00101 
00102 private:
00103         typedef std::list<planargraph::DirectedEdge*> DirEdgeList;
00104         typedef std::vector< DirEdgeList* > Sequences;
00105 
00106         LineMergeGraph graph;
00107         const geom::GeometryFactory *factory;
00108         unsigned int lineCount;
00109         bool isRun;
00110         std::auto_ptr<geom::Geometry> sequencedGeometry;
00111         bool isSequenceableVar;
00112 
00113         void addLine(const geom::LineString *lineString);
00114         void computeSequence();
00115         Sequences* findSequences();
00116         DirEdgeList* findSequence(planargraph::Subgraph& graph);
00117 
00118         void delAll( Sequences& );
00119 
00121         static geom::LineString* reverse(const geom::LineString *line);
00122 
00135         geom::Geometry* buildSequencedGeometry(const Sequences& sequences);
00136 
00137         static const planargraph::Node* findLowestDegreeNode(
00138                         const planargraph::Subgraph& graph);
00139 
00140         void addReverseSubpath(const planargraph::DirectedEdge *de,
00141                 DirEdgeList& deList,
00142                 DirEdgeList::iterator lit,
00143                 bool expectedClosed);
00144         
00153         static const planargraph::DirectedEdge* findUnvisitedBestOrientedDE(
00154                         const planargraph::Node* node);
00155 
00174         DirEdgeList* orient(DirEdgeList* seq);
00175 
00184         DirEdgeList* reverse(DirEdgeList& seq);
00185 
00193         bool hasSequence(planargraph::Subgraph& graph);
00194 
00195 public:
00196 
00197         static geom::Geometry* sequence(const geom::Geometry& geom)
00198         {
00199                 LineSequencer sequencer;
00200                 sequencer.add(geom);
00201                 return sequencer.getSequencedLineStrings();
00202         }
00203 
00204         LineSequencer()
00205                 :
00206                 factory(0),
00207                 lineCount(0),
00208                 isRun(false),
00209                 sequencedGeometry(0),
00210                 isSequenceableVar(false)
00211                 {}
00212 
00223         static bool isSequenced(const geom::Geometry* geom);
00224 
00231         bool isSequenceable() {
00232                 computeSequence();
00233                 return isSequenceableVar;
00234         }
00235 
00244         void add(const geom::Geometry& geometry) {
00245                 geometry.applyComponentFilter(*this);
00246         }
00247 
00248   template <class TargetContainer>
00249   void add(TargetContainer& geoms)
00250   {
00251     for (typename TargetContainer::const_iterator i = geoms.begin(),
00252          e = geoms.end(); i != e; ++i)
00253     {
00254       const geom::Geometry* g = *i;
00255       add(*g);
00256     }
00257   }
00258 
00263         void filter(const geom::Geometry* g)
00264         {
00265                 if (const geom::LineString *ls=dynamic_cast<const geom::LineString *>(g))
00266                 {
00267                         addLine(ls);
00268                 }
00269         }
00270 
00280         geom::Geometry*
00281         getSequencedLineStrings(bool release=1) {
00282                 computeSequence();
00283                 if (release) return sequencedGeometry.release();
00284                 else return sequencedGeometry.get();
00285         }
00286 };
00287 
00288 } // namespace geos::operation::linemerge
00289 } // namespace geos::operation
00290 } // namespace geos
00291 
00292 #ifdef _MSC_VER
00293 #pragma warning(pop)
00294 #endif
00295 
00296 #endif // GEOS_OP_LINEMERGE_LINESEQUENCER_H
00297 
00298 /**********************************************************************
00299  * $Log$
00300  * Revision 1.1  2006/03/22 10:13:53  strk
00301  * opLinemerge.h split
00302  *
00303  **********************************************************************/