Class: Stagecraft::Bounding::Sphere

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/core/bounding.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center = Larb::Vec3.new, radius = 0.0) ⇒ Sphere

Returns a new instance of Sphere.



51
52
53
54
# File 'lib/stagecraft/core/bounding.rb', line 51

def initialize(center = Larb::Vec3.new, radius = 0.0)
  @center = center
  @radius = radius.to_f
end

Instance Attribute Details

#centerObject (readonly)

Returns the value of attribute center.



49
50
51
# File 'lib/stagecraft/core/bounding.rb', line 49

def center
  @center
end

#radiusObject (readonly)

Returns the value of attribute radius.



49
50
51
# File 'lib/stagecraft/core/bounding.rb', line 49

def radius
  @radius
end

Instance Method Details

#intersects_ray?(origin, direction) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stagecraft/core/bounding.rb', line 67

def intersects_ray?(origin, direction)
  offset = center - origin
  projected = offset.dot(direction)
  distance_squared = offset.length_squared - (projected * projected)
  return nil if distance_squared > radius * radius

  half_chord = Math.sqrt((radius * radius) - distance_squared)
  near = projected - half_chord
  far = projected + half_chord
  far.negative? ? nil : [near, 0.0].max
end

#transform(matrix) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/stagecraft/core/bounding.rb', line 56

def transform(matrix)
  elements = matrix.to_a
  scale = [
    Math.sqrt(elements[0]**2 + elements[1]**2 + elements[2]**2),
    Math.sqrt(elements[4]**2 + elements[5]**2 + elements[6]**2),
    Math.sqrt(elements[8]**2 + elements[9]**2 + elements[10]**2)
  ].max
  transformed = matrix * center.to_vec4(1.0)
  self.class.new(transformed.xyz, radius * scale)
end