RavEngine
Loading...
Searching...
No Matches
math_ex.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_BASE_MATHS_MATH_EX_H_
29#define OZZ_OZZ_BASE_MATHS_MATH_EX_H_
30
31#include <cassert>
32#include <cmath>
33
34#include "ozz/base/platform.h"
35
36namespace ozz {
37namespace math {
38
39// Returns the linear interpolation of _a and _b with coefficient _f.
40// _f is not limited to range [0,1].
41OZZ_INLINE float Lerp(float _a, float _b, float _f) {
42 return (_b - _a) * _f + _a;
43}
44
45// Returns the minimum of _a and _b. Comparison's based on operator <.
46template <typename _Ty>
47OZZ_INLINE _Ty Min(_Ty _a, _Ty _b) {
48 return (_a < _b) ? _a : _b;
49}
50
51// Returns the maximum of _a and _b. Comparison's based on operator <.
52template <typename _Ty>
53OZZ_INLINE _Ty Max(_Ty _a, _Ty _b) {
54 return (_b < _a) ? _a : _b;
55}
56
57// Clamps _x between _a and _b. Comparison's based on operator <.
58// Result is unknown if _a is not less or equal to _b.
59template <typename _Ty>
60OZZ_INLINE _Ty Clamp(_Ty _a, _Ty _x, _Ty _b) {
61 const _Ty min = _x < _b ? _x : _b;
62 return min < _a ? _a : min;
63}
64
65// Implements int selection, avoiding branching.
66OZZ_INLINE int Select(bool _b, int _true, int _false) {
67 return _false ^ (-static_cast<int>(_b) & (_true ^ _false));
68}
69
70// Implements float selection, avoiding branching.
71OZZ_INLINE float Select(bool _b, float _true, float _false) {
72 union {
73 float f;
74 int32_t i;
75 } t = {_true};
76 union {
77 float f;
78 int32_t i;
79 } f = {_false};
80 union {
81 int32_t i;
82 float f;
83 } r = {f.i ^ (-static_cast<int32_t>(_b) & (t.i ^ f.i))};
84 return r.f;
85}
86
87// Implements pointer selection, avoiding branching.
88template <typename _Ty>
89OZZ_INLINE _Ty* Select(bool _b, _Ty* _true, _Ty* _false) {
90 union {
91 _Ty* p;
92 intptr_t i;
93 } t = {_true};
94 union {
95 _Ty* p;
96 intptr_t i;
97 } f = {_false};
98 union {
99 intptr_t i;
100 _Ty* p;
101 } r = {f.i ^ (-static_cast<intptr_t>(_b) & (t.i ^ f.i))};
102 return r.p;
103}
104
105// Implements const pointer selection, avoiding branching.
106template <typename _Ty>
107OZZ_INLINE const _Ty* Select(bool _b, const _Ty* _true, const _Ty* _false) {
108 union {
109 const _Ty* p;
110 intptr_t i;
111 } t = {_true};
112 union {
113 const _Ty* p;
114 intptr_t i;
115 } f = {_false};
116 union {
117 intptr_t i;
118 const _Ty* p;
119 } r = {f.i ^ (-static_cast<intptr_t>(_b) & (t.i ^ f.i))};
120 return r.p;
121}
122
123} // namespace math
124} // namespace ozz
125#endif // OZZ_OZZ_BASE_MATHS_MATH_EX_H_
int32 int32_t
Definition fwd.hpp:71