| Flutter Impeller |

scalar.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_SCALAR_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_SCALAR_H_
7
8 #include <cfloat>
9 #include <ostream>
10 #include <type_traits>
11 #include <valarray>
12
13 #include "impeller/geometry/constants.h"
14
15 namespace impeller {
16
17 // NOLINTBEGIN(google-explicit-constructor)
18
19 using Scalar = float;
20
21 template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
22 constexpr T Absolute(const T& val) {
23 return val >= T{} ? val : -val;
24 }
25
26 template <>
27 constexpr Scalar Absolute<Scalar>(const float& val) {
28 return fabsf(val);
29 }
30
31 constexpr inline bool ScalarNearlyZero(Scalar x,
32 Scalar tolerance = kEhCloseEnough) {
33 return Absolute(x) <= tolerance;
34 }
35
36 constexpr inline bool ScalarNearlyEqual(Scalar x,
37 Scalar y,
38 Scalar tolerance = kEhCloseEnough) {
39 return ScalarNearlyZero(x - y, tolerance);
40 }
41
42 struct Degrees;
43
44 struct Radians {
45 Scalar radians = 0.0;
46
47 constexpr Radians() = default;
48
49 explicit constexpr Radians(Scalar p_radians) : radians(p_radians) {}
50
51 constexpr bool IsFinite() const { return std::isfinite(radians); }
52
53 constexpr Radians operator-() { return Radians{-radians}; }
54
55 constexpr Radians operator+(Radians r) {
56 return Radians{radians + r.radians};
57 }
58
59 constexpr Radians operator-(Radians r) {
60 return Radians{radians - r.radians};
61 }
62
63 constexpr bool operator>(Radians r) { return radians > r.radians; }
64
65 constexpr bool operator>=(Radians r) { return radians >= r.radians; }
66
67 constexpr bool operator==(Radians r) { return radians == r.radians; }
68
69 constexpr bool operator!=(Radians r) { return radians != r.radians; }
70
71 constexpr bool operator<=(Radians r) { return radians <= r.radians; }
72
73 constexpr bool operator<(Radians r) { return radians < r.radians; }
74 };
75
76 struct Degrees {
77 Scalar degrees = 0.0;
78
79 constexpr Degrees() = default;
80
81 explicit constexpr Degrees(Scalar p_degrees) : degrees(p_degrees) {}
82
83 constexpr operator Radians() const {
84 return Radians{degrees * kPi / 180.0f};
85 };
86
87 constexpr bool IsFinite() const { return std::isfinite(degrees); }
88
89 constexpr Degrees operator-() const { return Degrees{-degrees}; }
90
91 constexpr Degrees operator+(Degrees d) const {
92 return Degrees{degrees + d.degrees};
93 }
94
95 constexpr Degrees operator-(Degrees d) const {
96 return Degrees{degrees - d.degrees};
97 }
98
99 constexpr bool operator>(Degrees d) { return degrees > d.degrees; }
100
101 constexpr bool operator>=(Degrees d) { return degrees >= d.degrees; }
102
103 constexpr bool operator==(Degrees d) { return degrees == d.degrees; }
104
105 constexpr bool operator!=(Degrees d) { return degrees != d.degrees; }
106
107 constexpr bool operator<=(Degrees d) { return degrees <= d.degrees; }
108
109 constexpr bool operator<(Degrees d) { return degrees < d.degrees; }
110
111 constexpr Degrees GetPositive() const {
112 Scalar deg = std::fmod(degrees, 360.0f);
113 if (deg < 0.0f) {
114 deg += 360.0f;
115 }
116 return Degrees{deg};
117 }
118 };
119
120 // NOLINTEND(google-explicit-constructor)
121
122 } // namespace impeller
123
124 namespace std {
125
126 inline std::ostream& operator<<(std::ostream& out, const impeller::Degrees& d) {
127 return out << "Degrees(" << d.degrees << ")";
128 }
129
130 inline std::ostream& operator<<(std::ostream& out, const impeller::Radians& r) {
131 return out << "Radians(" << r.radians << ")";
132 }
133
134 } // namespace std
135
136 #endif // FLUTTER_IMPELLER_GEOMETRY_SCALAR_H_
x
int32_t x
Definition: dl_golden_unittests.cc:458
constants.h
impeller
Definition: allocation.cc:12
impeller::kPi
constexpr float kPi
Definition: constants.h:26
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:57
impeller::ScalarNearlyZero
constexpr bool ScalarNearlyZero(Scalar x, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:31
impeller::ScalarNearlyEqual
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:36
impeller::Absolute
constexpr T Absolute(const T &val)
Definition: scalar.h:22
impeller::Absolute< Scalar >
constexpr Scalar Absolute< Scalar >(const float &val)
Definition: scalar.h:27
std
Definition: comparable.h:95
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
impeller::Degrees
Definition: scalar.h:76
impeller::Degrees::operator==
constexpr bool operator==(Degrees d)
Definition: scalar.h:103
impeller::Degrees::operator-
constexpr Degrees operator-(Degrees d) const
Definition: scalar.h:95
impeller::Degrees::operator-
constexpr Degrees operator-() const
Definition: scalar.h:89
impeller::Degrees::degrees
Scalar degrees
Definition: scalar.h:77
impeller::Degrees::operator>
constexpr bool operator>(Degrees d)
Definition: scalar.h:99
impeller::Degrees::operator+
constexpr Degrees operator+(Degrees d) const
Definition: scalar.h:91
impeller::Degrees::operator<
constexpr bool operator<(Degrees d)
Definition: scalar.h:109
impeller::Degrees::IsFinite
constexpr bool IsFinite() const
Definition: scalar.h:87
impeller::Degrees::Degrees
constexpr Degrees()=default
impeller::Degrees::operator>=
constexpr bool operator>=(Degrees d)
Definition: scalar.h:101
impeller::Degrees::operator!=
constexpr bool operator!=(Degrees d)
Definition: scalar.h:105
impeller::Degrees::Degrees
constexpr Degrees(Scalar p_degrees)
Definition: scalar.h:81
impeller::Degrees::GetPositive
constexpr Degrees GetPositive() const
Definition: scalar.h:111
impeller::Degrees::operator<=
constexpr bool operator<=(Degrees d)
Definition: scalar.h:107
impeller::Radians
Definition: scalar.h:44
impeller::Radians::IsFinite
constexpr bool IsFinite() const
Definition: scalar.h:51
impeller::Radians::operator!=
constexpr bool operator!=(Radians r)
Definition: scalar.h:69
impeller::Radians::Radians
constexpr Radians()=default
impeller::Radians::operator-
constexpr Radians operator-(Radians r)
Definition: scalar.h:59
impeller::Radians::operator>=
constexpr bool operator>=(Radians r)
Definition: scalar.h:65
impeller::Radians::operator-
constexpr Radians operator-()
Definition: scalar.h:53
impeller::Radians::operator<=
constexpr bool operator<=(Radians r)
Definition: scalar.h:71
impeller::Radians::operator+
constexpr Radians operator+(Radians r)
Definition: scalar.h:55
impeller::Radians::operator==
constexpr bool operator==(Radians r)
Definition: scalar.h:67
impeller::Radians::radians
Scalar radians
Definition: scalar.h:45
impeller::Radians::operator<
constexpr bool operator<(Radians r)
Definition: scalar.h:73
impeller::Radians::Radians
constexpr Radians(Scalar p_radians)
Definition: scalar.h:49
impeller::Radians::operator>
constexpr bool operator>(Radians r)
Definition: scalar.h:63