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.
3 4 5 |
# File 'lib/mittsu/math/sphere.rb', line 3 def center @center end |
#radius ⇒ Object
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.(@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 |
#clone ⇒ Object
85 86 87 |
# File 'lib/mittsu/math/sphere.rb', line 85 def clone Mittsu::Sphere.new.copy(self) end |
#contains_point?(point) ⇒ 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 |
#empty ⇒ Object
37 38 39 |
# File 'lib/mittsu/math/sphere.rb', line 37 def empty @radius <= 0 end |
#intersects_sphere?(sphere) ⇒ 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 |