]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/gizmo/TerminalSelectionGizmo.java
Allow adding adjustable length components in the middle of a pipe
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / gizmo / TerminalSelectionGizmo.java
1 package org.simantics.plant3d.gizmo;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.Set;
7
8 import javax.vecmath.Point2d;
9 import javax.vecmath.Vector3d;
10
11 import org.simantics.g3d.scenegraph.RenderListener;
12 import org.simantics.g3d.tools.PluginTools;
13 import org.simantics.g3d.vtk.common.VtkView;
14 import org.simantics.g3d.vtk.gizmo.vtkGizmo;
15 import org.simantics.g3d.vtk.utils.vtkUtil;
16 import org.simantics.plant3d.Activator;
17 import org.simantics.plant3d.scenegraph.PipelineComponent;
18 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
19
20 import vtk.vtkCellArray;
21 import vtk.vtkFloatArray;
22 import vtk.vtkPNGReader;
23 import vtk.vtkPoints;
24 import vtk.vtkPolyData;
25 import vtk.vtkPolyDataMapper2D;
26 import vtk.vtkPolygon;
27 import vtk.vtkProp;
28 import vtk.vtkRenderer;
29 import vtk.vtkTexture;
30 import vtk.vtkTexturedActor2D;
31
32 public class TerminalSelectionGizmo extends vtkGizmo {
33         
34
35         
36         vtkTexturedActor2D prevProp;
37         vtkTexturedActor2D nextProp;
38         vtkTexturedActor2D middleProp;
39         
40         boolean showPrev = false;
41         boolean showNext = false;
42         boolean showMiddle = false;
43         
44         Vector3d middle = new Vector3d();
45         Vector3d prev = new Vector3d();
46         Vector3d next = new Vector3d();
47         
48         VtkView panel;
49         
50         private RenderListener listener;
51         public TerminalSelectionGizmo(VtkView panel) {
52                 this.panel = panel;
53                 this.listener = new RenderListener() {
54                         
55                         @Override
56                         public void preRender() {
57
58                                 if (showMiddle) {
59                                         Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), middle);
60                                         middleProp.SetDisplayPosition((int)p.x, (int)p.y);
61                                 }
62                                 if (showPrev) {
63                                         Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), prev);
64                                         prevProp.SetDisplayPosition((int)p.x, (int)p.y);
65                                 }
66                                 if (showNext) {
67                                         Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), next);
68                                         nextProp.SetDisplayPosition((int)p.x, (int)p.y);
69                                 }
70                         }
71                         
72                         @Override
73                         public void postRender() {
74                                 
75                                 
76                         }
77                 };
78         }
79         
80         vtkRenderer getRenderer() {
81                 return panel.getRenderer();
82         }
83         
84         
85         @Override
86         public void attach(VtkView renderingPart) {
87                 if (nextProp == null) {
88                         loadData();
89                         
90                 }
91                 panel.addListener(listener);
92                 
93                 super.attach(renderingPart);
94                 
95         }
96         
97         @Override
98         public Collection<vtkProp> getGizmo() {
99                 List<vtkProp> list = new ArrayList<vtkProp>();
100                 if (showPrev) {
101                         list.add(prevProp);
102                 }
103                 if (showNext) {
104                         list.add(nextProp);
105                         
106                 }
107                 if (showMiddle) {
108                         list.add(middleProp);
109                 }
110                 return list;
111         }
112         
113         protected void attachActors() {
114                 panel.lock();
115                 vtkRenderer ren = getRenderer();
116                 if (showPrev) {
117                         ren.AddActor2D(prevProp);
118                 }
119                 if (showNext) {
120                         ren.AddActor2D(nextProp);
121                 }
122                 if (showMiddle) {
123                         ren.AddActor2D(middleProp);
124                 }
125                 panel.unlock();
126         }
127         
128         @Override
129         protected void deattachActors() {
130                 panel.removeListener(listener);
131                 panel.lock();
132                 vtkRenderer ren = getRenderer();
133                 ren.RemoveActor(prevProp);
134                 ren.RemoveActor(nextProp);
135                 ren.RemoveActor(middleProp);
136                 panel.unlock();
137         }
138         
139         public void setComponent(PipelineComponent component, Set<PositionType> allowed) {
140 //              showPrev = component.getPrevious() == null;
141 //              showNext = component.getNext() == null;
142 //              showMiddle = (component instanceof InlineComponent) && !component.getControlPoint().isFixed();
143                 showPrev = allowed.contains(PositionType.PREVIOUS);
144                 showNext = allowed.contains(PositionType.NEXT);
145                 showMiddle = allowed.contains(PositionType.SPLIT);
146         
147                 middle = component.getControlPoint().getWorldPosition();
148                 component.getControlPoint().getControlPointEnds(prev, next);
149
150         }
151         
152         private void loadData() {
153                 String middleTexFile = PluginTools.getAbsolutePath(Activator.getDefault().getBundle(), "icons/middle.png");
154                 String plusTexFile = PluginTools.getAbsolutePath(Activator.getDefault().getBundle(), "icons/plus.png");
155                 if (middleTexFile == null || plusTexFile == null)
156                         throw new RuntimeException("Cannot resolve required image files.");
157                 
158                 vtkPoints points = new vtkPoints();
159                 double pw = 8;
160                 points.InsertNextPoint(-pw, -pw, 0.0);
161                 points.InsertNextPoint( pw, -pw, 0.0);
162                 points.InsertNextPoint( pw,  pw, 0.0);
163                 points.InsertNextPoint(-pw,  pw, 0.0);
164                 
165                 
166                 vtkCellArray cellArray = new vtkCellArray();
167                 vtkPolygon polygon = new vtkPolygon();
168                 polygon.GetPointIds().SetNumberOfIds(4);
169                 polygon.GetPointIds().SetId(0, 0);
170                 polygon.GetPointIds().SetId(1, 1);
171                 polygon.GetPointIds().SetId(2, 2);
172                 polygon.GetPointIds().SetId(3, 3);
173                 
174                 cellArray.InsertNextCell(polygon);
175                 
176                 vtkPolyData quad = new vtkPolyData();
177                 quad.SetPoints(points);
178                 quad.SetPolys(cellArray);
179                 
180                 vtkFloatArray texCoords = new vtkFloatArray();
181                 
182                 texCoords.SetNumberOfComponents(2);
183                 
184                 texCoords.InsertNextTuple2(0.0, 0.0);
185                 texCoords.InsertNextTuple2(1.0, 0.0);
186                 texCoords.InsertNextTuple2(1.0, 1.0);
187                 texCoords.InsertNextTuple2(0.0, 1.0);
188                 
189                 quad.GetPointData().SetTCoords(texCoords);
190
191                 vtkPNGReader middleReader = new vtkPNGReader();
192                 middleReader.SetFileName(middleTexFile);
193                 
194                 vtkPNGReader plusReader = new vtkPNGReader();
195                 plusReader.SetFileName(plusTexFile);
196                 
197                 vtkTexture middleTex = new vtkTexture();
198                 middleTex.SetInputConnection(middleReader.GetOutputPort());
199                 middleTex.SetInterpolate(1);
200                 
201                 vtkTexture plusTex = new vtkTexture();
202                 plusTex.SetInputConnection(plusReader.GetOutputPort());
203                 plusTex.SetInterpolate(1);
204                 
205                 vtkPolyDataMapper2D mapper = new vtkPolyDataMapper2D();
206                 mapper.SetInputData(quad);
207
208                 nextProp = new vtkTexturedActor2D();
209                 prevProp = new vtkTexturedActor2D();
210                 middleProp = new vtkTexturedActor2D();
211                 
212                 nextProp.SetMapper(mapper);
213                 nextProp.SetTexture(plusTex);
214                 nextProp.SetPickable(1);
215                 
216                 prevProp.SetMapper(mapper);
217                 prevProp.SetTexture(plusTex);
218                 prevProp.SetPickable(1);
219                 
220                 middleProp.SetMapper(mapper);
221                 middleProp.SetTexture(middleTex);
222                 middleProp.SetPickable(1);
223                 
224                 
225                 middleProp.SetWidth(pw);
226                 middleProp.SetHeight(pw);
227                 
228                 prevProp.SetWidth(pw);
229                 prevProp.SetHeight(pw);
230                 
231                 nextProp.SetWidth(pw);
232                 nextProp.SetHeight(pw);
233                 
234                 plusReader.GetOutputPort().Delete();
235                 plusReader.Delete();
236                 middleReader.GetOutputPort().Delete();
237                 middleReader.Delete();
238                 middleTex.Delete();
239                 plusTex.Delete();
240                 
241                 mapper.Delete();
242                 quad.GetPointData().Delete();
243                 quad.Delete();
244                 points.Delete();
245                 polygon.GetPointIds().Delete();
246                 polygon.Delete();
247                 cellArray.Delete();
248                 texCoords.Delete();
249                 
250         }
251         
252         
253         public PositionType getPickedPosition(vtkProp[] picked) {
254                 if (picked == null)
255                         return null;
256                 for (vtkProp p : picked) {
257                         if (p.equals(middleProp))
258                                 return PositionType.SPLIT;
259                         if (p.equals(nextProp))
260                                 return PositionType.NEXT;
261                         if (p.equals(prevProp))
262                                 return PositionType.PREVIOUS;
263                 }
264                 return null;
265         }
266         
267
268 }