Skip to content

Commit e62e980

Browse files
committed
Add eigen backend
1 parent e009f4f commit e62e980

File tree

9 files changed

+370
-78
lines changed

9 files changed

+370
-78
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "dependencies/eigen"]
2+
path = dependencies/eigen
3+
url = [email protected]:libeigen/eigen.git

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.associations": {
3+
"*.rst": "reStructuredText",
4+
"*.usf": "cpp",
5+
"chrono": "cpp",
6+
"functional": "cpp"
7+
}
8+
}

CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ project(simple-2d-constraint-solver)
77

88
set(CMAKE_CXX_STANDARD 11)
99

10+
option(USE_EIGEN ON)
11+
1012
# ========================================================
1113
# GTEST
12-
1314
include(FetchContent)
1415
FetchContent_Declare(
1516
googletest
@@ -26,11 +27,17 @@ set_property(TARGET gtest PROPERTY FOLDER "gtest")
2627
set_property(TARGET gtest_main PROPERTY FOLDER "gtest")
2728

2829
# ========================================================
30+
if(USE_EIGEN)
31+
include_directories(dependencies/eigen)
32+
add_definitions(ATG_S2C_USE_EIGEN)
33+
endif(USE_EIGEN)
2934

3035
add_library(simple-2d-constraint-solver STATIC
36+
3137
# Source files
3238
src/rigid_body_system.cpp
33-
src/matrix.cpp
39+
src/matrix_custom.cpp
40+
src/matrix_eigen.cpp
3441
src/system_state.cpp
3542
src/utilities.cpp
3643
src/ode_solver.cpp
@@ -62,6 +69,8 @@ add_library(simple-2d-constraint-solver STATIC
6269
# Header files
6370
include/rigid_body_system.h
6471
include/matrix.h
72+
include/matrix_eigen.h
73+
include/matrix_custom.h
6574
include/utilities.h
6675
include/system_state.h
6776
include/ode_solver.h
@@ -93,10 +102,10 @@ add_library(simple-2d-constraint-solver STATIC
93102
)
94103

95104
# GTEST
96-
97105
enable_testing()
98106

99107
add_executable(simple-2d-constraint-solver-test
108+
100109
# Source files
101110
test/sanity_tests.cpp
102111
test/matrix_tests.cpp

dependencies/eigen

Submodule eigen added at e7248b2

include/matrix.h

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,10 @@
11
#ifndef ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_H
22
#define ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_H
33

4-
#include <assert.h>
5-
6-
namespace atg_scs {
7-
class Matrix {
8-
public:
9-
Matrix();
10-
Matrix(int width, int height, double value = 0.0);
11-
~Matrix();
12-
13-
void initialize(int width, int height, double value);
14-
void initialize(int width, int height);
15-
void resize(int width, int height);
16-
void destroy();
17-
18-
void set(const double *data);
19-
20-
__forceinline void set(int column, int row, double value) {
21-
assert(column >= 0 && column < m_width);
22-
assert(row >= 0 && row < m_height);
23-
24-
m_matrix[row][column] = value;
25-
}
26-
27-
__forceinline void add(int column, int row, double value) {
28-
assert(column >= 0 && column < m_width);
29-
assert(row >= 0 && row < m_height);
30-
31-
m_matrix[row][column] += value;
32-
}
33-
34-
__forceinline double get(int column, int row) {
35-
assert(column >= 0 && column < m_width);
36-
assert(row >= 0 && row < m_height);
37-
38-
return m_matrix[row][column];
39-
}
40-
41-
void set(Matrix *reference);
42-
43-
void multiply(Matrix &b, Matrix *target);
44-
void componentMultiply(Matrix &b, Matrix *target);
45-
void transposeMultiply(Matrix &b, Matrix *target);
46-
void leftScale(Matrix &scale, Matrix *target);
47-
void rightScale(Matrix &scale, Matrix *target);
48-
void scale(double s, Matrix *target);
49-
void subtract(Matrix &b, Matrix *target);
50-
void add(Matrix &b, Matrix *target);
51-
void negate(Matrix *target);
52-
bool equals(Matrix &b, double err = 1e-6);
53-
double vectorMagnitudeSquared() const;
54-
double dot(Matrix &b) const;
55-
56-
void madd(Matrix &b, double s);
57-
void pmadd(Matrix &b, double s);
58-
59-
void transpose(Matrix *target);
60-
int getWidth() const { return m_width; }
61-
int getHeight() const { return m_height; }
62-
63-
__forceinline void fastRowSwap(int a, int b) {
64-
double *temp = m_matrix[a];
65-
m_matrix[a] = m_matrix[b];
66-
m_matrix[b] = temp;
67-
}
68-
69-
protected:
70-
double **m_matrix;
71-
double *m_data;
72-
int m_width;
73-
int m_height;
74-
int m_capacityWidth;
75-
int m_capacityHeight;
76-
};
77-
} /* namespace atg_scs */
4+
#ifdef ATG_S2C_USE_EIGEN
5+
#include "matrix_eigen.h"
6+
#else
7+
#include "matrix_custom.h"
8+
#endif
789

7910
#endif /* ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_H */

include/matrix_custom.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#ifndef ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_CUSTOM_H
2+
#define ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_CUSTOM_H
3+
4+
#include <assert.h>
5+
6+
namespace atg_scs {
7+
class Matrix {
8+
public:
9+
Matrix();
10+
Matrix(int width, int height, double value = 0.0);
11+
~Matrix();
12+
13+
void initialize(int width, int height, double value);
14+
void initialize(int width, int height);
15+
void resize(int width, int height);
16+
void destroy();
17+
18+
void set(const double *data);
19+
20+
__forceinline void set(int column, int row, double value) {
21+
assert(column >= 0 && column < m_width);
22+
assert(row >= 0 && row < m_height);
23+
24+
m_matrix[row][column] = value;
25+
}
26+
27+
__forceinline void add(int column, int row, double value) {
28+
assert(column >= 0 && column < m_width);
29+
assert(row >= 0 && row < m_height);
30+
31+
m_matrix[row][column] += value;
32+
}
33+
34+
__forceinline double get(int column, int row) {
35+
assert(column >= 0 && column < m_width);
36+
assert(row >= 0 && row < m_height);
37+
38+
return m_matrix[row][column];
39+
}
40+
41+
void set(Matrix *reference);
42+
43+
void multiply(Matrix &b, Matrix *target);
44+
void componentMultiply(Matrix &b, Matrix *target);
45+
void transposeMultiply(Matrix &b, Matrix *target);
46+
void leftScale(Matrix &scale, Matrix *target);
47+
void rightScale(Matrix &scale, Matrix *target);
48+
void scale(double s, Matrix *target);
49+
void subtract(Matrix &b, Matrix *target);
50+
void add(Matrix &b, Matrix *target);
51+
void negate(Matrix *target);
52+
bool equals(Matrix &b, double err = 1e-6);
53+
double vectorMagnitudeSquared() const;
54+
double dot(Matrix &b) const;
55+
56+
void madd(Matrix &b, double s);
57+
void pmadd(Matrix &b, double s);
58+
59+
void transpose(Matrix *target);
60+
int getWidth() const { return m_width; }
61+
int getHeight() const { return m_height; }
62+
63+
__forceinline void fastRowSwap(int a, int b) {
64+
double *temp = m_matrix[a];
65+
m_matrix[a] = m_matrix[b];
66+
m_matrix[b] = temp;
67+
}
68+
69+
protected:
70+
double **m_matrix;
71+
double *m_data;
72+
int m_width;
73+
int m_height;
74+
int m_capacityWidth;
75+
int m_capacityHeight;
76+
};
77+
} /* namespace atg_scs */
78+
79+
#endif /* ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_H */

include/matrix_eigen.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#ifndef ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_EIGEN_H
2+
#define ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_EIGEN_H
3+
4+
#include <assert.h>
5+
6+
#ifdef ATG_S2C_USE_EIGEN
7+
#include "Eigen/Dense"
8+
9+
namespace atg_scs
10+
{
11+
class Matrix
12+
{
13+
typedef Eigen::MatrixXd MatrixType;
14+
15+
public:
16+
Matrix();
17+
Matrix(int width, int height, double value = 0.0);
18+
~Matrix();
19+
20+
void initialize(int width, int height, double value);
21+
void initialize(int width, int height);
22+
void resize(int width, int height);
23+
void destroy();
24+
25+
void set(const double *data);
26+
27+
__forceinline void set(int column, int row, double value)
28+
{
29+
m_matrix(row, column) = value;
30+
}
31+
32+
__forceinline void add(int column, int row, double value)
33+
{
34+
m_matrix(row, column) += value;
35+
}
36+
37+
__forceinline double get(int column, int row)
38+
{
39+
return m_matrix(row, column);
40+
}
41+
42+
void set(Matrix *reference);
43+
44+
void multiply(Matrix &b, Matrix *target);
45+
void componentMultiply(Matrix &b, Matrix *target);
46+
void transposeMultiply(Matrix &b, Matrix *target);
47+
void leftScale(Matrix &scale, Matrix *target);
48+
void rightScale(Matrix &scale, Matrix *target);
49+
void scale(double s, Matrix *target);
50+
void subtract(Matrix &b, Matrix *target);
51+
void add(Matrix &b, Matrix *target);
52+
void negate(Matrix *target);
53+
bool equals(Matrix &b, double err = 1e-6);
54+
double vectorMagnitudeSquared() const;
55+
double dot(Matrix &b) const;
56+
57+
void madd(Matrix &b, double s);
58+
void pmadd(Matrix &b, double s);
59+
60+
void transpose(Matrix *target);
61+
int getWidth() const { return m_matrix.cols(); }
62+
int getHeight() const { return m_matrix.rows(); }
63+
64+
__forceinline void fastRowSwap(int a, int b)
65+
{
66+
auto row_a = m_matrix.row(a);
67+
auto row_b = m_matrix.row(a);
68+
m_matrix.row(a) = row_b;
69+
m_matrix.row(b) = row_a;
70+
}
71+
72+
protected:
73+
MatrixType m_matrix;
74+
};
75+
} /* namespace atg_scs */
76+
77+
#endif
78+
79+
#endif /* ATG_SIMPLE_2D_CONSTRAINT_SOLVER_MATRIX_H */

src/matrix.cpp renamed to src/matrix_custom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../include/matrix.h"
1+
#include "../include/matrix_custom.h"
22

33
#include <algorithm>
44
#include <assert.h>

0 commit comments

Comments
 (0)