JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestVec3f01NOUI.java
Go to the documentation of this file.
1/**
2 * Copyright 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.opengl.test.junit.math;
30
31import org.junit.Assert;
32import org.junit.Test;
33import org.junit.FixMethodOrder;
34import org.junit.runners.MethodSorters;
35
36import com.jogamp.junit.util.JunitTracer;
37import com.jogamp.math.FloatUtil;
38import com.jogamp.math.Quaternion;
39import com.jogamp.math.Vec3f;
40
41@FixMethodOrder(MethodSorters.NAME_ASCENDING)
42public class TestVec3f01NOUI extends JunitTracer {
43 static final boolean DEBUG = false;
44
45 static final Quaternion QUAT_IDENT = new Quaternion(0f, 0f, 0f, 1f);
46
47 static final Vec3f ZERO = new Vec3f();
48 static final Vec3f ONE = Vec3f.ONE;
49 static final Vec3f NEG_ONE = new Vec3f ( -1f, -1f, -1f );
50 static final Vec3f UNIT_X = Vec3f.UNIT_X;
51 static final Vec3f UNIT_X_NEG = Vec3f.UNIT_X_NEG;
52 static final Vec3f UNIT_Y = Vec3f.UNIT_Y;
53 static final Vec3f UNIT_Z = Vec3f.UNIT_Z;
54
55 static final float MACH_EPSILON = FloatUtil.EPSILON;
56
57 //
58 // Basic
59 //
60
61 @Test
62 public void test01Normalize() {
63 final Vec3f v0 = UNIT_X;
64 final Vec3f v1 = new Vec3f(1, 2, 3);
65 Assert.assertEquals(1f, v0.length(), MACH_EPSILON);
66 Assert.assertEquals(1f, v1.normalize().length(), MACH_EPSILON);
67 }
68
69 @Test
70 public void test02Angle() {
71 // test 0 deg
72 {
73 System.err.println("Test 0-deg, UNIT_X vecs");
74 final Vec3f v0 = UNIT_X;
75 final Vec3f v1 = UNIT_X;
76 System.err.println("v0 "+v0);
77 System.err.println("v1 "+v1);
78 final float a0_v0_v1 = v0.angle(v1);
79 System.err.println("a0(v0, v1) = "+a0_v0_v1+" rad, "+FloatUtil.radToADeg(a0_v0_v1)+" deg, via dot, acos");
80 Assert.assertEquals(0f, a0_v0_v1, MACH_EPSILON);
81 }
82 // test 0 deg
83 {
84 System.err.println("Test 0-deg, free vecs");
85 final Vec3f v0 = new Vec3f(0.14f, 0.07f, 0f);
86 final Vec3f v1 = new Vec3f(0.33f, 0.07f, 0f);
87 final Vec3f v0_1 = v1.minus(v0);
88 System.err.println("v0 "+v0);
89 System.err.println("v1 "+v1);
90 System.err.println("v0_1 "+v0_1);
91
92 final float a0_x_v0_1 = UNIT_X.angle(v0_1);
93 System.err.println("a0(X, v0_1) = "+a0_x_v0_1+" rad, "+FloatUtil.radToADeg(a0_x_v0_1)+" deg, via dot, acos");
94 Assert.assertEquals(0f, a0_x_v0_1, MACH_EPSILON);
95 }
96 // test 180 deg
97 {
98 System.err.println("Test 180-deg, free vecs");
99 final Vec3f v0 = new Vec3f(0.33f, 0.07f, 0f);
100 final Vec3f v1 = new Vec3f(0.14f, 0.07f, 0f);
101 final Vec3f v0_1 = v1.minus(v0);
102 System.err.println("v0 "+v0);
103 System.err.println("v1 "+v1);
104 System.err.println("v0_1 "+v0_1);
105
106 final float a0_x_v0_1 = UNIT_X.angle(v0_1);
107 System.err.println("a0(X, v0_1) = "+a0_x_v0_1+" rad, "+FloatUtil.radToADeg(a0_x_v0_1)+" deg, via dot, acos");
108 Assert.assertEquals(FloatUtil.PI, a0_x_v0_1, MACH_EPSILON);
109 }
110 // test 90 deg
111 {
112 System.err.println("Test 90-deg, UNIT_X, UNIT_Y vecs");
113 final Vec3f v0 = UNIT_X;
114 final Vec3f v1 = UNIT_Y;
115 System.err.println("v0 "+v0);
116 System.err.println("v1 "+v1);
117 final float a0_v0_v1 = v0.angle(v1);
118 System.err.println("a0(v0, v1) = "+a0_v0_v1+" rad, "+FloatUtil.radToADeg(a0_v0_v1)+" deg, via dot, acos");
119 Assert.assertEquals(FloatUtil.HALF_PI, a0_v0_v1, MACH_EPSILON);
120 }
121 // test 180 deg
122 {
123 System.err.println("Test 180-deg, UNIT_X, UNIT_X_NEG vecs");
124 final Vec3f v0 = UNIT_X;
125 final Vec3f v1 = UNIT_X_NEG;
126 System.err.println("v0 "+v0);
127 System.err.println("v1 "+v1);
128 final float a0_v0_v1 = v0.angle(v1);
129 System.err.println("a0(v0, v1) = "+a0_v0_v1+" rad, "+FloatUtil.radToADeg(a0_v0_v1)+" deg, via dot, acos");
130 Assert.assertEquals(FloatUtil.PI, a0_v0_v1, MACH_EPSILON);
131 }
132 }
133
134 public static void main(final String args[]) {
135 org.junit.runner.JUnitCore.main(TestVec3f01NOUI.class.getName());
136 }
137}
Basic Float math utility functions.
Definition: FloatUtil.java:83
static final float PI
The value PI, i.e.
static final float EPSILON
Epsilon for floating point {@value}, as once computed via getMachineEpsilon() on an AMD-64 CPU.
static float radToADeg(final float rad)
Converts radians to arc-degree.
static final float HALF_PI
The value PI/2, i.e.
Quaternion implementation supporting Gimbal-Lock free rotations.
Definition: Quaternion.java:45
3D Vector based upon three float components.
Definition: Vec3f.java:37
static final Vec3f ONE
Definition: Vec3f.java:38
static final Vec3f UNIT_X_NEG
Definition: Vec3f.java:40
float angle(final Vec3f o)
Return the angle between two vectors in radians using Math#acos(double) on cosAngle(Vec3f).
Definition: Vec3f.java:366
Vec3f normalize()
Normalize this vector in place.
Definition: Vec3f.java:297
float length()
Return the length of this vector, a.k.a the norm or magnitude
Definition: Vec3f.java:283
static final Vec3f UNIT_X
Definition: Vec3f.java:39
Vec3f minus(final Vec3f arg)
Returns this - arg; creates new vector.
Definition: Vec3f.java:255
static final Vec3f UNIT_Z
Definition: Vec3f.java:43
static final Vec3f UNIT_Y
Definition: Vec3f.java:41