]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/gizmo/SplitPointSelectionGizmo.java
336fdbd56fbd2ea3311e82158607cef4d91fdc98
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / gizmo / SplitPointSelectionGizmo.java
1 package org.simantics.plant3d.gizmo;
2
3 import java.awt.event.MouseEvent;
4 import java.awt.event.MouseMotionListener;
5 import java.util.ArrayList;
6 import java.util.Collection;
7
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;
13
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;
25
26 import vtk.vtkProp;
27
28 public class SplitPointSelectionGizmo extends vtkGizmo {
29         
30         MeshActor actor;
31         
32         InteractiveVtkPanel panel;
33         private RenderListener listener;
34         private MouseMotionListener mouseListener;
35         
36         Point3d start;
37         Point3d end;
38         Vector3d dir;
39         
40         Vector2d mousePos = new Vector2d();
41         
42         Vector3d pa = new Vector3d();
43         Vector3d pb = new Vector3d();
44         
45         Tuple3d splitPoint = null;
46         
47         public SplitPointSelectionGizmo(InteractiveVtkPanel panel) {
48                 this.panel = panel;
49                 
50                 int res = 16;
51                 
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));
55                 
56                 Mesh tube_x = Cylinder.create(MathTools.ORIGIN, new Vector3d(0.8,0,0), 0.05, res);
57                 tube_x.add(cone_x);
58                 
59                 Color4d z_col = new Color4d(0,1,0,1);
60                 tube_x.setColor(z_col);
61                 
62                 actor = new MeshActor();
63                 actor.setMesh(tube_x);
64                 
65                 this.listener = new RenderListener() {
66                         @Override
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)) {
73                                         splitPoint = pa;
74                                         if (mu[0] < 0.0)
75                                                 splitPoint = start;
76                                         else if (mu[0] > 1.0)
77                                                 splitPoint = end;
78                                         Vector3d dir = new Vector3d(splitPoint);
79                                         dir.sub(pb);
80                                         double length = dir.length();
81                                         dir.scale(1.0/length);
82                                         AxisAngle4d aa = MathTools.createRotation(MathTools.X_AXIS, dir);
83                                         setRotation(aa);
84                                         setScale(length);
85                                         setPosition(pb);
86                                         actor.SetVisibility(1);
87                                 } else {
88                                         splitPoint = null;
89                                         actor.SetVisibility(0);
90                                 }
91                                 
92                         }
93                         
94                         @Override
95                         public void postRender() {
96                                 
97                         }
98                 };
99                 
100                 this.mouseListener = new MouseMotionListener() {
101                         
102                         @Override
103                         public void mouseMoved(MouseEvent e) {
104                                 mousePos.x = e.getX();
105                                 mousePos.y = e.getY();
106                                 SplitPointSelectionGizmo.this.panel.repaint();
107                         }
108                         
109                         @Override
110                         public void mouseDragged(MouseEvent e) {
111                                 mousePos.x = e.getX();
112                                 mousePos.y = e.getY();
113                                 SplitPointSelectionGizmo.this.panel.repaint();
114                         }
115                 };
116
117         }
118         
119         public void setSplit(Point3d start, Point3d end) {
120                 this.start = start;
121                 this.end = end;
122                 dir = new Vector3d(end);
123                 dir.sub(start);
124         }
125         
126         @Override
127         public void attach(Object renderingPart) {
128                 super.attach(renderingPart);
129                 panel.addListener(listener);
130                 panel.addMouseMotionListener(mouseListener);
131         }
132         
133         @Override
134         public void deattach() {
135                 panel.removeListener(listener);
136                 panel.removeMouseMotionListener(mouseListener);
137                 super.deattach();
138         }
139         
140         @Override
141         public Collection<vtkProp> getGizmo() {
142                 Collection<vtkProp> coll = new ArrayList<vtkProp>();
143                 coll.add(actor);
144                 return coll;
145         }
146         
147         public Tuple3d getSplitPoint() {
148                 return splitPoint;
149         }
150
151 }