Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
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
14
15#include <axel/BoundingBox.h>
16
17#include <vector>
18
19namespace momentum {
20
22template <typename T>
31
34
35template <typename T>
37 public:
43 const Skeleton& skel,
44 const ParameterTransform& pt,
45 const CollisionGeometry& cg,
46 bool filterRestPoseOverlaps = true);
47
48 explicit CollisionErrorFunctionT(const Character& character, bool filterRestPoseOverlaps = true);
49
50 [[nodiscard]] double getError(
51 const ModelParametersT<T>& params,
52 const SkeletonStateT<T>& state,
53 const MeshStateT<T>& meshState) final;
54
56 const ModelParametersT<T>& params,
57 const SkeletonStateT<T>& state,
58 const MeshStateT<T>& meshState,
59 Ref<VectorX<T>> gradient) override;
60
62 const ModelParametersT<T>& /*unused*/,
63 const SkeletonStateT<T>& state,
64 const MeshStateT<T>& meshState,
65 Ref<MatrixX<T>> jacobian,
66 Ref<VectorX<T>> residual,
67 int& usedRows) override;
68
69 [[nodiscard]] size_t getJacobianSize() const final;
70
71 [[nodiscard]] std::vector<Vector2i> getCollisionPairs() const;
72
78 [[nodiscard]] std::vector<CollisionDebugEntryT<T>> getCollisionDebugInfo() const;
79
80 protected:
81 void updateCollisionPairs(bool filterRestPoseOverlaps = true);
82
84 void computeBroadPhase(const SkeletonStateT<T>& state);
85
88 [[nodiscard]] bool checkCollision(
89 const CollisionGeometryStateT<T>& colState,
90 size_t indexA,
91 size_t indexB,
92 CollisionOverlapResultT<T>& result) const;
93
98 const SkeletonStateT<T>& state,
99 const Vector3<T>& position,
100 const Vector3<T>& direction,
101 T weight,
102 size_t startJoint,
103 size_t stopJoint,
104 Ref<VectorX<T>> gradient,
105 T scaleCorrection = T(0)) const;
106
111 const SkeletonStateT<T>& state,
112 const Vector3<T>& position,
113 const Vector3<T>& direction,
114 T factor,
115 size_t startJoint,
116 size_t stopJoint,
117 Ref<MatrixX<T>> jacobian,
118 int row,
119 T scaleCorrection = T(0)) const;
120
123 size_t indexA;
124 size_t indexB;
126 };
127
129
131 std::vector<CollisionPairInfo> validPairs_;
132
134 std::vector<axel::BoundingBox<T>> aabbs_;
135
137
138 // weights for the error functions
139 static constexpr T kCollisionWeight = 5e-3;
140};
141
142} // namespace momentum
bool checkCollision(const CollisionGeometryStateT< float > &colState, size_t indexA, size_t indexB, CollisionOverlapResultT< float > &result) const
std::vector< CollisionDebugEntryT< float > > getCollisionDebugInfo() const
void accumulateJacobianAlongChain(const SkeletonStateT< float > &state, const Vector3< float > &position, const Vector3< float > &direction, float factor, size_t startJoint, size_t stopJoint, Ref< MatrixX< float > > jacobian, int row, float scaleCorrection=float(0)) const
std::vector< axel::BoundingBox< float > > aabbs_
void updateCollisionPairs(bool filterRestPoseOverlaps=true)
CollisionErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const CollisionGeometry &cg, bool filterRestPoseOverlaps=true)
std::vector< Vector2i > getCollisionPairs() const
void accumulateGradientAlongChain(const SkeletonStateT< float > &state, const Vector3< float > &position, const Vector3< float > &direction, float weight, size_t startJoint, size_t stopJoint, Ref< VectorX< float > > gradient, float scaleCorrection=float(0)) const
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< VectorX< T > > gradient) override
void computeBroadPhase(const SkeletonStateT< float > &state)
CollisionErrorFunctionT(const Character &character, bool filterRestPoseOverlaps=true)
double getJacobian(const ModelParametersT< T > &, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< MatrixX< T > > jacobian, Ref< VectorX< T > > residual, int &usedRows) override
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
Vector2< std::int32_t > Vector2i
Definition types.h:292
CollisionDebugEntryT< double > CollisionDebugEntryd
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
CollisionDebugEntryT< float > CollisionDebugEntry
Vector< T, 3 > Vector3
Definition types.h:236
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Per-pair collision debug information returned by getCollisionDebugInfo().
Pre-computed valid collision pair with common ancestor.
Represents collision geometry states using a Structure of Arrays (SoA) format.
Narrow-phase collision information for a pair of primitives.
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.