aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/com/jogamp/math/geom/Cube.java
blob: ded963a04b3983619f8ab65ecc5a1d3ad1c5969b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
 * Copyright 2024 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package com.jogamp.math.geom;

import com.jogamp.math.Matrix4f;
import com.jogamp.math.Vec3f;

/**
 * Simple 8-point {@link Vec3f} cube compound having {@code z-far <= z-near}
 * <p>
 * 8-points from far to near (z), left to right (x) and bottom to top (y) in AABB case, otherwise arbitrary
 * <ul>
 *   <li>lbf, rbf, rtf, ltf</li>
 *   <li>lbn, rbn, rtn, ltn</li>
 * </ul>
 * </p>
 * <p>
 * A cube can be used to transform an {@link AABBox}
 * from object-space to e.g. model-view (Mv) space via {@link #Cube(AABBox)} and {@link #transform(Matrix4f)}
 * as required for an Mv {@link Frustum} presentation, see {@link #updateFrustumPlanes(Frustum)}.
 * </p>
 */
public class Cube {
    /** left -bottom-far (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f lbf;
    /** right-bottom-far (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f rbf;
    /** right-top   -far (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f rtf;
    /** left -top   -far (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f ltf;

    /** left -bottom-near (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f lbn;
    /** right-bottom-near (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f rbn;
    /** right-top   -near (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f rtn;
    /** left -top   -near (xyz) in AABB case, otherwise arbitrary */
    public final Vec3f ltn;

    @Override
    public final String toString() {
        return "[lbf "+lbf+", rbf "+rbf+", rtf "+rtf+", lbn "+lbn+", rbn "+rbn+", rtn "+rtn+", ltn "+ltn+"]";
    }

    /**
     * Construct a {@link Cube} with all points set to zero.
     */
    public Cube() {
        lbf = new Vec3f();
        rbf = new Vec3f();
        rtf = new Vec3f();
        ltf = new Vec3f();

        lbn = new Vec3f();
        rbn = new Vec3f();
        rtn = new Vec3f();
        ltn = new Vec3f();
    }

    /** Copy construct for a {@link Cube}. */
    public Cube(final Cube o) {
        lbf = new Vec3f(o.lbf);
        rbf = new Vec3f(o.rbf);
        rtf = new Vec3f(o.rtf);
        ltf = new Vec3f(o.ltf);

        lbn = new Vec3f(o.lbn);
        rbn = new Vec3f(o.rbn);
        rtn = new Vec3f(o.rtn);
        ltn = new Vec3f(o.ltn);
    }

    /** Construct a {@link Cube} with given {@link AABBox}. */
    public Cube(final AABBox box) {
        this( box.getLow(), box.getHigh());
    }

    /**
     * Construct a {@link Cube} with given {@link AABBox} minimum and maximum.
     * @param lo_lbf minimum left -bottom-far (xyz)
     * @param hi_rtn maximum right-top   -near (xyz)
     */
    public Cube(final Vec3f lo_lbf, final Vec3f hi_rtn) {
        lbf = new Vec3f(lo_lbf);
        rtn = new Vec3f(hi_rtn);

        rbf = new Vec3f(rtn.x(), lbf.y(), lbf.z());
        rtf = new Vec3f(rtn.x(), rtn.y(), lbf.z());
        ltf = new Vec3f(lbf.x(), rtn.y(), lbf.z());

        lbn = new Vec3f(lbf.x(), lbf.y(), rtn.z());
        rbn = new Vec3f(rtn.x(), lbf.y(), rtn.z());
        ltn = new Vec3f(lbf.x(), rtn.y(), rtn.z());
    }

    /**
     * Setting this cube to given {@link AABBox} minimum and maximum.
     */
    public Cube set(final AABBox box) {
        return set( box.getLow(), box.getHigh());
    }

