GEOS  3.13.0dev
NodedSegmentString.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
7  * Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
8  * Copyright (C) 2006 Refractions Research Inc.
9  * Copyright (C) 2001-2002 Vivid Solutions Inc.
10  *
11  * This is free software; you can redistribute and/or modify it under
12  * the terms of the GNU Lesser General Public Licence as published
13  * by the Free Software Foundation.
14  * See the COPYING file for more information.
15  *
16  *
17  **********************************************************************
18  *
19  * Last port: noding/NodedSegmentString.java r320 (JTS-1.12)
20  *
21  **********************************************************************/
22 
23 #pragma once
24 
25 #include <geos/export.h>
26 #include <geos/algorithm/LineIntersector.h>
27 #include <geos/geom/Coordinate.h>
28 #include <geos/geom/CoordinateSequence.h> // for inlines
29 #include <geos/noding/NodedSegmentString.h>
30 #include <geos/noding/NodableSegmentString.h> // for inheritance
31 #include <geos/noding/SegmentNode.h>
32 #include <geos/noding/SegmentNodeList.h>
33 #include <geos/noding/SegmentString.h>
34 #include <geos/util/IllegalArgumentException.h>
35 
36 #include <cstddef>
37 
38 #ifdef _MSC_VER
39 #pragma warning(push)
40 #pragma warning(disable: 4251 4355) // warning C4355: 'this' : used in base member initializer list
41 #endif
42 
43 namespace geos {
44 namespace noding { // geos::noding
45 
58 class GEOS_DLL NodedSegmentString : public NodableSegmentString {
59 public:
60 
61  // TODO: provide a templated method using an output iterator
62  template <class II>
63  static void
64  getNodedSubstrings(II from, II too_far,
65  SegmentString::NonConstVect* resultEdgelist)
66  {
67  for(II i = from; i != too_far; ++i) {
68  NodedSegmentString* nss = dynamic_cast<NodedSegmentString*>(*i);
69  assert(nss);
70  nss->getNodeList().addSplitEdges(resultEdgelist);
71  }
72  }
73 
74  template <class C>
75  static void
76  getNodedSubstrings(C* segStrings,
77  SegmentString::NonConstVect* resultEdgelist)
78  {
79  getNodedSubstrings(segStrings->begin(), segStrings->end(), resultEdgelist);
80  }
81 
82  static void getNodedSubstrings(const SegmentString::NonConstVect& segStrings,
83  SegmentString::NonConstVect* resultEdgeList);
84 
86  static SegmentString::NonConstVect* getNodedSubstrings(
87  const SegmentString::NonConstVect& segStrings);
88 
89  std::unique_ptr<geom::CoordinateSequence> getNodedCoordinates();
90 
91  bool hasNodes() const
92  {
93  return nodeList.size() > 0;
94  }
95 
106  NodedSegmentString(geom::CoordinateSequence* newPts, bool constructZ, bool constructM, const void* newContext)
107  : NodableSegmentString(newContext, newPts)
108  , nodeList(*this, constructZ, constructM)
109  {}
110 
111  NodedSegmentString(SegmentString* ss, bool constructZ, bool constructM)
112  : NodableSegmentString(ss->getData(), ss->getCoordinates()->clone().release())
113  , nodeList(*this, constructZ, constructM)
114  {}
115 
116  ~NodedSegmentString() override {
117  delete seq;
118  }
119 
120  SegmentNodeList& getNodeList();
121 
122  const SegmentNodeList& getNodeList() const;
123 
124  std::unique_ptr<geom::CoordinateSequence> releaseCoordinates();
125 
126  std::ostream& print(std::ostream& os) const override;
127 
128 
135  std::size_t segmentIndex, std::size_t geomIndex)
136  {
137  for (std::size_t i = 0, n = li->getIntersectionNum(); i < n; ++i) {
138  addIntersection(li, segmentIndex, geomIndex, i);
139  }
140  };
141 
150  std::size_t segmentIndex,
151  std::size_t geomIndex, std::size_t intIndex)
152  {
153  ::geos::ignore_unused_variable_warning(geomIndex);
154 
155  const auto& intPt = li->getIntersection(intIndex);
156  addIntersection(intPt, segmentIndex);
157  };
158 
166  template<typename CoordType>
167  void addIntersection(const CoordType& intPt,
168  std::size_t segmentIndex)
169  {
170  std::size_t normalizedSegmentIndex = segmentIndex;
171 
172  if (segmentIndex > size() - 2) {
173  throw util::IllegalArgumentException("SegmentString::addIntersection: SegmentIndex out of range");
174  }
175 
176  // normalize the intersection point location
177  auto nextSegIndex = normalizedSegmentIndex + 1;
178  if (nextSegIndex < size()) {
179  const auto& nextPt = getCoordinate<geom::CoordinateXY>(nextSegIndex);
180 
181  // Normalize segment index if intPt falls on vertex
182  // The check for point equality is 2D only -
183  // Z values are ignored
184  if(intPt.equals2D(nextPt)) {
185  normalizedSegmentIndex = nextSegIndex;
186  }
187  }
188 
189  /*
190  * Add the intersection point to edge intersection list
191  * (unless the node is already known)
192  */
193  nodeList.add(intPt, normalizedSegmentIndex);
194  }
195 
196 private:
197 
198  SegmentNodeList nodeList;
199 
200 };
201 
202 } // namespace geos::noding
203 } // namespace geos
204 
205 #ifdef _MSC_VER
206 #pragma warning(pop)
207 #endif
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
A LineIntersector is an algorithm that can both test whether two line segments intersect and compute ...
Definition: LineIntersector.h:53
const geom::CoordinateXYZM & getIntersection(std::size_t intIndex) const
Definition: LineIntersector.h:220
size_t getIntersectionNum() const
Definition: LineIntersector.h:207
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
An interface for classes which support adding nodes to a segment string.
Definition: NodableSegmentString.h:36
Represents a list of contiguous line segments, and supports noding the segments.
Definition: NodedSegmentString.h:58
void addIntersection(algorithm::LineIntersector *li, std::size_t segmentIndex, std::size_t geomIndex, std::size_t intIndex)
Add an SegmentNode for intersection intIndex.
Definition: NodedSegmentString.h:149
static SegmentString::NonConstVect * getNodedSubstrings(const SegmentString::NonConstVect &segStrings)
Returns allocated object.
NodedSegmentString(geom::CoordinateSequence *newPts, bool constructZ, bool constructM, const void *newContext)
Creates a new segment string from a list of vertices.
Definition: NodedSegmentString.h:106
void addIntersection(const CoordType &intPt, std::size_t segmentIndex)
Add an SegmentNode for intersection intIndex.
Definition: NodedSegmentString.h:167
void addIntersections(algorithm::LineIntersector *li, std::size_t segmentIndex, std::size_t geomIndex)
Add SegmentNodes for one or both intersections found for a segment of an edge to the edge intersectio...
Definition: NodedSegmentString.h:134
A list of the SegmentNode present along a NodedSegmentString.
Definition: SegmentNodeList.h:54
void addSplitEdges(std::vector< SegmentString * > &edgeList)
An interface for classes which represent a sequence of contiguous line segments.
Definition: SegmentString.h:47
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:33
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25