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.



7
8
9
# File 'lib/mittsu/math/sphere.rb', line 7

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.



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

def center
  @center
end

#radiusObject

Returns the value of attribute radius.



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

def radius
  @radius
end

Instance Method Details

#==(sphere) ⇒ Object



83
84
85
# File 'lib/mittsu/math/sphere.rb', line 83

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

#apply_matrix4(matrix) ⇒ Object



72
73
74
75
76
# File 'lib/mittsu/math/sphere.rb', line 72

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

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



66
67
68
69
70
# File 'lib/mittsu/math/sphere.rb', line 66

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



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

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



87
88
89
# File 'lib/mittsu/math/sphere.rb', line 87

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

#contains_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/mittsu/math/sphere.rb', line 43

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

#copy(sphere) ⇒ Object



33
34
35
36
37
# File 'lib/mittsu/math/sphere.rb', line 33

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

#distance_to_point(point) ⇒ Object



47
48
49
# File 'lib/mittsu/math/sphere.rb', line 47

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

#emptyObject



39
40
41
# File 'lib/mittsu/math/sphere.rb', line 39

def empty
  @radius <= 0
end

#intersects_sphere?(sphere) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/mittsu/math/sphere.rb', line 51

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

#set(center, radius) ⇒ Object



11
12
13
14
15
# File 'lib/mittsu/math/sphere.rb', line 11

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

#set_from_points(points, optional_center = nil) ⇒ Object



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

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



78
79
80
81
# File 'lib/mittsu/math/sphere.rb', line 78

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