28 std::vector<size_t> vertexIndices,
30 const Eigen::Vector3<T>& upDirection,
33 character_(character),
34 vertexIndices_(std::move(vertexIndices)),
35 targetHeight_(targetHeight),
36 upDirection_(upDirection.normalized()),
38 MT_CHECK(k > 0,
"k must be greater than 0, got {}", k);
39 MT_CHECK(!vertexIndices_.empty(),
"FloorErrorFunction requires at least one vertex index");
40 MT_CHECK(character.
mesh !=
nullptr,
"FloorErrorFunction requires a character with a mesh");
48 targetHeight_ = height;
53 upDirection_ = upDirection.normalized();
58 MT_CHECK(!indices.empty(),
"FloorErrorFunction requires at least one vertex index");
59 vertexIndices_ = std::move(indices);
63typename FloorErrorFunctionT<T>::MinHeightResult FloorErrorFunctionT<T>::calculateMinHeight(
69 const size_t effectiveK = std::min(k_, vertexIndices_.size());
72 using ProjectionPair = std::pair<T, size_t>;
73 std::priority_queue<ProjectionPair, std::vector<ProjectionPair>, std::less<>> minQueue;
75 for (
const auto vertexIdx : vertexIndices_) {
77 vertexIdx < meshState.posedMesh_->vertices.size(),
78 "Vertex index {} out of range (mesh has {} vertices)",
82 const T projection = upDirection_.dot(meshState.
posedMesh_->vertices[vertexIdx]);
84 if (minQueue.size() < effectiveK) {
85 minQueue.push({projection, vertexIdx});
86 }
else if (projection < minQueue.top().first) {
88 minQueue.push({projection, vertexIdx});
92 MinHeightResult result;
93 result.vertexIndices.reserve(effectiveK);
94 result.vertexWeights.reserve(effectiveK);
96 T projectionSum = T(0);
97 while (!minQueue.empty()) {
98 const auto& pair = minQueue.top();
99 result.vertexIndices.push_back(pair.second);
100 projectionSum += pair.first;
104 const T weight = T(1) / T(effectiveK);
105 result.vertexWeights.resize(effectiveK, weight);
106 result.avgProjection = projectionSum / T(effectiveK);
118 const auto minResult = calculateMinHeight(meshState);
119 const T diff = minResult.avgProjection - targetHeight_;
121 return this->
weight_ * diff * diff;
129 Eigen::Ref<Eigen::VectorX<T>> gradient) {
132 const auto minResult = calculateMinHeight(meshState);
133 const T diff = minResult.avgProjection - targetHeight_;
135 const T wgt = T(2) * diff * this->
weight_;
137 for (
size_t i = 0; i < minResult.vertexIndices.size(); ++i) {
138 calculateVertexGradient(
139 minResult.vertexIndices[i],
142 wgt * minResult.vertexWeights[i] * upDirection_,
146 return this->weight_ * diff * diff;
154 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
155 Eigen::Ref<Eigen::VectorX<T>> residual,
160 jacobian.cols() ==
static_cast<Eigen::Index
>(this->parameterTransform_.transform.cols()));
164 const auto minResult = calculateMinHeight(meshState);
165 const T diff = minResult.avgProjection - targetHeight_;
167 const T wgt = std::sqrt(this->
weight_);
168 residual(0) = wgt * diff;
170 jacobian.row(0).setZero();
171 auto jac_row = jacobian.row(0);
173 for (
size_t i = 0; i < minResult.vertexIndices.size(); ++i) {
174 calculateVertexJacobian(
175 minResult.vertexIndices[i],
178 wgt * minResult.vertexWeights[i] * upDirection_,
183 return wgt * wgt * diff * diff;
192template <
typename Derived>
193void FloorErrorFunctionT<T>::calculateVertexJacobian(
197 const Eigen::Vector3<T>& jacobianDirection,
198 Eigen::MatrixBase<Derived>& jacobian)
const {
204 this->character_, *meshState.
restMesh_, state, vertexIndex);
206 while (!skinningIter.finished()) {
207 size_t jointIndex = 0;
209 Eigen::Vector3<T> pos;
210 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
212 MT_CHECK(jointIndex < this->skeleton_.joints.size());
214 const auto& jointState = state.
jointState[jointIndex];
216 const Eigen::Vector3<T> posd = pos - jointState.translation();
218 for (
size_t d = 0; d < 3; d++) {
219 if (this->activeJointParams_[paramIndex + d]) {
221 boneWeight * jacobianDirection.dot(jointState.getTranslationDerivative(d)),
223 this->parameterTransform_,
226 if (this->activeJointParams_[paramIndex + 3 + d]) {
228 boneWeight * jacobianDirection.dot(jointState.getRotationDerivative(d, posd)),
230 this->parameterTransform_,
234 if (this->activeJointParams_[paramIndex + 6]) {
236 boneWeight * jacobianDirection.dot(jointState.getScaleDerivative(posd)),
238 this->parameterTransform_,
245void FloorErrorFunctionT<T>::calculateVertexGradient(
249 const Eigen::Vector3<T>& gradientDirection,
250 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
256 this->character_, *meshState.restMesh_, state, vertexIndex);
258 while (!skinningIter.finished()) {
259 size_t jointIndex = 0;
261 Eigen::Vector3<T> pos;
262 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
264 MT_CHECK(jointIndex < this->skeleton_.joints.size());
266 const auto& jointState = state.jointState[jointIndex];
268 const Eigen::Vector3<T> posd = pos - jointState.translation();
270 for (
size_t d = 0; d < 3; d++) {
271 if (this->activeJointParams_[paramIndex + d]) {
273 boneWeight * gradientDirection.dot(jointState.getTranslationDerivative(d)),
275 this->parameterTransform_,
278 if (this->activeJointParams_[paramIndex + 3 + d]) {
280 boneWeight * gradientDirection.dot(jointState.getRotationDerivative(d, posd)),
282 this->parameterTransform_,
286 if (this->activeJointParams_[paramIndex + 6]) {
288 boneWeight * gradientDirection.dot(jointState.getScaleDerivative(posd)),
290 this->parameterTransform_,
#define MT_CHECK_NOTNULL(ptr,...)
Asserts that ptr is not null; on failure, throws std::runtime_error with the optional fmt-style messa...
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Error function that constrains the minimum vertex height to a target value.
FloorErrorFunctionT(const Character &character, std::vector< size_t > vertexIndices, T targetHeight=T(0), const Eigen::Vector3< T > &upDirection=Eigen::Vector3< T >::UnitY(), size_t k=5)
Construct a floor error function.
size_t getJacobianSize() const final
double getJacobian(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
void setTargetHeight(T height)
Set the target height for the minimum vertices.
void setUpDirection(const Eigen::Vector3< T > &upDirection)
Set the up direction for height measurement.
void setVertexIndices(std::vector< size_t > indices)
Set the candidate vertex indices.
double getGradient(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
~FloorErrorFunctionT() override
double getError(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Functions for linear blend skinning (Skeletal Subspace Deformation).
void jacobian_jointParams_to_modelParams(const Eigen::Ref< const Eigen::VectorX< T > > &jacobian_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobian)
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
constexpr size_t kParametersPerJoint
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
A character model with skeletal structure, mesh, and optional components.
Mesh_u mesh
3D mesh representing the character's surface
Represents the complete state of a model's mesh.
std::unique_ptr< MeshT< T > > posedMesh_
Posed mesh after the skeleton transforms have been applied.
std::unique_ptr< MeshT< T > > restMesh_
Rest positions of the mesh after the shape basis (and potentially facial expression basis) has been a...
A vector of size numModelParams holding the values of all model parameters for a character.
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.