geo

package
v0.0.0-...-9c08f4d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2017 License: BSD-3-Clause Imports: 6 Imported by: 0

README

geo

package geo is a collection of geometric utility function. It's based on Christer Ericson's Real time collision detection (ISBN: 978-1-55860-732-3) and https://www.geometrictools.com/. As of today everything is in the same folder, the API is absolutelly not stable, if you need this code, either copy it or vendor it. Don't use the latest version just yet

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Barycentric

func Barycentric(a, b, c, p *glm.Vec3) (u, v, w float32)

Barycentric returns the barycentric coordinates for a point in a triangle. The barycentric coordinates cna be used for interpolation of {a, b, c} to point p (such as normals, texture coordinates, colors, etc)

func BarycentricWithCache

func BarycentricWithCache(b *BarycentricCache, p *glm.Vec3) (u, v, w float32)

BarycentricWithCache returns the barycentric coordinates for a point in a triangle.

func ClosestPointAABBPoint

func ClosestPointAABBPoint(a *AABB, p *glm.Vec3) glm.Vec3

ClosestPointAABBPoint returns the point in or on the AABB closest to p.

func ClosestPointLineTriangle

func ClosestPointLineTriangle(p, q, a, b, c *glm.Vec3) (u, v glm.Vec3)

ClosestPointLineTriangle returns the pair of points that are the closest from the line and the triangle.

func ClosestPointOBBPoint

func ClosestPointOBBPoint(o *OBB, p *glm.Vec3) glm.Vec3

ClosestPointOBBPoint returns the point in or on the OBB closest to p

func ClosestPointRect

func ClosestPointRect(p, a, b, c *glm.Vec3) glm.Vec3

ClosestPointRect is a shortcut for Rect3.ClosestPoint where the rectangle is defined by the span of [ab, ac].

func ClosestPointRectPoint

func ClosestPointRectPoint(r *Rect, p *glm.Vec3) glm.Vec3

ClosestPointRectPoint returns the point on the rectangle closest to p

func ClosestPointSegmentPoint

func ClosestPointSegmentPoint(a, b, c *glm.Vec3) (t float32, point glm.Vec3)

ClosestPointSegmentPoint returns the point on ab closest to c. Also returns t for the position of d, d(t) = a + t*(b - a)

func ClosestPointSegmentSegment

func ClosestPointSegmentSegment(p1, q1, p2, q2 *glm.Vec3) (s, t, u float32, c1, c2 glm.Vec3)

ClosestPointSegmentSegment computes points C₁ and C₂ of S₁(s) = p₁ + s * (q₁-p₁) and S₂(t) = p₂ + t * (q₂-p₂), returning s, t, and the squared distance u between S₁(s) and S₂(t).

func ClosestPointTetrahedronPoint

func ClosestPointTetrahedronPoint(p, a, b, c, d *glm.Vec3) glm.Vec3

ClosestPointTetrahedronPoint returns the closes point in or on tetrahedron abcd.

func ClosestPointTrianglePoint

func ClosestPointTrianglePoint(p, a, b, c *glm.Vec3) glm.Vec3

ClosestPointTrianglePoint returns the point on the triangle abc that is closest to p

func ClosestPointTriangleTriangle

func ClosestPointTriangleTriangle(a, b, c, d, e, f *glm.Vec3) (u, v glm.Vec3)

ClosestPointTriangleTriangle returns the pair of points that are the closest from the triangle pair.

func CovarianceMatrix

func CovarianceMatrix(cov *glm.Mat3, points []glm.Vec3)

CovarianceMatrix computes the covariance matrix of the given set of points.

func DOP8FromPoints

func DOP8FromPoints(d *DOP8, points []glm.Vec3)

DOP8FromPoints recomputes the 8-DOP from the given points in world space.

func DistToTriangle

func DistToTriangle(p, a, b, c *glm.Vec3) float32

DistToTriangle returns the distance of p to triangle {a b c}, CCW order

func DistanceToPlane

func DistanceToPlane(p *Plane, v *glm.Vec3) float32

DistanceToPlane returns the distance of v to plane p.

func ExtremePointsAlongDirection

