-
Notifications
You must be signed in to change notification settings - Fork 10
/
validate.go
462 lines (431 loc) · 12.6 KB
/
validate.go
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// © Copyright 2021 HP Development Company, L.P.
// SPDX-License Identifier: BSD-2-Clause
package go3mf
import (
"encoding/xml"
"image/color"
"sort"
"strings"
"sync"
"github.com/hpinc/go3mf/errors"
"github.com/hpinc/go3mf/spec"
)
func (m *Model) sortedChilds() []string {
if len(m.Childs) == 0 {
return nil
}
s := make([]string, 0, len(m.Childs))
for path := range m.Childs {
s = append(s, path)
}
sort.Strings(s)
return s
}
// Validate checks that the model is conformant with the 3MF specs.
func (m *Model) Validate() error {
var errs error
errs = errors.Append(errs, validateRelationship(m, m.RootRelationships, ""))
errs = errors.Append(errs, m.validateNamespaces())
rootPath := m.PathOrDefault()
sortedChilds := m.sortedChilds()
for _, path := range sortedChilds {
c := m.Childs[path]
if path == rootPath {
errs = errors.Append(errs, errors.ErrOPCDuplicatedModelName)
} else {
errs = errors.Append(errs, validateRelationship(m, c.Relationships, path))
}
}
errs = errors.Append(errs, validateRelationship(m, m.Relationships, rootPath))
errs = errors.Append(errs, checkMetadadata(m, m.Metadata))
for _, ext := range m.Extensions {
if ext, ok := spec.LoadValidator(ext.Namespace); ok {
errs = errors.Append(errs, ext.Validate(m, m.Path, m))
}
}
for _, path := range sortedChilds {
c := m.Childs[path]
err := c.Resources.validate(m, path)
if err != nil {
errs = errors.Append(errs, errors.WrapPath(err, attrResources, path))
}
}
err := m.Resources.validate(m, rootPath)
if err != nil {
errs = errors.Append(errs, errors.Wrap(err, attrResources))
}
err = m.Build.validate(m)
if err != nil {
errs = errors.Append(errs, errors.Wrap(err, attrBuild))
}
if errs != nil {
return errors.Wrap(errs, attrModel)
}
return nil
}
func (item *Item) validate(m *Model) error {
var errs error
opath := item.ObjectPath()
if item.ObjectID == 0 {
errs = errors.Append(errs, errors.NewMissingFieldError(attrObjectID))
} else if obj, ok := m.FindObject(opath, item.ObjectID); ok {
if obj.Type == ObjectTypeOther {
errs = errors.Append(errs, errors.ErrOtherItem)
}
} else {
errs = errors.Append(errs, errors.ErrMissingResource)
}
return errors.Append(errs, checkMetadadata(m, item.Metadata.Metadata))
}
func (b *Build) validate(m *Model) error {
var errs error
for i, item := range b.Items {
err := item.validate(m)
if err != nil {
errs = errors.Append(errs, errors.WrapIndex(err, attrItem, i))
}
}
return errs
}
var allowedMetadataNames = [...]string{ // sorted
"application", "copyright", "creationdate", "description", "designer",
"licenseterms", "modificationdate", "rating", "title",
}
func (m *Metadata) validate(model *Model) error {
if m.Name.Local == "" {
return errors.NewMissingFieldError(attrName)
}
var errs error
if m.Name.Space == "" {
nm := strings.ToLower(m.Name.Local)
n := sort.SearchStrings(allowedMetadataNames[:], nm)
if n >= len(allowedMetadataNames) || allowedMetadataNames[n] != nm {
errs = errors.Append(errs, errors.ErrMetadataName)
}
} else {
var hasExt bool
for _, ext := range model.Extensions {
if ext.Namespace == m.Name.Space {
hasExt = true
break
}
}
if !hasExt {
errs = errors.Append(errs, errors.ErrMetadataNamespace)
}
}
return errs
}
func checkMetadadata(model *Model, md []Metadata) error {
var errs error
names := make(map[xml.Name]struct{})
for i, m := range md {
err := m.validate(model)
errs = errors.Append(errs, errors.WrapIndex(err, attrMetadata, i))
if _, ok := names[m.Name]; ok {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrMetadataDuplicated, attrMetadata, i))
}
names[m.Name] = struct{}{}
}
return errs
}
// Validate validates the base materia is compliant with the 3MF specs.
func (r *BaseMaterials) Validate(m *Model, path string) error {
var errs error
if r.ID == 0 {
errs = errors.Append(errs, errors.ErrMissingID)
}
if len(r.Materials) == 0 {
errs = errors.Append(errs, errors.ErrEmptyResourceProps)
}
for j, b := range r.Materials {
if b.Name == "" {
errs = errors.Append(errs, errors.WrapIndex(errors.NewMissingFieldError(attrName), attrBase, j))
}
if b.Color == (color.RGBA{}) {
errs = errors.Append(errs, errors.WrapIndex(errors.NewMissingFieldError(attrDisplayColor), attrBase, j))
}
}
return errs
}
func (res *Resources) validate(m *Model, path string) error {
var errs error
assets := make(map[uint32]struct{})
for i, r := range res.Assets {
var aErrs error
id := r.Identify()
if id != 0 {
if _, ok := assets[id]; ok {
aErrs = errors.Append(aErrs, errors.ErrDuplicatedID)
}
}
assets[id] = struct{}{}
if r, ok := r.(*BaseMaterials); ok {
aErrs = errors.Append(aErrs, r.Validate(m, path))
}
for _, ext := range m.Extensions {
if ext, ok := spec.LoadValidator(ext.Namespace); ok {
aErrs = errors.Append(aErrs, ext.Validate(m, path, r))
}
}
errs = errors.Append(errs, errors.WrapIndex(aErrs, r.XMLName().Local, i))
}
for i, r := range res.Objects {
if r.ID != 0 {
if _, ok := assets[r.ID]; ok {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrDuplicatedID, attrObject, i))
}
}
assets[r.ID] = struct{}{}
err := r.Validate(m, path)
errs = errors.Append(errs, errors.WrapIndex(err, attrObject, i))
}
return errs
}
// Validate validates that the object is compliant with 3MF specs,
// except for the mesh coherency.
func (r *Object) Validate(m *Model, path string) error {
res, _ := m.FindResources(path)
var errs error
if r.ID == 0 {
errs = errors.Append(errs, errors.ErrMissingID)
}
if r.PIndex != 0 && r.PID == 0 {
errs = errors.Append(errs, errors.NewMissingFieldError(attrPID))
}
if (r.Mesh != nil && r.Components != nil) || (r.Mesh == nil && r.Components == nil) {
errs = errors.Append(errs, errors.ErrInvalidObject)
}
if r.Mesh != nil {
if r.PID != 0 {
if a, ok := res.FindAsset(r.PID); ok {
if a, ok := a.(spec.PropertyGroup); ok {
if int(r.PIndex) >= a.Len() {
errs = errors.Append(errs, errors.ErrIndexOutOfBounds)
}
}
} else {
errs = errors.Append(errs, errors.ErrMissingResource)
}
}
err := r.validateMesh(m, path)
if err != nil {
errs = errors.Append(errs, errors.Wrap(err, attrMesh))
}
}
if r.Components != nil && len(r.Components.Component) > 0 {
if r.PID != 0 {
errs = errors.Append(errs, errors.ErrComponentsPID)
}
errs = errors.Append(errs, r.validateComponents(m, path))
}
for _, ext := range m.Extensions {
if ext, ok := spec.LoadValidator(ext.Namespace); ok {
errs = errors.Append(errs, ext.Validate(m, path, r))
}
}
return errs
}
func (r *Object) validateMesh(m *Model, path string) error {
res, _ := m.FindResources(path)
var errs error
switch r.Type {
case ObjectTypeModel, ObjectTypeSolidSupport:
if len(r.Mesh.Vertices.Vertex) < 3 {
errs = errors.Append(errs, errors.ErrInsufficientVertices)
}
if len(r.Mesh.Triangles.Triangle) <= 3 && len(r.Mesh.Any) == 0 {
errs = errors.Append(errs, errors.ErrInsufficientTriangles)
}
}
nodeCount := uint32(len(r.Mesh.Vertices.Vertex))
for i, t := range r.Mesh.Triangles.Triangle {
if t.V1 == t.V2 || t.V1 == t.V3 || t.V2 == t.V3 {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrDuplicatedIndices, attrTriangle, i))
}
if t.V1 >= nodeCount || t.V2 >= nodeCount || t.V3 >= nodeCount {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrIndexOutOfBounds, attrTriangle, i))
}
if t.PID != 0 {
if t.PID == r.PID && t.P1 == r.PIndex &&
t.P2 == r.PIndex && t.P3 == r.PIndex {
continue
}
if a, ok := res.FindAsset(t.PID); ok {
if a, ok := a.(spec.PropertyGroup); ok {
l := a.Len()
if int(t.P1) >= l || int(t.P2) >= l || int(t.P3) >= l {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrIndexOutOfBounds, attrTriangle, i))
}
}
} else {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrMissingResource, attrTriangle, i))
}
}
}
return errs
}
func (r *Object) validateComponents(m *Model, path string) error {
var errs error
for j, c := range r.Components.Component {
if c.ObjectID == 0 {
errs = errors.Append(errs, errors.WrapIndex(errors.NewMissingFieldError(attrObjectID), attrComponent, j))
} else if ref, ok := m.FindObject(c.ObjectPath(path), c.ObjectID); ok {
if ref.ID == r.ID && c.ObjectPath(path) == path {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrRecursion, attrComponent, j))
}
} else {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrMissingResource, attrComponent, j))
}
}
if errs != nil {
return errors.Wrap(errs, attrComponents)
}
return nil
}
func (m *Model) validateNamespaces() error {
for _, ext := range m.Extensions {
if ext.IsRequired {
if _, ok := spec.Load(ext.Namespace); !ok {
return errors.ErrRequiredExt
}
}
}
return nil
}
func validateRelationship(m *Model, rels []Relationship, path string) error {
var errs error
type partrel struct{ path, rel string }
visitedParts := make(map[partrel]struct{})
var hasPrintTicket bool
for i, r := range rels {
if r.Path == "" || r.Path[0] != '/' || strings.Contains(r.Path, "/.") {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrOPCPartName, "relationship", i))
} else {
if _, ok := findAttachment(m.Attachments, r.Path); !ok {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrOPCRelTarget, "relationship", i))
}
if _, ok := visitedParts[partrel{r.Path, r.Type}]; ok {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrOPCDuplicatedRel, "relationship", i))
}
visitedParts[partrel{r.Path, r.Type}] = struct{}{}
}
switch r.Type {
case RelTypePrintTicket:
if a, ok := findAttachment(m.Attachments, r.Path); ok {
if a.ContentType != ContentTypePrintTicket {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrOPCContentType, "relationship", i))
}
if hasPrintTicket {
errs = errors.Append(errs, errors.WrapIndex(errors.ErrOPCDuplicatedTicket, "relationship", i))
}
hasPrintTicket = true
}
}
}
if path != "" && errs != nil {
for _, err := range errs.(*errors.List).Errors {
if err, ok := err.(*errors.Error); ok {
err.Path = path
}
}
}
return errs
}
func findAttachment(att []Attachment, path string) (*Attachment, bool) {
for _, a := range att {
if strings.EqualFold(a.Path, path) {
return &a, true
}
}
return nil, false
}
// ValidateCoherency checks that all the mesh are non-empty, manifold and oriented.
func (m *Model) ValidateCoherency() error {
var (
errs error
wg sync.WaitGroup
mu sync.Mutex
)
wg.Add(len(m.Resources.Objects))
for i := range m.Resources.Objects {
go func(i int) {
defer wg.Done()
r := m.Resources.Objects[i]
if isSolidObject(r) {
err := r.Mesh.ValidateCoherency()
if err != nil {
mu.Lock()
errs = errors.Append(errs, errors.Wrap(errors.WrapIndex(errors.Wrap(err, attrMesh), attrObject, i), attrResources))
mu.Unlock()
}
}
}(i)
}
for path, c := range m.Childs {
wg.Add(len(c.Resources.Objects))
for i := range c.Resources.Objects {
go func(path string, i int) {
defer wg.Done()
res := m.Childs[path].Resources
r := res.Objects[i]
if isSolidObject(r) {
err := r.Mesh.ValidateCoherency()
if err != nil {
mu.Lock()
errs = errors.Append(errs, errors.WrapPath(errors.WrapIndex(errors.Wrap(err, attrMesh), attrObject, i), attrResources, path))
mu.Unlock()
}
}
}(path, i)
}
}
wg.Wait()
if errs != nil {
return errors.Wrap(errs, attrModel)
}
return nil
}
func isSolidObject(r *Object) bool {
return r.Mesh != nil && (r.Type == ObjectTypeModel || r.Type == ObjectTypeSolidSupport)
}
// ValidateCoherency checks that the mesh is non-empty, manifold and oriented.
func (m *Mesh) ValidateCoherency() error {
if len(m.Vertices.Vertex) < 3 {
return errors.ErrInsufficientVertices
}
if len(m.Triangles.Triangle) <= 3 {
return errors.ErrInsufficientTriangles
}
var edgeCounter uint32
pairMatching := make(pairMatch)
for _, face := range m.Triangles.Triangle {
fv := [3]uint32{face.V1, face.V2, face.V3}
for j := 0; j < 3; j++ {
n1, n2 := fv[j], fv[(j+1)%3]
if _, ok := pairMatching.CheckMatch(n1, n2); !ok {
pairMatching.AddMatch(n1, n2, edgeCounter)
edgeCounter++
}
}
}
positive, negative := make([]uint32, edgeCounter), make([]uint32, edgeCounter)
for _, face := range m.Triangles.Triangle {
fv := [3]uint32{face.V1, face.V2, face.V3}
for j := 0; j < 3; j++ {
n1, n2 := fv[j], fv[(j+1)%3]
edgeIndex, _ := pairMatching.CheckMatch(n1, n2)
if n1 <= n2 {
positive[edgeIndex]++
} else {
negative[edgeIndex]++
}
}
}
for i := uint32(0); i < edgeCounter; i++ {
if positive[i] != 1 || negative[i] != 1 {
return errors.ErrMeshConsistency
}
}
return nil
}