 |
Dem Bones
1.0
Skinning Decomposition Library
|
13 #include <Eigen/Geometry>
15 #ifndef DEM_BONES_MAT_BLOCKS
17 #define DEM_BONES_DEM_BONES_EXT_MAT_BLOCKS_UNDEFINED
30 template<
class _Scalar,
class _AniMeshScalar>
33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
35 using MatrixX=Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic>;
36 using Matrix4=Eigen::Matrix<_Scalar, 4, 4>;
37 using Matrix3=Eigen::Matrix<_Scalar, 3, 3>;
38 using VectorX=Eigen::Matrix<_Scalar, Eigen::Dynamic, 1>;
39 using Vector4=Eigen::Matrix<_Scalar, 4, 1>;
40 using Vector3=Eigen::Matrix<_Scalar, 3, 1>;
41 using SparseMatrix=Eigen::SparseMatrix<_Scalar>;
42 using Triplet=Eigen::Triplet<_Scalar>;
127 void computeRTB(
int s, MatrixX& lr, MatrixX& lt, MatrixX& gb, MatrixX& lbr, MatrixX& lbt,
bool degreeRot=
true) {
135 lr.resize(nFs*3,
nB);
136 lt.resize(nFs*3,
nB);
140 MatrixX lm(4*nFs, 4*
nB);
141 #pragma omp parallel for
142 for (
int j=0; j<
nB; j++) {
143 Eigen::Vector3i ro=
rotOrder.col(j).template segment<3>(s*3);
147 else lb=
preMulInv.blk4(s, j)*gb.blk4(0,
parent(j)).inverse()*gb.blk4(0, j);
149 Vector3 curRot=Vector3::Zero();
150 toRot(lb.template topLeftCorner<3, 3>(), curRot, ro);
152 lbt.col(j)=lb.template topRightCorner<3, 1>();
155 for (
int k=0; k<nFs; k++) {
158 toRot(lm.template topLeftCorner<3, 3>(), curRot, ro);
159 lr.vec3(k, j)=curRot;
160 lt.vec3(k, j)=lm.template topRightCorner<3, 1>();
175 void computeCentroids(
int s, MatrixX& b) {
176 MatrixX c=MatrixX::Zero(4,
nB);
177 for (
int i=0; i<
nV; i++)
178 for (
typename SparseMatrix::InnerIterator it(
w, i); it; ++it)
179 c.col(it.row())+=pow(it.value(),
transAffineNorm)*
u.vec3(s, i).homogeneous();
180 b=MatrixX::Identity(4, 4).replicate(1,
nB);
181 for (
int j=0; j<
nB; j++)
182 if (c(3, j)!=0) b.transVec(0, j)=c.col(j).template head<3>()/c(3, j);
190 void computeBind(
int s, MatrixX& b) {
191 if (
bind.size()==0) {
194 for (
int k=0; k<
nS; k++) computeCentroids(k, b);
195 bind.block(4*s, 0, 4, 4*
nB)=b;
199 case 0: b=
bind.block(4*s, 0, 4, 4*
nB);
break;
200 case 1: computeCentroids(s, b);
break;
210 void toRot(
const Matrix3& rMat, Vector3& curRot,
const Eigen::Vector3i& ro, _Scalar eps=_Scalar(1e-10)) {
211 Vector3 r0=rMat.eulerAngles(ro(2), ro(1), ro(0)).reverse();
212 _Scalar gMin=(r0-curRot).squaredNorm();
216 for (
int fx=-1; fx<=1; fx+=2)
217 for (_Scalar sx=-2*EIGEN_PI; sx<2.1*EIGEN_PI; sx+=EIGEN_PI) {
219 for (
int fy=-1; fy<=1; fy+=2)
220 for (_Scalar sy=-2*EIGEN_PI; sy<2.1*EIGEN_PI; sy+=EIGEN_PI) {
222 for (
int fz=-1; fz<=1; fz+=2)
223 for (_Scalar sz=-2*EIGEN_PI; sz<2.1*EIGEN_PI; sz+=EIGEN_PI) {
225 tmpMat=Matrix3(Eigen::AngleAxis<_Scalar>(r(ro(2)), Vector3::Unit(ro(2))))*
226 Eigen::AngleAxis<_Scalar>(r(ro(1)), Vector3::Unit(ro(1)))*
227 Eigen::AngleAxis<_Scalar>(r(ro(0)), Vector3::Unit(ro(0)));
228 if ((tmpMat-rMat).squaredNorm()<eps) {
229 _Scalar tmp=(r-curRot).squaredNorm();
244 #ifdef DEM_BONES_DEM_BONES_EXT_MAT_BLOCKS_UNDEFINED
249 #undef DEM_BONES_MAT_BLOCKS
252 #undef rotMatFromEuler
MatrixX m
Bone transformations, size = [4*nF*4, 4*nB], m.blk4(k, j) is the 4*4 relative transformation matrix o...
Definition: DemBones.h:135
DemBonesExt()
Constructor and setting default parameters.
Definition: DemBonesExt.h:95
Extended class to handle hierarchical skeleton with local rotations/translations and bind matrices.
Definition: DemBonesExt.h:31
int nV
Number of vertices, typically indexed by i.
Definition: DemBones.h:113
_Scalar transAffineNorm
[parameter] p-norm for bone translations affinity soft constraint, default = 4.0
Definition: DemBones.h:89
MatrixX u
Geometry at the rest poses, size = [3*nS, nV], u.col(i).segment(3*s, 3) is the rest pose of vertex i ...
Definition: DemBones.h:127
const int & iterWeights
[zero indexed, read only] Current weights update iteration number that can be used for callback funct...
Definition: DemBones.h:150
void computeRTB(int s, MatrixX &lr, MatrixX <, MatrixX &gb, MatrixX &lbr, MatrixX &lbt, bool degreeRot=true)
Local rotations, translations and global bind matrices of a subject.
Definition: DemBonesExt.h:127
Defines some macros to access sub-blocks of packing transformation/position matrices for convenience.
Eigen::VectorXi fStart
Start frame indices, size = nS+1, fStart(s), fStart(s+1) are data frames for subject s.
Definition: DemBones.h:122
MatrixX preMulInv
Inverse pre-multiplication matrices, [size] = [4*nS, 4*nB], preMulInv.block(4*s, 4*j,...
Definition: DemBonesExt.h:85
void clear()
Clear all data.
Definition: DemBonesExt.h:101
Eigen::VectorXd fTime
Timestamps for bone transformations m, [size] = nS, fTime(k) is the timestamp of frame k.
Definition: DemBonesExt.h:73
int nS
Number of subjects, typically indexed by s.
Definition: DemBones.h:117
MatrixX bind
Original bind pre-matrix, [size] = [4*nS, 4*nB], bind.block(4*s, 4*j, 4, 4) is the global bind matrix...
Definition: DemBonesExt.h:82
Eigen::VectorXi parent
Parent bone index, [size] = nB, parent(j) is the index of parent bone of j, parent(j) = -1 if j has n...
Definition: DemBonesExt.h:79
int bindUpdate
Bind transformation update, 0=keep original, 1=set translations to p-norm centroids (using transAffin...
Definition: DemBonesExt.h:91
SparseMatrix w
Skinning weights, size = [nB, nV], w.col(i) are the skinning weights of vertex i, w(j,...
Definition: DemBones.h:130
int nB
Number of bones, typically indexed by j.
Definition: DemBones.h:115
std::vector< std::string > boneName
Name of bones, [size] = nB, boneName(j) is the name bone of j.
Definition: DemBonesExt.h:76
Smooth skinning decomposition with rigid bones and sparse, convex weights.
Definition: DemBones.h:65
Eigen::MatrixXi rotOrder
Rotation order, [size] = [3*nS, nB], rotOrder.col(j).segment<3>(3*s) is the rotation order of bone j ...
Definition: DemBonesExt.h:88
void clear()
Clear all data.
Definition: DemBones.h:154