Fixup ortho calculations and add ostream printers for geometry utils.

This commit is contained in:
Chinmay Garde 2021-11-28 12:27:45 -08:00 committed by Dan Field
parent 8ac160af41
commit 5c0de80dd1
8 changed files with 69 additions and 56 deletions

View File

@ -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);

View File

@ -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));

View File

@ -6,6 +6,7 @@
#include <cmath>
#include <optional>
#include <ostream>
#include <utility>
#include "impeller/geometry/matrix_decomposition.h"
@ -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;
}
@ -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

View File

@ -6,6 +6,7 @@
#include <algorithm>
#include <cmath>
#include <ostream>
#include <string>
#include "impeller/geometry/scalar.h"
@ -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

View File

@ -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

View File

@ -4,7 +4,9 @@
#pragma once
#include "vector.h"
#include <ostream>
#include "impeller/geometry/vector.h"
namespace impeller {
@ -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

View File

@ -4,6 +4,7 @@
#pragma once
#include <ostream>
#include <vector>
#include "impeller/geometry/point.h"
@ -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

View File

@ -6,6 +6,7 @@
#include <cmath>
#include <limits>
#include <ostream>
#include <string>
#include "impeller/geometry/scalar.h"
@ -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