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