| 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 auto operator<=>(const Radians& r) const = default;
64 };
65
66 struct Degrees {
67 Scalar degrees = 0.0;
68
69 constexpr Degrees() = default;
70
71 explicit constexpr Degrees(Scalar p_degrees) : degrees(p_degrees) {}
72
73 constexpr operator Radians() const {
74 return Radians{degrees * kPi / 180.0f};
75 };
76
77 constexpr bool IsFinite() const { return std::isfinite(degrees); }
78
79 constexpr Degrees operator-() const { return Degrees{-degrees}; }
80
81 constexpr Degrees operator+(Degrees d) const {
82 return Degrees{degrees + d.degrees};
83 }
84
85 constexpr Degrees operator-(Degrees d) const {
86 return Degrees{degrees - d.degrees};
87 }
88
89 constexpr auto operator<=>(const Degrees& d) const = default;
90
91 constexpr Degrees GetPositive() const {
92 Scalar deg = std::fmod(degrees, 360.0f);
93 if (deg < 0.0f) {
94 deg += 360.0f;
95 }
96 return Degrees{deg};
97 }
98 };
99
100 // NOLINTEND(google-explicit-constructor)
101
102 } // namespace impeller
103
104 namespace std {
105
106 inline std::ostream& operator<<(std::ostream& out, const impeller::Degrees& d) {
107 return out << "Degrees(" << d.degrees << ")";
108 }
109
110 inline std::ostream& operator<<(std::ostream& out, const impeller::Radians& r) {
111 return out << "Radians(" << r.radians << ")";
112 }
113
114 } // namespace std
115
116 #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:66
impeller::Degrees::operator-
constexpr Degrees operator-(Degrees d) const
Definition: scalar.h:85
impeller::Degrees::operator-
constexpr Degrees operator-() const
Definition: scalar.h:79
impeller::Degrees::degrees
Scalar degrees
Definition: scalar.h:67
impeller::Degrees::operator+
constexpr Degrees operator+(Degrees d) const
Definition: scalar.h:81
impeller::Degrees::IsFinite
constexpr bool IsFinite() const
Definition: scalar.h:77
impeller::Degrees::Degrees
constexpr Degrees()=default
impeller::Degrees::operator<=>
constexpr auto operator<=>(const Degrees &d) const =default
impeller::Degrees::Degrees
constexpr Degrees(Scalar p_degrees)
Definition: scalar.h:71
impeller::Degrees::GetPositive
constexpr Degrees GetPositive() const
Definition: scalar.h:91
impeller::Radians
Definition: scalar.h:44
impeller::Radians::IsFinite
constexpr bool IsFinite() const
Definition: scalar.h:51
impeller::Radians::Radians
constexpr Radians()=default
impeller::Radians::operator-
constexpr Radians operator-(Radians r)
Definition: scalar.h:59
impeller::Radians::operator-
constexpr Radians operator-()
Definition: scalar.h:53
impeller::Radians::operator+
constexpr Radians operator+(Radians r)
Definition: scalar.h:55
impeller::Radians::radians
Scalar radians
Definition: scalar.h:45
impeller::Radians::operator<=>
constexpr auto operator<=>(const Radians &r) const =default
impeller::Radians::Radians
constexpr Radians(Scalar p_radians)
Definition: scalar.h:49