Class: Ra::Shape::Cube

Inherits:
Base
  • Object
show all
Defined in:
lib/ra/shape/cube.rb

Overview

A cube centered at <0,0,0> with sides of l=2. A cube surface is defined:

x between (-1..+1)
y between (-1..+1)
z between (-1..+1)
x = ±1 OR y = ±1 OR z = ±1

A ray ‘x` / `y` / `z` values at `t` use the `origin` and `direction`:

x = origin.x + direction.x * t
y = origin.y + direction.y * t
z = origin.z + direction.z * t

The ray therefore may intersect when:

origin.x + direction.x * t = ±1 OR origin.y + direction.y * t = ±1 OR origin.z + direction.z * t = ±1

Thus 6 planes can be checked for intersect.

Instance Attribute Summary

Attributes inherited from Base

#material

Instance Method Summary collapse

Methods inherited from Base

#color, #initialize, #intersect, #normal

Constructor Details

This class inherits a constructor from Ra::Shape::Base

Instance Method Details

#l_normal(point:) ⇒ Ra::Tuple

Parameters:

  • point (Vector)

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ra/shape/cube.rb', line 55

def l_normal(point:)
  x = point[0].abs
  y = point[1].abs
  z = point[2].abs

  Vector[
    (is_x = x > y && x > z) ? 1 : 0,
    (is_y = y > x && y > z) ? 1 : 0,
    is_x || is_y ? 0 : 1,
    Ra::Tuple::VECTOR
  ]
end

#t_intersect(ray:) ⇒ Array<Numeric>

Parameters:

Returns:

  • (Array<Numeric>)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ra/shape/cube.rb', line 41

def t_intersect(ray:)
  t_min = t_min(ray:)
  t_max = t_max(ray:)

  return [] if !t_min || !t_max || t_min > t_max

  [
    t_min,
    t_max,
  ]
end

#uv_point(point:) ⇒ Vector

Returns <u = 0.0..1.0, v = 0.0..1.0>.

Parameters:

  • point (Vector)

    <x, y, z, Tuple::POINT>

Returns:

  • (Vector)

    <u = 0.0..1.0, v = 0.0..1.0>



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ra/shape/cube.rb', line 26

def uv_point(point:)
  x = point[0]
  y = point[1]
  z = point[2]
  value = [x, y, z].max_by(&:abs)

  case value
  when x then x.positive? ? uv_point_r(point:) : uv_point_l(point:)
  when y then y.positive? ? uv_point_u(point:) : uv_point_d(point:)
  else        z.positive? ? uv_point_f(point:) : uv_point_b(point:)
  end
end