Class: Mittsu::Sphere
- Inherits:
-
Object
- Object
- Mittsu::Sphere
- Defined in:
- lib/mittsu/math/sphere.rb
Instance Attribute Summary collapse
-
#center ⇒ Object
Returns the value of attribute center.
-
#radius ⇒ Object
Returns the value of attribute radius.
Instance Method Summary collapse
- #==(sphere) ⇒ Object
- #apply_matrix4(matrix) ⇒ Object
- #bounding_box(target = Mittsu::Box3.new) ⇒ Object
- #clamp_point(point, target = Mittsu::Vector3.new) ⇒ Object
- #clone ⇒ Object
- #contains_point?(point) ⇒ Boolean
- #copy(sphere) ⇒ Object
- #distance_to_point(point) ⇒ Object
- #empty ⇒ Object
-
#initialize(center = Mittsu::Vector3.new, radius = 0.0) ⇒ Sphere
constructor
A new instance of Sphere.
- #intersects_sphere?(sphere) ⇒ Boolean
- #set(center, radius) ⇒ Object
- #set_from_points(points, optional_center = nil) ⇒ Object
- #translate(offset) ⇒ Object
Constructor Details
Instance Attribute Details
#center ⇒ Object
Returns the value of attribute center.
5 6 7 |
# File 'lib/mittsu/math/sphere.rb', line 5 def center @center end |
#radius ⇒ Object
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.(@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 |
#clone ⇒ Object
87 88 89 |
# File 'lib/mittsu/math/sphere.rb', line 87 def clone Mittsu::Sphere.new.copy(self) end |
#contains_point?(point) ⇒ 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 |
#empty ⇒ Object
39 40 41 |
# File 'lib/mittsu/math/sphere.rb', line 39 def empty @radius <= 0 end |
#intersects_sphere?(sphere) ⇒ 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 |