func ExtremePointsAlongDirection(direction *glm.Vec3, points []glm.Vec3) (imin int, imax int)

ExtremePointsAlongDirection returns indices imin and imax into points of the least and most, respectively, distant points along the direction dir.

func InsideSpherePlane

func InsideSpherePlane(s *Sphere, p *Plane) bool

InsideSpherePlane returns true if s is completely inside plane p.

func IntersectRayAABB

func IntersectRayAABB(p, d *glm.Vec3, a *AABB) (t float32, q glm.Vec3, overlap bool)

IntersectRayAABB intersect ray R(t) = p + t*d against AABB a. When intersecting, return intersection distance t and point q of intersection.

func IntersectRaySphere

func IntersectRaySphere(p, d *glm.Vec3, s *Sphere) (t float32, q glm.Vec3, overlap bool)

IntersectRaySphere intersects ray r = p + td, |d| = 1, with sphere s and, if intersecting, // returns t value of intersection and intersection point q

func IntersectSegmentCylinder

func IntersectSegmentCylinder(sa, sb, p, q *glm.Vec3, r float32) (float32, bool)

IntersectSegmentCylinder intersects segment S(t)=sa+t(sb-sa), 0<=t<=1 against cylinder specified by p, q and r

func IntersectSegmentPlane

func IntersectSegmentPlane(a, b *glm.Vec3, p *Plane) (t float32, q glm.Vec3, overlap bool)

IntersectSegmentPlane returns how far in the segment, the point in world coordinates and true if the segment and the plane intersect

func IntersectSegmentQuad

func IntersectSegmentQuad(p, q, a, b, c, d *glm.Vec3) (glm.Vec3, bool)

IntersectSegmentQuad is given line pq and ccw quadrilateral abcd, return whether the line pierces the triangle. If so, also return the point r of intersection.

func IntersectSegmentTriangle

func IntersectSegmentTriangle(p, q, a, b, c *glm.Vec3) (u, v, w float32, overlap bool)

IntersectSegmentTriangle is given line pq and ccw triangle abc and return whether line pierces triangle. If so, also return the barycentric coordinates (u,v,w) of the intersection point.

func IntersectSegmentTriangle2

func IntersectSegmentTriangle2(p, q, a, b, c *glm.Vec3) (u, v, w, t float32, overlap bool)

IntersectSegmentTriangle2 is given segment pq and triangle abc and returns whether segment intersects triangle and if so, also returns the barycentric coordinates (u,v,w) of the intersection point.

func IsConvexQuad

func IsConvexQuad(a, b, c, d *glm.Vec3) bool

IsConvexQuad returns true if the qualidrateral is convex.

func IsPointInTriangle

func IsPointInTriangle(p, a, b, c *glm.Vec3) bool

IsPointInTriangle returns true if the point p projected on triangle {a,b,c} is inside triangle {a,b,c}

func Jacobi

func Jacobi(a, v *glm.Mat3)

Jacobi computes the eigenvectors and eigenvalues of the symmetric matrix A using the classic Jacobi method of iteratively updating A as A = J∧T * A * J, where J = J(p, q, theta) is the Jacobi rotation matrix.

On exit, v will contain the eigenvectors, and the diagonal elements of a are the corresponding eigenvalues.

See Golub, Van Loan, Matrix Computations, 3rd ed, p428

func MinimumAreaRectangle

func MinimumAreaRectangle(points []glm.Vec2) (minArea float32, center glm.Vec2, orientation [2]glm.Vec2)

MinimumAreaRectangle returns the center point and axis orientation of the minimum area rectangle in the xy plane.

func PointOutsidePlane

func PointOutsidePlane(p, a, b, c *glm.Vec3) bool

PointOutsidePlane returns true if p is outside or on triangle abc CCW.

func PointsOnOppositeSideOfPlane

func PointsOnOppositeSideOfPlane(p1, a, b, c, p2 *glm.Vec3) bool

PointsOnOppositeSideOfPlane returns true if point p is opposite of d, such that it doesn't matter if abc is CW or CCW

func Quickhull

func Quickhull(points []glm.Vec3)

Quickhull returns the convex hull of the given points.

func SqDistAABBPoint

