shape3d

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const InvalidCompactTreeItemID = CompactTreeItemID(0xFFFFFFFF)

InvalidCompactTreeItemID is an identifier that can be used by user code to mark an identifier as invalid. However, such an identifier will never be returned by the library and must also never be passed to the library.

View Source
const InvalidObjectID = ObjectID(invalidObjectIndex)

InvalidObjectID indicates an object that can never be part of the scene.

View Source
const InvalidShapeID = ShapeID(invalidShapeRef)

InvalidShapeID indicates a shape that can never be part of the scene.

Variables

This section is empty.

Functions

func CheckBoxMeshIntersection

func CheckBoxMeshIntersection(box Box, mesh Mesh, yield IntersectionYieldFunc)

CheckBoxMeshIntersection checks if a Box shape intersects with a Mesh shape.

func CheckSegmentBoxIntersection

func CheckSegmentBoxIntersection(segment Segment, box Box, yield IntersectionYieldFunc)

CheckSegmentBoxIntersection checks if the specified segment intersects the specified box and returns the intersection information.

This implementation uses the slab method adapted for oriented boxes. It returns the closest intersection point along the segment.

func CheckSegmentCircleIntersection

func CheckSegmentCircleIntersection(segment Segment, circle Circle, yield IntersectionYieldFunc)

CheckSegmentCircleIntersection returns whether the specified segment intersects the specified circle and returns the intersection point.

This implementation assumes that the circle has backface culling. Hence, a segment starting on the "negative" side of the circle and ending on the "positive" side will not produce an intersection.

A standard Intersection result is not meaningful here.

func CheckSegmentMeshIntersection

func CheckSegmentMeshIntersection(segment Segment, mesh Mesh, yield IntersectionYieldFunc)

CheckSegmentMeshIntersection checks if a segment intersects a mesh.

TODO: Rework this. Just like with sphere an Intersection is not meaningful here and instead an intersection point (one closest to segment start) should be returned.

func CheckSegmentSphereIntersection

func CheckSegmentSphereIntersection(segment Segment, sphere Sphere, yield IntersectionYieldFunc)

CheckSegmentSphereIntersection checks if the specified segment intersects the specified sphere and returns the intersection point.

This implementation assumes that the sphere has backface culling. Hence, a segment starting from inside the sphere will not produce an intersection. For such cases you can flip the segment to get the intersection going out.

func CheckSegmentSurfaceIntersection

func CheckSegmentSurfaceIntersection(segment Segment, surface Surface, yield IntersectionYieldFunc)

CheckSegmentSurfaceIntersection checks whether the specified segment intersects the specified surface and returns the intersection point.

This implementation assumes that the surface has backface culling. Hence, a segment starting on the "negative" side of the surface and ending on the "positive" side will not produce an intersection.

func CheckSegmentTriangleIntersection

func CheckSegmentTriangleIntersection(segment Segment, triangle Triangle, yield IntersectionYieldFunc)

CheckSegmentTriangleIntersection checks if a line segment shape intersects with a triangle shape.

This function assumes that the triangle has backface culling.

func CheckSphereBoxIntersection

func CheckSphereBoxIntersection(sphere Sphere, box Box, yield IntersectionYieldFunc)

CheckSphereBoxIntersection checks if the specified sphere intersects with the specified box.

func CheckSphereMeshIntersection

func CheckSphereMeshIntersection(sphere Sphere, mesh Mesh, yield IntersectionYieldFunc)

func CheckSphereSphereIntersection

func CheckSphereSphereIntersection(source, target Sphere, yield IntersectionYieldFunc)

CheckSphereSphereIntersection checks if a Sphere shape intersects with another Sphere shape.

func CheckSphereTriangleIntersection

func CheckSphereTriangleIntersection(sphere Sphere, triangle Triangle, yield IntersectionYieldFunc)

TODO: Revisit this!

func IsSegmentCircleIntersection

func IsSegmentCircleIntersection(segment Segment, circle Circle) bool

IsSegmentCircleIntersection returns whether the specified segment intersects the specified circle.

This implementation assumes that the circle has backface culling. Hence, a segment starting on the "negative" side of the circle and ending on the "positive" side will not produce an intersection.

func IsSegmentSphereIntersection

func IsSegmentSphereIntersection(segment Segment, sphere Sphere) bool

IsSegmentSphereIntersection checks if the specified segment intersects the specified sphere.

This implementation assumes that the sphere has backface culling. Hence, a segment starting from inside the sphere will not produce an intersection. For such cases you can flip the segment to get the intersection going out.

