-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: init dtkPhyMassSpringSolver, add dtkVector and dtkSparseMatrix …
…(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
1 parent
02ffaa8
commit 3fd757d
Showing
12 changed files
with
1,796 additions
and
1,023 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
Oops, something went wrong.