【CG物理模拟系列】弹性体模拟--Position-based法之Shape Matching(代码实现)

本文深入探讨了在计算机图形学(CG)领域中,如何使用Position-based方法进行弹性体模拟,特别是Shape Matching技术。通过分析`shape_matching.h`和`shape_matching.cpp`两个关键文件的代码实现,阐述了这一技术在游戏引擎和游戏开发中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Shape Matching法核心代码讲解
这次讲讲Shape Matching 算法的具体代码实现。
主要写在shape_matching.h和shape_matching.cpp两个文件中,以代码注释的形式进行讲解。

shape_matching.h

头文件中主要定义了一个ShapeMatching类及其 变量声明和函数声明,之后是一个雅可比行列式计算特征值和极分解函数,用于求出我们上一篇提到的变形行列A中的旋转分量R。
typedef void(*CollisionFunc)(Vec3&, Vec3&, Vec3&, int);

class ShapeMatching {
protected:
	//shape data
	int m_iNumOfVertices;							// 顶点总数

	vector<Vec3> m_vOrgPos;							//空间内顶点的初始坐标
	vector<Vec3> m_vCurPos;							//顶点的现坐标
	vector<Vec3> m_vNewPos;							//顶点的下一个坐标
	vector<Vec3> m_vGoalPos;						//目标点坐标
	vector<double> m_vMass;							//质量
	vector<Vec3> m_vVel;							//速度
	vector<bool> m_vFix;							//用于标识粒子是否是固定的

	//simulation parameters
	double m_fDt;								//time step
	Vec3 m_vMin, m_vMax;							//模拟空间的大小
	Vec3 m_vGraviity;							//重力加速度							

	double m_fAlpha;							//stiffness参数,大小在 0~1间
	double m_fBeta;								//deformation 参数,大小在 0~1间

	bool m_bLinearDeformation;						//< Linear/Quadratic deformation切换flag
	bool m_bVolumeConservation;						//volume conservation

	int m_iObjectNum;							//物体索引number
	CollisionFunc m_Collision;
public:
	ShapeMatching(int obj);
	~ShapeMatching();

	void Clear();
	void AddVertex(const Vec3 &pos, double mass);				//添加顶点
	void AddCluster(const vector<int> &list);				//添加cluster

	void Update();

	void SetTimeStep(double dt) { m_fDt = dt; };
	void SetSimulationSpace(Vec3 minp, Vec3 maxp) { m_vMin = minp; m_vMax = maxp; };
	void SetStiffness(double alpha, double beta) { m_fAlpha = alpha; m_fBeta = beta; };

	void SetCollisionFunc(CollisionFunc func) { m_Collision = func; };

	int GetNumOfVertices() const { return m_iNumOfVertices; }
	const Vec3& GetVertexPos(int i) { return m_vCurPos[i]; }
	double GetMass(int i) { return m_vMass[i]; }

	void FixVertex(int i, const Vec3 &pos);
	void UnFixVertex(int i);
	bool IsFixed(int i) { return m_vFix[i]; }
protected:
	//初始化
	void initialize(void);

	//shape matching 算法
	void calExternalForces(double dt);							//计算外力
	void calCollision(double dt);								//碰撞计算
	void shapeMatchingFun(Clust
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值