JavaCAD
RoundedCube.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 static eu.mihosoft.vrl.v3d.Transform.unity;
9 import java.util.List;
10 
11 import eu.mihosoft.vrl.v3d.parametrics.LengthParameter;
12 
13 // TODO: Auto-generated Javadoc
19 public class RoundedCube extends Primitive {
20 
25 
27  private Vector3d center;
28 
30  private boolean centered=true;
31 
34 
36  private double cornerRadius = 0.1;
37 
39  private int resolution = 8;
40 
45  public RoundedCube() {
46  center = new Vector3d(0, 0, 0);
47  dimensions = new Vector3d(1, 1, 1);
48  }
49 
56  public RoundedCube(double size) {
57  center = new Vector3d(0, 0, 0);
58  dimensions = new Vector3d(size, size, size);
59  }
60 
61 
63  this(Vector3d.ZERO, new Vector3d(w.getMM(), h.getMM(), d.getMM()));
64  parametrics.add(w);
65  parametrics.add(h);
66  parametrics.add(d);
67  }
69  this(size,size,size);
70  }
71 
80  this.center = center;
81  this.dimensions = dimensions;
82  }
83 
92  public RoundedCube(double w, double h, double d) {
93  this(Vector3d.ZERO, new Vector3d(w, h, d));
94  }
95 
96 
97  /* (non-Javadoc)
98  * @see eu.mihosoft.vrl.v3d.Primitive#toPolygons()
99  */
100  @Override
101  public List<Polygon> toPolygons() {
102  CSG spherePrototype =
104 
105  double x = dimensions.x / 2.0 - getCornerRadius();
106  double y = dimensions.y / 2.0 - getCornerRadius();
107  double z = dimensions.z / 2.0 - getCornerRadius();
108 
109  CSG sphere1 = spherePrototype.transformed(unity().translate(-x, -y, -z));
110  CSG sphere2 = spherePrototype.transformed(unity().translate(x, -y, -z));
111  CSG sphere3 = spherePrototype.transformed(unity().translate(x, y, -z));
112  CSG sphere4 = spherePrototype.transformed(unity().translate(-x, y, -z));
113 
114  CSG sphere5 = spherePrototype.transformed(unity().translate(-x, -y, z));
115  CSG sphere6 = spherePrototype.transformed(unity().translate(x, -y, z));
116  CSG sphere7 = spherePrototype.transformed(unity().translate(x, y, z));
117  CSG sphere8 = spherePrototype.transformed(unity().translate(-x, y, z));
118 
119  List<Polygon> result = sphere1.union(
120  sphere2, sphere3, sphere4,
121  sphere5, sphere6, sphere7, sphere8).hull().getPolygons();
122 
123  if (!centered) {
124 
125  Transform centerTransform = Transform.unity().translate(dimensions.x / 2.0, dimensions.y / 2.0, dimensions.z / 2.0);
126 
127  for (Polygon p : result) {
128  p.transform(centerTransform);
129  }
130  }
131 
132  return result;
133  }
134 
135  /* (non-Javadoc)
136  * @see eu.mihosoft.vrl.v3d.Primitive#getProperties()
137  */
138  @Override
140  return properties;
141  }
142 
148  public Vector3d getCenter() {
149  return center;
150  }
151 
157  public void setCenter(Vector3d center) {
158  this.center = center;
159  }
160 
167  return dimensions;
168  }
169 
176  this.dimensions = dimensions;
177  }
178 
184  centered = false;
185  return this;
186  }
187 
193  public int getResolution() {
194  return resolution;
195  }
196 
202  public void setResolution(int resolution) {
203  this.resolution = resolution;
204  }
205 
213  this.resolution = resolution;
214  return this;
215  }
216 
222  public double getCornerRadius() {
223  return cornerRadius;
224  }
225 
231  public void setCornerRadius(double cornerRadius) {
232  this.cornerRadius = cornerRadius;
233  }
234 
242  this.cornerRadius = cornerRadius;
243  return this;
244  }
245 
246 }
CSG transformed(Transform transform)
Definition: CSG.java:1676
List< Polygon > getPolygons()
Definition: CSG.java:698
CSG union(CSG csg)
Definition: CSG.java:736
RoundedCube(double w, double h, double d)
RoundedCube(LengthParameter size)
void setResolution(int resolution)
RoundedCube cornerRadius(double cornerRadius)
RoundedCube(Vector3d center, Vector3d dimensions)
RoundedCube(LengthParameter w, LengthParameter h, LengthParameter d)
final PropertyStorage properties
void setCenter(Vector3d center)
void setDimensions(Vector3d dimensions)
void setCornerRadius(double cornerRadius)
RoundedCube resolution(int resolution)
Vector3d transform(Vector3d vec)
Definition: Transform.java:468
Transform translate(Vector3d vec)
Definition: Transform.java:187
static Transform unity()
Definition: Transform.java:85
static Vector3d y(double y)
Definition: Vector3d.java:484
static final Vector3d ZERO
Definition: Vector3d.java:57
static Vector3d z(double z)
Definition: Vector3d.java:494
static Vector3d x(double x)
Definition: Vector3d.java:474