forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue+Logging.swift
More file actions
442 lines (388 loc) · 12.5 KB
/
Value+Logging.swift
File metadata and controls
442 lines (388 loc) · 12.5 KB
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
//
// Value+Logging.swift
// ShapeScript Lib
//
// Created by Nick Lockwood on 17/08/2021.
// Copyright © 2021 Nick Lockwood. All rights reserved.
//
import Euclid
import Foundation
public protocol Loggable {
/// Top-level log description
var logDescription: String { get }
/// Log description when nested inside an array or tuple
var nestedLogDescription: String { get }
}
public extension String {
init(logDescriptionFor value: Any) {
self.init(describing: (value as? Loggable)?.logDescription ?? value)
}
init(nestedLogDescriptionFor value: Any) {
self.init(describing: (value as? Loggable)?.nestedLogDescription ?? value)
}
}
extension String: Loggable {
public var logDescription: String {
self
}
public var nestedLogDescription: String {
"\"" + replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "\n", with: "\\n") + "\""
}
}
extension TextValue: Loggable {
var logDescription: String {
string
}
var nestedLogDescription: String {
string.nestedLogDescription
}
}
extension Double: Loggable {
private static let formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 4
formatter.usesGroupingSeparator = false
formatter.positiveInfinitySymbol = "∞"
formatter.negativeInfinitySymbol = "-∞"
formatter.notANumberSymbol = "NaN"
return formatter
}()
public var logDescription: String {
let result = Self.formatter.string(from: self as NSNumber) ?? "NaN"
return result == "-0" ? "0" : result
}
public var nestedLogDescription: String {
logDescription
}
}
extension Bool: Loggable {
public var logDescription: String {
"\(self)"
}
public var nestedLogDescription: String {
logDescription
}
}
extension Vector: Loggable {
public var logDescription: String {
"\(x.logDescription) \(y.logDescription) \(z.logDescription)"
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension Angle: Loggable {
public var logDescription: String {
halfturns.logDescription
}
public var nestedLogDescription: String {
logDescription
}
}
extension Rotation: Loggable {
public var logDescription: String {
"\(roll.logDescription) \(yaw.logDescription) \(pitch.logDescription)"
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension Color: Loggable {
public var logDescription: String {
var components = components
if a == 1 {
components.removeLast()
}
return components.map(\.logDescription).joined(separator: " ")
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension Texture: Loggable {
public var logDescription: String {
switch self {
case let .file(name: _, url: url, intensity: _):
return url.path.logDescription
case .data:
return "texture { #data }"
}
}
public var nestedLogDescription: String {
switch self {
case let .file(name: _, url: url, intensity: _):
return url.path.nestedLogDescription
case .data:
return "texture"
}
}
}
extension MaterialProperty: Loggable {
public var logDescription: String {
switch self {
case let .color(color):
return color.logDescription
case let .texture(texture):
return texture.logDescription
}
}
public var nestedLogDescription: String {
switch self {
case let .color(color):
return color.nestedLogDescription
case let .texture(texture):
return texture.nestedLogDescription
}
}
}
extension Material: Loggable {
public var logDescription: String {
let fields = [
opacity.map { "opacity \(Value.numberOrTexture($0).logDescription)" },
color.map { "color \($0.logDescription)" },
texture.map { "texture \($0.logDescription)" },
metallicity.map { "metallicity \(Value.numberOrTexture($0).logDescription)" },
roughness.map { "roughness \(Value.numberOrTexture($0).logDescription)" },
glow.map { "glow \($0.logDescription)" },
].compactMap { $0 }
switch fields.count {
case 0:
return "material { default }"
case 1:
return "material { \(fields[0]) }"
default:
return "material {\n \(fields.joined(separator: "\n "))\n}"
}
}
public var nestedLogDescription: String {
"material"
}
}
extension Path: Loggable {
public var logDescription: String {
if subpaths.count > 1 {
return "path { subpaths \(subpaths.count) }"
}
return "path { points \(points.count) }"
}
public var nestedLogDescription: String {
"path"
}
}
extension Polygon: Loggable {
public var logDescription: String {
"polygon { points \(vertices.count) }"
}
public var nestedLogDescription: String {
"polygon"
}
}
extension PathPoint: Loggable {
public var logDescription: String {
"\(nestedLogDescription) { \(position.logDescription) }"
}
public var nestedLogDescription: String {
isCurved ? "curve" : "point"
}
}
extension Bounds: Loggable {
public var logDescription: String {
"""
bounds {
min \(min.logDescription)
max \(max.logDescription)
}
"""
}
public var nestedLogDescription: String {
"bounds"
}
}
extension GeometryType: Loggable {
public var logDescription: String {
switch self {
case .group: return "group"
case .cone: return "cone"
case .cylinder: return "cylinder"
case .sphere: return "sphere"
case .cube: return "cube"
case .extrude: return "extrusion"
case .lathe: return "lathe"
case .loft: return "loft"
case .fill: return "fill"
case .hull: return "hull"
case .minkowski: return "minkowski"
case .union: return "union"
case .difference: return "difference"
case .intersection: return "intersection"
case .xor: return "xor"
case .stencil: return "stencil"
case .path: return "path"
case .mesh: return "mesh"
case .camera: return "camera"
case .light: return "light"
}
}
public var nestedLogDescription: String {
logDescription
}
}
extension Geometry: Loggable {
public var logDescription: String {
let epsilon = 0.0001
let scale = transform.scale
let scaleDescription: String?
if abs(scale.x - scale.y) < epsilon, abs(scale.y - scale.z) < epsilon {
scaleDescription = abs(scale.x - 1) < epsilon ? nil : "size \(scale.x.logDescription)"
} else {
scaleDescription = "size \(scale.logDescription)"
}
var fields = [
name.flatMap { $0.isEmpty ? nil : "name \($0.nestedLogDescription)" },
childCount == 0 ? nil : "children \(childCount)",
scaleDescription,
transform.translation == .zero ? nil : "position \(transform.translation.logDescription)",
transform.rotation == .identity ? nil : "orientation \(transform.rotation.logDescription)",
].compactMap { $0 }
switch type {
case let .camera(camera):
if let fov = camera.fov, abs(fov.degrees - 60) > epsilon {
fields.append("fov \(fov.logDescription)")
}
if let width = camera.width {
fields.append("width \(width.logDescription)")
}
if let height = camera.height {
fields.append("height \(height.logDescription)")
}
switch camera.background {
case let .color(color)?:
fields.append("background \(color.logDescription)")
case let .texture(.file(name: name, url: _, intensity: _))?:
fields.append("background \(name.nestedLogDescription)")
case .texture, nil:
break
}
case let .light(light):
if light.color != .white {
fields.append("color \(light.color.logDescription)")
}
fields.append("spread \(light.spread.logDescription)")
if light.penumbra != 1 {
fields.append("penumbra \(light.penumbra.logDescription)")
}
if light.shadowOpacity != 0 {
fields.append("shadow \(light.shadowOpacity.logDescription)")
}
case .mesh:
fields.append("polygons \(polygons { false }.count)")
case let .path(path):
if path.subpaths.count > 1 {
fields.append("subpaths \(path.subpaths.count)")
} else {
fields.append("points \(path.points.count)")
}
default:
break
}
let block: String
switch fields.count {
case 0:
block = ""
case 1:
block = " { \(fields[0]) }"
default:
block = " {\n \(fields.joined(separator: "\n "))\n}"
}
return type.logDescription + block
}
public var nestedLogDescription: String {
type.nestedLogDescription
}
}
extension Optional: Loggable {
public var logDescription: String {
map { String(logDescriptionFor: $0) } ?? "nil"
}
public var nestedLogDescription: String {
map { String(nestedLogDescriptionFor: $0) } ?? "nil"
}
}
extension Array: Loggable {
public var logDescription: String {
if count == 1 {
return String(logDescriptionFor: self[0])
}
return map { String(nestedLogDescriptionFor: $0) }.joined(separator: " ")
}
public var nestedLogDescription: String {
"(\(map { String(nestedLogDescriptionFor: $0) }.joined(separator: " ")))"
}
}
extension Dictionary: Loggable where Key == String {
public var logDescription: String {
let fields = map { "\($0.key) \(String(nestedLogDescriptionFor: $0.value))" }
switch fields.count {
case 0:
return "object {}"
case 1:
return "object { \(fields[0]) }"
default:
return "object {\n \(fields.sorted().joined(separator: "\n "))\n}"
}
}
public var nestedLogDescription: String {
"object"
}
}
extension RangeValue: Loggable {
public var logDescription: String {
let stepText = step.map { " step \($0.logDescription)" } ?? ""
guard let end else {
return "from \(start.logDescription)\(stepText)"
}
return "\(start.logDescription) to \(end.logDescription)\(stepText)"
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension Value: Loggable {
private var loggableValue: Loggable {
// Note: this switch is technically not needed, but serves to
// ensure logging conformance is not forgotten for new types
switch self {
case let .color(color): return color
case let .texture(texture): return texture
case let .material(material): return material
case let .boolean(boolean): return boolean
case let .number(number): return number
case let .radians(radians): return radians
case let .halfturns(halfturns): return halfturns
case let .vector(vector): return vector
case let .size(size): return size
case let .rotation(rotation): return rotation
case let .string(string): return string
case let .font(font): return font
case let .text(text): return text
case let .path(path): return path
case let .mesh(mesh): return mesh
case let .polygon(polygon): return polygon
case let .point(point): return point
case let .tuple(tuple): return tuple
case let .range(range): return range
case let .bounds(bounds): return bounds
case let .object(object): return object
}
}
public var logDescription: String {
loggableValue.logDescription
}
public var nestedLogDescription: String {
loggableValue.nestedLogDescription
}
}