Skip to content

Commit

Permalink
feat: init dtkPhyMassSpringSolver, add dtkVector and dtkSparseMatrix …
Browse files Browse the repository at this point in the history
…(todo)

Squashed commit of the following:

commit 864048e6ef9102b9c7b10c646d88ea102c69e332
Author: For-Chance <chancewithchance@outlook.com>
Date:   Thu Aug 1 08:48:18 2024 +0800

    feat: init dtkPhyMassSpringSolver, add dtkVector and dtkSparseMatrix

commit 0bdd0c71c81123f5292cae781df9b2b99f16c1f3
Author: For-Chance <chancewithchance@outlook.com>
Date:   Sun Jul 28 22:29:44 2024 +0800

    fix: stupid gui bug
  • Loading branch information
For-Chance committed Aug 1, 2024
1 parent 02ffaa8 commit 3fd757d
Show file tree
Hide file tree
Showing 12 changed files with 1,796 additions and 1,023 deletions.
1,497 changes: 771 additions & 726 deletions src/dtkPhysMassSpring.cpp

Large diffs are not rendered by default.

452 changes: 237 additions & 215 deletions src/include/dtkPhysMassSpring.h

Large diffs are not rendered by default.

57 changes: 30 additions & 27 deletions src/include/dtkPhysSpring.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,36 @@
#include "dtkPhysMassPoint.h"

namespace dtk {
class dtkPhysSpring {
public:
dtkPhysSpring(dtkPhysMassPoint *p1, dtkPhysMassPoint *p2,
const double &stiff = 5, const double &damp = 0);

dtkPhysMassPoint *GetFirstVertex() { return mVerteces[0]; }
dtkPhysMassPoint *GetSecondVertex() { return mVerteces[1]; }

// 更新力到质点,迭代更新
bool Update(double timeslice, ItrMethod method = Euler, dtkID iteration = 0,
bool limitDeformation = false);
void SetStiffness(double newStiffness) { mStiffness = newStiffness; }
void SetDamp(double newDamp) { mDamp = newDamp; }
double GetOriLength() { return mOriLength; }
double GetStiffness() { return mStiffness; }
double GetDamp() { return mDamp; }

public:
virtual ~dtkPhysSpring();

private:
dtkPhysMassPoint *mVerteces[2]; /**< 弹簧两个质点 */

double mOriLength; /**< 长度 */
double mStiffness; /**< 刚度 */
double mDamp; /**< 阻尼 */
};
class dtkPhysSpring {
public:
dtkPhysSpring(dtkPhysMassPoint* p1, dtkPhysMassPoint* p2,
const double& stiff = 5, const double& damp = 0);

dtkPhysMassPoint* GetFirstVertex() { return mVerteces[0]; }
dtkPhysMassPoint* GetSecondVertex() { return mVerteces[1]; }

// 更新力到质点,迭代更新
bool Update(double timeslice, ItrMethod method = Euler, dtkID iteration = 0,
bool limitDeformation = false);
void SetStiffness(double newStiffness) { mStiffness = newStiffness; }
void SetDamp(double newDamp) { mDamp = newDamp; }
double GetOriLength() { return mOriLength; }
void SetRestLength(const double& newRestLength) { mRestLength = newRestLength; }
double GetRestLength() { return mRestLength; }
double GetStiffness() { return mStiffness; }
double GetDamp() { return mDamp; }

public:
virtual ~dtkPhysSpring();

private:
dtkPhysMassPoint* mVerteces[2]; /**< 弹簧两个质点 */

double mOriLength; /**< 原始长度 */
double mRestLength; /**< 静息长度 */
double mStiffness; /**< 刚度 */
double mDamp; /**< 阻尼 */
};
} // namespace dtk

