📄 vecmath2.h
字号:
//
// vecmath2.h
//
// $Id: vecmath2.h,v 1.1.1.1 2001/02/28 00:28:40 cstolte Exp $
//
#ifndef SGL_VECMATH2_H
#define SGL_VECMATH2_H
#include <math.h>
#include <sgl/defs.h>
#include <sgl/mathfunc.h>
#include <sgl/tuples.h>
class Point2;
class Vector2;
class Normal2;
class Ray2;
/////////
// Point2
//
class Point2 : public Tuple2d {
public:
Point2() { }
Point2(double x, double y) : Tuple2d(x, y) { }
Point2(const Tuple2d &t) : Tuple2d(t) { }
Vector2 operator-(const Point2 &) const;
inline Point2 operator+(const Vector2 &) const;
Point2 &operator+=(const Vector2 &);
Point2 operator-(const Vector2 &) const;
Point2 &operator-=(const Vector2 &);
bool between(const Point2 &, const Point2 &) const;
static const Point2 origin;
};
extern double distance(const Point2 &, const Point2 &);
extern double distance_squared(const Point2 &, const Point2 &);
extern Point2 midpoint(const Point2 &, const Point2 &);
//////////
// Vector2
//
class Vector2 : public Tuple2d {
public:
Vector2() { }
Vector2(double x, double y) : Tuple2d(x, y) { }
Vector2(const Tuple2d &t) : Tuple2d(t) { }
Vector2 operator-() const;
Vector2 operator+(const Vector2 &) const;
Vector2 &operator+=(const Vector2 &);
Vector2 operator-(const Vector2 &) const;
Vector2 &operator-=(const Vector2 &);
Vector2 operator*(double) const;
Vector2 &operator*=(double);
Vector2 operator/(double) const;
Vector2 &operator/=(double);
double length() const { return sqrt(length_squared()); }
double length_squared() const;
Normal2 transpose() const;
Vector2 normalize() const;
Vector2 hat() const { return normalize(); }
};
inline Vector2 operator*(double, const Vector2 &);
extern double cross(const Vector2 &, const Vector2 &);
extern double dot(const Vector2 &, const Vector2 &);
extern double cos(const Vector2 &, const Vector2 &);
//////
// n and wi should be normalized before calling reflection() or
// refraction(), and wi should point away from the surface.
/////
extern Vector2 reflection(const Normal2 &n, const Vector2 &wi);
extern int refraction(const Normal2 &n, const Vector2 &wi,
Vector2 *wt, double ni, double nt);
//////////
// Normal2
//
class Normal2 : public Tuple2d {
public:
Normal2() { };
Normal2(double u1, double v1) : Tuple2d(u1, v1) { };
Normal2(const Tuple2d &t) : Tuple2d(t) { }
Normal2 operator-() const;
Normal2 operator+(const Normal2 &) const;
Normal2 &operator+=(const Normal2 &);
Normal2 operator-(const Normal2 &) const;
Normal2 &operator-=(const Normal2 &);
Normal2 operator*(double) const;
Normal2 &operator*=(double);
Normal2 operator/(double) const;
Normal2 &operator/=(double);
double length() const { return sqrt(length_squared()); }
double length_squared() const;
Vector2 transpose() const;
Normal2 normalize() const;
Normal2 hat() const { return normalize(); }
};
inline Normal2 operator*(double, const Normal2 &);
extern double dot(const Normal2 &, const Normal2 &);
extern double dot(const Vector2 &, const Normal2 &);
extern double dot(const Normal2 &, const Vector2 &);
extern double cos(const Normal2 &, const Normal2 &);
extern double cos(const Vector2 &, const Normal2 &);
extern double cos(const Normal2 &, const Vector2 &);
///////
// Ray2
//
class Ray2 {
public:
Ray2() { }
Ray2(const Point2 &org, const Vector2 &di)
: o(org), d(di) { }
Point2 operator()(double t) const { return o + t * d; }
const Point2 &origin() const { return o; }
const Vector2 &dir() const { return d; }
Ray2 hat() const { return Ray2(o, d.hat()); }
Point2 o;
Vector2 d;
};
extern std::ostream &operator<<(std::ostream &, const Ray2 &);
////////////////////
// inline functions
//
inline Vector2
Point2::operator-(const Point2 &p2) const {
return Vector2(u - p2.u, v - p2.v);
}
inline Point2
Point2::operator+(const Vector2 &p2) const {
return Point2(u + p2.u, v + p2.v);
}
inline Point2 &
Point2::operator+=(const Vector2 &p2) {
u += p2.u;
v += p2.v;
return *this;
}
inline Point2
Point2::operator-(const Vector2 &p2) const {
return Point2(u - p2.u, v - p2.v);
}
inline Point2 &
Point2::operator-=(const Vector2 &p2) {
u -= p2.u;
v -= p2.v;
return *this;
}
inline Vector2
Vector2::operator-() const {
return Vector2(-u, -v);
}
inline Vector2
Vector2::operator+(const Vector2 &v2) const {
return Vector2(u + v2.u, v + v2.v);
}
inline Vector2 &
Vector2::operator+=(const Vector2 &v2) {
u += v2.u;
v += v2.v;
return *this;
}
inline Vector2
Vector2::operator-(const Vector2 &v2) const {
return Vector2(u - v2.u, v - v2.v);
}
inline Vector2 &
Vector2::operator-=(const Vector2 &v2) {
u -= v2.u;
v -= v2.v;
return *this;
}
inline Vector2
Vector2::operator*(double d) const {
return Vector2(u * d, v * d);
}
inline Vector2 &
Vector2::operator*=(double d) {
u *= d;
v *= d;
return *this;
}
inline Vector2
Vector2::operator/(double d) const {
return Vector2(u / d, v / d);
}
inline Vector2 &
Vector2::operator/=(double d) {
u /= d;
v /= d;
return *this;
}
inline Vector2
operator*(double d, const Vector2 &v2) {
return Vector2(v2.u * d, v2.v * d);
}
inline Normal2
Normal2::operator-() const {
return Normal2(-u, -v);
}
inline Normal2
Normal2::operator+(const Normal2 &n2) const {
return Normal2(u + n2.u, v + n2.v);
}
inline Normal2 &
Normal2::operator+=(const Normal2 &n2) {
u += n2.u;
v += n2.v;
return *this;
}
inline Normal2
Normal2::operator-(const Normal2 &n2) const {
return Normal2(u - n2.u, v - n2.v);
}
inline Normal2 &
Normal2::operator-=(const Normal2 &n2) {
u -= n2.u;
v -= n2.v;
return *this;
}
inline Normal2
Normal2::operator*(double d) const {
return Normal2(u * d, v * d);
}
inline Normal2 &
Normal2::operator*=(double d) {
u *= d;
v *= d;
return *this;
}
inline Normal2
Normal2::operator/(double d) const {
return Normal2(u / d, v / d);
}
inline Normal2 &
Normal2::operator/=(double d) {
u /= d;
v /= d;
return *this;
}
inline Normal2
operator*(double d, const Normal2 &n2) {
return Normal2(n2.u * d, n2.v * d);
}
#endif /* SGL_VECMATH2_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -