]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/gizmo/SplitPointSelectionGizmo.java
Add split point actor to deletables to prevent memory leak
[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                 panel.addDeletable(actor);
65                 
66                 this.listener = new RenderListener() {
67                         @Override
68                         public void preRender() {
69                                 Ray ray = vtkUtil.createMouseRay(getRenderer(), mousePos.x, mousePos.y);
70                                 //ray.dir.add(ray.pos);
71                                 //if (MathTools.intersectLineLine(start, end, ray.pos, ray.dir, pa, pb)) {
72                                 double mu[] = new double[2];
73                                 if (MathTools.intersectStraightStraight(start, dir, ray.pos, ray.dir, pa, pb,mu)) {
74                                         splitPoint = pa;
75                                         if (mu[0] < 0.0)
76                                                 splitPoint = start;
77                                         else if (mu[0] > 1.0)
78                                                 splitPoint = end;
79                                         Vector3d dir = new Vector3d(splitPoint);
80                                         dir.sub(pb);
81                                         double length = dir.length();
82                                         dir.scale(1.0/length);
83                                         AxisAngle4d aa = MathTools.createRotation(MathTools.X_AXIS, dir);
84                                         setRotation(aa);
85                                         setScale(length);
86                                         setPosition(pb);
87                                         actor.SetVisibility(1);
88                                 } else {
89                                         splitPoint = null;
90                                         actor.SetVisibility(0);
91                                 }
92                                 
93                         }
94                         
95                         @Override
96                         public void postRender() {
97                                 
98                         }
99                 };
100                 
101                 this.mouseListener = new MouseMotionListener() {
102                         
103                         @Override
104                         public void mouseMoved(MouseEvent e) {
105                                 mousePos.x = e.getX();
106                                 mousePos.y = e.getY();
107                                 SplitPointSelectionGizmo.this.panel.repaint();
108                         }
109                         
110                         @Override
111                         public void mouseDragged(MouseEvent e) {
112                                 mousePos.x = e.getX();
113                                 mousePos.y = e.getY();
114                                 SplitPointSelectionGizmo.this.panel.repaint();
115                         }
116                 };
117
118         }
119         
120         public void setSplit(Point3d start, Point3d end) {
121                 this.start = start;
122                 this.end = end;
123                 dir = new Vector3d(end);
124                 dir.sub(start);
125         }
126         
127         @Override
128         public void attach(Object renderingPart) {
129                 super.attach(renderingPart);
130                 panel.addListener(listener);
131                 panel.addMouseMotionListener(mouseListener);
132         }
133         
134         @Override
135         public void deattach() {
136                 panel.removeListener(listener);
137                 panel.removeMouseMotionListener(mouseListener);
138                 super.deattach();
139         }
140         
141         @Override
142         public Collection<vtkProp> getGizmo() {
143                 Collection<vtkProp> coll = new ArrayList<vtkProp>();
144                 coll.add(actor);
145                 return coll;
146         }
147         
148         public Tuple3d getSplitPoint() {
149                 return splitPoint;
150         }
151
152 }