1 package org.simantics.plant3d.scenegraph;
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;
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;
28 import vtk.vtkProperty;
30 public class GeometryComponent {
32 private TopoDS_Shape solidModel;
33 private vtkSolidObject solidObject;
35 private vtkMeshObject meshObject;
37 private Map<String,Object> currentParameters;
38 private Map<String,Object> calculatedParameters;
40 private boolean parametersUpdated = true;
42 private GeometryProvider geometryProvider;
44 public GeometryComponent() {
45 currentParameters = new HashMap<>();
46 calculatedParameters = new HashMap<>();
49 public void visualize(VtkView panel) {
50 if (geometryProvider != null) {
53 if (geometryProvider instanceof SolidModelProvider) {
54 SolidModelProvider solidModelProvider = (SolidModelProvider)geometryProvider;
55 if (solidModel == null || parametersUpdated) {
56 createGeometry(solidModelProvider);
58 if ((solidObject == null || parametersUpdated) && solidModel != null) {
59 solidObject = new vtkSolidObject(panel, solidModel);
61 if (solidObject != null) {
62 solidObject.visualizeSolid(true,true, false,false);
63 updateVisuals(true, true);
65 } else if (geometryProvider instanceof MeshProvider) {
66 MeshProvider provider = (MeshProvider)geometryProvider;
67 if (mesh == null || parametersUpdated) {
68 mesh = provider.getMesh();
70 if ((meshObject== null || parametersUpdated) && mesh != null) {
71 meshObject = new vtkMeshObject(panel, mesh);
74 if (meshObject != null) {
75 meshObject.visualizeMesh();
76 updateVisuals(true, true);
81 parametersUpdated = false;
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);
93 private void createGeometry(SolidModelProvider solidModelProvider) {
94 Collection<TopoDS_Shape> shapes;
96 shapes = solidModelProvider.getModel();
97 if (shapes.size() == 1) {
98 solidModel = shapes.iterator().next();
100 solidModel = OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));
101 for (TopoDS_Shape shape : shapes) {
106 } catch (Exception e) {
107 // TODO Auto-generated catch block
112 public Map<String, Object> getParameterMap() {
113 return Collections.unmodifiableMap(currentParameters);
116 public Map<String, Object> getCalculatedParameters() {
117 return calculatedParameters;
120 public Map<String, Object> getTotalParameters() {
121 Map<String,Object> params = new HashMap<>();
122 params.putAll(currentParameters);
123 params.putAll(calculatedParameters);
127 public void setParametersUpdated(boolean parametersUpdated) {
128 this.parametersUpdated = parametersUpdated;
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)
138 if (newValue == null) {
141 if (currentValue instanceof Double) {
142 if (Math.abs((Double)currentValue-(Double)newValue) < MathTools.NEAR_ZERO)
145 currentParameters.put(id, newValue);
146 parametersUpdated = true;
152 public GeometryProvider getGeometry() {
153 return geometryProvider;
156 public void setGeometry(final GeometryProvider provider) {
157 if (provider != null && provider.equals(geometryProvider))
160 if (geometryProvider != null) {
163 geometryProvider = provider;
166 private void deleteData() {
167 geometryProvider = null;
168 if (solidObject != null) {
169 solidObject.clearActors();
172 if (solidModel != null) {
176 if (meshObject != null) {
177 meshObject.clearActors();
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());
194 public void stopVisualize() {
195 if (solidObject != null) {
196 solidObject.clearActorsVTK();
199 if (solidModel != null) {
203 if (meshObject != null) {
204 meshObject.clearActorsVTK();
212 private boolean selected = false;
213 private boolean hover = false;
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);
223 hover = type == HighlightEventType.Hover;
224 updateVisuals(false, true);
230 protected double[] getSelectedColor() {
231 return new double[]{1,0,0};
234 protected double[] getColor() {
235 return new double[]{1,1,0};
238 public void updateVisuals(boolean s, boolean h) {
239 if (solidObject != null) {
243 color = getSelectedColor();
249 for (vtkProp3D prop : solidObject.getSolid()) {
250 vtkProperty property = ((vtkActor)prop).GetProperty();
251 property.SetColor(color);
256 double color[] = new double[]{0,0,0};
258 color = new double[]{1,0,1};
259 for (vtkProp3D prop : solidObject.getEdges()) {
260 vtkProperty property = ((vtkActor)prop).GetProperty();
261 property.SetColor(color);
266 } else if (meshObject != null) {
270 color = getSelectedColor();
276 vtkProp3D prop = meshObject.getActor();
278 vtkProperty property = ((vtkActor)prop).GetProperty();
279 property.SetColor(color);
285 double color[] = new double[]{0,0,0};
286 MeshActor actor = meshObject.getActor();
288 vtkProperty property = actor.GetProperty();
290 color = new double[]{1,0,1};
291 property.SetEdgeVisibility(1);
292 property.SetEdgeColor(color);
294 property.SetEdgeVisibility(0);
295 property.SetEdgeColor(color);
302 // axes.addToRenderer();
303 // axes.setAxesVisibility(selected);