func SqDistAABBPoint(a *AABB, p *glm.Vec3) float32

SqDistAABBPoint returns the square distance of p to the AABB.

func SqDistOBBPoint

func SqDistOBBPoint(o *OBB, p *glm.Vec3) float32

SqDistOBBPoint returns the square distance of p to the OBB.

func SqDistPointSegment

func SqDistPointSegment(a, b, c *glm.Vec3) float32

SqDistPointSegment returns the squared distance between point c and segment ab

func SqDistRectPoint

func SqDistRectPoint(r *Rect, p *glm.Vec3) float32

SqDistRectPoint returns the square distance of p to the rectangle.

func SymSchur2

func SymSchur2(a *glm.Mat3, p, q int) (c, s float32)

SymSchur2 aka: 2-by-2 Symmetric Schur decomposition. Given an n-by-n symmetric matrix and indices p, q such that 1 <= p < q <= n, computes a sine-cosine pair (s, c) that will serve to form a Jacobi rotation matrix.

See Golub, Van Loan, Matrix Computations, 3rd ed, p428

func TestAABBAABB

func TestAABBAABB(a, b *AABB) bool

TestAABBAABB returns true if these AABB overlap.

func TestAABBPlane

func TestAABBPlane(b *AABB, p *Plane) bool

TestAABBPlane tests if AABB b intersects plane p.

func TestCapsuleCapsule

func TestCapsuleCapsule(a, b *Capsule) bool

TestCapsuleCapsule returns true if these Capsules overlap.

func TestCapsuleSphere

func TestCapsuleSphere(c *Capsule, s *Sphere) bool

TestCapsuleSphere returns true if the capsule and the sphere overlap.

func TestDOP8DOP8

func TestDOP8DOP8(a, b *DOP8) bool

TestDOP8DOP8 returns true if the 8-DOP intersect.

func TestOBBOBB

func TestOBBOBB(a, b *OBB) bool

TestOBBOBB returns true if these OBB overlap.

func TestOBBPlane

func TestOBBPlane(b *OBB, p *Plane) bool

TestOBBPlane returns true if b and p intersect.

func TestRaySphere

func TestRaySphere(p, d *glm.Vec3, s *Sphere) bool

TestRaySphere tests if ray r = p + td intersects sphere s

func TestSegmentAABB

func TestSegmentAABB(p0, p1 *glm.Vec3, b *AABB) bool

TestSegmentAABB tests if segment specified by points p0 and p1 intersects AABB b.

func TestSphereAABB

func TestSphereAABB(s *Sphere, b *AABB) bool

TestSphereAABB returns true if sphere s intersects AABB b

func TestSphereHalfspace

func TestSphereHalfspace(s *Sphere, p *Plane) bool

TestSphereHalfspace returns true if s is touching or inside halfspace p.

func TestSphereOBB

func TestSphereOBB(s *Sphere, b *OBB) bool

TestSphereOBB returns true if sphere s intersects OBB b, false otherwise. The point p on the OBB closest to the sphere center is also returned

func TestSpherePlane

func TestSpherePlane(s *Sphere, p *Plane) bool

TestSpherePlane returns true if s and p intersect. The plane normal must be normalized.

func TestSphereSphere

func TestSphereSphere(a, b *Sphere) bool

TestSphereSphere return true if the spheres overlap.

func TestSphereTriangle

func TestSphereTriangle(s *Sphere, a, b, c *glm.Vec3) bool

TestSphereTriangle returns true if sphere s intersects triangle ABC, false otherwise. The point p on abc closest to the sphere center is also returned.

func TestTriangleAABB

func TestTriangleAABB(v0, v1, v2 *glm.Vec3, b *AABB) bool

TestTriangleAABB returns true if [v0 v1 v2] intersects b

func TriangleAreaFromLengths

func TriangleAreaFromLengths(a, b, c float32) float32

TriangleAreaFromLengths returns the area of a triangle defined by the given lengths. Returns NaN if the triangle does not exist.

func UpdateAABB

func UpdateAABB(base, fill *AABB, t *glm.Mat3x4)

UpdateAABB computes an enclosing AABB base transformed by t and puts the result in fill. base and fill must not be the same.

