UNPKG

3 kBJavaScriptView Raw
1// ////////////////////////////////////
2// # Class Properties
3// This class is used to store properties of a solid
4// A property can for example be a Vertex, a Plane or a Line3D
5// Whenever an affine transform is applied to the CSG solid, all its properties are
6// transformed as well.
7// The properties can be stored in a complex nested structure (using arrays and objects)
8const Properties = function () {}
9
10Properties.prototype = {
11 _transform: function (matrix4x4) {
12 let result = new Properties()
13 Properties.transformObj(this, result, matrix4x4)
14 return result
15 },
16 _merge: function (otherproperties) {
17 let result = new Properties()
18 Properties.cloneObj(this, result)
19 Properties.addFrom(result, otherproperties)
20 return result
21 }
22}
23
24Properties.transformObj = function (source, result, matrix4x4) {
25 for (let propertyname in source) {
26 if (propertyname === '_transform') continue
27 if (propertyname === '_merge') continue
28 let propertyvalue = source[propertyname]
29 let transformed = propertyvalue
30 if (typeof (propertyvalue) === 'object') {
31 if (('transform' in propertyvalue) && (typeof (propertyvalue.transform) === 'function')) {
32 transformed = propertyvalue.transform(matrix4x4)
33 } else if (propertyvalue instanceof Array) {
34 transformed = []
35 Properties.transformObj(propertyvalue, transformed, matrix4x4)
36 } else if (propertyvalue instanceof Properties) {
37 transformed = new Properties()
38 Properties.transformObj(propertyvalue, transformed, matrix4x4)
39 }
40 }
41 result[propertyname] = transformed
42 }
43}
44
45Properties.cloneObj = function (source, result) {
46 for (let propertyname in source) {
47 if (propertyname === '_transform') continue
48 if (propertyname === '_merge') continue
49 let propertyvalue = source[propertyname]
50 let cloned = propertyvalue
51 if (typeof (propertyvalue) === 'object') {
52 if (propertyvalue instanceof Array) {
53 cloned = []
54 for (let i = 0; i < propertyvalue.length; i++) {
55 cloned.push(propertyvalue[i])
56 }
57 } else if (propertyvalue instanceof Properties) {
58 cloned = new Properties()
59 Properties.cloneObj(propertyvalue, cloned)
60 }
61 }
62 result[propertyname] = cloned
63 }
64}
65
66Properties.addFrom = function (result, otherproperties) {
67 for (let propertyname in otherproperties) {
68 if (propertyname === '_transform') continue
69 if (propertyname === '_merge') continue
70 if ((propertyname in result) &&
71 (typeof (result[propertyname]) === 'object') &&
72 (result[propertyname] instanceof Properties) &&
73 (typeof (otherproperties[propertyname]) === 'object') &&
74 (otherproperties[propertyname] instanceof Properties)) {
75 Properties.addFrom(result[propertyname], otherproperties[propertyname])
76 } else if (!(propertyname in result)) {
77 result[propertyname] = otherproperties[propertyname]
78 }
79 }
80}
81
82module.exports = Properties