cl_math.h Source File - ClanLib SDK
Description: cl_math.h Source File

cl_math.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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#pragma once
30
31#include <cmath>
32#include "../System/cl_platform.h"
33#include "vec4.h"
34#include <memory>
35
36namespace clan
37{
40
41 template<typename T, typename ...Args>
42 std::unique_ptr<T> make_unique(Args&& ...args)
43 {
44 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
45 }
46
47 #undef pow2
48 #undef min
49 #undef max
50
51 template<typename T>
52 inline T pow2(T value)
53 {
54 return value*value;
55 }
56
57 template<typename A, typename B> inline A min(A a, B b) { return a < b ? a : b; }
58 template<typename A, typename B> inline A max(A a, B b) { return a > b ? a : b; }
59
60 template<typename Type>
61 inline Vec2<Type> min(Vec2<Type> a, Vec2<Type> b)
62 {
63 return Vec2<Type>(min(a.x, b.x), min(a.y, b.y));
64 }
65
66 template<typename Type>
67 inline Vec3<Type> min(Vec3<Type> a, Vec3<Type> b)
68 {
69 return Vec3<Type>(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
70 }
71
72 template<typename Type>
73 inline Vec4<Type> min(Vec4<Type> a, Vec4<Type> b)
74 {
75 return Vec4<Type>(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
76 }
77
78 template<typename Type>
79 inline Vec2<Type> max(Vec2<Type> a, Vec2<Type> b)
80 {
81 return Vec2<Type>(max(a.x, b.x), max(a.y, b.y));
82 }
83
84 template<typename Type>
85 inline Vec3<Type> max(Vec3<Type> a, Vec3<Type> b)
86 {
87 return Vec3<Type>(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
88 }
89
90 template<typename Type>
91 inline Vec4<Type> max(Vec4<Type> a, Vec4<Type> b)
92 {
93 return Vec4<Type>(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
94 }
95
96 template<typename A, typename B, typename C>
97 inline C clamp(A val, B minval, C maxval)
98 {
99 return max((A)minval, min((A)maxval, val));
100 }
101
102 template<typename A, typename B, typename C>
103 inline A mix(A a, B b, C mix)
104 {
105 return a * (C(1) - mix) + b * mix;
106 }
107
108 inline int sign(int x)
109 {
110 if (x < 0)
111 return -1;
112 else if (x > 0)
113 return 1;
114 else
115 return 0;
116 }
117
118 inline float sign(float x)
119 {
120 if (x < 0.0f)
121 return -1.0f;
122 else if (x > 0.0f)
123 return 1.0f;
124 else
125 return 0.0f;
126 }
127
128 inline double sign(double x)
129 {
130 if (x < 0.0)
131 return -1.0;
132 else if (x > 0.0)
133 return 1.0;
134 else
135 return 0.0;
136 }
137
138 template<typename Type>
139 inline Vec2<Type> sign(const Vec2<Type> &x)
140 {
141 return Vec2<Type>(sign(x.x), sign(x.y));
142 }
143
144 template<typename Type>
145 inline Vec3<Type> sign(const Vec3<Type> &x)
146 {
147 return Vec3<Type>(sign(x.x), sign(x.y), sign(x.z));
148 }
149
150 template<typename Type>
151 inline Vec4<Type> sign(const Vec4<Type> &x)
152 {
153 return Vec4<Type>(sign(x.x), sign(x.y), sign(x.z), sign(x.w));
154 }
155
156 template<typename A, typename B, typename C> inline C smoothstep(A edge0, B edge1, C x)
157 {
158 C t = clamp((x - edge0) / (edge1 - edge0), C(0), C(1));
159 return t * t * (C(3) - C(2) * t);
160 }
161
162 inline int step(int edge, int x)
163 {
164 return x < edge ? 0 : 1;
165 }
166
167 inline long long step(long long edge, long long x)
168 {
169 return x < edge ? 0 : 1;
170 }
171
172 inline float step(float edge, float x)
173 {
174 return x < edge ? 0.0f : 1.0f;
175 }
176
177 inline double step(double edge, double x)
178 {
179 return x < edge ? 0.0 : 1.0;
180 }
181
182 template<typename Type>
183 inline Vec2<Type> step(const Vec2<Type> &edge, const Vec2<Type> &x)
184 {
185 return Vec2<Type>(step(edge.x, x.x), step(edge.y, x.y));
186 }
187
188 template<typename Type>
189 inline Vec3<Type> step(const Vec3<Type> &edge, const Vec3<Type> &x)
190 {
191 return Vec3<Type>(step(edge.x, x.x), step(edge.y, x.y), step(edge.z, x.z));
192 }
193
194 template<typename Type>
195 inline Vec4<Type> step(const Vec4<Type> &edge, const Vec4<Type> &x)
196 {
197 return Vec4<Type>(step(edge.x, x.x), step(edge.y, x.y), step(edge.z, x.z), step(edge.w, x.w));
198 }
199
201}
clan::Vec2
2D vector
Definition vec4.h:43
clan::Vec2::y
Type y
Definition vec2.h:81
clan::Vec2::x
Type x
Definition vec2.h:80
clan::Vec3
3D vector
Definition vec4.h:46
clan::Vec3::z
Type z
Definition vec3.h:81
clan::Vec3::y
Type y
Definition vec3.h:80
clan::Vec3::x
Type x
Definition vec3.h:79
clan::Vec4
4D vector
Definition vec4.h:75
clan::Vec4::z
Type z
Definition vec4.h:81
clan::Vec4::y
Type y
Definition vec4.h:80
clan::Vec4::x
Type x
Definition vec4.h:79
clan::Vec4::w
Type w
Definition vec4.h:82
clan::mix
A mix(A a, B b, C mix)
Definition cl_math.h:103
clan::pow2
T pow2(T value)
Definition cl_math.h:52
clan::make_unique
std::unique_ptr< T > make_unique(Args &&...args)
Definition cl_math.h:42
clan::smoothstep
C smoothstep(A edge0, B edge1, C x)
Definition cl_math.h:156
clan::sign
int sign(int x)
Definition cl_math.h:108
clan::step
int step(int edge, int x)
Definition cl_math.h:162
clan::BlendEquation::max
@ max
clan::BlendEquation::min
@ min
clan
Definition clanapp.h:36
clan::BrushWrapMode::clamp
@ clamp
clan::Key::a
@ a
clan::Key::b
@ b
clan::Key::x
@ x
clan::Key::t
@ t