struct Geode::Vector(T, N)

Overview

Generic vector type. Provides a collection of scalars of the same type.

T is the scalar type. N is a positive integer indicating the number of components.

Included Modules

Defined in:

geode/vectors/vector.cr

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from module Geode::CommonVector(T, N)

inspect(io : IO) : Nil inspect, map(& : T -> U) : CommonVector forall U map, map_with_index(offset = 0, & : T, Int32 -> U) : CommonVector(U, N) forall U map_with_index, size size, to_s(io : IO) : Nil to_s, zip_map(other : CommonVector(U, N), & : T, U -> V) : CommonVector(V, N) forall U, V zip_map

Instance methods inherited from module Geode::VectorOperations(N)

&*(scalar : Number) : CommonVector &*, &+(other : CommonVector(T, N)) : CommonVector forall T &+, &-(other : CommonVector(T, N)) : CommonVector forall T &-, *(scalar : Number) : CommonVector *, +(other : CommonVector(T, N)) : CommonVector forall T +, -(other : CommonVector(T, N)) : CommonVector forall T
- : self
-
, /(scalar : Number) : CommonVector /, //(scalar : Number) : CommonVector //, abs : self abs, abs2 : self abs2, ceil : self ceil, clamp(min : CommonVector(T, N), max : CommonVector(T, N)) : CommonVector forall T
clamp(min, max) : CommonVector
clamp(range : Range(CommonVector(T, N), CommonVector(T, N))) : CommonVector forall T
clamp(range : Range) : CommonVector
clamp
, edge(edge : CommonVector(T, N)) : self forall T
edge(edge : T) : self forall T
edge
, floor : self floor, fraction : self fraction, lerp(other : CommonVector(T, N), t : Number) : CommonVector forall T lerp, round(mode : Number::RoundingMode = :ties_even) : self
round(digits : Number, base = 10, *, mode : Number::RoundingMode = :ties_even) : self
round
, scale(vector : CommonVector(T, N)) : CommonVector forall T
scale(amount : Number) : CommonVector
scale
, scale!(vector : CommonVector(T, N)) : CommonVector forall T
scale!(amount : Number) : CommonVector
scale!
, sign : self sign

Instance methods inherited from module Geode::VectorMatrices(T, N)

&*(matrix : CommonMatrix(U, M, M)) : CommonVector forall U, M &*, *(matrix : CommonMatrix(U, M, M)) : CommonVector forall U, M *, to_column : CommonMatrix to_column, to_row : CommonMatrix to_row

Instance methods inherited from module Geode::VectorGeometry(N)

angle(other : CommonVector(T, N)) : Number forall T angle, dot(other : CommonVector(T, N)) forall T dot, dot!(other : CommonVector(T, N)) forall T dot!, forward(surface : CommonVector(T, N)) : CommonVector forall T forward, length length, mag mag, mag2 mag2, normalize : CommonVector normalize, project(other : CommonVector(T, N)) : CommonVector forall T project, reflect(surface : CommonVector(T, N)) : CommonVector forall T reflect, refract(surface : CommonVector(T, N), eta : Number) : CommonVector forall T refract, scale_to(length : Number) : CommonVector scale_to

Instance methods inherited from module Geode::VectorComparison(N)

==(other : CommonVector(T, N)) forall T ==, compare(other : CommonVector(T, N)) : CommonVector(Int32, N) forall T compare, eq?(other : CommonVector(T, N)) : CommonVector(Bool, N) forall T eq?, ge?(other : CommonVector(T, N)) : CommonVector(Bool, N) forall T ge?, gt?(other : CommonVector(T, N)) : CommonVector(Bool, N) forall T gt?, le?(other : CommonVector(T, N)) : CommonVector(Bool, N) forall T le?, lt?(other : CommonVector(T, N)) : CommonVector(Bool, N) forall T lt?, near_zero?(tolerance) near_zero?, zero? zero?

Constructor Detail

def self.new(vector : CommonVector(T, N)) #

Copies the contents of another vector.


[View source]
def self.new(array : StaticArray(T, N)) #

Constructs the vector with pre-existing values.

The memory allocated for components must match the size of the vector.


[View source]
def self.new(components : Indexable(T)) #

Creates a new matrix from a collection of components.

The size of components must be equal to N.

Vector(Int32, 5).new([1, 2, 3, 4, 5]) # => (1, 2, 3, 4, 5)

[View source]
def self.new(&) #

Constructs the vector by yielding for each component.

The value of each component should be returned from the block. The block will be given the index of each component as an argument.

Vector(Int32, 3).new { |i| i * 5 } # => (0, 5, 10)

[View source]
def self.zero : self #

Constructs a zero-vector.

Each component will have a scalar value equal to the type's'zero value. This is done by calling T.zero for each component. The type T must have a class method

Vector(Float32, 3).zero # => (0.0, 0.0, 0.0)

[View source]

Class Method Detail

def self.[](*elements) #

Constructs a vector with existing components.

The type of the components is specified by the type parameter. Each value is cast to the type T.

Vector(Float32, 5)[1, 2, 3, 4, 5] # => (1.0, 2.0, 3.0, 4.0, 5.0)

[View source]

Instance Method Detail

def cross(other : CommonVector(U, 3)) : CommonVector forall U #

Computes the cross-product of this and another vector.

Raises a compilation error if the vector size (N) isn't 3.

Vector[1, 3, 4].cross(Vector[2, -5, 8]) # => (44, 0, -11)

[View source]
def map(& : T -> U) : CommonVector forall U #

Returns a new vector where components are mapped by the given block.

vector = Vector[1, 2, 3]
vector.map { |v| v * 2 } # => (2, 4, 6)

[View source]
def to_slice : Slice(T) #

Returns a slice that points to the components in this vector.


[View source]
def to_unsafe : Pointer(T) #

Returns a pointer to the data for this vector.

The components are tightly packed and ordered consecutively in memory.


[View source]
def unsafe_fetch(index : Int) #

Retrieves the scalar value of the component at the given index, without checking size boundaries.

End-users should never invoke this method directly. Instead, methods like #[] and #[]? should be used.

This method should only be directly invoked if the index is certain to be in bounds.


[View source]
def w : T #

Retrieves the w component.

Raises a compilation error if the vector size (N) is less than 4.


[View source]
def x : T #

Retrieves the x component.

Raises a compilation error if the vector size (N) is less than 1.


[View source]
def y : T #

Retrieves the y component.

Raises a compilation error if the vector size (N) is less than 2.


[View source]
def z : T #

Retrieves the z component.

Raises a compilation error if the vector size (N) is less than 3.


[View source]

Macro Detail

macro [](*components) #

Constructs the vector with existing components.

The type of the components is derived from the type of each argument. The size of the vector is determined by the number of components.

Vector[1, 2, 3] # => (1, 2, 3)

[View source]