func IsSegmentSphereOverlap

func IsSegmentSphereOverlap(segment Segment, sphere Sphere) bool

IsSegmentSphereOverlap checks if the specified segment overlaps in any way the specified sphere, including being contained by the sphere.

func IsSegmentSurfaceIntersection

func IsSegmentSurfaceIntersection(segment Segment, surface Surface) bool

IsSegmentSurfaceIntersection checks whether the specified segment intersects the specified surface.

This implementation assumes that the surface has backface culling. Hence, a segment starting on the "negative" side of the surface and ending on the "positive" side will not produce an intersection.

func IsSphereSphereIntersection

func IsSphereSphereIntersection(source, target Sphere) bool

IsSphereSphereIntersection checks if the source sphere intersects with the target sphere.

Only a bool result is returned and no collision points or separation normals are evaluated.

Types

type Box

type Box struct {

	// Position holds the position of the box.
	Position dprec.Vec3

	// Rotation holds the rotation of the box.
	Rotation dprec.Quat

	// HalfWidth holds the half-width of the bx.
	HalfWidth float64

	// HalfHeight holds the half-height of the box.
	HalfHeight float64

	// HalfLength holds the half-length of the box.
	HalfLength float64
}

Box represents a cuboid shape.

func NewBox

func NewBox(position dprec.Vec3, rotation dprec.Quat, size dprec.Vec3) Box

NewBox creates a new box with the specified position, rotation and size.

The size is the full size of the box. Internally it will be converted to half sizes.

func TransformedBox

func TransformedBox(source Box, transform Transform) Box

TransformedBox creates a new box from the specified source box by applying the specified transformation.

func (*Box) BoundingSphere

func (b *Box) BoundingSphere() Sphere

BoundingSphere returns the bounding sphere of the box.

type BoxInfo

type BoxInfo[S any] struct {

	// ShapeInfo contains general shape information.
	ShapeInfo[S]

	// Box contains the box information.
	Box Box
}

BoxInfo contains the information needed to create a box shape.

type Circle

type Circle struct {

	// Position specifies the position of the circle.
	Position dprec.Vec3

	// Normal specifies the orientation of the circle using a normal vector.
	Normal dprec.Vec3

	// Radius specifies the radius of the circle.
	Radius float64
}

Circle represents a 2D circle in 3D space.

func NewCircle

func NewCircle(position, normal dprec.Vec3, radius float64) Circle

NewCircle returns a circle from the specified position, normal and radius.

type CompactCube

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

CompactCube represents a cube area used for inserting items into the tree.

func NewCompactCube

func NewCompactCube(x, y, z, size float64) CompactCube

NewCompactCube creates a new CompactCube instance from the specified position and size.

func NewCompactCubeFromSphere

func NewCompactCubeFromSphere(sphere Sphere) CompactCube

NewCompactCubeFromSphere creates a new CompactCube that wraps a sphere.

type CompactQueryAABB

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

CompactQueryAABB represents an axis-aligned bounding box used for querying the tree.

func NewCompactQueryAABB

func NewCompactQueryAABB(minX, maxX, minY, maxY, minZ, maxZ float64) CompactQueryAABB

NewCompactQueryAABB creates a new CompactQueryAABB instance from the specified bounds.

func NewCompactQueryAABBFromSphere

func NewCompactQueryAABBFromSphere(sphere Sphere) CompactQueryAABB

NewCompactQueryAABBFromSphere creates a new CompactQueryAABB that wraps a sphere.

type CompactQuerySegment

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

CompactQuerySegment represents a line segment used for querying the tree.

func NewCompactQuerySegment

func NewCompactQuerySegment(a, b dprec.Vec3) CompactQuerySegment

NewCompactQuerySegment creates a new CompactQuerySegment instance from the specified endpoints.

type CompactTree

type CompactTree[T any] struct {
	// contains filtered or unexported fields
}

CompactTree is a spatial structure that uses a loose octree implementation with shrinking bounding box to enable the fast searching of items.

func NewCompactTree

func NewCompactTree[T any](settings CompactTreeSettings) *CompactTree[T]

NewCompactTree creates a new CompactTree using the provided settings.

func (*CompactTree[T]) GC

func (t *CompactTree[T]) GC()

GC runs cleanup and optimization logic. You should call this at least once per frame.

func (*CompactTree[T]) Insert

func (t *CompactTree[T]) Insert(cube CompactCube, value T) CompactTreeItemID

