]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Support for capped tube 05/3005/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 4 Jul 2019 15:09:53 +0000 (18:09 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 4 Jul 2019 15:09:53 +0000 (18:09 +0300)
Also, added possibility to change length wise resolution of ArcCylinder

gitlab #7

Change-Id: I61600b0d07dda44ac9a692138927116cf491af1f

org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java
org.simantics.g3d/src/org/simantics/g3d/shape/Tube.java

index ab6178440ab04a8772dd25902eb59cdfcc0d0dd9..348fd8520cc5f27850b163454f7987eee8b60d53 100644 (file)
@@ -12,8 +12,12 @@ import javax.vecmath.Vector3d;
 import org.simantics.g3d.math.MathTools;
 
 public class ArcCylinder {
+       
+       public Mesh create(Point3d s, Point3d v, Point3d e, double r, int res) {
+               return create(s, v, e, r, res, 0.15);
+       }
 
-       public Mesh create(Point3d s, Point3d v, Point3d e, double rad, int res) {
+       public Mesh create(Point3d s, Point3d v, Point3d e, double r, int res, double res2) {
                
                Vector3d v1 = new Vector3d(s);
                v1.sub(v);
@@ -49,7 +53,7 @@ public class ArcCylinder {
                        rn.cross(v2, v1);
                        rn.normalize();
                        
-                       steps = (int)(Math.ceil(a/0.1));
+                       steps = (int)(Math.ceil(a/res2));
                        if (steps == 0)
                                steps = 1;
                        sa = a/steps;
@@ -83,7 +87,7 @@ public class ArcCylinder {
                                p.add(c);
                                
                        }
-                       createCircle(vertices, normals, p, t, rn, res, rad);
+                       createCircle(vertices, normals, p, t, rn, res, r);
                }
                int count = steps*res*6;
                for (int i = 0; i < count; i++) {
index 828fd2690c95e648d1c162c4701aca97eb68c67a..0300f068b601711001d5413e1fee21dd05a00902 100644 (file)
@@ -29,6 +29,8 @@ public class Tube {
        Double radius = 1.0;
        int resolution = 8;
        
+       boolean cap = false;
+       
        
        public void setResolution(int resolution) {
                if (resolution > 2)
@@ -56,6 +58,10 @@ public class Tube {
        }
        
        
+       public void setCap(boolean cap) {
+               this.cap = cap;
+       }
+       
        
        public Mesh create() {
                if (vertices.size() < 2 )
@@ -66,18 +72,43 @@ public class Tube {
                for (int i = 0; i < vertices.size() - 1; i++) {
                        t.set(vertices.get(i+1));
                        t.sub(vertices.get(i));
-                       if (t.lengthSquared() < 0.0001)
+                       if (t.lengthSquared() < 0.000001)
                                throw new IllegalArgumentException("vertices at index " + i + " are too close to each other");
                }
                
-               List<Vector3d> points = new ArrayList<Vector3d>(vertices.size()*resolution);
-               List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size()*resolution);
+               int vcount = vertices.size()*resolution;
+               int icount = (vertices.size()-1)*resolution*6;
+               
+               if (cap) {
+                       vcount+=2;
+                       icount+=resolution * 6;
+               }
+               
+               List<Vector3d> points = new ArrayList<Vector3d>(vcount);
+               List<Vector3d> normals = new ArrayList<Vector3d>(vcount);
                
                for (int i = 0; i < vertices.size(); i++) {
                        createCircle(i,points,normals);
                }
+               if (cap) {
+                       int l = vertices.size()-1;
+                       points.add(new Vector3d(vertices.get(0)));
+                       points.add(new Vector3d(vertices.get(l)));
+                       Vector3d n1 = new Vector3d(vertices.get(1));
+                       n1.sub(vertices.get(0));
+                       n1.normalize();
+                       normals.add(n1);
+                       
+                       Vector3d n2 = new Vector3d(vertices.get(l-1));
+                       n2.sub(vertices.get(l));
+                       n2.normalize();
+                       normals.add(n2);
+               }
+               
                
-               int index[] = new int[(vertices.size()-1)*resolution*6];
+               
+
+               int index[] = new int[icount];
                
                createIndices(index);
                List<Integer> indices = new ArrayList<Integer>();
@@ -187,6 +218,42 @@ public class Tube {
                                }
                        }
                }
+               if (cap) {
+                       int c = 0;
+                       int isi = ((vertices.size()-1) * resolution) * 6;
+                       int ivi = vertices.size() * resolution;
+                       for (int s = 0; s < resolution; s++) {
+                               int ii = isi + s*3;
+                               int iv = c*resolution + s;
+                               if (s < resolution - 1) {
+                                       index[ii+2] = iv;
+                                       index[ii+1] = iv+1;
+                                       index[ii+0] = ivi;
+                               } else {
+                                       index[ii+2] = iv;
+                                       index[ii+1] = iv+1-resolution;
+                                       index[ii+0] = ivi;
+                               }
+                               
+                       }
+                       isi += resolution * 3;
+                       c = (vertices.size()-1);
+                       for (int s = 0; s < resolution; s++) {
+                               int ii = isi + s*3;
+                               int iv = c*resolution + s;
+                               if (s < resolution - 1) {
+                                       index[ii+2] = iv;
+                                       index[ii+1] = iv+1;
+                                       index[ii+0] = ivi+1;
+                               } else {
+                                       index[ii+2] = iv;
+                                       index[ii+1] = iv+1-resolution;
+                                       index[ii+0] = ivi+1;
+                               }
+                               
+                       }
+                       
+               }
        }
 
 }