Class: Stagecraft::Bounding::Frustum

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(planes) ⇒ Frustum

Returns a new instance of Frustum.

Raises:

  • (ArgumentError)


103
104
105
106
107
# File 'lib/stagecraft/core/bounding.rb', line 103

def initialize(planes)
  raise ArgumentError, "frustum requires six planes" unless planes.length == 6

  @planes = planes.freeze
end

Instance Attribute Details

#planesObject (readonly)

Returns the value of attribute planes.



101
102
103
# File 'lib/stagecraft/core/bounding.rb', line 101

def planes
  @planes
end

Class Method Details

.from_matrix(matrix) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/stagecraft/core/bounding.rb', line 109

def self.from_matrix(matrix)
  m = matrix.to_a
  row = ->(index) { [m[index], m[index + 4], m[index + 8], m[index + 12]] }
  r0, r1, r2, r3 = 4.times.map { |index| row.call(index) }
  coefficients = [
    combine(r3, r0, 1), combine(r3, r0, -1),
    combine(r3, r1, 1), combine(r3, r1, -1),
    r2, combine(r3, r2, -1)
  ]
  new(coefficients.map { |a, b, c, d| Plane.new(Larb::Vec3.new(a, b, c), d) })
end

Instance Method Details

#intersects_sphere?(sphere) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/stagecraft/core/bounding.rb', line 121

def intersects_sphere?(sphere)
  planes.all? { |plane| plane.intersects_sphere?(sphere) }
end