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 #pragma once
6 
7 #include <array>
8 #include <iostream>
9 
10 #include "gtest/gtest.h"
13 #include "impeller/geometry/rect.h"
14 #include "impeller/geometry/size.h"
16 
17 inline bool NumberNear(double a, double b) {
18  static const double epsilon = 1e-3;
19  return (a > (b - epsilon)) && (a < (b + epsilon));
20 }
21 
22 inline ::testing::AssertionResult MatrixNear(impeller::Matrix a,
23  impeller::Matrix b) {
24  auto equal = NumberNear(a.m[0], b.m[0]) //
25  && NumberNear(a.m[1], b.m[1]) //
26  && NumberNear(a.m[2], b.m[2]) //
27  && NumberNear(a.m[3], b.m[3]) //
28  && NumberNear(a.m[4], b.m[4]) //
29  && NumberNear(a.m[5], b.m[5]) //
30  && NumberNear(a.m[6], b.m[6]) //
31  && NumberNear(a.m[7], b.m[7]) //
32  && NumberNear(a.m[8], b.m[8]) //
33  && NumberNear(a.m[9], b.m[9]) //
34  && NumberNear(a.m[10], b.m[10]) //
35  && NumberNear(a.m[11], b.m[11]) //
36  && NumberNear(a.m[12], b.m[12]) //
37  && NumberNear(a.m[13], b.m[13]) //
38  && NumberNear(a.m[14], b.m[14]) //
39  && NumberNear(a.m[15], b.m[15]);
40 
41  return equal ? ::testing::AssertionSuccess()
42  : ::testing::AssertionFailure() << "Matrixes are not equal.";
43 }
44 
45 inline ::testing::AssertionResult QuaternionNear(impeller::Quaternion a,
47  auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y) &&
48  NumberNear(a.z, b.z) && NumberNear(a.w, b.w);
49 
50  return equal ? ::testing::AssertionSuccess()
51  : ::testing::AssertionFailure() << "Quaternions are not equal.";
52 }
53 
54 inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b) {
55  auto equal = NumberNear(a.origin.x, b.origin.x) &&
56  NumberNear(a.origin.y, b.origin.y) &&
57  NumberNear(a.size.width, b.size.width) &&
59 
60  return equal ? ::testing::AssertionSuccess()
61  : ::testing::AssertionFailure() << "Rects are not equal.";
62 }
63 
64 inline ::testing::AssertionResult ColorNear(impeller::Color a,
65  impeller::Color b) {
66  auto equal = NumberNear(a.red, b.red) && NumberNear(a.green, b.green) &&
67  NumberNear(a.blue, b.blue) && NumberNear(a.alpha, b.alpha);
68 
69  return equal ? ::testing::AssertionSuccess()
70  : ::testing::AssertionFailure() << "Colors are not equal.";
71 }
72 
73 inline ::testing::AssertionResult PointNear(impeller::Point a,
74  impeller::Point b) {
75  auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y);
76 
77  return equal ? ::testing::AssertionSuccess()
78  : ::testing::AssertionFailure() << "Points are not equal.";
79 }
80 
81 inline ::testing::AssertionResult Vector3Near(impeller::Vector3 a,
83  auto equal =
84  NumberNear(a.x, b.x) && NumberNear(a.y, b.y) && NumberNear(a.z, b.z);
85 
86  return equal ? ::testing::AssertionSuccess()
87  : ::testing::AssertionFailure() << "Vector3s are not equal.";
88 }
89 
90 inline ::testing::AssertionResult Vector4Near(impeller::Vector4 a,
92  auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y) &&
93  NumberNear(a.z, b.z) && NumberNear(a.w, b.w);
94 
95  return equal ? ::testing::AssertionSuccess()
96  : ::testing::AssertionFailure() << "Vector4s are not equal.";
97 }
98 
99 inline ::testing::AssertionResult SizeNear(impeller::Size a, impeller::Size b) {
100  auto equal = NumberNear(a.width, b.width) && NumberNear(a.height, b.height);
101 
102  return equal ? ::testing::AssertionSuccess()
103  : ::testing::AssertionFailure() << "Sizes are not equal.";
104 }
105 
106 inline ::testing::AssertionResult Array4Near(std::array<uint8_t, 4> a,
107  std::array<uint8_t, 4> b) {
108  auto equal = NumberNear(a[0], b[0]) && NumberNear(a[1], b[1]) &&
109  NumberNear(a[2], b[2]) && NumberNear(a[3], b[3]);
110 
111  return equal ? ::testing::AssertionSuccess()
112  : ::testing::AssertionFailure() << "Arrays are not equal.";
113 }
114 
115 inline ::testing::AssertionResult ColorBufferNear(
116  std::vector<uint8_t> a,
117  std::vector<impeller::Color> b) {
118  if (a.size() != b.size() * 4) {
119  return ::testing::AssertionFailure()
120  << "Color buffer length does not match";
121  }
122  for (auto i = 0u; i < b.size(); i++) {
123  auto right = b[i].Premultiply().ToR8G8B8A8();
124  auto j = i * 4;
125  auto equal = NumberNear(a[j], right[0]) && NumberNear(a[j + 1], right[1]) &&
126  NumberNear(a[j + 2], right[2]) &&
127  NumberNear(a[j + 3], right[3]);
128  if (!equal) {
129  ::testing::AssertionFailure() << "Color buffers are not equal.";
130  }
131  }
132  return ::testing::AssertionSuccess();
133 }
134 
135 inline ::testing::AssertionResult ColorsNear(std::vector<impeller::Color> a,
136  std::vector<impeller::Color> b) {
137  if (a.size() != b.size()) {
138  return ::testing::AssertionFailure() << "Colors length does not match";
139  }
140  for (auto i = 0u; i < b.size(); i++) {
141  auto equal =
142  NumberNear(a[i].red, b[i].red) && NumberNear(a[i].green, b[i].green) &&
143  NumberNear(a[i].blue, b[i].blue) && NumberNear(a[i].alpha, b[i].alpha);
144 
145  if (!equal) {
146  ::testing::AssertionFailure() << "Colors are not equal.";
147  }
148  }
149  return ::testing::AssertionSuccess();
150 }
151 
152 #define ASSERT_MATRIX_NEAR(a, b) ASSERT_PRED2(&::MatrixNear, a, b)
153 #define ASSERT_QUATERNION_NEAR(a, b) ASSERT_PRED2(&::QuaternionNear, a, b)
154 #define ASSERT_RECT_NEAR(a, b) ASSERT_PRED2(&::RectNear, a, b)
155 #define ASSERT_COLOR_NEAR(a, b) ASSERT_PRED2(&::ColorNear, a, b)
156 #define ASSERT_POINT_NEAR(a, b) ASSERT_PRED2(&::PointNear, a, b)
157 #define ASSERT_VECTOR3_NEAR(a, b) ASSERT_PRED2(&::Vector3Near, a, b)
158 #define ASSERT_VECTOR4_NEAR(a, b) ASSERT_PRED2(&::Vector4Near, a, b)
159 #define ASSERT_SIZE_NEAR(a, b) ASSERT_PRED2(&::SizeNear, a, b)
160 #define ASSERT_ARRAY_4_NEAR(a, b) ASSERT_PRED2(&::Array4Near, a, b)
161 #define ASSERT_COLOR_BUFFER_NEAR(a, b) ASSERT_PRED2(&::ColorBufferNear, a, b)
162 #define ASSERT_COLORS_NEAR(a, b) ASSERT_PRED2(&::ColorsNear, a, b)
impeller::Matrix::m
Scalar m[16]
Definition: matrix.h:38
NumberNear
bool NumberNear(double a, double b)
Definition: geometry_asserts.h:17
impeller::Quaternion::z
Scalar z
Definition: quaternion.h:18
impeller::TRect::size
TSize< Type > size
Definition: rect.h:24
point.h
impeller::TPoint::y
Type y
Definition: point.h:24
impeller::Quaternion::w
Scalar w
Definition: quaternion.h:19
impeller::Color
Definition: color.h:122
impeller::Vector4
Definition: vector.h:229
MatrixNear
inline ::testing::AssertionResult MatrixNear(impeller::Matrix a, impeller::Matrix b)
Definition: geometry_asserts.h:22
impeller::Color::alpha
Scalar alpha
Definition: color.h:141
ColorNear
inline ::testing::AssertionResult ColorNear(impeller::Color a, impeller::Color b)
Definition: geometry_asserts.h:64
Array4Near
inline ::testing::AssertionResult Array4Near(std::array< uint8_t, 4 > a, std::array< uint8_t, 4 > b)
Definition: geometry_asserts.h:106
impeller::Color::green
Scalar green
Definition: color.h:131
impeller::Vector3::x
Scalar x
Definition: vector.h:20
impeller::Quaternion::x
Scalar x
Definition: quaternion.h:16
matrix.h
impeller::TSize
Definition: size.h:18
impeller::Quaternion
Definition: quaternion.h:13
Vector3Near
inline ::testing::AssertionResult Vector3Near(impeller::Vector3 a, impeller::Vector3 b)
Definition: geometry_asserts.h:81
impeller::Color::red
Scalar red
Definition: color.h:126
ColorBufferNear
inline ::testing::AssertionResult ColorBufferNear(std::vector< uint8_t > a, std::vector< impeller::Color > b)
Definition: geometry_asserts.h:115
impeller::Vector3::z
Scalar z
Definition: vector.h:22
impeller::TRect::origin
TPoint< Type > origin
Definition: rect.h:23
impeller::Vector4::x
Scalar x
Definition: vector.h:232
PointNear
inline ::testing::AssertionResult PointNear(impeller::Point a, impeller::Point b)
Definition: geometry_asserts.h:73
impeller::Vector3::y
Scalar y
Definition: vector.h:21
QuaternionNear
inline ::testing::AssertionResult QuaternionNear(impeller::Quaternion a, impeller::Quaternion b)
Definition: geometry_asserts.h:45
ColorsNear
inline ::testing::AssertionResult ColorsNear(std::vector< impeller::Color > a, std::vector< impeller::Color > b)
Definition: geometry_asserts.h:135
impeller::TSize::width
Type width
Definition: size.h:21
impeller::TPoint::x
Type x
Definition: point.h:23
impeller::Vector4::w
Scalar w
Definition: vector.h:235
vector.h
RectNear
inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b)
Definition: geometry_asserts.h:54
impeller::Vector4::y
Scalar y
Definition: vector.h:233
rect.h
impeller::TPoint< Scalar >
Vector4Near
inline ::testing::AssertionResult Vector4Near(impeller::Vector4 a, impeller::Vector4 b)
Definition: geometry_asserts.h:90
impeller::Quaternion::y
Scalar y
Definition: quaternion.h:17
impeller::TSize::height
Type height
Definition: size.h:22
impeller::Color::blue
Scalar blue
Definition: color.h:136
impeller::Vector4::z
Scalar z
Definition: vector.h:234
impeller::TRect< Scalar >
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::Vector3
Definition: vector.h:17
size.h
SizeNear
inline ::testing::AssertionResult SizeNear(impeller::Size a, impeller::Size b)
Definition: geometry_asserts.h:99