    /**
     * Setting this cube to given {@link AABBox} minimum and maximum.
     * @param lo_lbf minimum left -bottom-far (xyz)
     * @param hi_rtn maximum right-top   -near (xyz)
     */
    public Cube set(final Vec3f lo_lbf, final Vec3f hi_rtn) {
        lbf.set(lo_lbf);
        rtn.set(hi_rtn);

        rbf.set(rtn.x(), lbf.y(), lbf.z());
        rtf.set(rtn.x(), rtn.y(), lbf.z());
        ltf.set(lbf.x(), rtn.y(), lbf.z());

        lbn.set(lbf.x(), lbf.y(), rtn.z());
        rbn.set(rtn.x(), lbf.y(), rtn.z());
        ltn.set(lbf.x(), rtn.y(), rtn.z());
        return this;
    }

    public Cube set(final Cube o) {
        lbf.set(o.lbf);
        rbf.set(o.rbf);
        rtf.set(o.rtf);
        ltf.set(o.ltf);

        lbn.set(o.lbn);
        rbn.set(o.rbn);
        rtn.set(o.rtn);
        ltn.set(o.ltn);
        return this;
    }

    /** Affine 3f-vector transformation of all 8-points with given matrix, {@link Matrix4f#mulVec3f(Vec3f)}. */
    public Cube transform(final Matrix4f mat) {
        mat.mulVec3f(lbf);
        mat.mulVec3f(rbf);
        mat.mulVec3f(rtf);
        mat.mulVec3f(ltf);

        mat.mulVec3f(lbn);
        mat.mulVec3f(rbn);
        mat.mulVec3f(rtn);
        mat.mulVec3f(ltn);
        return this;
    }

    /**
     * Calculate the frustum planes using this {@link Cube}.
     * <p>
     * One useful application is to {@link Cube#transform(Matrix4f) transform}
     * an {@link AABBox}, see {@link Cube#Cube(AABBox)} from its object-space
     * into model-view (Mv) and produce the {@link Frustum} planes using this method
     * for CPU side object culling and GPU shader side fragment clipping.
     * </p>
     * <p>
     * Frustum plane's normals will point to the inside of the viewing frustum,
     * as required by the {@link Frustum} class.
     * </p>
     * @param frustum the output frustum
     * @return the output frustum for chaining
     * @see Frustum#updateFrustumPlanes(Cube)
     * @see Cube#Cube(AABBox)
     * @see Cube#transform(Matrix4f)
     */
    public Frustum updateFrustumPlanes(final Frustum frustum) {
        // n [  0.0 /  0.0 / -1.0 ]
        frustum.getPlanes()[Frustum.NEAR].set(
                (lbf).minus(lbn).normalize(), // | lbf - lbn |, inwards
                lbn );   // closest AABB point to origin on plane

        // n [  0.0 /  0.0 /  1.0 ]
        frustum.getPlanes()[Frustum.FAR].set(
                (lbn).minus(lbf).normalize(), // | lbn - lbf |, inwards
                lbf );   // closest AABB point to origin on plane

        // n [  1.0 /  0.0 /  0.0 ]
        frustum.getPlanes()[Frustum.LEFT].set(
                (rbf).minus(lbf).normalize(), // | rbf - lbf |, inwards
                lbn );   // closest AABB point to origin on plane

        // n [ -1.0 /  0.0 /  0.0 ]
        frustum.getPlanes()[Frustum.RIGHT].set(
                (lbf).minus(rbf).normalize(), // | lbf - rbf |, inwards
                rbn );   // closest AABB point to origin on plane

        // n [  0.0 /  1.0 /  0.0 ]
        frustum.getPlanes()[Frustum.BOTTOM].set(
                (ltf).minus(lbf).normalize(), // | ltf - lbf |, inwards
                lbn );   // closest AABB point to origin on plane

        // n [  0.0 / -1.0 /  0.0 ]
        frustum.getPlanes()[Frustum.TOP].set(
                (lbf).minus(ltf).normalize(), // | lbf - ltf |, inwards
                ltn );   // closest AABB point to origin on plane

        return frustum;
    }

}