Class: Ra::Shape::Base

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

Overview

An abstract shape. Any concrete subclass of shape must implement the methods l_normal and t_intersect. Both methods use a point / ray with a local transform applied.

Direct Known Subclasses

Cube, Plane, Sphere

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(material:, transform: Transform::IDENTITY) ⇒ Base



13
14
15
16
# File 'lib/ra/shape/base.rb', line 13

def initialize(material:, transform: Transform::IDENTITY)
  @material = material
  @transform = transform
end

Instance Attribute Details

#materialObject

Returns the value of attribute material.



9
10
11
# File 'lib/ra/shape/base.rb', line 9

def material
  @material
end

Instance Method Details

#color(point:) ⇒ Color



35
36
37
# File 'lib/ra/shape/base.rb', line 35

def color(point:)
  @material.color(point: uv_point(point: @transform.inverse * point))
end

#intersect(ray:) ⇒ Array<Ra::Intersection>



20
21
22
23
# File 'lib/ra/shape/base.rb', line 20

def intersect(ray:)
  t_intersect(ray: ray.transform(@transform.inverse))
    .map { |t| Ra::Intersection.new(ray:, shape: self, t:) }
end

#l_normal(point:) ⇒ Vector

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/ra/shape/base.rb', line 53

def l_normal(point:)
  raise NotImplementedError, '#l_normal must be implemented by a concrete subclass'
end

#normal(point:) ⇒ Vector



27
28
29
30
31
# File 'lib/ra/shape/base.rb', line 27

def normal(point:)
  normal = @transform.inverse.transpose * l_normal(point: @transform.inverse * point)

  Vector[normal[0], normal[1], normal[2], Ra::Tuple::VECTOR].normalize
end

#t_intersect(ray:) ⇒ Array<Intersection>

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/ra/shape/base.rb', line 47

def t_intersect(ray:)
  raise NotImplementedError, '#t_intersect must be implemented by a concrete subclass'
end

#uv_point(point:) ⇒ Vector

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

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/ra/shape/base.rb', line 41

def uv_point(point:)
  raise NotImplementedError, '#uv_point must be implemented by a concrete subclass'
end