/
EllipseOutlineGeometry.js
258 lines (230 loc) · 10.4 KB
/
EllipseOutlineGeometry.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*global define*/
define([
'./defaultValue',
'./defined',
'./BoundingSphere',
'./Cartesian3',
'./ComponentDatatype',
'./IndexDatatype',
'./DeveloperError',
'./Ellipsoid',
'./EllipseGeometryLibrary',
'./Geometry',
'./GeometryAttribute',
'./GeometryAttributes',
'./Math',
'./PrimitiveType'
], function(
defaultValue,
defined,
BoundingSphere,
Cartesian3,
ComponentDatatype,
IndexDatatype,
DeveloperError,
Ellipsoid,
EllipseGeometryLibrary,
Geometry,
GeometryAttribute,
GeometryAttributes,
CesiumMath,
PrimitiveType) {
"use strict";
var scratchCartesian1 = new Cartesian3();
var boundingSphereCenter = new Cartesian3();
function computeEllipse(options) {
var center = options.center;
boundingSphereCenter = Cartesian3.multiplyByScalar(options.ellipsoid.geodeticSurfaceNormal(center, boundingSphereCenter), options.height, boundingSphereCenter);
boundingSphereCenter = Cartesian3.add(center, boundingSphereCenter, boundingSphereCenter);
var boundingSphere = new BoundingSphere(boundingSphereCenter, options.semiMajorAxis);
var positions = EllipseGeometryLibrary.computeEllipsePositions(options, false, true).outerPositions;
var attributes = new GeometryAttributes({
position: new GeometryAttribute({
componentDatatype : ComponentDatatype.DOUBLE,
componentsPerAttribute : 3,
values : EllipseGeometryLibrary.raisePositionsToHeight(positions, options, false)
})
});
var length = positions.length / 3;
var indices = IndexDatatype.createTypedArray(length, length * 2);
var index = 0;
for ( var i = 0; i < length - 1; i++) {
indices[index++] = i;
indices[index++] = i + 1;
}
indices[index++] = length - 1;
indices[index++] = 0;
return {
boundingSphere : boundingSphere,
attributes : attributes,
indices : indices
};
}
var topBoundingSphere = new BoundingSphere();
var bottomBoundingSphere = new BoundingSphere();
function computeExtrudedEllipse(options) {
var numberOfVerticalLines = defaultValue(options.numberOfVerticalLines, 16);
numberOfVerticalLines = Math.max(numberOfVerticalLines, 0);
var center = options.center;
var ellipsoid = options.ellipsoid;
var semiMajorAxis = options.semiMajorAxis;
var scaledNormal = Cartesian3.multiplyByScalar(ellipsoid.geodeticSurfaceNormal(center, scratchCartesian1), options.height, scratchCartesian1);
topBoundingSphere.center = Cartesian3.add(center, scaledNormal, topBoundingSphere.center);
topBoundingSphere.radius = semiMajorAxis;
scaledNormal = Cartesian3.multiplyByScalar(ellipsoid.geodeticSurfaceNormal(center, scaledNormal), options.extrudedHeight, scaledNormal);
bottomBoundingSphere.center = Cartesian3.add(center, scaledNormal, bottomBoundingSphere.center);
bottomBoundingSphere.radius = semiMajorAxis;
var positions = EllipseGeometryLibrary.computeEllipsePositions(options, false, true).outerPositions;
var attributes = new GeometryAttributes({
position: new GeometryAttribute({
componentDatatype : ComponentDatatype.DOUBLE,
componentsPerAttribute : 3,
values : EllipseGeometryLibrary.raisePositionsToHeight(positions, options, true)
})
});
positions = attributes.position.values;
var boundingSphere = BoundingSphere.union(topBoundingSphere, bottomBoundingSphere);
var length = positions.length/3;
var indices = IndexDatatype.createTypedArray(length, length * 2 + numberOfVerticalLines * 2);
length /= 2;
var index = 0;
for (var i = 0; i < length - 1; i++) {
indices[index++] = i;
indices[index++] = i + 1;
indices[index++] = i + length;
indices[index++] = i + length + 1;
}
indices[index++] = length - 1;
indices[index++] = 0;
indices[index++] = length + length - 1;
indices[index++] = length;
var numSide;
if (numberOfVerticalLines > 0) {
var numSideLines = Math.min(numberOfVerticalLines, length);
numSide = Math.round(length/numSideLines);
}
var maxI = Math.min(numSide * numberOfVerticalLines, length);
if (numberOfVerticalLines > 0) {
for (i = 0; i < maxI; i += numSide) {
indices[index++] = i;
indices[index++] = i + length;
}
}
return {
boundingSphere : boundingSphere,
attributes : attributes,
indices : indices
};
}
/**
*
* A description of the outline of an ellipse on an ellipsoid.
*
* @alias EllipseOutlineGeometry
* @constructor
*
* @param {Cartesian3} options.center The ellipse's center point in the fixed frame.
* @param {Number} options.semiMajorAxis The length of the ellipse's semi-major axis in meters.
* @param {Number} options.semiMinorAxis The length of the ellipse's semi-minor axis in meters.
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid the ellipse will be on.
* @param {Number} [options.height=0.0] The height above the ellipsoid.
* @param {Number} [options.extrudedHeight] The height of the extrusion.
* @param {Number} [options.rotation=0.0] The angle from north (clockwise) in radians. The default is zero.
* @param {Number} [options.granularity=0.02] The angular distance between points on the ellipse in radians.
* @param {Number} [options.numberOfVerticalLines = 16] Number of lines to draw between the top and bottom surface of an extruded ellipse.
*
* @exception {DeveloperError} center is required.
* @exception {DeveloperError} semiMajorAxis is required.
* @exception {DeveloperError} semiMinorAxis is required.
* @exception {DeveloperError} semiMajorAxis and semiMinorAxis must be greater than zero.
* @exception {DeveloperError} semiMajorAxis must be larger than the semiMajorAxis.
* @exception {DeveloperError} granularity must be greater than zero.
*
* @see EllipseOutlineGeometry#createGeometry
*
* @example
* var ellipsoid = Ellipsoid.WGS84;
* var ellipse = new EllipseOutlineGeometry({
* ellipsoid : ellipsoid,
* center : ellipsoid.cartographicToCartesian(Cartographic.fromDegrees(-75.59777, 40.03883)),
* semiMajorAxis : 500000.0,
* semiMinorAxis : 300000.0,
* rotation : CesiumMath.toRadians(60.0)
* });
* var geometry = EllipseOutlineGeometry.createGeometry(ellipse);
*/
var EllipseOutlineGeometry = function(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var center = options.center;
var semiMajorAxis = options.semiMajorAxis;
var semiMinorAxis = options.semiMinorAxis;
var granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
var height = defaultValue(options.height, 0.0);
var extrudedHeight = options.extrudedHeight;
var extrude = (defined(extrudedHeight) && !CesiumMath.equalsEpsilon(height, extrudedHeight, 1.0));
if (!defined(center)) {
throw new DeveloperError('center is required.');
}
if (!defined(semiMajorAxis)) {
throw new DeveloperError('semiMajorAxis is required.');
}
if (!defined(semiMinorAxis)) {
throw new DeveloperError('semiMinorAxis is required.');
}
if (semiMajorAxis <= 0.0 || semiMinorAxis <= 0.0) {
throw new DeveloperError('Semi-major and semi-minor axes must be greater than zero.');
}
if (semiMajorAxis < semiMinorAxis) {
throw new DeveloperError('semiMajorAxis must be larger than the semiMajorAxis.');
}
if (granularity <= 0.0) {
throw new DeveloperError('granularity must be greater than zero.');
}
this._center = Cartesian3.clone(center);
this._semiMajorAxis = semiMajorAxis;
this._semiMinorAxis = semiMinorAxis;
this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
this._rotation = defaultValue(options.rotation, 0.0);
this._height = height;
this._granularity = granularity;
this._extrudedHeight = extrudedHeight;
this._extrude = extrude;
this._numberOfVerticalLines = Math.max(defaultValue(options.numberOfVerticalLines, 16), 0);
this._workerName = 'createEllipseOutlineGeometry';
};
/**
* Computes the geometric representation of an outline of an ellipse on an ellipsoid, including its vertices, indices, and a bounding sphere.
* @memberof EllipseOutlineGeometry
*
* @param {EllipseOutlineGeometry} ellipseGeometry A description of the ellipse.
* @returns {Geometry} The computed vertices and indices.
*/
EllipseOutlineGeometry.createGeometry = function(ellipseGeometry) {
var options = {
center : ellipseGeometry._center,
semiMajorAxis : ellipseGeometry._semiMajorAxis,
semiMinorAxis : ellipseGeometry._semiMinorAxis,
ellipsoid : ellipseGeometry._ellipsoid,
rotation : ellipseGeometry._rotation,
height : ellipseGeometry._height,
extrudedHeight : ellipseGeometry._extrudedHeight,
granularity : ellipseGeometry._granularity,
numberOfVerticalLines : ellipseGeometry._numberOfVerticalLines
};
var geometry;
if (ellipseGeometry._extrude) {
options.extrudedHeight = Math.min(ellipseGeometry._extrudedHeight, ellipseGeometry._height);
options.height = Math.max(ellipseGeometry._extrudedHeight, ellipseGeometry._height);
geometry = computeExtrudedEllipse(options);
} else {
geometry = computeEllipse(options);
}
return new Geometry({
attributes : geometry.attributes,
indices : geometry.indices,
primitiveType : PrimitiveType.LINES,
boundingSphere : geometry.boundingSphere
});
};
return EllipseOutlineGeometry;
});