Skip to content

Commit a1398d1

Browse files
committed
Improve diffing of cameras and lights
1 parent b705343 commit a1398d1

File tree

6 files changed

+66
-49
lines changed

6 files changed

+66
-49
lines changed

ShapeScript/GeometryType.swift

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,57 @@
99
import Euclid
1010

1111
public struct Camera: Hashable {
12-
public var hasPosition: Bool
13-
public var hasOrientation: Bool
14-
public var hasScale: Bool
12+
public var position: Vector?
13+
public var orientation: Rotation?
14+
public var scale: Vector?
1515
public var background: MaterialProperty?
1616
public var fov: Angle?
1717
public var width: Double?
1818
public var height: Double?
1919
}
2020

21+
public extension Camera {
22+
var hasPosition: Bool {
23+
get { position != nil }
24+
@available(*, deprecated, message: "Obsolete. Use position instead.")
25+
set { position = newValue ? .zero : nil }
26+
}
27+
28+
var hasOrientation: Bool {
29+
get { orientation != nil }
30+
@available(*, deprecated, message: "Obsolete. Use orientation instead.")
31+
set { orientation = newValue ? .identity : nil }
32+
}
33+
34+
var hasScale: Bool {
35+
get { scale != nil }
36+
@available(*, deprecated, message: "Obsolete. Use scale instead.")
37+
set { scale = newValue ? .one : nil }
38+
}
39+
}
40+
2141
public struct Light: Hashable {
22-
public var hasPosition: Bool
23-
public var hasOrientation: Bool
42+
public var position: Vector?
43+
public var orientation: Rotation?
2444
public var color: Color
2545
public var spread: Angle
2646
public var penumbra: Double
2747
}
2848

49+
public extension Light {
50+
var hasPosition: Bool {
51+
get { position != nil }
52+
@available(*, deprecated, message: "Obsolete. Use position instead.")
53+
set { position = newValue ? .zero : nil }
54+
}
55+
56+
var hasOrientation: Bool {
57+
get { orientation != nil }
58+
@available(*, deprecated, message: "Obsolete. Use orientation instead.")
59+
set { orientation = newValue ? .identity : nil }
60+
}
61+
}
62+
2963
public enum GeometryType: Hashable {
3064
case group
3165
// primitives

ShapeScript/Scene+SceneKit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ public extension Geometry {
349349
transform.scale *= scnCamera.orthographicScale
350350
}
351351
type = .camera(Camera(
352-
hasPosition: true,
353-
hasOrientation: true,
354-
hasScale: isOrtho,
352+
position: transform.offset,
353+
orientation: transform.rotation,
354+
scale: isOrtho ? transform.scale : nil,
355355
background: nil,
356356
fov: .degrees(isOrtho ? 0 : Double(scnCamera.fieldOfView)),
357357
width: nil,

ShapeScript/StandardLibrary.swift

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,14 @@ extension Dictionary where Key == String, Value == Symbol {
142142
"spread": .number,
143143
"penumbra": .number,
144144
])) { context in
145-
var hasPosition = false, hasOrientation = false
146-
if let position = context.value(for: "position")?.value as? Vector {
147-
context.transform.offset = position
148-
hasPosition = true
149-
}
150-
if let orientation = context.value(for: "orientation")?.value as? Rotation {
151-
context.transform.rotation = orientation
152-
hasOrientation = true
153-
}
145+
let position = context.value(for: "position")?.value as? Vector
146+
position.map { context.transform.offset = $0 }
147+
let orientation = context.value(for: "orientation")?.value as? Rotation
148+
orientation.map { context.transform.rotation = $0 }
154149
return .mesh(Geometry(
155150
type: .light(Light(
156-
hasPosition: hasPosition,
157-
hasOrientation: hasOrientation,
151+
position: position,
152+
orientation: orientation,
158153
color: context.value(for: "color")?.colorValue ?? .white,
159154
spread: context.value(for: "spread")?.angleValue ?? (.pi / 4),
160155
penumbra: context.value(for: "penumbra")?.doubleValue ?? 1
@@ -405,24 +400,17 @@ extension Dictionary where Key == String, Value == Symbol {
405400
"width": .number,
406401
"height": .number,
407402
])) { context in
408-
var hasPosition = false, hasOrientation = false, hasScale = false
409-
if let position = context.value(for: "position")?.value as? Vector {
410-
context.transform.offset = position
411-
hasPosition = true
412-
}
413-
if let orientation = context.value(for: "orientation")?.value as? Rotation {
414-
context.transform.rotation = orientation
415-
hasOrientation = true
416-
}
417-
if let size = context.value(for: "size")?.value as? Vector {
418-
context.transform.scale = size
419-
hasScale = true
420-
}
403+
let position = context.value(for: "position")?.value as? Vector
404+
position.map { context.transform.offset = $0 }
405+
let orientation = context.value(for: "orientation")?.value as? Rotation
406+
orientation.map { context.transform.rotation = $0 }
407+
let scale = context.value(for: "size")?.value as? Vector
408+
scale.map { context.transform.scale = $0 }
421409
return .mesh(Geometry(
422410
type: .camera(Camera(
423-
hasPosition: hasPosition,
424-
hasOrientation: hasOrientation,
425-
hasScale: hasScale,
411+
position: position,
412+
orientation: orientation,
413+
scale: scale,
426414
background: context.background,
427415
fov: context.value(for: "fov")?.angleValue,
428416
width: context.value(for: "width")?.doubleValue,

ShapeScriptTests/GeometryTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class GeometryTests: XCTestCase {
2525
)
2626
let camera = Geometry(
2727
type: .camera(Camera(
28-
hasPosition: false,
29-
hasOrientation: false,
30-
hasScale: false
28+
position: nil,
29+
orientation: nil,
30+
scale: nil
3131
)),
3232
name: nil,
3333
transform: Transform(

Viewer/Camera.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ struct Camera {
3636
extension Camera: Equatable {
3737
static let `default` = Self(type: .front)
3838

39-
static func == (lhs: Camera, rhs: Camera) -> Bool {
40-
lhs.type == rhs.type &&
41-
lhs.name == rhs.name &&
42-
lhs.settings == rhs.settings &&
43-
lhs.geometry?.transform == rhs.geometry?.transform
44-
}
45-
4639
init(type: CameraType) {
4740
self.type = type
4841
}
@@ -73,15 +66,15 @@ extension Camera: Equatable {
7366
}
7467

7568
var hasPosition: Bool {
76-
settings?.hasPosition ?? false
69+
settings?.position != nil
7770
}
7871

7972
var hasOrientation: Bool {
80-
settings?.hasOrientation ?? false
73+
settings?.orientation != nil
8174
}
8275

8376
var hasScale: Bool {
84-
settings?.hasScale ?? false
77+
settings?.scale != nil
8578
}
8679

8780
var background: MaterialProperty? {

Viewer/Document+View.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ extension Document {
164164
}
165165
if !oldCameras.isEmpty {
166166
var didUpdateCamera = false
167-
for (old, new) in zip(oldCameras, cameras) where old != new {
167+
for (old, new) in zip(oldCameras, cameras)
168+
where old.type != new.type || old.settings != new.settings
169+
{
168170
camera = new
169171
didUpdateCamera = true
170172
break

0 commit comments

Comments
 (0)