JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Vec4f.java
Go to the documentation of this file.
1/**
2 * Copyright 2022-2023 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 */
28
29package com.jogamp.math;
30
31/**
32 * 4D Vector based upon four float components.
33 *
34 * Implementation borrowed from [gfxbox2](https://jausoft.com/cgit/cs_class/gfxbox2.git/tree/include/pixel/pixel3f.hpp#n29)
35 * and its data layout from JOAL's Vec3f.
36 */
37public final class Vec4f {
38 private float x;
39 private float y;
40 private float z;
41 private float w;
42
43 public Vec4f() {}
44
45 public Vec4f(final Vec4f o) {
46 set(o);
47 }
48
49 /** Creating new Vec4f using { o, w }. */
50 public Vec4f(final Vec3f o, final float w) {
51 set(o, w);
52 }
53
54 public Vec4f copy() {
55 return new Vec4f(this);
56 }
57
58 public Vec4f(final float[/*4*/] xyzw) {
59 set(xyzw);
60 }
61
62 public Vec4f(final float x, final float y, final float z, final float w) {
63 set(x, y, z, w);
64 }
65
66 /** this = o, returns this. */
67 public Vec4f set(final Vec4f o) {
68 this.x = o.x;
69 this.y = o.y;
70 this.z = o.z;
71 this.w = o.w;
72 return this;
73 }
74
75 /** this = { o, w }, returns this. */
76 public Vec4f set(final Vec3f o, final float w) {
77 this.x = o.x();
78 this.y = o.y();
79 this.z = o.z();
80 this.w = w;
81 return this;
82 }
83
84 /** this = { x, y, z, w }, returns this. */
85 public Vec4f set(final float x, final float y, final float z, final float w) {
86 this.x = x;
87 this.y = y;
88 this.z = z;
89 this.w = w;
90 return this;
91 }
92
93 /** this = xyzw, returns this. */
94 public Vec4f set(final float[/*4*/] xyzw) {
95 this.x = xyzw[0];
96 this.y = xyzw[1];
97 this.z = xyzw[2];
98 this.w = xyzw[3];
99 return this;
100 }
101
102 /** xyzw[0..3] = this.{x, y, z, w}, returns this. */
103 public Vec4f toArray(final float[/*4*/] xyzw) {
104 xyzw[0] = this.x;
105 xyzw[1] = this.y;
106 xyzw[2] = this.z;
107 xyzw[3] = this.w;
108 return this;
109 }
110
111 /** Sets the ith component, 0 <= i < 4 */
112 public void set(final int i, final float val) {
113 switch (i) {
114 case 0: x = val; break;
115 case 1: y = val; break;
116 case 2: z = val; break;
117 case 3: w = val; break;
118 default: throw new IndexOutOfBoundsException();
119 }
120 }
121
122 /** xyzw = this, returns xyzw. */
123 public float[] get(final float[/*4*/] xyzw) {
124 xyzw[0] = this.x;
125 xyzw[1] = this.y;
126 xyzw[2] = this.z;
127 xyzw[3] = this.w;
128 return xyzw;
129 }
130
131 /** Gets the ith component, 0 <= i < 4 */
132 public float get(final int i) {
133 switch (i) {
134 case 0: return x;
135 case 1: return y;
136 case 2: return z;
137 case 3: return w;
138 default: throw new IndexOutOfBoundsException();
139 }
140 }
141
142 public float x() { return x; }
143 public float y() { return y; }
144 public float z() { return z; }
145 public float w() { return w; }
146
147 public void setX(final float x) { this.x = x; }
148 public void setY(final float y) { this.y = y; }
149 public void setZ(final float z) { this.z = z; }
150 public void setW(final float w) { this.w = w; }
151
152 /** this = max(this, m), returns this. */
153 public Vec4f max(final Vec4f m) {
154 this.x = Math.max(this.x, m.x);
155 this.y = Math.max(this.y, m.y);
156 this.z = Math.max(this.z, m.z);
157 this.w = Math.max(this.w, m.w);
158 return this;
159 }
160 /** this = min(this, m), returns this. */
161 public Vec4f min(final Vec4f m) {
162 this.x = Math.min(this.x, m.x);
163 this.y = Math.min(this.y, m.y);
164 this.z = Math.min(this.z, m.z);
165 this.w = Math.min(this.w, m.w);
166 return this;
167 }
168
169 /** Returns this * val; creates new vector */
170 public Vec4f mul(final float val) {
171 return new Vec4f(this).scale(val);
172 }
173
174 /** this = a * b, returns this. */
175 public Vec4f mul(final Vec4f a, final Vec4f b) {
176 x = a.x * b.x;
177 y = a.y * b.y;
178 z = a.z * b.z;
179 w = a.w * b.w;
180 return this;
181 }
182
183 /** this = this * s, returns this. */
184 public Vec4f mul(final Vec4f s) { return mul(s.x, s.y, s.z, s.w); }
185
186 /** this = this * { sx, sy, sz, sw }, returns this. */
187 public Vec4f mul(final float sx, final float sy, final float sz, final float sw) {
188 x *= sx;
189 y *= sy;
190 z *= sz;
191 w *= sw;
192 return this;
193 }
194
195 /** this = a / b, returns this. */
196 public Vec4f div(final Vec4f a, final Vec4f b) {
197 x = a.x / b.x;
198 y = a.y / b.y;
199 z = a.z / b.z;
200 w = a.w / b.w;
201 return this;
202 }
203
204 /** this = this / a, returns this. */
205 public Vec4f div(final Vec4f a) {
206 x /= a.x;
207 y /= a.y;
208 z /= a.z;
209 w /= a.w;
210 return this;
211 }
212
213 /** this = this * s, returns this. */
214 public Vec4f scale(final float s) {
215 x *= s;
216 y *= s;
217 z *= s;
218 w *= s;
219 return this;
220 }
221
222 /** Returns this + arg; creates new vector */
223 public Vec4f plus(final Vec4f arg) {
224 return new Vec4f(this).add(arg);
225 }
226
227 /** this = a + b, returns this. */
228 public Vec4f plus(final Vec4f a, final Vec4f b) {
229 x = a.x + b.x;
230 y = a.y + b.y;
231 z = a.z + b.z;
232 w = a.w + b.w;
233 return this;
234 }
235
236 /** this = this + { dx, dy, dz, dw }, returns this. */
237 public Vec4f add(final float dx, final float dy, final float dz, final float dw) {
238 x += dx;
239 y += dy;
240 z += dz;
241 w += dw;
242 return this;
243 }
244
245 /** this = this + b, returns this. */
246 public Vec4f add(final Vec4f b) {
247 x += b.x;
248 y += b.y;
249 z += b.z;
250 w += b.w;
251 return this;
252 }
253
254 /** Returns this - arg; creates new vector */
255 public Vec4f minus(final Vec4f arg) {
256 return new Vec4f(this).sub(arg);
257 }
258
259 /** this = a - b, returns this. */
260 public Vec4f minus(final Vec4f a, final Vec4f b) {
261 x = a.x - b.x;
262 y = a.y - b.y;
263 z = a.z - b.z;
264 w = a.w - b.w;
265 return this;
266 }
267
268 /** this = this - b, returns this. */
269 public Vec4f sub(final Vec4f b) {
270 x -= b.x;
271 y -= b.y;
272 z -= b.z;
273 w -= b.w;
274 return this;
275 }
276
277 /** Return true if all components are zero, i.e. it's absolute value < {@link #EPSILON}. */
278 public boolean isZero() {
279 return FloatUtil.isZero(x) && FloatUtil.isZero(y) && FloatUtil.isZero(z) && FloatUtil.isZero(w);
280 }
281
282 /**
283 * Return the length of this vector, a.k.a the <i>norm</i> or <i>magnitude</i>
284 */
285 public float length() {
286 return (float) Math.sqrt(lengthSq());
287 }
288
289 /**
290 * Return the squared length of this vector, a.k.a the squared <i>norm</i> or squared <i>magnitude</i>
291 */
292 public float lengthSq() {
293 return x*x + y*y + z*z + w*w;
294 }
295
296 /**
297 * Normalize this vector in place
298 */
299 public Vec4f normalize() {
300 final float lengthSq = lengthSq();
301 if ( FloatUtil.isZero( lengthSq ) ) {
302 x = 0.0f;
303 y = 0.0f;
304 z = 0.0f;
305 w = 0.0f;
306 } else {
307 final float invSqr = 1.0f / (float)Math.sqrt(lengthSq);
308 x *= invSqr;
309 y *= invSqr;
310 z *= invSqr;
311 w *= invSqr;
312 }
313 return this;
314 }
315
316 /**
317 * Return the squared distance between this vector and the given one.
318 * <p>
319 * When comparing the relative distance between two points it is usually sufficient to compare the squared
320 * distances, thus avoiding an expensive square root operation.
321 * </p>
322 */
323 public float distSq(final Vec4f o) {
324 final float dx = x - o.x;
325 final float dy = y - o.y;
326 final float dz = z - o.z;
327 final float dw = w - o.w;
328 return dx*dx + dy*dy + dz*dz + dw*dw;
329 }
330
331 /**
332 * Return the distance between this vector and the given one.
333 */
334 public float dist(final Vec4f o) {
335 return (float)Math.sqrt(distSq(o));
336 }
337
338
339 /**
340 * Return the dot product of this vector and the given one
341 * @return the dot product as float
342 */
343 public float dot(final Vec4f o) {
344 return x*o.x + y*o.y + z*o.z + w*o.w;
345 }
346
347 /**
348 * Return the cosines of the angle between two vectors
349 */
350 public float cosAngle(final Vec4f o) {
351 return dot(o) / ( length() * o.length() ) ;
352 }
353
354 /**
355 * Return the angle between two vectors in radians
356 */
357 public float angle(final Vec4f o) {
358 return (float) Math.acos( cosAngle(o) );
359 }
360
361 /**
362 * Equals check using a given {@link FloatUtil#EPSILON} value and {@link FloatUtil#isEqual(float, float, float)}.
363 * <p>
364 * Implementation considers following corner cases:
365 * <ul>
366 * <li>NaN == NaN</li>
367 * <li>+Inf == +Inf</li>
368 * <li>-Inf == -Inf</li>
369 * </ul>
370 * @param o comparison value
371 * @param epsilon consider using {@link FloatUtil#EPSILON}
372 * @return true if all components differ less than {@code epsilon}, otherwise false.
373 */
374 public boolean isEqual(final Vec4f o, final float epsilon) {
375 if( this == o ) {
376 return true;
377 } else {
378 return FloatUtil.isEqual(x, o.x, epsilon) &&
379 FloatUtil.isEqual(y, o.y, epsilon) &&
380 FloatUtil.isEqual(z, o.z, epsilon) &&
381 FloatUtil.isEqual(w, o.w, epsilon);
382 }
383 }
384
385 /**
386 * Equals check using {@link FloatUtil#EPSILON} in {@link FloatUtil#isEqual(float, float)}.
387 * <p>
388 * Implementation considers following corner cases:
389 * <ul>
390 * <li>NaN == NaN</li>
391 * <li>+Inf == +Inf</li>
392 * <li>-Inf == -Inf</li>
393 * </ul>
394 * @param o comparison value
395 * @return true if all components differ less than {@link FloatUtil#EPSILON}, otherwise false.
396 */
397 public boolean isEqual(final Vec4f o) {
398 if( this == o ) {
399 return true;
400 } else {
401 return FloatUtil.isEqual(x, o.x) &&
402 FloatUtil.isEqual(y, o.y) &&
403 FloatUtil.isEqual(z, o.z) &&
404 FloatUtil.isEqual(w, o.w);
405 }
406 }
407
408 @Override
409 public boolean equals(final Object o) {
410 if( o instanceof Vec4f ) {
411 return isEqual((Vec4f)o);
412 } else {
413 return false;
414 }
415 }
416
417 @Override
418 public String toString() {
419 return x + " / " + y + " / " + z + " / " + w;
420 }
421}
Basic Float math utility functions.
Definition: FloatUtil.java:83
static boolean isZero(final float a, final float epsilon)
Returns true if value is zero, i.e.
static boolean isEqual(final float a, final float b, final float epsilon)
Returns true if both values are equal, i.e.
3D Vector based upon three float components.
Definition: Vec3f.java:37
4D Vector based upon four float components.
Definition: Vec4f.java:37
Vec4f add(final Vec4f b)
this = this + b, returns this.
Definition: Vec4f.java:246
Vec4f toArray(final float[] xyzw)
xyzw[0..3] = this.
Definition: Vec4f.java:103
void setY(final float y)
Definition: Vec4f.java:148
void setX(final float x)
Definition: Vec4f.java:147
Vec4f div(final Vec4f a)
this = this / a, returns this.
Definition: Vec4f.java:205
Vec4f mul(final Vec4f a, final Vec4f b)
this = a * b, returns this.
Definition: Vec4f.java:175
Vec4f max(final Vec4f m)
this = max(this, m), returns this.
Definition: Vec4f.java:153
Vec4f plus(final Vec4f a, final Vec4f b)
this = a + b, returns this.
Definition: Vec4f.java:228
Vec4f(final float x, final float y, final float z, final float w)
Definition: Vec4f.java:62
Vec4f min(final Vec4f m)
this = min(this, m), returns this.
Definition: Vec4f.java:161
Vec4f scale(final float s)
this = this * s, returns this.
Definition: Vec4f.java:214
Vec4f mul(final float val)
Returns this * val; creates new vector.
Definition: Vec4f.java:170
Vec4f add(final float dx, final float dy, final float dz, final float dw)
this = this + { dx, dy, dz, dw }, returns this.
Definition: Vec4f.java:237
Vec4f normalize()
Normalize this vector in place.
Definition: Vec4f.java:299
Vec4f div(final Vec4f a, final Vec4f b)
this = a / b, returns this.
Definition: Vec4f.java:196
Vec4f plus(final Vec4f arg)
Returns this + arg; creates new vector.
Definition: Vec4f.java:223
float angle(final Vec4f o)
Return the angle between two vectors in radians.
Definition: Vec4f.java:357
float cosAngle(final Vec4f o)
Return the cosines of the angle between two vectors.
Definition: Vec4f.java:350
Vec4f sub(final Vec4f b)
this = this - b, returns this.
Definition: Vec4f.java:269
Vec4f minus(final Vec4f arg)
Returns this - arg; creates new vector.
Definition: Vec4f.java:255
float dist(final Vec4f o)
Return the distance between this vector and the given one.
Definition: Vec4f.java:334
Vec4f mul(final Vec4f s)
this = this * s, returns this.
Definition: Vec4f.java:184
boolean isEqual(final Vec4f o, final float epsilon)
Equals check using a given FloatUtil#EPSILON value and FloatUtil#isEqual(float, float,...
Definition: Vec4f.java:374
float dot(final Vec4f o)
Return the dot product of this vector and the given one.
Definition: Vec4f.java:343
float lengthSq()
Return the squared length of this vector, a.k.a the squared norm or squared magnitude
Definition: Vec4f.java:292
void setZ(final float z)
Definition: Vec4f.java:149
boolean isEqual(final Vec4f o)
Equals check using FloatUtil#EPSILON in FloatUtil#isEqual(float, float).
Definition: Vec4f.java:397
boolean equals(final Object o)
Definition: Vec4f.java:409
Vec4f(final Vec4f o)
Definition: Vec4f.java:45
Vec4f(final float[] xyzw)
Definition: Vec4f.java:58
float length()
Return the length of this vector, a.k.a the norm or magnitude
Definition: Vec4f.java:285
Vec4f(final Vec3f o, final float w)
Creating new Vec4f using { o, w }.
Definition: Vec4f.java:50
void setW(final float w)
Definition: Vec4f.java:150
Vec4f minus(final Vec4f a, final Vec4f b)
this = a - b, returns this.
Definition: Vec4f.java:260
float distSq(final Vec4f o)
Return the squared distance between this vector and the given one.
Definition: Vec4f.java:323
Vec4f mul(final float sx, final float sy, final float sz, final float sw)
this = this * { sx, sy, sz, sw }, returns this.
Definition: Vec4f.java:187
boolean isZero()
Return true if all components are zero, i.e.
Definition: Vec4f.java:278