GEOS  3.14.0dev
IndexedPointInAreaLocator.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2006 Refractions Research Inc.
7  * Copyright (C) 2018 Daniel Baston <dbaston@gmail.com>
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  *
15  **********************************************************************/
16 
17 #pragma once
18 
19 #include <geos/geom/LineSegment.h>
20 #include <geos/algorithm/locate/PointOnGeometryLocator.h> // inherited
21 #include <geos/index/ItemVisitor.h> // inherited
22 #include <geos/index/strtree/TemplateSTRtree.h>
23 
24 #include <memory>
25 #include <vector> // composition
26 
27 namespace geos {
28 namespace algorithm {
29 class RayCrossingCounter;
30 }
31 namespace geom {
32 class Geometry;
33 class Coordinate;
34 class CoordinateSequence;
35 }
36 }
37 
38 namespace geos {
39 namespace algorithm { // geos::algorithm
40 namespace locate { // geos::algorithm::locate
41 
55 private:
56  struct SegmentView {
57  SegmentView(const geom::CoordinateXY* p0, const geom::CoordinateXY* p1) {
58  // There is a significant performance benefit in fitting our
59  // line segment into 8 bytes (about 15-20%). Because we know that
60  // p1 follows p0 in a CoordinateSequence, we know that the address
61  // of p1 is 16, 24, or 32 bytes greater than the address of p0.
62  // By packing this offset into the least significant bits of p0,
63  // we can retrieve both p0 and p1 while only using 8 bytes.
64  std::size_t os = static_cast<std::size_t>(reinterpret_cast<const double*>(p1) - reinterpret_cast<const double*>(p0)) - 2u;
65  m_p0 = reinterpret_cast<std::size_t>(p0) | os;
66 
67  assert(&this->p0() == p0);
68  assert(&this->p1() == p1);
69  }
70 
71  const geom::CoordinateXY& p0() const {
72  auto ret = reinterpret_cast<const geom::CoordinateXY*>(m_p0 >> 2 << 2);
73  return *ret;
74  }
75 
76  const geom::CoordinateXY& p1() const {
77  auto offset = (m_p0 & 0x03) + 2;
78  auto ret = reinterpret_cast<const geom::CoordinateXY*>(reinterpret_cast<double*>(m_p0 >> 2 << 2) + offset);
79  return *ret;
80  }
81 
82  std::size_t m_p0;
83  };
84 
85  class IntervalIndexedGeometry {
86  private:
87 
88  index::strtree::TemplateSTRtree<SegmentView, index::strtree::IntervalTraits> index;
89 
90  void init(const geom::Geometry& g);
91  void addLine(const geom::CoordinateSequence* pts);
92 
93  public:
94  IntervalIndexedGeometry(const geom::Geometry& g);
95 
96  template<typename Visitor>
97  void query(double min, double max, Visitor&& f) {
98  index.query(index::strtree::Interval(min, max), f);
99  }
100  };
101 
102  const geom::Geometry& areaGeom;
103  std::unique_ptr<IntervalIndexedGeometry> index;
104 
105  void buildIndex(const geom::Geometry& g);
106 
107  // Declare type as noncopyable
109  IndexedPointInAreaLocator& operator=(const IndexedPointInAreaLocator& rhs) = delete;
110 
111 public:
120 
121  const geom::Geometry& getGeometry() const {
122  return areaGeom;
123  }
124 
132  geom::Location locate(const geom::CoordinateXY* /*const*/ p) override;
133 
134 };
135 
136 } // geos::algorithm::locate
137 } // geos::algorithm
138 } // geos
139 
Determines the location of Coordinates relative to an areal geometry, using indexing for efficiency.
Definition: IndexedPointInAreaLocator.h:54
IndexedPointInAreaLocator(const geom::Geometry &g)
Creates a new locator for a given Geometry.
geom::Location locate(const geom::CoordinateXY *p) override
Determines the Location of a point in an areal Geometry.
An interface for classes which determine the Location of points in Polygon or MultiPolygon geometries...
Definition: PointOnGeometryLocator.h:36
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:197
A contiguous portion of 1D-space. Used internally by SIRtree.
Definition: strtree/Interval.h:30
Location
Constants representing the location of a point relative to a geometry.
Definition: Location.h:32
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25