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 
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 template <>
26 constexpr Scalar Absolute<Scalar>(const float& val) {
27  return fabsf(val);
28 }
29 
30 constexpr inline bool ScalarNearlyZero(Scalar x,
31  Scalar tolerance = kEhCloseEnough) {
32  return Absolute(x) <= tolerance;
33 }
34 
35 constexpr inline bool ScalarNearlyEqual(Scalar x,
36  Scalar y,
37  Scalar tolerance = kEhCloseEnough) {
38  return ScalarNearlyZero(x - y, tolerance);
39 }
40 
41 struct Degrees;
42 
43 struct Radians {
44  Scalar radians = 0.0;
45 
46  constexpr Radians() = default;
47 
48  explicit constexpr Radians(Scalar p_radians) : radians(p_radians) {}
49 };
50 
51 struct Degrees {
52  Scalar degrees = 0.0;
53 
54  constexpr Degrees() = default;
55 
56  explicit constexpr Degrees(Scalar p_degrees) : degrees(p_degrees) {}
57 
58  constexpr operator Radians() const {
59  return Radians{degrees * kPi / 180.0f};
60  };
61 };
62 
63 // NOLINTEND(google-explicit-constructor)
64 
65 } // namespace impeller
66 
67 #endif // FLUTTER_IMPELLER_GEOMETRY_SCALAR_H_
int32_t x
constexpr float kPi
Definition: constants.h:26
float Scalar
Definition: scalar.h:18
constexpr float kEhCloseEnough
Definition: constants.h:57
constexpr bool ScalarNearlyZero(Scalar x, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:30
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:35
constexpr T Absolute(const T &val)
Definition: scalar.h:21
constexpr Scalar Absolute< Scalar >(const float &val)
Definition: scalar.h:26
Scalar degrees
Definition: scalar.h:52
constexpr Degrees()=default
constexpr Degrees(Scalar p_degrees)
Definition: scalar.h:56
constexpr Radians()=default
Scalar radians
Definition: scalar.h:44
constexpr Radians(Scalar p_radians)
Definition: scalar.h:48