-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathVec3f.java
More file actions
192 lines (169 loc) · 4.04 KB
/
Vec3f.java
File metadata and controls
192 lines (169 loc) · 4.04 KB
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
package nz.gen.geek_central.GLUseful;
/*
functional 3D vector operations
Copyright 2011 by Lawrence D'Oliveiro <ldo@geek-central.gen.nz>.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
*/
public class Vec3f
/* 3D vectors */
{
public final float x, y, z, w;
public Vec3f
(
float x,
float y,
float z
)
{
this.x = x;
this.y = y;
this.z = z;
this.w = 1.0f;
} /*Vec3f*/
public Vec3f
(
float x,
float y,
float z,
float w
)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
} /*Vec3f*/
public Vec3f
(
float[] v
)
{
if (v.length != 3 && v.length != 4)
{
throw new RuntimeException("need 3 or 4 floats to make a vector");
} /*if*/
x = v[0];
y = v[1];
z = v[2];
w = v.length == 4 ? v[3] : 1.0f;
} /*Vec3f*/
public float[] to_floats
(
int nrelts /* 3 or 4 */
)
{
float[] v;
switch (nrelts)
{
case 3:
v = new float[] {x, y, z};
break;
case 4:
v = new float[] {x, y, z, w};
break;
default:
throw new RuntimeException("vector can only convert to 3 or 4 floats");
/* break; */
} /*switch*/
return
v;
} /*to_floats*/
public static Vec3f zero()
{
return
new Vec3f(0.0f, 0.0f, 0.0f);
} /*zero*/
public Vec3f neg()
{
return
new Vec3f(-x, -y, -z, w);
} /*neg*/
public Vec3f add
(
Vec3f v
)
{
return
new Vec3f(x + v.x, y + v.y, z + v.z);
} /*add*/
public Vec3f sub
(
Vec3f v
)
{
return
new Vec3f(x - v.x, y - v.y, z - v.z);
} /*sub*/
public Vec3f mul
(
float s
)
{
return
new Vec3f(x * s, y * s, z * s, w);
} /*mul*/
public Vec3f recip()
{
return
new Vec3f(1.0f / x, 1.0f / y, 1.0f / z);
} /*recip*/
public float dot
(
Vec3f v
)
{
return
v.x * this.x + v.y * this.y + v.z * this.z;
} /*dot*/
public Vec3f cross
(
Vec3f v
)
{
return
new Vec3f
(
this.y * v.z - v.y * this.z,
this.z * v.x - v.z * this.x,
this.x * v.y - v.x * this.y
);
} /*cross*/
public float azimuth()
/* returns the angle between the x-axis and the line from the origin to the point. */
{
return
(float)Math.atan2(y, x);
} /*azimuth*/
public float elevation()
/* returns the angle between the x-y plane and the line from the origin to the point. */
{
return
(float)Math.atan2(z, (float)Math.sqrt(x * x + y * y));
} /*elevation*/
public float abs()
/* returns the distance between the point and the origin. */
{
return
(float)Math.sqrt((x * x + y * y + z * z) / (w * w));
} /*abs*/
public Vec3f unit()
{
final float abs = this.abs();
return
new Vec3f(x / abs, y / abs, z / abs);
} /*unit*/
public Vec3f norm()
/* rescales so w = 1. */
{
return
new Vec3f(x / w, y / w, z / w);
} /*norm*/
} /*Vec3f*/