JavaCAD
Cylinder.java
Go to the documentation of this file.
1 
34 package eu.mihosoft.vrl.v3d;
35 
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 
40 import eu.mihosoft.vrl.v3d.parametrics.LengthParameter;
41 
42 // TODO: Auto-generated Javadoc
51 public class Cylinder extends Primitive {
52  private static final double MINIMUM_RADIUS=0.001;
53  private Vector3d start;
54 
56  private Vector3d end;
57 
59  private double startRadius;
60 
62  private double endRadius;
63  private static int defaultNumSlices=16;
64 
67 
70 
76  public Cylinder() {
77  this.start = new Vector3d(0, -0.5, 0);
78  this.end = new Vector3d(0, 0.5, 0);
79  this.startRadius = 1;
80  this.endRadius = 1;
81  this.numSlices = defaultNumSlices;
82  }
83 
94  public Cylinder(Vector3d start, Vector3d end, double radius, int numSlices) {
95  this.start = start;
96  this.end = end;
97  this.startRadius = radius<MINIMUM_RADIUS?MINIMUM_RADIUS:radius;
98  this.endRadius = radius<MINIMUM_RADIUS?MINIMUM_RADIUS:radius;
99  this.numSlices = numSlices;
100  }
101 
114  this.start = start;
115  this.end = end;
118  this.numSlices = numSlices;
119  }
120 
131  public Cylinder(double radius, double height, int numSlices) {
132  this.start = Vector3d.ZERO;
133  this.end = Vector3d.Z_ONE.times(height);
134  this.startRadius = radius<MINIMUM_RADIUS?MINIMUM_RADIUS:radius;
135  this.endRadius = radius<MINIMUM_RADIUS?MINIMUM_RADIUS:radius;
136  this.numSlices = numSlices;
137  }
138 
150  public Cylinder(double startRadius, double endRadius, double height, int numSlices) {
151  this.start = Vector3d.ZERO;
152  this.end = Vector3d.Z_ONE.times(height);
155  this.numSlices = numSlices;
156  }
166  public Cylinder(double radius, double height) {
167  this.start = Vector3d.ZERO;
168  this.end = Vector3d.Z_ONE.times(height);
169  this.startRadius = radius<MINIMUM_RADIUS?MINIMUM_RADIUS:radius;
170  this.endRadius = radius<MINIMUM_RADIUS?MINIMUM_RADIUS:radius;
171  }
172 
183  public Cylinder(double startRadius, double endRadius, double height) {
184  this.start = Vector3d.ZERO;
185  this.end = Vector3d.Z_ONE.times(height);
188  }
190  this(startRadius.getMM(),endRadius.getMM(),height.getMM(),numSlices);
191  parametrics.add(startRadius);
192  parametrics.add(endRadius);
193  parametrics.add(height);
194  }
196  this(startRadius,startRadius,height,numSlices);
197  }
199  this(startRadius.getMM(),endRadius.getMM(),height.getMM());
200  parametrics.add(startRadius);
201  parametrics.add(endRadius);
202  parametrics.add(height);
203  }
205  this(startRadius,startRadius,height);
206  }
207  /* (non-Javadoc)
208  * @see eu.mihosoft.vrl.v3d.Primitive#toPolygons()
209  */
210  @Override
211  public List<Polygon> toPolygons() {
212  if(startRadius<=0)
213  throw new NumberFormatException("startRadius can not be negative");
214  if(endRadius<=0)
215  throw new NumberFormatException("endRadius can not be negative");
216  if(numSlices<3)
217  throw new NumberFormatException("Slices can not be less than 3");
218 
219  final Vector3d s = getStart();
220  Vector3d e = getEnd();
221  final Vector3d ray = e.minus(s);
222  if(ray.z<=0)
223  throw new NumberFormatException("Z can not be negative");
224 
225  final Vector3d axisZ = ray.normalized();
226  boolean isY = (Math.abs(axisZ.y) > 0.5);
227  final Vector3d axisX = new Vector3d(isY ? 1 : 0, !isY ? 1 : 0, 0).
228  cross(axisZ).normalized();
229  final Vector3d axisY = axisX.cross(axisZ).normalized();
230  Vertex startV = new Vertex(s, axisZ.negated());
231  Vertex endV = new Vertex(e, axisZ.normalized());
232  List<Polygon> polygons = new ArrayList<>();
233 
234  for (int i = 0; i < numSlices; i++) {
235  double t0 = i / (double) numSlices, t1 = (i + 1) / (double) numSlices;
236  polygons.add(new Polygon(Arrays.asList(
237  startV,
238  cylPoint(axisX, axisY, axisZ, ray, s, startRadius, 0, t0, -1),
239  cylPoint(axisX, axisY, axisZ, ray, s, startRadius, 0, t1, -1)),
240  properties
241  ));
242  polygons.add(new Polygon(Arrays.asList(
243  cylPoint(axisX, axisY, axisZ, ray, s, startRadius, 0, t1, 0),
244  cylPoint(axisX, axisY, axisZ, ray, s, startRadius, 0, t0, 0),
245  cylPoint(axisX, axisY, axisZ, ray, s, endRadius, 1, t0, 0),
246  cylPoint(axisX, axisY, axisZ, ray, s, endRadius, 1, t1, 0)),
247  properties
248  ));
249  polygons.add(new Polygon(
250  Arrays.asList(
251  endV,
252  cylPoint(axisX, axisY, axisZ, ray, s, endRadius, 1, t1, 1),
253  cylPoint(axisX, axisY, axisZ, ray, s, endRadius, 1, t0, 1)),
254  properties
255  ));
256  }
257 
258  return polygons;
259  }
260 
275  private Vertex cylPoint(
276  Vector3d axisX, Vector3d axisY, Vector3d axisZ, Vector3d ray, Vector3d s,
277  double r, double stack, double slice, double normalBlend) {
278  double angle = slice * Math.PI * 2;
279  Vector3d out = axisX.times(Math.cos(angle)).plus(axisY.times(Math.sin(angle)));
280  Vector3d pos = s.plus(ray.times(stack)).plus(out.times(r));
281  Vector3d normal = out.times(1.0 - Math.abs(normalBlend)).plus(axisZ.times(normalBlend));
282  return new Vertex(pos, normal);
283  }
284 
290  public Vector3d getStart() {
291  return start;
292  }
293 
299  public void setStart(Vector3d start) {
300  this.start = start;
301  }
302 
308  public Vector3d getEnd() {
309  return end;
310  }
311 
317  public void setEnd(Vector3d end) {
318  this.end = end;
319  }
320 
326  public double getStartRadius() {
327  return startRadius;
328  }
329 
335  public void setStartRadius(double radius) {
336  this.startRadius = radius;
337  }
338 
344  public double getEndRadius() {
345  return endRadius;
346  }
347 
353  public void setEndRadius(double radius) {
354  this.endRadius = radius;
355  }
356 
362  public int getNumSlices() {
363  return numSlices;
364  }
365 
371  public void setNumSlices(int numSlices) {
372  this.numSlices = numSlices;
373  }
374 
375  /* (non-Javadoc)
376  * @see eu.mihosoft.vrl.v3d.Primitive#getProperties()
377  */
378  @Override
380  return properties;
381  }
382 
383 
384 
385 }
static final double MINIMUM_RADIUS
Definition: Cylinder.java:52
List< Polygon > toPolygons()
Definition: Cylinder.java:211
Cylinder(double startRadius, double endRadius, double height, int numSlices)
Definition: Cylinder.java:150
Cylinder(double radius, double height, int numSlices)
Definition: Cylinder.java:131
Cylinder(double radius, double height)
Definition: Cylinder.java:166
Cylinder(double startRadius, double endRadius, double height)
Definition: Cylinder.java:183
Cylinder(LengthParameter startRadius, LengthParameter height, int numSlices)
Definition: Cylinder.java:195
Vertex cylPoint(Vector3d axisX, Vector3d axisY, Vector3d axisZ, Vector3d ray, Vector3d s, double r, double stack, double slice, double normalBlend)
Definition: Cylinder.java:275
void setStart(Vector3d start)
Definition: Cylinder.java:299
void setStartRadius(double radius)
Definition: Cylinder.java:335
void setEndRadius(double radius)
Definition: Cylinder.java:353
final PropertyStorage properties
Definition: Cylinder.java:69
Cylinder(LengthParameter startRadius, LengthParameter height)
Definition: Cylinder.java:204
Cylinder(Vector3d start, Vector3d end, double radius, int numSlices)
Definition: Cylinder.java:94
void setEnd(Vector3d end)
Definition: Cylinder.java:317
Cylinder(Vector3d start, Vector3d end, double startRadius, double endRadius, int numSlices)
Definition: Cylinder.java:113
void setNumSlices(int numSlices)
Definition: Cylinder.java:371
Cylinder(LengthParameter startRadius, LengthParameter endRadius, LengthParameter height)
Definition: Cylinder.java:198
Cylinder(LengthParameter startRadius, LengthParameter endRadius, LengthParameter height, int numSlices)
Definition: Cylinder.java:189
PropertyStorage getProperties()
Definition: Cylinder.java:379
Vector3d cross(Vector3d a)
Definition: Vector3d.java:291
Vector3d times(double a)
Definition: Vector3d.java:191
Vector3d plus(Vector3d v)
Definition: Vector3d.java:165
static Vector3d y(double y)
Definition: Vector3d.java:484
static final Vector3d ZERO
Definition: Vector3d.java:57
Vector3d minus(Vector3d v)
Definition: Vector3d.java:178
static Vector3d z(double z)
Definition: Vector3d.java:494
static final Vector3d Z_ONE
Definition: Vector3d.java:69