From 605468a995a53ea79602574e2c092ffe91f74d52 Mon Sep 17 00:00:00 2001 From: Zack Brown Date: Wed, 20 Sep 2023 11:50:40 +0100 Subject: [PATCH 1/3] Add method to inset polygon vertices --- Sources/Polygon+CSG.swift | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Sources/Polygon+CSG.swift b/Sources/Polygon+CSG.swift index 58b4b7ed..dadc67ad 100644 --- a/Sources/Polygon+CSG.swift +++ b/Sources/Polygon+CSG.swift @@ -245,3 +245,31 @@ extension Polygon { return false } } + +public extension Polygon { + func inset(by t: Double) -> Self? { + var v: [Vertex] = [] + + for i in orderedEdges.indices { + let e0 = orderedEdges[i] + let e1 = orderedEdges[(i + 1) % orderedEdges.count] + + let p0 = edgePlane(for: e0) + let p1 = edgePlane(for: e1) + + let normal = -(p0.normal + p1.normal).normalized() * t + + let p2 = p0.translated(by: normal) + let p3 = p1.translated(by: normal) + + let vector = vertices.first { $0.position.isEqual(to: e0.end) } + + guard let vector, + let intersection = p2.intersection(with: p3) else { return nil } + + v.append(Vertex(intersection.origin, vector.normal, vector.texcoord, vector.color)) + } + + return Self(v) + } +} From 13523ae165588e1cc9ce701d033947c26fd093ba Mon Sep 17 00:00:00 2001 From: Zack Brown Date: Wed, 20 Sep 2023 13:25:49 +0100 Subject: [PATCH 2/3] Refactor incorrect plane translation --- Sources/Polygon+CSG.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sources/Polygon+CSG.swift b/Sources/Polygon+CSG.swift index dadc67ad..adc1c972 100644 --- a/Sources/Polygon+CSG.swift +++ b/Sources/Polygon+CSG.swift @@ -257,10 +257,8 @@ public extension Polygon { let p0 = edgePlane(for: e0) let p1 = edgePlane(for: e1) - let normal = -(p0.normal + p1.normal).normalized() * t - - let p2 = p0.translated(by: normal) - let p3 = p1.translated(by: normal) + let p2 = p0.translated(by: -p0.normal * t) + let p3 = p1.translated(by: -p1.normal * t) let vector = vertices.first { $0.position.isEqual(to: e0.end) } From 600678c72883c0bf517c0713bd69701b05ea5276 Mon Sep 17 00:00:00 2001 From: Zack Brown Date: Wed, 20 Sep 2023 14:51:33 +0100 Subject: [PATCH 3/3] Tidy up inset method implementation --- Sources/Polygon+CSG.swift | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Sources/Polygon+CSG.swift b/Sources/Polygon+CSG.swift index adc1c972..a6f5cd51 100644 --- a/Sources/Polygon+CSG.swift +++ b/Sources/Polygon+CSG.swift @@ -248,24 +248,22 @@ extension Polygon { public extension Polygon { func inset(by t: Double) -> Self? { - var v: [Vertex] = [] - - for i in orderedEdges.indices { - let e0 = orderedEdges[i] - let e1 = orderedEdges[(i + 1) % orderedEdges.count] + let v: [Vertex] = orderedEdges.indices.compactMap { + let e0 = orderedEdges[$0] + let e1 = orderedEdges[($0 + 1) % orderedEdges.count] let p0 = edgePlane(for: e0) let p1 = edgePlane(for: e1) - let p2 = p0.translated(by: -p0.normal * t) - let p3 = p1.translated(by: -p1.normal * t) + let lhs = p0.translated(by: -p0.normal * t) + let rhs = p1.translated(by: -p1.normal * t) - let vector = vertices.first { $0.position.isEqual(to: e0.end) } + let vertex = vertices.first { $0.position.isEqual(to: e0.end) } - guard let vector, - let intersection = p2.intersection(with: p3) else { return nil } + guard let vertex, + let intersection = lhs.intersection(with: rhs) else { return nil } - v.append(Vertex(intersection.origin, vector.normal, vector.texcoord, vector.color)) + return Vertex(intersection.origin, vertex.normal, vertex.texcoord, vertex.color) } return Self(v)