#endif /* SIMPLEPHYSICSENGINE_DTKPHYSSPRING_H */
129 changes: 129 additions & 0 deletions src/math/include/dtkSparseMatrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* @file dtkSparseMatrix.h
* @brief dtkSparseMatrix 头文件
* @author Chance.H (chancewithchance@outlook.com)
* @version 1.0
* @date 2024-7-30
* @copyright MIT LICENSE
* https://github.com/Simple-XX/SimplePhysicsEngine
*/
#ifndef SIMPLEPHYSICSENGINE_DTKSPARSEMATRIX_H
#define SIMPLEPHYSICSENGINE_DTKSPARSEMATRIX_H
#include <vector>
#include <cassert>
#include <algorithm>

namespace dtk {

/**
* @class <dtkSparseMatrix>
* @brief 稀疏矩阵
* @author <>
* @note
* 实现稀疏矩阵
*/
template <class T>
class dtkSparseMatrix {
public:
typedef unsigned int Index;
typedef T ValueType;

struct Triplet {
Index row, col;
ValueType value;

Triplet(Index r, Index c, const ValueType& v) : row(r), col(c), value(v) {}
};

dtkSparseMatrix() : rows_(0), cols_(0) {}

dtkSparseMatrix(Index rows, Index cols) : rows_(rows), cols_(cols) {}

void resize(Index rows, Index cols) {
rows_ = rows;
cols_ = cols;
triplets_.clear();
}

void insert(Index row, Index col, const ValueType& value) {
assert(row < rows_ && col < cols_);
triplets_.emplace_back(row, col, value);
}

void setFromTriplets(const std::vector<Triplet>& triplets) {
triplets_ = triplets;
}

void reserve(Index reserveSize) {
triplets_.reserve(reserveSize);
}

void clear() {
triplets_.clear();
}

Index rows() const { return rows_; }
Index cols() const { return cols_; }
Index nonZeros() const { return triplets_.size(); }

const std::vector<Triplet>& triplets() const { return triplets_; }

dtkSparseMatrix<T> transpose() const {
dtkSparseMatrix<T> result(cols_, rows_);
for (const auto& triplet : triplets_) {
result.insert(triplet.col, triplet.row, triplet.value);
}
return result;
}

template <class VectorT>
std::vector<T> operator*(const VectorT& rhs) const {
assert(rhs.size() == cols_);
std::vector<T> result(rows_, T(0));
for (const auto& triplet : triplets_) {
result[triplet.row] += triplet.value * rhs[triplet.col];
}
return result;
}

dtkSparseMatrix<T> operator+(const dtkSparseMatrix<T>& rhs) const {
assert(rows_ == rhs.rows_ && cols_ == rhs.cols_);
dtkSparseMatrix<T> result(rows_, cols_);
result.triplets_ = triplets_;
for (const auto& triplet : rhs.triplets_) {
auto it = std::find_if(result.triplets_.begin(), result.triplets_.end(),
[&](const Triplet& t) { return t.row == triplet.row && t.col == triplet.col; });
if (it != result.triplets_.end()) {
it->value += triplet.value;
}
else {
result.triplets_.emplace_back(triplet.row, triplet.col, triplet.value);
}
}
return result;
}

dtkSparseMatrix<T> operator-(const dtkSparseMatrix<T>& rhs) const {
assert(rows_ == rhs.rows_ && cols_ == rhs.cols_);
dtkSparseMatrix<T> result(rows_, cols_);
result.triplets_ = triplets_;
for (const auto& triplet : rhs.triplets_) {
auto it = std::find_if(result.triplets_.begin(), result.triplets_.end(),
[&](const Triplet& t) { return t.row == triplet.row && t.col == triplet.col; });
if (it != result.triplets_.end()) {
it->value -= triplet.value;
}
else {
result.triplets_.emplace_back(triplet.row, triplet.col, -triplet.value);
}
}
return result;
}

private:
Index rows_, cols_;
std::vector<Triplet> triplets_;
};

} // namespace dtk
#endif /* SIMPLEPHYSICSENGINE_DTKSPARSEMATRIX_H */
Loading

0 comments on commit 3fd757d

Please sign in to comment.