Skip to content

Commit

Permalink
Merge pull request #8 from mheine94/master
Browse files Browse the repository at this point in the history
Visualisation Tool
  • Loading branch information
Michel Steuwer committed Jun 5, 2018
2 parents bfb55f0 + 72ccdce commit 38bd9a8
Show file tree
Hide file tree
Showing 19 changed files with 2,463 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[submodule "lib/ArithExpr"]
path = lib/ArithExpr
url = https://github.com/lift-project/arithexpr.git


3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ libraryDependencies += "org.clapper" %% "argot" % "1.0.3"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.7"
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.4.0"

// https://mvnrepository.com/artifact/org.jfree/jfreesvg
libraryDependencies += "org.jfree" % "jfreesvg" % "2.0"

// Time utilities
libraryDependencies += "com.github.nscala-time" %% "nscala-time" % "2.16.0"

Expand Down
22 changes: 20 additions & 2 deletions src/main/ir/ast/PrintType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@ package ir.ast

import ir.Type
import ir.interpreter.Interpreter._
import utils.paternoster.visualisation.TypeVisualiser


/**
* Small datatype to differetiate between textual or visual output
*/
sealed trait TypeOutput

case class VisualOutput(render: Boolean = false, expr: String = "") extends TypeOutput

case object TextOutput extends TypeOutput

