Module: ObjParser::MathUtils
- Defined in:
- lib/obj_parser/math_utils.rb
Class Method Summary collapse
- .binormal2_for_vertices_and_texures(vertices, textures) ⇒ Object
- .cross_product(v1, v2) ⇒ Object
- .divide_vector_by_scalar(vector, scalar) ⇒ Object
- .dot(vector1, vector2) ⇒ Object
- .mat_inverse(mat) ⇒ Object
- .mul_mat_per_vector(mat, vector) ⇒ Object
- .mul_vector_by_scalar(vector, scalar) ⇒ Object
- .normal_for_face_with_vertices(face_vertices) ⇒ Object
- .normalized_vector(vector) ⇒ Object
- .orthogonalized_vector_with_vector(vector1, vector2) ⇒ Object
- .substract_vectors(a, b) ⇒ Object
- .sum_vectors(a, b) ⇒ Object
- .tangent2_for_vertices_and_texures(vertices, textures) ⇒ Object
- .tangent_for_vertices_and_texures(vertices, textures) ⇒ Object
- .vector_length(vector) ⇒ Object
Class Method Details
.binormal2_for_vertices_and_texures(vertices, textures) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/obj_parser/math_utils.rb', line 29 def self.binormal2_for_vertices_and_texures(vertices, textures) u1, u2, u3 = textures.map {|texture| texture[0]} v1, v2, v3 = textures.map {|texture| texture[1]} p1, p2, p3 = vertices.map {|vertex| normalized_vector(vertex)} q1 = substract_vectors(p2, p1) q2 = substract_vectors(p3, p2) s1 = v2 - v1 s2 = v3 - v1 binormal = sum_vectors(mul_vector_by_scalar(q1, -s2), mul_vector_by_scalar(q2, s1)) end |
.cross_product(v1, v2) ⇒ Object
40 41 42 43 44 |
# File 'lib/obj_parser/math_utils.rb', line 40 def self.cross_product(v1, v2) [ ( (v1[1] * v2[2]) - (v1[2] * v2[1]) ), - ( (v1[0] * v2[2]) - (v1[2] * v2[0]) ), ( (v1[0] * v2[1]) - (v1[1] * v2[0]) ) ] end |
.divide_vector_by_scalar(vector, scalar) ⇒ Object
58 59 60 |
# File 'lib/obj_parser/math_utils.rb', line 58 def self.divide_vector_by_scalar(vector, scalar) vector.map {|component| component / scalar} end |
.dot(vector1, vector2) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/obj_parser/math_utils.rb', line 74 def self.dot(vector1, vector2) #http://en.wikipedia.org/wiki/Dot_product vector1.each_with_index.map do |component, index| component * vector2[index] end.inject(&:+) end |
.mat_inverse(mat) ⇒ Object
92 93 94 95 96 |
# File 'lib/obj_parser/math_utils.rb', line 92 def self.mat_inverse(mat) m = Matrix[mat[0],mat[1], mat[2]] puts "non regular" unless m.regular? m.regular? ? m.inverse.to_a : m.to_a end |
.mul_mat_per_vector(mat, vector) ⇒ Object
86 87 88 89 90 |
# File 'lib/obj_parser/math_utils.rb', line 86 def self.mul_mat_per_vector(mat, vector) m = Matrix[mat[0], mat[1], mat[2]] v = Matrix[ [vector[0]], [vector[1]], [vector[2]]] (m * v).to_a end |
.mul_vector_by_scalar(vector, scalar) ⇒ Object
54 55 56 |
# File 'lib/obj_parser/math_utils.rb', line 54 def self.mul_vector_by_scalar(vector, scalar) vector.map {|component| component * scalar} end |
.normal_for_face_with_vertices(face_vertices) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/obj_parser/math_utils.rb', line 98 def self.normal_for_face_with_vertices(face_vertices) p1 = face_vertices[0] p2 = face_vertices[1] p3 = face_vertices[2] u = substract_vectors(p2, p1) v = substract_vectors(p3, p1) cross_product(u, v) end |
.normalized_vector(vector) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/obj_parser/math_utils.rb', line 66 def self.normalized_vector(vector) current_vector_length = vector_length(vector) current_vector_length = current_vector_length == 0 ? 1 : current_vector_length vector.map do |component| component / current_vector_length end end |
.orthogonalized_vector_with_vector(vector1, vector2) ⇒ Object
81 82 83 84 |
# File 'lib/obj_parser/math_utils.rb', line 81 def self.orthogonalized_vector_with_vector(vector1, vector2) #Gram-Schmidt orthogonalization -> http://jerome.jouvie.free.fr/opengl-tutorials/Lesson8.php substract_vectors(vector1, mul_vector_by_scalar(vector2, dot(vector2, vector1))) end |
.substract_vectors(a, b) ⇒ Object
46 47 48 |
# File 'lib/obj_parser/math_utils.rb', line 46 def self.substract_vectors a, b [a[0] - b[0], a[1] - b[1], a[2] - b[2]] end |
.sum_vectors(a, b) ⇒ Object
50 51 52 |
# File 'lib/obj_parser/math_utils.rb', line 50 def self.sum_vectors a, b [a[0] + b[0], a[1] + b[1], a[2] + b[2]] end |
.tangent2_for_vertices_and_texures(vertices, textures) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/obj_parser/math_utils.rb', line 18 def self.tangent2_for_vertices_and_texures(vertices, textures) u1, u2, u3 = textures.map {|texture| texture[0]} v1, v2, v3 = textures.map {|texture| texture[1]} p1, p2, p3 = vertices.map {|vertex| normalized_vector(vertex)} q1 = substract_vectors(p2, p1) q2 = substract_vectors(p3, p2) t1 = u2 - u1 t2 = u3 - u1 substract_vectors(mul_vector_by_scalar(q1, t2), mul_vector_by_scalar(q2, t1)) end |
.tangent_for_vertices_and_texures(vertices, textures) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/obj_parser/math_utils.rb', line 7 def self.tangent_for_vertices_and_texures(vertices, textures) u1, u2, u3 = textures.map {|texture| texture[0]} v1, v2, v3 = textures.map {|texture| texture[1]} p1, p2, p3 = vertices.map {|vertex| normalized_vector(vertex)} #Tangent formula: http://jerome.jouvie.free.fr/opengl-tutorials/Lesson8.php nominator = substract_vectors( mul_vector_by_scalar(substract_vectors(p2, p1), (v3 - v1)), mul_vector_by_scalar(substract_vectors(p3, p1), (v2 - v1))) denominator = (u2 - u1) * (v3 - v1) - (v2 - v1) * (u3 - u1) denominator = denominator == 0 ? 1 : denominator divide_vector_by_scalar(nominator, denominator) end |
.vector_length(vector) ⇒ Object
62 63 64 |
# File 'lib/obj_parser/math_utils.rb', line 62 def self.vector_length(vector) Math.sqrt(vector.map { |item| (item * item) }.reduce(:+)) end |