Insert adds an item, which occupies the specified cube area, to this tree.

func (*CompactTree[T]) QueryAABB

func (t *CompactTree[T]) QueryAABB(queryBox CompactQueryAABB, yield VisitorFunc[T])

QueryAABB finds all items that are inside or intersect the specified axis-aligned bounding box. Each found item is passed to the specified yield function. The order in which items are passed is undefined and might change between invocations.

func (*CompactTree[T]) QuerySegment

func (t *CompactTree[T]) QuerySegment(querySegment CompactQuerySegment, yield VisitorFunc[T])

QuerySegment finds all items that intersect the specified segment. Each found item is passed to the specified yield function. The order in which items are passed is undefined and might change between invocations.

func (*CompactTree[T]) Remove

func (t *CompactTree[T]) Remove(id CompactTreeItemID)

Remove removes the item with the specified id from this tree.

func (*CompactTree[T]) Stats

func (t *CompactTree[T]) Stats() CompactTreeStats

Stats returns statistics on the current state of this tree.

func (*CompactTree[T]) Update

func (t *CompactTree[T]) Update(id CompactTreeItemID, cube CompactCube)

Update repositions the item with the specified id to the new area.

func (*CompactTree[T]) VisitStats

func (t *CompactTree[T]) VisitStats() CompactTreeVisitStats

VisitStats returns statistics information on the last executed search in this tree.

type CompactTreeItemID

type CompactTreeItemID uint32

CompactTreeItemID is an identifier used to control the placement of an item into a compact tree.

type CompactTreeSettings

type CompactTreeSettings struct {

	// Size specifies the dimension (side to side) of the tree node.
	//
	// If not specified, a default size of 4096 is used.
	//
	// Inserting an item outside these bounds has undefined behavior.
	Size opt.T[float64]

	// MaxDepth controls the maximum depth that the tree can reach.
	//
	// If not specified, a default max depth of 8 is used.
	MaxDepth opt.T[uint32]

	// InitialNodeCapacity is a hint as to the number of nodes that will be
	// needed to place all items. Usually one would find that number empirically.
	// This allows the tree to preallocate memory and avoid dynamic allocations.
	//
	// By default the initial capacity is 4096.
	InitialNodeCapacity opt.T[uint32]

	// InitialItemCapacity is a hint as to the likely upper bound of items that
	// will be inserted into the tree. This allows the tree to preallocate
	// memory and avoid dynamic allocations during insertion.
	//
	// By default the initial capacity is 1024.
	InitialItemCapacity opt.T[uint32]
}

CompactTreeSettings contains the settings for a CompactTree.

type CompactTreeStats

type CompactTreeStats struct {

	// NodeCount is the total number of nodes in the tree.
	NodeCount uint32

	// ItemCount is the total number of items in the tree.
	ItemCount uint32

	// ItemCountPerDepth contains the number of items at each depth level.
	ItemCountPerDepth []uint32
}

CompactTreeStats represents the current state of a CompactTree.

type CompactTreeVisitStats

type CompactTreeVisitStats struct {

	// NodeCountVisited is the number of nodes that were visited during the last
	// visit operation.
	NodeCountVisited uint32

	// NodeCountAccepted is the number of nodes that were determined relevant
	// during the last visit operation.
	NodeCountAccepted uint32

	// NodeCountRejected is the number of nodes that were determined irrelevant
	// during the last visit operation.
	NodeCountRejected uint32

	// ItemCountVisited is the number of items that were visited during the last
	// visit operation.
	ItemCountVisited uint32

	// ItemCountAccepted is the number of items that were determined relevant
	// during the last visit operation.
	ItemCountAccepted uint32

	// ItemCountRejected is the number of items that were determined irrelevant
	// during the last visit operation.
	ItemCountRejected uint32
}

CompactTreeVisitStats represents statistics on the last visit operation performed on a CompactTree.

type Filter

type Filter struct {

	// Mask is a bitmask used to filter shapes based on their assigned layers.
	Mask opt.T[uint32]

	// SkipDynamic indicates whether dynamic shapes should be excluded from the
	// results.
	SkipDynamic bool

	// SkipStatic indicates whether static shapes should be excluded from the
	// results.
	SkipStatic bool
}

Filter represents a set of criteria to filter 3D shapes in a scene.

type Intersection

type Intersection struct {

	// TargetContact contains the point on the target shape where the
	// intersection first occurred.
	TargetContact dprec.Vec3

	// TargetNormal contains the best direction in which the source shape
	// must be translated to avoid the intersection.
	TargetNormal dprec.Vec3

	// Depth returns the amount of penetration between the two shapes.
	Depth float64
}

