Dem Bones  1.0
Skinning Decomposition Library
DemBonesExt.h
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 
55 
67 
71 
73  Eigen::VectorXd fTime;
74 
76  std::vector<std::string> boneName;
77 
79  Eigen::VectorXi parent;
80 
82  MatrixX bind;
83 
85  MatrixX preMulInv;
86 
88  Eigen::MatrixXi rotOrder;
89 
92 
96  clear();
97  }
98 
101  void clear() {
102  fTime.resize(0);
103  boneName.resize(0);
104  parent.resize(0);
105  bind.resize(0, 0);
106  preMulInv.resize(0, 0);
107  rotOrder.resize(0, 0);
108  DemBones::clear();
109  }
110 
127  void computeRTB(int s, MatrixX& lr, MatrixX& lt, MatrixX& gb, MatrixX& lbr, MatrixX& lbt, bool degreeRot=true) {
128  computeBind(s, gb);
129 
130  if (parent.size()==0) parent=Eigen::VectorXi::Constant(nB, -1);
131  if (preMulInv.size()==0) preMulInv=MatrixX::Identity(4, 4).replicate(nS, nB);
132  if (rotOrder.size()==0) rotOrder=Eigen::Vector3i(0, 1, 2).replicate(nS, nB);
133 
134  int nFs=fStart(s+1)-fStart(s);
135  lr.resize(nFs*3, nB);
136  lt.resize(nFs*3, nB);
137  lbr.resize(3, nB);
138  lbt.resize(3, nB);
139 
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);
144 
145  Matrix4 lb;
146  if (parent(j)==-1) lb=preMulInv.blk4(s, j)*gb.blk4(0, j);
147  else lb=preMulInv.blk4(s, j)*gb.blk4(0, parent(j)).inverse()*gb.blk4(0, j);
148 
149  Vector3 curRot=Vector3::Zero();
150  toRot(lb.template topLeftCorner<3, 3>(), curRot, ro);
151  lbr.col(j)=curRot;
152  lbt.col(j)=lb.template topRightCorner<3, 1>();
153 
154  Matrix4 lm;
155  for (int k=0; k<nFs; k++) {
156  if (parent(j)==-1) lm=preMulInv.blk4(s, j)*m.blk4(k+fStart(s), j)*gb.blk4(0, j);
157  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);
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>();
161  }
162  }
163 
164  if (degreeRot) {
165  lr*=180/EIGEN_PI;
166  lbr*=180/EIGEN_PI;
167  }
168  }
169 
170 private:
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);
183  }
184 
190  void computeBind(int s, MatrixX& b) {
191  if (bind.size()==0) {
192  bind.resize(nS*4, nB*4);
193  MatrixX b;
194  for (int k=0; k<nS; k++) computeCentroids(k, b);
195  bind.block(4*s, 0, 4, 4*nB)=b;
196  }
197 
198  switch (bindUpdate) {
199  case 0: b=bind.block(4*s, 0, 4, 4*nB); break;
200  case 1: computeCentroids(s, b); break;
201  }
202  }
203 
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();
213  Vector3 rMin=r0;
214  Vector3 r;
215  Matrix3 tmpMat;
216  for (int fx=-1; fx<=1; fx+=2)
217  for (_Scalar sx=-2*EIGEN_PI; sx<2.1*EIGEN_PI; sx+=EIGEN_PI) {
218  r(0)=fx*r0(0)+sx;
219  for (int fy=-1; fy<=1; fy+=2)
220  for (_Scalar sy=-2*EIGEN_PI; sy<2.1*EIGEN_PI; sy+=EIGEN_PI) {
221  r(1)=fy*r0(1)+sy;
222  for (int fz=-1; fz<=1; fz+=2)
223  for (_Scalar sz=-2*EIGEN_PI; sz<2.1*EIGEN_PI; sz+=EIGEN_PI) {
224  r(2)=fz*r0(2)+sz;
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();
230  if (tmp<gMin) {
231  gMin=tmp;
232  rMin=r;
233  }
234  }
235  }
236  }
237  }
238  curRot=rMin;
239  }
240 };
241 
242 }
243 
244 #ifdef DEM_BONES_DEM_BONES_EXT_MAT_BLOCKS_UNDEFINED
245 #undef blk4
246 #undef rotMat
247 #undef transVec
248 #undef vec3
249 #undef DEM_BONES_MAT_BLOCKS
250 #endif
251 
252 #undef rotMatFromEuler
253 
254 #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:135
Dem::DemBonesExt::DemBonesExt
DemBonesExt()
Constructor and setting default parameters.
Definition: DemBonesExt.h:95
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:113
Dem::DemBones::transAffineNorm
_Scalar transAffineNorm
[parameter] p-norm for bone translations affinity soft constraint, default = 4.0
Definition: DemBones.h:89
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:127
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:150
Dem::DemBonesExt::computeRTB
void computeRTB(int s, 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:127
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:122
Dem::DemBonesExt::preMulInv
MatrixX preMulInv
Inverse pre-multiplication matrices, [size] = [4*nS, 4*nB], preMulInv.block(4*s, 4*j,...
Definition: DemBonesExt.h:85
Dem::DemBonesExt::clear
void clear()
Clear all data.
Definition: DemBonesExt.h:101
Dem::DemBonesExt::fTime
Eigen::VectorXd fTime
Timestamps for bone transformations m, [size] = nS, fTime(k) is the timestamp of frame k.
Definition: DemBonesExt.h:73
Dem::DemBones::nS
int nS
Number of subjects, typically indexed by s.
Definition: DemBones.h:117
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:82
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:79
Dem::DemBonesExt::bindUpdate
int bindUpdate
Bind transformation update, 0=keep original, 1=set translations to p-norm centroids (using transAffin...
Definition: DemBonesExt.h:91
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:130
Dem::DemBones::nB
int nB
Number of bones, typically indexed by j.
Definition: DemBones.h:115
Dem::DemBonesExt::boneName
std::vector< std::string > boneName
Name of bones, [size] = nB, boneName(j) is the name bone of j.
Definition: DemBonesExt.h:76
Dem::DemBones
Smooth skinning decomposition with rigid bones and sparse, convex weights.
Definition: DemBones.h:65
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:88
Dem::DemBones::clear
void clear()
Clear all data.
Definition: DemBones.h:154