/
EllipsoidOutlineGeometry.js
213 lines (187 loc) · 7.7 KB
/
EllipsoidOutlineGeometry.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
/*global define*/
define([
'./BoundingSphere',
'./Cartesian3',
'./ComponentDatatype',
'./defaultValue',
'./DeveloperError',
'./Ellipsoid',
'./Geometry',
'./GeometryAttribute',
'./GeometryAttributes',
'./IndexDatatype',
'./Math',
'./PrimitiveType'
], function(
BoundingSphere,
Cartesian3,
ComponentDatatype,
defaultValue,
DeveloperError,
Ellipsoid,
Geometry,
GeometryAttribute,
GeometryAttributes,
IndexDatatype,
CesiumMath,
PrimitiveType) {
"use strict";
var defaultRadii = new Cartesian3(1.0, 1.0, 1.0);
var cos = Math.cos;
var sin = Math.sin;
/**
* A description of the outline of an ellipsoid centered at the origin.
*
* @alias EllipsoidOutlineGeometry
* @constructor
*
* @param {Cartesian3} [options.radii=Cartesian3(1.0, 1.0, 1.0)] The radii of the ellipsoid in the x, y, and z directions.
* @param {Number} [options.stackPartitions=10] The count of stacks for the ellipsoid (1 greater than the number of parallel lines).
* @param {Number} [options.slicePartitions=8] The count of slices for the ellipsoid (Equal to the number of radial lines).
* @param {Number} [options.subdivisions=128] The number of points per line, determining the granularity of the curvature .
*
* @exception {DeveloperError} options.stackPartitions must be greater than or equal to one.
* @exception {DeveloperError} options.slicePartitions must be greater than or equal to zero.
* @exception {DeveloperError} options.subdivisions must be greater than or equal to zero.
*
* @example
* var ellipsoid = new Cesium.EllipsoidOutlineGeometry({
* radii : new Cesium.Cartesian3(1000000.0, 500000.0, 500000.0),
* stackPartitions: 6,
* slicePartitions: 5
* });
* var geometry = Cesium.EllipsoidOutlineGeometry.createGeometry(ellipsoid);
*/
var EllipsoidOutlineGeometry = function(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var radii = defaultValue(options.radii, defaultRadii);
var stackPartitions = defaultValue(options.stackPartitions, 10);
var slicePartitions = defaultValue(options.slicePartitions, 8);
var subdivisions = defaultValue(options.subdivisions, 128);
//>>includeStart('debug', pragmas.debug);
if (stackPartitions < 1) {
throw new DeveloperError('options.stackPartitions cannot be less than 1');
}
if (slicePartitions < 0) {
throw new DeveloperError('options.slicePartitions cannot be less than 0');
}
if (subdivisions < 0) {
throw new DeveloperError('options.subdivisions must be greater than or equal to zero.');
}
//>>includeEnd('debug');
this._radii = Cartesian3.clone(radii);
this._stackPartitions = stackPartitions;
this._slicePartitions = slicePartitions;
this._subdivisions = subdivisions;
this._workerName = 'createEllipsoidOutlineGeometry';
};
/**
* Computes the geometric representation of an outline of an ellipsoid, including its vertices, indices, and a bounding sphere.
* @memberof EllipsoidOutlineGeometry
*
* @param {EllipsoidOutlineGeometry} ellipsoidGeometry A description of the ellipsoid outline.
* @returns {Geometry} The computed vertices and indices.
*/
EllipsoidOutlineGeometry.createGeometry = function(ellipsoidGeometry) {
var radii = ellipsoidGeometry._radii;
var ellipsoid = Ellipsoid.fromCartesian3(radii);
var stackPartitions = ellipsoidGeometry._stackPartitions;
var slicePartitions = ellipsoidGeometry._slicePartitions;
var subdivisions = ellipsoidGeometry._subdivisions;
var indicesSize = subdivisions * (stackPartitions + slicePartitions - 1);
var positionSize = indicesSize - slicePartitions + 2;
var positions = new Float64Array(positionSize * 3);
var indices = IndexDatatype.createTypedArray(positionSize, indicesSize * 2);
var i;
var j;
var theta;
var phi;
var cosPhi;
var sinPhi;
var index = 0;
var cosTheta = new Array(subdivisions);
var sinTheta = new Array(subdivisions);
for (i = 0; i < subdivisions; i++) {
theta = CesiumMath.TWO_PI * i / subdivisions;
cosTheta[i] = cos(theta);
sinTheta[i] = sin(theta);
}
for (i = 1; i < stackPartitions; i++) {
phi = Math.PI * i / stackPartitions;
cosPhi = cos(phi);
sinPhi = sin(phi);
for (j = 0; j < subdivisions; j++) {
positions[index++] = radii.x * cosTheta[j] * sinPhi;
positions[index++] = radii.y * sinTheta[j] * sinPhi;
positions[index++] = radii.z * cosPhi;
}
}
cosTheta.length = slicePartitions;
sinTheta.length = slicePartitions;
for (i = 0; i < slicePartitions; i++) {
theta = CesiumMath.TWO_PI * i / slicePartitions;
cosTheta[i] = cos(theta);
sinTheta[i] = sin(theta);
}
positions[index++] = 0;
positions[index++] = 0;
positions[index++] = radii.z;
for (i = 1; i < subdivisions; i++) {
phi = Math.PI * i / subdivisions;
cosPhi = cos(phi);
sinPhi = sin(phi);
for (j = 0; j < slicePartitions; j++) {
positions[index++] = radii.x * cosTheta[j] * sinPhi;
positions[index++] = radii.y * sinTheta[j] * sinPhi;
positions[index++] = radii.z * cosPhi;
}
}
positions[index++] = 0;
positions[index++] = 0;
positions[index++] = -radii.z;
index = 0;
for (i = 0; i < stackPartitions - 1; ++i) {
var topRowOffset = (i * subdivisions);
for (j = 0; j < subdivisions - 1; ++j) {
indices[index++] = topRowOffset + j;
indices[index++] = topRowOffset + j + 1;
}
indices[index++] = topRowOffset + subdivisions - 1;
indices[index++] = topRowOffset;
}
var sliceOffset = subdivisions * (stackPartitions - 1);
for (j = 1; j < slicePartitions + 1; ++j) {
indices[index++] = sliceOffset;
indices[index++] = sliceOffset + j;
}
for (i = 0; i < subdivisions - 2; ++i) {
var topOffset = (i * slicePartitions) + 1 + sliceOffset;
var bottomOffset = ((i + 1) * slicePartitions) + 1 + sliceOffset;
for (j = 0; j < slicePartitions - 1; ++j) {
indices[index++] = bottomOffset + j;
indices[index++] = topOffset + j;
}
indices[index++] = bottomOffset + slicePartitions - 1;
indices[index++] = topOffset + slicePartitions - 1;
}
var lastPosition = positions.length / 3 - 1;
for (j = lastPosition - 1; j > lastPosition - slicePartitions - 1; --j) {
indices[index++] = lastPosition;
indices[index++] = j;
}
var attributes = new GeometryAttributes({
position: new GeometryAttribute({
componentDatatype : ComponentDatatype.DOUBLE,
componentsPerAttribute : 3,
values : positions
})
});
return new Geometry({
attributes : attributes,
indices : indices,
primitiveType : PrimitiveType.LINES,
boundingSphere : BoundingSphere.fromEllipsoid(ellipsoid)
});
};
return EllipsoidOutlineGeometry;
});