Intersection represents the intersection of two shapes.

func (*Intersection) EvalSourceContact

func (i *Intersection) EvalSourceContact() dprec.Vec3

EvalSourceContact calculates the contact point on the source shape.

func (*Intersection) EvalSourceNormal

func (i *Intersection) EvalSourceNormal() dprec.Vec3

EvalSourceNormal calculates the normal on the source shape along which the target shape needs to be repositioned to resolve the intersection.

func (*Intersection) Flipped

func (i *Intersection) Flipped() Intersection

Flipped returns a flipped (source and target swapped) version of this intersection.

type IntersectionCollection

type IntersectionCollection interface {

	// AddIntersection adds an intersection to the collection.
	AddIntersection(intersection Intersection)
}

IntersectionCollection represents a data structure that can hold one or more intersections.

type IntersectionYieldFunc

type IntersectionYieldFunc func(intersection Intersection)

IntersectionYieldFunc is a function type that can be used to yield intersections as they are found.

type LargestIntersection

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

LargestIntersection is an implementation of IntersectionCollection that keeps track of the closest (largest depth) observed intersection.

func (*LargestIntersection) AddIntersection

func (i *LargestIntersection) AddIntersection(intersection Intersection)

AddIntersection tracks the specified intersection.

func (*LargestIntersection) Intersection

func (i *LargestIntersection) Intersection() (Intersection, bool)

Intersection returns the worst observed intersection and a flag whether there was actually any intersection observed.

func (*LargestIntersection) Reset

func (i *LargestIntersection) Reset()

Reset clears any observed intersection.

type LargestObjectIntersection

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

LargestObjectIntersection is an implementation of ObjectIntersectionCollection that keeps track of the closest (largest depth) observed intersection.

func (*LargestObjectIntersection) AddIntersection

func (i *LargestObjectIntersection) AddIntersection(intersection ObjectIntersection)

AddIntersection tracks the specified intersection.

func (*LargestObjectIntersection) Intersection

func (i *LargestObjectIntersection) Intersection() (ObjectIntersection, bool)

Intersection returns the worst observed intersection and a flag whether there was actually any intersection observed.

func (*LargestObjectIntersection) Reset

func (i *LargestObjectIntersection) Reset()

Reset clears any observed intersection.

type LastIntersection

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

LastIntersection is an implementation of IntersectionCollection that keeps track of the last observed intersection.

func (*LastIntersection) AddIntersection

func (i *LastIntersection) AddIntersection(intersection Intersection)

AddIntersection tracks the specified intersection.

func (*LastIntersection) Intersection

func (i *LastIntersection) Intersection() (Intersection, bool)

Intersection returns the last observed intersection and a flag whether there was actually any intersection observed.

func (*LastIntersection) Reset

func (i *LastIntersection) Reset()

Reset clears any observed intersection.

type Mesh

type Mesh struct {

	// Triangles contains all the triangles that make up the mesh.
	Triangles []Triangle
}

Mesh represents a mesh shape that is comprised of triangles.

func NewMesh

func NewMesh(triangles []Triangle) Mesh

NewMesh creates a mesh from the specified triangles.

NOTE: The mesh becomes the owner of the triangle slice.

func TransformedMesh

func TransformedMesh(source Mesh, transform Transform) Mesh

TransformedMesh creates a new mesh from the specified source mesh by applying the specified transformation.

func (*Mesh) BoundingSphere

func (m *Mesh) BoundingSphere() Sphere

BoundingSphere returns a Sphere that encompases this mesh.

type MeshInfo

type MeshInfo[S any] struct {

	// ShapeInfo contains general shape information.
	ShapeInfo[S]

	// Mesh contains the mesh information.
	Mesh Mesh
}

MeshInfo contains the information needed to create a mesh shape.

type ObjectID

type ObjectID uint32

ObjectID is a reference to an object in the scene.

type ObjectInfo

type ObjectInfo[O any] struct {

	// Position optionally specifies a position where the object should be
	// placed.
	//
	// Defaults to the origin.
	Position opt.T[dprec.Vec3]

	// Rotation optionally specifies a rotation of the object.
	//
	// Defaults to the identity rotation.
	Rotation opt.T[dprec.Quat]

	// Static marks the object as static. Static objects are not checked for
	// intersections with other static objects.
	Static bool

	// UserData allows one to attach custom user data to an object.
	UserData O
}

