00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef LUX_NORMAL_H
00024 #define LUX_NORMAL_H
00025
00026 #include <iostream>
00027 #include "vector.h"
00028
00029 namespace lux
00030 {
00031
00032
00033 class Normal {
00034 friend class boost::serialization::access;
00035 public:
00036
00037 Normal(float _x=0, float _y=0, float _z=0)
00038 : x(_x), y(_y), z(_z) {}
00039 Normal operator-() const {
00040 return Normal(-x, -y, -z);
00041 }
00042 Normal operator+ (const Normal &v) const {
00043 return Normal(x + v.x, y + v.y, z + v.z);
00044 }
00045
00046 Normal& operator+=(const Normal &v) {
00047 x += v.x; y += v.y; z += v.z;
00048 return *this;
00049 }
00050 Normal operator- (const Normal &v) const {
00051 return Normal(x - v.x, y - v.y, z - v.z);
00052 }
00053
00054 Normal& operator-=(const Normal &v) {
00055 x -= v.x; y -= v.y; z -= v.z;
00056 return *this;
00057 }
00058 Normal operator* (float f) const {
00059 return Normal(f*x, f*y, f*z);
00060 }
00061
00062 Normal &operator*=(float f) {
00063 x *= f; y *= f; z *= f;
00064 return *this;
00065 }
00066 Normal operator/ (float f) const {
00067 float inv = 1.f/f;
00068 return Normal(x * inv, y * inv, z * inv);
00069 }
00070
00071 Normal &operator/=(float f) {
00072 float inv = 1.f/f;
00073 x *= inv; y *= inv; z *= inv;
00074 return *this;
00075 }
00076 float LengthSquared() const { return x*x + y*y + z*z; }
00077 float Length() const { return sqrtf(LengthSquared()); }
00078
00079 explicit Normal(const Vector &v)
00080 : x(v.x), y(v.y), z(v.z) {}
00081 float operator[](int i) const { return (&x)[i]; }
00082 float &operator[](int i) { return (&x)[i]; }
00083
00084 float x,y,z;
00085
00086 private:
00087 template<class Archive>
00088 void serialize(Archive & ar, const unsigned int version)
00089 {
00090 ar & x;
00091 ar & y;
00092 ar & z;
00093 }
00094 };
00095
00096 inline Normal operator*(float f, const Normal &n) {
00097 return Normal(f*n.x, f*n.y, f*n.z);
00098 }
00099
00100 inline Vector::Vector(const Normal &n)
00101 : x(n.x), y(n.y), z(n.z) { }
00102
00103 inline ostream &operator<<(ostream &os, const Normal &v) {
00104 os << v.x << ", " << v.y << ", " << v.z;
00105 return os;
00106 }
00107
00108 inline Normal Normalize(const Normal &n) {
00109 return n / n.Length();
00110 }
00111
00112 inline float Dot(const Normal &n1, const Normal &n2) {
00113 return n1.x * n2.x + n1.y * n2.y + n1.z * n2.z;
00114 }
00115
00116 inline float AbsDot(const Normal &n1, const Normal &n2) {
00117 return fabsf(n1.x * n2.x + n1.y * n2.y + n1.z * n2.z);
00118 }
00119
00120 }
00121
00122 #ifdef LUX_VECTOR_H
00123 #include "vector_normal.h"
00124 #endif
00125
00126 #endif //LUX_NORMAL_H