Flutter Engine
 
Loading...
Searching...
No Matches
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
15namespace impeller {
16
17// NOLINTBEGIN(google-explicit-constructor)
18
19using Scalar = float;
20
21template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
22constexpr T Absolute(const T& val) {
23 return val >= T{} ? val : -val;
24}
25
26template <>
27constexpr Scalar Absolute<Scalar>(const float& val) {
28 return fabsf(val);
29}
30
31constexpr inline bool ScalarNearlyZero(Scalar x,
32 Scalar tolerance = kEhCloseEnough) {
33 return Absolute(x) <= tolerance;
34}
35
36constexpr inline bool ScalarNearlyEqual(Scalar x,
37 Scalar y,
38 Scalar tolerance = kEhCloseEnough) {
39 return ScalarNearlyZero(x - y, tolerance);
40}
41
42struct Degrees;
43
44struct Radians {
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
66struct Degrees {
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
104namespace std {
105
106inline std::ostream& operator<<(std::ostream& out, const impeller::Degrees& d) {
107 return out << "Degrees(" << d.degrees << ")";
108}
109
110inline 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_
int32_t x
auto & d
Definition main.cc:28
double y
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 ref_ptr.h:261
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition arc.h:141
constexpr Degrees operator-(Degrees d) const
Definition scalar.h:85
constexpr Degrees operator-() const
Definition scalar.h:79
Scalar degrees
Definition scalar.h:67
constexpr Degrees operator+(Degrees d) const
Definition scalar.h:81
constexpr bool IsFinite() const
Definition scalar.h:77
constexpr Degrees()=default
constexpr auto operator<=>(const Degrees &d) const =default
constexpr Degrees(Scalar p_degrees)
Definition scalar.h:71
constexpr Degrees GetPositive() const
Definition scalar.h:91
constexpr bool IsFinite() const
Definition scalar.h:51
constexpr Radians()=default
constexpr Radians operator-(Radians r)
Definition scalar.h:59
constexpr Radians operator-()
Definition scalar.h:53
constexpr Radians operator+(Radians r)
Definition scalar.h:55
Scalar radians
Definition scalar.h:45
constexpr auto operator<=>(const Radians &r) const =default
constexpr Radians(Scalar p_radians)
Definition scalar.h:49