Added velocitySelect, some extra checks to the custom commands. Finally settled on a scaling strategy (Stick with the default IP but scale according to a given size (this size should ideally be the screen size openrndr is using on the screen)).

This commit is contained in:
David Diaz 2019-10-26 14:41:22 -06:00
parent 6c06d8ea37
commit adce888367
2 changed files with 36 additions and 37 deletions

View File

@ -18,11 +18,10 @@ fun main() = application {
val c = CompositionDrawer() val c = CompositionDrawer()
c.selectPen(1) c.selectPen(1)
c.circle(Vector2(40.0, 40.0), 30.0) c.circle(Vector2(40.0, 40.0), 30.0)
c.rectangle(75.0, 60.0, 80.0, 200.0) c.rectangle(75.0, 60.0, 80.0, 200.0)
c.lineSegment(Vector2(0.0, 0.0), Vector2(200.0, 200.0)) c.lineSegment(Vector2(0.0, 0.0), Vector2(200.0, 200.0))
c.composition.saveToHPGL(File("output.plt"))
c.composition.saveToHPGL(File("output.plt"), this.width, this.height)
this.application.exit() this.application.exit()
drawer.background(ColorRGBa.WHITE) drawer.background(ColorRGBa.WHITE)

View File

@ -4,44 +4,41 @@ import org.openrndr.math.Vector2
import org.openrndr.shape.* import org.openrndr.shape.*
import java.io.File import java.io.File
fun Composition.saveToHPGL(file: File) { fun Composition.saveToHPGL(file: File, width: Int = 0, height: Int = 0) {
val plot = HPGL().generateFromComposition(this) val plot = HPGL(width = width, height = height).generateFromComposition(this)
file.writeText(plot) file.writeText(plot)
} }
class HPGL(pageSize: PageSize = PageSize.A4) { class HPGL(
private var p1: Vector2 private val p1: Vector2 = Vector2.ZERO,
private var p2: Vector2 private val p2: Vector2 = Vector2.ZERO,
private val resolution = 35 private val width: Int = 0,
private val height: Int = 0
init { ) {
// TODO: Set these according to plotter
when (pageSize) {
PageSize.A3 -> {
p1 = Vector2(0.0, 0.0)
p2 = Vector2(0.0, 0.0)
}
PageSize.A4 -> {
p1 = Vector2(603.0, 521.0)
p2 = Vector2(10603.0, 7721.0)
}
}
}
fun generateFromComposition(composition: Composition): String { fun generateFromComposition(composition: Composition): String {
val result = StringBuilder() val result = StringBuilder()
result.appendln("IN;") // Initialize the plotter result.appendln("IN;") // Initialize the plotter
result.appendln("IP${p1.getCoords()},${p2.getCoords()};") // Setup coordinates
result.appendln("SC0,${p2.div(10.0).x},0,${p2.div(10.0).y};") // Setup coordinates // Setup coordinates
if (p1 != Vector2.ZERO || p2 != Vector2.ZERO) {
result.appendln("IP${p1.getCoords()},${p2.getCoords()};")
}
// Setup scaling
if (width == 0 && height == 0) {
result.appendln("SC;")
} else {
result.appendln("SC0,$width,0,$height;")
}
result.appendln("PA0,0;") // Plot absolutely result.appendln("PA0,0;") // Plot absolutely
composition.root.traverse { stage -> composition.root.traverse { stage ->
if (stage == Stage.Before) { if (stage == Stage.Before) {
when(this) { when (this) {
is ShapeNode -> { is ShapeNode -> {
this.shape.contours.forEach { shape -> for (shape in this.shape.contours) {
val segments = shape.sampleLinear().segments val segments = shape.sampleLinear().segments
segments.forEachIndexed { i, segment -> segments.forEachIndexed { i, segment ->
if (i == 0) { if (i == 0) {
@ -60,11 +57,15 @@ class HPGL(pageSize: PageSize = PageSize.A4) {
is SelectPen -> { is SelectPen -> {
result.appendln("SP${this.penNumber};") result.appendln("SP${this.penNumber};")
} }
is VelocitySelect -> {
result.appendln("VS${this.velocity};")
}
} }
} }
} }
result.appendln("PA0,0") result.appendln("PA0,0;")
return result.toString() return result.toString()
} }
} }
@ -73,11 +74,6 @@ private fun Vector2.getCoords(): String {
return "$x,$y" return "$x,$y"
} }
enum class PageSize {
A4,
A3
}
private enum class Stage { private enum class Stage {
Before, Before,
After After
@ -93,14 +89,18 @@ private fun CompositionNode.traverse(cb: CompositionNode.(stage: Stage) -> Unit)
this.cb(Stage.After) this.cb(Stage.After)
} }
// HPGL Custom Instructions
class SelectPen(val penNumber: Int) : GroupNode() class SelectPen(val penNumber: Int) : GroupNode()
class VelocitySelect(val velocity: Int) : GroupNode()
fun CompositionDrawer.selectPen(pen: Int) { fun CompositionDrawer.selectPen(pen: Int) {
require(pen >= 0) { "The pen can only be 0 or greater." }
this.root.children.add(SelectPen(pen)) this.root.children.add(SelectPen(pen))
} }
fun CompositionDrawer.velocitySelect(velocity: Int) { class VelocitySelect(val velocity: Double) : GroupNode()
fun CompositionDrawer.velocitySelect(velocity: Double) {
require(velocity in 0.0..127.9999) { "Invalid velocity value. The velocity should be a value between 0 and 127.9999" }
this.root.children.add(VelocitySelect(velocity)) this.root.children.add(VelocitySelect(velocity))
} }