Skip to content

Commit

Permalink
Tiling no longer a case class #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallisto committed Apr 24, 2024
1 parent 3597521 commit 4a5b5b7
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class TilingUniformitySpec extends AnyFlatSpec with Helper with should.Matchers
}

val matrixProblem: Map[Node, List[(List[Int], Boolean)]] =
uniformityProblem.outerOrderedStripFrom(uniformityProblem.edges.nodes.diff(uniformityProblem.perimeter.toRingNodes))
uniformityProblem.outerOrderedStripFrom(uniformityProblem.graphEdges.nodes.diff(uniformityProblem.perimeter.toRingNodes))

// "A uniformityProblem" can "return an uniform nodes tree" in {
// uniformityProblem.uniformNodesTree(matrixProblem) shouldBe
Expand Down Expand Up @@ -797,7 +797,7 @@ class TilingUniformitySpec extends AnyFlatSpec with Helper with should.Matchers
}

val matrix: Map[Node, List[(List[Int], Boolean)]] =
triSqrHexHexoid.outerOrderedStripFrom(triSqrHexHexoid.edges.nodes.diff(triSqrHexHexoid.perimeter.toRingNodes))
triSqrHexHexoid.outerOrderedStripFrom(triSqrHexHexoid.graphEdges.nodes.diff(triSqrHexHexoid.perimeter.toRingNodes))

it can "be examined for outer polygon strips from all nodes" in {
matrix shouldBe
Expand Down
7 changes: 4 additions & 3 deletions src/main/scala/io/github/scala_tessella/tessella/Tiling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import scala.util.Try
* @param edges the graph edges
* @note being `private` a [[Tiling]] cannot be created outside the class, thus ensuring edges validation
*/
case class Tiling private(edges: List[Edge]) extends Graph(edges) with Ordered[Tiling]:
class Tiling private(edges: List[Edge]) extends Graph(edges) with Ordered[Tiling]:

override def toString: String =
s"Tiling(${edges.stringify})"
Expand Down Expand Up @@ -410,6 +410,7 @@ case class Tiling private(edges: List[Edge]) extends Graph(edges) with Ordered[T

any match
case that: Tiling =>
TilingPolygonsCountOrdering.compare(this, that) == 0 &&
EdgesSizeOrdering.orElse(EdgesNodesSizeOrdering).compare(this.graphEdges, that.graphEdges) == 0 &&
this.orderedRoundedPerimeterAngles.isRotationOrReflectionOf(that.orderedRoundedPerimeterAngles) &&
loop(
Expand Down Expand Up @@ -545,7 +546,7 @@ case class Tiling private(edges: List[Edge]) extends Graph(edges) with Ordered[T
TilingGrowth.genericGrowPerimeterNodeByVertex(this)(
node,
vertex,
(t, newEdges) => Tiling(t.edges ++ newEdges),
(t, newEdges) => Tiling(t.graphEdges ++ newEdges),
otherNodeStrategies.toList
)
)
Expand Down Expand Up @@ -661,7 +662,7 @@ object Tiling extends UniTriangle with UniHex with Uni4Hex with Uni5Hex with Lay
val unsafeTiling: Tiling =
fromGraphUnsafe(tentative)
if unsafeTiling.perimeter.toRingNodes.isEmpty then
maybePerimeterEdges(unsafeTiling.edges).map(_ => Tiling.empty)
maybePerimeterEdges(unsafeTiling.graphEdges).map(_ => Tiling.empty)
else if !unsafeTiling.hasValidPerimeterVertices then
Left(invalidPerimeterVertexErrMsg(unsafeTiling))
else if !unsafeTiling.hasValidFullVertices then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object TilingCoordinates:
polygons.diff(List(polygon)).filter(_.toPolygonPathNodes.exists(node => !newCoords.contains(node)))
loop(newCoords, newPolygons)

if tiling.edges.isEmpty then
if tiling.graphEdges.isEmpty then
Map()
else
loop(startingCoords, tiling.orientedPolygons).flipVertically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ object TilingErrorMessages:

/** Error message for invalid grown tiling from inner edge, with SVG description */
def addToNonPerimeterEdgeErrMsg(edge: Edge): String =
if tiling.edges.contains(edge) then
if tiling.graphEdges.contains(edge) then
val svg: String =
addSVG(tiling.invalidTilingSVG(
Description(s"Adding to inner edge ${edge.stringify}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ object TilingGrowth:
.filter((_, point) => tiling.perimeterCoords.values.exists(_.almostEquals(point, LESSER_ACCURACY)))
.toList match
case _ :: _ =>
Left((tiling.edges ++ newEdges, _.invalidVertexCoordsErrMsg))
Left((tiling.graphEdges ++ newEdges, _.invalidVertexCoordsErrMsg))
case Nil =>
val newEdgesCoords: Coords =
newCoords ++ perimeterCoordsAt(end, start)
Expand All @@ -203,7 +203,7 @@ object TilingGrowth:
tiling.perimeter.toRingEdges.toList.withoutNodes(List(node))
.toSegments(tiling.perimeterCoords).filter(_.hasEndpointIn(enlargedBox))
if lines.lesserIntersects(perimeterLines) then
Left((tiling.edges ++ newEdges, _.invalidIntersectionErrMsg))
Left((tiling.graphEdges ++ newEdges, _.invalidIntersectionErrMsg))
else
Right(newEdges)

Expand Down Expand Up @@ -234,11 +234,11 @@ object TilingGrowth:

private def onPerimeterCheck(node: Node): Either[GrowthLeft, ?] =
if tiling.perimeter.toRingNodes.contains(node) then Right(())
else Left((tiling.edges, _.addToNonPerimeterNodeErrMsg(node)))
else Left((tiling.graphEdges, _.addToNonPerimeterNodeErrMsg(node)))

private def anglesCheck(nodes: List[Node], polygon: Polygon): Either[GrowthLeft, ?] =
nodes.find(node => Try(Vertex(polygon +: tiling.perimeterOrderedPolygons(node))).isFailure) match
case Some(node) => Left((tiling.edges, _.addExceedingAngleErrMsg(node, Vertex(polygon))))
case Some(node) => Left((tiling.graphEdges, _.addExceedingAngleErrMsg(node, Vertex(polygon))))
case None => Right(())

private def nodeImplementStrategy(otherNodeStrategies: List[OtherNodeStrategy]): Node => Either[GrowthLeft, Boolean] =
Expand Down Expand Up @@ -290,7 +290,7 @@ object TilingGrowth:
*/
def edgesFromPerimeterEdgeGrowth(edge: Edge, polygon: Polygon, otherNodeStrategies: List[OtherNodeStrategy]): Either[GrowthLeft, List[Edge]] =
tiling.perimeter.isOrientedAt(edge) match
case None => Left(tiling.edges, _.addToNonPerimeterEdgeErrMsg(edge))
case None => Left(tiling.graphEdges, _.addToNonPerimeterEdgeErrMsg(edge))
case Some(edgeIsPerimeterOriented) =>
val firstSecond: (Node, Node) =
if edgeIsPerimeterOriented then edge.pair else edge.pair.swap
Expand Down Expand Up @@ -344,7 +344,7 @@ object TilingGrowth:
f: (Tiling, List[Edge]) => Tiling,
otherNodeStrategies: List[OtherNodeStrategy]): Either[GrowthLeft, Tiling] =
if vertex.isFull then
Left((tiling.edges, _ => "Vertex to be added cannot be full"))
Left((tiling.graphEdges, _ => "Vertex to be added cannot be full"))
else
vertex.toPolygons.toList match
case Nil => Right(tiling)
Expand Down Expand Up @@ -422,7 +422,7 @@ object TilingGrowth:
/** Tries to create a simpler tiling by removing a node */
def maybeRemoveNode(node: Node): Either[String, Tiling] =
val removed: List[Edge] =
tiling.edges.withoutNodes(List(node))
tiling.graphEdges.withoutNodes(List(node))
val newEdges: List[Edge] =
removed.map(edge =>
Edge(edge.nodes.map({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ object SVG extends ConverterSVG:
extension (tiling: Tiling)

private def graphSVG: Option[Elem] =
Option(graphGroup(tiling.edges.map(edge => line(tiling.coords(edge.lesserNode), tiling.coords(edge.greaterNode)))))
Option(graphGroup(tiling.graphEdges.map(edge => line(tiling.coords(edge.lesserNode), tiling.coords(edge.greaterNode)))))

private def polygonsSVG(fillPolygons: Boolean): Option[Elem] =
if fillPolygons then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait Layered extends Reticulate:
if f(i, j)
yield Node(i + topRight.toInt * j)
val edges: List[Edge] =
triangleNet((l + 1) * 2, h + 1).toOption.get.edges.withoutNodes(topRight :: bottomLeft :: emptyNodes.toList).compact
triangleNet((l + 1) * 2, h + 1).toOption.get.graphEdges.withoutNodes(topRight :: bottomLeft :: emptyNodes.toList).compact
(edges, Node(edges.nodes.max(NodeOrdering).toInt - l))

/** Grid of 2 * l triangles with in between a full hexagon net
Expand All @@ -39,8 +39,8 @@ trait Layered extends Reticulate:
triHexBase(l, h)((i, j) => (i + invertedStep + j % 3) % 3 == 0)

private val fs: Map[Int, Int => (List[Edge], Node)] = Map(
3 -> { l => (triangleNet(l * 2, 1).toOption.get.edges, Node(l + 2)) },
4 -> { l => (squareNet(l, 1).toOption.get.edges, Node(l + 2)) },
3 -> { l => (triangleNet(l * 2, 1).toOption.get.graphEdges, Node(l + 2)) },
4 -> { l => (squareNet(l, 1).toOption.get.graphEdges, Node(l + 2)) },
36 -> { triHexBase(_, 1)((i, _) => i % 2 == 1) },
63 -> { triHexBase(_, 1)((i, _) => i % 2 == 0) },
336 -> { triHexBase(_, 1)((i, _) => i % 3 == 1) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.scalatest.matchers.should
class GraphSpec extends AnyFlatSpec with should.Matchers {

val reticulate: Graph =
Graph(sqr4x4Reticulate.edges)
Graph(sqr4x4Reticulate.graphEdges)

"A path of a graph" can "be created" in {
reticulate.Path(Vector(1, 2, 3, 4, 5, 10, 15, 20, 25).map(Node(_))) shouldEqual
Expand Down Expand Up @@ -227,7 +227,7 @@ class GraphSpec extends AnyFlatSpec with should.Matchers {
}

val graph: Graph =
Graph(triangle.edges)
Graph(triangle.graphEdges)

"A Graph" can "be printed" in {
graph.toString shouldBe
Expand Down
14 changes: 7 additions & 7 deletions src/test/scala/io/github/scala_tessella/tessella/Outliers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object Outliers extends Helper:
Tiling.hexTrianguloid(4).unsafe

lazy val gonExperiment: Tiling =
Tiling.maybe(sqr3x3Growth.edges ++ List(
Tiling.maybe(sqr3x3Growth.graphEdges ++ List(
8--17, 17--18, 18--19, 19--20, 20--9,
10--21, 21--11,
16--22, 22--5, 22--23, 23--5,
Expand All @@ -50,7 +50,7 @@ object Outliers extends Helper:
).unsafe

val closer: Tiling =
Tiling.maybe(edges12Nodes8.edges ++ List(8--9, 9--7, 9--10, 10--7)).unsafe
Tiling.maybe(edges12Nodes8.graphEdges ++ List(8--9, 9--7, 9--10, 10--7)).unsafe

lazy val pentagonGrown: Tiling =
TilingGrowth.maybePolygonGrow(Polygon(5), 10).unsafe
Expand Down Expand Up @@ -185,16 +185,16 @@ object Outliers extends Helper:
Tiling.fiveUniformIssue2(18, 12).unsafe

private val triangleEdges =
triangle.edges
triangle.graphEdges

private val hexTrianglesEdges =
Tiling.fromFullVertex(FullVertex.s("(3⁶)")).edges
Tiling.fromFullVertex(FullVertex.s("(3⁶)")).graphEdges

val minimalDisconnected: Graph =
Graph(triangleEdges ++ triangleEdges.compactStartingAt(Node(4)))

lazy val minimalDifferentFromItsPeri: Graph =
Graph(square.edges :+ 1--3)
Graph(square.graphEdges :+ 1--3)

val minimalThinlyConnected: Graph =
Graph(triangleEdges ++ triangleEdges.compactStartingAt(Node(3)))
Expand Down Expand Up @@ -443,10 +443,10 @@ object Outliers extends Helper:
Tiling.fromVertex(Vertex(3, 3, 3, 3, 3))

lazy val minimalSquareInsertion: Tiling =
Tiling.maybe(squareNet(3, 2).unsafe.edges.filterNot(_ == 2--3)).unsafe
Tiling.maybe(squareNet(3, 2).unsafe.graphEdges.filterNot(_ == 2--3)).unsafe

lazy val troubledGrowthByFullVertex: Tiling =
Tiling.maybe(Tiling.triangleNet(4, 3).unsafe.edges.diff(List(1--4, 4--7))).unsafe
Tiling.maybe(Tiling.triangleNet(4, 3).unsafe.graphEdges.diff(List(1--4, 4--7))).unsafe

val problematicEdges: List[Edge] =
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class TilingEqualitySpec extends AnyFlatSpec with Helper with ring_seq.Iterating
}

they can "have a different count of edges" in {
twoPgonsSevenEdges.edges.size should not equal
twoPgonsFiveEdges.edges.size
twoPgonsSevenEdges.graphEdges.size should not equal
twoPgonsFiveEdges.graphEdges.size
}

they must "be then ordered by ascending count of edges, if count of polygons is equal" in {
Expand All @@ -57,8 +57,8 @@ class TilingEqualitySpec extends AnyFlatSpec with Helper with ring_seq.Iterating
}

they can "have the same number of edges" in {
tiling1.edges.size shouldEqual
tiling2.edges.size
tiling1.graphEdges.size shouldEqual
tiling2.graphEdges.size
}

they must "be then ordered by ascending list of perimeter edge polygons, if all other is equal" in {
Expand All @@ -74,8 +74,8 @@ class TilingEqualitySpec extends AnyFlatSpec with Helper with ring_seq.Iterating
}

it can "have a different edge count from another" in {
twoSquares.edges.size should not equal
hexagon.edges.size
twoSquares.graphEdges.size should not equal
hexagon.graphEdges.size
}

it must "be different from other with different edges size" in {
Expand All @@ -87,8 +87,8 @@ class TilingEqualitySpec extends AnyFlatSpec with Helper with ring_seq.Iterating
Tiling.fromPolygon(7)

it can "have the same edge count of another" in {
twoSquares.edges.size shouldEqual
eptagon.edges.size
twoSquares.graphEdges.size shouldEqual
eptagon.graphEdges.size
}

it can "have a different node count from another" in {
Expand All @@ -102,8 +102,8 @@ class TilingEqualitySpec extends AnyFlatSpec with Helper with ring_seq.Iterating
}

"Two tilings" can "have the same edge count" in {
edges12Nodes8.edges.size shouldEqual
edges12Nodes8Similar.edges.size
edges12Nodes8.graphEdges.size shouldEqual
edges12Nodes8Similar.graphEdges.size
}

they can "have the same node count" in {
Expand Down

0 comments on commit 4a5b5b7

Please sign in to comment.