JavaCAD
RoundedCylinder.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package eu.mihosoft.vrl.v3d;
7 
8 import java.util.ArrayList;
9 import java.util.List;
10 
11 import eu.mihosoft.vrl.v3d.ext.quickhull3d.HullUtil;
12 
13 // TODO: Auto-generated Javadoc
19 public class RoundedCylinder extends Primitive {
20 
23 
25  private double cornerRadius = 2;
26 
28  private int resolution = 30;
29 
30  private double startRadius;
31 
32  private double endRadius;
33 
34  private double height;
35 
36  private int numCornerSlices;
37 
49  public RoundedCylinder(double startRadius, double endRadius, double height, int numSlices) {
50  this.startRadius = startRadius;
51  this.endRadius = endRadius;
52  this.height = height;
53  this.resolution = numSlices;
55  }
56 
66  public RoundedCylinder(double startRadius, double height) {
67  this.startRadius = startRadius;
68  this.endRadius = startRadius;
69  this.height = height;
70  this.resolution = 30;
72  }
73 
74 
75  /* (non-Javadoc)
76  * @see eu.mihosoft.vrl.v3d.Primitive#toPolygons()
77  */
78  @Override
79  public List<Polygon> toPolygons() {
80  double minHeight = height-cornerRadius*2;
81  ArrayList<CSG> cylParts =new ArrayList<>();
82 
83  for(int i=0;i<numCornerSlices;i++){
84  double radIncs = startRadius-cornerRadius+Math.sin(Math.PI/2*((double)i/(double)numCornerSlices))*cornerRadius;
85  double radInce = endRadius-cornerRadius+Math.sin(Math.PI/2*((double)i/(double)numCornerSlices))*cornerRadius;
86 
87  double heightInc = (Math.cos(Math.PI/2*((double)i/(double)(numCornerSlices)))*cornerRadius);
88  cylParts.add(
89  new Cylinder(radIncs, // Radius at the bottom
90  radInce, // Radius at the top
91  heightInc*2+minHeight, // Height
92  (int)resolution //resolution
93  )
94  .toCSG()
95  .movez(-heightInc)
96  );
97  }
98  return HullUtil.hull(cylParts).toZMin().getPolygons();
99  }
100 
101  /* (non-Javadoc)
102  * @see eu.mihosoft.vrl.v3d.Primitive#getProperties()
103  */
104  @Override
106  return properties;
107  }
108 
109 
115  public int getResolution() {
116  return resolution;
117  }
118 
126  this.resolution = resolution;
127  return this;
128  }
129 
135  public double getCornerRadius() {
136  return cornerRadius;
137  }
138 
146  this.cornerRadius = cornerRadius;
147  return this;
148  }
149 
150 
151 
152  public int getNumCornerSlices() {
153  return numCornerSlices;
154  }
155 
156 
157 
159  this.numCornerSlices = numCornerSlices;
160  return this;
161  }
162 
163 }
CSG movez(Number howFarToMove)
Definition: CSG.java:439
List< Polygon > getPolygons()
Definition: CSG.java:698
CSG toZMin(CSG target)
Definition: CSG.java:298
RoundedCylinder(double startRadius, double height)
RoundedCylinder resolution(int resolution)
RoundedCylinder(double startRadius, double endRadius, double height, int numSlices)
RoundedCylinder cornerRadius(double cornerRadius)
RoundedCylinder setNumCornerSlices(int numCornerSlices)