|
25 | 25 |
|
26 | 26 | import static java.lang.Math.sqrt; |
27 | 27 |
|
28 | | -public class OperatorCentroid2DLocal extends OperatorCentroid2D |
29 | | -{ |
30 | | - @Override |
31 | | - public Point2D execute(Geometry geometry, ProgressTracker progressTracker) |
32 | | - { |
33 | | - if (geometry.isEmpty()) { |
34 | | - return null; |
35 | | - } |
36 | | - |
37 | | - Geometry.Type geometryType = geometry.getType(); |
38 | | - switch (geometryType) { |
39 | | - case Point: |
40 | | - return ((Point) geometry).getXY(); |
41 | | - case Line: |
42 | | - return computeLineCentroid((Line) geometry); |
43 | | - case Envelope: |
44 | | - return ((Envelope) geometry).getCenterXY(); |
45 | | - case MultiPoint: |
46 | | - return computePointsCentroid((MultiPoint) geometry); |
47 | | - case Polyline: |
48 | | - return computePolylineCentroid(((Polyline) geometry)); |
49 | | - case Polygon: |
50 | | - return computePolygonCentroid((Polygon) geometry); |
51 | | - default: |
52 | | - throw new UnsupportedOperationException("Unexpected geometry type: " + geometryType); |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - private static Point2D computeLineCentroid(Line line) |
57 | | - { |
58 | | - return new Point2D((line.getEndX() - line.getStartX()) / 2, (line.getEndY() - line.getStartY()) / 2); |
59 | | - } |
60 | | - |
61 | | - // Points centroid is arithmetic mean of the input points |
62 | | - private static Point2D computePointsCentroid(MultiPoint multiPoint) |
63 | | - { |
64 | | - double xSum = 0; |
65 | | - double ySum = 0; |
66 | | - int pointCount = multiPoint.getPointCount(); |
67 | | - Point2D point2D = new Point2D(); |
68 | | - for (int i = 0; i < pointCount; i++) { |
69 | | - multiPoint.getXY(i, point2D); |
70 | | - xSum += point2D.x; |
71 | | - ySum += point2D.y; |
72 | | - } |
73 | | - return new Point2D(xSum / pointCount, ySum / pointCount); |
74 | | - } |
75 | | - |
76 | | - // Lines centroid is weighted mean of each line segment, weight in terms of line length |
77 | | - private static Point2D computePolylineCentroid(Polyline polyline) |
78 | | - { |
79 | | - double xSum = 0; |
80 | | - double ySum = 0; |
81 | | - double weightSum = 0; |
82 | | - |
83 | | - Point2D startPoint = new Point2D(); |
84 | | - Point2D endPoint = new Point2D(); |
85 | | - for (int i = 0; i < polyline.getPathCount(); i++) { |
86 | | - polyline.getXY(polyline.getPathStart(i), startPoint); |
87 | | - polyline.getXY(polyline.getPathEnd(i) - 1, endPoint); |
88 | | - double dx = endPoint.x - startPoint.x; |
89 | | - double dy = endPoint.y - startPoint.y; |
90 | | - double length = sqrt(dx * dx + dy * dy); |
91 | | - weightSum += length; |
92 | | - xSum += (startPoint.x + endPoint.x) * length / 2; |
93 | | - ySum += (startPoint.y + endPoint.y) * length / 2; |
94 | | - } |
95 | | - return new Point2D(xSum / weightSum, ySum / weightSum); |
96 | | - } |
97 | | - |
98 | | - // Polygon centroid: area weighted average of centroids in case of holes |
99 | | - private static Point2D computePolygonCentroid(Polygon polygon) |
100 | | - { |
101 | | - int pathCount = polygon.getPathCount(); |
102 | | - |
103 | | - if (pathCount == 1) { |
104 | | - return getPolygonSansHolesCentroid(polygon); |
105 | | - } |
106 | | - |
107 | | - double xSum = 0; |
108 | | - double ySum = 0; |
109 | | - double areaSum = 0; |
110 | | - |
111 | | - for (int i = 0; i < pathCount; i++) { |
112 | | - int startIndex = polygon.getPathStart(i); |
113 | | - int endIndex = polygon.getPathEnd(i); |
114 | | - |
115 | | - Polygon sansHoles = getSubPolygon(polygon, startIndex, endIndex); |
116 | | - |
117 | | - Point2D centroid = getPolygonSansHolesCentroid(sansHoles); |
118 | | - double area = sansHoles.calculateArea2D(); |
119 | | - |
120 | | - xSum += centroid.x * area; |
121 | | - ySum += centroid.y * area; |
122 | | - areaSum += area; |
123 | | - } |
124 | | - |
125 | | - return new Point2D(xSum / areaSum, ySum / areaSum); |
126 | | - } |
127 | | - |
128 | | - private static Polygon getSubPolygon(Polygon polygon, int startIndex, int endIndex) |
129 | | - { |
130 | | - Polyline boundary = new Polyline(); |
131 | | - boundary.startPath(polygon.getPoint(startIndex)); |
132 | | - for (int i = startIndex + 1; i < endIndex; i++) { |
133 | | - Point current = polygon.getPoint(i); |
134 | | - boundary.lineTo(current); |
135 | | - } |
136 | | - |
137 | | - final Polygon newPolygon = new Polygon(); |
138 | | - newPolygon.add(boundary, false); |
139 | | - return newPolygon; |
140 | | - } |
141 | | - |
142 | | - // Polygon sans holes centroid: |
143 | | - // c[x] = (Sigma(x[i] + x[i + 1]) * (x[i] * y[i + 1] - x[i + 1] * y[i]), for i = 0 to N - 1) / (6 * signedArea) |
144 | | - // c[y] = (Sigma(y[i] + y[i + 1]) * (x[i] * y[i + 1] - x[i + 1] * y[i]), for i = 0 to N - 1) / (6 * signedArea) |
145 | | - private static Point2D getPolygonSansHolesCentroid(Polygon polygon) |
146 | | - { |
147 | | - int pointCount = polygon.getPointCount(); |
148 | | - double xSum = 0; |
149 | | - double ySum = 0; |
150 | | - double signedArea = 0; |
151 | | - |
152 | | - Point2D current = new Point2D(); |
153 | | - Point2D next = new Point2D(); |
154 | | - for (int i = 0; i < pointCount; i++) { |
155 | | - polygon.getXY(i, current); |
156 | | - polygon.getXY((i + 1) % pointCount, next); |
157 | | - double ladder = current.x * next.y - next.x * current.y; |
158 | | - xSum += (current.x + next.x) * ladder; |
159 | | - ySum += (current.y + next.y) * ladder; |
160 | | - signedArea += ladder / 2; |
161 | | - } |
162 | | - return new Point2D(xSum / (signedArea * 6), ySum / (signedArea * 6)); |
163 | | - } |
| 28 | +public class OperatorCentroid2DLocal extends OperatorCentroid2D { |
| 29 | + @Override |
| 30 | + public Point2D execute(Geometry geometry, ProgressTracker progressTracker) { |
| 31 | + if (geometry.isEmpty()) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + Geometry.Type geometryType = geometry.getType(); |
| 36 | + switch (geometryType) { |
| 37 | + case Point: |
| 38 | + return ((Point) geometry).getXY(); |
| 39 | + case Line: |
| 40 | + return computeLineCentroid((Line) geometry); |
| 41 | + case Envelope: |
| 42 | + return ((Envelope) geometry).getCenterXY(); |
| 43 | + case MultiPoint: |
| 44 | + return computePointsCentroid((MultiPoint) geometry); |
| 45 | + case Polyline: |
| 46 | + return computePolylineCentroid(((Polyline) geometry)); |
| 47 | + case Polygon: |
| 48 | + return computePolygonCentroid((Polygon) geometry); |
| 49 | + default: |
| 50 | + throw new UnsupportedOperationException("Unexpected geometry type: " + geometryType); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static Point2D computeLineCentroid(Line line) { |
| 55 | + return new Point2D((line.getEndX() - line.getStartX()) / 2, (line.getEndY() - line.getStartY()) / 2); |
| 56 | + } |
| 57 | + |
| 58 | + // Points centroid is arithmetic mean of the input points |
| 59 | + private static Point2D computePointsCentroid(MultiPoint multiPoint) { |
| 60 | + double xSum = 0; |
| 61 | + double ySum = 0; |
| 62 | + int pointCount = multiPoint.getPointCount(); |
| 63 | + Point2D point2D = new Point2D(); |
| 64 | + for (int i = 0; i < pointCount; i++) { |
| 65 | + multiPoint.getXY(i, point2D); |
| 66 | + xSum += point2D.x; |
| 67 | + ySum += point2D.y; |
| 68 | + } |
| 69 | + return new Point2D(xSum / pointCount, ySum / pointCount); |
| 70 | + } |
| 71 | + |
| 72 | + // Lines centroid is weighted mean of each line segment, weight in terms of line |
| 73 | + // length |
| 74 | + private static Point2D computePolylineCentroid(Polyline polyline) { |
| 75 | + double xSum = 0; |
| 76 | + double ySum = 0; |
| 77 | + double weightSum = 0; |
| 78 | + |
| 79 | + Point2D startPoint = new Point2D(); |
| 80 | + Point2D endPoint = new Point2D(); |
| 81 | + for (int i = 0; i < polyline.getPathCount(); i++) { |
| 82 | + polyline.getXY(polyline.getPathStart(i), startPoint); |
| 83 | + polyline.getXY(polyline.getPathEnd(i) - 1, endPoint); |
| 84 | + double dx = endPoint.x - startPoint.x; |
| 85 | + double dy = endPoint.y - startPoint.y; |
| 86 | + double length = sqrt(dx * dx + dy * dy); |
| 87 | + weightSum += length; |
| 88 | + xSum += (startPoint.x + endPoint.x) * length / 2; |
| 89 | + ySum += (startPoint.y + endPoint.y) * length / 2; |
| 90 | + } |
| 91 | + return new Point2D(xSum / weightSum, ySum / weightSum); |
| 92 | + } |
| 93 | + |
| 94 | + // Polygon centroid: area weighted average of centroids in case of holes |
| 95 | + private static Point2D computePolygonCentroid(Polygon polygon) { |
| 96 | + int pathCount = polygon.getPathCount(); |
| 97 | + |
| 98 | + if (pathCount == 1) { |
| 99 | + return getPolygonSansHolesCentroid(polygon); |
| 100 | + } |
| 101 | + |
| 102 | + double xSum = 0; |
| 103 | + double ySum = 0; |
| 104 | + double areaSum = 0; |
| 105 | + |
| 106 | + for (int i = 0; i < pathCount; i++) { |
| 107 | + int startIndex = polygon.getPathStart(i); |
| 108 | + int endIndex = polygon.getPathEnd(i); |
| 109 | + |
| 110 | + Polygon sansHoles = getSubPolygon(polygon, startIndex, endIndex); |
| 111 | + |
| 112 | + Point2D centroid = getPolygonSansHolesCentroid(sansHoles); |
| 113 | + double area = sansHoles.calculateArea2D(); |
| 114 | + |
| 115 | + xSum += centroid.x * area; |
| 116 | + ySum += centroid.y * area; |
| 117 | + areaSum += area; |
| 118 | + } |
| 119 | + |
| 120 | + return new Point2D(xSum / areaSum, ySum / areaSum); |
| 121 | + } |
| 122 | + |
| 123 | + private static Polygon getSubPolygon(Polygon polygon, int startIndex, int endIndex) { |
| 124 | + Polyline boundary = new Polyline(); |
| 125 | + boundary.startPath(polygon.getPoint(startIndex)); |
| 126 | + for (int i = startIndex + 1; i < endIndex; i++) { |
| 127 | + Point current = polygon.getPoint(i); |
| 128 | + boundary.lineTo(current); |
| 129 | + } |
| 130 | + |
| 131 | + final Polygon newPolygon = new Polygon(); |
| 132 | + newPolygon.add(boundary, false); |
| 133 | + return newPolygon; |
| 134 | + } |
| 135 | + |
| 136 | + // Polygon sans holes centroid: |
| 137 | + // c[x] = (Sigma(x[i] + x[i + 1]) * (x[i] * y[i + 1] - x[i + 1] * y[i]), for i = |
| 138 | + // 0 to N - 1) / (6 * signedArea) |
| 139 | + // c[y] = (Sigma(y[i] + y[i + 1]) * (x[i] * y[i + 1] - x[i + 1] * y[i]), for i = |
| 140 | + // 0 to N - 1) / (6 * signedArea) |
| 141 | + private static Point2D getPolygonSansHolesCentroid(Polygon polygon) { |
| 142 | + int pointCount = polygon.getPointCount(); |
| 143 | + double xSum = 0; |
| 144 | + double ySum = 0; |
| 145 | + double signedArea = 0; |
| 146 | + |
| 147 | + Point2D current = new Point2D(); |
| 148 | + Point2D next = new Point2D(); |
| 149 | + for (int i = 0; i < pointCount; i++) { |
| 150 | + polygon.getXY(i, current); |
| 151 | + polygon.getXY((i + 1) % pointCount, next); |
| 152 | + double ladder = current.x * next.y - next.x * current.y; |
| 153 | + xSum += (current.x + next.x) * ladder; |
| 154 | + ySum += (current.y + next.y) * ladder; |
| 155 | + signedArea += ladder / 2; |
| 156 | + } |
| 157 | + return new Point2D(xSum / (signedArea * 6), ySum / (signedArea * 6)); |
| 158 | + } |
164 | 159 | } |
0 commit comments