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