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 
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_
int32_t x
constexpr float kPi
Definition: constants.h:26
float Scalar
Definition: scalar.h:19
constexpr float kEhCloseEnough
Definition: constants.h:57
constexpr bool ScalarNearlyZero(Scalar x, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:31
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:36
constexpr T Absolute(const T &val)
Definition: scalar.h:22
constexpr Scalar Absolute< Scalar >(const float &val)
Definition: scalar.h:27
Definition: comparable.h:95
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
constexpr bool operator==(Degrees d)
Definition: scalar.h:103
constexpr Degrees operator-(Degrees d) const
Definition: scalar.h:95
constexpr Degrees operator-() const
Definition: scalar.h:89
Scalar degrees
Definition: scalar.h:77
constexpr bool operator>(Degrees d)
Definition: scalar.h:99
constexpr Degrees operator+(Degrees d) const
Definition: scalar.h:91
constexpr bool operator<(Degrees d)
Definition: scalar.h:109
constexpr bool IsFinite() const
Definition: scalar.h:87
constexpr Degrees()=default
constexpr bool operator>=(Degrees d)
Definition: scalar.h:101
constexpr bool operator!=(Degrees d)
Definition: scalar.h:105
constexpr Degrees(Scalar p_degrees)
Definition: scalar.h:81
constexpr Degrees GetPositive() const
Definition: scalar.h:111
constexpr bool operator<=(Degrees d)
Definition: scalar.h:107
constexpr bool IsFinite() const
Definition: scalar.h:51
constexpr bool operator!=(Radians r)
Definition: scalar.h:69
constexpr Radians()=default
constexpr Radians operator-(Radians r)
Definition: scalar.h:59
constexpr bool operator>=(Radians r)
Definition: scalar.h:65
constexpr Radians operator-()
Definition: scalar.h:53
constexpr bool operator<=(Radians r)
Definition: scalar.h:71
constexpr Radians operator+(Radians r)
Definition: scalar.h:55
constexpr bool operator==(Radians r)
Definition: scalar.h:67
Scalar radians
Definition: scalar.h:45
constexpr bool operator<(Radians r)
Definition: scalar.h:73
constexpr Radians(Scalar p_radians)
Definition: scalar.h:49
constexpr bool operator>(Radians r)
Definition: scalar.h:63