Kiwano Engine v1.3.x
Interpolator.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/core/Function.h>
23#include <kiwano/math/Vec2.hpp>
24#include <kiwano/math/Rect.hpp>
25#include <kiwano/math/Transform.hpp>
26
27namespace kiwano
28{
29namespace math
30{
31
32template <typename _Method = void>
34
35template <>
37{
38 inline float operator()(float frac) const noexcept
39 {
40 return frac;
41 }
42};
43
44template <>
45struct InterpolateMethod<float (*)(float)>
46{
47 float (*method)(float);
48
49 InterpolateMethod(float (*method)(float)) noexcept
50 : method(method)
51 {
52 }
53
54 inline float operator()(float frac) const noexcept
55 {
56 if (method)
57 return method(frac);
58 return frac;
59 }
60};
61
62template <>
63struct InterpolateMethod<Function<float(float)>>
64{
65 Function<float(float)> method;
66
67 InterpolateMethod(const Function<float(float)>& method) noexcept
68 : method(method)
69 {
70 }
71
72 inline float operator()(float frac) const noexcept
73 {
74 if (method)
75 return method(frac);
76 return frac;
77 }
78};
79
80template <typename _Ty>
81class Interpolator;
82
83template <typename _Ty>
85{
86public:
87 template <typename _Method = void>
88 inline _Ty Interpolate(_Ty start, _Ty end, float frac, const InterpolateMethod<_Method>& method = {})
89 {
90 if (frac >= 1)
91 return end;
92 return start + static_cast<_Ty>(static_cast<float>(end - start) * method(frac));
93 }
94};
95
96template <typename _Ty>
98{
99public:
100 template <typename _Method = void>
101 inline Vec2T<_Ty> Interpolate(const Vec2T<_Ty>& start, const Vec2T<_Ty>& end, float frac,
102 const InterpolateMethod<_Method>& method = {})
103 {
104 if (frac >= 1)
105 return end;
106
108 return Vec2T<_Ty>{ fi.Interpolate(start.x, end.x, frac, method), fi.Interpolate(start.y, end.y, frac, method) };
109 }
110};
111
112template <typename _Ty>
114{
115public:
116 template <typename _Method = void>
117 inline RectT<_Ty> Interpolate(const RectT<_Ty>& start, const RectT<_Ty>& end, float frac,
118 const InterpolateMethod<_Method>& method = {})
119 {
120 if (frac >= 1)
121 return end;
122
124 return RectT<_Ty>{ vi.Interpolate(start.left_top, end.left_top, frac, method),
125 vi.Interpolate(start.right_bottom, end.right_bottom, frac, method) };
126 }
127};
128
129template <typename _Ty>
131{
132public:
133 template <typename _Method = void>
134 inline TransformT<_Ty> Interpolate(const TransformT<_Ty>& start, const TransformT<_Ty>& end, float frac,
135 const InterpolateMethod<_Method>& method = {})
136 {
137 if (frac >= 1)
138 return end;
139
142
143 TransformT<_Ty> transform;
144 transform.rotation = fi.Interpolate(start.rotation, end.rotation, frac, method);
145 transform.position = vi.Interpolate(start.position, end.position, frac, method);
146 transform.scale = vi.Interpolate(start.scale, end.scale, frac, method);
147 transform.skew = vi.Interpolate(start.skew, end.skew, frac, method);
148 return transform;
149 }
150};
151
152} // namespace math
153} // namespace kiwano
Definition: Function.h:228
Definition: Interpolator.h:85
二维放射变换
Definition: Transform.hpp:36
float rotation
旋转
Definition: Transform.hpp:40
Vec2T< ValueType > skew
错切角度
Definition: Transform.hpp:43
Vec2T< ValueType > position
坐标
Definition: Transform.hpp:41
Vec2T< ValueType > scale
缩放
Definition: Transform.hpp:42
Definition: Interpolator.h:33
Definition: Rect.hpp:30
Definition: Vec2.hpp:30