| Flutter Impeller |

geometry_asserts.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_GEOMETRY_ASSERTS_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_GEOMETRY_ASSERTS_H_
7
8 #include <array>
9 #include <iostream>
10
11 #include "gtest/gtest.h"
12 #include "impeller/geometry/matrix.h"
13 #include "impeller/geometry/point.h"
14 #include "impeller/geometry/rect.h"
15 #include "impeller/geometry/size.h"
16 #include "impeller/geometry/vector.h"
17
18 inline bool NumberNear(double a, double b) {
19 static const double epsilon = 1e-3;
20 return (a > (b - epsilon)) && (a < (b + epsilon));
21 }
22
23 inline ::testing::AssertionResult MatrixNear(impeller::Matrix a,
24 impeller::Matrix b) {
25 auto equal = NumberNear(a.m[0], b.m[0]) //
26 && NumberNear(a.m[1], b.m[1]) //
27 && NumberNear(a.m[2], b.m[2]) //
28 && NumberNear(a.m[3], b.m[3]) //
29 && NumberNear(a.m[4], b.m[4]) //
30 && NumberNear(a.m[5], b.m[5]) //
31 && NumberNear(a.m[6], b.m[6]) //
32 && NumberNear(a.m[7], b.m[7]) //
33 && NumberNear(a.m[8], b.m[8]) //
34 && NumberNear(a.m[9], b.m[9]) //
35 && NumberNear(a.m[10], b.m[10]) //
36 && NumberNear(a.m[11], b.m[11]) //
37 && NumberNear(a.m[12], b.m[12]) //
38 && NumberNear(a.m[13], b.m[13]) //
39 && NumberNear(a.m[14], b.m[14]) //
40 && NumberNear(a.m[15], b.m[15]);
41
42 return equal ? ::testing::AssertionSuccess()
43 : ::testing::AssertionFailure()
44 << "Matrixes are not equal " << a << " " << b;
45 }
46
47 inline ::testing::AssertionResult QuaternionNear(impeller::Quaternion a,
48 impeller::Quaternion b) {
49 auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y) &&
50 NumberNear(a.z, b.z) && NumberNear(a.w, b.w);
51
52 return equal ? ::testing::AssertionSuccess()
53 : ::testing::AssertionFailure() << "Quaternions are not equal.";
54 }
55
56 inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b) {
57 auto equal = NumberNear(a.GetLeft(), b.GetLeft()) &&
58 NumberNear(a.GetTop(), b.GetTop()) &&
59 NumberNear(a.GetRight(), b.GetRight()) &&
60 NumberNear(a.GetBottom(), b.GetBottom());
61
62 return equal ? ::testing::AssertionSuccess()
63 : ::testing::AssertionFailure()
64 << "Rects are not equal (" << a << " " << b << ")";
65 }
66
67 inline ::testing::AssertionResult ColorNear(impeller::Color a,
68 impeller::Color b) {
69 auto equal = NumberNear(a.red, b.red) && NumberNear(a.green, b.green) &&
70 NumberNear(a.blue, b.blue) && NumberNear(a.alpha, b.alpha);
71
72 return equal ? ::testing::AssertionSuccess()
73 : ::testing::AssertionFailure() << "Colors are not equal.";
74 }
75
76 inline ::testing::AssertionResult PointNear(impeller::Point a,
77 impeller::Point b) {
78 auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y);
79
80 return equal ? ::testing::AssertionSuccess()
81 : ::testing::AssertionFailure()
82 << "Points are not equal (" << a << " " << b << ").";
83 }
84
85 inline ::testing::AssertionResult Vector3Near(impeller::Vector3 a,
86 impeller::Vector3 b) {
87 auto equal =
88 NumberNear(a.x, b.x) && NumberNear(a.y, b.y) && NumberNear(a.z, b.z);
89
90 return equal ? ::testing::AssertionSuccess()
91 : ::testing::AssertionFailure() << "Vector3s are not equal.";
92 }
93
94 inline ::testing::AssertionResult Vector4Near(impeller::Vector4 a,
95 impeller::Vector4 b) {
96 auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y) &&
97 NumberNear(a.z, b.z) && NumberNear(a.w, b.w);
98
99 return equal ? ::testing::AssertionSuccess()
100 : ::testing::AssertionFailure() << "Vector4s are not equal.";
101 }
102
103 inline ::testing::AssertionResult SizeNear(impeller::Size a, impeller::Size b) {
104 auto equal = NumberNear(a.width, b.width) && NumberNear(a.height, b.height);
105
106 return equal ? ::testing::AssertionSuccess()
107 : ::testing::AssertionFailure() << "Sizes are not equal.";
108 }
109
110 inline ::testing::AssertionResult Array4Near(std::array<uint8_t, 4> a,
111 std::array<uint8_t, 4> b) {
112 auto equal = NumberNear(a[0], b[0]) && NumberNear(a[1], b[1]) &&
113 NumberNear(a[2], b[2]) && NumberNear(a[3], b[3]);
114
115 return equal ? ::testing::AssertionSuccess()
116 : ::testing::AssertionFailure() << "Arrays are not equal.";
117 }
118
119 inline ::testing::AssertionResult ColorBufferNear(
120 std::vector<uint8_t> a,
121 std::vector<impeller::Color> b) {
122 if (a.size() != b.size() * 4) {
123 return ::testing::AssertionFailure()
124 << "Color buffer length does not match";
125 }
126 for (auto i = 0u; i < b.size(); i++) {
127 auto right = b[i].Premultiply().ToR8G8B8A8();
128 auto j = i * 4;
129 auto equal = NumberNear(a[j], right[0]) && NumberNear(a[j + 1], right[1]) &&
130 NumberNear(a[j + 2], right[2]) &&
131 NumberNear(a[j + 3], right[3]);
132 if (!equal) {
133 ::testing::AssertionFailure() << "Color buffers are not equal.";
134 }
135 }
136 return ::testing::AssertionSuccess();
137 }
138
139 inline ::testing::AssertionResult ColorsNear(std::vector<impeller::Color> a,
140 std::vector<impeller::Color> b) {
141 if (a.size() != b.size()) {
142 return ::testing::AssertionFailure() << "Colors length does not match";
143 }
144 for (auto i = 0u; i < b.size(); i++) {
145 auto equal =
146 NumberNear(a[i].red, b[i].red) && NumberNear(a[i].green, b[i].green) &&
147 NumberNear(a[i].blue, b[i].blue) && NumberNear(a[i].alpha, b[i].alpha);
148
149 if (!equal) {
150 ::testing::AssertionFailure() << "Colors are not equal.";
151 }
152 }
153 return ::testing::AssertionSuccess();
154 }
155
156 #define ASSERT_MATRIX_NEAR(a, b) ASSERT_PRED2(&::MatrixNear, a, b)
157 #define ASSERT_QUATERNION_NEAR(a, b) ASSERT_PRED2(&::QuaternionNear, a, b)
158 #define ASSERT_RECT_NEAR(a, b) ASSERT_PRED2(&::RectNear, a, b)
159 #define ASSERT_COLOR_NEAR(a, b) ASSERT_PRED2(&::ColorNear, a, b)
160 #define ASSERT_POINT_NEAR(a, b) ASSERT_PRED2(&::PointNear, a, b)
161 #define ASSERT_VECTOR3_NEAR(a, b) ASSERT_PRED2(&::Vector3Near, a, b)
162 #define ASSERT_VECTOR4_NEAR(a, b) ASSERT_PRED2(&::Vector4Near, a, b)
163 #define ASSERT_SIZE_NEAR(a, b) ASSERT_PRED2(&::SizeNear, a, b)
164 #define ASSERT_ARRAY_4_NEAR(a, b) ASSERT_PRED2(&::Array4Near, a, b)
165 #define ASSERT_COLOR_BUFFER_NEAR(a, b) ASSERT_PRED2(&::ColorBufferNear, a, b)
166 #define ASSERT_COLORS_NEAR(a, b) ASSERT_PRED2(&::ColorsNear, a, b)
167
168 #define EXPECT_MATRIX_NEAR(a, b) EXPECT_PRED2(&::MatrixNear, a, b)
169 #define EXPECT_QUATERNION_NEAR(a, b) EXPECT_PRED2(&::QuaternionNear, a, b)
170 #define EXPECT_RECT_NEAR(a, b) EXPECT_PRED2(&::RectNear, a, b)
171 #define EXPECT_COLOR_NEAR(a, b) EXPECT_PRED2(&::ColorNear, a, b)
172 #define EXPECT_POINT_NEAR(a, b) EXPECT_PRED2(&::PointNear, a, b)
173 #define EXPECT_VECTOR3_NEAR(a, b) EXPECT_PRED2(&::Vector3Near, a, b)
174 #define EXPECT_VECTOR4_NEAR(a, b) EXPECT_PRED2(&::Vector4Near, a, b)
175 #define EXPECT_SIZE_NEAR(a, b) EXPECT_PRED2(&::SizeNear, a, b)
176 #define EXPECT_ARRAY_4_NEAR(a, b) EXPECT_PRED2(&::Array4Near, a, b)
177 #define EXPECT_COLOR_BUFFER_NEAR(a, b) EXPECT_PRED2(&::ColorBufferNear, a, b)
178 #define EXPECT_COLORS_NEAR(a, b) EXPECT_PRED2(&::ColorsNear, a, b)
179
180 #endif // FLUTTER_IMPELLER_GEOMETRY_GEOMETRY_ASSERTS_H_
impeller::Matrix::m
Scalar m[16]
Definition: matrix.h:39
NumberNear
bool NumberNear(double a, double b)
Definition: geometry_asserts.h:18
impeller::Quaternion::z
Scalar z
Definition: quaternion.h:19
point.h
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Quaternion::w
Scalar w
Definition: quaternion.h:20
impeller::Color
Definition: color.h:124
impeller::Vector4
Definition: vector.h:232
MatrixNear
inline ::testing::AssertionResult MatrixNear(impeller::Matrix a, impeller::Matrix b)
Definition: geometry_asserts.h:23
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
ColorNear
inline ::testing::AssertionResult ColorNear(impeller::Color a, impeller::Color b)
Definition: geometry_asserts.h:67
Array4Near
inline ::testing::AssertionResult Array4Near(std::array< uint8_t, 4 > a, std::array< uint8_t, 4 > b)
Definition: geometry_asserts.h:110
impeller::Color::green
Scalar green
Definition: color.h:133
impeller::Vector3::x
Scalar x
Definition: vector.h:23
impeller::Quaternion::x
Scalar x
Definition: quaternion.h:17
matrix.h
impeller::TSize< Scalar >
impeller::Quaternion
Definition: quaternion.h:14
impeller::TRect::GetLeft
constexpr auto GetLeft() const
Definition: rect.h:318
Vector3Near
inline ::testing::AssertionResult Vector3Near(impeller::Vector3 a, impeller::Vector3 b)
Definition: geometry_asserts.h:85
impeller::Color::red
Scalar red
Definition: color.h:128
ColorBufferNear
inline ::testing::AssertionResult ColorBufferNear(std::vector< uint8_t > a, std::vector< impeller::Color > b)
Definition: geometry_asserts.h:119
impeller::Vector3::z
Scalar z
Definition: vector.h:25
impeller::Vector4::x
Scalar x
Definition: vector.h:235
PointNear
inline ::testing::AssertionResult PointNear(impeller::Point a, impeller::Point b)
Definition: geometry_asserts.h:76
impeller::Vector3::y
Scalar y
Definition: vector.h:24
QuaternionNear
inline ::testing::AssertionResult QuaternionNear(impeller::Quaternion a, impeller::Quaternion b)
Definition: geometry_asserts.h:47
ColorsNear
inline ::testing::AssertionResult ColorsNear(std::vector< impeller::Color > a, std::vector< impeller::Color > b)
Definition: geometry_asserts.h:139
impeller::TSize::width
Type width
Definition: size.h:22
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::Vector4::w
Scalar w
Definition: vector.h:238
impeller::TRect::GetRight
constexpr auto GetRight() const
Definition: rect.h:322
vector.h
RectNear
inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b)
Definition: geometry_asserts.h:56
impeller::Vector4::y
Scalar y
Definition: vector.h:236
rect.h
impeller::TPoint< Scalar >
impeller::saturated::b
SI b
Definition: saturated_math.h:87
Vector4Near
inline ::testing::AssertionResult Vector4Near(impeller::Vector4 a, impeller::Vector4 b)
Definition: geometry_asserts.h:94
impeller::Quaternion::y
Scalar y
Definition: quaternion.h:18
impeller::TRect::GetBottom
constexpr auto GetBottom() const
Definition: rect.h:324
impeller::TSize::height
Type height
Definition: size.h:23
impeller::Color::blue
Scalar blue
Definition: color.h:138
impeller::Vector4::z
Scalar z
Definition: vector.h:237
impeller::TRect::GetTop
constexpr auto GetTop() const
Definition: rect.h:320
impeller::TRect< Scalar >
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Vector3
Definition: vector.h:20
size.h
SizeNear
inline ::testing::AssertionResult SizeNear(impeller::Size a, impeller::Size b)
Definition: geometry_asserts.h:103