Skip to content

Commit 4363618

Browse files
Nick Lockwoodnicklockwood
authored andcommitted
Add volume member + tuple access for polygons/triangles
1 parent cec5d89 commit 4363618

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

ShapeScript/Members.swift

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ extension ValueType {
109109
"step": .number,
110110
"min": .number,
111111
"max": .number,
112+
"volume": .number,
112113
"size": .size,
113114
"center": .vector,
114115
"count": .number,
@@ -172,13 +173,19 @@ extension Value {
172173
}) {
173174
members.append("bounds")
174175
}
176+
if values.allSatisfy({ $0.type == .mesh }) {
177+
members += ["polygons", "triangles", "volume"]
178+
}
179+
if values.allSatisfy({ $0.type == .polygon }) {
180+
members += ["polygons", "triangles"]
181+
}
175182
return members
176183
case .range:
177184
return ["start", "end", "step"]
178185
case let .mesh(geometry):
179186
var members = ["name", "bounds"]
180187
if geometry.hasMesh {
181-
members += ["polygons", "triangles", "material"]
188+
members += ["polygons", "triangles", "material", "volume"]
182189
}
183190
return members
184191
case .path:
@@ -291,6 +298,48 @@ extension Value {
291298
return nil
292299
}
293300
}))
301+
case "volume":
302+
return .number(values.reduce(0) { total, value -> Double in
303+
switch value {
304+
case let .mesh(geometry):
305+
return total + geometry.volume(with: geometry.worldTransform) {
306+
!isCancelled()
307+
}
308+
default:
309+
assertionFailure()
310+
return total
311+
}
312+
})
313+
case "polygons":
314+
return .tuple(values.flatMap {
315+
switch $0 {
316+
case let .mesh(geometry):
317+
_ = geometry.build { !isCancelled() }
318+
let polygons = (geometry.mesh?.polygons ?? [])
319+
.transformed(by: geometry.transform)
320+
return polygons.map { Value.polygon($0) }
321+
case .polygon:
322+
return [self]
323+
default:
324+
assertionFailure()
325+
return []
326+
}
327+
})
328+
case "triangles":
329+
return .tuple(values.flatMap {
330+
switch $0 {
331+
case let .mesh(geometry):
332+
_ = geometry.build { !isCancelled() }
333+
let triangles = (geometry.mesh?.triangulate().polygons ?? [])
334+
.transformed(by: geometry.transform)
335+
return triangles.map { Value.polygon($0) }
336+
case let .polygon(polygon):
337+
return polygon.triangulate().map { Value.polygon($0) }
338+
default:
339+
assertionFailure()
340+
return []
341+
}
342+
})
294343
default:
295344
if let index = name.ordinalIndex {
296345
return index < values.count ? values[index] : nil
@@ -327,6 +376,10 @@ extension Value {
327376
return .tuple(triangles.map { .polygon($0) })
328377
case "material" where geometry.hasMesh:
329378
return .material(geometry.material)
379+
case "volume" where geometry.hasMesh:
380+
return .number(geometry.volume(with: geometry.worldTransform) {
381+
!isCancelled()
382+
})
330383
default:
331384
return nil
332385
}

docs/ios/expressions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ for point in circle.points {
312312
}
313313
```
314314

315-
For [meshes](meshes.md) you can access the `name`, `bounds`, `polygons`, `triangles` and `material` members:
315+
For [meshes](meshes.md) you can access the `name`, `bounds`, `volume`, `polygons`, `triangles` and `material` members:
316316

317317
```swift
318318
print cube.bounds.size // prints 1 1 1
319+
print cube.volume // prints 1
319320
print cube.polygons.count // prints 6
320321
print cube.triangles.count // prints 12
321322
```

docs/mac/expressions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ for point in circle.points {
312312
}
313313
```
314314

315-
For [meshes](meshes.md) you can access the `name`, `bounds`, `polygons`, `triangles` and `material` members:
315+
For [meshes](meshes.md) you can access the `name`, `bounds`, `volume`, `polygons`, `triangles` and `material` members:
316316

317317
```swift
318318
print cube.bounds.size // prints 1 1 1
319+
print cube.volume // prints 1
319320
print cube.polygons.count // prints 6
320321
print cube.triangles.count // prints 12
321322
```

docs/src/expressions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ for point in circle.points {
312312
}
313313
```
314314

315-
For [meshes](meshes.md) you can access the `name`, `bounds`, `polygons`, `triangles` and `material` members:
315+
For [meshes](meshes.md) you can access the `name`, `bounds`, `volume`, `polygons`, `triangles` and `material` members:
316316

317317
```swift
318318
print cube.bounds.size // prints 1 1 1
319+
print cube.volume // prints 1
319320
print cube.polygons.count // prints 6
320321
print cube.triangles.count // prints 12
321322
```

0 commit comments

Comments
 (0)