PeriDyno 1.2.1
Loading...
Searching...
No Matches
HierarchicalModel.h
Go to the documentation of this file.
1#pragma once
3
4#include "AnimationCurve.h"
5#include <iterator>
6#include <random>
7#include "TextureMesh.h"
8#include "Quat.h"
10#include "Topology/SkinInfo.h"
11
12#define ERRORTIME -2321.51
13
14namespace dyno
15{
16 class ModelObject;
17 class Bone;
18
19 class ModelObject : public Object
20 {
21 public:
22
25 bool operator==(const ModelObject& model);
26
27 public:
28
29 std::string name;
30 Mat4f localTransform = Mat4f::identityMatrix();
31 Mat4f worldTransform = Mat4f::identityMatrix();
37
38 std::vector<std::shared_ptr<ModelObject>> child;
39 std::vector<std::shared_ptr<ModelObject>> parent;// bone - parent - root
40 int id = -1;
41 };
42
43
44 class Bone : public ModelObject
45 {
46 public:
47
48 Bone() {};
49
50 public:
51
52 Mat4f inverseBindMatrix = Mat4f::identityMatrix();
53 };
54
55
56 class MeshInfo : public ModelObject
57 {
58 public:
60
62
63 void resizeSkin(int size);
64
65 unsigned int size() { return vertices.size(); }
66
67
68
69
70 std::vector<Vec3f> points;
71 std::vector<Vec3f> vertices;
72
73 std::vector<int> verticeId_pointId;
74 std::map<int,std::vector<int>> pointId_verticeId;
75
76
77 std::vector<Vec3f> normals;
78 std::vector<Vec2f> texcoords;
79 std::vector<Vec3f> verticesColor;
80 std::vector<std::vector<TopologyModule::Triangle>> facegroup_triangles;
81 std::vector<std::vector<TopologyModule::Triangle>> facegroup_normalIndex;
82 std::vector<CArrayList<uint>> facegroup_polygons;
83
84 std::vector<std::shared_ptr<Material>> materials;
85
86 std::vector<TAlignedBox3D<Real>> boundingBox;
87 std::vector<Transform3f> boundingTransform;
88
89 std::vector<Vec4f> boneIndices0;
90 std::vector<Vec4f> boneWeights0;
91 std::vector<Vec4f> boneIndices1;
92 std::vector<Vec4f> boneWeights1;
93 std::vector<Vec4f> boneIndices2;
94 std::vector<Vec4f> boneWeights2;
95
96 };
97
98
100 {
101 public:
103
105
106 void clear();
107
109 int findMeshIndexByName(std::string name);
110 int findObjectIndexByName(std::string name);
111 void pushBackBone(std::shared_ptr<Bone> bone);
112 void pushBackMesh(std::shared_ptr<MeshInfo> mesh);
113 std::shared_ptr<ModelObject> getObjectByName(std::string name);
114 int getObjIndexByName(std::string name);
115 int getBoneIndexByName(std::string name);
120 Real getVectorDataByTime(std::vector<Real> data, std::vector<Real> timeCode, Real time);
121 int findMaxSmallerIndex(const std::vector<float>& arr, float v);
122 std::vector<std::shared_ptr<Bone>>& getBones() { return mBones; }
124 DArray<Vec3f>& intialPosition,
125 DArray<Vec3f>& worldPosition,
126 DArray<Mat4f>& joint_inverseBindMatrix,
127 DArray<Mat4f>& WorldMatrix,
128 DArray<Vec4f>& bind_joints_0,
129 DArray<Vec4f>& bind_joints_1,
130 DArray<Vec4f>& bind_joints_2,
131 DArray<Vec4f>& weights_0,
132 DArray<Vec4f>& weights_1,
133 DArray<Vec4f>& weights_2,
134 Mat4f transform,
135 bool isNormal,
136 Vec2u range
137 );
138
140 DArray<Vec3f>& intialVertices,
141 DArray<Vec3f>& Vertices,
142 DArray<Mat4f>& joint_inverseBindMatrix,
143 DArray<Mat4f>& WorldMatrix,
144
145 DArrayList<int>& point2Vertice,
146 DArray<Vec4f>& bind_joints_0,
147 DArray<Vec4f>& bind_joints_1,
148 DArray<Vec4f>& bind_joints_2,
149 DArray<Vec4f>& weights_0,
150 DArray<Vec4f>& weights_1,
151 DArray<Vec4f>& weights_2,
152
153 Mat4f transform,
154 bool isNormal,
155
156 Vec2u range
157 );
158
160 DArray<Vec3f>& intialVertices,
161 DArray<Vec3f>& Vertices,
162 DArray<Mat4f>& joint_inverseBindMatrix,
163 DArray<Mat4f>& WorldMatrix,
164
165 DArrayList<int>& point2Vertice,
166 DArray<Vec4f>& bind_joints_0,
167 DArray<Vec4f>& bind_joints_1,
168 DArray<Vec4f>& bind_joints_2,
169 DArray<Vec4f>& weights_0,
170 DArray<Vec4f>& weights_1,
171 DArray<Vec4f>& weights_2,
172
173 Mat4f transform,
174 bool isNormal,
175
176 Vec2u range
177 );
178
180 DArray<Vec3f>& initialNormal,
181 DArray<Mat4f>& joint_inverseBindMatrix,
182 DArray<Mat4f>& WorldMatrix,
183
184 DArrayList<int>& point2Vertice,
185 DArray<Vec4f>& bind_joints_0,
186 DArray<Vec4f>& bind_joints_1,
187 DArray<Vec4f>& bind_joints_2,
188 DArray<Vec4f>& weights_0,
189 DArray<Vec4f>& weights_1,
190 DArray<Vec4f>& weights_2,
191
192 Vec2u range
193 );
194
196
198
200
201 void updateSkinData(std::shared_ptr<TextureMesh> texMesh);
202
203 Mat4f createLocalTransform(std::shared_ptr<ModelObject> object);
204
205 void coutMatrix(int id, Mat4f c)
206 {
207 printf("********** step: %d ***********\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n ***********************\n\n",
208 id,
209 c(0, 0), c(0, 1), c(0, 2), c(0, 3),
210 c(1, 0), c(1, 1), c(1, 2), c(1, 3),
211 c(2, 0), c(2, 1), c(2, 2), c(2, 3),
212 c(3, 0), c(3, 1), c(3, 2), c(3, 3)
213 );
214 }
215
217
218 template< typename Vec3f, typename Mat4f>
220 DArray<Vec3f>& intialPosition,
221 DArray<Vec3f>& worldPosition,
222 DArray<Vec3f>& intialNormal,
223 DArray<Vec3f>& Normal,
224 Mat4f& WorldMatrix
225 );
226
227
228 template< typename Vec3f, typename Mat4f>
230 DArray<Vec3f>& intialPosition,
231 DArray<Vec3f>& worldPosition,
232 DArray<Vec3f>& intialNormal,
233 DArray<Vec3f>& Normal,
234 DArray<Mat4f>& WorldMatrix,
235 DArray<uint>& vertexId_shape,
236 DArray<int>& shapeId_MeshId
237 );
238
239 template< typename Vec3f, typename uint>
241 DArray<Vec3f>& finalPos,
242 DArray<uint>& shapeId,
244 );
245
246 std::vector<std::shared_ptr<MeshInfo>>& getMeshes() { return mMeshes; }
247
248 std::vector<Mat4f> getObjectWorldMatrix()
249 {
250 this->updateMeshWorldMatrix();
251 auto objects = mModelObjects;
252 int maxId = 0;
253 for (auto obj : objects)
254 {
255 maxId = maxId >= obj->id ? maxId : obj->id;
256 }
257 std::vector<Mat4f> meshWorldMatrix(maxId + 1);
258
259 for (auto obj : objects)
260 {
261 if (obj->id != -1)
262 meshWorldMatrix[obj->id] = obj->worldTransform;
263 }
264 return meshWorldMatrix;
265 }
266
268 std::vector<std::shared_ptr<Shape>>& shapes,
269 DArray<Vec3f>& Position,
270 DArray<Vec3f>& Normal,
271 DArray<int>* vertices2Point = nullptr);
272
274
275 std::shared_ptr<JointAnimationInfo>& getJointAnimation() { return mJointAnimationData; }
276
277
278 private:
279
280 void buildTree(std::string& str, const std::vector<std::shared_ptr<ModelObject>>& child, uint level)
281 {
282 str.append("\n");
283
284 for (auto chi : child)
285 {
286 for (size_t i = 0; i < level; i++)
287 {
288 str.append(" ");
289 }
290 str.append("-");
291 str.append(chi->name);
292 buildTree(str, chi->child, level + 1);
293 }
294 }
295
296
297
298 public:
299
300 std::vector<std::shared_ptr<ModelObject>> mModelObjects;
301 std::vector<std::shared_ptr<MeshInfo>> mMeshes;
302 std::vector<std::shared_ptr<Bone>> mBones;
303 std::vector<Vec3f> mBoneRotations;
304 std::vector<Vec3f> mBoneTranslations;
305 std::vector<Vec3f> mBoneScales;
306 std::vector<Mat4f> mBoneWorldMatrix;
307 std::vector<Mat4f> mBoneInverseBindMatrix;
308 std::vector<Mat4f> mBoneLocalMatrix;
309
310 std::shared_ptr<JointInfo> mJointData;
311 std::shared_ptr<SkinInfo> mSkinData;
312 std::shared_ptr<JointAnimationInfo> mJointAnimationData;
313
314 float mTimeStart = -1;
315 float mTimeEnd = -1;
316
317
318 private:
319 Real lerp(Real v0, Real v1, float weight);
321 };
322
323
324}
#define ERRORTIME
Implementation of quaternion.
double Real
Definition Typedef.inl:23
Mat4f inverseBindMatrix
void getVerticesNormalInBindPose(DArray< Vec3f > &initialNormal, DArray< Mat4f > &joint_inverseBindMatrix, DArray< Mat4f > &WorldMatrix, DArrayList< int > &point2Vertice, DArray< Vec4f > &bind_joints_0, DArray< Vec4f > &bind_joints_1, DArray< Vec4f > &bind_joints_2, DArray< Vec4f > &weights_0, DArray< Vec4f > &weights_1, DArray< Vec4f > &weights_2, Vec2u range)
std::vector< Mat4f > mBoneLocalMatrix
void skinVerticesAnimation(DArray< Vec3f > &intialVertices, DArray< Vec3f > &Vertices, DArray< Mat4f > &joint_inverseBindMatrix, DArray< Mat4f > &WorldMatrix, DArrayList< int > &point2Vertice, DArray< Vec4f > &bind_joints_0, DArray< Vec4f > &bind_joints_1, DArray< Vec4f > &bind_joints_2, DArray< Vec4f > &weights_0, DArray< Vec4f > &weights_1, DArray< Vec4f > &weights_2, Mat4f transform, bool isNormal, Vec2u range)
Real lerp(Real v0, Real v1, float weight)
void c_skinVerticesAnimation(DArray< Vec3f > &intialVertices, DArray< Vec3f > &Vertices, DArray< Mat4f > &joint_inverseBindMatrix, DArray< Mat4f > &WorldMatrix, DArrayList< int > &point2Vertice, DArray< Vec4f > &bind_joints_0, DArray< Vec4f > &bind_joints_1, DArray< Vec4f > &bind_joints_2, DArray< Vec4f > &weights_0, DArray< Vec4f > &weights_1, DArray< Vec4f > &weights_2, Mat4f transform, bool isNormal, Vec2u range)
std::shared_ptr< JointAnimationInfo > mJointAnimationData
std::shared_ptr< SkinInfo > mSkinData
int findMeshIndexByName(std::string name)
void buildTree(std::string &str, const std::vector< std::shared_ptr< ModelObject > > &child, uint level)
std::vector< Mat4f > mBoneWorldMatrix
void shapeTransform(DArray< Vec3f > &intialPosition, DArray< Vec3f > &worldPosition, DArray< Vec3f > &intialNormal, DArray< Vec3f > &Normal, DArray< Mat4f > &WorldMatrix, DArray< uint > &vertexId_shape, DArray< int > &shapeId_MeshId)
void skinAnimation(DArray< Vec3f > &intialPosition, DArray< Vec3f > &worldPosition, DArray< Mat4f > &joint_inverseBindMatrix, DArray< Mat4f > &WorldMatrix, DArray< Vec4f > &bind_joints_0, DArray< Vec4f > &bind_joints_1, DArray< Vec4f > &bind_joints_2, DArray< Vec4f > &weights_0, DArray< Vec4f > &weights_1, DArray< Vec4f > &weights_2, Mat4f transform, bool isNormal, Vec2u range)
void flipNormal(DArray< Vec3f > &Normal)
std::vector< std::shared_ptr< MeshInfo > > mMeshes
std::shared_ptr< ModelObject > getObjectByName(std::string name)
void coutMatrix(int id, Mat4f c)
std::vector< Mat4f > mBoneInverseBindMatrix
std::vector< std::shared_ptr< Bone > > mBones
void computeTexMeshVerticesNormal(std::vector< std::shared_ptr< Shape > > &shapes, DArray< Vec3f > &Position, DArray< Vec3f > &Normal, DArray< int > *vertices2Point=nullptr)
void shapeToCenter(DArray< Vec3f > &iniPos, DArray< Vec3f > &finalPos, DArray< uint > &shapeId, DArray< Vec3f > &t)
int findObjectIndexByName(std::string name)
std::shared_ptr< JointAnimationInfo > & getJointAnimation()
int getObjIndexByName(std::string name)
std::vector< Vec3f > mBoneScales
std::vector< Vec3f > mBoneRotations
int findMaxSmallerIndex(const std::vector< float > &arr, float v)
void textureMeshTransform(DArray< Vec3f > &intialPosition, DArray< Vec3f > &worldPosition, DArray< Vec3f > &intialNormal, DArray< Vec3f > &Normal, Mat4f &WorldMatrix)
std::shared_ptr< JointInfo > mJointData
void updateSkinData(std::shared_ptr< TextureMesh > texMesh)
void updatePoint2Vertice(DArrayList< int > &d_p2v, DArray< int > &d_v2p)
Mat4f createLocalTransform(std::shared_ptr< ModelObject > object)
Real getVectorDataByTime(std::vector< Real > data, std::vector< Real > timeCode, Real time)
std::vector< std::shared_ptr< ModelObject > > mModelObjects
std::vector< std::shared_ptr< MeshInfo > > & getMeshes()
void updateWorldTransformByKeyFrame(Real time)
void pushBackMesh(std::shared_ptr< MeshInfo > mesh)
void pushBackBone(std::shared_ptr< Bone > bone)
int getBoneIndexByName(std::string name)
std::vector< Vec3f > mBoneTranslations
std::vector< std::shared_ptr< Bone > > & getBones()
std::vector< Mat4f > getObjectWorldMatrix()
void resizeSkin(int size)
std::vector< Vec3f > normals
std::vector< Vec4f > boneIndices0
std::vector< Vec3f > vertices
std::vector< CArrayList< uint > > facegroup_polygons
std::map< int, std::vector< int > > pointId_verticeId
std::vector< Vec4f > boneIndices2
std::vector< Vec2f > texcoords
std::vector< Vec4f > boneWeights2
std::vector< std::vector< TopologyModule::Triangle > > facegroup_triangles
std::vector< Vec4f > boneWeights0
std::vector< Vec4f > boneWeights1
std::vector< std::shared_ptr< Material > > materials
std::vector< std::vector< TopologyModule::Triangle > > facegroup_normalIndex
std::vector< int > verticeId_pointId
std::vector< Vec4f > boneIndices1
std::vector< Vec3f > verticesColor
std::vector< Vec3f > points
std::vector< Transform3f > boundingTransform
unsigned int size()
std::vector< TAlignedBox3D< Real > > boundingBox
bool operator==(const ModelObject &model)
std::vector< std::shared_ptr< ModelObject > > parent
std::vector< std::shared_ptr< ModelObject > > child
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::GPU > DArray
Definition Array.inl:89
Vector< uint32_t, 2 > Vec2u
Definition Vector2D.h:83
ArrayList< ElementType, DeviceType::GPU > DArrayList
Definition ArrayList.inl:83
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
unsigned int uint
Definition VkProgram.h:14
SquareMatrix< float, 4 > Mat4f
Definition Matrix4x4.h:85