Documentation
¶
Overview ¶
Package vecmath is the 3D math of Glutter (SPEC §27.7): vectors, quaternions, matrices, rays, planes, boxes, spheres and frustums in float64, as values that never allocate.
The CPU works in float64 and the GPU in float32 (D30): a CAD scene 50 km wide with a detail of 0.1 mm keeps its precision in the matrices that are multiplied here, and only the result, taken relative to the camera, goes to the GPU through Mat4.Float32.
Matrices are column-major, as WGSL reads them: the entry in row r and column c is m[c*4+r]. A matrix applies to a column vector, so a.Mul(b) applies b first. The clip space is WebGPU's: x and y from -1 to 1, z from 0 to 1, and the view looks down -z. The projections are reversed-Z: the near plane maps to 1 and the far one (or infinity) to 0, which makes the depth of a float32 buffer even over the distance. The rotations are right-handed: a positive angle turns counterclockwise when seen from the positive end of the axis.
Flutter: no counterpart
Index ¶
- Variables
- type Box3
- func (b Box3) BoundingSphere() Sphere
- func (b Box3) Center() Vec3
- func (b Box3) Contains(p Vec3) bool
- func (b Box3) Corner(i int) Vec3
- func (b Box3) DistanceTo(p Vec3) float64
- func (b Box3) Expand(p Vec3) Box3
- func (b Box3) Intersects(o Box3) bool
- func (b Box3) IsEmpty() bool
- func (b Box3) Size() Vec3
- func (b Box3) Transform(m Mat4) Box3
- func (b Box3) Union(o Box3) Box3
- type Frustum
- type Mat3
- type Mat4
- func LookAt(eye, center, up Vec3) Mat4
- func Mat4Identity() Mat4
- func NewMat4FromMatrix4(m *ui.Matrix4) Mat4
- func NewMat4RotationX(angle float64) Mat4
- func NewMat4RotationY(angle float64) Mat4
- func NewMat4RotationZ(angle float64) Mat4
- func NewMat4Scale(v Vec3) Mat4
- func NewMat4TRS(translation Vec3, rotation Quat, scale Vec3) Mat4
- func NewMat4Translation(v Vec3) Mat4
- func Orthographic(left, right, bottom, top, near, far float64) Mat4
- func Perspective(fovY, aspect, near float64) Mat4
- func PerspectiveFinite(fovY, aspect, near, far float64) Mat4
- func (a Mat4) Det() float64
- func (a Mat4) Float32() [16]float32
- func (a Mat4) Inverse() (Mat4, bool)
- func (a Mat4) Mat3() Mat3
- func (a Mat4) Matrix4() ui.Matrix4
- func (a Mat4) Mul(b Mat4) Mat4
- func (a Mat4) MulVec4(v Vec4) Vec4
- func (a Mat4) NearlyEqual(b Mat4, eps float64) bool
- func (a Mat4) NormalMatrix() Mat3
- func (a Mat4) ProjectPoint(p Vec3) (ndc Vec3, w float64)
- func (a Mat4) TransformDirection(v Vec3) Vec3
- func (a Mat4) TransformPoint(p Vec3) Vec3
- func (a Mat4) Translation() Vec3
- func (a Mat4) Transpose() Mat4
- func (a Mat4) WithTranslation(t Vec3) Mat4
- type Plane
- type Quat
- func (a Quat) AxisAngle() (Vec3, float64)
- func (a Quat) Conjugate() Quat
- func (a Quat) Dot(b Quat) float64
- func (a Quat) Inverse() Quat
- func (a Quat) Length() float64
- func (a Quat) Mat3() Mat3
- func (a Quat) Mat4() Mat4
- func (a Quat) Mul(b Quat) Quat
- func (a Quat) Normalize() Quat
- func (a Quat) Rotate(v Vec3) Vec3
- func (a Quat) Slerp(b Quat, t float64) Quat
- type Ray
- type Sphere
- type Vec2
- func (a Vec2) Add(b Vec2) Vec2
- func (a Vec2) Cross(b Vec2) float64
- func (a Vec2) Dot(b Vec2) float64
- func (a Vec2) Float32() [2]float32
- func (a Vec2) Length() float64
- func (a Vec2) LengthSq() float64
- func (a Vec2) Lerp(b Vec2, t float64) Vec2
- func (a Vec2) Neg() Vec2
- func (a Vec2) Normalize() Vec2
- func (a Vec2) Scale(s float64) Vec2
- func (a Vec2) Sub(b Vec2) Vec2
- type Vec3
- func (a Vec3) Add(b Vec3) Vec3
- func (a Vec3) AngleTo(b Vec3) float64
- func (a Vec3) Cross(b Vec3) Vec3
- func (a Vec3) DistanceSqTo(b Vec3) float64
- func (a Vec3) DistanceTo(b Vec3) float64
- func (a Vec3) Dot(b Vec3) float64
- func (a Vec3) Float32() [3]float32
- func (a Vec3) IsFinite() bool
- func (a Vec3) Length() float64
- func (a Vec3) LengthSq() float64
- func (a Vec3) Lerp(b Vec3, t float64) Vec3
- func (a Vec3) Max(b Vec3) Vec3
- func (a Vec3) Min(b Vec3) Vec3
- func (a Vec3) MulComponents(b Vec3) Vec3
- func (a Vec3) NearlyEqual(b Vec3, eps float64) bool
- func (a Vec3) Neg() Vec3
- func (a Vec3) Normalize() Vec3
- func (a Vec3) Perpendicular() Vec3
- func (a Vec3) ProjectOnto(b Vec3) Vec3
- func (a Vec3) Reflect(n Vec3) Vec3
- func (a Vec3) Scale(s float64) Vec3
- func (a Vec3) Sub(b Vec3) Vec3
- func (a Vec3) Vec4(w float64) Vec4
- type Vec4
Constants ¶
This section is empty.
Variables ¶
var ( // Vec3Zero is the vector (0, 0, 0). // // Flutter: no counterpart Vec3Zero = Vec3{} // Vec3X is the unit vector of the x axis. // // Flutter: no counterpart Vec3X = Vec3{X: 1} // Vec3Y is the unit vector of the y axis. // // Flutter: no counterpart Vec3Y = Vec3{Y: 1} // Vec3Z is the unit vector of the z axis. // // Flutter: no counterpart Vec3Z = Vec3{Z: 1} )
The unit vectors and the zero vector. They are values, so no program can change them.
Functions ¶
This section is empty.
Types ¶
type Box3 ¶
type Box3 struct {
// Min is the corner with the smallest coordinates.
Min Vec3
// Max is the corner with the largest coordinates.
Max Vec3
}
Box3 is an axis-aligned box. The empty box has Min above Max, so that Expand and Union of it are the other operand.
Flutter: package:vector_math Aabb3
func EmptyBox3 ¶
func EmptyBox3() Box3
EmptyBox3 returns the box that contains no point.
Flutter: no counterpart
func NewBox3 ¶
NewBox3 creates the box with the two corners, in any order.
Flutter: package:vector_math Aabb3.minMax
func (Box3) BoundingSphere ¶
BoundingSphere returns the sphere that has the box's center and holds its corners.
Flutter: no counterpart
func (Box3) Center ¶
Center returns the middle of the box.
Flutter: package:vector_math Aabb3.center
func (Box3) Contains ¶
Contains says whether the point is in the box, or on its surface.
Flutter: package:vector_math Aabb3.containsVector3
func (Box3) Corner ¶
Corner returns one of the eight corners: bit 0 of i picks the x, bit 1 the y and bit 2 the z of Max over Min.
Flutter: no counterpart
func (Box3) DistanceTo ¶
DistanceTo returns the distance from the point to the box, which is 0 for a point inside.
Flutter: no counterpart
func (Box3) Expand ¶
Expand returns the smallest box that holds the box and the point.
Flutter: package:vector_math Aabb3.hullPoint
func (Box3) Intersects ¶
Intersects says whether the boxes share a point.
Flutter: package:vector_math Aabb3.intersectsWithAabb3
type Frustum ¶
type Frustum struct {
// Planes are, in order, the left, right, bottom, top, near and far planes. The far plane of a projection with an
// infinite far plane has the zero normal and a distance above 0, so that every point is on its inner side.
Planes [6]Plane
}
Frustum is the six planes of the volume that a view matrix and a projection see; the normals point inwards, so a point is inside when its distance to every plane is not negative.
Flutter: package:vector_math Frustum
func NewFrustum ¶
NewFrustum creates the frustum of a matrix that is a projection times a view (the clip-space matrix of a camera), in WebGPU's clip space.
Flutter: package:vector_math Frustum.matrix
func (Frustum) ContainsPoint ¶
ContainsPoint says whether the point is inside the frustum.
Flutter: package:vector_math Frustum.containsVector3
func (Frustum) IntersectsBox ¶
IntersectsBox says whether the box is at least partly inside the frustum. It can say true for a box that is outside near an edge of the frustum.
Flutter: package:vector_math Frustum.intersectsWithAabb3
func (Frustum) IntersectsSphere ¶
IntersectsSphere says whether the sphere is at least partly inside the frustum. It can say true for a sphere that is outside near a corner of the frustum.
Flutter: package:vector_math Frustum.intersectsWithSphere
type Mat3 ¶
type Mat3 [9]float64
Mat3 is a 3x3 matrix, stored column-major: the entry in row r and column c is m[c*3+r].
Flutter: package:vector_math Matrix3
func Mat3Identity ¶
func Mat3Identity() Mat3
Mat3Identity returns the identity matrix.
Flutter: package:vector_math Matrix3.identity
func (Mat3) Float32 ¶
Float32 returns the entries as float32, in the same order.
Flutter: no counterpart
func (Mat3) Inverse ¶
Inverse returns the inverse matrix, and false when the matrix is singular (its determinant is 0 or not a number).
Flutter: package:vector_math Matrix3.invert
func (Mat3) Mul ¶
Mul returns the product: the matrix that applies b first, then a.
Flutter: package:vector_math Matrix3.operator*
type Mat4 ¶
type Mat4 [16]float64
Mat4 is a 4x4 matrix, stored column-major as WGSL reads it: the entry in row r and column c is m[c*4+r]. It applies to column vectors, so a.Mul(b) applies b first.
Flutter: package:vector_math Matrix4
func LookAt ¶
LookAt creates the view matrix of a camera at eye that looks at center, with up as its upward direction: the matrix that turns world space into the space of the camera, which looks down -z. It returns the identity when eye and center are the same point, or when up is along the view.
Flutter: package:vector_math setViewMatrix
func Mat4Identity ¶
func Mat4Identity() Mat4
Mat4Identity returns the identity matrix.
Flutter: package:vector_math Matrix4.identity
func NewMat4FromMatrix4 ¶
NewMat4FromMatrix4 creates the matrix from a float32 matrix of the engine API.
Flutter: no counterpart
func NewMat4RotationX ¶
NewMat4RotationX creates the rotation around the x axis by the angle in radians.
Flutter: package:vector_math Matrix4.rotationX
func NewMat4RotationY ¶
NewMat4RotationY creates the rotation around the y axis by the angle in radians.
Flutter: package:vector_math Matrix4.rotationY
func NewMat4RotationZ ¶
NewMat4RotationZ creates the rotation around the z axis by the angle in radians.
Flutter: package:vector_math Matrix4.rotationZ
func NewMat4Scale ¶
NewMat4Scale creates the matrix that scales by v along the axes.
Flutter: package:vector_math Matrix4.diagonal3
func NewMat4TRS ¶
NewMat4TRS creates the matrix that scales, then rotates, then moves.
Flutter: package:vector_math Matrix4.compose
func NewMat4Translation ¶
NewMat4Translation creates the matrix that moves points by v.
Flutter: package:vector_math Matrix4.translation
func Orthographic ¶
Orthographic creates a projection of the box that goes from left to right, bottom to top, and from near to far in front of the eye, in WebGPU's clip space with reversed-Z: the depth is 1 at near and 0 at far. Near may be negative (a plane behind the eye).
Flutter: package:vector_math setOrthographicMatrix
func Perspective ¶
Perspective creates a projection with the vertical field of view fovY, in radians, the aspect (width over height) and the near plane, in WebGPU's clip space with reversed-Z and the far plane at infinity: a point at the near plane gets the depth 1 and a point infinitely far gets 0.
Flutter: package:vector_math setPerspectiveMatrix
func PerspectiveFinite ¶
PerspectiveFinite is Perspective with a far plane: the depth is 1 at near and 0 at far.
Flutter: package:vector_math setPerspectiveMatrix
func (Mat4) Float32 ¶
Float32 returns the entries as float32, in the same order (what a uniform buffer takes).
Flutter: no counterpart
func (Mat4) Inverse ¶
Inverse returns the inverse matrix, and false when the matrix is singular (its determinant is 0 or not a number).
Flutter: package:vector_math Matrix4.invert
func (Mat4) Mat3 ¶
Mat3 returns the upper left 3x3 block: the rotation and the scale.
Flutter: package:vector_math Matrix4.getRotation
func (Mat4) Matrix4 ¶
Matrix4 returns the matrix as the float32 matrix of the engine API (what a Canvas takes).
Flutter: no counterpart
func (Mat4) Mul ¶
Mul returns the product: the matrix that applies b first, then a.
Flutter: package:vector_math Matrix4.operator*
func (Mat4) MulVec4 ¶
MulVec4 returns the vector transformed by the matrix.
Flutter: package:vector_math Matrix4.transform
func (Mat4) NearlyEqual ¶
NearlyEqual says whether every entry of a is within eps of b's.
Flutter: no counterpart
func (Mat4) NormalMatrix ¶
NormalMatrix returns the matrix for the normals of a surface: the inverse transpose of the upper left block. It is the identity block when that one is singular.
Flutter: package:vector_math Matrix4.copyNormalMatrix
func (Mat4) ProjectPoint ¶
ProjectPoint returns the point transformed by the matrix and divided by its w: the point of normalized device coordinates for a projection matrix, and the point behind the eye gets the negative w of the clip point in the second result.
Flutter: package:vector_math Matrix4.perspectiveTransform
func (Mat4) TransformDirection ¶
TransformDirection returns the vector turned and scaled by the matrix, without its translation.
Flutter: package:vector_math Matrix4.rotate3
func (Mat4) TransformPoint ¶
TransformPoint returns the point moved by the matrix, with w taken as 1 and the result not divided by its w: right for the matrices of an object, not for a projection (use ProjectPoint).
Flutter: package:vector_math Matrix4.transform3
func (Mat4) Translation ¶
Translation returns the last column's first three entries: where the matrix moves the origin.
Flutter: package:vector_math Matrix4.getTranslation
func (Mat4) Transpose ¶
Transpose returns the matrix with rows and columns exchanged.
Flutter: package:vector_math Matrix4.transposed
func (Mat4) WithTranslation ¶
WithTranslation returns the matrix with its translation set to t.
Flutter: package:vector_math Matrix4.setTranslation
type Plane ¶
type Plane struct {
// Normal is the direction that is perpendicular to the plane.
Normal Vec3
// D is minus the dot product of the normal with any point of the plane.
D float64
}
Plane is the points p with Normal.Dot(p) + D = 0. When Normal has length 1, DistanceTo is the distance in the units of the scene, positive on the side that the normal points to.
Flutter: package:vector_math Plane
func NewPlaneFromPointNormal ¶
NewPlaneFromPointNormal creates the plane through the point, perpendicular to the normal (which is made of length 1).
Flutter: package:vector_math Plane.components
func NewPlaneFromPoints ¶
NewPlaneFromPoints creates the plane through the three points; its normal is the one that a counterclockwise triangle a, b, c points to. Points on one line give the plane with the zero normal.
Flutter: package:vector_math Plane.setFromPoints
func (Plane) ClosestPoint ¶
ClosestPoint returns the point of the plane that is nearest to v.
Flutter: no counterpart
func (Plane) DistanceTo ¶
DistanceTo returns the signed distance of the point to the plane.
Flutter: package:vector_math Plane.distanceToVector3
type Quat ¶
type Quat struct {
// X is the i component.
X float64
// Y is the j component.
Y float64
// Z is the k component.
Z float64
// W is the real component.
W float64
}
Quat is a quaternion x*i + y*j + z*k + w. A rotation is a quaternion of length 1 (the identity has w = 1), and a.Mul(b) applies b first, as for matrices.
Flutter: package:vector_math Quaternion
func NewQuatAxisAngle ¶
NewQuatAxisAngle creates the rotation of the angle, in radians, around the axis (which need not have length 1). An axis without length gives the identity.
Flutter: package:vector_math Quaternion.axisAngle
func NewQuatBasis ¶
NewQuatBasis creates the rotation that turns the x, y and z axes into the three vectors, which must be perpendicular and of length 1.
Flutter: no counterpart
func NewQuatFromTo ¶
NewQuatFromTo creates the shortest rotation that turns the direction from into the direction to. Directions without length give the identity.
Flutter: package:vector_math Quaternion.fromTwoVectors
func QuatIdentity ¶
func QuatIdentity() Quat
QuatIdentity is the rotation that does nothing.
Flutter: package:vector_math Quaternion.identity
func (Quat) AxisAngle ¶
AxisAngle returns the axis, of length 1, and the angle, from 0 to 2 pi, of the rotation. The identity has the axis (1, 0, 0) and the angle 0.
Flutter: package:vector_math Quaternion.radians
func (Quat) Conjugate ¶
Conjugate returns the quaternion with the vector part negated, which is the inverse of a rotation.
Flutter: package:vector_math Quaternion.conjugated
func (Quat) Dot ¶
Dot returns the dot product, as of 4D vectors.
Flutter: package:vector_math Quaternion.dot
func (Quat) Inverse ¶
Inverse returns the inverse of the quaternion, which is the conjugate for a rotation. A quaternion without length gives the identity.
Flutter: package:vector_math Quaternion.inverted
func (Quat) Mat3 ¶
Mat3 returns the rotation as a matrix. The quaternion should have length 1.
Flutter: package:vector_math Quaternion.asRotationMatrix
func (Quat) Mat4 ¶
Mat4 returns the rotation as a matrix. The quaternion should have length 1.
Flutter: no counterpart
func (Quat) Mul ¶
Mul returns the product: the rotation that applies b first, then a.
Flutter: package:vector_math Quaternion.operator*
func (Quat) Normalize ¶
Normalize returns the quaternion of length 1 in the direction of a, or the identity when a has no length.
Flutter: package:vector_math Quaternion.normalized
type Ray ¶
type Ray struct {
// Origin is where the ray starts.
Origin Vec3
// Dir is the direction of the ray, normally of length 1; the t of the methods is in units of its length.
Dir Vec3
}
Ray is a half line: the points Origin + t*Dir for t >= 0.
Flutter: package:vector_math Ray
func (Ray) IntersectBox ¶
IntersectBox returns the t where the ray enters the box, which is 0 when the origin is inside, and false when the ray misses the box.
Flutter: package:vector_math Ray.intersectsWithAabb3
func (Ray) IntersectPlane ¶
IntersectPlane returns the t where the ray meets the plane, and false when it never does: the ray is parallel to the plane, or the plane is behind the origin.
Flutter: no counterpart
func (Ray) IntersectSphere ¶
IntersectSphere returns the t where the ray enters the sphere (0 when the origin is inside), and false when it misses.
Flutter: package:vector_math Ray.intersectsWithSphere
func (Ray) IntersectTriangle ¶
IntersectTriangle returns the t where the ray meets the triangle, and false when it misses it or is parallel.
Flutter: package:vector_math Ray.intersectsWithTriangle
type Sphere ¶
type Sphere struct {
// Center is the middle of the ball.
Center Vec3
// Radius is the distance from the center to the surface.
Radius float64
}
Sphere is a ball.
Flutter: package:vector_math Sphere
func (Sphere) Contains ¶
Contains says whether the point is in the ball.
Flutter: package:vector_math Sphere.containsVector3
func (Sphere) Intersects ¶
Intersects says whether the balls share a point.
Flutter: package:vector_math Sphere.intersectsWithSphere
type Vec2 ¶
Vec2 is a 2D vector.
Flutter: package:vector_math Vector2
func (Vec2) Add ¶
Add returns the sum of the vectors.
Flutter: package:vector_math Vector2.operator+
func (Vec2) Cross ¶
Cross returns the z component of the cross product of the vectors seen as 3D.
Flutter: package:vector_math Vector2.cross
func (Vec2) LengthSq ¶
LengthSq returns the squared length.
Flutter: package:vector_math Vector2.length2
func (Vec2) Lerp ¶
Lerp returns the point at t of the way from a to b.
Flutter: package:vector_math Vector2.mix
func (Vec2) Normalize ¶
Normalize returns the vector of length 1 with the direction of a, or the zero vector when a has no length.
Flutter: package:vector_math Vector2.normalized
type Vec3 ¶
type Vec3 struct {
// X is the first component.
X float64
// Y is the second component.
Y float64
// Z is the third component.
Z float64
}
Vec3 is a 3D vector or point.
Flutter: package:vector_math Vector3
func (Vec3) Add ¶
Add returns the sum of the vectors.
Flutter: package:vector_math Vector3.operator+
func (Vec3) AngleTo ¶
AngleTo returns the angle between the vectors, from 0 to pi. It is 0 when one of them has no length.
Flutter: package:vector_math Vector3.angleTo
func (Vec3) Cross ¶
Cross returns the cross product, which is perpendicular to both vectors, with the right-hand rule.
Flutter: package:vector_math Vector3.cross
func (Vec3) DistanceSqTo ¶
DistanceSqTo returns the squared distance between the points.
Flutter: package:vector_math Vector3.distanceToSquared
func (Vec3) DistanceTo ¶
DistanceTo returns the distance between the points.
Flutter: package:vector_math Vector3.distanceTo
func (Vec3) IsFinite ¶
IsFinite says whether no component is infinite or NaN.
Flutter: no counterpart
func (Vec3) LengthSq ¶
LengthSq returns the squared length.
Flutter: package:vector_math Vector3.length2
func (Vec3) Lerp ¶
Lerp returns the point at t of the way from a to b.
Flutter: package:vector_math Vector3.mix
func (Vec3) Max ¶
Max returns the largest of each component.
Flutter: package:vector_math Vector3.max
func (Vec3) Min ¶
Min returns the smallest of each component.
Flutter: package:vector_math Vector3.min
func (Vec3) MulComponents ¶
MulComponents returns the product of the components, one by one.
Flutter: package:vector_math Vector3.operator*
func (Vec3) NearlyEqual ¶
NearlyEqual says whether every component of a is within eps of b's.
Flutter: no counterpart
func (Vec3) Normalize ¶
Normalize returns the vector of length 1 with the direction of a, or the zero vector when a has no length.
Flutter: package:vector_math Vector3.normalized
func (Vec3) Perpendicular ¶
Perpendicular returns a unit vector that is perpendicular to a, or the zero vector when a has no length.
Flutter: no counterpart
func (Vec3) ProjectOnto ¶
ProjectOnto returns the part of a that is along b, or the zero vector when b has no length.
Flutter: no counterpart
func (Vec3) Reflect ¶
Reflect returns the vector reflected by the surface with the normal n, which has length 1.
Flutter: package:vector_math Vector3.reflect
func (Vec3) Scale ¶
Scale returns the vector times the number.
Flutter: package:vector_math Vector3.scale
type Vec4 ¶
type Vec4 struct {
// X is the first component.
X float64
// Y is the second component.
Y float64
// Z is the third component.
Z float64
// W is the fourth component.
W float64
}
Vec4 is a 4D vector, or a point in homogeneous coordinates.
Flutter: package:vector_math Vector4
func (Vec4) Add ¶
Add returns the sum of the vectors.
Flutter: package:vector_math Vector4.operator+
func (Vec4) Lerp ¶
Lerp returns the point at t of the way from a to b.
Flutter: package:vector_math Vector4.mix
func (Vec4) PerspectiveDivide ¶
PerspectiveDivide returns the first three components divided by w: the point of normalized device coordinates that a clip-space point stands for. A w of 0 gives infinities.
Flutter: no counterpart
func (Vec4) Scale ¶
Scale returns the vector times the number.
Flutter: package:vector_math Vector4.scale