GEOS  3.9.0dev
Centroid.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2013 Sandro Santilli <strk@kbt.io>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU Lesser General Public Licence as published
10  * by the Free Software Foundation.
11  * See the COPYING file for more information.
12  *
13  **********************************************************************
14  *
15  * Last port: algorithm/Centroid.java r728 (JTS-0.13+)
16  *
17  **********************************************************************/
18
19 #ifndef GEOS_ALGORITHM_CENTROID_H
20 #define GEOS_ALGORITHM_CENTROID_H
21
22 #include <geos/export.h>
23 #include <geos/geom/Coordinate.h> // for composition
24 #include <memory> // for std::unique_ptr
25
26 // Forward declarations
27 namespace geos {
28 namespace geom {
29 class Geometry;
30 class Polygon;
31 class CoordinateSequence;
32 }
33 }
34
35
36 namespace geos {
37 namespace algorithm { // geos::algorithm
38
61 class GEOS_DLL Centroid {
62
63 public:
64
74  static bool getCentroid(const geom::Geometry& geom, geom::Coordinate& cent);
75
79  Centroid(const geom::Geometry& geom)
80  :
81  areasum2(0.0),
82  totalLength(0.0),
83  ptCount(0)
84  {
85  add(geom);
86  }
87
96  bool getCentroid(geom::Coordinate& cent) const;
97
98 private:
99
100  std::unique_ptr<geom::Coordinate> areaBasePt;
101  geom::Coordinate triangleCent3;
102  geom::Coordinate cg3;
103  geom::Coordinate lineCentSum;
104  geom::Coordinate ptCentSum;
105  double areasum2;
106  double totalLength;
107  int ptCount;
108
114  void add(const geom::Geometry& geom);
115
116  void setAreaBasePoint(const geom::Coordinate& basePt);
117
118  void add(const geom::Polygon& poly);
119
120  void addShell(const geom::CoordinateSequence& pts);
121
122  void addHole(const geom::CoordinateSequence& pts);
123
124  void addTriangle(const geom::Coordinate& p0, const geom::Coordinate& p1, const geom::Coordinate& p2,
125  bool isPositiveArea);
126
132  static void centroid3(const geom::Coordinate& p1, const geom::Coordinate& p2, const geom::Coordinate& p3,
133  geom::Coordinate& c);
134
139  static double area2(const geom::Coordinate& p1, const geom::Coordinate& p2, const geom::Coordinate& p3);
140
147  void addLineSegments(const geom::CoordinateSequence& pts);
148
153  void addPoint(const geom::Coordinate& pt);
154 };
155
156 } // namespace geos::algorithm
157 } // namespace geos
158
159 #endif // GEOS_ALGORITHM_CENTROID_H
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:60
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Centroid(const geom::Geometry &geom)
Creates a new instance for computing the centroid of a geometry.
Definition: Centroid.h:79
Represents a linear polygon, which may include holes.
Definition: Polygon.h:64
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:26
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:58
Computes the centroid of a Geometry of any dimension.
Definition: Centroid.h:61