forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValues.swift
More file actions
373 lines (332 loc) · 10.7 KB
/
Values.swift
File metadata and controls
373 lines (332 loc) · 10.7 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
//
// Values.swift
// ShapeScript Lib
//
// Created by Nick Lockwood on 26/10/2023.
// Copyright © 2023 Nick Lockwood. All rights reserved.
//
import Euclid
import Foundation
enum Value: Hashable {
case color(Color)
case texture(Texture?)
case material(Material)
case boolean(Bool)
case number(Double)
case radians(Double)
case halfturns(Double)
case vector(Vector)
case size(Vector)
case rotation(Rotation)
case string(String)
case font(String)
case text(TextValue)
case path(Path)
case mesh(Geometry)
case polygon(Polygon)
case point(PathPoint)
case tuple([Value])
case range(RangeValue)
case bounds(Bounds)
case object([String: Value])
}
extension Value: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self = .string(value)
}
}
extension Value: ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral {
init(floatLiteral value: Double) {
self = .number(value)
}
init(integerLiteral value: Int) {
self = .number(Double(value))
}
}
extension Value: ExpressibleByBooleanLiteral {
init(booleanLiteral value: Bool) {
self = .boolean(value)
}
}
extension Value: ExpressibleByArrayLiteral {
init(arrayLiteral elements: Value...) {
self.init(elements)
}
init(_ elements: [Value]) {
self = .tuple(elements)
}
init(_ elements: Value...) {
self = elements.count == 1 ? elements[0] : .tuple(elements)
}
}
extension Value: ExpressibleByDictionaryLiteral {
init(dictionaryLiteral elements: (String, Value)...) {
self.init(Dictionary(elements) { $1 })
}
init(_ elements: [String: Value]) {
self = .object(elements)
}
}
struct RangeValue: Hashable {
var start: Double
var end, step: Double?
init(from start: Double, to end: Double?) {
self.start = start
self.end = end
}
}
extension RangeValue {
init?(from start: Double, to end: Double?, step: Double?) {
guard step != 0 else {
return nil
}
self.init(from: start, to: end)
self.step = step
}
var stepIsPositive: Bool { step ?? 1 > 0 }
private static let epsilon: Double = 0.0000001
private var adjustedEnd: Double? {
end.map { $0 + (stepIsPositive ? 1 : -1) * Self.epsilon }
}
var stride: StrideThrough<Double>? {
adjustedEnd.map { Swift.stride(from: start, through: $0, by: step ?? 1) }
}
func contains(_ value: Double) -> Bool {
if stepIsPositive ? value < start : value > start { return false }
if let step,
case let remainder = abs((value - start).remainder(dividingBy: step)),
remainder > Self.epsilon, abs(step) - remainder > Self.epsilon
{
return false
}
guard let adjustedEnd else { return true }
return stepIsPositive ? value <= adjustedEnd : value >= adjustedEnd
}
}
struct TextValue: Hashable {
var string: String
var font: String?
var color: Color?
var linespacing: Double?
}
extension Value {
static let void: Value = .tuple([])
static func colorOrTexture(_ value: MaterialProperty) -> Value {
switch value {
case let .color(color):
return .color(color)
case let .texture(texture):
return .texture(texture)
}
}
static func numberOrTexture(_ value: MaterialProperty) -> Value {
switch value {
case let .color(color):
return .number(color.r)
case let .texture(texture):
return .texture(texture)
}
}
var errorDescription: String {
switch self {
case let .mesh(geometry):
switch geometry.type {
case .path: return "path"
case .cone: return "cone"
case .cylinder: return "cylinder"
case .sphere: return "sphere"
case .cube: return "cube"
case .group, .extrude, .lathe, .loft, .fill, .hull, .minkowski, .union, .difference,
.intersection, .xor, .stencil, .mesh:
return "mesh"
case .camera: return "camera"
case .light: return "light"
}
default:
return type.errorDescription
}
}
var value: AnyHashable {
switch unwrapped(recursive: true) {
case let .color(color): return color
case let .texture(texture): return texture.map { $0 as AnyHashable } ?? AnyHashable("")
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(geometry): return geometry
case let .polygon(polygon): return polygon
case let .point(point): return point
case let .tuple(values): return values.map(\.value)
case let .range(range): return range
case let .bounds(bounds): return bounds
case let .object(values): return values.mapValues { $0.value }
}
}
var doubleValue: Double {
assert(value is Double)
return value as? Double ?? 0
}
var doublesValue: [Double] {
switch self {
case let .tuple(values):
return values.map(\.doubleValue)
case let .number(value):
return [value]
default:
assertionFailure()
return []
}
}
var angleValue: Angle? {
switch self {
case let .radians(radians):
return .radians(radians)
case let .halfturns(halfturns):
return .halfturns(halfturns)
default:
assertionFailure()
return nil
}
}
var intValue: Int {
Int(truncating: doubleValue as NSNumber)
}
var boolValue: Bool {
assert(value is Bool)
return value as? Bool ?? false
}
var stringValue: String {
switch self {
case let .tuple(values):
var spaceNeeded = false
return values.map {
switch $0 {
case let .string(string):
spaceNeeded = false
return string
case let value:
defer { spaceNeeded = true }
let string = value.stringValue
return spaceNeeded ? " \(string)" : string
}
}.joined()
default:
assert(value is Loggable)
return (value as? Loggable)?.logDescription ?? ""
}
}
var tupleValue: [AnyHashable] {
if case let .tuple(values) = self {
return values.map(\.value)
}
return [value]
}
var objectValue: [String: AnyHashable] {
value as? [String: AnyHashable] ?? [:]
}
var sequenceValue: AnySequence<Value>? {
switch self {
case let .range(range):
return range.stride.map { AnySequence($0.lazy.map { .number($0) }) }
case let .tuple(values):
if values.count == 1, let first = values.first {
if case .range = first {
// Special case to handle unbounded ranges
return first.sequenceValue
} else if let value = first.sequenceValue {
return value
}
}
return AnySequence(values)
case let .object(values):
return AnySequence(values.sorted(by: {
$0.0 < $1.0
}).map {
[.string($0), $1]
})
case .boolean, .vector, .size, .rotation, .color, .texture, .material,
.number, .radians, .halfturns, .string, .font, .text, .path, .mesh,
.polygon, .point, .bounds:
return nil
}
}
var vectorValue: Vector {
assert(value is Vector)
return value as? Vector ?? .zero
}
var rotationValue: Rotation {
assert(value is Rotation)
return value as? Rotation ?? .identity
}
var colorValue: Color {
assert(value is Color)
return value as? Color ?? .white
}
var colorOrTextureValue: MaterialProperty? {
switch self {
case let .color(color):
return .color(color)
case let .texture(texture):
return texture.map { .texture($0) }
case .boolean, .vector, .size, .rotation, .range, .tuple, .number,
.radians, .halfturns, .string, .font, .text, .path, .material, .mesh,
.polygon, .point, .bounds, .object:
return nil
}
}
var numberOrTextureValue: MaterialProperty? {
switch self {
case let .number(value):
return .color(.init(value, value))
case let .texture(texture):
return texture.map { .texture($0) }
case .boolean, .vector, .size, .rotation, .range, .tuple, .color,
.radians, .halfturns, .string, .font, .text, .path, .material,
.mesh, .polygon, .point, .bounds, .object:
return nil
}
}
/// Recursively unwrap a tuple containing only one value
func unwrapped(recursive: Bool) -> Value {
if case let .tuple(values) = self, values.count == 1 {
return recursive ? values[0].unwrapped(recursive: true) : values[0]
}
return self
}
/// Recursively flatten nested tuples
func flattened(recursive: Bool) -> [Value] {
[self].flattened(recursive: recursive)
}
}
extension [Value] {
/// Recursively unwrap a list containing only one tuple value
func unwrapped(recursive: Bool) -> [Value] {
if count == 1, case let .tuple(values) = self[0] {
return recursive ? values.unwrapped(recursive: true) : values
}
return self
}
/// Recursively flatten nested tuples
func flattened(recursive: Bool) -> [Value] {
flatMap {
switch $0 {
case let .tuple(values):
return recursive ? values.flattened(recursive: true) : values
case .color, .texture, .material, .boolean, .number,
.radians, .halfturns, .vector, .size, .rotation,
.string, .font, .text, .path, .mesh, .polygon, .point,
.range, .bounds, .object:
return [$0]
}
}
}
}