Class: Topolys::Vector3D
- Inherits:
-
Object
- Object
- Topolys::Vector3D
- Defined in:
- lib/topolys/geometry.rb
Overview
Point3D
Instance Attribute Summary collapse
-
#x ⇒ Float
readonly
X, Y, or Z component.
-
#y ⇒ Float
readonly
X, Y, or Z component.
-
#z ⇒ Float
readonly
X, Y, or Z component.
Class Method Summary collapse
Instance Method Summary collapse
-
#*(scalar) ⇒ Vector3D
Multiplies a 3D vector by a scalar.
-
#+(other) ⇒ Vector3D
Adds 2x 3D vectors - overrides ‘+’ operator.
-
#-(other) ⇒ Vector3D
Subtracts a 3D vector from another 3D vector - overrides ‘-’ operator.
-
#/(scalar) ⇒ Vector3D
Divides a 3D vector by a non-zero scalar.
-
#angle(other) ⇒ Float
Gets angle [0,PI) between self & another 3D vector.
-
#cross(other) ⇒ Vector3D
Gets the cross product between self & another 3D vector.
-
#dot(other) ⇒ Float
Gets the dot (or inner) product of self & another 3D vector.
-
#initialize(x, y, z) ⇒ Vector3D
constructor
Initializes a Vector3D object.
-
#magnitude ⇒ Float
Gets 3D vector magnitude (or length).
-
#normalize! ⇒ Object
Normalizes a 3D vector.
-
#outer_product(other) ⇒ Matrix
Gets the outer product between self & another 3D vector.
- #to_s ⇒ Object
Constructor Details
#initialize(x, y, z) ⇒ Vector3D
Initializes a Vector3D object
71 72 73 74 75 76 77 78 |
# File 'lib/topolys/geometry.rb', line 71 def initialize(x, y, z) raise "Incorrect x argument for Vector3D, expected Numeric but got #{x.class}" unless x.is_a?(Numeric) raise "Incorrect y argument for Vector3D, expected Numeric but got #{y.class}" unless y.is_a?(Numeric) raise "Incorrect z argument for Vector3D, expected Numeric but got #{z.class}" unless z.is_a?(Numeric) @x = x @y = y @z = z end |
Instance Attribute Details
#x ⇒ Float (readonly)
Returns X, Y, or Z component.
63 64 65 |
# File 'lib/topolys/geometry.rb', line 63 def x @x end |
#y ⇒ Float (readonly)
Returns X, Y, or Z component.
63 64 65 |
# File 'lib/topolys/geometry.rb', line 63 def y @y end |
#z ⇒ Float (readonly)
Returns X, Y, or Z component.
63 64 65 |
# File 'lib/topolys/geometry.rb', line 63 def z @z end |
Class Method Details
.x_axis ⇒ Object
84 85 86 |
# File 'lib/topolys/geometry.rb', line 84 def Vector3D.x_axis Vector3D.new(1,0,0) end |
Instance Method Details
#*(scalar) ⇒ Vector3D
Multiplies a 3D vector by a scalar
131 132 133 134 135 136 137 |
# File 'lib/topolys/geometry.rb', line 131 def *(scalar) return nil unless scalar.is_a?(Numeric) x = @x * scalar y = @y * scalar z = @z * scalar return Topolys::Vector3D.new(x, y, z) end |
#+(other) ⇒ Vector3D
Adds 2x 3D vectors - overrides ‘+’ operator
102 103 104 105 106 107 108 |
# File 'lib/topolys/geometry.rb', line 102 def +(other) return nil unless other.is_a?(Topolys::Vector3D) x = @x + other.x y = @y + other.y z = @z + other.z return Topolys::Vector3D.new(x, y, z) end |
#-(other) ⇒ Vector3D
Subtracts a 3D vector from another 3D vector - overrides ‘-’ operator. Leaves original 3D vector intact if other is not a Vector3D object
117 118 119 120 121 122 123 |
# File 'lib/topolys/geometry.rb', line 117 def -(other) return nil unless other.is_a?(Topolys::Vector3D) x = @x - other.x y = @y - other.y z = @z - other.z return Topolys::Vector3D.new(x, y, z) end |
#/(scalar) ⇒ Vector3D
Divides a 3D vector by a non-zero scalar
145 146 147 148 149 150 151 152 |
# File 'lib/topolys/geometry.rb', line 145 def /(scalar) return nil unless scalar.is_a?(Numeric) return nil if scalar.zero? x = @x / scalar y = @y / scalar z = @z / scalar Topolys::Vector3D.new(x, y, z) end |
#angle(other) ⇒ Float
Gets angle [0,PI) between self & another 3D vector
225 226 227 228 229 230 231 232 233 |
# File 'lib/topolys/geometry.rb', line 225 def angle(other) return nil unless other.is_a?(Topolys::Vector3D) prod = magnitude * other.magnitude return nil if prod.zero? val = dot(other) / prod val = [-1.0, val].max val = [ val, 1.0].min Math.acos(val) end |
#cross(other) ⇒ Vector3D
Gets the cross product between self & another 3D vector
190 191 192 193 194 195 196 |
# File 'lib/topolys/geometry.rb', line 190 def cross(other) return nil unless other.is_a?(Topolys::Vector3D) x = @y * other.z - @z * other.y y = @z * other.x - @x * other.z z = @x * other.y - @y * other.x return Topolys::Vector3D.new(x, y, z) end |
#dot(other) ⇒ Float
Gets the dot (or inner) product of self & another 3D vector
179 180 181 182 |
# File 'lib/topolys/geometry.rb', line 179 def dot(other) return nil unless other.is_a?(Topolys::Vector3D) return @x * other.x + @y * other.y + @z * other.z end |
#magnitude ⇒ Float
Gets 3D vector magnitude (or length)
158 159 160 |
# File 'lib/topolys/geometry.rb', line 158 def magnitude Math.sqrt(@x**2 + @y**2 + @z**2) end |
#normalize! ⇒ Object
Normalizes a 3D vector
164 165 166 167 168 169 170 171 |
# File 'lib/topolys/geometry.rb', line 164 def normalize! n = magnitude unless n.zero? @x /= n @y /= n @z /= n end end |
#outer_product(other) ⇒ Matrix
Gets the outer product between self & another 3D vector
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/topolys/geometry.rb', line 204 def outer_product(other) return nil unless other.is_a?(Topolys::Vector3D) result = Matrix.zero(3,3) result[0,0] = @x*other.x result[0,1] = @x*other.y result[0,2] = @x*other.z result[1,0] = @y*other.x result[1,1] = @y*other.y result[1,2] = @y*other.z result[2,0] = @z*other.x result[2,1] = @z*other.y result[2,2] = @z*other.z return result end |
#to_s ⇒ Object
80 81 82 |
# File 'lib/topolys/geometry.rb', line 80 def to_s "[#{@x}, #{@y}, #{@z}]" end |