Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
plane_collision_error_function.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
13
14#include <span>
15#include <vector>
16
17namespace momentum {
18
24template <typename T>
26 public:
28 const Skeleton& skel,
29 const ParameterTransform& pt,
30 const CollisionGeometry& cg,
31 const Vector3<T>& planeNormal,
32 T planeOffset);
33
35 const Character& character,
36 const Vector3<T>& planeNormal,
37 T planeOffset);
38
39 [[nodiscard]] double getError(
40 const ModelParametersT<T>& params,
41 const SkeletonStateT<T>& state,
42 const MeshStateT<T>& meshState) final;
43
45 const ModelParametersT<T>& params,
46 const SkeletonStateT<T>& state,
47 const MeshStateT<T>& meshState,
48 Ref<VectorX<T>> gradient) override;
49
51 const ModelParametersT<T>& params,
52 const SkeletonStateT<T>& state,
53 const MeshStateT<T>& meshState,
54 Ref<MatrixX<T>> jacobian,
55 Ref<VectorX<T>> residual,
56 int& usedRows) override;
57
58 [[nodiscard]] size_t getJacobianSize() const final;
59
61 void setPlane(const Vector3<T>& planeNormal, T planeOffset) {
62 collisionQuery_.setPlane(planeNormal, planeOffset);
63 }
64
66 [[nodiscard]] const Vector3<T>& getPlaneNormal() const {
67 return collisionQuery_.getPlaneNormal();
68 }
69
71 [[nodiscard]] T getPlaneOffset() const {
72 return collisionQuery_.getPlaneOffset();
73 }
74
77 return collisionQuery_;
78 }
79
81 [[nodiscard]] const PlaneCollisionQueryT<T>& getCollisionQuery() const {
82 return collisionQuery_;
83 }
84
85 [[nodiscard]] std::vector<size_t> getCollidingPrimitives() const;
86
91 [[nodiscard]] std::vector<PlaneCollisionDebugEntryT<T>> getCollisionDebugInfo() const;
92
97 [[nodiscard]] std::vector<Vector3<T>> getContactPoints(
98 std::span<const JointStateT<T>> states,
99 T contactMargin);
100
106 [[nodiscard]] std::vector<Vector3<T>> getContactPointsByParent(
107 std::span<const JointStateT<T>> states,
108 T contactMargin);
109
115 [[nodiscard]] std::vector<PlaneCollisionContactPointT<T>> getContactPointsByParentWithDetails(
116 std::span<const JointStateT<T>> states,
117 T contactMargin);
118
119 protected:
124 const SkeletonStateT<T>& state,
125 const Vector3<T>& position,
126 const Vector3<T>& direction,
127 T weight,
128 size_t startJoint,
129 Ref<VectorX<T>> gradient,
130 T scaleCorrection = T(0)) const;
131
136 const SkeletonStateT<T>& state,
137 const Vector3<T>& position,
138 const Vector3<T>& direction,
139 T factor,
140 size_t startJoint,
141 Ref<MatrixX<T>> jacobian,
142 int row,
143 T scaleCorrection = T(0)) const;
144
146
147 static constexpr T kCollisionWeight = 5e-3f;
148};
149
150} // namespace momentum
double getJacobian(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< MatrixX< T > > jacobian, Ref< VectorX< T > > residual, int &usedRows) override
PlaneCollisionErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const CollisionGeometry &cg, const Vector3< T > &planeNormal, T planeOffset)
PlaneCollisionQueryT< T > & getCollisionQuery()
Return the reusable query helper used by this error function.
void setPlane(const Vector3< float > &planeNormal, float planeOffset)
void accumulateJacobianAlongChain(const SkeletonStateT< T > &state, const Vector3< T > &position, const Vector3< T > &direction, T factor, size_t startJoint, Ref< MatrixX< T > > jacobian, int row, T scaleCorrection=T(0)) const
Accumulate Jacobian contributions along a joint chain up to the root.
std::vector< PlaneCollisionDebugEntryT< T > > getCollisionDebugInfo() const
Query detailed collision state for all currently-colliding primitives.
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.
T getPlaneOffset() const
Return the plane offset after normalization by setPlane().
void accumulateGradientAlongChain(const SkeletonStateT< T > &state, const Vector3< T > &position, const Vector3< T > &direction, T weight, size_t startJoint, Ref< VectorX< T > > gradient, T scaleCorrection=T(0)) const
Accumulate gradient contributions along a joint chain up to the root.
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.
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< VectorX< T > > gradient) override
const PlaneCollisionQueryT< T > & getCollisionQuery() const
Return the reusable query helper used by this error function.
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
const Vector3< T > & getPlaneNormal() const
Return the normalized plane normal used by collision and contact queries.
PlaneCollisionErrorFunctionT(const Character &character, const Vector3< T > &planeNormal, T planeOffset)
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 an explicit state.
Stateful query helper for collision primitives against a fixed world plane.
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
CollisionGeometryT< float > CollisionGeometry
SkeletonT< float > Skeleton
Definition fwd.h:263
Eigen::Ref< T > Ref
Definition types.h:43
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
Eigen::Matrix< T, Dynamic, Dynamic > MatrixX
Dynamic-size, column-major dense matrix.
Definition types.h:80
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
Vector< T, 3 > Vector3
Definition types.h:236
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
Represents the complete state of a model's mesh.
Definition mesh_state.h:24
A vector of size numModelParams holding the values of all model parameters for a character.
Definition types.h:99
Represents the complete state of a skeleton.