Class: Topolys::Vector3D

Inherits:
Object
  • Object
show all
Defined in:
lib/topolys/geometry.rb

Overview

Point3D

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z) ⇒ Vector3D

Initializes a Vector3D object

Parameters:

  • X-coordinate (Float)
  • Y-coordinate (Float)
  • Z-coordinate (Float)


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

#xFloat (readonly)

Returns X, Y, or Z component.

Returns:

  • (Float)

    X, Y, or Z component



63
64
65
# File 'lib/topolys/geometry.rb', line 63

def x
  @x
end

#yFloat (readonly)

Returns X, Y, or Z component.

Returns:

  • (Float)

    X, Y, or Z component



63
64
65
# File 'lib/topolys/geometry.rb', line 63

def y
  @y
end

#zFloat (readonly)

Returns X, Y, or Z component.

Returns:

  • (Float)

    X, Y, or Z component



63
64
65
# File 'lib/topolys/geometry.rb', line 63

def z
  @z
end

Class Method Details

.x_axisObject



84
85
86
# File 'lib/topolys/geometry.rb', line 84

def Vector3D.x_axis
  Vector3D.new(1,0,0)
end

.y_axisObject



88
89
90
# File 'lib/topolys/geometry.rb', line 88

def Vector3D.y_axis
  Vector3D.new(0,1,0)
end

.z_axisObject



92
93
94
# File 'lib/topolys/geometry.rb', line 92

def Vector3D.z_axis
  Vector3D.new(0,0,1)
end

Instance Method Details

#*(scalar) ⇒ Vector3D

Multiplies a 3D vector by a scalar

Parameters:

  • scalar (Float)

    A scalar

Returns:

  • (Vector3D)

    Returns a new, scaled 3D vector - nil if not numeric



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

Parameters:

  • other (Vector3D)

    Another 3D vector

Returns:

  • (Vector3D)

    Returns a new 3D resultant vector - nil if not Vector3D objects



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

Parameters:

  • other (Vector3D)

    Another 3D vector

Returns:

  • (Vector3D)

    Returns a new 3D resultant vector - nil if not Vector3D objects



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

Parameters:

  • scalar (Float)

    A non-zero scalar

Returns:

  • (Vector3D)

    Returns a new, scaled 3D vector - nil if 0 or not numeric



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

Parameters:

  • other (Vector3D)

    Another 3D vector

Returns:

  • (Float)

    Returns angle - nil if not Vector3D objects



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

Parameters:

  • other (Vector3D)

    Another 3D vector

Returns:

  • (Vector3D)

    Returns cross product - nil if not Vector3D objects



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

Parameters:

  • other (Vector3D)

    Another 3D vector

Returns:

  • (Float)

    Returns dot product - nil if not Vector3D objects



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

#magnitudeFloat

Gets 3D vector magnitude (or length)

Returns:

  • (Float)

    Returns magnitude of the 3D vector



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

Parameters:

  • other (Vector3D)

    Another 3D vector

Returns:

  • (Matrix)

    Returns outer product



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_sObject



80
81
82
# File 'lib/topolys/geometry.rb', line 80

def to_s
  "[#{@x}, #{@y}, #{@z}]"
end