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

Returns a new instance of Base.

Parameters:

  • material (Ra::Material)
  • transform (Ra::Matrix) (defaults to: Transform::IDENTITY)


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

#transformObject

Returns the value of attribute transform.



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

def transform
  @transform
end

Instance Method Details

#color(point:) ⇒ Color

Parameters:

  • point (Vector)

Returns:



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

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

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

Parameters:

Returns:



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

#normal(point:) ⇒ Vector

Parameters:

  • point (Vector)

Returns:

  • (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