ObjectInfo contains the information needed to create an object in a scene.

type ObjectIntersection

type ObjectIntersection struct {

	// SourceObjectID contains the ID of the first involved object.
	//
	// This ID is equal to InvalidObjectID if the check was not performed with
	// a scene object.
	SourceObjectID ObjectID

	// SourceShapeID contains the ID of the shape from the first involved object.
	//
	// This ID is equal to InvalidShapeID if the check was not performed with
	// a scene object.
	SourceShapeID ShapeID

	// TargetObjectID contains the ID of the second involved object.
	//
	// This ID is equal to InvalidObjectID if the check was not performed with
	// a scene object.
	TargetObjectID ObjectID

	// TargetShapeID contains the ID of the shape from the second involved object.
	//
	// This ID is equal to InvalidShapeID if the check was not performed with
	// a scene object.
	TargetShapeID ShapeID

	// Intersection holds the underlying raw shape intersection.
	Intersection
}

ObjectIntersection represents the intersectio of two objects.

func (*ObjectIntersection) Flipped

Flipped returns a flipped (source and target swapped) version of this intersection.

type ObjectIntersectionBucket

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

ObjectIntersectionBucket is a container for object intersections.

func NewIntersectionBucket

func NewIntersectionBucket(initialCapacity int) *ObjectIntersectionBucket

NewIntersectionBucket creates a new IntersectionBucket instance with the specified initial capacity.

func (*ObjectIntersectionBucket) AddIntersection

func (b *ObjectIntersectionBucket) AddIntersection(intersection ObjectIntersection)

Add adds a new Intersection to this set.

func (*ObjectIntersectionBucket) Intersections

func (s *ObjectIntersectionBucket) Intersections() []ObjectIntersection

Intersections returns a slice of all intersections that have been observed.

NOTE: The slice must not be modified or cached as it will be reused.

func (*ObjectIntersectionBucket) IsEmpty

func (b *ObjectIntersectionBucket) IsEmpty() bool

IsEmpty returns whether no intersections were found.

func (*ObjectIntersectionBucket) Reset

func (b *ObjectIntersectionBucket) Reset()

Reset clears the buffer of this result set so that it can be reused.

type ObjectIntersectionCollection

type ObjectIntersectionCollection interface {

	// AddIntersection adds an intersection to the collection.
	AddIntersection(intersection ObjectIntersection)
}

ObjectIntersectionCollection represents a data structure that can hold one or more object intersections.

type Scene

type Scene[T, S any] struct {
	// contains filtered or unexported fields
}

Scene represents a 3D scene where objects made of shapes can be added.

func NewScene

func NewScene[O, S any](settings SceneSettings) *Scene[O, S]

NewScene creates a new scene.

func (*Scene[O, S]) AttachBox

func (s *Scene[O, S]) AttachBox(objID ObjectID, info BoxInfo[S]) ShapeID

AttachBox creates a box shape and attaches it to the object to be used for intersection tests.

func (*Scene[O, S]) AttachMesh

func (s *Scene[O, S]) AttachMesh(objID ObjectID, info MeshInfo[S]) ShapeID

AttachMesh creates a mesh shape and attaches it to the object to be used for intersection tests.

func (*Scene[O, S]) AttachSphere

func (s *Scene[O, S]) AttachSphere(objID ObjectID, info SphereInfo[S]) ShapeID

AttachSphere creates a sphere shape and attaches it to the object to be used for intersection tests.

func (*Scene[O, S]) BoxIter

func (s *Scene[O, S]) BoxIter(filter Filter) iter.Seq[Box]

BoxIter returns an iterator over all box shapes in the scene that match the filter.

func (*Scene[O, S]) CheckBoxIntersection

func (s *Scene[O, S]) CheckBoxIntersection(box Box, filter Filter) (ObjectIntersection, bool)

CheckBoxIntersection returns the first intersection of the box with the scene.

func (*Scene[O, S]) CheckMeshIntersection

func (s *Scene[O, S]) CheckMeshIntersection(mesh Mesh, filter Filter) (ObjectIntersection, bool)

CheckMeshIntersection returns the first intersection of the mesh with the scene.

func (*Scene[O, S]) CheckSegmentIntersection

func (s *Scene[O, S]) CheckSegmentIntersection(segment Segment, filter Filter) (ObjectIntersection, bool)

CheckSegmentIntersection returns the first intersection of the segment with the scene.

func (*Scene[O, S]) CheckSphereIntersection

func (s *Scene[O, S]) CheckSphereIntersection(sphere Sphere, filter Filter) (ObjectIntersection, bool)

CheckSphereIntersection returns the first intersection of the sphere with the scene.

func (*Scene[O, S]) CollectBoxIntersections

func (s *Scene[O, S]) CollectBoxIntersections(box Box, filter Filter, collection ObjectIntersectionCollection)

CollectBoxIntersections collects all intersections of the box with objects in the scene.

func (*Scene[O, S]) CollectIntersections

func (s *Scene[O, S]) CollectIntersections(collection ObjectIntersectionCollection)

CollectIntersections yields intersections found in this scene.

func (*Scene[O, S]) CollectMeshIntersections

func (s *Scene[O, S]) CollectMeshIntersections(mesh Mesh, filter Filter, collection ObjectIntersectionCollection)

CollectMeshIntersections collects all intersections of the mesh with objects in the scene.

func (*Scene[O, S]) CollectSegmentIntersections

func (s *Scene[O, S]) CollectSegmentIntersections(segment Segment, filter Filter, collection ObjectIntersectionCollection)

CollectSegmentIntersections collects all intersections of the segment with objects in the scene.

func (*Scene[O, S]) CollectSphereIntersections

func (s *Scene[O, S]) CollectSphereIntersections(sphere Sphere, filter Filter, collection ObjectIntersectionCollection)

CheckSphereIntersection returns the first intersection of the sphere with the scene.

func (*Scene[O, S]) CreateObject

func (s *Scene[O, S]) CreateObject(info ObjectInfo[O]) ObjectID

CreateObject creates a new object.

func (*Scene[O, S]) DeleteObject

func (s *Scene[O, S]) DeleteObject(objID ObjectID)

DeleteObject deletes an object.

func (*Scene[O, S]) DeleteShape

func (s *Scene[O, S]) DeleteShape(shapeID ShapeID)

DeleteShape deletes a shape from an object. The object is not deleted and continues to exist in the scene.

func (*Scene[O, S]) EachBox

func (s *Scene[O, S]) EachBox(filter Filter, yield func(Box) bool)

EachBox iterates over all box shapes in the scene that match the filter and yields them to the provided callback.

func (*Scene[O, S]) EachMesh

func (s *Scene[O, S]) EachMesh(filter Filter, yield func(Mesh) bool)

EachMesh iterates over all mesh shapes in the scene that match the filter and yields them to the provided callback.

func (*Scene[O, S]) EachSphere

func (s *Scene[O, S]) EachSphere(filter Filter, yield func(Sphere) bool)

EachSphere iterates over all sphere shapes in the scene that match the filter and yields them to the provided callback.

func (*Scene[O, S]) GC

func (s *Scene[O, S]) GC()

GC cleans up internal data and allows for memory reuse. This should be called once per frame.

func (*Scene[O, S]) GetObjectTransform

func (s *Scene[O, S]) GetObjectTransform(objID ObjectID) Transform

GetObjectTransform returns the given object's transform.

func (*Scene[O, S]) GetObjectUserData

func (s *Scene[O, S]) GetObjectUserData(objID ObjectID) O

GetObjectUserData returns the user data associated with the given object.

func (*Scene[O, S]) GetShapeUserData

func (s *Scene[O, S]) GetShapeUserData(shapeID ShapeID) S

GetShapeUserData returns the user data associated with the given shape.

func (*Scene[O, S]) MeshIter

func (s *Scene[O, S]) MeshIter(filter Filter) iter.Seq[Mesh]

MeshIter returns an iterator over all mesh shapes in the scene that match the filter.

func (*Scene[O, S]) SetObjectTransform

func (s *Scene[O, S]) SetObjectTransform(objID ObjectID, transform Transform)

SetObjectTransform relocates the given object.

func (*Scene[O, S]) SetObjectUserData

func (s *Scene[O, S]) SetObjectUserData(objID ObjectID, userData O)

SetObjectUserData assigns the specified user data to the object.

func (*Scene[O, S]) SetShapeUserData

func (s *Scene[O, S]) SetShapeUserData(shapeID ShapeID, userData S)

SetShapeUserData assigns the specified user data to the shape.

func (*Scene[O, S]) SphereIter

func (s *Scene[O, S]) SphereIter(filter Filter) iter.Seq[Sphere]

SphereIter returns an iterator over all sphere shapes in the scene that match the filter.

type SceneSettings

