Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8e390b9

Browse files
authored
centroid fixes (#227)
1 parent8070e1e commit8e390b9

File tree

6 files changed

+238
-174
lines changed

6 files changed

+238
-174
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ Thumbs.db
3838
target/*
3939
/bin/
4040
/target/
41+
42+
.metadata/

‎.travis.yml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
language:java
22
jdk:
3-
-openjdk7
4-
-openjdk8
5-
-oraclejdk8
6-
-oraclejdk9
3+
-openjdk9
4+
-openjdk10
5+
-openjdk14
6+
# - oraclejdk9
7+
-oraclejdk11
78
notifications:
89
email:false

‎pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@
9494
<properties>
9595
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9696

97-
<java.source.version>1.6</java.source.version>
98-
<java.target.version>1.6</java.target.version>
97+
<java.source.version>1.7</java.source.version>
98+
<java.target.version>1.7</java.target.version>
9999

100100
<!-- dependency versions-->
101101
<jackson.version>2.9.6</jackson.version>
@@ -194,7 +194,7 @@
194194
<plugin>
195195
<groupId>org.sonatype.plugins</groupId>
196196
<artifactId>nexus-staging-maven-plugin</artifactId>
197-
<version>1.6.2</version>
197+
<version>1.6.8</version>
198198
<extensions>true</extensions>
199199
<configuration>
200200
<serverId>ossrh</serverId>
Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 1995-2017 Esri
2+
Copyright 1995-2019 Esri
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -23,8 +23,6 @@
2323
*/
2424
packagecom.esri.core.geometry;
2525

26-
importstaticjava.lang.Math.sqrt;
27-
2826
publicclassOperatorCentroid2DLocalextendsOperatorCentroid2D {
2927
@Override
3028
publicPoint2Dexecute(Geometrygeometry,ProgressTrackerprogressTracker) {
@@ -56,7 +54,7 @@ private static Point2D computeLineCentroid(Line line) {
5654
}
5755

5856
// Points centroid is arithmetic mean of the input points
59-
privatestaticPoint2DcomputePointsCentroid(MultiPointmultiPoint) {
57+
privatestaticPoint2DcomputePointsCentroid(MultiVertexGeometrymultiPoint) {
6058
doublexSum =0;
6159
doubleySum =0;
6260
intpointCount =multiPoint.getPointCount();
@@ -71,89 +69,75 @@ private static Point2D computePointsCentroid(MultiPoint multiPoint) {
7169

7270
// Lines centroid is weighted mean of each line segment, weight in terms of line
7371
// length
74-
privatestaticPoint2DcomputePolylineCentroid(Polylinepolyline) {
75-
doublexSum =0;
76-
doubleySum =0;
77-
doubleweightSum =0;
78-
79-
Point2DstartPoint =newPoint2D();
80-
Point2DendPoint =newPoint2D();
81-
for (inti =0;i <polyline.getPathCount();i++) {
82-
polyline.getXY(polyline.getPathStart(i),startPoint);
83-
polyline.getXY(polyline.getPathEnd(i) -1,endPoint);
84-
doubledx =endPoint.x -startPoint.x;
85-
doubledy =endPoint.y -startPoint.y;
86-
doublelength =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-
returnnewPoint2D(xSum /weightSum,ySum /weightSum);
92-
}
93-
94-
// Polygon centroid: area weighted average of centroids in case of holes
95-
privatestaticPoint2DcomputePolygonCentroid(Polygonpolygon) {
96-
intpathCount =polygon.getPathCount();
97-
98-
if (pathCount ==1) {
99-
returngetPolygonSansHolesCentroid(polygon);
72+
privatestaticPoint2DcomputePolylineCentroid(MultiPathpolyline) {
73+
doubletotalLength =polyline.calculateLength2D();
74+
if (totalLength ==0) {
75+
returncomputePointsCentroid(polyline);
10076
}
101-
102-
doublexSum =0;
103-
doubleySum =0;
104-
doubleareaSum =0;
105-
106-
for (inti =0;i <pathCount;i++) {
107-
intstartIndex =polygon.getPathStart(i);
108-
intendIndex =polygon.getPathEnd(i);
109-
110-
PolygonsansHoles =getSubPolygon(polygon,startIndex,endIndex);
111-
112-
Point2Dcentroid =getPolygonSansHolesCentroid(sansHoles);
113-
doublearea =sansHoles.calculateArea2D();
114-
115-
xSum +=centroid.x *area;
116-
ySum +=centroid.y *area;
117-
areaSum +=area;
77+
78+
MathUtils.KahanSummatorxSum =newMathUtils.KahanSummator(0);
79+
MathUtils.KahanSummatorySum =newMathUtils.KahanSummator(0);
80+
Point2Dpoint =newPoint2D();
81+
SegmentIteratoriter =polyline.querySegmentIterator();
82+
while (iter.nextPath()) {
83+
while (iter.hasNextSegment()) {
84+
Segmentseg =iter.nextSegment();
85+
seg.getCoord2D(0.5,point);
86+
doublelength =seg.calculateLength2D();
87+
point.scale(length);
88+
xSum.add(point.x);
89+
ySum.add(point.y);
90+
}
11891
}
119-
120-
returnnewPoint2D(xSum /areaSum,ySum /areaSum);
92+
93+
returnnewPoint2D(xSum.getResult() /totalLength,ySum.getResult() /totalLength);
12194
}
12295

123-
privatestaticPolygongetSubPolygon(Polygonpolygon,intstartIndex,intendIndex) {
124-
Polylineboundary =newPolyline();
125-
boundary.startPath(polygon.getPoint(startIndex));
126-
for (inti =startIndex +1;i <endIndex;i++) {
127-
Pointcurrent =polygon.getPoint(i);
128-
boundary.lineTo(current);
96+
//Polygoncentroid: area weighted average of centroids
97+
privatestaticPoint2DcomputePolygonCentroid(Polygonpolygon) {
98+
doubletotalArea =polygon.calculateArea2D();
99+
if (totalArea ==0)
100+
{
101+
returncomputePolylineCentroid(polygon);
129102
}
130-
131-
finalPolygonnewPolygon =newPolygon();
132-
newPolygon.add(boundary,false);
133-
returnnewPolygon;
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-
privatestaticPoint2DgetPolygonSansHolesCentroid(Polygonpolygon) {
142-
intpointCount =polygon.getPointCount();
143-
doublexSum =0;
144-
doubleySum =0;
145-
doublesignedArea =0;
146-
103+
104+
MathUtils.KahanSummatorxSum =newMathUtils.KahanSummator(0);
105+
MathUtils.KahanSummatorySum =newMathUtils.KahanSummator(0);
106+
Point2DstartPoint =newPoint2D();
147107
Point2Dcurrent =newPoint2D();
148108
Point2Dnext =newPoint2D();
149-
for (inti =0;i <pointCount;i++) {
150-
polygon.getXY(i,current);
151-
polygon.getXY((i +1) %pointCount,next);
152-
doubleladder =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;
109+
Point2Dorigin =polygon.getXY(0);
110+
111+
for (intipath =0,npaths =polygon.getPathCount();ipath <npaths;ipath++) {
112+
intstartIndex =polygon.getPathStart(ipath);
113+
intendIndex =polygon.getPathEnd(ipath);
114+
intpointCount =endIndex -startIndex;
115+
if (pointCount <3) {
116+
continue;
117+
}
118+
119+
polygon.getXY(startIndex,startPoint);
120+
polygon.getXY(startIndex +1,current);
121+
current.sub(startPoint);
122+
for (inti =startIndex +2,n =endIndex;i <n;i++) {
123+
polygon.getXY(i,next);
124+
next.sub(startPoint);
125+
doubletwiceTriangleArea =next.x *current.y -current.x *next.y;
126+
xSum.add((current.x +next.x) *twiceTriangleArea);
127+
ySum.add((current.y +next.y) *twiceTriangleArea);
128+
current.setCoords(next);
129+
}
130+
131+
startPoint.sub(origin);
132+
startPoint.scale(6.0 *polygon.calculateRingArea2D(ipath));
133+
//add weighted startPoint
134+
xSum.add(startPoint.x);
135+
ySum.add(startPoint.y);
156136
}
157-
returnnewPoint2D(xSum / (signedArea *6),ySum / (signedArea *6));
137+
138+
totalArea *=6.0;
139+
Point2Dres =newPoint2D(xSum.getResult() /totalArea,ySum.getResult() /totalArea);
140+
res.add(origin);
141+
returnres;
158142
}
159143
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp