Skip to content

Commit

Permalink
Mesh demo
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Jan 20, 2025
1 parent dd09354 commit f117021
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package indigoextras.mesh

import indigo.shared.collections.Batch
import indigo.shared.geometry.Vertex
import indigoextras.mesh.datatypes.Edge
import indigoextras.mesh.datatypes.Tri
import indigoextras.mesh.datatypes.Triangle

import scala.annotation.tailrec

// Bowyer-Watson Delaunay Triangulation
object BowyerWatson:

def triangulation(pointList: Batch[Vertex]): Mesh =
val superTriangle = datatypes.Triangle.encompassing(pointList)
val superTriangle = Triangle.encompassing(pointList)
triangulation(pointList, superTriangle)

def triangulation(pointList: Batch[Vertex], superTriangle: Triangle): Mesh =
Expand Down Expand Up @@ -100,7 +97,7 @@ object BowyerWatson:

def edgeToTriangle(edge: Edge, mesh: Mesh, point: Vertex): Batch[Triangle] =
Batch.fromOption(
datatypes.Triangle.fromVertices(
Triangle.fromVertices(
point :: mesh.vertices.filter(v => edge.indices.contains(v._1)).map(_._2)
)
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package indigoextras.mesh.datatypes
package indigoextras.mesh

import indigo.shared.collections.Batch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package indigoextras.mesh
import indigo.shared.collections.Batch
import indigo.shared.geometry.LineSegment
import indigo.shared.geometry.Vertex
import indigoextras.mesh.datatypes.Edge
import indigoextras.mesh.datatypes.Tri
import indigoextras.mesh.datatypes.Triangle

import scala.annotation.tailrec

Expand Down Expand Up @@ -308,7 +305,7 @@ object Mesh:
.flatMap(_._2.indices)
.toSet
if vSet.size == 3 then
datatypes.Triangle.fromVertices(
Triangle.fromVertices(
vSet.toList.flatMap(i => mesh.vertices.find(_._1 == i).toList).map(_._2)
)
else None
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package indigoextras.mesh.datatypes
package indigoextras.mesh

import indigo.shared.collections.Batch

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package indigoextras.mesh.datatypes
package indigoextras.mesh

import indigo.shared.collections.Batch
import indigo.shared.geometry.BoundingBox
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package indigoextras.mesh

import indigo.*
import indigoextras.mesh.datatypes.*

class BowyerWatsonTests extends munit.FunSuite:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import indigo.Batch
import indigo.Vertex
import indigo.shared.geometry.LineSegment

import datatypes.Triangle
import datatypes.Tri
import datatypes.Edge

class MeshTests extends munit.FunSuite {

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
val viewportHeight: Int = gameHeight * magnificationLevel // 256

def initialScene(bootData: SandboxBootData): Option[SceneName] =
Some(ComponentUIScene.name)
Some(MeshScene.name)

def scenes(bootData: SandboxBootData): NonEmptyList[Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]] =
NonEmptyList(
Expand Down Expand Up @@ -53,7 +53,8 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
PathFindingScene,
CaptureScreenScene,
NineSliceScene,
ComponentUIScene
ComponentUIScene,
MeshScene
)

val eventFilters: EventFilters = EventFilters.Permissive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import com.example.sandbox.scenes.PathFindingModel
import com.example.sandbox.scenes.PointersModel
import indigo.*
import indigo.syntax.*
import indigoextras.mesh.*
import indigoextras.ui.*
import indigoextras.ui.simple.InputFieldChange

object SandboxModel {

private given CanEqual[Option[String], Option[String]] = CanEqual.derived

def randomPoint(dice: Dice, offset: Point): Point =
Point(dice.rollFromZero(100), dice.rollFromZero(100)).moveBy(offset)

def initialModel(startupData: SandboxStartupData): SandboxGameModel =
val dice = Dice.fromSeed(1)
val offset = Point(75, 75)
val points = List.fill(10)(randomPoint(dice, offset)).toBatch
val superTriangle = Triangle.encompassing(points.map(_.toVertex), 10)
val mesh = Mesh.fromVertices(points.map(_.toVertex), superTriangle)

SandboxGameModel(
DudeModel(startupData.dude, DudeIdle),
SaveLoadPhases.NotStarted,
Expand All @@ -23,7 +33,12 @@ object SandboxModel {
PathFindingModel.empty,
Radians.zero,
0,
components
components,
MeshData(
points,
superTriangle,
mesh
)
)

def components: ComponentGroup[Int] =
Expand Down Expand Up @@ -320,7 +335,14 @@ final case class SandboxGameModel(
pathfinding: PathFindingModel,
rotation: Radians,
num: Int,
components: ComponentGroup[Int]
components: ComponentGroup[Int],
meshData: MeshData
)

final case class MeshData(
points: Batch[Point],
superTriangle: Triangle,
mesh: Mesh
)

final case class DudeModel(dude: Dude, walkDirection: DudeDirection) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.example.sandbox.scenes

import com.example.sandbox.SandboxGameModel
import com.example.sandbox.SandboxStartupData
import com.example.sandbox.SandboxViewModel
import indigo.*
import indigo.scenes.*
import indigoextras.mesh.*

object MeshScene extends Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]:

type SceneModel = SandboxGameModel
type SceneViewModel = SandboxViewModel

def eventFilters: EventFilters =
EventFilters.Restricted

def modelLens: Lens[SandboxGameModel, SandboxGameModel] =
Lens.keepOriginal

def viewModelLens: Lens[SandboxViewModel, SandboxViewModel] =
Lens.keepOriginal

def name: SceneName =
SceneName("mesh scene")

def subSystems: Set[SubSystem[SandboxGameModel]] =
Set()

def updateModel(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel
): GlobalEvent => Outcome[SandboxGameModel] =
_ => Outcome(model)

def updateViewModel(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel,
viewModel: SandboxViewModel
): GlobalEvent => Outcome[SandboxViewModel] =
_ => Outcome(viewModel)

def present(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel,
viewModel: SandboxViewModel
): Outcome[SceneUpdateFragment] =

Outcome(
SceneUpdateFragment(
drawMesh(
model.meshData.points,
model.meshData.superTriangle,
model.meshData.mesh.toLineSegments
)
)
)

def drawMesh(
points: Batch[Point],
superTriangle: Triangle,
lines: Batch[LineSegment]
): Batch[SceneNode] =
val pts: Batch[Shape.Circle] =
points.map { pt =>
Shape.Circle(
pt,
5,
Fill.Color(RGBA.Green)
)
}

val st = superTriangle.toLineSegments.map { ls =>
Shape.Line(ls.start.toPoint, ls.end.toPoint, Stroke(1, RGBA.Magenta))
}

val ml = lines.map { ls =>
Shape.Line(ls.start.toPoint, ls.end.toPoint, Stroke(1, RGBA.Cyan))
}

pts ++ st ++ ml

0 comments on commit f117021

Please sign in to comment.