class Object

Public Instance Methods

angle_between(a, b) click to toggle source
# File lib/ruby3mf/mesh_normal_analyzer.rb, line 185
def angle_between(a, b)
  cos_theta = dot_product(a, b) / (magnitude(a) * magnitude(b))
  Math.acos(cos_theta)
end
cross_product(a, b) click to toggle source

Various utility functions

# File lib/ruby3mf/mesh_normal_analyzer.rb, line 160
def cross_product(a, b)
  result = [0, 0, 0]
  result[0] = a[1]*b[2] - a[2]*b[1]
  result[1] = a[2]*b[0] - a[0]*b[2]
  result[2] = a[0]*b[1] - a[1]*b[0]

  result
end
dot_product(a, b) click to toggle source
# File lib/ruby3mf/mesh_normal_analyzer.rb, line 169
def dot_product(a, b)
  a[0]*b[0] + a[1]*b[1] + a[2]*b[2]
end
equal(a, b) click to toggle source
# File lib/ruby3mf/mesh_normal_analyzer.rb, line 181
def equal(a, b)
  (a[0] - b[0]).abs < 0.0001 && (a[1] - b[1]).abs < 0.0001 && (a[2] - b[2]).abs < 0.0001
end
magnitude(a) click to toggle source
# File lib/ruby3mf/mesh_normal_analyzer.rb, line 177
def magnitude(a)
  Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2])
end
normalize(a) click to toggle source
# File lib/ruby3mf/mesh_normal_analyzer.rb, line 190
def normalize(a)
  length = magnitude(a)
  [a[0]/length, a[1]/length, a[2]/length]
end
point_cloud_center(vertices) click to toggle source
# File lib/ruby3mf/mesh_normal_analyzer.rb, line 195
def point_cloud_center(vertices)
  if vertices.count < 1
    return [0, 0, 0]
  end

  vertex = vertices[0]
  min_x = max_x = vertex[0]
  min_y = max_y = vertex[1]
  min_z = max_z = vertex[2]

  vertices.each do |vertex|
    x = vertex[0]
    y = vertex[1]
    z = vertex[2]

    min_x = x if x < min_x
    max_x = x if x > max_x
    min_y = y if y < min_y
    max_y = y if y > max_y
    min_z = z if z < min_z
    max_z = z if z > max_z
  end

  [(min_x + max_x) / 2.0, (min_y + max_y) / 2.0, (min_z + max_z) / 2.0]
end
vector_to(a, b) click to toggle source
# File lib/ruby3mf/mesh_normal_analyzer.rb, line 173
def vector_to(a, b)
  [b[0] - a[0], b[1] - a[1], b[2] - a[2]]
end