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;
10 import java.util.Objects;
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;
29 import vtk.vtkProperty;
31 public class GeometryComponent {
33 private TopoDS_Shape solidModel;
34 private vtkSolidObject solidObject;
36 private vtkMeshObject meshObject;
38 private Map<String,Object> currentParameters;
39 private Map<String,Object> calculatedParameters;
41 private boolean parametersUpdated = true;
43 private GeometryProvider geometryProvider;
45 public GeometryComponent() {
46 currentParameters = new HashMap<>();
47 calculatedParameters = new HashMap<>();
50 public void visualize(VtkView panel) {
51 if (geometryProvider != null) {
54 if (geometryProvider instanceof SolidModelProvider) {
55 SolidModelProvider solidModelProvider = (SolidModelProvider)geometryProvider;
56 if (solidModel == null || parametersUpdated) {
57 createGeometry(solidModelProvider);
59 if ((solidObject == null || parametersUpdated) && solidModel != null) {
60 solidObject = new vtkSolidObject(panel, solidModel);
62 if (solidObject != null) {
63 solidObject.visualizeSolid(true,true, false,false);
64 updateVisuals(true, true);
66 } else if (geometryProvider instanceof MeshProvider) {
67 MeshProvider provider = (MeshProvider)geometryProvider;
68 if (mesh == null || parametersUpdated) {
69 mesh = provider.getMesh();
71 if ((meshObject== null || parametersUpdated) && mesh != null) {
72 meshObject = new vtkMeshObject(panel, mesh);
75 if (meshObject != null) {
76 meshObject.visualizeMesh();
77 updateVisuals(true, true);
82 parametersUpdated = false;
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);
94 private void createGeometry(SolidModelProvider solidModelProvider) {
95 Collection<TopoDS_Shape> shapes;
97 shapes = solidModelProvider.getModel();
98 if (shapes.size() == 1) {
99 solidModel = shapes.iterator().next();
101 solidModel = OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));
102 for (TopoDS_Shape shape : shapes) {
107 } catch (Exception e) {
108 // TODO Auto-generated catch block
113 public Map<String, Object> getParameterMap() {
114 return Collections.unmodifiableMap(currentParameters);
117 public Map<String, Object> getCalculatedParameters() {
118 return calculatedParameters;
121 public Map<String, Object> getTotalParameters() {
122 Map<String,Object> params = new HashMap<>();
123 params.putAll(currentParameters);
124 params.putAll(calculatedParameters);
128 public void setParametersUpdated(boolean parametersUpdated) {
129 this.parametersUpdated = parametersUpdated;
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))
139 if (newValue == null) {
142 if (currentValue instanceof Double) {
143 if (Math.abs((Double)currentValue-(Double)newValue) < MathTools.NEAR_ZERO)
146 currentParameters.put(id, newValue);
147 parametersUpdated = true;
153 public GeometryProvider getGeometry() {
154 return geometryProvider;
157 public void setGeometry(final GeometryProvider provider) {
158 if (provider != null && provider.equals(geometryProvider))
161 if (geometryProvider != null) {
164 geometryProvider = provider;
167 private void deleteData() {
168 geometryProvider = null;
169 if (solidObject != null) {
170 solidObject.clearActors();
173 if (solidModel != null) {
177 if (meshObject != null) {
178 meshObject.clearActors();
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());
195 public void stopVisualize() {
196 if (solidObject != null) {
197 solidObject.clearActorsVTK();
200 if (solidModel != null) {
204 if (meshObject != null) {
205 meshObject.clearActorsVTK();
213 private boolean selected = false;
214 private boolean hover = false;
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);
224 hover = type == HighlightEventType.Hover;
225 updateVisuals(false, true);
231 protected double[] getSelectedColor() {
232 return new double[]{1,0,0};
235 protected double[] getColor() {
236 return new double[]{1,1,0};
239 public void updateVisuals(boolean s, boolean h) {
240 if (solidObject != null) {
244 color = getSelectedColor();
250 for (vtkProp3D prop : solidObject.getSolid()) {
251 vtkProperty property = ((vtkActor)prop).GetProperty();
252 property.SetColor(color);
257 double color[] = new double[]{0,0,0};
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);
268 } else if (meshObject != null) {
272 color = getSelectedColor();
278 vtkProp3D prop = meshObject.getActor();
280 vtkProperty property = ((vtkActor)prop).GetProperty();
281 property.SetColor(color);
287 double color[] = new double[]{0,0,0};
288 MeshActor actor = meshObject.getActor();
290 vtkProperty property = actor.GetProperty();
292 color = new double[]{1,0,1};
293 property.SetEdgeVisibility(1);
294 property.SetEdgeColor(color);
296 property.SetEdgeVisibility(0);
297 property.SetEdgeColor(color);
304 // axes.addToRenderer();
305 // axes.setAxesVisibility(selected);