JavaCAD
Bounds.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 // TODO: Auto-generated Javadoc
14 public class Bounds {
15 
17  private final Vector3d center;
18 
20  private final Vector3d bounds;
21 
23  private final Vector3d min;
24 
26  private final Vector3d max;
27 
29  private CSG csg;
30 
32  private Cube cube;
33 
41  this.center = new Vector3d(
42  (max.x + min.x) / 2,
43  (max.y + min.y) / 2,
44  (max.z + min.z) / 2);
45 
46  this.bounds = new Vector3d(
47  Math.abs(max.x - min.x),
48  Math.abs(max.y - min.y),
49  Math.abs(max.z - min.z));
50 
51  this.min = min.clone();
52  this.max = max.clone();
53  }
54 
55  /* (non-Javadoc)
56  * @see java.lang.Object#clone()
57  */
58  @Override
59  public Bounds clone() {
60  return new Bounds(min.clone(), max.clone());
61  }
62 
68  public Vector3d getCenter() {
69  return center;
70  }
71 
77  public Vector3d getBounds() {
78  return bounds;
79  }
80 
86  public CSG toCSG() {
87  if (csg == null) {
88  cube = new Cube(center, bounds);
89  csg = cube.toCSG();
90  }
91 
92  return csg;
93  }
94 
100  public Cube toCube() {
101  if (cube == null) {
102  cube = new Cube(center, bounds);
103  csg = cube.toCSG();
104  }
105 
106  return cube;
107  }
108 
117  public boolean contains(Vertex v) {
118  return contains(v.pos);
119  }
120 
129  public boolean contains(Vector3d v) {
130  boolean inX = min.x <= v.x && v.x <= max.x;
131  boolean inY = min.y <= v.y && v.y <= max.y;
132  boolean inZ = min.z <= v.z && v.z <= max.z;
133 
134  return inX && inY && inZ;
135  }
136 
145  public boolean contains(Polygon p) {
146  return p.vertices.stream().allMatch(v -> contains(v));
147  }
148 
158  @Deprecated
159  public boolean intersects(Polygon p) {
160  throw new UnsupportedOperationException("Implementation missing!");
161  }
162 
171  public boolean intersects(Bounds b) {
172 
173  if (b.getMin().x > this.getMax().x || b.getMax().x < this.getMin().x) {
174  return false;
175  }
176  if (b.getMin().y > this.getMax().y || b.getMax().y < this.getMin().y) {
177  return false;
178  }
179  if (b.getMin().z > this.getMax().z || b.getMax().z < this.getMin().z) {
180  return false;
181  }
182 
183  return true;
184 
185  }
186 
192  public Vector3d getMin() {
193  return min;
194  }
195 
201  public Vector3d getMax() {
202  return max;
203  }
204 
205  /* (non-Javadoc)
206  * @see java.lang.Object#toString()
207  */
208  @Override
209  public String toString() {
210  return "[center: " + center + ", bounds: " + bounds + "]";
211  }
212 
213 }
final Vector3d bounds
Definition: Bounds.java:20
boolean intersects(Bounds b)
Definition: Bounds.java:171
final Vector3d min
Definition: Bounds.java:23
boolean contains(Polygon p)
Definition: Bounds.java:145
boolean contains(Vertex v)
Definition: Bounds.java:117
final Vector3d max
Definition: Bounds.java:26
Bounds(Vector3d min, Vector3d max)
Definition: Bounds.java:40
final Vector3d center
Definition: Bounds.java:17
boolean contains(Vector3d v)
Definition: Bounds.java:129
boolean intersects(Polygon p)
Definition: Bounds.java:159
final List< Vertex > vertices
Definition: Polygon.java:54
static Vector3d y(double y)
Definition: Vector3d.java:484
static Vector3d z(double z)
Definition: Vector3d.java:494
static Vector3d x(double x)
Definition: Vector3d.java:474