Class: Mittsu::Vector3
Direct Known Subclasses
Color
Constant Summary
collapse
- ELEMENTS =
{ x: 0, y: 1, z: 2 }
- DIMENSIONS =
ELEMENTS.count
Instance Attribute Summary
Attributes inherited from Vector
#elements, #index, #uv
Instance Method Summary
collapse
-
#add_scaled_vector(vector, scalar) ⇒ Object
-
#apply_axis_angle(axis, angle) ⇒ Object
-
#apply_euler(euler) ⇒ Object
-
#apply_matrix3(m) ⇒ Object
-
#apply_matrix4(m) ⇒ Object
-
#apply_projection(m) ⇒ Object
-
#apply_quaternion(q) ⇒ Object
-
#clamp_length(min, max) ⇒ Object
-
#cross(v) ⇒ Object
-
#cross_vectors(a, b) ⇒ Object
-
#distance_to_squared(v) ⇒ Object
-
#dot(v) ⇒ Object
-
#equals(vector) ⇒ Object
-
#from_attribute(attribute, index, offset = 0) ⇒ Object
-
#from_buffer_attribute(attribute, index) ⇒ Object
-
#get_component(index) ⇒ Object
-
#initialize(x = 0, y = 0, z = 0) ⇒ Vector3
constructor
A new instance of Vector3.
-
#length_manhattan ⇒ Object
-
#manhattan_distance_to(vector) ⇒ Object
-
#project(camera) ⇒ Object
-
#random ⇒ Object
TODO: maybe add range 3 values as arguments (range_x, range_y, range_z) to this method.
-
#random_direction ⇒ Object
-
#set(x, y, z) ⇒ Object
-
#set_component(index, value) ⇒ Object
-
#set_from_cylindrical(cylinder) ⇒ Object
-
#set_from_cylindrical_coords(radius, theta, y) ⇒ Object
-
#set_from_matrix3_column(matrix, index) ⇒ Object
-
#set_from_matrix_column(index, matrix) ⇒ Object
-
#set_from_matrix_position(m) ⇒ Object
-
#set_from_matrix_scale(m) ⇒ Object
-
#set_from_spherical(sphere) ⇒ Object
-
#set_from_spherical_coords(radius, phi, theta) ⇒ Object
-
#set_scalar(scalar) ⇒ Object
-
#transform_direction(m) ⇒ Object
-
#unproject(camera) ⇒ Object
-
#x ⇒ Object
-
#x=(value) ⇒ Object
-
#y ⇒ Object
-
#y=(value) ⇒ Object
-
#z ⇒ Object
-
#z=(value) ⇒ Object
Methods inherited from Vector
#==, #[], #[]=, #add, #add_scalar, #add_vectors, #angle_to, #ceil, #clamp, #clamp_scalar, #clone, #copy, #distance_to, #divide, #divide_scalar, #each_dimension, #floor, #from_array, #length, #length_sq, #lerp, #lerp_vectors, #max, #min, #multiply, #multiply_scalar, #multiply_vectors, #negate, #normalize, #project_on_plane, #project_on_vector, #reflect, #round, #round_to_zero, #set_length, #sub, #sub_scalar, #sub_vectors, #to_array, #to_s
Constructor Details
#initialize(x = 0, y = 0, z = 0) ⇒ Vector3
Returns a new instance of Vector3.
8
9
10
|
# File 'lib/mittsu/math/vector3.rb', line 8
def initialize(x = 0, y = 0, z = 0)
super [x.to_f, y.to_f, z.to_f]
end
|
Instance Method Details
#add_scaled_vector(vector, scalar) ⇒ Object
228
229
230
231
232
|
# File 'lib/mittsu/math/vector3.rb', line 228
def add_scaled_vector(vector, scalar)
set(vector.x * scalar, vector.y * scalar, vector.z * scalar)
self
end
|
#apply_axis_angle(axis, angle) ⇒ Object
30
31
32
33
34
|
# File 'lib/mittsu/math/vector3.rb', line 30
def apply_axis_angle(axis, angle)
quaternion = Mittsu::Quaternion.new
self.apply_quaternion(quaternion.set_from_axis_angle(axis, angle))
self
end
|
#apply_euler(euler) ⇒ Object
24
25
26
27
28
|
# File 'lib/mittsu/math/vector3.rb', line 24
def apply_euler(euler)
quaternion = Mittsu::Quaternion.new
self.apply_quaternion(quaternion.set_from_euler(euler))
self
end
|
#apply_matrix3(m) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/mittsu/math/vector3.rb', line 36
def apply_matrix3(m)
_x, _y, _z = *@elements
e = m.elements
@elements[0] = e[0] * _x + e[3] * _y + e[6] * _z
@elements[1] = e[1] * _x + e[4] * _y + e[7] * _z
@elements[2] = e[2] * _x + e[5] * _y + e[8] * _z
self
end
|
#apply_matrix4(m) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/mittsu/math/vector3.rb', line 48
def apply_matrix4(m)
_x, _y, _z = *@elements
e = m.elements
@elements[0] = e[0] * _x + e[4] * _y + e[8] * _z + e[12]
@elements[1] = e[1] * _x + e[5] * _y + e[9] * _z + e[13]
@elements[2] = e[2] * _x + e[6] * _y + e[10] * _z + e[14]
self
end
|
#apply_projection(m) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/mittsu/math/vector3.rb', line 61
def apply_projection(m)
_x, _y, _z = *@elements
e = m.elements
d = 1.0 / (e[3] * _x + e[7] * _y + e[11] * _z + e[15])
@elements[0] = (e[0] * _x + e[4] * _y + e[8] * _z + e[12]) * d
@elements[1] = (e[1] * _x + e[5] * _y + e[9] * _z + e[13]) * d
@elements[2] = (e[2] * _x + e[6] * _y + e[10] * _z + e[14]) * d
self
end
|
#apply_quaternion(q) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/mittsu/math/vector3.rb', line 75
def apply_quaternion(q)
_x, _y, _z = *@elements
qx = q.x
qy = q.y
qz = q.z
qw = q.w
ix = qw * _x + qy * _z - qz * _y
iy = qw * _y + qz * _x - qx * _z
iz = qw * _z + qx * _y - qy * _x
iw = -qx * _x - qy * _y - qz * _z
@elements[0] = ix * qw + iw * - qx + iy * - qz - iz * - qy
@elements[1] = iy * qw + iw * - qy + iz * - qx - ix * - qz
@elements[2] = iz * qw + iw * - qz + ix * - qy - iy * - qx
self
end
|
#clamp_length(min, max) ⇒ Object
271
272
273
|
# File 'lib/mittsu/math/vector3.rb', line 271
def clamp_length(min, max)
divide_scalar(length || 1).multiply_scalar([min, [max, length].min].max)
end
|
#cross(v) ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/mittsu/math/vector3.rb', line 133
def cross(v)
_x, _y, _z = *@elements
@elements[0] = _y * v.z - _z * v.y
@elements[1] = _z * v.x - _x * v.z
@elements[2] = _x * v.y - _y * v.x
self
end
|
#cross_vectors(a, b) ⇒ Object
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/mittsu/math/vector3.rb', line 141
def cross_vectors(a, b)
ax, ay, az = a.x, a.y, a.z
bx, by, bz = b.x, b.y, b.z
@elements[0] = ay * bz - az * by
@elements[1] = az * bx - ax * bz
@elements[2] = ax * by - ay * bx
self
end
|
#distance_to_squared(v) ⇒ Object
152
153
154
155
156
157
|
# File 'lib/mittsu/math/vector3.rb', line 152
def distance_to_squared(v)
dx = x - v.x
dy = y - v.y
dz = z - v.z
dx * dx + dy * dy + dz * dz
end
|
#dot(v) ⇒ Object
125
126
127
|
# File 'lib/mittsu/math/vector3.rb', line 125
def dot(v)
x * v.x + y * v.y + z * v.z
end
|
#equals(vector) ⇒ Object
259
260
261
262
263
|
# File 'lib/mittsu/math/vector3.rb', line 259
def equals(vector)
_x, _y, _z = *@elements
((vector.x == _x) && (vector.y == _y) && (vector.z == _z ))
end
|
#from_attribute(attribute, index, offset = 0) ⇒ Object
298
299
300
301
302
303
304
305
306
|
# File 'lib/mittsu/math/vector3.rb', line 298
def from_attribute(attribute, index, offset = 0)
index = index * attribute.itemSize + offset
@elements[0] = attribute.array[index]
@elements[1] = attribute.array[index + 1]
@elements[2] = attribute.array[index + 2]
self
end
|
#from_buffer_attribute(attribute, index) ⇒ Object
265
266
267
268
269
|
# File 'lib/mittsu/math/vector3.rb', line 265
def from_buffer_attribute(attribute, index)
set(attribute.get_x(index), attribute.get_y(index), attribute.get_z(index))
self
end
|
#get_component(index) ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/mittsu/math/vector3.rb', line 213
def get_component(index)
_x, _y, _z = *@elements
case index
when 0
return _x
when 1
return _y
when 2
return _z
else
raise ArgumentError, "index is out of range: #{index}"
end
end
|
#length_manhattan ⇒ Object
129
130
131
|
# File 'lib/mittsu/math/vector3.rb', line 129
def length_manhattan
x.abs + y.abs + z.abs
end
|
#manhattan_distance_to(vector) ⇒ Object
275
276
277
278
279
|
# File 'lib/mittsu/math/vector3.rb', line 275
def manhattan_distance_to(vector)
_x, _y, _z = *@elements
(_x - vector.x).abs + (_y - vector.y).abs + (_z - vector.z).abs
end
|
#project(camera) ⇒ Object
97
98
99
100
101
|
# File 'lib/mittsu/math/vector3.rb', line 97
def project(camera)
matrix = Mittsu::Matrix4.new
matrix.multiply_matrices(camera.projection_matrix, matrix.get_inverse(camera.matrix_world))
self.apply_projection(matrix)
end
|
#random ⇒ Object
TODO: maybe add range 3 values as arguments (range_x, range_y, range_z) to this method
282
283
284
285
286
|
# File 'lib/mittsu/math/vector3.rb', line 282
def random()
set(Random.new.rand, Random.new.rand, Random.new.rand)
self
end
|
#random_direction ⇒ Object
288
289
290
291
292
293
294
295
296
|
# File 'lib/mittsu/math/vector3.rb', line 288
def random_direction()
u = (Random.new.rand - 0.5) *2
t = Random.new.rand * ::Math::PI * 2
f = ::Math.sqrt(1 - u ** 2)
set(f * ::Math.cos(t), f * ::Math.sin(t), u)
self
end
|
#set(x, y, z) ⇒ Object
12
13
14
|
# File 'lib/mittsu/math/vector3.rb', line 12
def set(x, y, z)
super [x.to_f, y.to_f, z.to_f]
end
|
#set_component(index, value) ⇒ Object
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/mittsu/math/vector3.rb', line 196
def set_component(index, value)
_x, _y, _z = *@elements
case index
when 0
set(value, _y, _z)
when 1
set(_x, value, _z)
when 2
set(_x, _y, value)
else
raise ArgumentError, "index is out of range: #{index}"
end
self
end
|
#set_from_cylindrical(cylinder) ⇒ Object
245
246
247
|
# File 'lib/mittsu/math/vector3.rb', line 245
def set_from_cylindrical(cylinder)
set_from_cylindrical_coords(cylinder.radius, cylinder.theta, cylinder.y)
end
|
#set_from_cylindrical_coords(radius, theta, y) ⇒ Object
249
250
251
252
253
|
# File 'lib/mittsu/math/vector3.rb', line 249
def set_from_cylindrical_coords(radius, theta, y)
set(radius * ::Math.sin(theta), y, radius * ::Math.sin(theta))
self
end
|
#set_from_matrix3_column(matrix, index) ⇒ Object
255
256
257
|
# File 'lib/mittsu/math/vector3.rb', line 255
def set_from_matrix3_column(matrix, index)
from_array(matrix.elements, index * 3)
end
|
#set_from_matrix_column(index, matrix) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/mittsu/math/vector3.rb', line 178
def set_from_matrix_column(index, matrix)
offset = index * 4
me = matrix.elements
@elements[0] = me[offset]
@elements[1] = me[offset + 1]
@elements[2] = me[offset + 2]
self
end
|
#set_from_matrix_position(m) ⇒ Object
159
160
161
162
163
164
|
# File 'lib/mittsu/math/vector3.rb', line 159
def set_from_matrix_position(m)
@elements[0] = m.elements[12]
@elements[1] = m.elements[13]
@elements[2] = m.elements[14]
self
end
|
#set_from_matrix_scale(m) ⇒ Object
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/mittsu/math/vector3.rb', line 166
def set_from_matrix_scale(m)
sx = self.set(m.elements[0], m.elements[1], m.elements[ 2]).length
sy = self.set(m.elements[4], m.elements[5], m.elements[ 6]).length
sz = self.set(m.elements[8], m.elements[9], m.elements[10]).length
@elements[0] = sx
@elements[1] = sy
@elements[2] = sz
self
end
|
#set_from_spherical(sphere) ⇒ Object
234
235
236
|
# File 'lib/mittsu/math/vector3.rb', line 234
def set_from_spherical(sphere)
set_from_spherical_coords(sphere.radius, sphere.phi, sphere.theta)
end
|
#set_from_spherical_coords(radius, phi, theta) ⇒ Object
238
239
240
241
242
243
|
# File 'lib/mittsu/math/vector3.rb', line 238
def set_from_spherical_coords(radius, phi, theta)
sin_phi_radius = ::Math.sin(phi) * radius
set(sin_phi_radius * ::Math.sin(theta), ::Math.cos(phi) * radius, sin_phi_radius * ::Math.cos(theta))
self
end
|
#set_scalar(scalar) ⇒ Object
190
191
192
193
194
|
# File 'lib/mittsu/math/vector3.rb', line 190
def set_scalar(scalar)
set(scalar, scalar, scalar)
self
end
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/mittsu/math/vector3.rb', line 109
def transform_direction(m)
_x, _y, _z = *@elements
e = m.elements
@elements[0] = e[0] * _x + e[4] * _y + e[8] * _z
@elements[1] = e[1] * _x + e[5] * _y + e[9] * _z
@elements[2] = e[2] * _x + e[6] * _y + e[10] * _z
self.normalize
self
end
|
#unproject(camera) ⇒ Object
103
104
105
106
107
|
# File 'lib/mittsu/math/vector3.rb', line 103
def unproject(camera)
matrix = Mittsu::Matrix4.new
matrix.multiply_matrices(camera.matrix_world, matrix.inverse(camera.projection_matrix))
self.apply_projection(matrix)
end
|
#x ⇒ Object
16
|
# File 'lib/mittsu/math/vector3.rb', line 16
def x; @elements[0]; end
|
#x=(value) ⇒ Object
20
|
# File 'lib/mittsu/math/vector3.rb', line 20
def x=(value); @elements[0] = value.to_f; end
|
#y ⇒ Object
17
|
# File 'lib/mittsu/math/vector3.rb', line 17
def y; @elements[1]; end
|
#y=(value) ⇒ Object
21
|
# File 'lib/mittsu/math/vector3.rb', line 21
def y=(value); @elements[1] = value.to_f; end
|
#z ⇒ Object
18
|
# File 'lib/mittsu/math/vector3.rb', line 18
def z; @elements[2]; end
|
#z=(value) ⇒ Object
22
|
# File 'lib/mittsu/math/vector3.rb', line 22
def z=(value); @elements[2] = value.to_f; end
|