📄 mathfunc.h
字号:
//
// mathfunc.h
//
// $Id: mathfunc.h,v 1.1.1.1 2001/02/28 00:28:37 cstolte Exp $
//
#ifndef SGL_MATHFUNC_H
#define SGL_MATHFUNC_H
#include <math.h>
#include <sgl/defs.h>
class RGB;
class Vector3;
class Normal3;
const double epsilon = 1e-8;
class Point2;
class Point3;
class Vector2;
class Vector3;
class Normal2;
class Normal3;
inline double
lerp(double pos, double start, double end)
{
return start + (pos * (end - start));
}
extern Point2 lerp(const Point2 &, const Point2 &, double);
extern Point3 lerp(const Point3 &, const Point3 &, double);
extern Vector2 lerp(const Vector2 &, const Vector2 &, double);
extern Vector3 lerp(const Vector3 &, const Vector3 &, double);
extern Normal2 lerp(const Normal2 &, const Normal2 &, double);
extern Normal3 lerp(const Normal3 &, const Normal3 &, double);
inline int
zero(double x, double eps = epsilon)
{
return ( -eps < x ) && ( x < eps );
}
inline int
equal(double a, double b)
{
return (zero(a-b));
}
template <class T, class U, class V> inline T
clamp(const T &val, const U &min, const V &max)
{
if (val < min)
return min;
else if (val > max)
return max;
else
return val;
}
#if 0
inline double
clamp(double val, double min, double max)
{
if (val < min)
return min;
else if (val > max)
return max;
else
return val;
}
inline int
clamp(int val, int min, int max)
{
if (val < min)
return min;
else if (val > max)
return max;
else
return val;
}
#endif
#ifdef MINMAX
template <class T> inline T
max(const T &a, const T &b)
{
if (a >= b)
return a;
else
return b;
}
template <class T> inline T
min(const T &a, const T &b)
{
if (a <= b)
return a;
else
return b;
}
#endif
// min
inline int
min(int a, int b, int c)
{
if (a <= b) {
if (a <= c) return a;
else return c;
}
else {
if (b <= c) return b;
else return c;
}
}
inline float
min(float a, float b, float c)
{
if (a <= b) {
if (a <= c) return a;
else return c;
}
else {
if (b <= c) return b;
else return c;
}
}
inline double
min(double a, double b, double c)
{
if (a <= b) {
if (a <= c) return a;
else return c;
}
else {
if (b <= c) return b;
else return c;
}
}
inline int
min(int a, int b, int c, int d)
{
if (a <= b) {
if (a <= c) {
if (a <= d) return a;
else return d;
}
else {
if (c <= d) return c;
else return d;
}
}
else {
if (b <= c) {
if (b <= d) return b;
else return d;
}
else {
if (c <= d) return c;
else return d;
}
}
}
inline float
min(float a, float b, float c, float d)
{
if (a <= b) {
if (a <= c) {
if (a <= d) return a;
else return d;
}
else {
if (c <= d) return c;
else return d;
}
}
else {
if (b <= c) {
if (b <= d) return b;
else return d;
}
else {
if (c <= d) return c;
else return d;
}
}
}
inline double
min(double a, double b, double c, double d)
{
if (a <= b) {
if (a <= c) {
if (a <= d) return a;
else return d;
}
else {
if (c <= d) return c;
else return d;
}
}
else {
if (b <= c) {
if (b <= d) return b;
else return d;
}
else {
if (c <= d) return c;
else return d;
}
}
}
// max
inline int
max(int a, int b, int c)
{
if (a >= b) {
if (a >= c) return a;
else return c;
}
else {
if (b >= c) return b;
else return c;
}
}
inline float
max(float a, float b, float c)
{
if (a >= b) {
if (a >= c) return a;
else return c;
}
else {
if (b >= c) return b;
else return c;
}
}
inline double
max(double a, double b, double c)
{
if (a >= b) {
if (a >= c) return a;
else return c;
}
else {
if (b >= c) return b;
else return c;
}
}
inline int
max(int a, int b, int c, int d)
{
if (a >= b) {
if (a >= c) {
if (a >= d) return a;
else return d;
}
else {
if (c >= d) return c;
else return d;
}
}
else {
if (b >= c) {
if (b >= d) return b;
else return d;
}
else {
if (c >= d) return c;
else return d;
}
}
}
inline float
max(float a, float b, float c, float d)
{
if (a >= b) {
if (a >= c) {
if (a >= d) return a;
else return d;
}
else {
if (c >= d) return c;
else return d;
}
}
else {
if (b >= c) {
if (b >= d) return b;
else return d;
}
else {
if (c >= d) return c;
else return d;
}
}
}
inline double
max(double a, double b, double c, double d)
{
if (a >= b) {
if (a >= c) {
if (a >= d) return a;
else return d;
}
else {
if (c >= d) return c;
else return d;
}
}
else {
if (b >= c) {
if (b >= d) return b;
else return d;
}
else {
if (c >= d) return c;
else return d;
}
}
}
template <class T> inline T
sqr(const T &a)
{
return a * a;
}
extern int cube( int s );
extern unsigned long cube(unsigned long s);
extern double cube( double f );
extern int even( int );
extern int odd( int );
inline float frac(float f) { return f >= 0. ? f - int(f) : frac(-f); }
inline double frac(double d) { return d >= 0. ? d - int(d) : frac(-d); }
extern int quadratic(float a, float b, float c, float *t,
float mint = 1e-6);
extern int quadratic(double a, double b, double c, double *t,
double mint = 1e-6);
inline double
radians(double a)
{
return (M_PI/180.)*a;
}
inline double
degrees(double a)
{
return (180./M_PI)*a;
}
// Greatest common divisor, least common multiple
extern int gcd(int, int);
extern int lcm(int, int);
extern double mod(double, double);
extern float mod(float, float);
extern int mod(int, int);
extern u_int next_prime(u_int start);
extern double bias(double g, double x);
extern double gain(double g, double x);
extern double gammacorrect(double g, double x);
extern RGB gammacorrect(double g, const RGB &color);
extern Vector3 faceforward(const Vector3 &, const Vector3 &);
extern Normal3 faceforward(const Normal3 &, const Vector3 &);
extern Vector3 faceforward(const Vector3 &, const Normal3 &);
extern int step(double min, double val);
extern int pulse(double min, double max, double val);
extern double boxstep(double min, double max, double val);
extern double smoothstep(double min, double max, double val);
extern double spline(double t, const double *pts, int n_pts);
/*extern RGB spline(RGB t, const RGB *pts, int n_pts);*/
extern Vector3 spline(double t, const Vector3 *pts, int n_pts);
extern Normal3 spline(double t, const Normal3 *pts, int n_pts);
extern double ptlined(const Point3 &l0, const Point3 &l1, const Point3 &p);
extern double ptlined2(const Point3 &l0, const Point3 &l1,
const Point3 &p);
extern double ptsegd(const Point3 &l0, const Point3 &l1, const Point3 &p);
extern double ptsegd2(const Point3 &l0, const Point3 &l1,
const Point3 &p);
#endif /* SGL_MATHFUNC_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -