Dem Bones  1.1.0
Skinning Decomposition Library
DemBonesExt.h
Go to the documentation of this file.
1 // Dem Bones - Skinning Decomposition Library //
3 // Copyright (c) 2019, Electronic Arts. All rights reserved. //
5 
6 
7 
8 #ifndef DEM_BONES_EXT
9 #define DEM_BONES_EXT
10 
11 #include "DemBones.h"
12 
13 #include <Eigen/Geometry>
14 
15 #ifndef DEM_BONES_MAT_BLOCKS
16 #include "MatBlocks.h"
17 #define DEM_BONES_DEM_BONES_EXT_MAT_BLOCKS_UNDEFINED
18 #endif
19 
20 namespace Dem
21 {
22 
30 template<class _Scalar, class _AniMeshScalar>
31 class DemBonesExt: public DemBones<_Scalar, _AniMeshScalar> {
32 public:
33  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34 
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>;
43 
54 
68 
72 
74  Eigen::VectorXd fTime;
75 
77  std::vector<std::string> boneName;
78 
80  Eigen::VectorXi parent;
81 
84 
87 
89  Eigen::MatrixXi rotOrder;
90 
93 
97  clear();
98  }
99 
102  void clear() {
103  fTime.resize(0);
104  boneName.resize(0);
105  parent.resize(0);
106  bind.resize(0, 0);
107  preMulInv.resize(0, 0);
108  rotOrder.resize(0, 0);
110  }
111 
129  void computeRTB(int s, const MatrixX& orient, MatrixX& lr, MatrixX& lt, MatrixX& gb, MatrixX& lbr, MatrixX& lbt, bool degreeRot=true) {
130  computeBind(s, gb);
131 
132  if (parent.size()==0) parent=Eigen::VectorXi::Constant(nB, -1);
133  if (preMulInv.size()==0) preMulInv=MatrixX::Identity(4, 4).replicate(nS, nB);
134  if (rotOrder.size()==0) rotOrder=Eigen::Vector3i(0, 1, 2).replicate(nS, nB);
135 
136  int nFs=fStart(s+1)-fStart(s);
137  lr.resize(nFs*3, nB);
138  lt.resize(nFs*3, nB);
139  lbr.resize(3, nB);
140  lbt.resize(3, nB);
141 
142  MatrixX lm(4*nFs, 4*nB);
143  #pragma omp parallel for
144  for (int j=0; j<nB; j++) {
145  Eigen::Vector3i ro=rotOrder.col(j).template segment<3>(s*3);
146 
147  Vector3 ov=orient.col(j)*EIGEN_PI/180;
148  Matrix3 invOM=Matrix3(Eigen::AngleAxis<_Scalar>(ov(ro(2)), Vector3::Unit(ro(2))))*
149  Eigen::AngleAxis<_Scalar>(ov(ro(1)), Vector3::Unit(ro(1)))*
150  Eigen::AngleAxis<_Scalar>(ov(ro(0)), Vector3::Unit(ro(0)));
151  invOM.transposeInPlace();
152 
153  Matrix4 lb;
154  if (parent(j)==-1) lb=preMulInv.blk4(s, j)*gb.blk4(0, j);
155  else lb=preMulInv.blk4(s, j)*gb.blk4(0, parent(j)).inverse()*gb.blk4(0, j);
156 
157  Vector3 curRot=Vector3::Zero();
158  toRot(invOM*lb.template topLeftCorner<3, 3>(), curRot, ro);
159  lbr.col(j)=curRot;
160  lbt.col(j)=lb.template topRightCorner<3, 1>();
161 
162  Matrix4 lm;
163  for (int k=0; k<nFs; k++) {
164  if (parent(j)==-1) lm=preMulInv.blk4(s, j)*m.blk4(k+fStart(s), j)*gb.blk4(0, j);
165  else lm=preMulInv.blk4(s, j)*(m.blk4(k+fStart(s), parent(j))*gb.blk4(0, parent(j))).inverse()*m.blk4(k+fStart(s), j)*gb.blk4(0, j);
166  toRot(invOM*lm.template topLeftCorner<3, 3>(), curRot, ro);
167  lr.vec3(k, j)=curRot;
168  lt.vec3(k, j)=lm.template topRightCorner<3, 1>();
169  }
170  }
171 
172  if (degreeRot) {
173  lr*=180/EIGEN_PI;
174  lbr*=180/EIGEN_PI;
175  }
176  }
177 
178 private:
183  void computeCentroids(int s, MatrixX& b) {
184  MatrixX c=MatrixX::Zero(4, nB);
185  for (int i=0; i<nV; i++)
186  for (typename SparseMatrix::InnerIterator it(w, i); it; ++it)
187  c.col(it.row())+=pow(it.value(), transAffineNorm)*u.vec3(s, i).homogeneous();
188  for (int j=0; j<nB; j++)
189  if ((c(3, j)!=0)&&(lockM(j)==0)) b.transVec(0, j)=c.col(j).template head<3>()/c(3, j);
190  }
191 
197  void computeBind(int s, MatrixX& b) {
198  if (bind.size()==0) {
199  lockM=Eigen::VectorXi::Zero(nB);
200  bind.resize(nS*4, nB*4);
201  for (int k=0; k<nS; k++) {
202  b=MatrixX::Identity(4, 4).replicate(1, nB);
203  computeCentroids(k, b);
204  bind.block(4*s, 0, 4, 4*nB)=b;
205  }
206  }
207 
208  b=bind.block(4*s, 0, 4, 4*nB);
209  if (bindUpdate==1) computeCentroids(s, b);
210  }
211 
218  void toRot(const Matrix3& rMat, Vector3& curRot, const Eigen::Vector3i& ro, _Scalar eps=_Scalar(1e-10)) {
219  Vector3 r0=rMat.eulerAngles(ro(2), ro(1), ro(0)).reverse();
220  _Scalar gMin=(r0-curRot).squaredNorm();
221  Vector3 rMin=r0;
222  Vector3 r;
223  Matrix3 tmpMat;
224  for (int fx=-1; fx<=1; fx+=2)
225  for (_Scalar sx=-2*EIGEN_PI; sx<2.1*EIGEN_PI; sx+=EIGEN_PI) {
226  r(0)=fx*r0(0)+sx;
227  for (int fy=-1; fy<=1; fy+=2)
228  for (_Scalar sy=-2*EIGEN_PI; sy<2.1*EIGEN_PI; sy+=EIGEN_PI) {
229  r(1)=fy*r0(1)+sy;
230  for (int fz=-1; fz<=1; fz+=2)
231  for (_Scalar sz=-2*EIGEN_PI; sz<2.1*EIGEN_PI; sz+=EIGEN_PI) {
232  r(2)=fz*r0(2)+sz;
233  tmpMat=Matrix3(Eigen::AngleAxis<_Scalar>(r(ro(2)), Vector3::Unit(ro(2))))*
234  Eigen::AngleAxis<_Scalar>(r(ro(1)), Vector3::Unit(ro(1)))*
235  Eigen::AngleAxis<_Scalar>(r(ro(0)), Vector3::Unit(ro(0)));
236  if ((tmpMat-rMat).squaredNorm()<eps) {
237  _Scalar tmp=(r-curRot).squaredNorm();
238  if (tmp<gMin) {
239  gMin=tmp;
240  rMin=r;
241  }
242  }
243  }
244  }
245  }
246  curRot=rMin;
247  }
248 };
249 
250 }
251 
252 #ifdef DEM_BONES_DEM_BONES_EXT_MAT_BLOCKS_UNDEFINED
253 #undef blk4
254 #undef rotMat
255 #undef transVec
256 #undef vec3
257 #undef DEM_BONES_MAT_BLOCKS
258 #endif
259 
260 #undef rotMatFromEuler
261 
262 #endif
Dem::DemBones::m
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:139
Dem::DemBonesExt::computeRTB
void computeRTB(int s, const MatrixX &orient, MatrixX &lr, MatrixX &lt, MatrixX &gb, MatrixX &lbr, MatrixX &lbt, bool degreeRot=true)
Local rotations, translations and global bind matrices of a subject.
Definition: DemBonesExt.h:129
Dem::DemBonesExt::DemBonesExt
DemBonesExt()
Constructor and setting default parameters.
Definition: DemBonesExt.h:96
Dem::DemBonesExt
Extended class to handle hierarchical skeleton with local rotations/translations and bind matrices.
Definition: DemBonesExt.h:31
Dem::DemBones::nV
int nV
Number of vertices, typically indexed by i.
Definition: DemBones.h:114
Dem::DemBonesExt::MatrixX
Eigen::Matrix< _Scalar, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Definition: DemBonesExt.h:35
Dem
Definition: ConvexLS.h:15
Dem::DemBones::transAffineNorm
_Scalar transAffineNorm
[parameter] p-norm for bone translations affinity soft constraint, default = 4.0
Definition: DemBones.h:90
Dem::DemBones::Vector4
Eigen::Matrix< _Scalar, 4, 1 > Vector4
Definition: DemBones.h:74
DemBones.h
Dem::DemBones::u
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:128
Dem::DemBones::iterWeights
const int & iterWeights
[zero indexed, read only] Current weights update iteration number that can be used for callback funct...
Definition: DemBones.h:157
Dem::DemBones::SparseMatrix
Eigen::SparseMatrix< _Scalar > SparseMatrix
Definition: DemBones.h:76
MatBlocks.h
Defines some macros to access sub-blocks of packing transformation/position matrices for convenience.
Dem::DemBones::fStart
Eigen::VectorXi fStart
Start frame indices, size = nS+1, fStart(s), fStart(s+1) are data frames for subject s.
Definition: DemBones.h:123
Dem::DemBonesExt::preMulInv
MatrixX preMulInv
Inverse pre-multiplication matrices, [size] = [4*nS, 4*nB], preMulInv.block(4*s, 4*j,...
Definition: DemBonesExt.h:86
Dem::DemBonesExt::clear
void clear()
Clear all data.
Definition: DemBonesExt.h:102
Dem::DemBonesExt::Matrix3
Eigen::Matrix< _Scalar, 3, 3 > Matrix3
Definition: DemBonesExt.h:37
Dem::DemBonesExt::fTime
Eigen::VectorXd fTime
Timestamps for bone transformations m, [size] = nS, fTime(k) is the timestamp of frame k.
Definition: DemBonesExt.h:74
Dem::DemBones::nS
int nS
Number of subjects, typically indexed by s.
Definition: DemBones.h:118
Dem::DemBones::VectorX
Eigen::Matrix< _Scalar, Eigen::Dynamic, 1 > VectorX
Definition: DemBones.h:73
Dem::DemBonesExt::Vector3
Eigen::Matrix< _Scalar, 3, 1 > Vector3
Definition: DemBonesExt.h:40
Dem::DemBonesExt::bind
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:83
Dem::DemBonesExt::parent
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:80
Dem::DemBonesExt::bindUpdate
int bindUpdate
Bind transformation update, 0=keep original, 1=set translations to p-norm centroids (using transAffin...
Definition: DemBonesExt.h:92
Dem::DemBones::Vector3
Eigen::Matrix< _Scalar, 3, 1 > Vector3
Definition: DemBones.h:75
Dem::DemBones::Triplet
Eigen::Triplet< _Scalar > Triplet
Definition: DemBones.h:77
Dem::DemBones::w
SparseMatrix w
Skinning weights, size = [nB, nV], w.col(i) are the skinning weights of vertex i, w(j,...
Definition: DemBones.h:131
Dem::DemBones::MatrixX
Eigen::Matrix< _Scalar, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Definition: DemBones.h:70
Dem::DemBones::nB
int nB
Number of bones, typically indexed by j.
Definition: DemBones.h:116
Dem::DemBones::Matrix4
Eigen::Matrix< _Scalar, 4, 4 > Matrix4
Definition: DemBones.h:71
Dem::DemBonesExt::boneName
std::vector< std::string > boneName
Name of bones, [size] = nB, boneName(j) is the name bone of j.
Definition: DemBonesExt.h:77
Dem::DemBones
Smooth skinning decomposition with rigid bones and sparse, convex weights.
Definition: DemBones.h:66
Dem::DemBonesExt::rotOrder
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:89
Dem::DemBones::lockM
Eigen::VectorXi lockM
Bone transformation lock control, size = nB, lockM(j) is the amount of input transformations will be ...
Definition: DemBones.h:142
Dem::DemBones::Matrix3
Eigen::Matrix< _Scalar, 3, 3 > Matrix3
Definition: DemBones.h:72
Dem::DemBones::clear
void clear()
Clear all data.
Definition: DemBones.h:161