| 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 <type_traits>
10 #include <valarray>
11
12 #include "impeller/geometry/constants.h"
13
14 namespace impeller {
15
16 // NOLINTBEGIN(google-explicit-constructor)
17
18 using Scalar = float;
19
20 template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
21 constexpr T Absolute(const T& val) {
22 return val >= T{} ? val : -val;
23 }
24
25 constexpr inline bool ScalarNearlyZero(Scalar x,
26 Scalar tolerance = kEhCloseEnough) {
27 return Absolute(x) <= tolerance;
28 }
29
30 constexpr inline bool ScalarNearlyEqual(Scalar x,
31 Scalar y,
32 Scalar tolerance = kEhCloseEnough) {
33 return ScalarNearlyZero(x - y, tolerance);
34 }
35
36 struct Degrees;
37
38 struct Radians {
39 Scalar radians = 0.0;
40
41 constexpr Radians() = default;
42
43 explicit constexpr Radians(Scalar p_radians) : radians(p_radians) {}
44 };
45
46 struct Degrees {
47 Scalar degrees = 0.0;
48
49 constexpr Degrees() = default;
50
51 explicit constexpr Degrees(Scalar p_degrees) : degrees(p_degrees) {}
52
53 constexpr operator Radians() const {
54 return Radians{degrees * kPi / 180.0f};
55 };
56 };
57
58 // NOLINTEND(google-explicit-constructor)
59
60 } // namespace impeller
61
62 #endif // FLUTTER_IMPELLER_GEOMETRY_SCALAR_H_
impeller::Degrees::Degrees
constexpr Degrees()=default
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Degrees::degrees
Scalar degrees
Definition: scalar.h:47
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::kPi
constexpr float kPi
Definition: constants.h:26
impeller::Radians::radians
Scalar radians
Definition: scalar.h:39
impeller::Radians::Radians
constexpr Radians()=default
impeller::Radians
Definition: scalar.h:38
impeller::ScalarNearlyZero
constexpr bool ScalarNearlyZero(Scalar x, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:25
impeller::Absolute
constexpr T Absolute(const T &val)
Definition: scalar.h:21
constants.h
impeller::Radians::Radians
constexpr Radians(Scalar p_radians)
Definition: scalar.h:43
impeller::ScalarNearlyEqual
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:30
impeller::Degrees
Definition: scalar.h:46
impeller
Definition: aiks_context.cc:10
impeller::Degrees::Degrees
constexpr Degrees(Scalar p_degrees)
Definition: scalar.h:51