Skip to content

Commit

Permalink
Merge eeb830b into 3a131ff
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Jul 14, 2020
2 parents 3a131ff + eeb830b commit a10289b
Show file tree
Hide file tree
Showing 31 changed files with 520 additions and 146 deletions.
52 changes: 52 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* [void](#void)
* [wrap](#wrap)
* [wrapArguments](#wrapArguments)
* [wrapMultilineStatementBraces](#wrapMultilineStatementBraces)
* [yodaConditions](#yodaConditions)

----------
Expand Down Expand Up @@ -1589,6 +1590,57 @@ provided for `--wrapparameters`, the value for `--wraparguments` will be used.
</details>
<br/>

## wrapMultilineStatementBraces

Wrap the opening brace of multiline statements (if/guard/while/func).

<details>
<summary>Examples</summary>

```diff
if foo,
- bar {
// ...
}

if foo,
+ bar
+ {
// ...
}
```

```diff
guard foo,
- bar else {
// ...
}

guard foo,
+ bar else
+ {
// ...
}
```

```diff
func foo(
bar: Int,
- baz: Int) {
// ...
}

func foo(
bar: Int,
+ baz: Int)
+ {
// ...
}
```

</details>
<br/>

## yodaConditions

Prefer constant values to be on the right-hand-side of expressions.
15 changes: 10 additions & 5 deletions Snapshots/Euclid/Sources/CSG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ private func reduce(_ meshes: [Mesh], using fn: (Mesh, Mesh) -> Mesh) -> Mesh {
}

private func reduce(_ meshesAndBounds: inout [(Mesh, Bounds)],
at i: Int, using fn: (Mesh, Mesh) -> Mesh) -> Mesh {
at i: Int, using fn: (Mesh, Mesh) -> Mesh) -> Mesh
{
var (m, mb) = meshesAndBounds[i]
var j = i + 1, count = meshesAndBounds.count
while j < count {
Expand Down Expand Up @@ -302,7 +303,8 @@ private class BSPNode {
private func clip(_ polygons: [Polygon],
_ keeping: ClipRule,
_ clipBackfaces: Bool,
_ id: inout Int) -> [Polygon] {
_ id: inout Int) -> [Polygon]
{
var polygons = polygons
var node = self
var total = [Polygon]()
Expand Down Expand Up @@ -458,7 +460,8 @@ extension Polygon {
func clip(to polygons: [Polygon],
_ inside: inout [Polygon],
_ outside: inout [Polygon],
_ id: inout Int) {
_ id: inout Int)
{
precondition(isConvex)
var toTest = [self]
for polygon in polygons where !toTest.isEmpty {
Expand All @@ -475,7 +478,8 @@ extension Polygon {
func clip(_ polygon: Polygon,
_ inside: inout [Polygon],
_ outside: inout [Polygon],
_ id: inout Int) {
_ id: inout Int)
{
precondition(isConvex)
guard polygon.isConvex else {
polygon.tessellate().forEach {
Expand All @@ -500,7 +504,8 @@ extension Polygon {
_ coplanar: inout [Polygon],
_ front: inout [Polygon],
_ back: inout [Polygon],
_ id: inout Int) {
_ id: inout Int)
{
enum PolygonType: Int {
case coplanar = 0
case front = 1
Expand Down
3 changes: 2 additions & 1 deletion Snapshots/Euclid/Sources/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public extension CGPath {
func endPath() {
if points.count > 1 {
if points.count > 2, points.first == points.last,
let firstElement = firstElement {
let firstElement = firstElement
{
updateLastPoint(nextElement: firstElement)
}
let points = sanitizePoints(points)
Expand Down
12 changes: 8 additions & 4 deletions Snapshots/Euclid/Sources/Paths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ internal extension Path {
points.remove(at: i - 1)
} else if p0.position.x >= 0 {
if i == 1 ||
(p1.position.y == p0.position.y && p1.position.z == p0.position.z) {
(p1.position.y == p0.position.y && p1.position.z == p0.position.z)
{
points.remove(at: i - 1)
}
} else {
Expand All @@ -401,7 +402,8 @@ internal extension Path {
// TODO: extend this to work in 3D
// TODO: improve this using https://en.wikipedia.org/wiki/Line–line_intersection
private func lineIntersection(_ p0: Vector, _ p1: Vector,
_ p2: Vector, _ p3: Vector) -> Vector? {
_ p2: Vector, _ p3: Vector) -> Vector?
{
let x1 = p0.x, y1 = p0.y
let x2 = p1.x, y2 = p1.y
let x3 = p2.x, y3 = p2.y
Expand Down Expand Up @@ -431,15 +433,17 @@ private func lineIntersection(_ p0: Vector, _ p1: Vector,

// TODO: extend this to work in 3D
private func lineSegmentsIntersect(_ p0: Vector, _ p1: Vector,
_ p2: Vector, _ p3: Vector) -> Bool {
_ p2: Vector, _ p3: Vector) -> Bool
{
guard let pi = lineIntersection(p0, p1, p2, p3) else {
return false // lines are parallel
}
// TODO: is there a cheaper way to do this?
if pi.x < min(p0.x, p1.x) || pi.x > max(p0.x, p1.x) ||
pi.x < min(p2.x, p3.x) || pi.x > max(p2.x, p3.x) ||
pi.y < min(p0.y, p1.y) || pi.y > max(p0.y, p1.y) ||
pi.y < min(p2.y, p3.y) || pi.y > max(p2.y, p3.y) {
pi.y < min(p2.y, p3.y) || pi.y > max(p2.y, p3.y)
{
return false
}
return true
Expand Down
9 changes: 6 additions & 3 deletions Snapshots/Euclid/Sources/Polygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public extension Polygon {
/// Vertices are assumed to be in anticlockwise order for the purpose of deriving the plane
init?(_ vertices: [Vertex], material: Material = nil) {
guard vertices.count > 2, !verticesAreDegenerate(vertices),
let plane = Plane(points: vertices.map { $0.position }) else {
let plane = Plane(points: vertices.map { $0.position }) else
{
return nil
}
self.init(unchecked: vertices, plane: plane, material: material)
Expand All @@ -71,7 +72,8 @@ public extension Polygon {
for i in 0 ..< count {
if (points[i].y > p.y) != (points[j].y > p.y),
p.x < (points[j].x - points[i].x) * (p.y - points[i].y) /
(points[j].y - points[i].y) + points[i].x {
(points[j].y - points[i].y) + points[i].x
{
c = !c
}
j = i
Expand Down Expand Up @@ -185,7 +187,8 @@ public extension Polygon {
if triangle == nil ||
triangle!.plane.normal.dot(plane.normal) <= 0 || vertices.contains(where: {
!triangle!.vertices.contains($0) && triangle!.containsPoint($0.position)
}) {
})
{
i += 1
if i == vertices.count {
i = 0
Expand Down
39 changes: 26 additions & 13 deletions Snapshots/Euclid/Sources/Shapes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public extension Path {
}

func arc(_ p0: PathPoint, _ p1: PathPoint, _ p2: PathPoint,
_ detail: Int, _ range: ArcRange = .all) -> [PathPoint] {
_ detail: Int, _ range: ArcRange = .all) -> [PathPoint]
{
let detail = detail + 1
assert(detail >= 2)
let steps: [Double]
Expand Down Expand Up @@ -181,7 +182,8 @@ public extension Mesh {
static func cube(center c: Vector = .init(0, 0, 0),
size s: Vector,
faces: Faces = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
let polygons: [Polygon] = [
[[5, 1, 3, 7], [+1, 0, 0]],
[[0, 4, 6, 2], [-1, 0, 0]],
Expand Down Expand Up @@ -229,7 +231,8 @@ public extension Mesh {
static func cube(center c: Vector = .init(0, 0, 0),
size s: Double = 1,
faces: Faces = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
return cube(center: c, size: Vector(s, s, s), faces: faces, material: material)
}

Expand All @@ -240,7 +243,8 @@ public extension Mesh {
poleDetail: Int = 0,
faces: Faces = .default,
wrapMode: WrapMode = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
var semicircle = [PathPoint]()
let stacks = max(2, stacks ?? (slices / 2))
let r = max(abs(r), epsilon)
Expand All @@ -265,7 +269,8 @@ public extension Mesh {
poleDetail: Int = 0,
faces: Faces = .default,
wrapMode: WrapMode = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
let r = max(abs(r), epsilon)
let h = max(abs(h), epsilon)
let wrapMode = wrapMode == .default ? .tube : wrapMode
Expand Down Expand Up @@ -293,7 +298,8 @@ public extension Mesh {
addDetailAtBottomPole: Bool = false,
faces: Faces = .default,
wrapMode: WrapMode = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
let r = max(abs(r), epsilon)
let h = max(abs(h), epsilon)
let poleDetail = poleDetail ?? Int(sqrt(Double(slices)))
Expand Down Expand Up @@ -332,7 +338,8 @@ public extension Mesh {
addDetailForFlatPoles: Bool = false,
faces: Faces = .default,
wrapMode: WrapMode = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
let subpaths = profile.subpaths
if subpaths.count > 1 {
return .xor(subpaths.map {
Expand Down Expand Up @@ -483,7 +490,8 @@ public extension Mesh {
// TODO: improve this by not adding backfaces inside closed subsectors
if let first = vertices.first?.position,
let last = vertices.last?.position,
first != last, first.x != 0 || last.x != 0 {
first != last, first.x != 0 || last.x != 0
{
polygons += polygons.map { $0.inverted() }
}
return Mesh(polygons)
Expand All @@ -494,7 +502,8 @@ public extension Mesh {
static func extrude(_ shape: Path,
depth: Double = 1,
faces: Faces = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
let offset = (shape.plane?.normal ?? Vector(0, 0, 1)) * (depth / 2)
if offset.lengthSquared < epsilon {
return fill(shape, faces: faces, material: material)
Expand All @@ -508,7 +517,8 @@ public extension Mesh {
/// Connect multiple 3D paths
static func loft(_ shapes: [Path],
faces: Faces = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
var subpathCount = 0
let arrayOfSubpaths: [[Path]] = shapes.map {
let subpaths = $0.subpaths
Expand Down Expand Up @@ -559,7 +569,8 @@ public extension Mesh {
// TODO: better handling of case where e0 and e1 counts don't match
let invert: Bool
if let n = prev.plane?.normal,
let p0p1 = directionBetweenShapes(prev, path), p0p1.dot(n) > 0 {
let p0p1 = directionBetweenShapes(prev, path), p0p1.dot(n) > 0
{
invert = false
} else {
invert = true
Expand Down Expand Up @@ -599,7 +610,8 @@ public extension Mesh {
}
if !isClosed, var polygon = Polygon(shape: prev, material: material) {
if let p0p1 = directionBetweenShapes(shapes[shapes.count - 2], prev),
p0p1.dot(polygon.plane.normal) < 0 {
p0p1.dot(polygon.plane.normal) < 0
{
polygon = polygon.inverted()
}
polygons.append(polygon)
Expand All @@ -617,7 +629,8 @@ public extension Mesh {
/// Fill a path to form a polygon
static func fill(_ shape: Path,
faces: Faces = .default,
material: Polygon.Material = nil) -> Mesh {
material: Polygon.Material = nil) -> Mesh
{
let subpaths = shape.subpaths
if subpaths.count > 1 {
return .xor(subpaths.map { .fill($0, faces: faces, material: material) })
Expand Down
6 changes: 4 additions & 2 deletions Snapshots/Expression/Examples/Colors/UIColor+Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ private let functions: [AnyExpression.Symbol: AnyExpression.SymbolEvaluator] = [
.function("rgb", arity: 3): { args in
guard let r = args[0] as? Double,
let g = args[1] as? Double,
let b = args[2] as? Double else {
let b = args[2] as? Double else
{
throw AnyExpression.Error.message("Type mismatch")
}
return UIColor(
Expand All @@ -41,7 +42,8 @@ private let functions: [AnyExpression.Symbol: AnyExpression.SymbolEvaluator] = [
guard let r = args[0] as? Double,
let g = args[1] as? Double,
let b = args[2] as? Double,
let a = args[3] as? Double else {
let a = args[3] as? Double else
{
throw Expression.Error.message("Type mismatch")
}
return UIColor(
Expand Down
3 changes: 2 additions & 1 deletion Snapshots/Expression/Sources/AnyExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ public struct AnyExpression: CustomStringConvertible {
default:
// TODO: should we numberify Bool values like this?
if let boolValue = anyValue as? Bool,
let value: T = AnyExpression.cast(boolValue ? 1 : 0) {
let value: T = AnyExpression.cast(boolValue ? 1 : 0)
{
return value
}
}
Expand Down
Loading

0 comments on commit a10289b

Please sign in to comment.