GEOS 3.11.1
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#pragma once
20
21#include <geos/export.h>
22#include <geos/geom/Coordinate.h> // for composition
23#include <memory> // for std::unique_ptr
24
25// Forward declarations
26namespace geos {
27namespace geom {
28class Geometry;
29class Polygon;
30class CoordinateSequence;
31}
32}
33
34
35namespace geos {
36namespace algorithm { // geos::algorithm
37
60class GEOS_DLL Centroid {
61
62public:
63
73 static bool getCentroid(const geom::Geometry& geom, geom::Coordinate& cent);
74
79 :
80 areasum2(0.0),
81 totalLength(0.0),
82 ptCount(0)
83 {
84 add(geom);
85 }
86
95 bool getCentroid(geom::Coordinate& cent) const;
96
97private:
98
99 std::unique_ptr<geom::Coordinate> areaBasePt;
100 geom::Coordinate triangleCent3;
102 geom::Coordinate lineCentSum;
103 geom::Coordinate ptCentSum;
104 double areasum2;
105 double totalLength;
106 int ptCount;
107
113 void add(const geom::Geometry& geom);
114
115 void setAreaBasePoint(const geom::Coordinate& basePt);
116
117 void add(const geom::Polygon& poly);
118
119 void addShell(const geom::CoordinateSequence& pts);
120
121 void addHole(const geom::CoordinateSequence& pts);
122
123 void addTriangle(const geom::Coordinate& p0, const geom::Coordinate& p1, const geom::Coordinate& p2,
124 bool isPositiveArea);
125
131 static void centroid3(const geom::Coordinate& p1, const geom::Coordinate& p2, const geom::Coordinate& p3,
133
138 static double area2(const geom::Coordinate& p1, const geom::Coordinate& p2, const geom::Coordinate& p3);
139
146 void addLineSegments(const geom::CoordinateSequence& pts);
147
152 void addPoint(const geom::Coordinate& pt);
153};
154
155} // namespace geos::algorithm
156} // namespace geos
157
Computes the centroid of a Geometry of any dimension.
Definition: Centroid.h:60
Centroid(const geom::Geometry &geom)
Creates a new instance for computing the centroid of a geometry.
Definition: Centroid.h:78
static bool getCentroid(const geom::Geometry &geom, geom::Coordinate &cent)
Computes the centroid point of a geometry.
bool getCentroid(geom::Coordinate &cent) const
Gets the computed centroid.
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:44
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Represents a linear polygon, which may include holes.
Definition: Polygon.h:61
Basic namespace for all GEOS functionalities.
Definition: geos.h:39