* Replaced OCCT-based geometries for Straight and Elbow.
* MeshActor is able to bypass mesh normals to VTK
gitlab #10
Change-Id: Ib201d29802921df35a5fa88da922e8265b54df7c
package org.simantics.g3d.vtk.shape;
import javax.vecmath.Tuple3d;
-import javax.vecmath.Vector3d;
import org.simantics.g3d.shape.Color4d;
import org.simantics.g3d.shape.Mesh;
import vtk.vtkActor;
-import vtk.vtkDataSetMapper;
+import vtk.vtkFloatArray;
import vtk.vtkIdList;
+import vtk.vtkPointData;
import vtk.vtkPoints;
import vtk.vtkPolyData;
import vtk.vtkPolyDataMapper;
public class MeshActor extends vtkActor {
+
public static vtkPolyData createPolyData(Mesh mesh) {
vtkPolyData polyData = new vtkPolyData();
polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
}
list.Delete();
triangle.Delete();
-
+
vtkPoints points = new vtkPoints();
for (int i = 0; i < mesh.getVertices().size(); i++) {
polyData.SetPoints(points);
points.Delete();
+ if (mesh.getNormals() != null) {
+ vtkFloatArray normals = new vtkFloatArray();
+ normals.SetNumberOfComponents(3);
+ normals.SetNumberOfTuples(mesh.getNormals().size());
+ for (int i = 0; i < mesh.getNormals().size(); i++) {
+ Tuple3d p = mesh.getNormals().get(i);
+ normals.InsertTuple3(i, p.x, p.y, p.z);
+ }
+ vtkPointData pointData = polyData.GetPointData();
+ pointData.SetNormals(normals);
+ normals.Delete();
+ pointData.Delete();
+ }
if (mesh.getColors() != null) {
vtkUnsignedCharArray colors = new vtkUnsignedCharArray();
colors.SetName("Colors");
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
vtkPolyData polyData = createPolyData(mesh);
- boolean computeNormals = true;
- if (computeNormals) {
+
+ if (mesh.getNormals() == null) {
vtkPolyDataNormals normals = new vtkPolyDataNormals();
normals.SetInputData(polyData);
+ normals.ComputePointNormalsOn();
mapper.SetInputConnection(normals.GetOutputPort());
normals.GetOutputPort().Delete();
normals.Delete();
polyData.Delete();
}
+
+ @Override
+ public void Delete() {
+ // TODO Auto-generated method stub
+ super.Delete();
+ }
}
--- /dev/null
+package org.simantics.g3d.vtk.shape;
+
+import org.simantics.g3d.shape.Mesh;
+import org.simantics.utils.threads.AWTThread;
+import org.simantics.utils.threads.ThreadUtils;
+
+import vtk.vtkPanel;
+import vtk.vtkRenderer;
+
+public class vtkMeshObject {
+
+ private vtkPanel panel;
+ private Mesh mesh;
+
+ private MeshActor meshActor;
+
+ public vtkMeshObject(vtkPanel panel, Mesh mesh) {
+ this.mesh = mesh;
+ this.panel = panel;
+ }
+
+ public void visualizeMesh() {
+ clearActorsAWT();
+ meshActor = new MeshActor();
+ meshActor.setMesh(mesh);
+ //System.out.println("Set mesh " + mesh.getVertices().size() + " " + mesh.getIndices().size() + " " + this);
+ showActorsAWT();
+ }
+
+ public MeshActor getActor() {
+ assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread());
+ return meshActor;
+ }
+
+ public void showActorsAWT() {
+ //System.out.println("showActorsAWT " + this + " " + meshActor != null);
+ assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread());
+ vtkRenderer ren = panel.GetRenderer();
+ if (meshActor != null)
+ ren.AddActor(meshActor);
+ }
+
+ public void showActors() {
+ ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
+
+ @Override
+ public void run() {
+ showActorsAWT();
+ }
+ });
+ }
+
+ public void clearActorsAWT() {
+ assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread());
+
+ if (meshActor == null)
+ return;
+ //System.out.println("clearActorsAwt " + this);
+ vtkRenderer ren = panel.GetRenderer();
+ if (ren == null)
+ return;
+ panel.lock();
+ if (meshActor.GetVTKId() != 0) {
+ ren.RemoveActor(meshActor);
+ meshActor.Delete();
+ }
+ panel.unlock();
+ meshActor = null;
+ }
+
+ private void clearActorsAWT(MeshActor meshActor) {
+ assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread());
+
+ if (meshActor == null)
+ return;
+ //System.out.println("clearActorsAwt2 " + this);
+ vtkRenderer ren = panel.GetRenderer();
+ if (ren == null)
+ return;
+ panel.lock();
+ if (meshActor.GetVTKId() != 0) {
+ ren.RemoveActor(meshActor);
+ meshActor.Delete();
+ }
+ panel.unlock();
+ meshActor = null;
+ }
+
+ public void clearActors() {
+ if (meshActor == null)
+ return;
+ ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
+
+ @Override
+ public void run() {
+ clearActorsAWT(meshActor);
+ }
+ });
+ }
+
+ public void dispose() {
+ mesh = null;
+ clearActors();
+ }
+
+ public void delete() {
+ mesh = null;
+ clearActorsAWT();
+ }
+
+}
--- /dev/null
+package org.simantics.g3d.scenegraph;
+
+public interface GeometryProvider {
+
+}
--- /dev/null
+package org.simantics.g3d.scenegraph;
+
+import org.simantics.g3d.shape.Mesh;
+
+public interface MeshProvider extends GeometryProvider{
+ Mesh getMesh();
+}
--- /dev/null
+package org.simantics.g3d.scenegraph;
+
+import java.util.Map;
+
+public interface ParametricGeometryProvider extends GeometryProvider{
+
+ /**
+ * Sets parameters for geometry calculation
+ * @param props
+ */
+ public void setProperties(Map<String,Object> props);
+
+ /**
+ * Returns calculated parameters from geometry calculation
+ * @param returnProps
+ */
+ public void updateCalculatedProperties(Map<String,Object> returnProps);
+}
--- /dev/null
+package org.simantics.g3d.scenegraph;
+
+public interface ParametricMeshProvider extends MeshProvider, ParametricGeometryProvider{
+
+}
return new Mesh(vertices, normals, indices);
}
+ public Mesh create(Point3d c, Vector3d axis, Vector3d an, double R, double r, double a, int res, double res2) {
+
+
+
+ int steps = 0;
+ double sa = 0.0;
+
+ Vector3d rn = new Vector3d(axis);
+ rn.normalize();
+ Vector3d r1 = new Vector3d(an);
+ r1.normalize();
+ r1.scale(R);
+
+
+ if ((a +0.0001) > Math.PI) {
+ steps = 1;
+ } else {
+ steps = (int)(Math.ceil(a/res2));
+ if (steps == 0)
+ steps = 1;
+ sa = a/steps;
+ }
+
+ List<Tuple3d> vertices = new ArrayList<Tuple3d>(res * (steps+1));
+ List<Tuple3d> normals = new ArrayList<Tuple3d>(res * (steps+1));
+ List<Integer> indices = new ArrayList<Integer>();
+
+ for (int i = 0; i <= steps; i++) {
+ Vector3d p;
+ Vector3d t;
+ p = new Vector3d();
+ double ca = sa * i;
+ Quat4d q = MathTools.getQuat(new AxisAngle4d(rn, ca));
+ MathTools.rotate(q, r1, p);
+ t = new Vector3d();
+ t.cross(rn,p);
+ t.normalize();
+ p.add(c);
+
+
+ createCircle(vertices, normals, p, t, rn, res, r);
+ }
+ int count = steps*res*6;
+ for (int i = 0; i < count; i++) {
+ indices.add(-1);
+ }
+ createIndices(steps, res, indices);
+ return new Mesh(vertices, normals, indices);
+ }
+
private static void createCircle(List<Tuple3d> points, List<Tuple3d> normals, Tuple3d p, Vector3d t, Vector3d n, int res, double radius) {
n = new Vector3d(n);
n.scale(radius);
if (actors.size() == 0)
return;
vtkRenderer ren = panel.GetRenderer();
+ if (ren == null)
+ return;
+ panel.lock();
for (vtkProp3D act : actors) {
if (act.GetVTKId() != 0) {
ren.RemoveActor(act);
act.Delete();
}
}
+ panel.unlock();
actors.clear();
solid.clear();
edges.clear();
if (actors.size() == 0)
return;
vtkRenderer ren = panel.GetRenderer();
+ if (ren == null)
+ return;
panel.lock();
for (vtkProp3D act : actors) {
if (act.GetVTKId() != 0) {
Require-Bundle: org.eclipse.core.runtime,
javax.vecmath;bundle-version="1.5.2",
org.jcae.opencascade;bundle-version="1.0.0",
- org.simantics.utils.datastructures;bundle-version="1.1.0"
+ org.simantics.utils.datastructures;bundle-version="1.1.0",
+ org.simantics.g3d;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.simantics.opencascade
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.opencascade;\r
-\r
-import java.util.Map;\r
-\r
-public interface ParametricSolidModelProvider extends SolidModelProvider{\r
- \r
- public void setProperties(Map<String,Object> props);\r
- \r
- public void updateCalculatedProperties(Map<String,Object> returnProps);\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.opencascade;
+
+import org.simantics.g3d.scenegraph.ParametricGeometryProvider;
+
+public interface ParametricSolidModelProvider extends SolidModelProvider, ParametricGeometryProvider{
+
+
+}
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.opencascade;\r
-\r
-import java.util.Collection;\r
-\r
-import org.jcae.opencascade.jni.TopoDS_Shape;\r
-\r
-public interface SolidModelProvider {\r
-\r
- \r
- public Collection<TopoDS_Shape> getModel() throws Exception;\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.opencascade;
+
+import java.util.Collection;
+
+import org.jcae.opencascade.jni.TopoDS_Shape;
+import org.simantics.g3d.scenegraph.GeometryProvider;
+
+public interface SolidModelProvider extends GeometryProvider {
+
+
+ public Collection<TopoDS_Shape> getModel() throws Exception;
+}
<?xml version="1.0" encoding="UTF-8"?>
<adapters>
-<target interface="org.simantics.opencascade.SolidModelProvider">
+<target interface="org.simantics.g3d.scenegraph.GeometryProvider">
<resource uri="http://www.simantics.org/Plant3D-0.1/Builtin/ElbowGeometryProvider"
class="org.simantics.plant3d.geometry.ElbowGeometryProvider">
<this />
--- /dev/null
+package org.simantics.plant3d.geometry;
+
+import java.util.Map;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.simantics.db.Resource;
+import org.simantics.g3d.scenegraph.ParametricMeshProvider;
+
+public abstract class BuiltinMeshProvider implements ParametricMeshProvider, IAdaptable{
+
+ private Resource resource;
+
+ public BuiltinMeshProvider(Resource resource) {
+ this.resource = resource;
+ }
+
+ @Override
+ public Object getAdapter(Class adapter) {
+ if (Resource.class == adapter)
+ return resource;
+ return null;
+ }
+
+ @Override
+ public void updateCalculatedProperties(Map<String, Object> returnProps) {
+
+ }
+
+}
import java.util.Collections;
import java.util.Map;
+import javax.vecmath.Point3d;
+import javax.vecmath.Vector3d;
+
import org.jcae.opencascade.jni.TopoDS_Shape;
import org.simantics.db.Resource;
import org.simantics.g3d.math.MathTools;
+import org.simantics.g3d.shape.ArcCylinder;
+import org.simantics.g3d.shape.Mesh;
import org.simantics.opencascade.OccTriangulator;
-public class ElbowGeometryProvider extends BuiltinGeometryProvider {
+//public class ElbowGeometryProvider extends BuiltinGeometryProvider {
+public class ElbowGeometryProvider extends BuiltinMeshProvider {
public ElbowGeometryProvider(Resource resource) {
super(resource);
private double turnRadius = 0.05;
private double turnAngle = Math.PI * 0.5;
- @Override
+// @Override
public Collection<TopoDS_Shape> getModel() throws Exception {
double t = Math.tan((Math.PI - turnAngle) * 0.5);
double R = 0.0;
return Collections.singletonList(shape);
}
+// @Override
+ public Mesh getMesh() {
+ double t = Math.tan((Math.PI - turnAngle) * 0.5);
+ double R = 0.0;
+ if (t > MathTools.NEAR_ZERO)
+ R = turnRadius / t;
+ ArcCylinder cyl = new ArcCylinder();
+
+ //return cyl.create(new Point3d(-R, 0.0, -turnRadius), new Point3d(0.0,turnRadius,0.0), new Point3d(-R, 0.0, turnRadius), radius, 16, 0.2);
+ return cyl.create(new Point3d(-R, 0.0, -turnRadius), new Vector3d(0.0,1.0,0.0), new Vector3d(0.0,0.0, 1.0),turnRadius, radius, turnAngle,16, 0.2);
+ }
+
@Override
public void setProperties(Map<String, Object> props) {
if (props.containsKey("turnRadius"))
package org.simantics.plant3d.geometry;
-import java.util.Collection;
-import java.util.Collections;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
-import org.jcae.opencascade.jni.TopoDS_Shape;
+import javax.vecmath.Point3d;
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
import org.simantics.db.Resource;
import org.simantics.g3d.math.MathTools;
-import org.simantics.opencascade.OccTriangulator;
+import org.simantics.g3d.shape.Mesh;
+import org.simantics.g3d.shape.Tube;
-public class StraightGeometryProvider extends BuiltinGeometryProvider {
+//public class StraightGeometryProvider extends BuiltinGeometryProvider {
+public class StraightGeometryProvider extends BuiltinMeshProvider {
public StraightGeometryProvider(Resource resource) {
super(resource);
private double length = 1.0;
private double radius = 0.01;
- @Override
- public Collection<TopoDS_Shape> getModel() throws Exception {
- TopoDS_Shape shape = OccTriangulator.makeCylinder(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius, length);
+// @Override
+// public Collection<TopoDS_Shape> getModel() throws Exception {
+// TopoDS_Shape shape = OccTriangulator.makeCylinder(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius, length);
// System.out.println("Create straight l:" + length + " r:" + radius);
- return Collections.singletonList(shape);
+// return Collections.singletonList(shape);
+// }
+
+ @Override
+ public Mesh getMesh() {
+ if (length < .0001)
+ return null;
+ Tube tube = new Tube();
+ tube.setResolution(16);
+ List<Tuple3d> vertices = new ArrayList<Tuple3d>();
+ List<Double> radius = new ArrayList<Double>();
+ List<Vector3d> tangents = new ArrayList<Vector3d>();
+ vertices.add(new Point3d(-length*0.5, 0.0, 0.0));
+ vertices.add(new Point3d( length*0.5, 0.0, 0.0));
+ radius.add(this.radius);
+ radius.add(this.radius);
+ tangents.add(new Vector3d(1.0,0.0,0.0));
+ tangents.add(new Vector3d(1.0,0.0,0.0));
+ tube.setVertices(vertices);
+ tube.setRadiis(radius);
+ tube.setTangents(tangents);
+ tube.setCap(false);
+ return tube.create();
}
@Override
--- /dev/null
+package org.simantics.plant3d.scenegraph;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.jcae.opencascade.jni.TopoDS_Shape;
+import org.simantics.g3d.math.MathTools;
+import org.simantics.g3d.scenegraph.GeometryProvider;
+import org.simantics.g3d.scenegraph.MeshProvider;
+import org.simantics.g3d.scenegraph.NodeHighlighter.HighlightEventType;
+import org.simantics.g3d.scenegraph.ParametricGeometryProvider;
+import org.simantics.g3d.shape.Mesh;
+import org.simantics.g3d.vtk.shape.MeshActor;
+import org.simantics.g3d.vtk.shape.vtkMeshObject;
+import org.simantics.opencascade.OccTriangulator;
+import org.simantics.opencascade.SolidModelProvider;
+import org.simantics.opencascade.vtk.vtkSolidObject;
+
+import vtk.vtkActor;
+import vtk.vtkPanel;
+import vtk.vtkProp3D;
+import vtk.vtkProperty;
+
+public class GeometryComponent {
+
+ private TopoDS_Shape solidModel;
+ private vtkSolidObject solidObject;
+ private Mesh mesh;
+ private vtkMeshObject meshObject;
+
+ private Map<String,Object> currentParameters;
+ private Map<String,Object> calculatedParameters;
+
+ private boolean parametersUpdated = true;
+
+ private GeometryProvider geometryProvider;
+
+ public GeometryComponent() {
+ currentParameters = new HashMap<String, Object>();
+ calculatedParameters = new HashMap<String, Object>();
+ }
+
+ public void visualize(vtkPanel panel) {
+ if (geometryProvider != null) {
+ updateParameters();
+
+ if (geometryProvider instanceof SolidModelProvider) {
+ SolidModelProvider solidModelProvider = (SolidModelProvider)geometryProvider;
+ if (solidModel == null || parametersUpdated) {
+ createGeometry(solidModelProvider);
+ }
+ if ((solidObject == null || parametersUpdated) && solidModel != null) {
+ solidObject = new vtkSolidObject(panel, solidModel);
+ }
+ if (solidObject != null) {
+ solidObject.visualizeSolid(true,true, false,false);
+ updateVisuals(true, true);
+ }
+ } else if (geometryProvider instanceof MeshProvider) {
+ MeshProvider provider = (MeshProvider)geometryProvider;
+ if (mesh == null || parametersUpdated) {
+ mesh = provider.getMesh();
+ }
+ if ((meshObject== null || parametersUpdated) && mesh != null) {
+ meshObject = new vtkMeshObject(panel, mesh);
+
+ }
+ if (meshObject != null) {
+ meshObject.visualizeMesh();
+ updateVisuals(true, true);
+ }
+ }
+ }
+
+ parametersUpdated = false;
+
+ }
+
+ public void updateParameters() {
+ //if (parametersUpdated && geometryProvider instanceof ParametricGeometryProvider) {
+ if (geometryProvider instanceof ParametricGeometryProvider) {
+ ((ParametricGeometryProvider)geometryProvider).setProperties(currentParameters);
+ ((ParametricGeometryProvider)geometryProvider).updateCalculatedProperties(calculatedParameters);
+ }
+ }
+
+ private void createGeometry(SolidModelProvider solidModelProvider) {
+ Collection<TopoDS_Shape> shapes;
+ try {
+ shapes = solidModelProvider.getModel();
+ if (shapes.size() == 1) {
+ solidModel = shapes.iterator().next();
+ } else {
+ solidModel = OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));
+ for (TopoDS_Shape shape : shapes) {
+ shape.delete();
+ }
+ }
+// shapes.clear();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public Map<String, Object> getParameterMap() {
+ return Collections.unmodifiableMap(currentParameters);
+ }
+
+ public Map<String, Object> getCalculatedParameters() {
+ return calculatedParameters;
+ }
+
+ public void setParametersUpdated(boolean parametersUpdated) {
+ this.parametersUpdated = parametersUpdated;
+ }
+
+ public Set<String> setParameterMap(Map<String, Object> parameters) {
+ Set<String> ids = new HashSet<String>();
+ for (String id : parameters.keySet()) {
+ Object currentValue = currentParameters.get(id);
+ Object newValue = parameters.get(id);
+ if (currentValue == newValue)
+ continue;
+ if (currentValue instanceof Double) {
+ if (Math.abs((Double)currentValue-(Double)newValue) < MathTools.NEAR_ZERO)
+ continue;
+ }
+ currentParameters.put(id, newValue);
+ parametersUpdated = true;
+ ids.add(id);
+ }
+ return ids;
+ }
+
+ public GeometryProvider getGeometry() {
+ return geometryProvider;
+ }
+
+ public void setGeometry(final GeometryProvider provider) {
+ if (provider != null && provider.equals(geometryProvider))
+ return;
+
+ if (geometryProvider != null) {
+ deleteData();
+ }
+ geometryProvider = provider;
+ }
+
+ private void deleteData() {
+ geometryProvider = null;
+ if (solidObject != null) {
+ solidObject.clearActors();
+ solidObject = null;
+ }
+ if (solidModel != null) {
+ solidModel.delete();
+ solidModel = null;
+ }
+ if (meshObject != null) {
+ meshObject.clearActors();
+ meshObject = null;
+ }
+ if (mesh != null)
+ mesh = null;
+
+ }
+
+ public Collection<vtkProp3D> getActors() {
+ List<vtkProp3D> list = new ArrayList<vtkProp3D>();
+ if (solidObject != null)
+ list.addAll(solidObject.getActors());
+ if (meshObject != null && meshObject.getActor() != null)
+ list.add(meshObject.getActor());
+ return list;
+ }
+
+ public void stopVisualize() {
+ if (solidObject != null) {
+ solidObject.clearActorsAWT();
+ solidObject = null;
+ }
+ if (solidModel != null) {
+ solidModel.delete();
+ solidModel = null;
+ }
+ if (meshObject != null) {
+ meshObject.clearActorsAWT();
+ meshObject = null;
+ }
+ if (mesh != null)
+ mesh = null;
+
+ }
+
+ private boolean selected = false;
+ private boolean hover = false;
+
+ public void highlight(HighlightEventType type) {
+ if (type == HighlightEventType.Selection || type == HighlightEventType.ClearSelection) {
+ selected = type == HighlightEventType.Selection;
+// hingeA.visualizeSolid(selected,true,false);
+// hingeB.visualizeSolid(selected,true,false);
+ updateVisuals(true, false);
+// update(null);
+ } else {
+ hover = type == HighlightEventType.Hover;
+ updateVisuals(false, true);
+ }
+
+
+ }
+
+ protected double[] getSelectedColor() {
+ return new double[]{1,0,0};
+ }
+
+ protected double[] getColor() {
+ return new double[]{1,1,0};
+ }
+
+ public void updateVisuals(boolean s, boolean h) {
+ if (solidObject != null) {
+ if (s) {
+ double color[];
+ if (selected) {
+ color = getSelectedColor();
+
+ } else {
+ color = getColor();
+
+ }
+ for (vtkProp3D prop : solidObject.getSolid()) {
+ vtkProperty property = ((vtkActor)prop).GetProperty();
+ property.SetColor(color);
+ property.Delete();
+ }
+ }
+ if (h) {
+ double color[] = new double[]{0,0,0};
+ if (hover)
+ color = new double[]{1,0,1};
+ for (vtkProp3D prop : solidObject.getEdges()) {
+ vtkProperty property = ((vtkActor)prop).GetProperty();
+ property.SetColor(color);
+ property.Delete();
+ }
+
+ }
+ } else if (meshObject != null) {
+ if (s) {
+ double color[];
+ if (selected) {
+ color = getSelectedColor();
+
+ } else {
+ color = getColor();
+
+ }
+ vtkProp3D prop = meshObject.getActor();
+ if (prop != null) {
+ vtkProperty property = ((vtkActor)prop).GetProperty();
+ property.SetColor(color);
+ property.Delete();
+ }
+
+ }
+ if (h) {
+ double color[] = new double[]{0,0,0};
+ MeshActor actor = meshObject.getActor();
+ if (actor != null) {
+ vtkProperty property = actor.GetProperty();
+ if (hover) {
+ color = new double[]{1,0,1};
+ property.SetEdgeVisibility(1);
+ property.SetEdgeColor(color);
+ } else {
+ property.SetEdgeVisibility(0);
+ property.SetEdgeColor(color);
+ }
+ property.Delete();
+ }
+ }
+ } else {
+// if (s) {
+// axes.addToRenderer();
+// axes.setAxesVisibility(selected);
+// }
+ }
+ }
+}
package org.simantics.plant3d.scenegraph;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
import java.util.Map;
+import java.util.Set;
-import org.jcae.opencascade.jni.TopoDS_Shape;
-import org.simantics.g3d.math.MathTools;
+import org.simantics.g3d.scenegraph.GeometryProvider;
import org.simantics.g3d.scenegraph.NodeHighlighter;
import org.simantics.g3d.vtk.utils.vtkUtil;
import org.simantics.objmap.graph.annotations.RelatedGetObj;
import org.simantics.objmap.graph.annotations.RelatedSetObj;
-import org.simantics.opencascade.OccTriangulator;
-import org.simantics.opencascade.ParametricSolidModelProvider;
-import org.simantics.opencascade.SolidModelProvider;
-import org.simantics.opencascade.vtk.vtkSolidObject;
import org.simantics.plant3d.ontology.Plant3D;
-import vtk.vtkActor;
import vtk.vtkPanel;
import vtk.vtkProp3D;
-import vtk.vtkProperty;
import vtk.vtkRenderer;
public abstract class GeometryNode extends P3DNode implements ParameterizedNode, NodeHighlighter {
- private TopoDS_Shape solidModel;
- private vtkSolidObject solidObject;
-
- private Map<String,Object> currentParameters;
- private Map<String,Object> calculatedParameters;
-
- private boolean parametersUpdated = true;
-
+ private GeometryComponent component;
+
public GeometryNode() {
- currentParameters = new HashMap<String, Object>();
- calculatedParameters = new HashMap<String, Object>();
+ component = new GeometryComponent() {
+ @Override
+ protected double[] getColor() {
+ return GeometryNode.this.getColor();
+ }
+
+ @Override
+ protected double[] getSelectedColor() {
+ return GeometryNode.this.getSelectedColor();
+ }
+ };
}
@Override
public void visualize(vtkPanel panel) {
- if (solidModelProvider != null) {
-
- updateParameters();
- if (solidModel == null || parametersUpdated) {
- createGeometry();
- }
- }
- if ((solidObject == null || parametersUpdated) && solidModel != null) {
- solidObject = new vtkSolidObject(panel, solidModel);
- }
- if (solidObject != null) {
- solidObject.visualizeSolid(true,true, false,false);
- updateVisuals(true, true);
- }
- parametersUpdated = false;
-
+ updateParameters();
+ component.visualize(panel);
}
public void updateParameters() {
- if (solidModelProvider instanceof ParametricSolidModelProvider) {
- ((ParametricSolidModelProvider)solidModelProvider).setProperties(currentParameters);
- ((ParametricSolidModelProvider)solidModelProvider).updateCalculatedProperties(calculatedParameters);
- }
- }
-
- private void createGeometry() {
- Collection<TopoDS_Shape> shapes;
- try {
- shapes = solidModelProvider.getModel();
- if (shapes.size() == 1) {
- solidModel = shapes.iterator().next();
- } else {
- solidModel = OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));
- for (TopoDS_Shape shape : shapes) {
- shape.delete();
- }
- }
-// shapes.clear();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+ component.updateParameters();
}
+
public Map<String, Object> getParameterMap() {
- return Collections.unmodifiableMap(currentParameters);
+ return component.getParameterMap();
}
public Map<String, Object> getCalculatedParameters() {
- return calculatedParameters;
+ return component.getCalculatedParameters();
}
public void setParametersUpdated(boolean parametersUpdated) {
- this.parametersUpdated = parametersUpdated;
+ component.setParametersUpdated(parametersUpdated);
}
public void setParameterMap(Map<String, Object> parameters) {
- for (String id : parameters.keySet()) {
- Object currentValue = currentParameters.get(id);
- Object newValue = parameters.get(id);
- if (currentValue == newValue)
- continue;
- if (currentValue instanceof Double) {
- if (Math.abs((Double)currentValue-(Double)newValue) < MathTools.NEAR_ZERO)
- continue;
- }
- currentParameters.put(id, newValue);
- parametersUpdated = true;
+ Set<String> ids = component.setParameterMap(parameters);
+ for (String id : ids) {
firePropertyChanged(id);
}
}
- private SolidModelProvider solidModelProvider;
-
@RelatedGetObj(Plant3D.URIs.hasGeometry)
- public SolidModelProvider getGeometry() {
- return solidModelProvider;
+ public GeometryProvider getGeometry() {
+ return component.getGeometry();
}
@RelatedSetObj(Plant3D.URIs.hasGeometry)
- public void setGeometry(final SolidModelProvider provider) {
- if (provider != null && provider.equals(solidModelProvider))
+ public void setGeometry(final GeometryProvider provider) {
+ if (provider != null && provider.equals(component.getGeometry()))
return;
-
- if (solidModelProvider != null) {
- deleteData();
- }
- solidModelProvider = provider;
+ component.setGeometry(provider);
firePropertyChanged(Plant3D.URIs.hasGeometry);
}
-
- private void deleteData() {
- solidModelProvider = null;
- if (solidObject != null) {
- solidObject.clearActors();
- solidObject = null;
- }
- if (solidModel != null) {
- solidModel.delete();
- solidModel = null;
- }
- }
-
+
@Override
public Collection<vtkProp3D> getActors() {
- List<vtkProp3D> list = new ArrayList<vtkProp3D>();
- if (solidObject != null)
- list.addAll(solidObject.getActors());
- return list;
+ return component.getActors();
}
@Override
public void stopVisualize() {
- if (solidObject != null) {
- solidObject.clearActorsAWT();
- solidObject = null;
- }
- if (solidModel != null) {
- solidModel.delete();
- solidModel = null;
- }
-
+ component.stopVisualize();
}
- private boolean selected = false;
- private boolean hover = false;
-
@Override
public void highlight(HighlightEventType type) {
- if (type == HighlightEventType.Selection || type == HighlightEventType.ClearSelection) {
- selected = type == HighlightEventType.Selection;
-// hingeA.visualizeSolid(selected,true,false);
-// hingeB.visualizeSolid(selected,true,false);
- updateVisuals(true, false);
-// update(null);
- } else {
- hover = type == HighlightEventType.Hover;
- updateVisuals(false, true);
- }
-
+ component.highlight(type);
}
return new double[]{1,1,0};
}
- private void updateVisuals(boolean s, boolean h) {
- if (solidObject != null) {
- if (s) {
- double color[];
- if (selected) {
- color = getSelectedColor();
-
- } else {
- color = getColor();
-
- }
- for (vtkProp3D prop : solidObject.getSolid()) {
- vtkProperty property = ((vtkActor)prop).GetProperty();
- property.SetColor(color);
- property.Delete();
- }
- }
- if (h) {
- double color[] = new double[]{0,0,0};
- if (hover)
- color = new double[]{1,0,1};
- for (vtkProp3D prop : solidObject.getEdges()) {
- vtkProperty property = ((vtkActor)prop).GetProperty();
- property.SetColor(color);
- property.Delete();
- }
-
- }
- } else {
-// if (s) {
-// axes.addToRenderer();
-// axes.setAxesVisibility(selected);
-// }
- }
- }
-
public void update(vtkRenderer ren) {
vtkUtil.updateTransform(getActors(), getWorldPosition(), getWorldOrientation());
}
-
- public TopoDS_Shape getSolidModel() {
- return solidModel;
- }
+
}
package org.simantics.plant3d.scenegraph;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
import java.util.Map;
+import java.util.Set;
-import org.jcae.opencascade.jni.TopoDS_Shape;
-import org.simantics.g3d.math.MathTools;
+import org.simantics.g3d.scenegraph.GeometryProvider;
import org.simantics.g3d.scenegraph.NodeHighlighter;
import org.simantics.g3d.vtk.utils.vtkUtil;
import org.simantics.objmap.graph.annotations.RelatedGetObj;
import org.simantics.objmap.graph.annotations.RelatedSetObj;
-import org.simantics.opencascade.OccTriangulator;
-import org.simantics.opencascade.ParametricSolidModelProvider;
-import org.simantics.opencascade.SolidModelProvider;
-import org.simantics.opencascade.vtk.vtkSolidObject;
import org.simantics.plant3d.ontology.Plant3D;
-import vtk.vtkActor;
import vtk.vtkPanel;
import vtk.vtkProp3D;
-import vtk.vtkProperty;
import vtk.vtkRenderer;
public class P3DParentGeometryNode<T extends IP3DNode> extends P3DParentNode<T> implements ParameterizedNode, NodeHighlighter{
- private TopoDS_Shape solidModel;
- private vtkSolidObject solidObject;
-
- private Map<String,Object> currentParameters;
- private Map<String,Object> calculatedParameters;
-
- private boolean parametersUpdated = true;
+
+ private GeometryComponent component;
public P3DParentGeometryNode() {
- currentParameters = new HashMap<String, Object>();
- calculatedParameters = new HashMap<String, Object>();
+ component = new GeometryComponent() {
+ @Override
+ protected double[] getColor() {
+ return P3DParentGeometryNode.this.getColor();
+ }
+
+ @Override
+ protected double[] getSelectedColor() {
+ return P3DParentGeometryNode.this.getSelectedColor();
+ }
+ };
}
@Override
public void visualize(vtkPanel ren) {
- if (solidModelProvider != null) {
-
- updateParameters();
-
- if (solidModel == null || parametersUpdated) {
- createGeometry();
- }
- }
- if ((solidObject == null || parametersUpdated) && solidModel != null) {
- solidObject = new vtkSolidObject(ren, solidModel);
- }
- if (solidObject != null) {
- solidObject.visualizeSolid(true,true, false,false);
- updateVisuals(true, true);
- }
- parametersUpdated = false;
-
+ updateParameters();
+ component.visualize(ren);
}
public void updateParameters() {
- if (solidModelProvider instanceof ParametricSolidModelProvider) {
- ((ParametricSolidModelProvider)solidModelProvider).setProperties(currentParameters);
- ((ParametricSolidModelProvider)solidModelProvider).updateCalculatedProperties(calculatedParameters);
- }
+ component.updateParameters();
}
-
- private void createGeometry() {
- Collection<TopoDS_Shape> shapes;
- try {
- shapes = solidModelProvider.getModel();
- if (shapes.size() == 1) {
- solidModel = shapes.iterator().next();
- } else {
- solidModel = OccTriangulator.makeCompound(shapes.toArray(new TopoDS_Shape[shapes.size()]));
- for (TopoDS_Shape shape : shapes) {
- shape.delete();
- }
- }
-// shapes.clear();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+
+ public Map<String, Object> getParameterMap() {
+ return component.getParameterMap();
}
-
-
- public Map<String, Object> getParameterMap() {
- return Collections.unmodifiableMap(currentParameters);
+ public Map<String, Object> getCalculatedParameters() {
+ return component.getCalculatedParameters();
}
public void setParameterMap(Map<String, Object> parameters) {
- for (String id : parameters.keySet()) {
- Object currentValue = currentParameters.get(id);
- Object newValue = parameters.get(id);
- if (currentValue == newValue)
- continue;
- if (currentValue instanceof Double) {
- if (Math.abs((Double)currentValue-(Double)newValue) < MathTools.NEAR_ZERO)
- continue;
- }
- currentParameters.put(id, newValue);
- parametersUpdated = true;
+ Set<String> ids = component.setParameterMap(parameters);
+ for (String id : ids) {
firePropertyChanged(id);
}
}
-
-
- private SolidModelProvider solidModelProvider;
+
@RelatedGetObj(Plant3D.URIs.hasGeometry)
- public SolidModelProvider getGeometry() {
- return solidModelProvider;
+ public GeometryProvider getGeometry() {
+ //return geometryProvider;
+ return component.getGeometry();
}
@RelatedSetObj(Plant3D.URIs.hasGeometry)
- public void setGeometry(final SolidModelProvider provider) {
- if (provider != null && provider.equals(solidModelProvider))
+ public void setGeometry(final GeometryProvider provider) {
+ if (provider != null && provider.equals(component.getGeometry()))
return;
-
- if (solidModelProvider != null) {
- deleteData();
- }
- solidModelProvider = provider;
+ component.setGeometry(provider);
firePropertyChanged(Plant3D.URIs.hasGeometry);
}
- private void deleteData() {
- solidModelProvider = null;
- if (solidObject != null) {
- solidObject.clearActors();
- solidObject = null;
- }
- if (solidModel != null) {
- solidModel.delete();
- solidModel = null;
- }
- }
@Override
public Collection<vtkProp3D> getActors() {
- List<vtkProp3D> list = new ArrayList<vtkProp3D>();
- if (solidObject != null)
- list.addAll(solidObject.getActors());
- return list;
+ return component.getActors();
}
@Override
public void stopVisualize() {
- if (solidObject != null) {
- solidObject.clearActorsAWT();
- solidObject = null;
- }
- if (solidModel != null) {
- solidModel.delete();
- solidModel = null;
- }
-
+ component.stopVisualize();
}
-
- private boolean selected = false;
- private boolean hover = false;
-
+
@Override
public void highlight(HighlightEventType type) {
- if (type == HighlightEventType.Selection || type == HighlightEventType.ClearSelection) {
- selected = type == HighlightEventType.Selection;
-// hingeA.visualizeSolid(selected,true,false);
-// hingeB.visualizeSolid(selected,true,false);
- updateVisuals(true, false);
-// update(null);
- } else {
- hover = type == HighlightEventType.Hover;
- updateVisuals(false, true);
- }
-
-
+ component.highlight(type);
}
protected double[] getSelectedColor() {
return new double[]{1,1,0};
}
- private void updateVisuals(boolean s, boolean h) {
- if (solidObject != null) {
- if (s) {
- double color[];
- if (selected) {
- color = getSelectedColor();
-
- } else {
- color = getColor();
- }
- for (vtkProp3D prop : solidObject.getSolid()) {
- vtkProperty property = ((vtkActor)prop).GetProperty();
- property.SetColor(color);
- property.Delete();
- }
- }
- if (h) {
- double color[] = new double[]{0,0,0};
- if (hover)
- color = new double[]{1,0,1};
- for (vtkProp3D prop : solidObject.getEdges()) {
- vtkProperty property = ((vtkActor)prop).GetProperty();
- property.SetColor(color);
- property.Delete();
- }
-
- }
- } else {
-// if (s) {
-// axes.addToRenderer();
-// axes.setAxesVisibility(selected);
-// }
- }
- }
public void update(vtkRenderer ren) {
vtkUtil.updateTransform(getActors(), getWorldPosition(), getWorldOrientation());
}
-
- public TopoDS_Shape getSolidModel() {
- return solidModel;
- }
+
}
}
@RelatedGetObj(Plant3D.URIs.HasAlternativePipeRun)
- public PipeRun getAlternaitvePipeRun() {
+ public PipeRun getAlternativePipeRun() {
return alternativePipeRun;
}
import org.simantics.db.Resource;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.request.Read;
+import org.simantics.g3d.scenegraph.GeometryProvider;
import org.simantics.objmap.graph.schema.IMappingSchema;
import org.simantics.objmap.graph.schema.MappingSchemas;
import org.simantics.objmap.graph.schema.SimpleSchema;
-import org.simantics.opencascade.SolidModelProvider;
import org.simantics.plant3d.ontology.Plant3D;
public class SchemaBuilder {
schema.addLinkType(MappingSchemas.fromAnnotations(g, P3DRootNode.class));
schema.addLinkType(MappingSchemas.fromAnnotations(g, PipeRun.class));
- schema.addLinkType(MappingSchemas.fromAdaptable(g, Plant3D.URIs.Builtin_GeometryProvider, SolidModelProvider.class));
+ schema.addLinkType(MappingSchemas.fromAdaptable(g, Plant3D.URIs.Builtin_GeometryProvider, GeometryProvider.class));
return schema;
} catch (IllegalAccessException e) {
throw new DatabaseException(e);
import org.simantics.db.Resource;
import org.simantics.db.common.request.ReadRequest;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.g3d.scenegraph.GeometryProvider;
import org.simantics.layer0.Layer0;
-import org.simantics.opencascade.SolidModelProvider;
import org.simantics.plant3d.ontology.Plant3D;
import org.simantics.plant3d.scenegraph.EndComponent;
import org.simantics.plant3d.scenegraph.InlineComponent;
private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
- private static Map<String,SolidModelProvider> providers = new HashMap<String,SolidModelProvider>();
+ private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
public static void preloadCache() {
Simantics.getSession().asyncRequest(new ReadRequest() {
});
}
- private static SolidModelProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
+ private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
Layer0 l0 = Layer0.getInstance(graph);
Plant3D p3d = Plant3D.getInstance(graph);
}
}
if (geom != null) {
- SolidModelProvider provider = graph.adapt(geom, SolidModelProvider.class);
+ GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
return provider;
}
return null;
Plant3D p3d = Plant3D.getInstance(graph);
Resource type = graph.getResource(typeURI);
- SolidModelProvider provider = getProvider(graph, type);
+ GeometryProvider provider = getProvider(graph, type);
if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
providers.put(typeURI, provider);
ComponentUtils.clazzes.put(typeURI,getClazz(graph, type));
public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
Class<? extends PipelineComponent> type = clazzes.get(typeURI);
- SolidModelProvider provider = providers.get(typeURI);
+ GeometryProvider provider = providers.get(typeURI);
if (type == null || provider == null) {
load(typeURI);
type = clazzes.get(typeURI);