Class: CyberarmEngine::Ray

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/ray.rb

Instance Method Summary collapse

Constructor Details

#initialize(origin, direction, range = Float::INFINITY) ⇒ Ray

Returns a new instance of Ray.



3
4
5
6
7
8
9
10
11
12
# File 'lib/cyberarm_engine/ray.rb', line 3

def initialize(origin, direction, range = Float::INFINITY)
  raise "Origin must be a Vector!" unless origin.is_a?(Vector)
  raise "Direction must be a Vector!" unless direction.is_a?(Vector)

  @origin = origin
  @direction = direction
  @range = range

  @inverse_direction = @direction.inverse
end

Instance Method Details

#intersect?(intersectable) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/cyberarm_engine/ray.rb', line 14

def intersect?(intersectable)
  if intersectable.is_a?(BoundingBox)
    intersect_bounding_box?(intersectable)
  else
    raise NotImplementedError, "Ray intersection test for #{intersectable.class} not implemented."
  end
end

#intersect_bounding_box?(box) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cyberarm_engine/ray.rb', line 23

def intersect_bounding_box?(box)
  tmin = -@range
  tmax = @range

  tx1 = (box.min.x - @origin.x) * @inverse_direction.x
  tx2 = (box.max.x - @origin.x) * @inverse_direction.x

  tmin = max(tmin, min(tx1, tx2))
  tmax = min(tmax, max(tx1, tx2))

  ty1 = (box.min.y - @origin.y) * @inverse_direction.y
  ty2 = (box.max.y - @origin.y) * @inverse_direction.y

  tmin = max(tmin, min(ty1, ty2))
  tmax = min(tmax, max(ty1, ty2))

  tz1 = (box.min.z - @origin.z) * @inverse_direction.z
  tz2 = (box.max.z - @origin.z) * @inverse_direction.z

  tmin = max(tmin, min(tz1, tz2))
  tmax = min(tmax, max(tz1, tz2))

  tmax >= max(tmin, 0.0)
end

#max(x, y) ⇒ Object



52
53
54
# File 'lib/cyberarm_engine/ray.rb', line 52

def max(x, y)
  ((x) > (y) ? x : y)
end

#min(x, y) ⇒ Object



48
49
50
# File 'lib/cyberarm_engine/ray.rb', line 48

def min(x, y)
  ((x) < (y) ? x : y)
end