1 package org.simantics.plant3d.gizmo;
3 import java.util.ArrayList;
4 import java.util.Collection;
8 import javax.vecmath.Point2d;
9 import javax.vecmath.Vector3d;
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;
20 import vtk.vtkCellArray;
21 import vtk.vtkFloatArray;
22 import vtk.vtkPNGReader;
24 import vtk.vtkPolyData;
25 import vtk.vtkPolyDataMapper2D;
26 import vtk.vtkPolygon;
28 import vtk.vtkRenderer;
29 import vtk.vtkTexture;
30 import vtk.vtkTexturedActor2D;
32 public class TerminalSelectionGizmo extends vtkGizmo {
36 vtkTexturedActor2D prevProp;
37 vtkTexturedActor2D nextProp;
38 vtkTexturedActor2D middleProp;
40 boolean showPrev = false;
41 boolean showNext = false;
42 boolean showMiddle = false;
44 Vector3d middle = new Vector3d();
45 Vector3d prev = new Vector3d();
46 Vector3d next = new Vector3d();
50 private RenderListener listener;
51 public TerminalSelectionGizmo(VtkView panel) {
53 this.listener = new RenderListener() {
56 public void preRender() {
59 Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), middle);
60 middleProp.SetDisplayPosition((int)p.x, (int)p.y);
63 Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), prev);
64 prevProp.SetDisplayPosition((int)p.x, (int)p.y);
67 Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), next);
68 nextProp.SetDisplayPosition((int)p.x, (int)p.y);
73 public void postRender() {
80 vtkRenderer getRenderer() {
81 return panel.getRenderer();
86 public void attach(VtkView renderingPart) {
87 if (nextProp == null) {
91 panel.addListener(listener);
93 super.attach(renderingPart);
98 public Collection<vtkProp> getGizmo() {
99 List<vtkProp> list = new ArrayList<vtkProp>();
108 list.add(middleProp);
113 protected void attachActors() {
115 vtkRenderer ren = getRenderer();
117 ren.AddActor2D(prevProp);
120 ren.AddActor2D(nextProp);
123 ren.AddActor2D(middleProp);
129 protected void deattachActors() {
130 panel.removeListener(listener);
132 vtkRenderer ren = getRenderer();
133 ren.RemoveActor(prevProp);
134 ren.RemoveActor(nextProp);
135 ren.RemoveActor(middleProp);
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);
147 middle = component.getControlPoint().getWorldPosition();
148 component.getControlPoint().getControlPointEnds(prev, next);
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.");
158 vtkPoints points = new vtkPoints();
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);
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);
174 cellArray.InsertNextCell(polygon);
176 vtkPolyData quad = new vtkPolyData();
177 quad.SetPoints(points);
178 quad.SetPolys(cellArray);
180 vtkFloatArray texCoords = new vtkFloatArray();
182 texCoords.SetNumberOfComponents(2);
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);
189 quad.GetPointData().SetTCoords(texCoords);
191 vtkPNGReader middleReader = new vtkPNGReader();
192 middleReader.SetFileName(middleTexFile);
194 vtkPNGReader plusReader = new vtkPNGReader();
195 plusReader.SetFileName(plusTexFile);
197 vtkTexture middleTex = new vtkTexture();
198 middleTex.SetInputConnection(middleReader.GetOutputPort());
199 middleTex.SetInterpolate(1);
201 vtkTexture plusTex = new vtkTexture();
202 plusTex.SetInputConnection(plusReader.GetOutputPort());
203 plusTex.SetInterpolate(1);
205 vtkPolyDataMapper2D mapper = new vtkPolyDataMapper2D();
206 mapper.SetInputData(quad);
208 nextProp = new vtkTexturedActor2D();
209 prevProp = new vtkTexturedActor2D();
210 middleProp = new vtkTexturedActor2D();
212 nextProp.SetMapper(mapper);
213 nextProp.SetTexture(plusTex);
214 nextProp.SetPickable(1);
216 prevProp.SetMapper(mapper);
217 prevProp.SetTexture(plusTex);
218 prevProp.SetPickable(1);
220 middleProp.SetMapper(mapper);
221 middleProp.SetTexture(middleTex);
222 middleProp.SetPickable(1);
225 middleProp.SetWidth(pw);
226 middleProp.SetHeight(pw);
228 prevProp.SetWidth(pw);
229 prevProp.SetHeight(pw);
231 nextProp.SetWidth(pw);
232 nextProp.SetHeight(pw);
234 plusReader.GetOutputPort().Delete();
236 middleReader.GetOutputPort().Delete();
237 middleReader.Delete();
242 quad.GetPointData().Delete();
245 polygon.GetPointIds().Delete();
253 public PositionType getPickedPosition(vtkProp[] picked) {
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;