1 package org.simantics.plant3d.gizmo;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import javax.vecmath.Point2d;
8 import javax.vecmath.Vector3d;
10 import org.simantics.g3d.scenegraph.RenderListener;
11 import org.simantics.g3d.tools.PluginTools;
12 import org.simantics.g3d.vtk.common.VtkView;
13 import org.simantics.g3d.vtk.gizmo.vtkGizmo;
14 import org.simantics.g3d.vtk.utils.vtkUtil;
15 import org.simantics.plant3d.Activator;
17 import vtk.vtkCellArray;
18 import vtk.vtkFloatArray;
19 import vtk.vtkPNGReader;
21 import vtk.vtkPolyData;
22 import vtk.vtkPolyDataMapper2D;
23 import vtk.vtkPolygon;
25 import vtk.vtkRenderer;
26 import vtk.vtkTexture;
27 import vtk.vtkTexturedActor2D;
29 public class ConstraintPointGizmo extends vtkGizmo {
31 List<vtkTexturedActor2D> props;
32 List<Vector3d> positions;
36 private RenderListener listener;
38 public ConstraintPointGizmo(VtkView panel) {
40 props = new ArrayList<>();
41 positions = new ArrayList<>();
43 this.listener = new RenderListener() {
46 public void preRender() {
47 for (int i = 0; i < positions.size(); i++) {
48 Vector3d pos = positions.get(i);
49 vtkTexturedActor2D prop = props.get(i);
50 Point2d p = vtkUtil.getScreenCoordinates(getRenderer(), pos);
51 prop.SetDisplayPosition((int)p.x, (int)p.y);
56 public void postRender() {
64 vtkRenderer getRenderer() {
65 return panel.getRenderer();
70 public void attach(VtkView renderingPart) {
72 panel.addListener(listener);
74 super.attach(renderingPart);
79 public Collection<vtkProp> getGizmo() {
80 List<vtkProp> list = new ArrayList<vtkProp>();
81 for (vtkTexturedActor2D p : props) {
87 protected void attachActors() {
89 vtkRenderer ren = getRenderer();
90 while (props.size() < positions.size())
91 props.add(createActor());
92 for (int i = 0; i < positions.size(); i++) {
93 vtkTexturedActor2D p = props.get(i);
100 protected void deattachActors() {
101 panel.removeListener(listener);
103 vtkRenderer ren = getRenderer();
104 for (vtkTexturedActor2D p : props) {
105 ren.RemoveActor2D(p);
111 public void addPosition(Vector3d position) {
112 positions.add(position);
115 public void clearPositions() {
119 vtkPolyDataMapper2D mapper;
124 private vtkTexturedActor2D createActor() {
125 if (mapper == null) {
128 vtkTexturedActor2D nextProp = new vtkTexturedActor2D();
130 nextProp.SetMapper(mapper);
131 nextProp.SetTexture(plusTex);
132 nextProp.SetPickable(0);
133 nextProp.SetWidth(pw);
134 nextProp.SetHeight(pw);
135 nextProp.GetProperty().SetColor(new double[] {255.0,0.0,255.0});
136 nextProp.GetProperty().Delete();
138 panel.addDeletable(nextProp);
143 public void dispose() {
148 private void loadData() {
149 String plusTexFile = PluginTools.getAbsolutePath(Activator.getDefault().getBundle(), "icons/crosshair.png");
150 if (plusTexFile == null)
151 throw new RuntimeException("Cannot resolve required image files.");
153 vtkPoints points = new vtkPoints();
154 points.InsertNextPoint(-pw, -pw, 0.0);
155 points.InsertNextPoint( pw, -pw, 0.0);
156 points.InsertNextPoint( pw, pw, 0.0);
157 points.InsertNextPoint(-pw, pw, 0.0);
160 vtkCellArray cellArray = new vtkCellArray();
161 vtkPolygon polygon = new vtkPolygon();
162 polygon.GetPointIds().SetNumberOfIds(4);
163 polygon.GetPointIds().SetId(0, 0);
164 polygon.GetPointIds().SetId(1, 1);
165 polygon.GetPointIds().SetId(2, 2);
166 polygon.GetPointIds().SetId(3, 3);
168 cellArray.InsertNextCell(polygon);
170 vtkPolyData quad = new vtkPolyData();
171 quad.SetPoints(points);
172 quad.SetPolys(cellArray);
174 vtkFloatArray texCoords = new vtkFloatArray();
176 texCoords.SetNumberOfComponents(2);
178 texCoords.InsertNextTuple2(0.0, 0.0);
179 texCoords.InsertNextTuple2(1.0, 0.0);
180 texCoords.InsertNextTuple2(1.0, 1.0);
181 texCoords.InsertNextTuple2(0.0, 1.0);
183 quad.GetPointData().SetTCoords(texCoords);
186 vtkPNGReader plusReader = new vtkPNGReader();
187 plusReader.SetFileName(plusTexFile);
189 plusTex = new vtkTexture();
190 plusTex.SetInputConnection(plusReader.GetOutputPort());
191 plusTex.SetInterpolate(1);
193 mapper = new vtkPolyDataMapper2D();
194 mapper.SetInputData(quad);
196 panel.addDeletable(mapper);
197 panel.addDeletable(plusTex);
200 plusReader.GetOutputPort().Delete();
202 quad.GetPointData().Delete();
205 polygon.GetPointIds().Delete();