Skip to content

Commit

Permalink
optimize space filler, too
Browse files Browse the repository at this point in the history
  • Loading branch information
bobnik committed Oct 16, 2024
1 parent 5d097fb commit ff169aa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 57 deletions.
53 changes: 53 additions & 0 deletions src/common/lindenmayer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
import Victor from "victor"
import { vertexRoundP, cloneVertex } from "./geometry"
import { buildGraph, edgeKey } from "@/common/Graph"
import { cloneVertices } from "@/common/geometry"

const shortestPath = (nodes) => {
const graph = buildGraph(nodes)
const path = []
const visited = {}

for (let i = 0; i < nodes.length - 1; i++) {
const node1 = nodes[i]
const node2 = nodes[i + 1]
let node1Key = node1.toString()
let edge12Key = edgeKey(node1, node2)

if (visited[edge12Key]) {
const unvisitedNode = nearestUnvisitedNode(i + 1, nodes, visited, graph)

if (unvisitedNode != null) {
const shortestSubPath = graph.dijkstraShortestPath(
node1Key,
unvisitedNode.toString(),
)

path.push(...cloneVertices(shortestSubPath.slice(1)))
i = nodes.indexOf(unvisitedNode) - 1
}
} else {
path.push(node2)
visited[edge12Key] = true
}
}

return path
}

const nearestUnvisitedNode = (nodeIndex, nodes, visited, graph) => {
for (let i = nodeIndex; i < nodes.length - 1; i++) {
const node1 = nodes[i]
const node2 = nodes[i + 1]

if (!visited[edgeKey(node1, node2)]) {
return node2
}
}

return null // all nodes visited
}

export const onSubtypeChange = (subtype, changes, attrs) => {
// if we switch back with too many iterations, the code
Expand Down Expand Up @@ -122,3 +169,9 @@ export const lsystemPath = (instructions, config) => {

return currVertices
}

export const lsystemOptimize = (vertices, config) => {
return config.shortestPath >= config.iterations
? shortestPath(vertices)
: vertices
}
58 changes: 3 additions & 55 deletions src/features/shapes/lsystem/LSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Shape from "../Shape"
import {
lsystem,
lsystemPath,
lsystemOptimize,
onSubtypeChange,
onMinIterations,
onMaxIterations,
} from "@/common/lindenmayer"
import { subtypes } from "./subtypes"
import { resizeVertices, cloneVertices } from "@/common/geometry"
import { buildGraph, edgeKey } from "@/common/Graph"
import { resizeVertices } from "@/common/geometry"

const options = {
subtype: {
Expand Down Expand Up @@ -64,65 +64,13 @@ export default class LSystem extends Shape {
config.angle = Math.PI / 2
}

const curve = lsystemPath(lsystem(config), config)
const path = lsystemOptimize(lsystemPath(lsystem(config), config), config)
const scale = 18.0 // to normalize starting size
const path =
config.shortestPath >= iterations ? this.shortestPath(curve) : curve

return resizeVertices(path, scale, scale)
}

getOptions() {
return options
}

shortestPath(nodes) {
const graph = buildGraph(nodes)
const path = []
const visited = {}

for (let i = 0; i < nodes.length - 1; i++) {
const node1 = nodes[i]
const node2 = nodes[i + 1]
let node1Key = node1.toString()
let edge12Key = edgeKey(node1, node2)

if (visited[edge12Key]) {
const unvisitedNode = this.nearestUnvisitedNode(
i + 1,
nodes,
visited,
graph,
)

if (unvisitedNode != null) {
const shortestSubPath = graph.dijkstraShortestPath(
node1Key,
unvisitedNode.toString(),
)

path.push(...cloneVertices(shortestSubPath.slice(1)))
i = nodes.indexOf(unvisitedNode) - 1
}
} else {
path.push(node2)
visited[edge12Key] = true
}
}

return path
}

nearestUnvisitedNode(nodeIndex, nodes, visited, graph) {
for (let i = nodeIndex; i < nodes.length - 1; i++) {
const node1 = nodes[i]
const node2 = nodes[i + 1]

if (!visited[edgeKey(node1, node2)]) {
return node2
}
}

return null // all nodes visited
}
}
3 changes: 2 additions & 1 deletion src/features/shapes/space_filler/SpaceFiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Shape from "../Shape"
import {
lsystem,
lsystemPath,
lsystemOptimize,
onSubtypeChange,
onMinIterations,
onMaxIterations,
Expand Down Expand Up @@ -86,7 +87,7 @@ export default class SpaceFiller extends Shape {
config.angle = Math.PI / 2
}

let curve = lsystemPath(lsystem(config), config)
let curve = lsystemOptimize(lsystemPath(lsystem(config), config), config)
let scale = 1

if (config.iterationsGrow) {
Expand Down
3 changes: 2 additions & 1 deletion src/features/shapes/space_filler/subtypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export const subtypes = {
9: "--8++++6[+9++++7]--7",
},
angle: Math.PI / 5,
maxIterations: 6,
maxIterations: 5,
shortestPath: 5,
iterationsGrow: (config) => {
return 1 + Math.max(1, 3 / config.iterations)
},
Expand Down

0 comments on commit ff169aa

Please sign in to comment.