JavaCAD
Sphere.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.List;
38 
39 import eu.mihosoft.vrl.v3d.parametrics.LengthParameter;
40 
41 // TODO: Auto-generated Javadoc
50 public class Sphere extends Primitive {
51 
52  private static final int NUM_SLICES = 16;
53 
54  private static final int NUM_STACKS = 8;
55 
57  private Vector3d center;
58 
60  private double radius;
61 
63  private int numSlices;
64 
66  private int numStacks;
67 
70 
76  public Sphere() {
77  init();
78  }
79 
86  public Sphere(double radius) {
87  init();
88  this.radius = radius;
89  }
90 // public Cube(LengthParameter w, LengthParameter h, LengthParameter d) {
91 // this(Vector3d.ZERO, new Vector3d(w.getMM(), h.getMM(), d.getMM()));
92 //
93 // }
94  public Sphere(LengthParameter size) {
95  this(size.getMM());
96  parametrics.add(size);
97  }
98  public Sphere(LengthParameter size, int numSlices, int numStacks) {
99  this(size.getMM(), numSlices, numStacks);
100  parametrics.add(size);
101  }
110  public Sphere(double radius, int numSlices, int numStacks) {
111  init();
112  this.radius = radius;
113  this.setNumSlices(numSlices);
114  this.setNumStacks(numStacks);
115  }
116 
126  public Sphere(Vector3d center, double radius, int numSlices, int numStacks) {
127  this.center = center;
128  this.radius = radius;
129  this.setNumSlices(numSlices);
130  this.setNumStacks(numStacks);
131  }
132 
136  private void init() {
137  center = new Vector3d(0, 0, 0);
138  radius = 1;
141  }
142 
152  private Vertex sphereVertex(Vector3d c, double r, double theta, double phi) {
153  theta *= Math.PI * 2;
154  phi *= Math.PI;
155  Vector3d dir = new Vector3d(
156  Math.cos(theta) * Math.sin(phi),
157  Math.cos(phi),
158  Math.sin(theta) * Math.sin(phi)
159  );
160  return new Vertex(c.plus(dir.times(r)), dir);
161  }
162 
163  /* (non-Javadoc)
164  * @see eu.mihosoft.vrl.v3d.Primitive#toPolygons()
165  */
166  @Override
167  public List<Polygon> toPolygons() {
168  if(radius<=0)
169  throw new NumberFormatException("radius can not be negative");
170  List<Polygon> polygons = new ArrayList<>();
171 
172  for (int i = 0; i < getNumSlices(); i++) {
173  for (int j = 0; j < getNumStacks(); j++) {
174  final List<Vertex> vertices = new ArrayList<>();
175 
176  vertices.add(
177  sphereVertex(center, radius, i / (double) getNumSlices(),
178  j / (double) getNumStacks())
179  );
180  if (j > 0) {
181  vertices.add(
182  sphereVertex(center, radius, (i + 1) / (double) getNumSlices(),
183  j / (double) getNumStacks())
184  );
185  }
186  if (j < getNumStacks() - 1) {
187  vertices.add(
188  sphereVertex(center, radius, (i + 1) / (double) getNumSlices(),
189  (j + 1) / (double) getNumStacks())
190  );
191  }
192  vertices.add(
193  sphereVertex(center, radius, i / (double) getNumSlices(),
194  (j + 1) / (double) getNumStacks())
195  );
196  polygons.add(new Polygon(vertices, getProperties()));
197  }
198  }
199  return polygons;
200  }
201 
207  public Vector3d getCenter() {
208  return center;
209  }
210 
217  this.center = center;
218  return this;
219  }
220 
226  public double getRadius() {
227  return radius;
228  }
229 
235  public Sphere setRadius(double radius) {
236  this.radius = radius;return this;
237  }
238 
244  public int getNumSlices() {
245  return numSlices;
246  }
247 
254  if(numSlices>(NUM_SLICES*4))
255  numSlices=(NUM_SLICES*4);
256  this.numSlices = numSlices;return this;
257  }
258 
264  public int getNumStacks() {
265  return numStacks;
266  }
267 
274  if(numStacks>(NUM_STACKS*4))
275  numStacks=(NUM_STACKS*4);
276  this.numStacks = numStacks;return this;
277  }
278 
279  /* (non-Javadoc)
280  * @see eu.mihosoft.vrl.v3d.Primitive#getProperties()
281  */
282  @Override
284  return properties;
285  }
286 
287 }
Sphere(Vector3d center, double radius, int numSlices, int numStacks)
Definition: Sphere.java:126
static final int NUM_SLICES
Definition: Sphere.java:52
Sphere(double radius)
Definition: Sphere.java:86
Sphere(LengthParameter size, int numSlices, int numStacks)
Definition: Sphere.java:98
Sphere setNumStacks(int numStacks)
Definition: Sphere.java:273
Sphere setNumSlices(int numSlices)
Definition: Sphere.java:253
List< Polygon > toPolygons()
Definition: Sphere.java:167
Sphere(LengthParameter size)
Definition: Sphere.java:94
Sphere setRadius(double radius)
Definition: Sphere.java:235
final PropertyStorage properties
Definition: Sphere.java:69
Vertex sphereVertex(Vector3d c, double r, double theta, double phi)
Definition: Sphere.java:152
Sphere setCenter(Vector3d center)
Definition: Sphere.java:216
PropertyStorage getProperties()
Definition: Sphere.java:283
static final int NUM_STACKS
Definition: Sphere.java:54
Sphere(double radius, int numSlices, int numStacks)
Definition: Sphere.java:110
Vector3d times(double a)
Definition: Vector3d.java:191
Vector3d plus(Vector3d v)
Definition: Vector3d.java:165