32 #ifndef glslmath_INCLUDE_ONCE 33 #define glslmath_INCLUDE_ONCE 95 #if defined(__CUDACC__) 125 return log((1+x)/(1-x))/2;
130 template<
typename T>
bool isnan(T value) {
return !(value == value); }
140 T
modf(T a, T& intpart);
282 ::atan2(a.
y(), b.
y()) );
288 ::atan2(a.
y(), b.
y()),
289 ::atan2(a.
z(), b.
z()) );
295 ::atan2(a.
y(), b.
y()),
296 ::atan2(a.
z(), b.
z()),
297 ::atan2(a.
w(), b.
w()) );
439 (T)::
pow(a.
y(), b.
y()) );
445 (T)::
pow(a.
y(), b.
y()),
446 (T)::
pow(a.
z(), b.
z()) );
452 (T)::
pow(a.
y(), b.
y()),
453 (T)::
pow(a.
z(), b.
z()),
454 (T)::
pow(a.
w(), b.
w()) );
517 (T)::
pow(2, a.
y()) );
524 (T)::
pow(2, a.
z()) );
532 (T)::
pow(2, a.
w()) );
646 T
abs(T a) {
return a >= 0 ? a : -a; }
651 return Vector2<T>( a.
x() >= 0 ? a.
x() : -a.
x(), a.
y() >= 0 ? a.
y() : -a.
y() );
657 return Vector3<T>( a.
x() >= 0 ? a.
x() : -a.
x(), a.
y() >= 0 ? a.
y() : -a.
y(), a.
z() >= 0 ? a.
z() : -a.
z() );
663 return Vector4<T>( a.
x() >= 0 ? a.
x() : -a.
x(), a.
y() >= 0 ? a.
y() : -a.
y(), a.
z() >= 0 ? a.
z() : -a.
z(), a.
w() >= 0 ? a.
w() : -a.
w() );
669 T
sign(T a) {
return a > 0 ? 1 : a == 0 ? 0 : (T)-1; }
674 return Vector2<T>( a.
x() > 0 ? 1 : a.
x() == 0 ? 0 : (T)-1,
675 a.
y() > 0 ? 1 : a.
y() == 0 ? 0 : (T)-1 );
681 return Vector3<T>( a.
x() > 0 ? 1 : a.
x() == 0 ? 0 : (T)-1,
682 a.
y() > 0 ? 1 : a.
y() == 0 ? 0 : (T)-1,
683 a.
z() > 0 ? 1 : a.
z() == 0 ? 0 : (T)-1 );
689 return Vector4<T>( a.
x() > 0 ? 1 : a.
x() == 0 ? 0 : (T)-1,
690 a.
y() > 0 ? 1 : a.
y() == 0 ? 0 : (T)-1,
691 a.
z() > 0 ? 1 : a.
z() == 0 ? 0 : (T)-1,
692 a.
w() > 0 ? 1 : a.
w() == 0 ? 0 : (T)-1 );
790 float modf(
float a,
float& intpart) {
791 #if defined(_MSC_VER) 794 double dintpart = intpart;
795 float r = (float)::
modf((
double)a,&dintpart);
796 intpart = (float)dintpart;
807 modf(a.
y(), intpart.
y()) );
814 modf(a.
z(), intpart.
z()) );
822 modf(a.
w(), intpart.
w()) );
838 if ((a -(intpart + 0.5f)) < epsilon)
841 if (::fmod(intpart, 2) < epsilon)
845 return ceil(intpart + 0.5f);
864 if ((a -(intpart + 0.5)) < epsilon)
867 if (::fmod(intpart, 2) < epsilon)
871 return ceil(intpart + 0.5);
952 T
mix(T a, T b, T t) {
return a*(1-t) + b*t; }
967 a.
y()*(1-t.
y()) + b.
y()*t.
y() );
974 a.
y()*(1-t.
y()) + b.
y()*t.
y(),
975 a.
z()*(1-t.
z()) + b.
z()*t.
z() );
982 a.
y()*(1-t.
y()) + b.
y()*t.
y(),
983 a.
z()*(1-t.
z()) + b.
z()*t.
z(),
984 a.
w()*(1-t.
w()) + b.
w()*t.
w() );
990 T
step( T edge, T a ) {
if (a<edge)
return 0;
else return 1; }
996 a.
y()<edge.
y() ? 0 : (T)1 );
1003 a.
y()<edge.
y() ? 0 : (T)1,
1004 a.
z()<edge.
z() ? 0 : (T)1 );
1007 template<
typename T>
1011 a.
y()<edge.
y() ? 0 : (T)1,
1012 a.
z()<edge.
z() ? 0 : (T)1,
1013 a.
w()<edge.
w() ? 0 : (T)1 );
1017 template<
typename T>
1020 T t =
clamp( (a - edge0) / (edge1 - edge0), (T)0, (T)1);
1021 return t * t * (3 - 2 * t);
1024 template<
typename T>
1029 t =
clamp( (a.
x() - edge0.
x()) / (edge1.
x() - edge0.
x()), (T)0, (T)1); v.
x() = t * t * (3 - 2 * t);
1030 t =
clamp( (a.
y() - edge0.
y()) / (edge1.
y() - edge0.
y()), (T)0, (T)1); v.
y() = t * t * (3 - 2 * t);
1034 template<
typename T>
1039 t =
clamp( (a.
x() - edge0.
x()) / (edge1.
x() - edge0.
x()), (T)0, (T)1); v.
x() = t * t * (3 - 2 * t);
1040 t =
clamp( (a.
y() - edge0.
y()) / (edge1.
y() - edge0.
y()), (T)0, (T)1); v.
y() = t * t * (3 - 2 * t);
1041 t =
clamp( (a.
z() - edge0.
z()) / (edge1.
z() - edge0.
z()), (T)0, (T)1); v.
z() = t * t * (3 - 2 * t);
1045 template<
typename T>
1050 t =
clamp( (a.
x() - edge0.
x()) / (edge1.
x() - edge0.
x()), (T)0, (T)1); v.
x() = t * t * (3 - 2 * t);
1051 t =
clamp( (a.
y() - edge0.
y()) / (edge1.
y() - edge0.
y()), (T)0, (T)1); v.
y() = t * t * (3 - 2 * t);
1052 t =
clamp( (a.
z() - edge0.
z()) / (edge1.
z() - edge0.
z()), (T)0, (T)1); v.
z() = t * t * (3 - 2 * t);
1053 t =
clamp( (a.
w() - edge0.
w()) / (edge1.
w() - edge0.
w()), (T)0, (T)1); v.
w() = t * t * (3 - 2 * t);
1059 template<
typename T>
1062 template<
typename T>
1065 template<
typename T>
1070 template<
typename T>
1073 template<
typename T>
1076 template<
typename T>
1083 template<
typename T>
1086 template<
typename T>
1089 template<
typename T>
1092 template<
typename T>
1097 template<
typename T>
1100 template<
typename T>
1103 template<
typename T>
1106 template<
typename T>
1111 inline float dot(
float a,
float b) {
return a*b; }
1115 inline double dot(
double a,
double b) {
return a*b; }
1119 inline real
dot(
int a,
int b) {
return (real)a*b; }
1123 inline real
dot(
unsigned int a,
unsigned int b) {
return (real)a*b; }
1127 template<
typename T>
1130 template<
typename T>
1133 template<
typename T>
1136 template<
typename T>
1141 template<
typename T>
1144 template<
typename T>
1147 template<
typename T>
1150 template<
typename T>
1155 template<
typename T>
1158 template<
typename T>
1161 template<
typename T>
1164 template<
typename T>
1169 template<
typename T>
1172 T k = 1 - eta * eta * (1 -
dot(N, I) *
dot(N, I));
1176 return eta * I - (eta *
dot(N, I) +
::sqrt(k)) * N;
1179 template<
typename T>
1182 T k = 1 - eta * eta * (1 -
dot(N, I) *
dot(N, I));
1186 return eta * I - N * (eta *
dot(N, I) +
::sqrt(k));
1189 template<
typename T>
1192 T k = 1 - eta * eta * (1 -
dot(N, I) *
dot(N, I));
1196 return eta * I - N * (eta *
dot(N, I) +
::sqrt(k));
1199 template<
typename T>
1202 T k = 1 - eta * eta * (1 -
dot(N, I) *
dot(N, I));
1206 return eta * I - N * (eta *
dot(N, I) +
::sqrt(k));
1213 template<
typename T>
1217 for(
int i=0; i<2; ++i)
1218 for(
int j=0; j<2; ++j)
1219 t.
e(j,i) = a.
e(j,i) * b.
e(j,i);
1223 template<
typename T>
1227 for(
int i=0; i<3; ++i)
1228 for(
int j=0; j<3; ++j)
1229 t.
e(j,i) = a.
e(j,i) * b.
e(j,i);
1233 template<
typename T>
1237 for(
int i=0; i<4; ++i)
1238 for(
int j=0; j<4; ++j)
1239 t.
e(j,i) = a.
e(j,i) * b.
e(j,i);
1245 template<
typename T>
1249 for(
int i=0; i<2; ++i)
1250 for(
int j=0; j<2; ++j)
1251 m.
e(i,j) = a[i] * b[j];
1255 template<
typename T>
1259 for(
int i=0; i<3; ++i)
1260 for(
int j=0; j<3; ++j)
1261 m.
e(i,j) = a[i] * b[j];
1265 template<
typename T>
1269 for(
int i=0; i<4; ++i)
1270 for(
int j=0; j<4; ++j)
1271 m.
e(i,j) = a[i] * b[j];
1277 template<
typename T>
1281 for(
int i=0; i<2; ++i)
1282 for(
int j=0; j<2; ++j)
1283 t.
e(j,i) = a.
e(i,j);
1287 template<
typename T>
1291 for(
int i=0; i<3; ++i)
1292 for(
int j=0; j<3; ++j)
1293 t.
e(j,i) = a.
e(i,j);
1297 template<
typename T>
1301 for(
int i=0; i<4; ++i)
1302 for(
int j=0; j<4; ++j)
1303 t.
e(j,i) = a.
e(i,j);
1311 template<
typename T>
1313 return ivec4( a.
x() < b.
x() ? 1 : 0,
1314 a.
y() < b.
y() ? 1 : 0,
1315 a.
z() < b.
z() ? 1 : 0,
1316 a.
w() < b.
w() ? 1 : 0 );
1319 template<
typename T>
1321 return ivec3( a.
x() < b.
x() ? 1 : 0,
1322 a.
y() < b.
y() ? 1 : 0,
1323 a.
z() < b.
z() ? 1 : 0 );
1326 template<
typename T>
1328 return ivec2( a.
x() < b.
x() ? 1 : 0,
1329 a.
y() < b.
y() ? 1 : 0 );
1334 template<
typename T>
1336 return ivec4( a.
x() <= b.
x() ? 1 : 0,
1337 a.
y() <= b.
y() ? 1 : 0,
1338 a.
z() <= b.
z() ? 1 : 0,
1339 a.
w() <= b.
w() ? 1 : 0 );
1342 template<
typename T>
1344 return ivec3( a.
x() <= b.
x() ? 1 : 0,
1345 a.
y() <= b.
y() ? 1 : 0,
1346 a.
z() <= b.
z() ? 1 : 0 );
1349 template<
typename T>
1351 return ivec2( a.
x() <= b.
x() ? 1 : 0,
1352 a.
y() <= b.
y() ? 1 : 0 );
1357 template<
typename T>
1359 return ivec4( a.
x() > b.
x() ? 1 : 0,
1360 a.
y() > b.
y() ? 1 : 0,
1361 a.
z() > b.
z() ? 1 : 0,
1362 a.
w() > b.
w() ? 1 : 0 );
1365 template<
typename T>
1367 return ivec3( a.
x() > b.
x() ? 1 : 0,
1368 a.
y() > b.
y() ? 1 : 0,
1369 a.
z() > b.
z() ? 1 : 0 );
1372 template<
typename T>
1374 return ivec2( a.
x() > b.
x() ? 1 : 0,
1375 a.
y() > b.
y() ? 1 : 0 );
1380 template<
typename T>
1382 return ivec4( a.
x() >= b.
x() ? 1 : 0,
1383 a.
y() >= b.
y() ? 1 : 0,
1384 a.
z() >= b.
z() ? 1 : 0,
1385 a.
w() >= b.
w() ? 1 : 0 );
1388 template<
typename T>
1390 return ivec3( a.
x() >= b.
x() ? 1 : 0,
1391 a.
y() >= b.
y() ? 1 : 0,
1392 a.
z() >= b.
z() ? 1 : 0 );
1395 template<
typename T>
1397 return ivec2( a.
x() >= b.
x() ? 1 : 0,
1398 a.
y() >= b.
y() ? 1 : 0 );
1403 template<
typename T>
1405 return ivec4( a.
x() == b.
x() ? 1 : 0,
1406 a.
y() == b.
y() ? 1 : 0,
1407 a.
z() == b.
z() ? 1 : 0,
1408 a.
w() == b.
w() ? 1 : 0 );
1411 template<
typename T>
1413 return ivec3( a.
x() == b.
x() ? 1 : 0,
1414 a.
y() == b.
y() ? 1 : 0,
1415 a.
z() == b.
z() ? 1 : 0 );
1418 template<
typename T>
1420 return ivec2( a.
x() == b.
x() ? 1 : 0,
1421 a.
y() == b.
y() ? 1 : 0 );
1426 template<
typename T>
1428 return ivec4( a.
x() != b.
x() ? 1 : 0,
1429 a.
y() != b.
y() ? 1 : 0,
1430 a.
z() != b.
z() ? 1 : 0,
1431 a.
w() != b.
w() ? 1 : 0 );
1434 template<
typename T>
1436 return ivec3( a.
x() != b.
x() ? 1 : 0,
1437 a.
y() != b.
y() ? 1 : 0,
1438 a.
z() != b.
z() ? 1 : 0 );
1441 template<
typename T>
1443 return ivec2( a.
x() != b.
x() ? 1 : 0,
1444 a.
y() != b.
y() ? 1 : 0 );
1449 inline bool any(
const ivec2& a) {
return a.
x() != 0 || a.
y() != 0; }
1450 inline bool any(
const ivec3& a) {
return a.
x() != 0 || a.
y() != 0 || a.
z() != 0; }
1451 inline bool any(
const ivec4& a) {
return a.
x() != 0 || a.
y() != 0 || a.
z() != 0 || a.
w() != 0; }
1455 inline bool all(
const ivec2& a) {
return a.
x() != 0 && a.
y() != 0; }
1456 inline bool all(
const ivec3& a) {
return a.
x() != 0 && a.
y() != 0 && a.
z() != 0; }
1457 inline bool all(
const ivec4& a) {
return a.
x() != 0 && a.
y() != 0 && a.
z() != 0 && a.
w() != 0; }
1461 #if defined(_MSC_VER) 1462 inline ivec2 not(
const ivec2& a) {
return ivec2( a.
x() != 0 ? 0 : 1, a.
y() != 0 ? 0 : 1); }
1463 inline ivec3 not(
const ivec3& a) {
return ivec3( a.
x() != 0 ? 0 : 1, a.
y() != 0 ? 0 : 1, a.
z() != 0 ? 0 : 1); }
1464 inline ivec4 not(
const ivec4& a) {
return ivec4( a.
x() != 0 ? 0 : 1, a.
y() != 0 ? 0 : 1, a.
z() != 0 ? 0 : 1, a.
w() != 0 ? 0 : 1 ); }
const T_Scalar & z() const
float clamp(float x, float minval, float maxval)
Vector2< int > ivec2
A 2 components vector with int precision.
const Vector3 & normalize(T_Scalar *len=NULL)
const T_Scalar & e(int i, int j) const
const T_Scalar & x() const
ivec4 notEqual(const Vector4< T > &a, const Vector4< T > &b)
const T_Scalar & z() const
const Vector2 & normalize(T_Scalar *len=NULL)
The Matrix2 class is a template class that implements a generic 2x2 matrix, see also vl::dmat2...
const T_Scalar & e(int i, int j) const
T refract(T I, T N, T eta)
const double dRAD_TO_DEG
Constant to convert radian into degree using double precision.
The Vector4 class is a template class that implements a generic 4 components vector, see also vl::fvec4, vl::dvec4, vl::uvec4, vl::ivec4, vl::svec4, vl::usvec4, vl::bvec4, vl::ubvec4.
T faceforward(T N, T I, T Nref)
ivec4 lessThan(const Vector4< T > &a, const Vector4< T > &b)
Visualization Library main namespace.
float dot(float a, float b)
const double dDEG_TO_RAD
Constant to convert degree into radian using double precision.
Vector4< T > floor(const Vector4< T > &a)
Matrix2< T > transpose(const Matrix2< T > &a)
The Matrix3 class is a template class that implements a generic 3x3 matrix, see also vl::dmat3...
Vector4< T > tan(const Vector4< T > &angle)
The Vector3 class is a template class that implements a generic 3 components vector, see also vl::fvec3, vl::dvec3, vl::uvec3, vl::ivec3, vl::svec3, vl::usvec3, vl::bvec3, vl::ubvec3.
Vector4< int > ivec4
A 4 components vector with int precision.
float max(float a, float b)
float min(float a, float b)
const T_Scalar & e(int i, int j) const
ivec4 greaterThanEqual(const Vector4< T > &a, const Vector4< T > &b)
const T_Scalar & y() const
const T_Scalar & y() const
The Matrix4 class is a template class that implements a generic 4x4 matrix, see also vl::dmat4...
float roundEven(float a, float epsilon)
Matrix2< T > matrixCompMult(const Matrix2< T > &a, const Matrix2< T > &b)
Vector3< int > ivec3
A 3 components vector with int precision.
ivec4 greaterThan(const Vector4< T > &a, const Vector4< T > &b)
The Vector2 class is a template class that implements a generic 2 components vector, see also vl::fvec2, vl::dvec2, vl::uvec2, vl::ivec2, vl::svec2, vl::usvec2, vl::bvec2, vl::ubvec2.
Vector4< T > cos(const Vector4< T > &angle)
Vector4< T > modf(const Vector4< T > &a, Vector4< T > &intpart)
Vector4< T > sqrt(const Vector4< T > &a)
const Vector4 & normalize(T_Scalar *len=NULL)
Vector4< T > acos(const Vector4< T > &angle)
const T_Scalar & x() const
Vector4< T > sin(const Vector4< T > &angle)
Vector4< T > ceil(const Vector4< T > &a)
T smoothstep(T edge0, T edge1, T a)
const T_Scalar & x() const
Vector4< T > asin(const Vector4< T > &angle)
Matrix2< T > outerProduct(const Vector2< T > &a, const Vector2< T > &b)
const T_Scalar & y() const
Vector4< T > atan(const Vector4< T > &a, const Vector4< T > &b)
ivec4 lessThanEqual(const Vector4< T > &a, const Vector4< T > &b)
ivec4 equal(const Vector4< T > &a, const Vector4< T > &b)
const T_Scalar & w() const