Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
plane_collision_query.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
15#include <momentum/math/types.h>
16
17#include <cstddef>
18#include <span>
19#include <vector>
20
21namespace momentum {
22
24template <typename T>
33
36
38template <typename T>
44
47
53template <typename T>
55 public:
57 struct Result {
61 T radius = T(0);
62 T overlap = T(0);
64
66 [[nodiscard]] T surfaceDistance() const {
67 return signedDistance - radius;
68 }
69 };
70
77 const CollisionGeometry& cg,
78 const Vector3<T>& planeNormal,
79 T planeOffset);
80
83 const Character& character,
84 const Vector3<T>& planeNormal,
85 T planeOffset);
86
92 void setPlane(const Vector3<T>& planeNormal, T planeOffset);
93
95 [[nodiscard]] const Vector3<T>& getPlaneNormal() const {
96 return planeNormal_;
97 }
98
100 [[nodiscard]] T getPlaneOffset() const {
101 return planeOffset_;
102 }
103
105 [[nodiscard]] size_t primitiveCount() const {
106 return primitiveIndices_.size();
107 }
108
110 [[nodiscard]] size_t candidatePrimitiveCount() const {
111 return candidatePrimitiveIndices_.size();
112 }
113
115 void update(const SkeletonStateT<T>& state, T contactMargin);
116
118 void update(std::span<const JointStateT<T>> states, T contactMargin);
119
122
124 [[nodiscard]] const std::vector<Result>& activeParentCollisions() const {
125 return activeParentCollisions_;
126 }
127
129 [[nodiscard]] std::vector<size_t> getCollidingPrimitives() const;
130
132 [[nodiscard]] std::vector<PlaneCollisionDebugEntryT<T>> getCollisionDebugInfo() const;
133
138 [[nodiscard]] std::vector<Vector3<T>> getContactPoints(
139 std::span<const JointStateT<T>> states,
140 T contactMargin);
141
147 [[nodiscard]] std::vector<Vector3<T>> getContactPointsByParent(
148 std::span<const JointStateT<T>> states,
149 T contactMargin);
150
156 [[nodiscard]] std::vector<PlaneCollisionContactPointT<T>> getContactPointsByParentWithDetails(
157 std::span<const JointStateT<T>> states,
158 T contactMargin);
159
160 private:
161 void updatePrimitiveList();
162
163 [[nodiscard]] bool mayPrimitiveContactPlane(
164 const SkeletonStateT<T>& state,
165 size_t primitiveIndex,
166 T contactMargin) const;
167
168 [[nodiscard]] T primitiveParentSpaceBound(size_t primitiveIndex) const;
169
170 void validateContactMargin(T contactMargin) const;
171
172 void validateJointStateParents(std::span<const JointStateT<T>> states) const;
173
174 [[nodiscard]] bool checkCollision(size_t primitiveIndex, Result& result) const;
175
176 const CollisionGeometry collisionGeometry_;
177 std::vector<size_t> primitiveIndices_;
178 std::vector<size_t> candidatePrimitiveIndices_;
179 std::vector<T> primitiveParentSpaceBounds_;
180 std::vector<Result> activeParentCollisions_;
181 std::vector<size_t> parentContactIndices_;
182 std::vector<T> parentSurfaceDistances_;
183 CollisionGeometryStateT<T> collisionState_;
184 Vector3<T> planeNormal_;
185 T planeOffset_;
186};
187
188} // namespace momentum
std::vector< PlaneCollisionDebugEntryT< T > > getCollisionDebugInfo() const
Query detailed collision state for all currently-colliding primitives.
void updateActiveParentCollisions()
Select the deepest active collision under each parent joint from current candidates.
T getPlaneOffset() const
Return the plane offset after normalization by setPlane().
std::vector< PlaneCollisionContactPointT< T > > getContactPointsByParentWithDetails(std::span< const JointStateT< T > > states, T contactMargin)
Return one detailed world-space primitive surface point per parent joint at or near the plane.
const std::vector< Result > & activeParentCollisions() const
Return the current deepest active collisions, one per parent joint.
std::vector< Vector3< T > > getContactPointsByParent(std::span< const JointStateT< T > > states, T contactMargin)
Return one world-space primitive surface point per parent joint at or near the plane.
void update(std::span< const JointStateT< T > > states, T contactMargin)
Update candidate primitive state for explicit joint states and signed plane-distance margin.
const Vector3< T > & getPlaneNormal() const
Return the normalized plane normal used by collision and contact queries.
void update(const SkeletonStateT< T > &state, T contactMargin)
Update candidate primitive state for a skeleton state and signed plane-distance margin.
PlaneCollisionQueryT(const CollisionGeometry &cg, const Vector3< T > &planeNormal, T planeOffset)
Construct a query over explicit collision geometry.
void setPlane(const Vector3< T > &planeNormal, T planeOffset)
Set the collision plane after normalizing planeNormal and the matching offset.
std::vector< Vector3< T > > getContactPoints(std::span< const JointStateT< T > > states, T contactMargin)
Return world-space primitive surface points at or near the plane for explicit joint states.
size_t primitiveCount() const
Number of character-attached collision primitives owned by this query.
std::vector< size_t > getCollidingPrimitives() const
Return collision primitive indices that penetrate the plane in the current query state.
size_t candidatePrimitiveCount() const
Number of primitives retained by the most recent broad-phase update.
PlaneCollisionQueryT(const Character &character, const Vector3< T > &planeNormal, T planeOffset)
Construct a query over a character's collision geometry and initialize it at the zero pose.
CollisionGeometryT< float > CollisionGeometry
PlaneCollisionDebugEntryT< double > PlaneCollisionDebugEntryd
Definition fwd.h:208
PlaneCollisionContactPointT< double > PlaneCollisionContactPointd
Definition fwd.h:189
PlaneCollisionContactPointT< float > PlaneCollisionContactPoint
Definition fwd.h:188
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
Vector< T, 3 > Vector3
Definition types.h:236
PlaneCollisionDebugEntryT< float > PlaneCollisionDebugEntry
Definition fwd.h:207
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Represents collision geometry states using a Structure of Arrays (SoA) format.
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
World-space plane contact point selected for support/contact queries.
Per-primitive collision debug information returned by plane-collision queries.
Collision information for one primitive selected by the current query state.
T surfaceDistance() const
Signed distance from the primitive surface point to the plane.
Represents the complete state of a skeleton.