JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
Cube.java
Go to the documentation of this file.
1/**
2 * Copyright 2024 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.math.geom;
29
30import com.jogamp.math.Matrix4f;
31import com.jogamp.math.Vec3f;
32
33/**
34 * Simple 8-point {@link Vec3f} cube compound having {@code z-far <= z-near}
35 * <p>
36 * 8-points from far to near (z), left to right (x) and bottom to top (y) in AABB case, otherwise arbitrary
37 * <ul>
38 * <li>lbf, rbf, rtf, ltf</li>
39 * <li>lbn, rbn, rtn, ltn</li>
40 * </ul>
41 * </p>
42 * <p>
43 * A cube can be used to transform an {@link AABBox}
44 * from object-space to e.g. model-view (Mv) space via {@link #Cube(AABBox)} and {@link #transform(Matrix4f)}
45 * as required for an Mv {@link Frustum} presentation, see {@link #updateFrustumPlanes(Frustum)}.
46 * </p>
47 */
48public class Cube {
49 /** left -bottom-far (xyz) in AABB case, otherwise arbitrary */
50 public final Vec3f lbf;
51 /** right-bottom-far (xyz) in AABB case, otherwise arbitrary */
52 public final Vec3f rbf;
53 /** right-top -far (xyz) in AABB case, otherwise arbitrary */
54 public final Vec3f rtf;
55 /** left -top -far (xyz) in AABB case, otherwise arbitrary */
56 public final Vec3f ltf;
57
58 /** left -bottom-near (xyz) in AABB case, otherwise arbitrary */
59 public final Vec3f lbn;
60 /** right-bottom-near (xyz) in AABB case, otherwise arbitrary */
61 public final Vec3f rbn;
62 /** right-top -near (xyz) in AABB case, otherwise arbitrary */
63 public final Vec3f rtn;
64 /** left -top -near (xyz) in AABB case, otherwise arbitrary */
65 public final Vec3f ltn;
66
67 @Override
68 public final String toString() {
69 return "[lbf "+lbf+", rbf "+rbf+", rtf "+rtf+", lbn "+lbn+", rbn "+rbn+", rtn "+rtn+", ltn "+ltn+"]";
70 }
71
72 /**
73 * Construct a {@link Cube} with all points set to zero.
74 */
75 public Cube() {
76 lbf = new Vec3f();
77 rbf = new Vec3f();
78 rtf = new Vec3f();
79 ltf = new Vec3f();
80
81 lbn = new Vec3f();
82 rbn = new Vec3f();
83 rtn = new Vec3f();
84 ltn = new Vec3f();
85 }
86
87 /** Copy construct for a {@link Cube}. */
88 public Cube(final Cube o) {
89 lbf = new Vec3f(o.lbf);
90 rbf = new Vec3f(o.rbf);
91 rtf = new Vec3f(o.rtf);
92 ltf = new Vec3f(o.ltf);
93
94 lbn = new Vec3f(o.lbn);
95 rbn = new Vec3f(o.rbn);
96 rtn = new Vec3f(o.rtn);
97 ltn = new Vec3f(o.ltn);
98 }
99
100 /** Construct a {@link Cube} with given {@link AABBox}. */
101 public Cube(final AABBox box) {
102 this( box.getLow(), box.getHigh());
103 }
104
105 /**
106 * Construct a {@link Cube} with given {@link AABBox} minimum and maximum.
107 * @param lo_lbf minimum left -bottom-far (xyz)
108 * @param hi_rtn maximum right-top -near (xyz)
109 */
110 public Cube(final Vec3f lo_lbf, final Vec3f hi_rtn) {
111 lbf = new Vec3f(lo_lbf);
112 rtn = new Vec3f(hi_rtn);
113
114 rbf = new Vec3f(rtn.x(), lbf.y(), lbf.z());
115 rtf = new Vec3f(rtn.x(), rtn.y(), lbf.z());
116 ltf = new Vec3f(lbf.x(), rtn.y(), lbf.z());
117
118 lbn = new Vec3f(lbf.x(), lbf.y(), rtn.z());
119 rbn = new Vec3f(rtn.x(), lbf.y(), rtn.z());
120 ltn = new Vec3f(lbf.x(), rtn.y(), rtn.z());
121 }
122
123 /**
124 * Setting this cube to given {@link AABBox} minimum and maximum.
125 */
126 public Cube set(final AABBox box) {
127 return set( box.getLow(), box.getHigh());
128 }
129
130 /**
131 * Setting this cube to given {@link AABBox} minimum and maximum.
132 * @param lo_lbf minimum left -bottom-far (xyz)
133 * @param hi_rtn maximum right-top -near (xyz)
134 */
135 public Cube set(final Vec3f lo_lbf, final Vec3f hi_rtn) {
136 lbf.set(lo_lbf);
137 rtn.set(hi_rtn);
138
139 rbf.set(rtn.x(), lbf.y(), lbf.z());
140 rtf.set(rtn.x(), rtn.y(), lbf.z());
141 ltf.set(lbf.x(), rtn.y(), lbf.z());
142
143 lbn.set(lbf.x(), lbf.y(), rtn.z());
144 rbn.set(rtn.x(), lbf.y(), rtn.z());
145 ltn.set(lbf.x(), rtn.y(), rtn.z());
146 return this;
147 }
148
149 public Cube set(final Cube o) {
150 lbf.set(o.lbf);
151 rbf.set(o.rbf);
152 rtf.set(o.rtf);
153 ltf.set(o.ltf);
154
155 lbn.set(o.lbn);
156 rbn.set(o.rbn);
157 rtn.set(o.rtn);
158 ltn.set(o.ltn);
159 return this;
160 }
161
162 /** Affine 3f-vector transformation of all 8-points with given matrix, {@link Matrix4f#mulVec3f(Vec3f)}. */
163 public Cube transform(final Matrix4f mat) {
164 mat.mulVec3f(lbf);
165 mat.mulVec3f(rbf);
166 mat.mulVec3f(rtf);
167 mat.mulVec3f(ltf);
168
169 mat.mulVec3f(lbn);
170 mat.mulVec3f(rbn);
171 mat.mulVec3f(rtn);
172 mat.mulVec3f(ltn);
173 return this;
174 }
175
176 /**
177 * Calculate the frustum planes using this {@link Cube}.
178 * <p>
179 * One useful application is to {@link Cube#transform(Matrix4f) transform}
180 * an {@link AABBox}, see {@link Cube#Cube(AABBox)} from its object-space
181 * into model-view (Mv) and produce the {@link Frustum} planes using this method
182 * for CPU side object culling and GPU shader side fragment clipping.
183 * </p>
184 * <p>
185 * Frustum plane's normals will point to the inside of the viewing frustum,
186 * as required by the {@link Frustum} class.
187 * </p>
188 * @param frustum the output frustum
189 * @return the output frustum for chaining
190 * @see Frustum#updateFrustumPlanes(Cube)
191 * @see Cube#Cube(AABBox)
192 * @see Cube#transform(Matrix4f)
193 */
194 public Frustum updateFrustumPlanes(final Frustum frustum) {
195 // n [ 0.0 / 0.0 / -1.0 ]
196 frustum.getPlanes()[Frustum.NEAR].set(
197 (lbf).minus(lbn).normalize(), // | lbf - lbn |, inwards
198 lbn ); // closest AABB point to origin on plane
199
200 // n [ 0.0 / 0.0 / 1.0 ]
201 frustum.getPlanes()[Frustum.FAR].set(
202 (lbn).minus(lbf).normalize(), // | lbn - lbf |, inwards
203 lbf ); // closest AABB point to origin on plane
204
205 // n [ 1.0 / 0.0 / 0.0 ]
206 frustum.getPlanes()[Frustum.LEFT].set(
207 (rbf).minus(lbf).normalize(), // | rbf - lbf |, inwards
208 lbn ); // closest AABB point to origin on plane
209
210 // n [ -1.0 / 0.0 / 0.0 ]
211 frustum.getPlanes()[Frustum.RIGHT].set(
212 (lbf).minus(rbf).normalize(), // | lbf - rbf |, inwards
213 rbn ); // closest AABB point to origin on plane
214
215 // n [ 0.0 / 1.0 / 0.0 ]
216 frustum.getPlanes()[Frustum.BOTTOM].set(
217 (ltf).minus(lbf).normalize(), // | ltf - lbf |, inwards
218 lbn ); // closest AABB point to origin on plane
219
220 // n [ 0.0 / -1.0 / 0.0 ]
221 frustum.getPlanes()[Frustum.TOP].set(
222 (lbf).minus(ltf).normalize(), // | lbf - ltf |, inwards
223 ltn ); // closest AABB point to origin on plane
224
225 return frustum;
226 }
227
228}
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Definition: Matrix4f.java:89
final Vec3f mulVec3f(final Vec3f v_in, final Vec3f v_out)
Affine 3f-vector transformation by 4x4 matrix.
Definition: Matrix4f.java:841
3D Vector based upon three float components.
Definition: Vec3f.java:37
Vec3f set(final Vec3f o)
this = o, returns this.
Definition: Vec3f.java:79
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final Vec3f getHigh()
Returns the maximum right-top-near (xyz) coordinate.
Definition: AABBox.java:131
final Vec3f getLow()
Returns the minimum left-bottom-far (xyz) coordinate.
Definition: AABBox.java:140
Simple 8-point Vec3f cube compound having z-far <= z-near @endiliteral.
Definition: Cube.java:48
Frustum updateFrustumPlanes(final Frustum frustum)
Calculate the frustum planes using this Cube.
Definition: Cube.java:194
Cube transform(final Matrix4f mat)
Affine 3f-vector transformation of all 8-points with given matrix, Matrix4f#mulVec3f(Vec3f).
Definition: Cube.java:163
final Vec3f ltf
left -top -far (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:56
final Vec3f ltn
left -top -near (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:65
final String toString()
Definition: Cube.java:68
final Vec3f rtn
right-top -near (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:63
final Vec3f lbn
left -bottom-near (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:59
Cube()
Construct a Cube with all points set to zero.
Definition: Cube.java:75
final Vec3f rbn
right-bottom-near (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:61
final Vec3f rtf
right-top -far (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:54
final Vec3f rbf
right-bottom-far (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:52
Cube(final Cube o)
Copy construct for a Cube.
Definition: Cube.java:88
Cube(final AABBox box)
Construct a Cube with given AABBox.
Definition: Cube.java:101
final Vec3f lbf
left -bottom-far (xyz) in AABB case, otherwise arbitrary
Definition: Cube.java:50
Cube(final Vec3f lo_lbf, final Vec3f hi_rtn)
Construct a Cube with given AABBox minimum and maximum.
Definition: Cube.java:110
Plane set(final Plane o)
Definition: Frustum.java:175
Providing frustum planes derived by different inputs (P*MV, ..) used to classify objects.
Definition: Frustum.java:81
static final int NEAR
Index for near plane: {@value}.
Definition: Frustum.java:268
static final int BOTTOM
Index for bottom plane: {@value}.
Definition: Frustum.java:264
final Plane[] getPlanes()
Planes are ordered in the returned array as follows:
Definition: Frustum.java:289
static final int RIGHT
Index for right plane: {@value}.
Definition: Frustum.java:262
static final int TOP
Index for top plane: {@value}.
Definition: Frustum.java:266
static final int LEFT
Index for left plane: {@value}.
Definition: Frustum.java:260
static final int FAR
Index for far plane: {@value}.
Definition: Frustum.java:270