Skip to content

Commit

Permalink
Fixup ortho calculations and add ostream printers for geometry utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent bb633ca commit f9a6a2c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ TEST_F(AiksTest, CanRenderGroupOpacity) {
Paint alpha;
alpha.color = Color::Red().WithAlpha(0.5);

canvas.SaveLayer(alpha);
// canvas.SaveLayer(alpha);

canvas.DrawRect({000, 000, 100, 100}, red);
canvas.DrawRect({020, 020, 100, 100}, green);
Expand Down
43 changes: 0 additions & 43 deletions impeller/geometry/geometry_unittests.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,6 @@
#include "impeller/geometry/size.h"
#include "impeller/geometry/vector.h"

namespace std {

inline std::ostream& operator<<(std::ostream& out, const impeller::Matrix& m) {
out << "(";
for (size_t i = 0; i < 4u; i++) {
for (size_t j = 0; j < 4u; j++) {
out << m.e[i][j] << ",";
}
out << std::endl;
}
out << ")";
return out;
}

inline std::ostream& operator<<(std::ostream& out,
const impeller::Quaternion& q) {
out << "(" << q.x << ", " << q.y << ", " << q.z << ", " << q.w << ")";
return out;
}

template <class T>
inline std::ostream& operator<<(std::ostream& out,
const impeller::TSize<T>& s) {
out << "(" << s.width << ", " << s.height << ")";
return out;
}

template <class T>
inline std::ostream& operator<<(std::ostream& out,
const impeller::TPoint<T>& p) {
out << "(" << p.x << ", " << p.y << ")";
return out;
}

template <class T>
inline std::ostream& operator<<(std::ostream& out,
const impeller::TRect<T>& r) {
out << "(" << r.origin << ", " << r.size << ")";
return out;
}

} // namespace std

inline bool NumberNear(double a, double b) {
static const double epsilon = 1e-3;
return (a > (b - epsilon)) && (a < (b + epsilon));
Expand Down
21 changes: 19 additions & 2 deletions impeller/geometry/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cmath>
#include <optional>
#include <ostream>
#include <utility>

#include "impeller/geometry/matrix_decomposition.h"
Expand Down Expand Up @@ -257,8 +258,8 @@ struct Matrix {
static constexpr Matrix MakeOrthographic(TSize<T> size) {
// Per assumptions about NDC documented above.
const auto scale =
MakeScale({1.0f / static_cast<Scalar>(size.width),
-1.0f / static_cast<Scalar>(size.height), 1.0});
MakeScale({2.0f / static_cast<Scalar>(size.width),
-2.0f / static_cast<Scalar>(size.height), 1.0});
const auto translate = MakeTranslation({-1.0, 1.0, 0.5});
return translate * scale;
}
Expand All @@ -275,3 +276,19 @@ inline Vector4 operator*(const Vector4& v, const Matrix& m) {
}

} // namespace impeller

namespace std {

inline std::ostream& operator<<(std::ostream& out, const impeller::Matrix& m) {
out << "(";
for (size_t i = 0; i < 4u; i++) {
for (size_t j = 0; j < 4u; j++) {
out << m.e[i][j] << ",";
}
out << std::endl;
}
out << ")";
return out;
}

} // namespace std
12 changes: 12 additions & 0 deletions impeller/geometry/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <algorithm>
#include <cmath>
#include <ostream>
#include <string>

#include "impeller/geometry/scalar.h"
Expand Down Expand Up @@ -113,3 +114,14 @@ using Point = TPoint<Scalar>;
using IPoint = TPoint<int64_t>;

} // namespace impeller

namespace std {

template <class T>
inline std::ostream& operator<<(std::ostream& out,
const impeller::TPoint<T>& p) {
out << "(" << p.x << ", " << p.y << ")";
return out;
}

} // namespace std
7 changes: 0 additions & 7 deletions impeller/geometry/quaternion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,4 @@ Quaternion Quaternion::Slerp(const Quaternion& to, double time) const {
}
}

std::string Quaternion::ToString() const {
std::stringstream stream;
stream << "{" << x << ", "
<< ", " << y << ", " << z << ", " << w << "}";
return stream.str();
}

} // namespace impeller
16 changes: 13 additions & 3 deletions impeller/geometry/quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#pragma once

#include "vector.h"
#include <ostream>

#include "impeller/geometry/vector.h"

namespace impeller {

Expand Down Expand Up @@ -73,8 +75,16 @@ struct Quaternion {
bool operator!=(const Quaternion& o) const {
return x != o.x || y != o.y || z != o.z || w != o.w;
}

std::string ToString() const;
};

} // namespace impeller

namespace std {

inline std::ostream& operator<<(std::ostream& out,
const impeller::Quaternion& q) {
out << "(" << q.x << ", " << q.y << ", " << q.z << ", " << q.w << ")";
return out;
}

} // namespace std
12 changes: 12 additions & 0 deletions impeller/geometry/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <ostream>
#include <vector>

#include "impeller/geometry/point.h"
Expand Down Expand Up @@ -122,3 +123,14 @@ using Rect = TRect<Scalar>;
using IRect = TRect<int64_t>;

} // namespace impeller

namespace std {

template <class T>
inline std::ostream& operator<<(std::ostream& out,
const impeller::TRect<T>& r) {
out << "(" << r.origin << ", " << r.size << ")";
return out;
}

} // namespace std
12 changes: 12 additions & 0 deletions impeller/geometry/size.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cmath>
#include <limits>
#include <ostream>
#include <string>

#include "impeller/geometry/scalar.h"
Expand Down Expand Up @@ -100,3 +101,14 @@ using ISize = TSize<int64_t>;
static_assert(sizeof(Size) == 2 * sizeof(Scalar));

} // namespace impeller

namespace std {

template <class T>
inline std::ostream& operator<<(std::ostream& out,
const impeller::TSize<T>& s) {
out << "(" << s.width << ", " << s.height << ")";
return out;
}

} // namespace std

0 comments on commit f9a6a2c

Please sign in to comment.