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