17#include <dispenso/parallel_for.h>
27 jointState.size() == inverseBindPose.size(),
30 inverseBindPose.size());
32 std::vector<Eigen::Matrix4<T>> transforms(inverseBindPose.size());
33 for (
size_t i = 0; i < inverseBindPose.size(); i++) {
34 transforms[i].noalias() = (jointState[i].transform * inverseBindPose[i]).matrix();
43 typename DeduceSpanType<
const Eigen::Matrix4<T>>::type skinningTransforms) {
47 skin.
index.rows() ==
static_cast<int>(points.size()),
52 skin.
weight.rows() ==
static_cast<int>(points.size()),
59 dispenso::ParForOptions options;
60 options.minItemsPerChunk = 1024u;
62 dispenso::parallel_for(
63 dispenso::makeChunkedRange(0, skin.
index.rows(), dispenso::ParForChunking::kAdaptive),
64 [&](
const size_t rangeBegin,
const size_t rangeEnd) {
65 for (size_t i = rangeBegin; i != rangeEnd; i++) {
66 const Vector3<T>& pos = points[i];
67 auto& output = result[i];
76 for (size_t j = 0; j < kMaxSkinJoints; j++) {
77 const auto& weight = skin.weight(i, j);
83 skin.index(i, j) < skinningTransforms.size(),
84 "skin.index({}, {}): {} vs {}",
88 skinningTransforms.size());
89 const auto& transformation = skinningTransforms[skin.index(i, j)];
93 Eigen::Vector3<T> temp = transformation.template topRightCorner<3, 1>();
94 temp.noalias() += transformation.template topLeftCorner<3, 3>() * pos;
95 output.noalias() += temp * weight;
111 return applySSD<T>(skin, points, skinningTransforms);
130 worldTransforms.size() == inverseBindPose.size(),
132 worldTransforms.size(),
133 inverseBindPose.size());
135 std::vector<Eigen::Matrix4<T>> skinningTransforms(inverseBindPose.size());
136 for (
size_t i = 0; i < inverseBindPose.size(); i++) {
137 skinningTransforms[i].noalias() = (worldTransforms[i] * inverseBindPose[i]).matrix();
139 return applySSD<T>(skin, points, skinningTransforms);
149 worldTransforms.size() == inverseBindPose.size(),
151 worldTransforms.size(),
152 inverseBindPose.size());
154 std::vector<Eigen::Matrix4<T>> skinningTransforms(inverseBindPose.size());
155 for (
size_t i = 0; i < inverseBindPose.size(); i++) {
156 skinningTransforms[i].noalias() = (worldTransforms[i] * inverseBindPose[i]).matrix();
158 return applySSD<T>(skin, points, skinningTransforms);
165 typename DeduceSpanType<
const Eigen::Matrix4<T>>::type skinningTransforms,
192 outputMesh.
faces.size(),
195 dispenso::ParForOptions options;
196 options.minItemsPerChunk = 1024u;
198 dispenso::parallel_for(
199 dispenso::makeChunkedRange(0, skin.
index.rows(), dispenso::ParForChunking::kAdaptive),
200 [&](
const size_t rangeBegin,
const size_t rangeEnd) {
201 for (size_t i = rangeBegin; i != rangeEnd; i++) {
202 const Vector3<T>& pos = mesh.vertices[i];
203 const Vector3<T>& nml = mesh.normals[i];
204 auto& outputp = outputMesh.vertices[i];
206 auto& outputn = outputMesh.normals[i];
215 for (size_t j = 0; j < kMaxSkinJoints; j++) {
216 const auto& weight = skin.weight(i, j);
217 if (weight == 0.0f) {
222 skin.index(i, j) < skinningTransforms.size(),
223 "skin.index({}, {}): {} vs {}",
227 skinningTransforms.size());
228 const auto& transformation = skinningTransforms[skin.index(i, j)];
232 const auto& topLeft = transformation.template topLeftCorner<3, 3>();
233 Eigen::Vector3<T> temp = transformation.template topRightCorner<3, 1>();
234 temp.noalias() += topLeft * pos;
235 outputp.noalias() += temp * weight;
240 outputn.noalias() += topLeft * nml * weight;
256 applySSD(skin, mesh, skinningTransforms, outputMesh);
277 worldTransforms.size() == inverseBindPose.size(),
279 worldTransforms.size(),
280 inverseBindPose.size());
282 std::vector<Eigen::Matrix4<T>> skinningTransforms(inverseBindPose.size());
283 for (
size_t i = 0; i < inverseBindPose.size(); i++) {
284 skinningTransforms[i].noalias() = (worldTransforms[i] * inverseBindPose[i]).matrix();
286 applySSD(skin, mesh, skinningTransforms, outputMesh);
297 worldTransforms.size() == inverseBindPose.size(),
299 worldTransforms.size(),
300 inverseBindPose.size());
302 std::vector<Eigen::Matrix4<T>> skinningTransforms(inverseBindPose.size());
303 for (
size_t i = 0; i < inverseBindPose.size(); i++) {
304 skinningTransforms[i].noalias() = (worldTransforms[i] * inverseBindPose[i]).matrix();
306 applySSD(skin, mesh, skinningTransforms, outputMesh);
313 const size_t index) {
315 state.
jointState.size() == inverseBindPose.size(),
318 inverseBindPose.size());
320 gsl::narrow_cast<Eigen::Index>(index) < skin.
index.rows(),
326 transform.matrix().setZero();
329 const auto& weight = skin.
weight(index, j);
330 if (weight == 0.0f) {
334 auto jointIndex = skin.
index(index, j);
336 jointIndex < inverseBindPose.size(),
337 "skin.index({}, {}): {} vs {}",
341 inverseBindPose.size());
342 const auto transformation =
343 state.
jointState[jointIndex].transform * inverseBindPose[jointIndex];
345 transform.matrix().noalias() += transformation.matrix() * weight;
348 return transform.inverse();
368 state.
jointState.size() == inverseBindPose.size(),
371 inverseBindPose.size());
373 skin.
index.rows() ==
static_cast<int>(points.size()),
378 skin.
weight.rows() ==
static_cast<int>(points.size()),
383 std::vector<Vector3f> res(points.size());
386 for (
size_t i = 0; i < transformations.size(); i++) {
387 transformations[i] = state.
jointState[i].transform * inverseBindPose[i];
390 for (
int i = 0; i != (int)skin.
index.rows(); i++) {
394 transform.matrix().setZero();
397 const auto& weight = skin.
weight(i, j);
398 if (weight == 0.0f) {
403 skin.
index(i, j) < transformations.size(),
404 "skin.index({}, {}): {} vs {}",
408 transformations.size());
409 const auto& transformation = transformations[skin.
index(i, j)];
411 transform.matrix().noalias() += transformation.matrix() * weight;
414 res[i].noalias() = transform.inverse() * pos;
551 for (
int k = 0; k < locator.
skinWeights.size(); ++k) {
552 const auto weight =
static_cast<T
>(locator.
skinWeights[k]);
556 const auto boneIndex = locator.
parents[k];
558 boneIndex >= state.
jointState.size() || boneIndex >= inverseBindPose.size(),
559 "Bone index {} exceeds jointState size {} or inverseBindPose size {}",
562 inverseBindPose.size());
563 const auto& jointState = state.
jointState[boneIndex];
566 weight * (jointState.transform * (inverseBindPose[boneIndex].cast<T>() * restPosition));
570 return worldPos / weightSum;
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Functions for linear blend skinning (Skeletal Subspace Deformation).
Affine3< float > Affine3f
Affine3f getInverseSSDTransformation(const TransformationList &inverseBindPose, const SkinWeights &skin, const SkeletonState &state, const size_t index)
Computes the inverse SSD transformation for a specific vertex.
Vector3< T > getSkinnedLocatorPosition(const SkinnedLocator &locator, const Vector3< T > &restPosition, const TransformationList &inverseBindPose, const SkeletonStateT< T > &state)
Computes the world position of a skinned locator using linear blend skinning.
std::vector< Affine3f > TransformationList
One affine transform per joint, indexed by joint id.
SkeletonStateT< float > SkeletonState
void applyInverseSSD(const TransformationList &inverseBindPose, const SkinWeights &skin, momentum::span< const Vector3f > points, const SkeletonState &state, Mesh &mesh)
Applies inverse SSD to points, storing results in a mesh.
std::vector< Vector3< T > > applySSD(const SkinWeights &skin, typename DeduceSpanType< const Vector3< T > >::type points, typename DeduceSpanType< const Eigen::Matrix4< T > >::type skinningTransforms)
Applies forward SSD (linear blend skinning) to points using precomputed skinning transforms.
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Vector3< float > Vector3f
std::vector< Eigen::Matrix4< T > > computeSkinningTransforms(typename DeduceSpanType< const JointStateT< T > >::type jointState, const TransformationListT< T > &inverseBindPose)
Computes the skinning transforms from joint states and inverse bind poses.
std::vector< Affine3< T > > TransformationListT
Scalar-templated counterpart of TransformationList.
constexpr uint32_t kMaxSkinJoints
Maximum number of joints that can influence a single vertex.
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
A utility struct that facilitates the deduction of a momentum::span type from a given type.
Represents the state of a joint with local and global transformations.
A generic mesh representation with support for vertices, faces, lines, and texture coordinates.
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
std::vector< Eigen::Vector3< T > > normals
List of vertex normals (same size as vertices when computed).
std::vector< Eigen::Vector3i > faces
List of triangular faces defined by vertex indices.
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.
Linear blend skinning weights for a character mesh: per-vertex joint indices and influence weights.
WeightMatrix weight
Joint influence weights per vertex.
IndexMatrix index
Joint indices influencing each vertex.
A locator attached to multiple joints, with its runtime position determined by blending the skinning ...
Eigen::Matrix< uint32_t, kMaxSkinJoints, 1 > parents
Indices of the parent joints used for skinning.
Eigen::Matrix< float, kMaxSkinJoints, 1 > skinWeights
Skinning weight for each entry in parents. Typically sums to 1.0.
Vector3f position
Position in the character's rest-pose coordinate frame.