Skip to content

Commit 7072987

Browse files
committed
It runs but transforms are incorrect
1 parent 2b5ebdc commit 7072987

File tree

2 files changed

+87
-105
lines changed

2 files changed

+87
-105
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/transforms/TransformMatrix.cpp

Lines changed: 0 additions & 82 deletions
This file was deleted.

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/transforms/TransformMatrix.h

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,23 @@ class TransformMatrix {
1313
public:
1414
using Shared = std::shared_ptr<const TransformMatrix>;
1515

16-
explicit TransformMatrix(size_t dimension);
17-
explicit TransformMatrix(
18-
jsi::Runtime &rt,
19-
const jsi::Value &value,
20-
size_t dimension);
21-
explicit TransformMatrix(const folly::dynamic &array, size_t dimension);
22-
16+
TransformMatrix() = default;
2317
virtual ~TransformMatrix() = default;
2418

25-
size_t getDimension() const;
26-
bool isSingular() const;
27-
bool normalize();
28-
void transpose();
19+
virtual size_t getDimension() const = 0;
20+
virtual size_t getSize() const = 0;
21+
22+
virtual bool isSingular() const = 0;
23+
virtual bool normalize() = 0;
24+
virtual void transpose() = 0;
2925
virtual double determinant() const = 0;
3026

31-
std::string toString() const;
32-
folly::dynamic toDynamic() const;
27+
virtual std::string toString() const = 0;
28+
virtual folly::dynamic toDynamic() const = 0;
3329

3430
virtual double &operator[](size_t index) = 0;
3531
virtual const double &operator[](size_t index) const = 0;
3632
virtual bool operator==(const TransformMatrix &other) const = 0;
37-
38-
protected:
39-
const size_t dimension_;
40-
const size_t size_;
4133
};
4234

4335
template <typename TDerived, size_t TDimension>
@@ -46,19 +38,36 @@ class TransformMatrixBase : public TransformMatrix {
4638
static constexpr size_t SIZE = TDimension * TDimension;
4739
using MatrixArray = std::array<double, SIZE>;
4840

49-
TransformMatrixBase() : TransformMatrix(TDimension) {}
41+
TransformMatrixBase() : TransformMatrix() {
42+
// Create an identity matrix
43+
for (size_t i = 0; i < TDimension; ++i) {
44+
matrix_[i * (TDimension + 1)] = 1;
45+
}
46+
}
47+
5048
explicit TransformMatrixBase(MatrixArray matrix)
51-
: TransformMatrix(TDimension), matrix_(std::move(matrix)) {}
49+
: TransformMatrix(), matrix_(std::move(matrix)) {}
50+
5251
explicit TransformMatrixBase(jsi::Runtime &rt, const jsi::Value &value)
53-
: TransformMatrix(rt, value, TDimension) {}
52+
: TransformMatrix() {
53+
const auto &array = value.asObject(rt).asArray(rt);
54+
for (size_t i = 0; i < SIZE; ++i) {
55+
matrix_[i] = array.getValueAtIndex(rt, i).asNumber();
56+
}
57+
}
58+
5459
explicit TransformMatrixBase(const folly::dynamic &array)
55-
: TransformMatrix(array, TDimension) {}
60+
: TransformMatrix() {
61+
for (size_t i = 0; i < SIZE; ++i) {
62+
matrix_[i] = array[i].asDouble();
63+
}
64+
}
5665

5766
TransformMatrixBase(const TransformMatrixBase &other)
58-
: TransformMatrix(TDimension), matrix_(other.matrix_) {}
67+
: TransformMatrix(), matrix_(other.matrix_) {}
5968

6069
TransformMatrixBase(TransformMatrixBase &&other) noexcept
61-
: TransformMatrix(TDimension), matrix_(std::move(other.matrix_)) {}
70+
: TransformMatrix(), matrix_(std::move(other.matrix_)) {}
6271

6372
static bool canConstruct(jsi::Runtime &rt, const jsi::Value &value) {
6473
if (!value.isObject()) {
@@ -92,6 +101,61 @@ class TransformMatrixBase : public TransformMatrix {
92101
return matrix_[index];
93102
}
94103

104+
size_t getDimension() const override {
105+
return TDimension;
106+
}
107+
108+
size_t getSize() const override {
109+
return SIZE;
110+
}
111+
112+
bool isSingular() const override {
113+
return determinant() == 0;
114+
}
115+
116+
bool normalize() override {
117+
const auto last = matrix_[SIZE - 1];
118+
if (last == 0) {
119+
return false;
120+
}
121+
if (last == 1) {
122+
return true;
123+
}
124+
125+
for (size_t i = 0; i < SIZE; ++i) {
126+
matrix_[i] /= last;
127+
}
128+
return true;
129+
}
130+
131+
void transpose() override {
132+
for (size_t i = 0; i < TDimension; ++i) {
133+
for (size_t j = 0; j < TDimension; ++j) {
134+
matrix_[i * TDimension + j] = matrix_[j * TDimension + i];
135+
}
136+
}
137+
}
138+
139+
std::string toString() const override {
140+
std::string result = "[";
141+
for (size_t i = 0; i < SIZE; ++i) {
142+
result += std::to_string(matrix_[i]);
143+
if (i < SIZE - 1) {
144+
result += ", ";
145+
}
146+
}
147+
result += "]";
148+
return result;
149+
}
150+
151+
folly::dynamic toDynamic() const override {
152+
folly::dynamic result = folly::dynamic::array;
153+
for (size_t i = 0; i < SIZE; ++i) {
154+
result.push_back(matrix_[i]);
155+
}
156+
return result;
157+
}
158+
95159
inline TDerived operator*(const TDerived &rhs) const {
96160
return TDerived(multiply(rhs));
97161
}

0 commit comments

Comments
 (0)