forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangleGeometrySpec.js
271 lines (237 loc) · 11.2 KB
/
RectangleGeometrySpec.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
259
260
261
262
263
264
265
266
267
268
269
270
271
/*global defineSuite*/
defineSuite([
'Core/RectangleGeometry',
'Core/Cartesian3',
'Core/Ellipsoid',
'Core/GeographicProjection',
'Core/Math',
'Core/Matrix2',
'Core/Rectangle',
'Core/VertexFormat'
], function(
RectangleGeometry,
Cartesian3,
Ellipsoid,
GeographicProjection,
CesiumMath,
Matrix2,
Rectangle,
VertexFormat) {
"use strict";
/*global jasmine,describe,xdescribe,it,xit,expect,beforeEach,afterEach,beforeAll,afterAll,spyOn,runs,waits,waitsFor*/
it('computes positions', function() {
var rectangle = new Rectangle(-2.0, -1.0, 0.0, 1.0);
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITION_ONLY,
rectangle : rectangle,
granularity : 1.0
}));
var positions = m.attributes.position.values;
var length = positions.length;
expect(positions.length).toEqual(9 * 3);
expect(m.indices.length).toEqual(8 * 3);
var expectedNWCorner = Ellipsoid.WGS84.cartographicToCartesian(Rectangle.getNorthwest(rectangle));
var expectedSECorner = Ellipsoid.WGS84.cartographicToCartesian(Rectangle.getSoutheast(rectangle));
expect(new Cartesian3(positions[0], positions[1], positions[2])).toEqualEpsilon(expectedNWCorner, CesiumMath.EPSILON9);
expect(new Cartesian3(positions[length - 3], positions[length - 2], positions[length - 1])).toEqualEpsilon(expectedSECorner, CesiumMath.EPSILON9);
});
it('computes all attributes', function() {
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.ALL,
rectangle : new Rectangle(-2.0, -1.0, 0.0, 1.0),
granularity : 1.0
}));
expect(m.attributes.position.values.length).toEqual(9 * 3);
expect(m.attributes.st.values.length).toEqual(9 * 2);
expect(m.attributes.normal.values.length).toEqual(9 * 3);
expect(m.attributes.tangent.values.length).toEqual(9 * 3);
expect(m.attributes.binormal.values.length).toEqual(9 * 3);
expect(m.indices.length).toEqual(8 * 3);
});
it('compute positions with rotation', function() {
var rectangle = new Rectangle(-1, -1, 1, 1);
var angle = CesiumMath.PI_OVER_TWO;
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITIONS_ONLY,
rectangle : rectangle,
rotation : angle,
granularity : 1.0
}));
var positions = m.attributes.position.values;
var length = positions.length;
expect(length).toEqual(9 * 3);
expect(m.indices.length).toEqual(8 * 3);
var unrotatedSECorner = Rectangle.getSoutheast(rectangle);
var projection = new GeographicProjection();
var projectedSECorner = projection.project(unrotatedSECorner);
var rotation = Matrix2.fromRotation(angle);
var rotatedSECornerCartographic = projection.unproject(Matrix2.multiplyByVector(rotation, projectedSECorner));
var rotatedSECorner = Ellipsoid.WGS84.cartographicToCartesian(rotatedSECornerCartographic);
var actual = new Cartesian3(positions[length - 3], positions[length - 2], positions[length - 1]);
expect(actual).toEqualEpsilon(rotatedSECorner, CesiumMath.EPSILON6);
});
it('compute vertices with PI rotation', function() {
var rectangle = new Rectangle(-1, -1, 1, 1);
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
rectangle : rectangle,
rotation : CesiumMath.PI,
granularity : 1.0
}));
var positions = m.attributes.position.values;
var length = positions.length;
expect(length).toEqual(9 * 3);
expect(m.indices.length).toEqual(8 * 3);
var unrotatedNWCorner = Ellipsoid.WGS84.cartographicToCartesian(Rectangle.getNorthwest(rectangle));
var unrotatedSECorner = Ellipsoid.WGS84.cartographicToCartesian(Rectangle.getSoutheast(rectangle));
var actual = new Cartesian3(positions[0], positions[1], positions[2]);
expect(actual).toEqualEpsilon(unrotatedSECorner, CesiumMath.EPSILON8);
actual = new Cartesian3(positions[length - 3], positions[length - 2], positions[length - 1]);
expect(actual).toEqualEpsilon(unrotatedNWCorner, CesiumMath.EPSILON8);
});
it('compute texture coordinates with rotation', function() {
var rectangle = new Rectangle(-1, -1, 1, 1);
var angle = CesiumMath.PI_OVER_TWO;
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITION_AND_ST,
rectangle : rectangle,
stRotation : angle,
granularity : 1.0
}));
var positions = m.attributes.position.values;
var st = m.attributes.st.values;
var length = st.length;
expect(positions.length).toEqual(9 * 3);
expect(length).toEqual(9 * 2);
expect(m.indices.length).toEqual(8 * 3);
expect(st[length - 2]).toEqualEpsilon(0.0, CesiumMath.EPSILON14);
expect(st[length - 1]).toEqualEpsilon(0.0, CesiumMath.EPSILON14);
});
it('throws without rectangle', function() {
expect(function() {
return new RectangleGeometry({});
}).toThrowDeveloperError();
});
it('throws if rotated rectangle is invalid', function() {
expect(function() {
return RectangleGeometry.createGeometry(new RectangleGeometry({
rectangle : new Rectangle(-CesiumMath.PI_OVER_TWO, 1, CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO),
rotation : CesiumMath.PI_OVER_TWO
}));
}).toThrowDeveloperError();
});
it('throws if east is less than west', function() {
expect(function() {
return new RectangleGeometry({
rectangle : new Rectangle(CesiumMath.PI_OVER_TWO, -CesiumMath.PI_OVER_TWO, -CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO)
});
}).toThrowDeveloperError();
});
it('throws if north is less than south', function() {
expect(function() {
return new RectangleGeometry({
rectangle : new Rectangle(-CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO, -CesiumMath.PI_OVER_TWO)
});
}).toThrowDeveloperError();
});
it('computes positions extruded', function() {
var rectangle = new Rectangle(-2.0, -1.0, 0.0, 1.0);
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITION_ONLY,
rectangle : rectangle,
granularity : 1.0,
extrudedHeight : 2
}));
var positions = m.attributes.position.values;
expect(positions.length).toEqual((9 + 8 + 4) * 3 * 2);
expect(m.indices.length).toEqual((8 * 2 + 4 * 4) * 3);
});
it('computes all attributes extruded', function() {
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.ALL,
rectangle : new Rectangle(-2.0, -1.0, 0.0, 1.0),
granularity : 1.0,
extrudedHeight : 2
}));
expect(m.attributes.position.values.length).toEqual((9 + 8 + 4) * 3 * 2);
expect(m.attributes.st.values.length).toEqual((9 + 8 + 4) * 2 * 2);
expect(m.attributes.normal.values.length).toEqual((9 + 8 + 4) * 3 * 2);
expect(m.attributes.tangent.values.length).toEqual((9 + 8 + 4) * 3 * 2);
expect(m.attributes.binormal.values.length).toEqual((9 + 8 + 4) * 3 * 2);
expect(m.indices.length).toEqual((8 * 2 + 4 * 4) * 3);
});
it('compute positions with rotation extruded', function() {
var rectangle = new Rectangle(-1, -1, 1, 1);
var angle = CesiumMath.PI_OVER_TWO;
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITIONS_ONLY,
rectangle : rectangle,
rotation : angle,
granularity : 1.0,
extrudedHeight : 2
}));
var positions = m.attributes.position.values;
var length = positions.length;
expect(length).toEqual((9 + 8 + 4) * 3 * 2);
expect(m.indices.length).toEqual((8 * 2 + 4 * 4) * 3);
var unrotatedSECorner = Rectangle.getSoutheast(rectangle);
var projection = new GeographicProjection();
var projectedSECorner = projection.project(unrotatedSECorner);
var rotation = Matrix2.fromRotation(angle);
var rotatedSECornerCartographic = projection.unproject(Matrix2.multiplyByVector(rotation, projectedSECorner));
var rotatedSECorner = Ellipsoid.WGS84.cartographicToCartesian(rotatedSECornerCartographic);
var actual = new Cartesian3(positions[length - 21], positions[length - 20], positions[length - 19]);
expect(actual).toEqualEpsilon(rotatedSECorner, CesiumMath.EPSILON6);
});
it('computes extruded top open', function() {
var rectangle = new Rectangle(-2.0, -1.0, 0.0, 1.0);
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITION_ONLY,
rectangle : rectangle,
granularity : 1.0,
extrudedHeight : 2,
closeTop : false
}));
var positions = m.attributes.position.values;
expect(positions.length).toEqual((((8 + 4) * 2) + 9) * 3);
expect(m.indices.length).toEqual((8 + 4 * 4) * 3);
});
it('computes extruded bottom open', function() {
var rectangle = new Rectangle(-2.0, -1.0, 0.0, 1.0);
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITION_ONLY,
rectangle : rectangle,
granularity : 1.0,
extrudedHeight : 2,
closeBottom : false
}));
var positions = m.attributes.position.values;
expect(positions.length).toEqual((((8 + 4) * 2) + 9) * 3);
expect(m.indices.length).toEqual((8 + 4 * 4) * 3);
});
it('computes extruded top and bottom open', function() {
var rectangle = new Rectangle(-2.0, -1.0, 0.0, 1.0);
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITION_ONLY,
rectangle : rectangle,
granularity : 1.0,
extrudedHeight : 2,
closeTop : false,
closeBottom : false
}));
var positions = m.attributes.position.values;
expect(positions.length).toEqual((8 + 4) * 2 * 3);
expect(m.indices.length).toEqual(4 * 3 * 4);
});
it('computes non-extruded rectangle if height is small', function() {
var rectangle = new Rectangle(-2.0, -1.0, 0.0, 1.0);
var m = RectangleGeometry.createGeometry(new RectangleGeometry({
vertexFormat : VertexFormat.POSITION_ONLY,
rectangle : rectangle,
granularity : 1.0,
extrudedHeight : 0.1
}));
var positions = m.attributes.position.values;
expect(positions.length).toEqual(9 * 3);
expect(m.indices.length).toEqual(8 * 3);
});
});