forked from Esri/geometry-api-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundary.java
More file actions
180 lines (156 loc) · 5.17 KB
/
Boundary.java
File metadata and controls
180 lines (156 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright 1995-2013 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373
email: contracts@esri.com
*/
package com.esri.core.geometry;
class Boundary {
static Geometry calculate(Geometry geom, ProgressTracker progress_tracker) {
int gt = geom.getType().value();
if (gt == Geometry.GeometryType.Polygon) {
Polyline dst = new Polyline(geom.getDescription());
if (!geom.isEmpty()) {
((MultiPathImpl)geom._getImpl())._copyToUnsafe((MultiPathImpl)dst._getImpl());
}
return dst;
} else if (gt == Geometry.GeometryType.Polyline) {
return calculatePolylineBoundary_(geom._getImpl(), progress_tracker);
} else if (gt == Geometry.GeometryType.Envelope) {
Polyline dst = new Polyline(geom.getDescription());
if (!geom.isEmpty())
dst.addEnvelope((Envelope) geom, false);
return dst;
} else if (Geometry.isSegment(gt)) {
MultiPoint mp = new MultiPoint(geom.getDescription());
if (!geom.isEmpty() && !((Segment) geom).isClosed()) {
Point pt = new Point();
((Segment) geom).queryStart(pt);
mp.add(pt);
((Segment) geom).queryEnd(pt);
mp.add(pt);
}
return mp;
} else if (Geometry.isPoint(gt)) {
// returns empty point for points and multipoints.
return null;
}
throw new IllegalArgumentException();
}
private static final class MultiPathImplBoundarySorter extends ClassicSort {
AttributeStreamOfDbl m_xy;
static final class CompareIndices extends
AttributeStreamOfInt32.IntComparator {
AttributeStreamOfDbl m_xy;
Point2D pt1_helper;
Point2D pt2_helper;
CompareIndices(AttributeStreamOfDbl xy) {
m_xy = xy;
pt1_helper = new Point2D();
pt2_helper = new Point2D();
}
@Override
public int compare(int v1, int v2) {
m_xy.read(2 * v1, pt1_helper);
m_xy.read(2 * v2, pt2_helper);
return pt1_helper.compare(pt2_helper);
}
}
MultiPathImplBoundarySorter(AttributeStreamOfDbl xy) {
m_xy = xy;
}
@Override
public void userSort(int begin, int end, AttributeStreamOfInt32 indices) {
indices.Sort(begin, end, new CompareIndices(m_xy));
}
@Override
public double getValue(int index) {
return m_xy.read(2 * index + 1);
}
}
static MultiPoint calculatePolylineBoundary_(Object impl,
ProgressTracker progress_tracker) {
MultiPathImpl mpImpl = (MultiPathImpl) impl;
MultiPoint dst = new MultiPoint(mpImpl.getDescription());
if (!mpImpl.isEmpty()) {
AttributeStreamOfInt32 indices = new AttributeStreamOfInt32(0);
indices.reserve(mpImpl.getPathCount() * 2);
for (int ipath = 0, nPathCount = mpImpl.getPathCount(); ipath < nPathCount; ipath++) {
int path_size = mpImpl.getPathSize(ipath);
if (path_size > 0 && !mpImpl.isClosedPathInXYPlane(ipath))// closed
// paths
// of
// polyline
// do
// not
// contribute
// to
// the
// boundary.
{
int start = mpImpl.getPathStart(ipath);
indices.add(start);
int end = mpImpl.getPathEnd(ipath) - 1;
indices.add(end);
}
}
if (indices.size() > 0) {
BucketSort sorter = new BucketSort();
AttributeStreamOfDbl xy = (AttributeStreamOfDbl) (mpImpl
.getAttributeStreamRef(VertexDescription.Semantics.POSITION));
sorter.sort(indices, 0, indices.size(),
new MultiPathImplBoundarySorter(xy));
Point2D ptPrev = new Point2D();
xy.read(2 * indices.get(0), ptPrev);
int ind = 0;
int counter = 1;
Point point = new Point();
Point2D pt = new Point2D();
for (int i = 1, n = indices.size(); i < n; i++) {
xy.read(2 * indices.get(i), pt);
if (pt.isEqual(ptPrev)) {
if (indices.get(ind) > indices.get(i)) {
// remove duplicate point
indices.set(ind, NumberUtils.intMax());
ind = i;// just for the heck of it, have the first
// point in the order to be added to the
// boundary.
} else
indices.set(i, NumberUtils.intMax());
counter++;
} else {
if ((counter & 1) == 0) {// remove boundary point
indices.set(ind, NumberUtils.intMax());
}
ptPrev.setCoords(pt);
ind = i;
counter = 1;
}
}
if ((counter & 1) == 0) {// remove the point
indices.set(ind, NumberUtils.intMax());
}
indices.sort(0, indices.size());
for (int i = 0, n = indices.size(); i < n; i++) {
if (indices.get(i) == NumberUtils.intMax())
break;
mpImpl.getPointByVal(indices.get(i), point);
dst.add(point);
}
}
}
return dst;
}
}