]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/geometry/ReducerGeometryProvider.java
Merge "Allow property editing with just a single mouse click"
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / geometry / ReducerGeometryProvider.java
1 package org.simantics.plant3d.geometry;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.vecmath.Point3d;
10 import javax.vecmath.Tuple3d;
11 import javax.vecmath.Vector3d;
12
13 import org.jcae.opencascade.jni.TopoDS_Shape;
14 import org.simantics.db.Resource;
15 import org.simantics.g3d.math.MathTools;
16 import org.simantics.g3d.shape.Mesh;
17 import org.simantics.g3d.shape.Tube;
18 import org.simantics.opencascade.OccTriangulator;
19
20 //public class ReducerGeometryProvider extends BuiltinGeometryProvider  {
21 public class ReducerGeometryProvider extends BuiltinMeshProvider  {
22         
23         public ReducerGeometryProvider(Resource resource) {
24                 super(resource);
25         }
26
27         private double radius = 0.01;
28         private double radius2 = 0.02;
29         private double offset = 0.0;
30         
31 //      @Override
32         public Collection<TopoDS_Shape> getModel() throws Exception {
33                 if (radius < MathTools.NEAR_ZERO || radius2 < MathTools.NEAR_ZERO)
34                         return Collections.emptyList();
35                 double length = Math.max(0.1, Math.abs(radius-radius2)*4.0);
36                 TopoDS_Shape shape;
37                 if (Math.abs(radius-radius2) < MathTools.NEAR_ZERO) {
38                         shape = OccTriangulator.makeCylinder(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius, length);
39                 } else {
40                         shape = OccTriangulator.makeCone(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius,radius2, length);
41                 }
42                 return Collections.singletonList(shape);
43         }
44         
45         @Override
46         public Mesh getMesh() {
47                 double length = Math.max(0.1, Math.abs(radius-radius2)*4.0);
48                 if (length < .0001 || radius < MathTools.NEAR_ZERO || radius2 < MathTools.NEAR_ZERO)
49                         return null;
50                 Tube tube = new Tube();
51                 tube.setResolution(16);
52                 List<Tuple3d> vertices = new ArrayList<Tuple3d>();
53                 List<Double> radius = new ArrayList<Double>();
54                 List<Vector3d> tangents = new ArrayList<Vector3d>();
55                 vertices.add(new Point3d(-length*0.5, 0.0, 0.0));
56                 vertices.add(new Point3d( length*0.5, offset, 0.0));
57                 radius.add(this.radius);
58                 radius.add(this.radius2);
59                 tangents.add(new Vector3d(1.0,0.0,0.0));
60                 tangents.add(new Vector3d(1.0,0.0,0.0));
61                 tube.setVertices(vertices);
62                 tube.setRadiis(radius);
63                 tube.setTangents(tangents);
64                 tube.setCap(false);
65                 return tube.create();
66         }
67         
68         @Override
69         public void setProperties(Map<String, Object> props) {
70
71                 
72                 if (props.containsKey("radius")) {
73                         radius = (Double)props.get("radius");
74                 }
75                 if (props.containsKey("radius2")) {
76                         radius2 = (Double)props.get("radius2");
77                 }
78                 
79                 if (props.containsKey("offset")) {
80                         offset = (Double)props.get("offset");
81                 }
82                 
83                 
84                 
85         }
86         
87         @Override
88         public void updateCalculatedProperties(Map<String, Object> returnProps) {
89                 returnProps.put("length", Math.max(0.1, Math.abs(radius-radius2)*4.0));
90                 
91         }
92
93 }