JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Vec3f.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistribution of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 */
32
33package com.jogamp.audio.windows.waveout;
34
35/** 3-element single-precision vector */
36
37class Vec3f {
38 public static final Vec3f X_AXIS = new Vec3f( 1, 0, 0);
39 public static final Vec3f Y_AXIS = new Vec3f( 0, 1, 0);
40 public static final Vec3f Z_AXIS = new Vec3f( 0, 0, 1);
41 public static final Vec3f NEG_X_AXIS = new Vec3f(-1, 0, 0);
42 public static final Vec3f NEG_Y_AXIS = new Vec3f( 0, -1, 0);
43 public static final Vec3f NEG_Z_AXIS = new Vec3f( 0, 0, -1);
44
45 private float x;
46 private float y;
47 private float z;
48
49 public Vec3f() {}
50
51 public Vec3f(final Vec3f arg) {
52 set(arg);
53 }
54
55 public Vec3f(final float x, final float y, final float z) {
56 set(x, y, z);
57 }
58
59 public Vec3f copy() {
60 return new Vec3f(this);
61 }
62
63 public void set(final Vec3f arg) {
64 set(arg.x, arg.y, arg.z);
65 }
66
67 public void set(final float x, final float y, final float z) {
68 this.x = x;
69 this.y = y;
70 this.z = z;
71 }
72
73 /** Sets the ith component, 0 <= i < 3 */
74 public void set(final int i, final float val) {
75 switch (i) {
76 case 0: x = val; break;
77 case 1: y = val; break;
78 case 2: z = val; break;
79 default: throw new IndexOutOfBoundsException();
80 }
81 }
82
83 /** Gets the ith component, 0 <= i < 3 */
84 public float get(final int i) {
85 switch (i) {
86 case 0: return x;
87 case 1: return y;
88 case 2: return z;
89 default: throw new IndexOutOfBoundsException();
90 }
91 }
92
93 public float x() { return x; }
94 public float y() { return y; }
95 public float z() { return z; }
96
97 public void setX(final float x) { this.x = x; }
98 public void setY(final float y) { this.y = y; }
99 public void setZ(final float z) { this.z = z; }
100
101 public float dot(final Vec3f arg) {
102 return x * arg.x + y * arg.y + z * arg.z;
103 }
104
105 public float length() {
106 return (float) Math.sqrt(lengthSquared());
107 }
108
109 public float lengthSquared() {
110 return this.dot(this);
111 }
112
113 public void normalize() {
114 final float len = length();
115 if (len == 0.0f) return;
116 scale(1.0f / len);
117 }
118
119 /** Returns this * val; creates new vector */
120 public Vec3f times(final float val) {
121 final Vec3f tmp = new Vec3f(this);
122 tmp.scale(val);
123 return tmp;
124 }
125
126 /** this = this * val */
127 public void scale(final float val) {
128 x *= val;
129 y *= val;
130 z *= val;
131 }
132
133 /** Returns this + arg; creates new vector */
134 public Vec3f plus(final Vec3f arg) {
135 final Vec3f tmp = new Vec3f();
136 tmp.add(this, arg);
137 return tmp;
138 }
139
140 /** this = this + b */
141 public void add(final Vec3f b) {
142 add(this, b);
143 }
144
145 /** this = a + b */
146 public void add(final Vec3f a, final Vec3f b) {
147 x = a.x + b.x;
148 y = a.y + b.y;
149 z = a.z + b.z;
150 }
151
152 /** Returns this + s * arg; creates new vector */
153 public Vec3f addScaled(final float s, final Vec3f arg) {
154 final Vec3f tmp = new Vec3f();
155 tmp.addScaled(this, s, arg);
156 return tmp;
157 }
158
159 /** this = a + s * b */
160 public void addScaled(final Vec3f a, final float s, final Vec3f b) {
161 x = a.x + s * b.x;
162 y = a.y + s * b.y;
163 z = a.z + s * b.z;
164 }
165
166 /** Returns this - arg; creates new vector */
167 public Vec3f minus(final Vec3f arg) {
168 final Vec3f tmp = new Vec3f();
169 tmp.sub(this, arg);
170 return tmp;
171 }
172
173 /** this = this - b */
174 public void sub(final Vec3f b) {
175 sub(this, b);
176 }
177
178 /** this = a - b */
179 public void sub(final Vec3f a, final Vec3f b) {
180 x = a.x - b.x;
181 y = a.y - b.y;
182 z = a.z - b.z;
183 }
184
185 /** Returns this cross arg; creates new vector */
186 public Vec3f cross(final Vec3f arg) {
187 final Vec3f tmp = new Vec3f();
188 tmp.cross(this, arg);
189 return tmp;
190 }
191
192 /** this = a cross b. NOTE: "this" must be a different vector than
193 both a and b. */
194 public void cross(final Vec3f a, final Vec3f b) {
195 x = a.y * b.z - a.z * b.y;
196 y = a.z * b.x - a.x * b.z;
197 z = a.x * b.y - a.y * b.x;
198 }
199
200 /** Sets each component of this vector to the product of the
201 component with the corresponding component of the argument
202 vector. */
203 public void componentMul(final Vec3f arg) {
204 x *= arg.x;
205 y *= arg.y;
206 z *= arg.z;
207 }
208
209 @Override
210 public String toString() {
211 return "(" + x + ", " + y + ", " + z + ")";
212 }
213}