]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/gizmo/SplitPointSelectionGizmo.java
Add units to ComponentSelectionDialog
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / gizmo / SplitPointSelectionGizmo.java
1 package org.simantics.plant3d.gizmo;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import javax.vecmath.AxisAngle4d;
7 import javax.vecmath.Point3d;
8 import javax.vecmath.Tuple3d;
9 import javax.vecmath.Vector2d;
10 import javax.vecmath.Vector3d;
11
12 import org.eclipse.swt.events.MouseMoveListener;
13 import org.simantics.g3d.math.MathTools;
14 import org.simantics.g3d.math.Ray;
15 import org.simantics.g3d.scenegraph.RenderListener;
16 import org.simantics.g3d.shape.Color4d;
17 import org.simantics.g3d.shape.Cone;
18 import org.simantics.g3d.shape.Cylinder;
19 import org.simantics.g3d.shape.Mesh;
20 import org.simantics.g3d.vtk.common.VtkView;
21 import org.simantics.g3d.vtk.gizmo.vtkGizmo;
22 import org.simantics.g3d.vtk.shape.MeshActor;
23 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
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         VtkView panel;
33         private RenderListener listener;
34         //private MouseMotionListener mouseListener;
35         private MouseMoveListener mouseListener;
36         
37         Point3d start;
38         Point3d end;
39         Vector3d dir;
40         
41         Vector2d mousePos = new Vector2d();
42         
43         Vector3d pa = new Vector3d();
44         Vector3d pb = new Vector3d();
45         
46         Tuple3d splitPoint = null;
47         
48         public SplitPointSelectionGizmo(VtkView panel) {
49                 this.panel = panel;
50                 
51                 int res = 16;
52                 
53                 Mesh cone_x = Cone.create(0.1, res);
54                 cone_x.rotate(MathTools.getQuat(new AxisAngle4d(0,0,-1,Math.PI*0.5)));
55                 cone_x.translate(new Vector3d(0.8,0,0));
56                 
57                 Mesh tube_x = Cylinder.create(MathTools.ORIGIN, new Vector3d(0.8,0,0), 0.05, res);
58                 tube_x.add(cone_x);
59                 
60                 Color4d z_col = new Color4d(0,1,0,1);
61                 tube_x.setColor(z_col);
62                 
63                 actor = new MeshActor();
64                 actor.setMesh(tube_x);
65                 panel.addDeletable(actor);
66                 
67                 this.listener = new RenderListener() {
68                         @Override
69                         public void preRender() {
70                                 Ray ray = vtkUtil.createMouseRay(SplitPointSelectionGizmo.this.panel.getRenderer(), mousePos.x, mousePos.y);
71                                 //ray.dir.add(ray.pos);
72                                 //if (MathTools.intersectLineLine(start, end, ray.pos, ray.dir, pa, pb)) {
73                                 double mu[] = new double[2];
74                                 if (MathTools.intersectStraightStraight(start, dir, ray.pos, ray.dir, pa, pb,mu)) {
75                                         splitPoint = pa;
76                                         if (mu[0] < 0.0)
77                                                 splitPoint = start;
78                                         else if (mu[0] > 1.0)
79                                                 splitPoint = end;
80                                         Vector3d dir = new Vector3d(splitPoint);
81                                         dir.sub(pb);
82                                         double length = dir.length();
83                                         dir.scale(1.0/length);
84                                         AxisAngle4d aa = MathTools.createRotation(MathTools.X_AXIS, dir);
85                                         setRotation(aa);
86                                         setScale(length);
87                                         setPosition(pb);
88                                         actor.SetVisibility(1);
89                                 } else {
90                                         splitPoint = null;
91                                         actor.SetVisibility(0);
92                                 }
93                                 
94                         }
95                         
96                         @Override
97                         public void postRender() {
98                                 
99                         }
100                 };
101                 
102 //              this.mouseListener = new MouseMotionListener() {
103 //                      
104 //                      @Override
105 //                      public void mouseMoved(MouseEvent e) {
106 //                              mousePos.x = e.getX();
107 //                              mousePos.y = e.getY();
108 //                              SplitPointSelectionGizmo.this.panel.refresh();
109 //                      }
110 //                      
111 //                      @Override
112 //                      public void mouseDragged(MouseEvent e) {
113 //                              mousePos.x = e.getX();
114 //                              mousePos.y = e.getY();
115 //                              SplitPointSelectionGizmo.this.panel.refresh();
116 //                      }
117 //              };
118                 this.mouseListener = new MouseMoveListener() {
119                         
120                         @Override
121                         public void mouseMove(org.eclipse.swt.events.MouseEvent e) {
122                                 mousePos.x = e.x;
123                                 mousePos.y = e.y;
124                                 SplitPointSelectionGizmo.this.panel.refresh();
125                                 
126                         }
127                 };
128
129         }
130         
131         public void setSplit(Point3d start, Point3d end) {
132                 this.start = start;
133                 this.end = end;
134                 dir = new Vector3d(end);
135                 dir.sub(start);
136         }
137         
138         @Override
139         public void attach(VtkView renderingPart) {
140                 super.attach(renderingPart);
141                 panel.addListener(listener);
142                 // FIXME
143                 //panel.addMouseMotionListener(mouseListener);
144                 ((InteractiveVtkComposite)panel).getComponent().addMouseMoveListener(mouseListener);
145         }
146         
147         @Override
148         public void deattach() {
149                 panel.removeListener(listener);
150                 // FIXME
151                 //panel.removeMouseMotionListener(mouseListener);
152                 super.deattach();
153         }
154         
155         @Override
156         public Collection<vtkProp> getGizmo() {
157                 Collection<vtkProp> coll = new ArrayList<vtkProp>();
158                 coll.add(actor);
159                 return coll;
160         }
161         
162         public Tuple3d getSplitPoint() {
163                 return splitPoint;
164         }
165
166 }