type SceneSettings struct {

	// Size specifies the dimension (from side to side) of the scene.
	// Inserting an item outside these bounds has undefined behavior.
	Size opt.T[float64]

	// MaxDepth controls the maximum depth that the underlying octree can reach.
	MaxDepth opt.T[uint32]

	// InitialNodeCapacity is a hint as to the number of nodes that will be
	// needed to place all items. Usually one would find that number empirically.
	// This allows the octree to preallocate memory and avoid dynamic allocations.
	InitialNodeCapacity opt.T[uint32]

	// InitialItemCapacity is a hint as to the likely upper bound of items that
	// will be inserted into the octree. This allows the octree to preallocate
	// memory and avoid dynamic allocations during insertion.
	InitialItemCapacity opt.T[uint32]
}

SceneSettings contains information needed to create an optimal scene.

type Segment

type Segment struct {

	// A is the start of the segment.
	A dprec.Vec3

	// B is the end of the segment.
	B dprec.Vec3
}

Segment represents a line segment with fixed start and end points.

func NewSegment

func NewSegment(a, b dprec.Vec3) Segment

NewSegment creates a new segment with the specified start and end points.

func (Segment) Flipped

func (s Segment) Flipped() Segment

Flipped returns a new segment with the start and end points swapped.

type ShapeID

type ShapeID shapeRef

ShapeID is a reference to a shape in the scene.

type ShapeInfo

type ShapeInfo[S any] struct {

	// RejectGroup becomes active if a value larger than zero is specified.
	// Shapes that share the same reject group are not checked for intersection.
	RejectGroup uint32

	// SourceMask specifies the layers in which this shape is positioned.
	SourceMask opt.T[uint32]

	// TargetMask specifies the layers with which this shape can intersect.
	TargetMask opt.T[uint32]

	// UserData allows one to attach custom user data to a shape.
	UserData S
}

ShapeInfo contains information needed to create a new shape in the scene.

type SmallestIntersection

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

SmallestIntersection is an implementation of IntersectionCollection that keeps track of the farthest (smallest depth) observed intersection.

func (*SmallestIntersection) AddIntersection

func (i *SmallestIntersection) AddIntersection(intersection Intersection)

AddIntersection tracks the specified intersection.

func (*SmallestIntersection) Intersection

func (i *SmallestIntersection) Intersection() (Intersection, bool)

Intersection returns the best observed intersection and a flag whether there was actually any intersection observed.

func (*SmallestIntersection) Reset

func (i *SmallestIntersection) Reset()

Reset clears any observed intersection.

type SmallestObjectIntersection

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

SmallestObjectIntersection is an implementation of ObjectIntersectionCollection that keeps track of the farthest (smallest depth) observed intersection.

func (*SmallestObjectIntersection) AddIntersection

func (i *SmallestObjectIntersection) AddIntersection(intersection ObjectIntersection)

AddIntersection tracks the specified intersection.

func (*SmallestObjectIntersection) Intersection

func (i *SmallestObjectIntersection) Intersection() (ObjectIntersection, bool)

Intersection returns the worst observed intersection and a flag whether there was actually any intersection observed.

func (*SmallestObjectIntersection) Reset

func (i *SmallestObjectIntersection) Reset()

Reset clears any observed intersection.

type Sphere

type Sphere struct {

	// Position is the position of the sphere.
	Position dprec.Vec3

	// Radius is the radius of the sphere.
	Radius float64
}

Sphere represents a sphere shape.

func NewSphere

func NewSphere(position dprec.Vec3, radius float64) Sphere

NewSphere creates a new sphere with the specified position and radius.

func TransformedSphere

func TransformedSphere(source Sphere, transform Transform) Sphere

TransformedSphere creates a new sphere based off of an existing one by applying the specified transformation.

type SphereInfo

type SphereInfo[S any] struct {

	// ShapeInfo contains general shape information.
	ShapeInfo[S]

	// Sphere contains the sphere information.
	Sphere Sphere
}

SphereInfo contains the information needed to create a sphere shape.

type Surface

type Surface struct {

	// Normal is a unit vector that represents the orientation of the plane.
	Normal dprec.Vec3

	// Distance is the distance of the plane from the origin along the normal.
	Distance float64
}

Surface represents a plane in 3D space.

It uses a normal + distance representation which both compact and also easier to use that the official A, B, C, D, since the normal is expected to be normalized.

func BasisXSurface

func BasisXSurface() Surface

BasisXSurface surface returns a surface that is oriented along the X axis.

func BasisYSurface

func BasisYSurface() Surface

BasisYSurface surface returns a surface that is oriented along the Y axis.

func BasisZSurface

