]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/gizmo/ConstraintPointGizmo.java
Add units to ComponentSelectionDialog
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / gizmo / ConstraintPointGizmo.java
1 package org.simantics.plant3d.gizmo;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
7 import javax.vecmath.Point2d;
8 import javax.vecmath.Vector3d;
9
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;
16
17 import vtk.vtkCellArray;
18 import vtk.vtkFloatArray;
19 import vtk.vtkPNGReader;
20 import vtk.vtkPoints;
21 import vtk.vtkPolyData;
22 import vtk.vtkPolyDataMapper2D;
23 import vtk.vtkPolygon;
24 import vtk.vtkProp;
25 import vtk.vtkRenderer;
26 import vtk.vtkTexture;
27 import vtk.vtkTexturedActor2D;
28
29 public class ConstraintPointGizmo extends vtkGizmo {
30         
31     List<vtkTexturedActor2D> props;
32     List<Vector3d> positions;
33     
34     VtkView panel;
35     
36     private RenderListener listener;
37     
38     public ConstraintPointGizmo(VtkView panel) {
39         this.panel = panel;
40         props = new ArrayList<>();
41         positions = new ArrayList<>();
42         
43         this.listener = new RenderListener() {
44             
45             @Override
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);
52                 }
53             }
54             
55             @Override
56             public void postRender() {
57                 
58                 
59             }
60         };
61         
62     }
63     
64     vtkRenderer getRenderer() {
65         return panel.getRenderer();
66     }
67     
68     
69     @Override
70     public void attach(VtkView renderingPart) {
71         
72         panel.addListener(listener);
73         
74         super.attach(renderingPart);
75         
76     }
77     
78     @Override
79     public Collection<vtkProp> getGizmo() {
80         List<vtkProp> list = new ArrayList<vtkProp>();
81         for (vtkTexturedActor2D p : props) {
82             list.add(p);
83         }
84         return list;
85     }
86     
87     protected void attachActors() {
88         panel.lock();
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);
94             ren.AddActor2D(p);
95         }
96         panel.unlock();
97     }
98     
99     @Override
100     protected void deattachActors() {
101         panel.removeListener(listener);
102         panel.lock();
103         vtkRenderer ren = getRenderer();
104         for (vtkTexturedActor2D p : props) {
105             ren.RemoveActor2D(p);
106         }
107         panel.unlock();
108     }
109     
110     
111     public void addPosition(Vector3d position) {
112         positions.add(position);
113     }
114     
115     public void clearPositions() {
116         positions.clear();
117     }
118     
119     vtkPolyDataMapper2D mapper;
120     vtkTexture plusTex;
121     
122     double pw = 8;
123     
124     private vtkTexturedActor2D createActor() {
125         if (mapper == null) {
126             loadData();
127         }
128         vtkTexturedActor2D nextProp = new vtkTexturedActor2D();
129         
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();
137         
138         panel.addDeletable(nextProp);
139         
140         return nextProp;
141     }
142     
143     public void dispose() {
144         
145     }
146     
147     
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.");
152         
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);
158         
159         
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);
167         
168         cellArray.InsertNextCell(polygon);
169         
170         vtkPolyData quad = new vtkPolyData();
171         quad.SetPoints(points);
172         quad.SetPolys(cellArray);
173         
174         vtkFloatArray texCoords = new vtkFloatArray();
175         
176         texCoords.SetNumberOfComponents(2);
177         
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);
182         
183         quad.GetPointData().SetTCoords(texCoords);
184
185         
186         vtkPNGReader plusReader = new vtkPNGReader();
187         plusReader.SetFileName(plusTexFile);
188         
189         plusTex = new vtkTexture();
190         plusTex.SetInputConnection(plusReader.GetOutputPort());
191         plusTex.SetInterpolate(1);
192         
193         mapper = new vtkPolyDataMapper2D();
194         mapper.SetInputData(quad);
195         
196         panel.addDeletable(mapper);
197         panel.addDeletable(plusTex);
198
199         
200         plusReader.GetOutputPort().Delete();
201         plusReader.Delete();
202         quad.GetPointData().Delete();
203         quad.Delete();
204         points.Delete();
205         polygon.GetPointIds().Delete();
206         polygon.Delete();
207         cellArray.Delete();
208         texCoords.Delete();
209         
210     }
211     
212     
213   
214
215 }