]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/GeometryComponent.java
Add units to ComponentSelectionDialog
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / GeometryComponent.java
1 package org.simantics.plant3d.scenegraph;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Objects;
11 import java.util.Set;
12
13 import org.jcae.opencascade.jni.TopoDS_Shape;
14 import org.simantics.g3d.math.MathTools;
15 import org.simantics.g3d.scenegraph.GeometryProvider;
16 import org.simantics.g3d.scenegraph.MeshProvider;
17 import org.simantics.g3d.scenegraph.NodeHighlighter.HighlightEventType;
18 import org.simantics.g3d.scenegraph.ParametricGeometryProvider;
19 import org.simantics.g3d.shape.Mesh;
20 import org.simantics.g3d.vtk.common.VtkView;
21 import org.simantics.g3d.vtk.shape.MeshActor;
22 import org.simantics.g3d.vtk.shape.vtkMeshObject;
23 import org.simantics.opencascade.OccTriangulator;
24 import org.simantics.opencascade.SolidModelProvider;
25 import org.simantics.opencascade.vtk.vtkSolidObject;
26
27 import vtk.vtkActor;
28 import vtk.vtkProp3D;
29 import vtk.vtkProperty;
30
31 public class GeometryComponent {
32         
33         private TopoDS_Shape solidModel;
34         private vtkSolidObject solidObject;
35         private Mesh mesh;
36         private vtkMeshObject meshObject;
37         
38         private Map<String,Object> currentParameters;
39         private Map<String,Object> calculatedParameters;
40         
41         private boolean parametersUpdated = true;
42         
43         private GeometryProvider geometryProvider;
44         
45         public GeometryComponent() {
46                 currentParameters = new HashMap<>();
47                 calculatedParameters = new HashMap<>();
48         }
49         
50         public void visualize(VtkView panel) {
51                 if (geometryProvider != null) {
52                         updateParameters();
53                         
54                         if (geometryProvider instanceof SolidModelProvider) {
55                                 SolidModelProvider solidModelProvider = (SolidModelProvider)geometryProvider;
56                                 if (solidModel == null || parametersUpdated) {
57                                         createGeometry(solidModelProvider);     
58                                 }
59                                 if ((solidObject == null || parametersUpdated) && solidModel != null) {
60                                         solidObject = new vtkSolidObject(panel, solidModel);
61                                 }
62                                 if (solidObject != null) {
63                                         solidObject.visualizeSolid(true,true, false,false);
64                                         updateVisuals(true, true);
65                                 }
66                         } else  if (geometryProvider instanceof MeshProvider) {
67                                 MeshProvider provider = (MeshProvider)geometryProvider;
68                                 if (mesh == null || parametersUpdated) {
69                                         mesh = provider.getMesh();
70                                 }
71                                 if ((meshObject== null || parametersUpdated) && mesh != null) {
72                                         meshObject = new vtkMeshObject(panel, mesh);
73                                         
74                                 }
75                                 if (meshObject != null) {
76                                         meshObject.visualizeMesh();
77                                         updateVisuals(true, true);
78                                 }
79                         }
80                 }
81                 
82                 parametersUpdated = false;
83
84         }
85         
86         public void updateParameters() {
87                 //if (parametersUpdated && geometryProvider instanceof ParametricGeometryProvider) {
88                 if (geometryProvider instanceof ParametricGeometryProvider) {
89                         ((ParametricGeometryProvider)geometryProvider).setProperties(currentParameters);
90                         ((ParametricGeometryProvider)geometryProvider).updateCalculatedProperties(calculatedParameters);
91                 }
92         }
93         
94         private void createGeometry(SolidModelProvider solidModelProvider) {
95                 Collection<TopoDS_Shape> shapes;
96                 try {
97                         shapes = solidModelProvider.getModel();
98                         if (shapes.size() == 1) {
99                                 solidModel = shapes.iterator().next();
100                         } else { 
101                                 solidModel = OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));
102                                 for (TopoDS_Shape shape : shapes) {
103                                         shape.delete();
104                                 }
105                         }
106 //                              shapes.clear();
107                 } catch (Exception e) {
108                         // TODO Auto-generated catch block
109                         e.printStackTrace();
110                 }
111         }
112         
113         public Map<String, Object> getParameterMap() {
114                 return Collections.unmodifiableMap(currentParameters);
115         }
116         
117         public Map<String, Object> getCalculatedParameters() {
118                 return calculatedParameters;
119         }
120         
121         public Map<String, Object> getTotalParameters() {
122         Map<String,Object> params = new HashMap<>();
123         params.putAll(currentParameters);
124         params.putAll(calculatedParameters);
125         return params;
126     }
127         
128         public void setParametersUpdated(boolean parametersUpdated) {
129                 this.parametersUpdated = parametersUpdated;
130         }
131         
132         public Set<String> setParameterMap(Map<String, Object> parameters) {
133                 Set<String> ids = new HashSet<String>();
134                 for (String id : parameters.keySet()) {
135                         Object currentValue = currentParameters.get(id);
136                         Object newValue = parameters.get(id);
137                         if (Objects.equals(currentValue, newValue))
138                                 continue;
139                         if (newValue == null) {
140                             continue;
141                         }
142                         if (currentValue instanceof Double) {
143                                 if (Math.abs((Double)currentValue-(Double)newValue) < MathTools.NEAR_ZERO)
144                                         continue;
145                         }
146                         currentParameters.put(id, newValue);
147                         parametersUpdated = true;
148                         ids.add(id);
149                 }
150                 return ids;
151         }
152         
153         public GeometryProvider getGeometry() {
154                 return geometryProvider;
155         }
156         
157         public void setGeometry(final GeometryProvider provider) {
158                 if (provider != null && provider.equals(geometryProvider))
159                         return;
160
161                 if (geometryProvider != null) {
162                         deleteData();
163                 }
164                 geometryProvider = provider;
165         }
166         
167         private void deleteData() {
168                 geometryProvider = null;
169                 if (solidObject != null) {
170                         solidObject.clearActors();
171                         solidObject = null;
172                 }
173                 if (solidModel != null) {
174                         solidModel.delete();
175                         solidModel = null;
176                 }
177                 if (meshObject != null) {
178                         meshObject.clearActors();
179                         meshObject = null;
180                 }
181                 if (mesh != null)
182                         mesh = null;
183                 
184         }
185
186         public Collection<vtkProp3D> getActors() {
187                 List<vtkProp3D> list = new ArrayList<vtkProp3D>();
188                 if (solidObject != null)
189                         list.addAll(solidObject.getActors());
190                 if (meshObject != null && meshObject.getActor() != null)
191                         list.add(meshObject.getActor());
192                 return list;
193         }
194         
195         public void stopVisualize() {
196                 if (solidObject != null) {
197                         solidObject.clearActorsVTK();
198                         solidObject = null;
199                 }
200                 if (solidModel != null) {
201                         solidModel.delete();
202                         solidModel = null;
203                 }
204                 if (meshObject != null) {
205                         meshObject.clearActorsVTK();
206                         meshObject = null;
207                 }
208                 if (mesh != null)
209                         mesh = null;
210                 
211         }
212         
213         private boolean selected = false;
214         private boolean hover = false;
215         
216         public void highlight(HighlightEventType type) {
217                 if (type == HighlightEventType.Selection || type == HighlightEventType.ClearSelection) {
218                         selected = type == HighlightEventType.Selection;
219 //                      hingeA.visualizeSolid(selected,true,false);
220 //                      hingeB.visualizeSolid(selected,true,false);
221                         updateVisuals(true, false);
222 //                      update(null);
223                 } else {
224                         hover = type == HighlightEventType.Hover;
225                         updateVisuals(false, true);
226                 }
227                 
228                 
229         }
230         
231         protected double[] getSelectedColor() {
232                 return new double[]{1,0,0};
233         }
234         
235         protected double[] getColor() {
236                 return new double[]{1,1,0};
237         }
238         
239         public void updateVisuals(boolean s, boolean h) {
240                 if (solidObject != null) {
241                         if (s) {
242                                 double color[];
243                                 if (selected) {
244                                         color = getSelectedColor();
245                                 
246                                 } else {
247                                         color = getColor();
248
249                                 }
250                                 for (vtkProp3D prop : solidObject.getSolid()) {
251                                         vtkProperty property = ((vtkActor)prop).GetProperty();
252                                         property.SetColor(color);
253                                         property.Delete();
254                                 }
255                         }
256                         if (h) {
257                                 double color[] = new double[]{0,0,0};
258                                 if (hover)
259                                         color = new double[]{1,0,1};
260                                 for (vtkProp3D prop : solidObject.getEdges()) {
261                                         prop.SetVisibility(hover ? 1 : 0);
262                                         vtkProperty property = ((vtkActor)prop).GetProperty();
263                                         property.SetColor(color);
264                                         property.Delete();
265                                 }
266                                 
267                         }
268                 } else if (meshObject != null) {
269                         if (s) {
270                                 double color[];
271                                 if (selected) {
272                                         color = getSelectedColor();
273                                 
274                                 } else {
275                                         color = getColor();
276
277                                 }
278                                 vtkProp3D prop = meshObject.getActor();
279                                 if (prop != null) {
280                                         vtkProperty property = ((vtkActor)prop).GetProperty();
281                                         property.SetColor(color);
282                                         property.Delete();
283                                 }
284                                 
285                         }
286                         if (h) {
287                                 double color[] = new double[]{0,0,0};
288                                 MeshActor actor = meshObject.getActor();
289                                 if (actor != null) {
290                                         vtkProperty property = actor.GetProperty();
291                                         if (hover) {
292                                                 color = new double[]{1,0,1};
293                                                 property.SetEdgeVisibility(1);
294                                                 property.SetEdgeColor(color);
295                                         } else {
296                                                 property.SetEdgeVisibility(0);
297                                                 property.SetEdgeColor(color);
298                                         }
299                                         property.Delete();
300                                 }
301                         }
302                 } else {
303 //                      if (s) {
304 //                              axes.addToRenderer();
305 //                              axes.setAxesVisibility(selected);
306 //                      }
307                 }
308         }
309 }