struct Chem::Spatial::Vec3
- Chem::Spatial::Vec3
- Struct
- Value
- Object
Defined in:
chem/spatial/quat.crchem/spatial/vec3.cr
Constant Summary
-
BACK =
-Z
-
The backward unit vector. Shorthand for
Vec3[0, 0, -1]
or-Vec3::Z
. -
DOWN =
-Y
-
The downward unit vector. Shorthand for
Vec3[0, -1, 0]
or-Vec3::Y
. -
FORWARD =
Z
-
The forward unit vector. Shorthand for
Vec3[0, 0, 1]
orVec3::Z
. -
LEFT =
-X
-
The left unit vector. Shorthand for
Vec3[-1, 0, 0]
or-Vec3::X
. -
RIGHT =
X
-
The right unit vector. Shorthand for
Vec3[1, 0, 0]
orVec3::X
. -
UP =
Y
-
The upward unit vector. Shorthand for
Vec3[0, 1, 0]
orVec3::Y
. -
X =
Vec3[1, 0, 0]
-
The unit vector pointing towards the X axis. Shorthand for
Vec3[1, 0, 0]
. -
XY =
Vec3[1, 1, 0].normalize
-
The unit vector pointing towards the XY direction. Shorthand for
Vec3[1, 1, 0].normalize
. -
XYZ =
Vec3[1, 1, 1].normalize
-
The unit vector pointing towards the XYZ direction. Shorthand for
Vec3[1, 1, 1].normalize
. -
XZ =
Vec3[1, 0, 1].normalize
-
The unit vector pointing towards the XZ direction. Shorthand for
Vec3[1, 0, 1].normalize
. -
Y =
Vec3[0, 1, 0]
-
The unit vector pointing towards the Y axis. Shorthand for
Vec3[0, 1, 0]
. -
YZ =
Vec3[0, 1, 1].normalize
-
The unit vector pointing towards the YZ direction. Shorthand for
Vec3[0, 1, 1].normalize
. -
Z =
Vec3[0, 0, 1]
-
The unit vector pointing towards the Z axis. Shorthand for
Vec3[0, 0, 1]
.
Constructors
-
.[](x : Number, y : Number, z : Number) : self
Returns a new vector representing the position (x, y, z).
-
.[](direction : Direction) : self
Returns the unit vector pointing towards the given direction.
-
.additive_identity : self
Returns the additive identity of this type.
-
.from_io(io : IO, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) : self
Reads a vector from io in the given format.
-
.new(x : Float64, y : Float64, z : Float64)
Creates a new vector representing the position (x, y, z).
-
.new(direction : Direction) : self
Returns the unit vector pointing towards the given direction.
-
.rand(random : Random = Random::DEFAULT) : self
Returns a random vector with the elements within 0 and 1.
-
.zero : self
Returns the zero vector.
Instance Method Summary
-
#*(rhs : Number) : self
Returns the element-wise multiplication of the vector by rhs.
-
#*(rhs : Vec3) : self
Returns the element-wise multiplication of the vector by rhs.
-
#*(rhs : Quat) : self
Returns the conjugate of the vector by the inverse of rhs.
-
#+(rhs : Number) : self
Returns the element-wise addition of the vector by rhs.
-
#+(rhs : Vec3) : self
Returns the element-wise addition of the vector by rhs.
-
#+(rhs : Size3) : self
Returns the element-wise addition of the vector by rhs.
-
#-(rhs : Number) : self
Returns the element-wise subtraction of the vector by rhs.
-
#-(rhs : Vec3) : self
Returns the element-wise subtraction of the vector by rhs.
-
#-(rhs : Size3) : self
Returns the element-wise subtraction of the vector by rhs.
-
#- : self
Returns the negation of the vector.
-
#/(rhs : Number) : self
Returns the element-wise division of the vector by rhs.
-
#/(rhs : Vec3) : self
Returns the element-wise division of the vector by rhs.
-
#=~(other : self) : Bool
Returns
true
if the elements of the vectors are close to each other, elsefalse
. -
#[](index : Int32) : Float64
Returns the ith component of the vector in the XYZ order.
-
#abs : Float64
Returns the absolute value (norm or length) of the vector.
-
#abs2 : Float64
Returns the square of the absolute value of the vector.
-
#angle(to other : self) : Float64
Returns the angle (in radians) to the given vector.
-
#backward? : Bool
Returns
true
if the vector faces backward (-Z), elsefalse
. -
#ceil : self
Returns a new vector with all components rounded towards positive infinity.
-
#clamp(min : self | Nil, max : self | Nil) : self
Clamps (component-wise) the vector between min and max.
-
#clamp(min : Float64 | Nil, max : Float64 | Nil) : self
Clamps the absolute value (norm or length) of the vector between min and max.
-
#close_to?(rhs : self, delta : Number = Float64::EPSILON) : Bool
Returns
true
if the elements of the vectors are within delta from each other, elsefalse
. -
#cross(rhs : Vec3) : self
Returns the cross product of the vector and rhs.
-
#distance(to other : self) : Float64
Returns the distance between the vector and other.
-
#distance2(to other : self) : Float64
Returns the squared distance between the vector and other.
-
#dot(rhs : Vec3) : Float64
Returns the dot product of the vector and rhs.
-
#downward? : Bool
Returns
true
if the vector faces downward (-Y), elsefalse
. -
#faces?(other : self) : Bool
Returns
true
if the vector points towards the direction of other, elsefalse
. -
#faces?(direction : Direction) : Bool
Returns
true
if the vector points towards the given direction, elsefalse
. -
#floor : self
Returns a new vector with all components rounded towards negative infinity.
-
#forward? : Bool
Returns
true
if the vector faces forward (Z), elsefalse
. -
#image(i : Int, j : Int, k : Int) : self
Returns vector's PBC image in fractional coordinates
-
#leftward? : Bool
Returns
true
if the vector faces left (-X), elsefalse
. -
#map(& : Float64 -> Number) : self
Returns a vector with the results of the component-wise mapping by the given block.
-
#map_with_index(& : Float64, Int32 -> Number) : self
Returns a vector with the results of the component-wise mapping by the given block yielding both the value and index.
-
#normalize : self
Returns the unit vector pointing in the same direction of the vector.
-
#normalized? : Bool
Returns
true
if the vector is normalized, elsefalse
. -
#orthogonal?(to other : self) : Bool
Returns
true
if the vector is orthogonal to other, elsefalse
. -
#orthogonal?(to direction : Direction) : Bool
Returns
true
if the vector is orthogonal to the given direction, elsefalse
. -
#pad(padding : Number) : self
Returns a vector by increasing the length by padding.
-
#parallel?(to other : self) : Bool
Returns
true
if the vector is parallel to other, elsefalse
. -
#parallel?(to direction : Direction) : Bool
Returns
true
if the vector is parallel to the given direction, elsefalse
. -
#project(vec : self) : self
Returns the projection of the vector on vec.
-
#project(direction : Direction) : self
Returns the projection of the vector onto the given direction.
-
#reject(vec : self) : self
Returns the rejection of the vector on vec.
-
#reject(direction : Direction) : self
Returns the rejection of the vector onto the given direction.
-
#resize(length : Number) : self
Returns a vector pointing in the same direction with the given length.
-
#rightward? : Bool
Returns
true
if the vector faces right (X), elsefalse
. -
#rotate(x : Number, y : Number, z : Number) : self
Returns the vector rotated by the given Euler angles in degrees.
-
#rotate(about rotaxis : Vec3, by angle : Number) : self
Returns the vector rotated about rotaxis by angle degrees.
-
#rotate(about direction : Direction, by angle : Number) : self
Returns the vector rotated about the given direction by angle degrees.
-
#rotate(quat : Quat) : self
Returns the vector rotated by the given quaternion.
-
#round(mode : Number::RoundingMode = :ties_even) : self
Rounds the vector to integer values using rounding mode.
-
#round(digits : Number, mode : Number::RoundingMode = :ties_even) : self
Rounds the vector to the given precision using rounding mode.
-
#signed_angle(to other : self, axis : self) : Float64
Returns the signed angle (in radians) to the given vector.
-
#to_a : Array(Float64)
Returns an array with the components of the vector.
-
#to_io(io : IO, format : IO::ByteFormat = :system_endian) : Nil
Writes the binary representation of the vector to io in the given format.
-
#to_q : Quat
Returns the quaternion representation of the vector, i.e.,
Quat[0, x, y, z]
. -
#to_s(io : IO) : Nil
Same as
#inspect(io)
. -
#transform(transformation : Transform) : self
Returns the vector resulting of applying the given transformation.
-
#transform(& : Float64, Float64, Float64 -> FloatTriple) : self
Returns a new vector with the return value of the given block, which is invoked with the X, Y, and Z components.
-
#translate(by offset : Vec3) : self
Returns the vector translated by the given offset.
- #unsafe_fetch(index : Int) : Float64
-
#upward? : Bool
Returns
true
if the vector faces upward (Y), elsefalse
. -
#wrap(around center : self) : self
Returns the vector by wrapping into the primary unit cell centered at center.
-
#wrap : self
Returns the vector by wrapping into the primary unit cell.
-
#x : Float64
X component of the vector.
-
#xy : self
Returns the XY components (Z set to zero) of the vector.
-
#xz : self
Returns the XZ components (Y set to zero) of the vector.
-
#y : Float64
Y component of the vector.
-
#yz : self
Returns the YZ components (X set to zero) of the vector.
-
#z : Float64
Z component of the vector.
-
#zero? : Bool
Returns
true
if the vector is zero, elsefalse
.
Constructor Detail
Returns a new vector representing the position (x, y, z).
Returns the unit vector pointing towards the given direction.
Returns the additive identity of this type. This is the zero vector.
Reads a vector from io in the given format. See also:
IO#read_bytes
.
Creates a new vector representing the position (x, y, z).
Returns the unit vector pointing towards the given direction.
Returns a random vector with the elements within 0 and 1.
Instance Method Detail
Returns the element-wise multiplication of the vector by rhs.
Returns the element-wise multiplication of the vector by rhs.
Returns the conjugate of the vector by the inverse of rhs. See
Quat#*
for details.
Returns the element-wise subtraction of the vector by rhs.
Returns the element-wise subtraction of the vector by rhs.
Returns true
if the elements of the vectors are close to each
other, else false
. See the #close_to
method.
Returns the ith component of the vector in the XYZ order. Raises
IndexError
if index is invalid.
Returns true
if the vector faces backward (-Z), else false
.
See the #faces?
method.
Clamps (component-wise) the vector between min and max.
Clamps the absolute value (norm or length) of the vector between min and max.
Returns true
if the elements of the vectors are within delta
from each other, else false
.
Vec3[1, 2, 3].close_to?(Vec3[1, 2, 3]) # => true
Vec3[1, 2, 3].close_to?(Vec3[1.001, 1.999, 3.00004], 1e-3) # => true
Vec3[1, 2, 3].close_to?(Vec3[3, 2, 1]) # => false
Vec3[1, 2, 3].close_to?(Vec3[1.001, 1.999, 3.00004], 1e-8) # => false
Returns the squared distance between the vector and other.
Prefer this method instead of #distance
when comparing or
sorting vectors as it runs faster.
Returns true
if the vector faces downward (-Y), else false
.
See the #faces?
method.
Returns true
if the vector points towards the direction of
other, else false
.
Vec3[1, 0, 0].faces?(Vec3[1, 0, 0]) # => true
Vec3[1, 2, 0].faces?(Vec3[1, 0, 0]) # => true
Vec3[1, 2, 3].faces?(Vec3[1, 0, 0]) # => true
Vec3[1, -2, -3].faces?(Vec3[1, 0, 0]) # => true
Vec3[-1, 0, 0].faces?(Vec3[1, 0, 0]) # => false
Vec3[-1, 2, 0].faces?(Vec3[1, 0, 0]) # => false
Vec3[-1, 2, 3].faces?(Vec3[1, 0, 0]) # => false
Vec3[-1, -2, -3].faces?(Vec3[1, 0, 0]) # => false
Returns true
if the vector points towards the given direction,
else false
.
Vec3[1, 0, 0].faces?(:x) # => true
Vec3[1, 2, 0].faces?(:x) # => true
Vec3[1, 2, 3].faces?(:x) # => true
Vec3[1, -2, -3].faces?(:x) # => true
Vec3[-1, 0, 0].faces?(:x) # => false
Vec3[-1, 2, 0].faces?(:x) # => false
Vec3[-1, 2, 3].faces?(:x) # => false
Vec3[-1, -2, -3].faces?(:x) # => false
Returns true
if the vector faces forward (Z), else false
. See
the #faces?
method.
Returns vector's PBC image in fractional coordinates
vec = Vec3[0.456, 0.1, 0.8]
vec.image 1, 0, 0 # => Vec3[1.456, 0.1, 0.8]
vec.image -1, 0, 0 # => Vec3[-0.544, 0.1, 0.8]
vec.image -1, 1, -5 # => Vec3[-0.544, 1.1, -4.2]
Returns true
if the vector faces left (-X), else false
. See
the #faces?
method.
Returns a vector with the results of the component-wise mapping by the given block. This is useful to perform non-standard transformations.
Vec3[1, 2, 3].map(&.**(2)) # => Vec3[1.0, 4.0, 9.0]
Returns a vector with the results of the component-wise mapping by the given block yielding both the value and index. This is useful to perform non-standard transformations.
Vec3[1, 2, 3].map { |ele, i| ele * i } # => Vec3[0.0, 2.0, 6.0]
Returns the unit vector pointing in the same direction of the vector.
A normalized or "unit" vector maintains its direction but its length becomes 1.
v = Vec3[2.5, 0, 0].normalize # => Vec[1.0, 0.0, 0.0]
v.abs # => 1.0
v = Vec3[1, 1, 1].normalize # => Vec[0.577, 0.577, 0.577]
v.abs # => 1.0
Returns true
if the vector is normalized, else false
.
A normalized or "unit" vector is a vector with the length equal (close) to 1.
Returns true
if the vector is orthogonal to other, else
false
.
Returns true
if the vector is orthogonal to the given direction,
else false
.
Returns a vector by increasing the length by padding.
Vec3[1, 0, 0].pad(2) # => Vec3[3, 0, 0]
a = Vec3[1, 2, 3]
a.abs # => 3.7416573867739413
b = a.pad(2) # => Vec3[1.535, 3.069, 4.604]
b.abs # => 5.741657386773941
a.normalize =~ b.normalize # => true
Returns true
if the vector is parallel to other, else false
.
Returns true
if the vector is parallel to the given direction,
else false
.
Returns the projection of the vector onto the given direction.
Vec3[1, 2, 3].project(:y) # => Vec3[ 0 2 0 ]
Vec3[1, 2, 3].project(:xz) # => Vec3[ 2 0 2 ]
Returns the rejection of the vector onto the given direction.
Vec3[1, 2, 3].reject(:y) # => Vec3[ 1 0 3 ]
Vec3[1, 2, 3].reject(:xz) # => Vec3[-1 2 1 ]
Returns a vector pointing in the same direction with the given length.
a = Vec3[1, 2, 3]
a.abs # => 3.7416573867739413
b = a.resize 0.5 # => Vec3[0.134, 0.267, 0.401]
b.abs # => 0.5
b.normalize =~ a.normalize # => true
Returns true
if the vector faces right (X), else false
. See
the #faces?
method.
Returns the vector rotated by the given Euler angles in degrees.
Delegates to Quat.rotation
for computing the rotation.
Returns the vector rotated about rotaxis by angle
degrees. Delegates to Quat.rotation
for computing the rotation.
Returns the vector rotated about the given direction by angle
degrees. Delegates to Quat.rotation
for computing the rotation.
Rounds the vector to integer values using rounding mode. See the
Number#round
method for details.
Rounds the vector to the given precision using rounding mode.
See the Number#round
method for details.
Returns the signed angle (in radians) to the given vector.
The sign of the angle is positive if the motion is counterclockwise with respect to axis, else negative.
Returns an array with the components of the vector.
Vec3[1, 2, 3].to_a # => [1.0, 2.0, 3.0]
Writes the binary representation of the vector to io in the
given format. See also IO#write_bytes
.
Returns the quaternion representation of the vector, i.e.,
Quat[0, x, y, z]
.
Returns the vector resulting of applying the given transformation.
Returns a new vector with the return value of the given block, which is invoked with the X, Y, and Z components.
Vec3[3, 2, 1].transform do |x, y, z|
x *= 2
z /= 0.5
{x, y, z}
end # => Vec3[6, 2, 2]
Returns the vector translated by the given offset.
Returns true
if the vector faces upward (Y), else false
. See
the #faces?
method.
Returns the vector by wrapping into the primary unit cell centered at center. The vector is assumed to be expressed in fractional coordinates.
Returns the vector by wrapping into the primary unit cell. The vector is assumed to be expressed in fractional coordinates.