func Variance

func Variance(s []float32) float32

Variance computes the variance of a float slice.

Types

type AABB

type AABB struct {
	// Center represents the center of the bounding box.
	Center glm.Vec3

	// HalfExtend represents the 3 half extends of the bounding box.
	HalfExtend glm.Vec3
}

AABB is an axis-aligned bounding box

func AABBFromSphere

func AABBFromSphere(s *Sphere) AABB

AABBFromSphere returns the AABB bounding this sphere.

NOTE: If you need to use this function you better start questioning the algorithm you're implementing as the sphere is both faster and bounds the underlying object better.

type BarycentricCache

type BarycentricCache struct {
	// contains filtered or unexported fields
}

BarycentricCache is a cache of data for quick Barycentric calls. Use it if you need to call Barycentric on the same triangle over and over.

func BarycentricCacheFromTriangle

func BarycentricCacheFromTriangle(a, b, c *glm.Vec3) BarycentricCache

BarycentricCacheFromTriangle takes a triangle and returns a barycentric cache for queries.

type Capsule

type Capsule struct {
	A, B   glm.Vec3
	Radius float32
}

Capsule is a cylinder with round end or can be used as a swept sphere.

type DOP8

type DOP8 struct {
	Min [4]float32
	Max [4]float32
}

DOP8 is an 8-DOP.

type OBB

type OBB struct {
	// The center of the OBB
	Center glm.Vec3
	// The orientation of the OBB, these need to be orthonormal.
	Orientation [3]glm.Vec3
	// The half extends of the OBB.
	HalfExtend glm.Vec3
}

OBB is a Oriented Bounding Box.

type Plane

type Plane struct {
	// the normal to the plane
	N glm.Vec3

	// The arbitrary point that the plane starts on.
	P glm.Vec3
}

Plane is a hyperplane in 3D.

func PlaneFromPoints

func PlaneFromPoints(a, b, c *glm.Vec3) Plane

PlaneFromPoints computes the plane given by (a,b,c), ordered ccw.

type Rect

type Rect struct {
	// the center of the rectangle
	Center glm.Vec3
	// the orientation of the rectangle in space.
	Orientation [2]glm.Vec3
	// the half extends of the rectangle.
	HalfExtend glm.Vec2
}

Rect is a rectangle in 3D space, they are a simpler version of OBBs.

type Simplex

type Simplex struct {
	// the Points contained in the simplex. Data past Points[Size] is assumed to
	// be garbage.
	Points [4]glm.Vec3 // use an array to keep the memory all in 1 spot
	// the current extend of the simplex.
	Size int
}

Simplex represents a simplex in 3D. Either a point, a line, a triangle, or a tetrahedron.

func (*Simplex) Merge

func (s *Simplex) Merge(u *glm.Vec3)

Merge merges the given vector to the simplex. This will panic if you add a 5th vertex.

func (*Simplex) NearestToOrigin

func (s *Simplex) NearestToOrigin() (direction glm.Vec3, containsOrigin bool)

NearestToOrigin modifies the simplex to contain only the minimum amount of points required to describe the direction to origin, it also returns the next direction to search in GJK and true if the origin is contained in the simplex

type Slab

type Slab struct {
	// The direction of the slab.
	Normal glm.Vec3

	// The distance from origin along the Normal that the slab starts and end.
	Near, Far float32
}

Slab represent a region R = (x, y, z) | Near <= a*x + b*y + c*z <= Far

type Sphere

type Sphere struct {
	Center          glm.Vec3
	Radius, Radius2 float32
}

Sphere is a bounding volume for spheres.

func EigenSphere

func EigenSphere(points []glm.Vec3) Sphere

EigenSphere sets this sphere to the bounding sphere of the given points using eigen values algorithm, this doesn't necessarily wrap all the points so use RitterEigenSphere.

func RitterEigenSphere

func RitterEigenSphere(points []glm.Vec3) Sphere

RitterEigenSphere sets this sphere to wrap all the given points using eigen values as base.

func (*Sphere) MergePoint

func (s *Sphere) MergePoint(v *glm.Vec3)

MergePoint updates the bounding sphere to encompass v if needed.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL