Documentation
¶
Overview ¶
Package math3d provides dependency-free geometric value types and operations.
Conventions ¶
The package uses right-handed Cartesian cross products. View and world helpers treat local forward as the negative Z direction; the package does not prescribe a world-up axis.
Matrices use row-vector semantics. A point is transformed as [x y z 1] * M, while a direction uses [x y z 0] * M. Surface normals use the inverse transpose of M's linear transform so they remain perpendicular under non-uniform scale and shear. Translation occupies M41, M42, and M43. In a product A * B, A is applied first and B second.
Rotation APIs accept radians unless their documentation explicitly says otherwise. Positive rotations follow the right-hand rule. Coordinate record fields whose upstream declarations do not specify units document their units individually rather than inheriting this rule.
Primary floating-point values use float32, matching the upstream C# float representation. Types prefixed with D use float64. AxisAngle is also float64-based to match its upstream representation. Floating-point values retain normal Go IEEE 754 equality behavior, including its behavior for NaN. Approximate comparisons use an explicit absolute tolerance; conveniences may use the source default of 1e-7. No mutable global tolerance is used.
Values have public fields and may be constructed and changed directly by callers. Package operations use value semantics: they do not mutate value arguments or receiver values, and return changed values instead. A zero value is safe to store and pass, but it is not necessarily geometrically meaningful; operations document any stronger preconditions and report ordinary failures with an ok result.
Struct layout is an API representation, not a binary wire format. Any binary encoding supplied by this package defines component order and endianness explicitly instead of serializing raw struct memory.
Index ¶
- Constants
- func CatmullRom(value1, value2, value3, value4, amount float32) float32
- func CombineHash(first, second int32) int32
- func Coplanar(a, b, c, d Vec3, tolerance float32) bool
- func HashInts(values ...int32) int32
- func Hermite(value1, tangent1, value2, tangent2, amount float32) float32
- func IsNonZeroAndValid(value, tolerance float32) bool
- func NearestPowerOfTwo(value int32) (int32, bool)
- func Percentage(denominator, numerator float64) float64
- func RandomFloat(minimum, maximum float32, index, seed int32) float32
- func RandomInt(index, seed int32) int32
- func RandomUint(index, seed int32) uint32
- func RandomUnitFloat(index, seed int32) float32
- func SmoothStep(value1, value2, amount float32) float32
- func Smoothstep(x float64) float64
- func StatsAverage[T statsVector](stats Stats[T]) (T, bool)
- func StatsExtents[T statsVector](stats Stats[T]) T
- func StatsMiddle[T statsVector](stats Stats[T]) T
- func ToDegrees(radians float64) float64
- func ToRadians(degrees float64) float64
- func Version() string
- func WrapAngle(angle float32) float32
- type AxisAngle
- type Box2
- func (b Box2) AlmostEqual(other Box2, tolerance float32) bool
- func (b Box2) Center() Vec2
- func (b Box2) ContainsBox(other Box2) Containment
- func (b Box2) ContainsPoint(point Vec2) bool
- func (b Box2) Corners() [4]Vec2
- func (b Box2) Empty() bool
- func (b Box2) Expanded(point Vec2) Box2
- func (b Box2) Extent() Vec2
- func (b Box2) Intersection(other Box2) (Box2, bool)
- func (b Box2) Intersects(other Box2) bool
- func (b Box2) Merge(other Box2) Box2
- func (b Box2) Translated(offset Vec2) Box2
- func (b Box2) Valid() bool
- type Box3
- func (b Box3) AlmostEqual(other Box3, tolerance float32) bool
- func (b Box3) Center() Vec3
- func (b Box3) ContainsBox(other Box3) Containment
- func (b Box3) ContainsPoint(point Vec3) bool
- func (b Box3) ContainsSphere(sphere Sphere) Containment
- func (b Box3) Corners() [8]Vec3
- func (b Box3) DBox3() DBox3
- func (b Box3) Empty() bool
- func (b Box3) Expanded(point Vec3) Box3
- func (b Box3) Extent() Vec3
- func (b Box3) Intersection(other Box3) (Box3, bool)
- func (b Box3) Intersects(other Box3) bool
- func (b Box3) IntersectsPlane(plane Plane) PlaneIntersection
- func (b Box3) IntersectsSphere(sphere Sphere) bool
- func (b Box3) Merge(other Box3) Box3
- func (b Box3) Transformed(matrix Mat4) (Box3, bool)
- func (b Box3) Translated(offset Vec3) Box3
- func (b Box3) Valid() bool
- type Box4
- func (b Box4) AlmostEqual(other Box4, tolerance float32) bool
- func (b Box4) Center() Vec4
- func (b Box4) Empty() bool
- func (b Box4) Expanded(point Vec4) Box4
- func (b Box4) Extent() Vec4
- func (b Box4) Intersection(other Box4) (Box4, bool)
- func (b Box4) Merge(other Box4) Box4
- func (b Box4) Valid() bool
- type Byte2
- type Byte3
- type Byte4
- type Complex
- type Containment
- type Cylindrical
- type DBox2
- func (b DBox2) AlmostEqual(other DBox2, tolerance float64) bool
- func (b DBox2) Center() DVec2
- func (b DBox2) Empty() bool
- func (b DBox2) Expanded(point DVec2) DBox2
- func (b DBox2) Extent() DVec2
- func (b DBox2) Intersection(other DBox2) (DBox2, bool)
- func (b DBox2) Merge(other DBox2) DBox2
- func (b DBox2) Valid() bool
- type DBox3
- func (b DBox3) AlmostEqual(other DBox3, tolerance float64) bool
- func (b DBox3) Box3() Box3
- func (b DBox3) Center() DVec3
- func (b DBox3) ContainsPoint(point DVec3) bool
- func (b DBox3) Empty() bool
- func (b DBox3) Expanded(point DVec3) DBox3
- func (b DBox3) Extent() DVec3
- func (b DBox3) Intersection(other DBox3) (DBox3, bool)
- func (b DBox3) Merge(other DBox3) DBox3
- func (b DBox3) Valid() bool
- type DBox4
- func (b DBox4) AlmostEqual(other DBox4, tolerance float64) bool
- func (b DBox4) Center() DVec4
- func (b DBox4) Empty() bool
- func (b DBox4) Expanded(point DVec4) DBox4
- func (b DBox4) Extent() DVec4
- func (b DBox4) Intersection(other DBox4) (DBox4, bool)
- func (b DBox4) Merge(other DBox4) DBox4
- func (b DBox4) Valid() bool
- type DInterval
- func (i DInterval) AlmostEqual(other DInterval, tolerance float64) bool
- func (i DInterval) Center() float64
- func (i DInterval) Empty() bool
- func (i DInterval) Expanded(value float64) DInterval
- func (i DInterval) Extent() float64
- func (i DInterval) Intersection(other DInterval) (DInterval, bool)
- func (i DInterval) Merge(other DInterval) DInterval
- func (i DInterval) Valid() bool
- type DPlane
- type DQuat
- type DRay
- type DSphere
- type DVec2
- func (v DVec2) Add(o DVec2) DVec2
- func (v DVec2) AlmostEqual(o DVec2, tolerance float64) bool
- func (v DVec2) Cross(o DVec2) float64
- func (v DVec2) Div(o DVec2) DVec2
- func (v DVec2) Dot(o DVec2) float64
- func (v DVec2) IsFinite() bool
- func (v DVec2) IsInf() bool
- func (v DVec2) IsNaN() bool
- func (v DVec2) Magnitude() float64
- func (v DVec2) MagnitudeSquared() float64
- func (v DVec2) Max(o DVec2) DVec2
- func (v DVec2) MaxComponent() float64
- func (v DVec2) Min(o DVec2) DVec2
- func (v DVec2) MinComponent() float64
- func (v DVec2) Mul(o DVec2) DVec2
- func (v DVec2) Negated() DVec2
- func (v DVec2) Normalized() (DVec2, bool)
- func (v DVec2) ProductComponents() float64
- func (v DVec2) Scale(s float64) DVec2
- func (v DVec2) Sub(o DVec2) DVec2
- func (v DVec2) SumComponents() float64
- func (v DVec2) SumSquaredComponents() float64
- func (v DVec2) Vec2() Vec2
- type DVec3
- func (v DVec3) Add(o DVec3) DVec3
- func (v DVec3) AlmostEqual(o DVec3, tolerance float64) bool
- func (v DVec3) Cross(o DVec3) DVec3
- func (v DVec3) Div(o DVec3) DVec3
- func (v DVec3) Dot(o DVec3) float64
- func (v DVec3) IsFinite() bool
- func (v DVec3) IsInf() bool
- func (v DVec3) IsNaN() bool
- func (v DVec3) Magnitude() float64
- func (v DVec3) MagnitudeSquared() float64
- func (v DVec3) Max(o DVec3) DVec3
- func (v DVec3) MaxComponent() float64
- func (v DVec3) Min(o DVec3) DVec3
- func (v DVec3) MinComponent() float64
- func (v DVec3) Mul(o DVec3) DVec3
- func (v DVec3) Negated() DVec3
- func (v DVec3) Normalized() (DVec3, bool)
- func (v DVec3) ProductComponents() float64
- func (v DVec3) Scale(s float64) DVec3
- func (v DVec3) Sub(o DVec3) DVec3
- func (v DVec3) SumComponents() float64
- func (v DVec3) SumSquaredComponents() float64
- func (v DVec3) Vec3() Vec3
- type DVec4
- func (v DVec4) Add(o DVec4) DVec4
- func (v DVec4) AlmostEqual(o DVec4, tolerance float64) bool
- func (v DVec4) Div(o DVec4) DVec4
- func (v DVec4) Dot(o DVec4) float64
- func (v DVec4) IsFinite() bool
- func (v DVec4) IsInf() bool
- func (v DVec4) IsNaN() bool
- func (v DVec4) Magnitude() float64
- func (v DVec4) MagnitudeSquared() float64
- func (v DVec4) Max(o DVec4) DVec4
- func (v DVec4) MaxComponent() float64
- func (v DVec4) Min(o DVec4) DVec4
- func (v DVec4) MinComponent() float64
- func (v DVec4) Mul(o DVec4) DVec4
- func (v DVec4) Negated() DVec4
- func (v DVec4) Normalized() (DVec4, bool)
- func (v DVec4) ProductComponents() float64
- func (v DVec4) Scale(s float64) DVec4
- func (v DVec4) Sub(o DVec4) DVec4
- func (v DVec4) SumComponents() float64
- func (v DVec4) SumSquaredComponents() float64
- func (v DVec4) Vec4() Vec4
- type Domain
- type Euler
- type Geo
- type HDRColor
- type Horizontal
- type IVec2
- type IVec3
- type IVec4
- type Interval
- func (i Interval) AlmostEqual(other Interval, tolerance float32) bool
- func (i Interval) Center() float32
- func (i Interval) Empty() bool
- func (i Interval) Expanded(value float32) Interval
- func (i Interval) Extent() float32
- func (i Interval) Intersection(other Interval) (Interval, bool)
- func (i Interval) Merge(other Interval) Interval
- func (i Interval) Valid() bool
- type LogPolar
- type Mat4
- func BillboardMat4(objectPosition, cameraPosition, cameraUp, cameraForward Vec3) (Mat4, bool)
- func ComposeMat4(scale Vec3, rotation Quat, translation Vec3) Mat4
- func ConstrainedBillboardMat4(objectPosition, cameraPosition, rotateAxis, cameraForward, objectForward Vec3) (Mat4, bool)
- func IdentityMat4() Mat4
- func LookAtMat4(position, target, up Vec3) (Mat4, bool)
- func Mat4FromAxisAngle(axis Vec3, angle float32) Mat4
- func Mat4FromQuat(q Quat) Mat4
- func Mat4FromRows(row0, row1, row2, row3 Vec4) Mat4
- func Mat4FromYawPitchRoll(yaw, pitch, roll float32) Mat4
- func NewMat4(m11, m12, m13, m14 float32, m21, m22, m23, m24 float32, ...) Mat4
- func OrthographicMat4(width, height, near, far float32) Mat4
- func OrthographicOffCenterMat4(left, right, bottom, top, near, far float32) Mat4
- func PerspectiveFOVMat4(fieldOfView, aspect, near, far float32) (Mat4, bool)
- func PerspectiveMat4(width, height, near, far float32) (Mat4, bool)
- func PerspectiveOffCenterMat4(left, right, bottom, top, near, far float32) (Mat4, bool)
- func ReflectionMat4(plane Plane) (Mat4, bool)
- func RotationAroundMat4(rotation Mat4, center Vec3) Mat4
- func RotationXMat4(angle float32) Mat4
- func RotationYMat4(angle float32) Mat4
- func RotationZMat4(angle float32) Mat4
- func ScaleAroundMat4(v, center Vec3) Mat4
- func ScaleMat4(v Vec3) Mat4
- func ShadowMat4(lightDirection Vec3, plane Plane) (Mat4, bool)
- func TranslationMat4(v Vec3) Mat4
- func WorldMat4(position, forward, up Vec3) (Mat4, bool)
- func (m Mat4) Add(o Mat4) Mat4
- func (m Mat4) AlmostEqual(other Mat4, tolerance float32) bool
- func (m Mat4) At(row, column int) float32
- func (m Mat4) Column(column int) Vec4
- func (m Mat4) Decompose() (scale Vec3, rotation Quat, translation Vec3, ok bool)
- func (m Mat4) Determinant() float32
- func (m Mat4) Inverse() (Mat4, bool)
- func (m Mat4) IsIdentity() bool
- func (m Mat4) IsReflection() bool
- func (m Mat4) Lerp(o Mat4, amount float32) Mat4
- func (m Mat4) Mul(o Mat4) Mat4
- func (m Mat4) Quat() Quat
- func (m Mat4) RayFromNormalizedScreen(screen Vec2) (Ray, bool)
- func (m Mat4) RotationDeterminant() float32
- func (m Mat4) Row(row int) Vec4
- func (m Mat4) Scale(s float32) Mat4
- func (m Mat4) Sub(o Mat4) Mat4
- func (m Mat4) TransformDirection(v Vec3) Vec3
- func (m Mat4) TransformNormal(normal Vec3) (Vec3, bool)
- func (m Mat4) TransformPoint(v Vec3) Vec3
- func (m Mat4) TransformVec4(v Vec4) Vec4
- func (m Mat4) Translation() Vec3
- func (m Mat4) Transposed() Mat4
- func (m Mat4) WithTranslation(translation Vec3) Mat4
- type Plane
- func (p Plane) AlmostEqual(other Plane, tolerance float32) bool
- func (p Plane) ClassifyPoint(point Vec3) float32
- func (p Plane) Dot(value Vec4) float32
- func (p Plane) DotCoordinate(point Vec3) float32
- func (p Plane) DotNormal(direction Vec3) float32
- func (p Plane) Normalized() (Plane, bool)
- func (p Plane) Project(point Vec3) (Vec3, bool)
- func (p Plane) Rotated(rotation Quat) Plane
- func (p Plane) Transformed(matrix Mat4) (Plane, bool)
- func (p Plane) Vec4() Vec4
- type PlaneIntersection
- type Polar
- type Quad2
- type Quad3
- type Quat
- func Concatenate(first, second Quat) Quat
- func IdentityQuat() Quat
- func LookAtQuat(position, target, up, localForward Vec3) (Quat, bool)
- func NewQuat(x, y, z, w float32) Quat
- func QuatFromAxisAngle(axis Vec3, angle float32) Quat
- func QuatFromEulerAngles(angles Vec3) Quat
- func QuatFromVector(vector Vec3, scalar float32) Quat
- func QuatFromYawPitchRoll(yaw, pitch, roll float32) Quat
- func QuatRotationX(angle float32) Quat
- func QuatRotationY(angle float32) Quat
- func QuatRotationZ(angle float32) Quat
- func RotationBetween(from, to, oppositeAxis Vec3) (Quat, bool)
- func (q Quat) Add(other Quat) Quat
- func (q Quat) AlmostEqual(other Quat, tolerance float32) bool
- func (q Quat) Conjugated() Quat
- func (q Quat) DQuat() DQuat
- func (q Quat) Div(other Quat) (Quat, bool)
- func (q Quat) Dot(other Quat) float32
- func (q Quat) EulerAngles() Vec3
- func (q Quat) Inverse() (Quat, bool)
- func (q Quat) Length() float64
- func (q Quat) LengthSquared() float32
- func (q Quat) Lerp(other Quat, amount float32) (Quat, bool)
- func (q Quat) Mul(other Quat) Quat
- func (q Quat) Negated() Quat
- func (q Quat) Normalized() (Quat, bool)
- func (q Quat) Rotate(v Vec3) Vec3
- func (q Quat) RotateToVec4(v Vec3) Vec4
- func (q Quat) RotateVec2(v Vec2) Vec2
- func (q Quat) RotateVec2ToVec4(v Vec2) Vec4
- func (q Quat) RotateVec4(v Vec4) Vec4
- func (q Quat) Scale(scalar float32) Quat
- func (q Quat) Slerp(other Quat, amount float32) Quat
- func (q Quat) Sub(other Quat) Quat
- func (q Quat) Vector() Vec3
- type RGB
- type RGBA
- type Ray
- func (r Ray) AlmostEqual(other Ray, tolerance float32) bool
- func (r Ray) IntersectBox(box Box3) (float32, bool)
- func (r Ray) IntersectPlane(plane Plane, tolerance float32) (float32, bool)
- func (r Ray) IntersectSphere(sphere Sphere) (float32, bool)
- func (r Ray) IntersectTriangle(triangle Triangle3, tolerance float32) (float32, bool)
- func (r Ray) PointAt(t float32) Vec3
- func (r Ray) Transformed(matrix Mat4) (Ray, bool)
- type Segment2
- type Segment3
- func (s Segment3) AlmostEqual(other Segment3, tolerance float32) bool
- func (s Segment3) Length() float64
- func (s Segment3) LengthSquared() float32
- func (s Segment3) Map(mapper func(Vec3) Vec3) Segment3
- func (s Segment3) Midpoint() Vec3
- func (s Segment3) PointAt(t float32) Vec3
- func (s Segment3) Ray() Ray
- func (s Segment3) Reversed() Segment3
- func (s Segment3) Transformed(matrix Mat4) Segment3
- func (s Segment3) Vector() Vec3
- func (s Segment3) WithLength(length float32) (Segment3, bool)
- type Sphere
- func (s Sphere) AlmostEqual(other Sphere, tolerance float32) bool
- func (s Sphere) Bounds() Box3
- func (s Sphere) ContainsBox(box Box3) Containment
- func (s Sphere) ContainsPoint(point Vec3) Containment
- func (s Sphere) ContainsSphere(other Sphere) Containment
- func (s Sphere) DistanceToPoint(point Vec3) float32
- func (s Sphere) DistanceToSphere(other Sphere) float32
- func (s Sphere) Intersects(other Sphere) bool
- func (s Sphere) IntersectsBox(box Box3) bool
- func (s Sphere) IntersectsPlane(plane Plane) PlaneIntersection
- func (s Sphere) Merge(other Sphere) (Sphere, bool)
- func (s Sphere) Transformed(matrix Mat4) (Sphere, bool)
- func (s Sphere) Translated(offset Vec3) Sphere
- func (s Sphere) Valid() bool
- type Spherical
- type Stats
- type Transform
- type Triangle2
- type Triangle3
- func (t Triangle3) AlmostEqual(other Triangle3, tolerance float32) bool
- func (t Triangle3) Area() float32
- func (t Triangle3) Bounds() Box3
- func (t Triangle3) Centroid() Vec3
- func (t Triangle3) Degenerate() bool
- func (t Triangle3) Edges() [3]Segment3
- func (t Triangle3) IsSliver(tolerance float32) bool
- func (t Triangle3) Map(mapper func(Vec3) Vec3) Triangle3
- func (t Triangle3) Normal() (Vec3, bool)
- func (t Triangle3) NormalDirection() Vec3
- func (t Triangle3) Perimeter() float64
- func (t Triangle3) Transformed(matrix Mat4) Triangle3
- type Vec2
- func (v Vec2) Add(other Vec2) Vec2
- func (v Vec2) AlmostEqual(other Vec2, tolerance float32) bool
- func (v Vec2) Cross(other Vec2) float32
- func (v Vec2) DVec2() DVec2
- func (v Vec2) Div(other Vec2) Vec2
- func (v Vec2) Dot(other Vec2) float32
- func (v Vec2) IsFinite() bool
- func (v Vec2) IsInf() bool
- func (v Vec2) IsNaN() bool
- func (v Vec2) Magnitude() float64
- func (v Vec2) MagnitudeSquared() float64
- func (v Vec2) Max(other Vec2) Vec2
- func (v Vec2) MaxComponent() float32
- func (v Vec2) Min(other Vec2) Vec2
- func (v Vec2) MinComponent() float32
- func (v Vec2) Mul(other Vec2) Vec2
- func (v Vec2) Negated() Vec2
- func (v Vec2) Normalized() (Vec2, bool)
- func (v Vec2) ProductComponents() float32
- func (v Vec2) Reflect(normal Vec2) (Vec2, bool)
- func (v Vec2) Scale(scalar float32) Vec2
- func (v Vec2) Sub(other Vec2) Vec2
- func (v Vec2) SumComponents() float32
- func (v Vec2) SumSquaredComponents() float32
- func (v Vec2) Vec3() Vec3
- func (v Vec2) Vec4() Vec4
- type Vec3
- func (v Vec3) Add(other Vec3) Vec3
- func (v Vec3) AlmostEqual(other Vec3, tolerance float32) bool
- func (v Vec3) Angle(other Vec3) (float32, bool)
- func (v Vec3) CatmullRom(value2, value3, value4 Vec3, amount float32) Vec3
- func (v Vec3) Cross(other Vec3) Vec3
- func (v Vec3) DVec3() DVec3
- func (v Vec3) Div(other Vec3) Vec3
- func (v Vec3) Dot(other Vec3) float32
- func (v Vec3) Hermite(tangent1, value2, tangent2 Vec3, amount float32) Vec3
- func (v Vec3) IsBackFace(lineOfSight Vec3) bool
- func (v Vec3) IsCollinear(other Vec3, tolerance float32) bool
- func (v Vec3) IsFinite() bool
- func (v Vec3) IsInf() bool
- func (v Vec3) IsNaN() bool
- func (v Vec3) IsPerpendicular(other Vec3, tolerance float32) bool
- func (v Vec3) Magnitude() float64
- func (v Vec3) MagnitudeSquared() float64
- func (v Vec3) Max(other Vec3) Vec3
- func (v Vec3) MaxComponent() float32
- func (v Vec3) Min(other Vec3) Vec3
- func (v Vec3) MinComponent() float32
- func (v Vec3) Mul(other Vec3) Vec3
- func (v Vec3) Negated() Vec3
- func (v Vec3) Normalized() (Vec3, bool)
- func (v Vec3) ProductComponents() float32
- func (v Vec3) Projection(other Vec3) (Vec3, bool)
- func (v Vec3) Reflect(normal Vec3) (Vec3, bool)
- func (v Vec3) Rejection(other Vec3) (Vec3, bool)
- func (v Vec3) Scale(scalar float32) Vec3
- func (v Vec3) SignedAngle(other, axis Vec3) (float32, bool)
- func (v Vec3) SmoothStep(value2 Vec3, amount float32) Vec3
- func (v Vec3) Sub(other Vec3) Vec3
- func (v Vec3) SumComponents() float32
- func (v Vec3) SumSquaredComponents() float32
- func (v Vec3) Vec4() Vec4
- func (v Vec3) XY() Vec2
- func (v Vec3) XZ() Vec2
- func (v Vec3) XZY() Vec3
- func (v Vec3) YXZ() Vec3
- func (v Vec3) YZ() Vec2
- func (v Vec3) YZX() Vec3
- func (v Vec3) ZXY() Vec3
- func (v Vec3) ZYX() Vec3
- type Vec4
- func (v Vec4) Add(o Vec4) Vec4
- func (v Vec4) AlmostEqual(o Vec4, tolerance float32) bool
- func (v Vec4) DVec4() DVec4
- func (v Vec4) Div(o Vec4) Vec4
- func (v Vec4) Dot(o Vec4) float32
- func (v Vec4) IsFinite() bool
- func (v Vec4) IsInf() bool
- func (v Vec4) IsNaN() bool
- func (v Vec4) Magnitude() float64
- func (v Vec4) MagnitudeSquared() float64
- func (v Vec4) Max(o Vec4) Vec4
- func (v Vec4) MaxComponent() float32
- func (v Vec4) Min(o Vec4) Vec4
- func (v Vec4) MinComponent() float32
- func (v Vec4) Mul(o Vec4) Vec4
- func (v Vec4) Negated() Vec4
- func (v Vec4) Normalized() (Vec4, bool)
- func (v Vec4) ProductComponents() float32
- func (v Vec4) Scale(s float32) Vec4
- func (v Vec4) Sub(o Vec4) Vec4
- func (v Vec4) SumComponents() float32
- func (v Vec4) SumSquaredComponents() float32
- func (v Vec4) Vec2() Vec2
- func (v Vec4) Vec3() Vec3
Examples ¶
Constants ¶
const ( Pi float32 = 3.1415927 HalfPi float32 = Pi / 2 TwoPi float32 = Pi * 2 Tolerance float32 = 0.0000001 Log10E float32 = 0.4342945 Log2E float32 = 1.442695 E float32 = 2.7182817 RadiansToDegrees float64 = 57.295779513082320876798154814105 DegreesToRadians float64 = 0.017453292519943295769236907684886 OneTenthOfADegree float64 = DegreesToRadians / 10 MillimetersToFeet float64 = 0.00328084 FeetToMillimeters float64 = 1 / MillimetersToFeet )
Mathematical constants preserve the precision and values of the upstream declarations.
Variables ¶
This section is empty.
Functions ¶
func CatmullRom ¶
CatmullRom interpolates between value2 and value3 using value1 and value4 as neighboring control values. Amount is not clamped.
func CombineHash ¶
CombineHash combines two source-compatible signed 32-bit hash values.
func Coplanar ¶
Coplanar reports whether four finite points have an absolute scalar triple product strictly less than tolerance. As in upstream, tolerance is absolute and scales with the cube of the coordinates. A degenerate point set is coplanar; a non-positive or non-finite tolerance returns false.
func IsNonZeroAndValid ¶
IsNonZeroAndValid reports whether value is finite and its absolute value is greater than tolerance.
func NearestPowerOfTwo ¶
NearestPowerOfTwo returns the power of two whose base-two logarithm is nearest to value's, matching the pinned upstream helper for positive inputs. Zero, negative values, and results that do not fit in int32 fail.
func Percentage ¶
Percentage returns numerator as a percentage of denominator.
func RandomFloat ¶
RandomFloat returns a deterministic value in the inclusive range [minimum, maximum] when minimum <= maximum.
func RandomUint ¶
RandomUint returns the unsigned representation of RandomInt.
func RandomUnitFloat ¶
RandomUnitFloat returns a deterministic value in [0, 1].
func SmoothStep ¶
SmoothStep smoothly interpolates from value1 to value2, clamping amount to [0, 1].
func Smoothstep ¶
Smoothstep evaluates x²(3-2x) without clamping x.
func StatsAverage ¶
StatsAverage returns the component-wise average. It fails when Count is not positive.
func StatsExtents ¶
func StatsExtents[T statsVector](stats Stats[T]) T
StatsExtents returns Max-Min component by component.
func StatsMiddle ¶
func StatsMiddle[T statsVector](stats Stats[T]) T
StatsMiddle returns the midpoint between Min and Max.
Types ¶
type AxisAngle ¶
AxisAngle is the upstream AxisAngle value. Its float64 precision matches the source despite the absence of a D prefix. Angle is in radians. A zero axis is safe to represent but does not define a rotation.
func NewAxisAngle ¶
NewAxisAngle constructs an AxisAngle without normalizing axis.
type Box2 ¶
type Box2 struct {
Min, Max Vec2
}
Box2 is the upstream AABox2D value. Construction preserves endpoint order; any Min component greater than its corresponding Max component represents an empty box. The zero value is a point box at the origin.
func Box2FromPoints ¶
Box2FromPoints returns the bounds of points, or false for an empty or non-finite input.
func (Box2) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
func (Box2) ContainsBox ¶
func (b Box2) ContainsBox(other Box2) Containment
func (Box2) ContainsPoint ¶
func (Box2) Intersects ¶
func (Box2) Translated ¶
type Box3 ¶
type Box3 struct {
Min, Max Vec3
}
Box3 is the upstream AABox value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.
func Box3FromPoints ¶
Box3FromPoints returns the bounds of points, or false for an empty or non-finite input.
Example ¶
package main
import (
"fmt"
"github.com/maloquacious/math3d"
)
func main() {
box, ok := math3d.Box3FromPoints([]math3d.Vec3{
math3d.V3(2, -1, 4),
math3d.V3(-3, 5, 1),
math3d.V3(0, 2, 8),
})
if !ok {
return
}
fmt.Printf("min: %.0f %.0f %.0f\n", box.Min.X, box.Min.Y, box.Min.Z)
fmt.Printf("max: %.0f %.0f %.0f\n", box.Max.X, box.Max.Y, box.Max.Z)
}
Output: min: -3 -1 1 max: 2 5 8
func (Box3) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
func (Box3) ContainsBox ¶
func (b Box3) ContainsBox(other Box3) Containment
func (Box3) ContainsPoint ¶
func (Box3) ContainsSphere ¶
func (b Box3) ContainsSphere(sphere Sphere) Containment
func (Box3) Intersects ¶
func (Box3) IntersectsPlane ¶
func (b Box3) IntersectsPlane(plane Plane) PlaneIntersection
IntersectsPlane classifies b against plane. Contact is intersecting.
func (Box3) IntersectsSphere ¶
func (Box3) Transformed ¶
Transformed returns the axis-aligned bounds of all transformed corners.
func (Box3) Translated ¶
type Box4 ¶
type Box4 struct {
Min, Max Vec4
}
Box4 is the upstream AABox4D value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.
func (Box4) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
type Complex ¶
type Complex struct {
Real, Imaginary float64
}
Complex is the upstream Complex value. It interoperates explicitly with Go's complex128 rather than shadowing the built-in conversion syntax.
func ComplexFrom128 ¶
func ComplexFrom128(value complex128) Complex
ComplexFrom128 converts a built-in complex value.
func NewComplex ¶
NewComplex constructs a Complex value.
func (Complex) AlmostEqual ¶
AlmostEqual reports whether corresponding components differ by less than tolerance. It follows normal IEEE behavior: NaN never compares equal.
func (Complex) Complex128 ¶
func (c Complex) Complex128() complex128
Complex128 converts c to a built-in complex value.
type Containment ¶
type Containment uint8
Containment describes the spatial relationship between two shapes.
const ( // ContainmentDisjoint is the zero value. ContainmentDisjoint Containment = iota ContainmentContains ContainmentIntersects )
type Cylindrical ¶
type Cylindrical struct {
Radius, Azimuth, Height float64
}
Cylindrical is the upstream CylindricalCoordinate value.
func NewCylindrical ¶
func NewCylindrical(radius, azimuth, height float64) Cylindrical
NewCylindrical constructs a Cylindrical value.
func (Cylindrical) AlmostEqual ¶
func (v Cylindrical) AlmostEqual(other Cylindrical, tolerance float64) bool
AlmostEqual compares corresponding components using a strict absolute tolerance.
type DBox2 ¶
type DBox2 struct {
Min, Max DVec2
}
DBox2 is the upstream DAABox2D value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.
func EmptyDBox2 ¶
func EmptyDBox2() DBox2
func (DBox2) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
type DBox3 ¶
type DBox3 struct {
Min, Max DVec3
}
DBox3 is the upstream DAABox value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.
func EmptyDBox3 ¶
func EmptyDBox3() DBox3
func (DBox3) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
func (DBox3) ContainsPoint ¶
type DBox4 ¶
type DBox4 struct {
Min, Max DVec4
}
DBox4 is the upstream DAABox4D value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.
func EmptyDBox4 ¶
func EmptyDBox4() DBox4
func (DBox4) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
type DInterval ¶
type DInterval struct {
Min, Max float64
}
DInterval stores inclusive float64 scalar bounds. Construction preserves bound order; Min > Max is the empty representation. The zero value is [0, 0].
func EmptyDInterval ¶
func EmptyDInterval() DInterval
EmptyDInterval returns the canonical empty float64 interval.
func NewDInterval ¶
NewDInterval constructs a DInterval without reordering its bounds.
func (DInterval) AlmostEqual ¶
AlmostEqual compares corresponding bounds using a strict absolute tolerance.
type DPlane ¶
DPlane is the float64 plane representation using the equation dot(Normal, point) + D = 0. Its zero value does not define a plane.
func DPlaneFromVec4 ¶
DPlaneFromVec4 constructs a DPlane from normal components followed by D.
func (DPlane) AlmostEqual ¶
AlmostEqual compares corresponding stored components.
type DQuat ¶
type DQuat struct {
X, Y, Z, W float64
}
DQuat is the upstream DQuaternion value. The zero value is not a valid rotation, and direct construction does not normalize the value.
func DQuatFromVector ¶
DQuatFromVector constructs a DQuat from its vector and scalar parts.
func IdentityDQuat ¶
func IdentityDQuat() DQuat
IdentityDQuat returns the float64 identity rotation.
func (DQuat) AlmostEqual ¶
AlmostEqual compares corresponding components using a strict absolute tolerance. It compares representations, so q and -q are not treated as equivalent rotations.
type DRay ¶
type DRay struct {
Origin, Direction DVec3
}
DRay is the float64 ray representation. Direction is not implicitly normalized. A zero direction does not define a geometric ray.
func (DRay) AlmostEqual ¶
AlmostEqual compares corresponding stored components.
type DSphere ¶
DSphere is the float64 sphere representation. NewDSphere preserves negative radii. The zero value is a radius-zero sphere at the origin.
func NewDSphere ¶
NewDSphere constructs a DSphere without validating radius.
func (DSphere) AlmostEqual ¶
AlmostEqual compares corresponding stored components.
func (DSphere) ContainsPoint ¶
func (s DSphere) ContainsPoint(point DVec3) Containment
func (DSphere) Intersects ¶
type DVec2 ¶
type DVec2 struct{ X, Y float64 }
DVec2 is the upstream DVector2 value.
func (DVec2) MagnitudeSquared ¶
func (DVec2) MaxComponent ¶
func (DVec2) MinComponent ¶
func (DVec2) Normalized ¶
func (DVec2) ProductComponents ¶
func (DVec2) SumComponents ¶
func (DVec2) SumSquaredComponents ¶
type DVec3 ¶
type DVec3 struct{ X, Y, Z float64 }
DVec3 is the upstream DVector3 value.
func (DVec3) MagnitudeSquared ¶
func (DVec3) MaxComponent ¶
func (DVec3) MinComponent ¶
func (DVec3) Normalized ¶
func (DVec3) ProductComponents ¶
func (DVec3) SumComponents ¶
func (DVec3) SumSquaredComponents ¶
type DVec4 ¶
type DVec4 struct{ X, Y, Z, W float64 }
DVec4 is the upstream DVector4 value.
func (DVec4) MagnitudeSquared ¶
func (DVec4) MaxComponent ¶
func (DVec4) MinComponent ¶
func (DVec4) Normalized ¶
func (DVec4) ProductComponents ¶
func (DVec4) SumComponents ¶
func (DVec4) SumSquaredComponents ¶
type Domain ¶
type Domain struct {
Lower, Upper float64
}
Domain is the upstream ValueDomain range.
type Euler ¶
type Euler struct {
Yaw, Pitch, Roll float32
}
Euler is the upstream Euler value. Its yaw, pitch, and roll angles are radians. The zero value represents no rotation; conversion conventions are defined with the quaternion algorithms.
type Geo ¶
type Geo struct {
Latitude, Longitude float64
}
Geo is the upstream GeoCoordinate value. Upstream does not specify whether latitude and longitude are expressed in degrees or radians.
type HDRColor ¶
type HDRColor struct {
R, G, B, A float32
}
HDRColor is the upstream ColorHDR value.
func NewHDRColor ¶
NewHDRColor constructs an HDRColor.
type Horizontal ¶
type Horizontal struct {
Azimuth, Inclination float64
}
Horizontal is the upstream HorizontalCoordinate value.
func HorizontalFromDVec2 ¶
func HorizontalFromDVec2(v DVec2) Horizontal
HorizontalFromDVec2 maps X to azimuth and Y to inclination.
func HorizontalFromVec2 ¶
func HorizontalFromVec2(v Vec2) Horizontal
HorizontalFromVec2 maps X to azimuth and Y to inclination.
func NewHorizontal ¶
func NewHorizontal(azimuth, inclination float64) Horizontal
NewHorizontal constructs a Horizontal value.
func (Horizontal) AlmostEqual ¶
func (v Horizontal) AlmostEqual(other Horizontal, tolerance float64) bool
AlmostEqual compares corresponding components using a strict absolute tolerance.
func (Horizontal) DVec2 ¶
func (v Horizontal) DVec2() DVec2
DVec2 converts azimuth and inclination to X and Y.
func (Horizontal) Vec2 ¶
func (v Horizontal) Vec2() Vec2
Vec2 converts azimuth and inclination to float32 X and Y.
type IVec2 ¶
type IVec2 struct {
X, Y int32
}
IVec2 is the upstream Int2 value. Its components retain C#'s 32-bit width.
type IVec3 ¶
type IVec3 struct {
X, Y, Z int32
}
IVec3 is the upstream Int3 value. Its components retain C#'s 32-bit width.
type IVec4 ¶
type IVec4 struct {
X, Y, Z, W int32
}
IVec4 is the upstream Int4 value. Its components retain C#'s 32-bit width.
type Interval ¶
type Interval struct {
Min, Max float32
}
Interval stores inclusive scalar bounds. Construction preserves bound order; Min > Max is the empty representation. The zero value is [0, 0].
func EmptyInterval ¶
func EmptyInterval() Interval
EmptyInterval returns the canonical empty float32 interval.
func NewInterval ¶
NewInterval constructs an Interval without reordering its bounds.
func (Interval) AlmostEqual ¶
AlmostEqual compares corresponding bounds using a strict absolute tolerance.
type LogPolar ¶
type LogPolar struct {
Rho, Azimuth float64
}
LogPolar is the upstream LogPolarCoordinate value.
func NewLogPolar ¶
NewLogPolar constructs a LogPolar value.
type Mat4 ¶
type Mat4 struct {
M11, M12, M13, M14 float32
M21, M22, M23, M24 float32
M31, M32, M33, M34 float32
M41, M42, M43, M44 float32
}
Mat4 is the upstream Matrix4x4 value. Fields are in row-major order, but transformations use row vectors: translation occupies M41, M42, and M43. The zero value is a zero matrix, not an identity transform.
func BillboardMat4 ¶
BillboardMat4 creates a spherical billboard at objectPosition.
func ComposeMat4 ¶
ComposeMat4 creates scale, then rotation, then translation for row vectors.
func ConstrainedBillboardMat4 ¶
func ConstrainedBillboardMat4(objectPosition, cameraPosition, rotateAxis, cameraForward, objectForward Vec3) (Mat4, bool)
ConstrainedBillboardMat4 creates a cylindrical billboard around a unit axis.
func LookAtMat4 ¶
LookAtMat4 creates a right-handed view matrix. It fails for degenerate directions.
func Mat4FromAxisAngle ¶
Mat4FromAxisAngle creates a rotation around a unit axis.
func Mat4FromQuat ¶
Mat4FromQuat creates the row-vector rotation matrix represented by q.
func Mat4FromRows ¶
Mat4FromRows constructs a Mat4 from four rows.
func Mat4FromYawPitchRoll ¶
Mat4FromYawPitchRoll creates roll (Z), then pitch (X), then yaw (Y).
func NewMat4 ¶
func NewMat4( m11, m12, m13, m14 float32, m21, m22, m23, m24 float32, m31, m32, m33, m34 float32, m41, m42, m43, m44 float32, ) Mat4
NewMat4 constructs a Mat4 from rows in row-major order.
func OrthographicMat4 ¶
func PerspectiveFOVMat4 ¶
PerspectiveFOVMat4 creates a right-handed perspective projection with depth [0,1].
func PerspectiveMat4 ¶
PerspectiveMat4 creates a perspective projection from near-plane dimensions.
func PerspectiveOffCenterMat4 ¶
PerspectiveOffCenterMat4 creates an asymmetric perspective projection.
func ReflectionMat4 ¶
ReflectionMat4 creates a reflection about dot(Normal, point)+D=0.
func RotationAroundMat4 ¶
func RotationXMat4 ¶
func RotationYMat4 ¶
func RotationZMat4 ¶
func ScaleAroundMat4 ¶
ScaleAroundMat4 creates a scale about center.
func ShadowMat4 ¶
ShadowMat4 projects onto plane along lightDirection.
func TranslationMat4 ¶
func (Mat4) AlmostEqual ¶
AlmostEqual compares corresponding elements using a strict absolute tolerance.
func (Mat4) At ¶
At returns the element at the zero-based row and column. It panics if either index is outside [0, 4).
func (Mat4) Decompose ¶
Decompose extracts scale, rotation, and translation from an affine SRT matrix.
func (Mat4) Determinant ¶
func (Mat4) Inverse ¶
Inverse returns the inverse matrix, or false when m is singular or non-finite.
func (Mat4) IsIdentity ¶
IsIdentity reports exact representation equality with the identity matrix.
func (Mat4) IsReflection ¶
func (Mat4) Mul ¶
Mul multiplies two matrices. With row vectors, m is applied before o.
Example ¶
package main
import (
"fmt"
"github.com/maloquacious/math3d"
)
func main() {
// With row vectors, multiplication order is application order: scale,
// then rotate, then translate.
matrix := math3d.ScaleMat4(math3d.V3(2, 2, 2)).
Mul(math3d.RotationZMat4(math3d.HalfPi)).
Mul(math3d.TranslationMat4(math3d.V3(5, -1, 0)))
point := matrix.TransformPoint(math3d.V3(1, 0, 0))
fmt.Printf("%.0f %.0f %.0f\n", point.X, point.Y, point.Z)
}
Output: 5 1 0
func (Mat4) RayFromNormalizedScreen ¶
RayFromNormalizedScreen constructs a ray by unprojecting screen through m. Screen uses normalized coordinates: (0, 0) is the top-left, (1, 1) is the bottom-right, and values outside that range extrapolate beyond the viewport. Clip-space depth is [0, 1].
The matrix is not an inverse: this method inverts it. A projection matrix produces a view-space ray; a row-vector view-projection matrix view.Mul(projection) produces a world-space ray. The ray starts on the near clip plane, not at the camera, and its direction is normalized. It fails for non-finite screen coordinates, a singular matrix, an invalid homogeneous divide, or a degenerate direction.
Example ¶
package main
import (
"fmt"
"github.com/maloquacious/math3d"
)
func main() {
projection, ok := math3d.PerspectiveFOVMat4(math3d.HalfPi, 1, 1, 10)
if !ok {
return
}
// Screen coordinates are normalized with a top-left origin. A projection
// matrix alone produces a view-space ray starting on the near clip plane.
ray, ok := projection.RayFromNormalizedScreen(math3d.V2(0.5, 0.5))
if !ok {
return
}
fmt.Printf("origin: %.0f %.0f %.0f\n", ray.Origin.X, ray.Origin.Y, ray.Origin.Z)
fmt.Printf("direction: %.0f %.0f %.0f\n", ray.Direction.X, ray.Direction.Y, ray.Direction.Z)
}
Output: origin: 0 0 -1 direction: 0 0 -1
func (Mat4) RotationDeterminant ¶
func (Mat4) TransformDirection ¶
TransformDirection transforms a direction with homogeneous W=0.
func (Mat4) TransformNormal ¶
TransformNormal transforms a surface normal using the inverse transpose of m, as required by the row-vector convention. Translation has no effect, and the result is not normalized. It fails for a non-finite normal, a singular or non-finite matrix, or a non-finite result.
func (Mat4) TransformPoint ¶
TransformPoint transforms a position with homogeneous W=1.
func (Mat4) TransformVec4 ¶
TransformVec4 applies m to the row vector v.
func (Mat4) Translation ¶
Translation returns the row-vector translation stored in m.
func (Mat4) Transposed ¶
func (Mat4) WithTranslation ¶
WithTranslation returns m with its row-vector translation replaced.
type Plane ¶
Plane represents dot(Normal, point) + D = 0. The normal is not implicitly normalized. The zero value does not define a geometric plane.
func PlaneFromNormalPoint ¶
PlaneFromNormalPoint constructs a normalized plane containing point. It fails when normal cannot be normalized. Unlike the inconsistent upstream factory, D follows dot(Normal, point)+D=0.
func PlaneFromPoints ¶
PlaneFromPoints constructs a normalized plane through a, b, and c. Vertex order determines the normal direction. It fails when the points do not define a finite, non-degenerate plane.
func PlaneFromVec4 ¶
PlaneFromVec4 constructs a Plane from normal components followed by D.
func (Plane) AlmostEqual ¶
AlmostEqual compares corresponding stored components.
func (Plane) ClassifyPoint ¶
ClassifyPoint returns the signed plane equation value for point. Negative values are behind the normal, positive values are in front, and zero is on the plane. It is a signed distance only when Normal is unit length.
func (Plane) DotCoordinate ¶
DotCoordinate evaluates dot(Normal, point)+D.
func (Plane) Normalized ¶
Normalized returns an equivalent plane with a unit normal. It fails for a zero or non-finite normal.
func (Plane) Project ¶
Project projects point orthogonally onto p. It supports non-unit normals and fails for a zero or non-finite normal or a non-finite result.
func (Plane) Rotated ¶
Rotated rotates p's normal and leaves D unchanged. Rotation must be a unit quaternion.
func (Plane) Transformed ¶
Transformed applies matrix's inverse transpose to p. It fails when matrix is singular or when the resulting plane has non-finite components.
type PlaneIntersection ¶
type PlaneIntersection uint8
PlaneIntersection describes which side of a plane a shape occupies.
const ( // PlaneIntersectionFront is the zero value. PlaneIntersectionFront PlaneIntersection = iota PlaneIntersectionBack PlaneIntersectionIntersecting )
type Polar ¶
type Polar struct {
Radius, Azimuth float64
}
Polar is the upstream PolarCoordinate value.
type Quad2 ¶
type Quad2 struct{ A, B, C, D Vec2 }
Quad2 is the upstream Quad2D value. Vertices retain caller-supplied order. The zero value is degenerate.
type Quad3 ¶
type Quad3 struct{ A, B, C, D Vec3 }
Quad3 is the upstream Quad value. Vertices retain caller-supplied order. The zero value is degenerate.
func (Quad3) AlmostEqual ¶
AlmostEqual compares corresponding vertices without disregarding winding.
func (Quad3) Transformed ¶
Transformed applies matrix to every vertex as a point.
type Quat ¶
type Quat struct {
X, Y, Z, W float32
}
Quat is the upstream Quaternion value. X, Y, and Z are its vector part and W is its scalar part. The zero value is not a valid rotation.
Quat values need not be normalized. Operations that require a unit quaternion are introduced with the quaternion algorithms.
func Concatenate ¶
Concatenate returns the rotation first followed by second.
func LookAtQuat ¶
LookAtQuat returns a rotation that turns the caller-supplied localForward direction toward target-position while using up to define the horizontal plane. Inputs need not be normalized, but localForward must be perpendicular to up. It fails for non-finite or zero inputs, coincident position and target, direction/up parallelism, or localForward/up non-perpendicularity.
func QuatFromAxisAngle ¶
QuatFromAxisAngle creates a quaternion for an angle in radians. Axis must be unit length.
func QuatFromEulerAngles ¶
QuatFromEulerAngles creates an intrinsic X, then Y, then Z rotation.
func QuatFromVector ¶
QuatFromVector constructs a Quat from its vector and scalar parts.
func QuatFromYawPitchRoll ¶
QuatFromYawPitchRoll creates roll (Z), then pitch (X), then yaw (Y).
func QuatRotationX ¶
func QuatRotationY ¶
func QuatRotationZ ¶
func RotationBetween ¶
RotationBetween returns a rotation from one unit vector to another. For opposite vectors, oppositeAxis must be unit and perpendicular to from.
func (Quat) AlmostEqual ¶
AlmostEqual compares corresponding components using a strict absolute tolerance. It compares representations, so q and -q are not treated as equivalent rotations.
func (Quat) Conjugated ¶
func (Quat) EulerAngles ¶
EulerAngles returns intrinsic X, Y, Z angles in radians.
func (Quat) Inverse ¶
Inverse returns the multiplicative inverse. It fails for zero and non-finite norms.
func (Quat) LengthSquared ¶
func (Quat) Lerp ¶
Lerp linearly interpolates along the shortest rotational arc and normalizes the result.
func (Quat) Mul ¶
Mul returns the Hamilton product q·other. Quaternion multiplication applies other first and q second; use Concatenate for explicitly ordered rotations.
func (Quat) Normalized ¶
Normalized returns a unit quaternion. It fails for zero and non-finite lengths.
func (Quat) RotateToVec4 ¶
RotateToVec4 rotates v and returns homogeneous coordinates with W set to 1. q must be unit.
func (Quat) RotateVec2 ¶
RotateVec2 rotates v embedded as (X, Y, 0) and returns the resulting X and Y components. Rotation out of the XY plane is discarded. q must be unit.
func (Quat) RotateVec2ToVec4 ¶
RotateVec2ToVec4 rotates v embedded as (X, Y, 0) and returns homogeneous coordinates with W set to 1. q must be unit.
func (Quat) RotateVec4 ¶
RotateVec4 rotates the X, Y, and Z components of v and preserves W exactly. q must be unit.
type Ray ¶
type Ray struct {
Origin, Direction Vec3
}
Ray stores an origin and direction. Direction is not implicitly normalized, and its length scales the ray parameter. A zero direction is representable but does not define a geometric ray.
func (Ray) AlmostEqual ¶
AlmostEqual compares corresponding stored components.
func (Ray) IntersectBox ¶
IntersectBox returns the first non-negative parameter at which r meets box.
func (Ray) IntersectPlane ¶
IntersectPlane returns the non-negative parameter at which r meets plane. Parallel rays, including rays contained in the plane, do not have one distinguished intersection and return false.
func (Ray) IntersectSphere ¶
IntersectSphere returns the first non-negative ray parameter at which r meets sphere. It supports directions of any non-zero finite length.
Example ¶
package main
import (
"fmt"
"github.com/maloquacious/math3d"
)
func main() {
// Direction is not unit length, so t is a ray parameter rather than a
// Euclidean distance.
ray := math3d.NewRay(math3d.V3(-5, 0, 0), math3d.V3(2, 0, 0))
t, ok := ray.IntersectSphere(math3d.NewSphere(math3d.V3(0, 0, 0), 1))
if !ok {
return
}
point := ray.PointAt(t)
fmt.Printf("t: %.0f\n", t)
fmt.Printf("point: %.0f %.0f %.0f\n", point.X, point.Y, point.Z)
}
Output: t: 2 point: -1 0 0
func (Ray) IntersectTriangle ¶
IntersectTriangle returns the positive ray parameter at which r meets triangle. Both windings are accepted, and edges and vertices are included. Hits at the ray origin or within tolerance of it are excluded.
type Segment2 ¶
type Segment2 struct{ A, B Vec2 }
Segment2 is the upstream Line2D value. Its zero value is degenerate.
func NewSegment2 ¶
NewSegment2 constructs a Segment2 from its endpoints.
func (Segment2) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
func (Segment2) ContainsOnLine ¶
ContainsOnLine reports whether point is within tolerance of the infinite supporting line. It does not restrict the point to the segment's bounds.
func (Segment2) Intersects ¶
Intersects reports whether two closed 2D segments touch or cross. Collinear overlap and shared endpoints count as intersections. Tolerance is an absolute cross-product tolerance, matching the upstream operation.
type Segment3 ¶
type Segment3 struct{ A, B Vec3 }
Segment3 is the upstream Line value, represented by endpoints A and B. Its zero value is a degenerate segment at the origin.
func NewSegment3 ¶
NewSegment3 constructs a Segment3 from its endpoints.
func (Segment3) AlmostEqual ¶
AlmostEqual compares corresponding endpoints.
func (Segment3) LengthSquared ¶
LengthSquared returns the squared distance between the endpoints.
func (Segment3) PointAt ¶
PointAt linearly maps parameter t onto the segment's supporting line. Values outside [0, 1] extrapolate beyond the endpoints.
func (Segment3) Transformed ¶
Transformed applies matrix to both endpoints as points.
type Sphere ¶
Sphere stores a center and radius. NewSphere preserves any radius, including a negative one; algorithms that require a non-negative finite radius state that precondition. The zero value is a radius-zero sphere at the origin.
func (Sphere) AlmostEqual ¶
AlmostEqual compares corresponding stored components.
func (Sphere) ContainsBox ¶
func (s Sphere) ContainsBox(box Box3) Containment
func (Sphere) ContainsPoint ¶
func (s Sphere) ContainsPoint(point Vec3) Containment
func (Sphere) ContainsSphere ¶
func (s Sphere) ContainsSphere(other Sphere) Containment
func (Sphere) DistanceToPoint ¶
func (Sphere) DistanceToSphere ¶
func (Sphere) Intersects ¶
func (Sphere) IntersectsBox ¶
func (Sphere) IntersectsPlane ¶
func (s Sphere) IntersectsPlane(plane Plane) PlaneIntersection
func (Sphere) Merge ¶
Merge returns the smallest sphere on the line between the centers that encloses both spheres. Both inputs must be valid.
func (Sphere) Transformed ¶
Transformed returns a sphere enclosing s after an affine transform. Its radius is scaled by the largest singular value of the linear matrix, which is exact for rotation and non-uniform scale and conservative for shear.
func (Sphere) Translated ¶
type Spherical ¶
type Spherical struct {
Radius, Azimuth, Inclination float64
}
Spherical is the upstream SphericalCoordinate value.
func NewSpherical ¶
NewSpherical constructs a Spherical value.
type Stats ¶
Stats contains one-pass summary statistics for a sequence of values. Count is zero for an empty sequence, in which case Min, Max, and Sum are their zero values.
type Transform ¶
Transform is a rigid transform represented by a position and orientation. It has no scale. Its zero value has a zero quaternion and is therefore not a valid rigid transform; use IdentityTransform when identity is required.
Example ¶
package main
import (
"fmt"
"github.com/maloquacious/math3d"
)
func main() {
transform := math3d.NewTransform(
math3d.V3(10, 0, 0),
math3d.QuatRotationZ(math3d.HalfPi),
)
point := transform.TransformPoint(math3d.V3(1, 0, 0))
fmt.Printf("%.0f %.0f %.0f\n", point.X, point.Y, point.Z)
}
Output: 10 1 0
func IdentityTransform ¶
func IdentityTransform() Transform
IdentityTransform returns the identity rigid transform.
func NewTransform ¶
NewTransform constructs a Transform without normalizing orientation.
func (Transform) AlmostEqual ¶
AlmostEqual compares the stored representation, not geometric equivalence.
func (Transform) Mat4 ¶
Mat4 returns the row-vector matrix that rotates by Orientation and then translates by Position. Orientation must be a unit quaternion for the result to be a rigid transform.
func (Transform) TransformDirection ¶
TransformDirection rotates direction by Orientation without translating it. Orientation must be a unit quaternion.
func (Transform) TransformPoint ¶
TransformPoint rotates point by Orientation and then adds Position. Orientation must be a unit quaternion.
type Triangle2 ¶
type Triangle2 struct{ A, B, C Vec2 }
Triangle2 is the upstream Triangle2D value. Vertices retain caller-supplied order. The zero value is degenerate.
func NewTriangle2 ¶
NewTriangle2 constructs a Triangle2 in A, B, C order.
func (Triangle2) AlmostEqual ¶
AlmostEqual compares corresponding vertices without disregarding winding.
func (Triangle2) Contains ¶
Contains reports whether point lies in the strict interior. Points on an edge are excluded, matching the upstream operation.
func (Triangle2) SignedArea ¶
SignedArea returns the oriented area. Positive values indicate counter-clockwise winding.
type Triangle3 ¶
type Triangle3 struct{ A, B, C Vec3 }
Triangle3 is the upstream Triangle value. Vertices retain caller-supplied order. The zero value is degenerate.
func NewTriangle3 ¶
NewTriangle3 constructs a Triangle3 in A, B, C order.
func (Triangle3) AlmostEqual ¶
AlmostEqual compares corresponding vertices without disregarding winding.
func (Triangle3) Degenerate ¶
Degenerate reports whether the vertices are non-finite or have exactly zero geometric area. Use IsSliver when a non-zero minimum edge length is needed.
func (Triangle3) IsSliver ¶
IsSliver reports whether any edge length is at most tolerance. A negative or non-finite tolerance, or non-finite vertices, is treated as invalid.
func (Triangle3) Normal ¶
Normal returns the unit surface normal. It fails for a non-finite or zero-area triangle.
func (Triangle3) NormalDirection ¶
NormalDirection returns the winding-dependent, unnormalized surface normal.
func (Triangle3) Transformed ¶
Transformed applies matrix to every vertex as a point.
type Vec2 ¶
type Vec2 struct {
X, Y float32
}
Vec2 is the upstream Vector2 value. Its components use source-compatible float32 precision.
func RandomVec2 ¶
RandomVec2 returns a vector whose components use consecutive derived indices.
func (Vec2) MagnitudeSquared ¶
func (Vec2) MaxComponent ¶
func (Vec2) MinComponent ¶
func (Vec2) Normalized ¶
Normalized returns a unit vector. It fails when the magnitude is zero or non-finite, including when its float32 calculation overflows.
func (Vec2) ProductComponents ¶
func (Vec2) Reflect ¶
Reflect returns v reflected around normal. Normal need not be unit length. It fails when v or normal is non-finite, normal has zero or overflowed squared magnitude, or the result is non-finite.
func (Vec2) SumComponents ¶
func (Vec2) SumSquaredComponents ¶
type Vec3 ¶
type Vec3 struct {
X, Y, Z float32
}
Vec3 is the upstream Vector3 value. Its components use source-compatible float32 precision.
Example ¶
package main
import (
"fmt"
"github.com/maloquacious/math3d"
)
func main() {
direction, ok := math3d.V3(3, 0, 4).Normalized()
if !ok {
return
}
fmt.Printf("unit: %.1f %.1f %.1f\n", direction.X, direction.Y, direction.Z)
fmt.Printf("dot: %.1f\n", direction.Dot(math3d.V3(1, 2, 3)))
}
Output: unit: 0.6 0.0 0.8 dot: 3.0
func RandomVec3 ¶
RandomVec3 returns a vector whose components use consecutive derived indices.
func (Vec3) Angle ¶
Angle returns the smaller angle between v and other in radians. It fails when either vector is zero or non-finite, an intermediate overflows, or no finite angle can be calculated.
func (Vec3) CatmullRom ¶
CatmullRom interpolates between value2 and value3 using v and value4 as the neighboring control values. Amount is not clamped.
func (Vec3) Hermite ¶
Hermite interpolates from v with tangent1 to value2 with tangent2. Amount is not clamped.
func (Vec3) IsBackFace ¶
IsBackFace reports whether normal faces away from lineOfSight, represented by a strict negative dot product. The vectors need not be normalized; zero and non-finite inputs return false.
func (Vec3) IsCollinear ¶
IsCollinear reports whether the smaller angle between the vectors, ignoring direction, is at most tolerance radians. Unlike upstream's defective signed-angle test, it works in three dimensions and treats opposite vectors as collinear. Zero, non-finite, and overflowed vectors return false.
func (Vec3) IsPerpendicular ¶
IsPerpendicular reports whether both vectors are finite and nonzero and the absolute value of their dot product is strictly less than tolerance. Like upstream, tolerance is absolute and therefore depends on vector scale.
func (Vec3) MagnitudeSquared ¶
func (Vec3) MaxComponent ¶
func (Vec3) MinComponent ¶
func (Vec3) Normalized ¶
func (Vec3) ProductComponents ¶
func (Vec3) Projection ¶
Projection returns the projection of v onto other. It fails when either vector is non-finite, other has zero or overflowed squared magnitude, or the result is non-finite.
func (Vec3) Reflect ¶
Reflect returns v reflected around normal. Normal need not be unit length. It has the same failure conditions as Projection.
func (Vec3) Rejection ¶
Rejection returns the component of v perpendicular to other. It has the same failure conditions as Projection.
func (Vec3) SignedAngle ¶
SignedAngle returns the smaller angle from v to other in radians, with its sign determined by axis·(v×other). It fails when Angle fails, axis is zero or non-finite, or a nonzero angle has no orientation around axis.
func (Vec3) SmoothStep ¶
SmoothStep smoothly interpolates from v to value2, clamping amount to [0, 1].
func (Vec3) SumComponents ¶
func (Vec3) SumSquaredComponents ¶
type Vec4 ¶
type Vec4 struct {
X, Y, Z, W float32
}
Vec4 is the upstream Vector4 value. Its components use source-compatible float32 precision.
func RandomVec4 ¶
RandomVec4 returns a vector whose components use consecutive derived indices.