Class: Mittsu::Sphere

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/math/sphere.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center = Mittsu::Vector3.new, radius = 0.0) ⇒ Sphere

Returns a new instance of Sphere.



5
6
7
# File 'lib/mittsu/math/sphere.rb', line 5

def initialize(center = Mittsu::Vector3.new, radius = 0.0)
  @center, @radius = center, radius.to_f
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



3
4
5
# File 'lib/mittsu/math/sphere.rb', line 3

def center
  @center
end

#radiusObject

Returns the value of attribute radius.



3
4
5
# File 'lib/mittsu/math/sphere.rb', line 3

def radius
  @radius
end

Instance Method Details

#==(sphere) ⇒ Object



81
82
83
# File 'lib/mittsu/math/sphere.rb', line 81

def ==(sphere)
  sphere.center == (@center) && sphere.radius == @radius
end

#apply_matrix4(matrix) ⇒ Object



70
71
72
73
74
# File 'lib/mittsu/math/sphere.rb', line 70

def apply_matrix4(matrix)
  @center.apply_matrix4(matrix)
  @radius = @radius * matrix.max_scale_on_axis
  self
end

#bounding_box(target = Mittsu::Box3.new) ⇒ Object



64
65
66
67
68
# File 'lib/mittsu/math/sphere.rb', line 64

def bounding_box(target = Mittsu::Box3.new)
  target.set(@center, @center)
  target.expand_by_scalar(@radius)
  target
end

#clamp_point(point, target = Mittsu::Vector3.new) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/mittsu/math/sphere.rb', line 54

def clamp_point(point, target = Mittsu::Vector3.new)
  delta_length_sq = @center.distance_to_squared(point)
  target.copy(point)
  if delta_length_sq > (@radius * @radius)
    target.sub(@center).normalize
    target.multiply_scalar(@radius).add(@center)
  end
  target
end

#cloneObject



85
86
87
# File 'lib/mittsu/math/sphere.rb', line 85

def clone
  Mittsu::Sphere.new.copy(self)
end

#contains_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mittsu/math/sphere.rb', line 41

def contains_point?(point)
  point.distance_to_squared(@center) <= @radius * @radius
end

#copy(sphere) ⇒ Object



31
32
33
34
35
# File 'lib/mittsu/math/sphere.rb', line 31

def copy(sphere)
  @center.copy(sphere.center)
  @radius = sphere.radius
  self
end

#distance_to_point(point) ⇒ Object



45
46
47
# File 'lib/mittsu/math/sphere.rb', line 45

def distance_to_point(point)
  point.distance_to(@center) - @radius
end

#emptyObject



37
38
39
# File 'lib/mittsu/math/sphere.rb', line 37

def empty
  @radius <= 0
end

#intersects_sphere?(sphere) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/mittsu/math/sphere.rb', line 49

def intersects_sphere?(sphere)
  radiusSum = @radius + sphere.radius
  sphere.center.distance_to_squared(@center) <= radiusSum * radiusSum
end

#set(center, radius) ⇒ Object



9
10
11
12
13
# File 'lib/mittsu/math/sphere.rb', line 9

def set(center, radius)
  @center.copy(center)
  @radius = radius.to_f
  self
end

#set_from_points(points, optional_center = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mittsu/math/sphere.rb', line 15

def set_from_points(points, optional_center = nil)
  box = Mittsu::Box3.new
  c = @center
  if optional_center.nil?
    box.set_from_points(points).center(c)
  else
    c.copy(optional_center)
  end
  max_radius_sq = 0.0
  points.each do |point|
    max_radius_sq = [max_radius_sq, c.distance_to_squared(point)].max
  end
  @radius = ::Math.sqrt(max_radius_sq)
  self
end

#translate(offset) ⇒ Object



76
77
78
79
# File 'lib/mittsu/math/sphere.rb', line 76

def translate(offset)
  @center.add(offset)
  self
end