1 package org.simantics.plant3d.gizmo;
3 import java.awt.event.MouseEvent;
4 import java.awt.event.MouseMotionListener;
5 import java.util.ArrayList;
6 import java.util.Collection;
8 import javax.vecmath.AxisAngle4d;
9 import javax.vecmath.Point3d;
10 import javax.vecmath.Tuple3d;
11 import javax.vecmath.Vector2d;
12 import javax.vecmath.Vector3d;
14 import org.simantics.g3d.math.MathTools;
15 import org.simantics.g3d.math.Ray;
16 import org.simantics.g3d.scenegraph.RenderListener;
17 import org.simantics.g3d.shape.Color4d;
18 import org.simantics.g3d.shape.Cone;
19 import org.simantics.g3d.shape.Cylinder;
20 import org.simantics.g3d.shape.Mesh;
21 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;
22 import org.simantics.g3d.vtk.gizmo.vtkGizmo;
23 import org.simantics.g3d.vtk.shape.MeshActor;
24 import org.simantics.g3d.vtk.utils.vtkUtil;
28 public class SplitPointSelectionGizmo extends vtkGizmo {
32 InteractiveVtkPanel panel;
33 private RenderListener listener;
34 private MouseMotionListener mouseListener;
40 Vector2d mousePos = new Vector2d();
42 Vector3d pa = new Vector3d();
43 Vector3d pb = new Vector3d();
45 Tuple3d splitPoint = null;
47 public SplitPointSelectionGizmo(InteractiveVtkPanel panel) {
52 Mesh cone_x = Cone.create(0.1, res);
53 cone_x.rotate(MathTools.getQuat(new AxisAngle4d(0,0,-1,Math.PI*0.5)));
54 cone_x.translate(new Vector3d(0.8,0,0));
56 Mesh tube_x = Cylinder.create(MathTools.ORIGIN, new Vector3d(0.8,0,0), 0.05, res);
59 Color4d z_col = new Color4d(0,1,0,1);
60 tube_x.setColor(z_col);
62 actor = new MeshActor();
63 actor.setMesh(tube_x);
65 this.listener = new RenderListener() {
67 public void preRender() {
68 Ray ray = vtkUtil.createMouseRay(getRenderer(), mousePos.x, mousePos.y);
69 //ray.dir.add(ray.pos);
70 //if (MathTools.intersectLineLine(start, end, ray.pos, ray.dir, pa, pb)) {
71 double mu[] = new double[2];
72 if (MathTools.intersectStraightStraight(start, dir, ray.pos, ray.dir, pa, pb,mu)) {
78 Vector3d dir = new Vector3d(splitPoint);
80 double length = dir.length();
81 dir.scale(1.0/length);
82 AxisAngle4d aa = MathTools.createRotation(MathTools.X_AXIS, dir);
86 actor.SetVisibility(1);
89 actor.SetVisibility(0);
95 public void postRender() {
100 this.mouseListener = new MouseMotionListener() {
103 public void mouseMoved(MouseEvent e) {
104 mousePos.x = e.getX();
105 mousePos.y = e.getY();
106 SplitPointSelectionGizmo.this.panel.repaint();
110 public void mouseDragged(MouseEvent e) {
111 mousePos.x = e.getX();
112 mousePos.y = e.getY();
113 SplitPointSelectionGizmo.this.panel.repaint();
119 public void setSplit(Point3d start, Point3d end) {
122 dir = new Vector3d(end);
127 public void attach(Object renderingPart) {
128 super.attach(renderingPart);
129 panel.addListener(listener);
130 panel.addMouseMotionListener(mouseListener);
134 public void deattach() {
135 panel.removeListener(listener);
136 panel.removeMouseMotionListener(mouseListener);
141 public Collection<vtkProp> getGizmo() {
142 Collection<vtkProp> coll = new ArrayList<vtkProp>();
147 public Tuple3d getSplitPoint() {