-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathBoundingVolumeValidator.ts
364 lines (343 loc) · 9.66 KB
/
BoundingVolumeValidator.ts
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { defined } from "../base/defined";
import { ValidationContext } from "./ValidationContext";
import { BasicValidator } from "./BasicValidator";
import { RootPropertyValidator } from "./RootPropertyValidator";
import { ExtendedObjectsValidators } from "./ExtendedObjectsValidators";
import { BoundingVolume } from "../structure/BoundingVolume";
import { JsonValidationIssues } from "../issues/JsonValidationIssues";
import { SemanticValidationIssues } from "../issues/SemanticValidationIssues";
/**
* A class for validations related to `boundingVolume` objects.
*
* @private
*/
export class BoundingVolumeValidator {
/**
* Performs the validation to ensure that the given object is a
* valid `boundingVolume` object.
*
* @param boundingVolumePath The path that indicates the location of
* the given object, to be used in the validation issue message.
* @param boundingVolume The object to validate
* @param context The `ValidationContext` that any issues will be added to
* @returns Whether the given object is valid
*/
static async validateBoundingVolume(
boundingVolumePath: string,
boundingVolume: BoundingVolume,
context: ValidationContext
): Promise<boolean> {
// Make sure that the given value is an object
if (
!BasicValidator.validateObject(
boundingVolumePath,
"boundingVolume",
boundingVolume,
context
)
) {
return false;
}
let result = true;
// Validate the object as a RootProperty
if (
!RootPropertyValidator.validateRootProperty(
boundingVolumePath,
"boundingVolume",
boundingVolume,
context
)
) {
result = false;
}
// Perform the validation of the object in view of the
// extensions that it may contain
if (
!ExtendedObjectsValidators.validateExtendedObject(
boundingVolumePath,
boundingVolume,
context
)
) {
result = false;
}
// If there was an extension validator that overrides the
// default validation, then skip the remaining validation.
if (ExtendedObjectsValidators.hasOverride(boundingVolume)) {
return result;
}
if (
!BoundingVolumeValidator.validateBoundingVolumeInternal(
boundingVolumePath,
boundingVolume,
context
)
) {
result = false;
}
return result;
}
/**
* Implementation for validateBoundingVolume
*
* @param boundingVolumePath The path that indicates the location of
* the given object, to be used in the validation issue message.
* @param boundingVolume The object to validate
* @param context The `ValidationContext` that any issues will be added to
* @returns Whether the given object is valid
*/
private static validateBoundingVolumeInternal(
boundingVolumePath: string,
boundingVolume: BoundingVolume,
context: ValidationContext
): boolean {
// Make sure that the given value is an object
if (
!BasicValidator.validateObject(
boundingVolumePath,
"boundingVolume",
boundingVolume,
context
)
) {
return false;
}
let result = true;
const box = boundingVolume.box;
const region = boundingVolume.region;
const sphere = boundingVolume.sphere;
// The bounding volume MUST contain one of these properties
if (!defined(box) && !defined(region) && !defined(sphere)) {
const path = boundingVolumePath;
const issue = JsonValidationIssues.ANY_OF_ERROR(
path,
"boundingVolume",
"box",
"region",
"sphere"
);
context.addIssue(issue);
result = false;
}
// Validate the box
const boxPath = boundingVolumePath + "/box";
if (defined(box)) {
if (
!BoundingVolumeValidator.validateBoundingBox(boxPath, box!, context)
) {
result = false;
}
}
// Validate the region
const regionPath = boundingVolumePath + "/region";
if (defined(region)) {
if (
!BoundingVolumeValidator.validateBoundingRegion(
regionPath,
region!,
context
)
) {
result = false;
}
}
// Validate the sphere
const spherePath = boundingVolumePath + "/sphere";
if (defined(sphere)) {
if (
!BoundingVolumeValidator.validateBoundingSphere(
spherePath,
sphere!,
context
)
) {
result = false;
}
}
return result;
}
/**
* Perform a validation of the given `boundingVolume.box` array.
*
* @param path The path for the validation issues
* @param box The box array
* @param context The `ValidationContext`
* @returns Whether the object was valid
*/
static validateBoundingBox(
path: string,
box: number[],
context: ValidationContext
): boolean {
// The box MUST be an array with length 12
// Each element of the box MUST be a number
const expectedLength = 12;
const expectedElementType = "number";
if (
!BasicValidator.validateArray(
path,
"box",
box,
expectedLength,
expectedLength,
expectedElementType,
context
)
) {
return false;
}
return true;
}
/**
* Perform a validation of the given `boundingVolume.sphere` array.
*
* @param path The path for the validation issues
* @param sphere The sphere array
* @param context The `ValidationContext`
* @returns Whether the object was valid
*/
static validateBoundingSphere(
path: string,
sphere: number[],
context: ValidationContext
): boolean {
// The sphere MUST be an array with length 4
// Each element of the sphere MUST be a number
const expectedLength = 4;
const expectedElementType = "number";
if (
!BasicValidator.validateArray(
path,
"sphere",
sphere,
expectedLength,
expectedLength,
expectedElementType,
context
)
) {
return false;
}
// The radius MUST NOT be negative
const radius = sphere![3];
if (radius < 0.0) {
const message =
`The 'radius' entry of the bounding sphere ` +
`may not be negative, but is ${radius}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path + "/3",
message
);
context.addIssue(issue);
return false;
}
return true;
}
/**
* Perform a validation of the given `boundingVolume.region` array.
*
* @param path The path for the validation issues
* @param region The region array
* @param context The `ValidationContext`
* @returns Whether the object was valid
*/
static validateBoundingRegion(
path: string,
region: number[],
context: ValidationContext
): boolean {
// The region MUST be an array with length 6
// Each element of the region MUST be a number
const expectedLength = 6;
const expectedElementType = "number";
if (
!BasicValidator.validateArray(
path,
"region",
region,
expectedLength,
expectedLength,
expectedElementType,
context
)
) {
return false;
}
const westRad = region[0];
const southRad = region[1];
const eastRad = region[2];
const northRad = region[3];
const minimumHeight = region[4];
const maximumHeight = region[5];
let result = true;
if (westRad < -Math.PI || westRad > Math.PI) {
const message =
`The 'west' entry of the bounding region ` +
`must be in [-PI,PI], but is ${westRad}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path + "/0",
message
);
context.addIssue(issue);
result = false;
}
if (southRad < -Math.PI / 2 || southRad > Math.PI / 2) {
const message =
`The 'south' entry of the bounding region ` +
`must be in [-PI/2,PI/2], but is ${southRad}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path + "/1",
message
);
context.addIssue(issue);
result = false;
}
if (eastRad < -Math.PI || eastRad > Math.PI) {
const message =
`The 'east' entry of the bounding region ` +
`must be in [-PI,PI], but is ${eastRad}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path + "/2",
message
);
context.addIssue(issue);
result = false;
}
if (northRad < -Math.PI / 2 || northRad > Math.PI / 2) {
const message =
`The 'north' entry of the bounding region ` +
`must be in [-PI/2,PI/2], but is ${northRad}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path + "/3",
message
);
context.addIssue(issue);
result = false;
}
if (southRad > northRad) {
const message =
`The 'south' entry of the bounding region ` +
`may not be larger than the 'north' entry, but the south ` +
`is ${southRad} and the north is ${northRad}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path,
message
);
context.addIssue(issue);
result = false;
}
if (minimumHeight > maximumHeight) {
const message =
`The minimum height of the bounding region ` +
`may not be larger than the maximum height, but the minimum height ` +
`is ${minimumHeight} and the maximum height is ${maximumHeight}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path,
message
);
context.addIssue(issue);
result = false;
}
return result;
}
}