//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** @author Alekhya Chennupati, John Miller * @version 1.2 * @date May 15 11:58:34 EDT 2015 * @see LICENSE (MIT style license file). */ package scalation.scala3d import javafx.scene._ import javafx.application.Application import javafx.event.{Event, EventHandler, EventTarget} import javafx.scene.input.MouseEvent import javafx.scene.paint.{Color, PhongMaterial} import javafx.css.Styleable import math.{acos, toDegrees} //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** The `Line3D` class is used to create lines in 3D. * @param p1 the first endpoint of the line * @param p2 the second endpoint of the line */ class Line3D (p1: Point3D, p2: Point3D) extends Cylinder (1.0, p1.distance (p2)) { private val DEBUG = true if (DEBUG) println(s"p1 = $p1, p2 = $p2") private val y_axis = Point3D (0.0, 1.0, 0.0) private val translation = Translate (p2 midpoint p1) private val rot_axis = (p2 subtract p1).crossProduct (y_axis) private val rot_angle = acos (p2.subtract (p1).normalize ().dotProduct (y_axis)) val rotation = Rotate (-toDegrees (rot_angle), rot_axis) getTransforms ().add (translation) getTransforms ().add (rotation) } // Line3D class //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** The `Line3Dep` class is used to create lines in 3D with endpoints (ep). * @param p1 the first endpoint of the line * @param p2 the second endpoint of the line * @param sz the size/radius of the endpoints */ class Line3Dep (p1: Point3D, p2: Point3D, sz: Double = 3.0) extends Line3D (p1, p2) { private val sphere1 = new Sphere (sz) private val sphere2 = new Sphere (sz) sphere1.getTransforms ().add (Translate (p1)) sphere2.getTransforms ().add (Translate (p2)) //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** Get the two spheres serving as endpoints for the line. */ def getSpheres = (sphere1, sphere2) } // Line3Dep class //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** The `Line3DHelper` class is used for displaying a line in 3D. */ case class Line3DHelper () extends javafx.application.Application () { private val root = new Group () private val camera = new Camera3D () private var anchorX = 0.0 private var anchorY = 0.0 private var anchorAngle = 0.0 def mkEventHandler [E <: Event] (f: E => Unit) = new EventHandler [E] { def handle (e: E) = f(e) } override def start (primaryStage: Stage) { println ("Line3DHelper: start") val line = new Line3Dep (Point3D.ZERO, Point3D (50.0, -100.0, 50.0), 2.0) val s = line.getSpheres root.getChildren ().addAll (new Group (line), new Group (s._1), new Group (s._2)) try { val scene = new Scene (root, 1024, 768, true) scene.setOnMousePressed (mkEventHandler ((event: MouseEvent) => { anchorX = event.getSceneX () anchorY = event.getSceneY () anchorAngle = camera.getRotate () })) scene.setOnMouseDragged (mkEventHandler ((event: MouseEvent) => { camera.setRotate (anchorAngle + anchorX - event.getSceneX ()) })) scene.setFill (paint.Color.YELLOW) primaryStage.setTitle ("scala3d Line3Dep demo") primaryStage.setScene (scene) primaryStage.show () scene.setCamera (camera) } catch { case e: Exception => e.printStackTrace () } // try } // start } // Line3DHelper class //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** The `Line3DTest` class is used to test the `Line3D` and `Line3Dep` classes. * run-main scalation.scala3d.Line3DTest */ object Line3DTest { def main (args: Array [String]) { javafx.application.Application.launch (classOf [Line3DHelper], args: _*) } // main } // Line3DTest object