Class: Stagecraft::Bounding::Box3

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min = nil, max = nil) ⇒ Box3

Returns a new instance of Box3.



8
9
10
11
# File 'lib/stagecraft/core/bounding.rb', line 8

def initialize(min = nil, max = nil)
  @min = min || Larb::Vec3.new(Float::INFINITY, Float::INFINITY, Float::INFINITY)
  @max = max || Larb::Vec3.new(-Float::INFINITY, -Float::INFINITY, -Float::INFINITY)
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



6
7
8
# File 'lib/stagecraft/core/bounding.rb', line 6

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



6
7
8
# File 'lib/stagecraft/core/bounding.rb', line 6

def min
  @min
end

Instance Method Details

#centerObject



23
24
25
26
27
# File 'lib/stagecraft/core/bounding.rb', line 23

def center
  return Larb::Vec3.new if empty?

  (min + max) * 0.5
end

#empty?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/stagecraft/core/bounding.rb', line 19

def empty?
  min.x > max.x || min.y > max.y || min.z > max.z
end

#expand_by_point(point) ⇒ Object



13
14
15
16
17
# File 'lib/stagecraft/core/bounding.rb', line 13

def expand_by_point(point)
  @min = min.min(point)
  @max = max.max(point)
  self
end

#intersects_ray?(origin, inverse_direction) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/stagecraft/core/bounding.rb', line 33

def intersects_ray?(origin, inverse_direction)
  tmin = -Float::INFINITY
  tmax = Float::INFINITY
  3.times do |axis|
    first = (min[axis] - origin[axis]) * inverse_direction[axis]
    second = (max[axis] - origin[axis]) * inverse_direction[axis]
    first, second = second, first if first > second
    tmin = [tmin, first].max
    tmax = [tmax, second].min
    return false if tmax < [tmin, 0.0].max
  end
  true
end

#sizeObject



29
30
31
# File 'lib/stagecraft/core/bounding.rb', line 29

def size
  empty? ? Larb::Vec3.new : max - min
end