func BasisZSurface() Surface

BasisZSurface surface returns a surface that is oriented along the Z axis.

func NewSurface

func NewSurface(point, normal dprec.Vec3) Surface

NewSurface returns a new surface from the specified point and normal.

func (Surface) Point

func (s Surface) Point() dprec.Vec3

Point returns an arbitrary point on the surface.

type Transform

type Transform struct {

	// Translation specifies the translation that the transformation applies.
	Translation dprec.Vec3

	// Rotation specifies the rotation that the transformation applies.
	Rotation dprec.Quat
}

Transform represents a shape transformation.

func ChainedTransform

func ChainedTransform(parent, child Transform) Transform

ChainedTransform returns the Transform that is the result of combining two Transforms together.

func IdentityTransform

func IdentityTransform() Transform

IdentityTransform returns a new Transform that represents the origin.

func RotationTransform

func RotationTransform(rotation dprec.Quat) Transform

RotationTransform returns a new Transform that represents a rotation.

func TRTransform

func TRTransform(translation dprec.Vec3, rotation dprec.Quat) Transform

TRTransform returns a new Transform that represents both a translation and a rotation.

func TranslationTransform

func TranslationTransform(translation dprec.Vec3) Transform

TranslationTransform returns a new Transform that represents a translation.

func (*Transform) Apply

func (t *Transform) Apply(v dprec.Vec3) dprec.Vec3

Apply returns the transformation of the specified vector.

type Triangle

type Triangle struct {

	// A is the first vertex of the triangle.
	A dprec.Vec3

	// B is the second vertex of the triangle.
	B dprec.Vec3

	// C is the third vertex of the triangle.
	C dprec.Vec3
}

Triangle represents a tringle in 3D space.

func NewTriangle

func NewTriangle(a, b, c dprec.Vec3) Triangle

NewTriangle creates a new Triangle shape.

func TransformedTriangle

func TransformedTriangle(source Triangle, transform Transform) Triangle

TransformedTriangle creates a new triangle from the specified source triangle by applying the specified transformation.

func (*Triangle) Area

func (t *Triangle) Area() float64

Area returns the triangle's surface area.

func (*Triangle) BoundingSphere

func (t *Triangle) BoundingSphere() Sphere

BoundingSphere returns a Sphere shape that encompases this triangle.

func (*Triangle) Center

func (t *Triangle) Center() dprec.Vec3

Center returns the center of mass of the triangle.

func (*Triangle) ContainsPoint

func (t *Triangle) ContainsPoint(point dprec.Vec3) bool

ContainsPoint checks whether the specified Point is inside the triangle.

Beware, currently this method assumes that the point lies somewhere on the surface plane of the triangle.

func (*Triangle) IsLookingTowards

func (t *Triangle) IsLookingTowards(direction dprec.Vec3) bool

IsLookingTowards checks whether the orientation of the triangle looks towards the same hemisphere as the provided direction.

func (*Triangle) Normal

func (t *Triangle) Normal() dprec.Vec3

Normal returns the orientation of the triangle's surface.

type VisitorBucket

type VisitorBucket[T any] struct {
	// contains filtered or unexported fields
}

VisitorBucket can be used to store items returned from a query.

func NewVisitorBucket

func NewVisitorBucket[T any](initCapacity int) *VisitorBucket[T]

NewVisitorBucket creates a new VisitorBucket instance with the specified initial capacity, which is only used to preallocate memory (it is allowed to exceed the initial capacity).

func (*VisitorBucket[T]) Add

func (r *VisitorBucket[T]) Add(item T) bool

Add records the passed item into the bucket.

func (*VisitorBucket[T]) Each

func (r *VisitorBucket[T]) Each(yield func(item T) bool)

Each calls the provided closure function for each stored item.

func (*VisitorBucket[T]) Items

func (r *VisitorBucket[T]) Items() []T

Items returns the underlying slice of stored items. The returned slice is valid only until the Reset function is called.

func (*VisitorBucket[T]) Reset

func (r *VisitorBucket[T]) Reset()

Reset clears any stored items.

func (*VisitorBucket[T]) VisitorFunc

func (r *VisitorBucket[T]) VisitorFunc() VisitorFunc[T]

VisitorFunc returns a VisitorFunc that can be passed to query functions.

Make sure to call Reset before reusing the bucket, unless you want to append to the existing items.

type VisitorFunc

type VisitorFunc[T any] func(item T) bool

VisitorFunc is a mechanism to receive items from a query.

Jump to

Keyboard shortcuts

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