/**
* A pattern for debugging Lift code.
* Identity function that prints the Lift type of its input.
* Generates no OpenCL code.
*/
case class PrintType() extends Pattern(arity = 1) {
case class PrintType(outputType: TypeOutput = TextOutput) extends Pattern(arity = 1) {
override def checkType(argType: Type,
setType: Boolean): Type = {
println(argType.toString)
outputType match {
case v: VisualOutput => {
TypeVisualiser(argType, v.render, v.expr)
}
case TextOutput => println(argType.toString)
}

argType
}


override def eval(valueMap: ValueMap, args: Any*): Any = {
args.head
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/ir/view/InputView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object InputView {
case Pad(left, right,boundary) => buildViewPad(left, right, boundary, argView)
case PadConstant(left, right, value) => buildViewPadConstant(left, right, value, argView)
case ArrayAccess(i) => argView.access(i)
case PrintType() | Scatter(_) | _: Tuple | Pad(_, _, _) | Id() => argView
case PrintType(_) | Scatter(_) | _: Tuple | Pad(_, _, _) | Id() => argView
case dunno => throw new NotImplementedError(s"inputView.scala: $dunno")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/ir/view/OutputView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object OutputView {
View.initialiseNewView(call.args.head.t, call.args.head.inputDepth, call.args.head.mem.variable)
case _: ArrayAccess | _: UnsafeArrayAccess | _ : CheckedArrayAccess =>
View.initialiseNewView(call.args.head.t, call.args.head.inputDepth, call.args.head.mem.variable)
case PrintType() | Get(_) | _: Tuple | Gather(_) | Filter() |
case PrintType(_) | Get(_) | _: Tuple | Gather(_) | Filter() |
Pad(_, _, _) | PadConstant(_, _, _) | Id() =>
writeView
case dunno => throw new NotImplementedError(s"OutputView.scala: $dunno")
Expand Down
2 changes: 1 addition & 1 deletion src/main/opencl/generator/OpenCLGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class OpenCLGenerator extends Generator {
case Unzip() | Transpose() | TransposeW() | asVector(_) | asScalar() |
Split(_) | Join() | Slide(_, _) | Zip(_) | Tuple(_) | Filter() |
Head() | Tail() | Scatter(_) | Gather(_) | Get(_) | Pad(_, _, _) | PadConstant(_, _, _) |
ArrayAccess(_) | PrintType() =>
ArrayAccess(_) | PrintType(_) =>
case _ => (block: MutableBlock) += Comment("__" + call.toString + "__")
}
case v: Value => generateValue(v, block)
Expand Down
2 changes: 1 addition & 1 deletion src/main/opencl/ir/InferOpenCLAddressSpace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object InferOpenCLAddressSpace {

case Unzip() | Zip(_) | Transpose() | TransposeW() | asVector(_) |
asScalar() | Split(_) | Join() | Scatter(_) | Gather(_) |
Pad(_,_,_) | PadConstant(_, _, _) | Tuple(_) | Slide(_,_) | Head() | Tail() | PrintType() |
Pad(_,_,_) | PadConstant(_, _, _) | Tuple(_) | Slide(_,_) | Head() | Tail() | PrintType(_) |
UnsafeArrayAccess(_) | CheckedArrayAccess(_) | ArrayAccess(_) | Id() =>

setAddressSpaceDefault(addressSpaces)
Expand Down
2 changes: 1 addition & 1 deletion src/main/opencl/ir/OpenCLMemoryAllocator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ object OpenCLMemoryAllocator {

case Split(_) | Join() | asVector(_) | asScalar() |
Transpose() | Unzip() | TransposeW() | Slide(_, _) | Pad(_, _, _) | PadConstant(_, _, _) |
Head() | Tail() | Gather(_) | Scatter(_) | ArrayAccess(_) | PrintType() | Id() =>
Head() | Tail() | Gather(_) | Scatter(_) | ArrayAccess(_) | PrintType(_) | Id() =>
inMem
}
}
Expand Down
129 changes: 129 additions & 0 deletions src/main/utils/paternoster/gui/MainPane.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package utils.paternoster.gui

import javafx.scene.canvas.{Canvas, GraphicsContext}
import javafx.scene.image.WritableImage
import javafx.scene.layout.Pane
import javafx.scene.text.{Font, Text}

import org.jfree.graphics2d.svg.SVGGraphics2D
import utils.paternoster.rendering.Graphics.GraphicalPrimitive
import utils.paternoster.rendering.{JavaFXRenderer, SVGRenderer}



/**
* Custon Pane class for painting of the visualisation.
*/
class MainPane(val width:Int, val height:Int) extends Pane {
//General scaling
var unitX = 6d
var unitY = 6d
//Used to separate things
val smallX = 1
val smallY = 1
val canvas = new Canvas(width,height)
//canvas.setScaleX(0.5)
//canvas.setScaleY(0.5)
this.getChildren.add(canvas)

/**
* Draws all primitives to the canvas.
* @param primitives The primitives that will be drawn.
*/
def draw(primitives:Iterable[GraphicalPrimitive]) = {
val gc = this.canvas.getGraphicsContext2D
val context = JavaFXRenderer.Context(gc, unitX, unitY, smallX, smallY, getNumberFontFx() , getExpressionFontFx() ,width.toDouble,height.toDouble)
JavaFXRenderer.drawPrimitives(primitives, context)
}

/**
* Draws all primitives to an svg file.
* @param primitives The primitives that will be drawn.
* @return The svg content.
*/
def renderToSvg(primitives:Iterable[GraphicalPrimitive],visualisationDimensions : (Double,Double)): String ={

var g2 = new SVGGraphics2D(visualisationDimensions._1.toInt*unitX.toInt,visualisationDimensions._2.toInt*unitY.toInt);
var context = SVGRenderer.Context(g2, unitX, unitY, smallX, smallY, getNumberFontAwt() , getExpressionFontAwt() ,width.toDouble,height.toDouble)
SVGRenderer.drawPrimitives(primitives,context)
var svgOuptut = g2.getSVGElement()
svgOuptut
}

/**
* Helper method that returns the height of a rendered String.
* @param str The text.
* @param font The font of the rendering.
* @return The height of the text in the given font.
*/
def getStringHeight(str:String,font: javafx.scene.text.Font): Double ={
var text = new Text(str)
text.setFont(font)
var textHeight = text.getLayoutBounds().getHeight / unitY
textHeight
}

/**
* Number - Awt font getter.
* @return The awt font for numbers.
*/
def getNumberFontAwt():java.awt.Font={
new java.awt.Font(canvas.getGraphicsContext2D.getFont.getName,java.awt.Font.PLAIN,10)
}

/**
* Expression - Awt font getter.
* @return The awt font for source code.
*/
def getExpressionFontAwt() :java.awt.Font={
new java.awt.Font(canvas.getGraphicsContext2D.getFont.getName,java.awt.Font.PLAIN,15)
}

/**
* Number - JavaFx font getter.
* @return The JavaFx font for numbers.
*/
def getNumberFontFx():Font={
new Font(canvas.getGraphicsContext2D.getFont.getName,10)
}

/**
* Expression - JavaFx font getter.
* @return The JavaFx font for source code.
*/
def getExpressionFontFx() :Font={
new Font(canvas.getGraphicsContext2D.getFont.getName,15)
}

/**
* Creates a snapshot of the canvas and writes it to the given WritableImage.
* @param wim The WritableImage that the snapshot will be drawn to.
*/
def getSnapShot(wim: WritableImage): Unit ={
canvas.snapshot(null,wim)
}

/**
* Getter for the graphicsContext.
* @return The context.
*/
def getGraphicsContext(): GraphicsContext ={
canvas.getGraphicsContext2D
}

/**
* Setter for the canvas width.
* @param width The new width.
*/
def setCanvasWidth(width:Double):Unit={
this.canvas.setWidth(width)
}

/**
* Setter for the canvas height.
* @param height The new height.
*/
def setCanvasHeight(height:Double):Unit={
this.canvas.setHeight(height)
}
}

0 comments on commit 38bd9a8

Please sign in to comment.