-
Notifications
You must be signed in to change notification settings - Fork 155
/
BoundingVolumeS2Validator.ts
237 lines (218 loc) · 6.49 KB
/
BoundingVolumeS2Validator.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
import { defined } from "../../base/defined";
import { Validator } from "../Validator";
import { ValidationContext } from "../ValidationContext";
import { BasicValidator } from "../BasicValidator";
import { RootPropertyValidator } from "../RootPropertyValidator";
import { SemanticValidationIssues } from "../../issues/SemanticValidationIssues";
import { BoundingVolumeS2ValidationIssues } from "./BoundingVolumeS2ValidationIssues";
import { BoundingVolumeValidator } from "../BoundingVolumeValidator";
import { ExtendedObjectsValidators } from "../ExtendedObjectsValidators";
/**
* A class for the validation of bounding volumes that contain
* `3DTILES_bounding_volume_S2` extension objects
*
* @private
*/
export class BoundingVolumeS2Validator implements Validator<any> {
/**
* Performs the validation of a `BoundungVolume` object that
* contains a `3DTILES_bounding_volume_S2` extension object.
*
* @param path The path for `ValidationIssue` instances
* @param boundingVolume The object to validate
* @param context The `ValidationContext` that any issues will be added to
* @returns Whether the object was valid
*/
async validateObject(
path: string,
boundingVolume: any,
context: ValidationContext
): Promise<boolean> {
// Make sure that the given value is an object
if (
!BasicValidator.validateObject(
path,
"boundingVolume",
boundingVolume,
context
)
) {
return false;
}
let result = true;
// Validate the box
const box = boundingVolume.box;
const boxPath = path + "/box";
if (defined(box)) {
if (
!BoundingVolumeValidator.validateBoundingBox(boxPath, box!, context)
) {
result = false;
}
}
// Validate the region
const region = boundingVolume.region;
const regionPath = path + "/region";
if (defined(region)) {
if (
!BoundingVolumeValidator.validateBoundingRegion(
regionPath,
region!,
context
)
) {
result = false;
}
}
// Validate the sphere
const sphere = boundingVolume.sphere;
const spherePath = path + "/sphere";
if (defined(sphere)) {
if (
!BoundingVolumeValidator.validateBoundingSphere(
spherePath,
sphere!,
context
)
) {
result = false;
}
}
// If there is a 3DTILES_bounding_volume_S2 extension,
// perform the corresponding object
const extensions = boundingVolume.extensions;
if (defined(extensions)) {
const key = "3DTILES_bounding_volume_S2";
const s2 = extensions[key];
const s2Path = path + "/" + key;
if (
!BoundingVolumeS2Validator.validateBoundingVolumeS2(s2Path, s2, context)
) {
result = false;
}
}
return result;
}
/**
* Performs the validation to ensure that the given object is a
* valid `3DTILES_bounding_volume_S2` object.
*
* @param path The path for `ValidationIssue` instances
* @param object The object to validate
* @param context The `ValidationContext` that any issues will be added to
* @returns Whether the object was valid
*/
static validateBoundingVolumeS2(
path: string,
object: any,
context: ValidationContext
): boolean {
// Make sure that the given value is an object
if (!BasicValidator.validateObject(path, "object", object, context)) {
return false;
}
let result = true;
// Validate the object as a RootProperty
if (
!RootPropertyValidator.validateRootProperty(
path,
"3DTILES_bounding_volume_S2",
object,
context
)
) {
result = false;
}
// Perform the validation of the object in view of the
// extensions that it may contain
if (
!ExtendedObjectsValidators.validateExtendedObject(path, object, context)
) {
result = false;
}
// If there was an extension validator that overrides the
// default validation, then skip the remaining validation.
if (ExtendedObjectsValidators.hasOverride(object)) {
return result;
}
// Validate the token
const token = object.token;
const tokenPath = path + "/token";
// The token MUST be defined
// The token MUST be a string
if (!BasicValidator.validateString(tokenPath, "token", token, context)) {
result = false;
} else {
// The token MUST be a valid S2 token
if (!BoundingVolumeS2Validator.isValidToken(token)) {
const message = `The S2 token '${token}' is not valid`;
const issue = BoundingVolumeS2ValidationIssues.S2_TOKEN_INVALID(
tokenPath,
message
);
context.addIssue(issue);
result = false;
}
}
// Validate the minimumHeight
const minimumHeight = object.minimumHeight;
const minimumHeightPath = path + "/minimumHeight";
// The minimumHeight MUST be a number
if (
!BasicValidator.validateNumber(
minimumHeightPath,
"minimumHeight",
minimumHeight,
context
)
) {
result = false;
}
// Validate the maximumHeight
const maximumHeight = object.maximumHeight;
const maximumHeightPath = path + "/maximumHeight";
// The maximumHeight MUST be a number
if (
!BasicValidator.validateNumber(
maximumHeightPath,
"maximumHeight",
maximumHeight,
context
)
) {
result = false;
}
// The minimumHeight MUST NOT be larger
// than the maximumHeight
if (defined(minimumHeight) && defined(maximumHeight)) {
if (minimumHeight > maximumHeight) {
const message =
`The minimumHeight may not be larger than the ` +
`maximumHeight, but the minimumHeight is ${minimumHeight} ` +
`and the maximum height is ${maximumHeight}`;
const issue = SemanticValidationIssues.BOUNDING_VOLUME_INCONSISTENT(
path,
message
);
context.addIssue(issue);
result = false;
}
}
return result;
}
/**
* Peforms a basic validation that the given string is a valid S2 cell token
*
* @param token The token
* @returns Whether the token is valid
*/
private static isValidToken(token: string): boolean {
// According to cesium/Source/Core/S2Cell.js
if (!/^[0-9a-fA-F]{1,16}$/.test(token)) {
return false;
}
// Further constraints could be added here (e.g. that
// the first digit is only a value in [0,5] ...)
return true;
}
}