cl_math.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28
29
30 #pragma once
31
32 #include <cmath>
33 #include "../System/cl_platform.h"
34 #include "vec4.h"
35 #include <memory>
36
37 namespace clan
38 {
41
42 template<typename T, typename ...Args>
43 std::unique_ptr<T> make_unique(Args&& ...args)
44 {
45 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
46 }
47
48 #undef pow2
49 #undef min
50 #undef max
51
52 template<typename T>
53 inline T pow2(T value)
54 {
55 return value*value;
56 }
57
58 template<typename A, typename B> inline A min(A a, B b) { return a < b ? a : b; }
59 template<typename A, typename B> inline A max(A a, B b) { return a > b ? a : b; }
60
61 template<typename Type>
62 inline Vec2<Type> min(Vec2<Type> a, Vec2<Type> b)
63 {
64 return Vec2<Type>(min(a.x, b.x), min(a.y, b.y));
65 }
66
67 template<typename Type>
68 inline Vec3<Type> min(Vec3<Type> a, Vec3<Type> b)
69 {
70 return Vec3<Type>(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
71 }
72
73 template<typename Type>
74 inline Vec4<Type> min(Vec4<Type> a, Vec4<Type> b)
75 {
76 return Vec4<Type>(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
77 }
78
79 template<typename Type>
80 inline Vec2<Type> max(Vec2<Type> a, Vec2<Type> b)
81 {
82 return Vec2<Type>(max(a.x, b.x), max(a.y, b.y));
83 }
84
85 template<typename Type>
86 inline Vec3<Type> max(Vec3<Type> a, Vec3<Type> b)
87 {
88 return Vec3<Type>(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
89 }
90
91 template<typename Type>
92 inline Vec4<Type> max(Vec4<Type> a, Vec4<Type> b)
93 {
94 return Vec4<Type>(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
95 }
96
97 template<typename A, typename B, typename C>
98 inline C clamp(A val, B minval, C maxval)
99 {
100 return max((A)minval, min((A)maxval, val));
101 }
102
103 template<typename A, typename B, typename C>
104 inline A mix(A a, B b, C mix)
105 {
106 return a * (C(1) - mix) + b * mix;
107 }
108
109 inline int sign(int x)
110 {
111 if (x < 0)
112 return -1;
113 else if (x > 0)
114 return 1;
115 else
116 return 0;
117 }
118
119 inline float sign(float x)
120 {
121 if (x < 0.0f)
122 return -1.0f;
123 else if (x > 0.0f)
124 return 1.0f;
125 else
126 return 0.0f;
127 }
128
129 inline double sign(double x)
130 {
131 if (x < 0.0)
132 return -1.0;
133 else if (x > 0.0)
134 return 1.0;
135 else
136 return 0.0;
137 }
138
139 template<typename Type>
140 inline Vec2<Type> sign(const Vec2<Type> &x)
141 {
142 return Vec2<Type>(sign(x.x), sign(x.y));
143 }
144
145 template<typename Type>
146 inline Vec3<Type> sign(const Vec3<Type> &x)
147 {
148 return Vec3<Type>(sign(x.x), sign(x.y), sign(x.z));
149 }
150
151 template<typename Type>
152 inline Vec4<Type> sign(const Vec4<Type> &x)
153 {
154 return Vec4<Type>(sign(x.x), sign(x.y), sign(x.z), sign(x.w));
155 }
156
157 template<typename A, typename B, typename C> inline C smoothstep(A edge0, B edge1, C x)
158 {
159 C t = clamp((x - edge0) / (edge1 - edge0), C(0), C(1));
160 return t * t * (C(3) - C(2) * t);
161 }
162
163 inline int step(int edge, int x)
164 {
165 return x < edge ? 0 : 1;
166 }
167
168 inline long long step(long long edge, long long x)
169 {
170 return x < edge ? 0 : 1;
171 }
172
173 inline float step(float edge, float x)
174 {
175 return x < edge ? 0.0f : 1.0f;
176 }
177
178 inline double step(double edge, double x)
179 {
180 return x < edge ? 0.0 : 1.0;
181 }
182
183 template<typename Type>
184 inline Vec2<Type> step(const Vec2<Type> &edge, const Vec2<Type> &x)
185 {
186 return Vec2<Type>(step(edge.x, x.x), step(edge.y, x.y));
187 }
188
189 template<typename Type>
190 inline Vec3<Type> step(const Vec3<Type> &edge, const Vec3<Type> &x)
191 {
192 return Vec3<Type>(step(edge.x, x.x), step(edge.y, x.y), step(edge.z, x.z));
193 }
194
195 template<typename Type>
196 inline Vec4<Type> step(const Vec4<Type> &edge, const Vec4<Type> &x)
197 {
198 return Vec4<Type>(step(edge.x, x.x), step(edge.y, x.y), step(edge.z, x.z), step(edge.w, x.w));
199 }
200
201 }
202
204
clan
Definition: clanapp.h:35
clan::Vec3::x
Type x
Definition: vec3.h:80
clan::pow2
T pow2(T value)
Definition: cl_math.h:53
clan::smoothstep
C smoothstep(A edge0, B edge1, C x)
Definition: cl_math.h:157
clan::Vec3::y
Type y
Definition: vec3.h:81
clan::Vec4::z
Type z
Definition: vec4.h:82
clan::make_unique
std::unique_ptr< T > make_unique(Args &&...args)
Definition: cl_math.h:43
clan::Key::t
clan::min
A min(A a, B b)
Definition: cl_math.h:58
clan::Vec2::x
Type x
Definition: vec2.h:80
clan::Key::f
clan::max
A max(A a, B b)
Definition: cl_math.h:59
clan::Vec2
2D vector
Definition: line.h:48
clan::Vec3
3D vector
Definition: line_ray.h:48
clan::Vec4::y
Type y
Definition: vec4.h:81
clan::Vec4::w
Type w
Definition: vec4.h:83
clan::mix
A mix(A a, B b, C mix)
Definition: cl_math.h:104
clan::step
int step(int edge, int x)
Definition: cl_math.h:163
clan::Vec4::x
Type x
Definition: vec4.h:80
clan::Vec4
4D vector
Definition: size.h:47
clan::sign
int sign(int x)
Definition: cl_math.h:109
clan::Vec3::z
Type z
Definition: vec3.h:82
clan::Vec2::y
Type y
Definition: vec2.h:81
clan::clamp
C clamp(A val, B minval, C maxval)
Definition: cl_math.h:98