57 lines
1.3 KiB
C++
57 lines
1.3 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef K_MATH_MATH_H
|
||
#define K_MATH_MATH_H
|
||
|
||
#include "../Defines.h"
|
||
#include "../geo/Point3.h"
|
||
|
||
class Math {
|
||
|
||
public:
|
||
|
||
/** ensure val stays within [min:max] */
|
||
template <typename Scalar> static inline Scalar clamp(const Scalar min, const Scalar max, const Scalar val) {
|
||
if (unlikely(val < min)) {return min;}
|
||
if (unlikely(val > max)) {return max;}
|
||
return val;
|
||
}
|
||
|
||
/** get the cross-product of u and v */
|
||
static inline Point3 cross(const Point3 u, const Point3 v) {
|
||
float x = u.y*v.z - u.z*v.y;
|
||
float y = u.z*v.x - u.x*v.z;
|
||
float z = u.x*v.y - u.y*v.x;
|
||
return Point3(x,y,z);
|
||
}
|
||
|
||
/** get the normal-vector for u and v */
|
||
static inline Point3 normal(const Point3 u, const Point3 v) {
|
||
const Point3 p = cross(u,v);
|
||
return p/p.length();
|
||
}
|
||
|
||
};
|
||
|
||
namespace std {
|
||
|
||
template<class T> const T& min(const T& a, const T& b, const T& c) {
|
||
return min(a, min(b,c));
|
||
}
|
||
template<class T> const T& min(const T& a, const T& b, const T& c, const T& d) {
|
||
return min(a, min(b, min(c,d)));
|
||
}
|
||
|
||
}
|
||
|
||
|
||
#endif // K_MATH_MATH_H
|