]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/geometry/ReducerGeometryProvider.java
Added support for eccentric reducers
[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 //              GP_Circ circ = new GP_Circ(new double[]{-length*0.5, 0.0, 0.0,1.0,0.0,0.0}, radius);
34 //              GP_Circ circ2 = new GP_Circ(new double[]{length*0.5, 0.0, 0.0,1.0,0.0,0.0}, radius2);
35 //              System.out.println("Reducer " + length  + " " + radius + " " + radius2);
36                 double length = Math.max(0.1, Math.abs(radius-radius2)*4.0);
37                 TopoDS_Shape shape;
38                 if (Math.abs(radius-radius2) < MathTools.NEAR_ZERO) {
39                         shape = OccTriangulator.makeCylinder(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius, length);
40                 } else {
41                         shape = OccTriangulator.makeCone(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius,radius2, length);
42                 }
43                 return Collections.singletonList(shape);
44         }
45         
46         @Override
47         public Mesh getMesh() {
48                 double length = Math.max(0.1, Math.abs(radius-radius2)*4.0);
49                 if (length < .0001)
50                         return null;
51                 Tube tube = new Tube();
52                 tube.setResolution(16);
53                 List<Tuple3d> vertices = new ArrayList<Tuple3d>();
54                 List<Double> radius = new ArrayList<Double>();
55                 List<Vector3d> tangents = new ArrayList<Vector3d>();
56                 vertices.add(new Point3d(-length*0.5, 0.0, 0.0));
57                 vertices.add(new Point3d( length*0.5, offset, 0.0));
58                 radius.add(this.radius);
59                 radius.add(this.radius2);
60                 tangents.add(new Vector3d(1.0,0.0,0.0));
61                 tangents.add(new Vector3d(1.0,0.0,0.0));
62                 tube.setVertices(vertices);
63                 tube.setRadiis(radius);
64                 tube.setTangents(tangents);
65                 tube.setCap(false);
66                 return tube.create();
67         }
68         
69         @Override
70         public void setProperties(Map<String, Object> props) {
71
72                 
73                 if (props.containsKey("radius")) {
74                         radius = (Double)props.get("radius");
75                 }
76                 if (props.containsKey("radius2")) {
77                         radius2 = (Double)props.get("radius2");
78                 }
79                 
80                 if (props.containsKey("offset")) {
81                         offset = (Double)props.get("offset");
82                 }
83                 
84                 
85                 
86         }
87         
88         @Override
89         public void updateCalculatedProperties(Map<String, Object> returnProps) {
90                 returnProps.put("length", Math.max(0.1, Math.abs(radius-radius2)*4.0));
91                 
92         }
93
94 }