Class: Mittsu::Vector3

Inherits:
Vector
  • Object
show all
Defined in:
lib/mittsu/math/vector3.rb

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

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

#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)
  # input: THREE.Matrix4 affine matrix
  _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)
  # input: THREE.Matrix4 projection matrix
  _x, _y, _z = *@elements

  e = m.elements
  d = 1.0 / (e[3] * _x + e[7] * _y + e[11] * _z + e[15]) # perspective divide

  @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

  # calculate quat * vector
  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

  # calculate result * inverse quat
  @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

#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

#from_attribute(attribute, index, offset = 0) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/mittsu/math/vector3.rb', line 190

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

#length_manhattanObject



129
130
131
# File 'lib/mittsu/math/vector3.rb', line 129

def length_manhattan
  x.abs + y.abs + 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

#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_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

#transform_direction(m) ⇒ Object



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)
  # input: THREE.Matrix4 affine matrix
  # vector interpreted as a direction
  _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

#xObject



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

#yObject



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

#zObject



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