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_GEOMETRY_H
00024 #define LUX_GEOMETRY_H
00025
00026 #include "lux.h"
00027 #include <float.h>
00028
00029 #ifdef LUX_USE_SSE
00030 #include <xmmintrin.h>
00031 #endif
00032
00033
00034 #include "geometry/vector.h"
00035 #include "geometry/point.h"
00036 #include "geometry/normal.h"
00037 #ifndef LUX_USE_SSE
00038 #include "geometry/matrix4x4.h"
00039 #else
00040 #include "geometry/matrix4x4-sse.h"
00041 #endif
00042 #include "geometry/ray.h"
00043 #include "geometry/raydifferential.h"
00044 #include "geometry/bbox.h"
00045 #include "geometry/transform.h"
00046
00047 namespace lux
00048 {
00049
00050
00051
00052
00053 inline float Dot(const Vector &v1, const Vector &v2) {
00054 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
00055 }
00056
00057 inline float AbsDot(const Vector &v1, const Vector &v2) {
00058 return fabsf(Dot(v1, v2));
00059 }
00060
00061
00062 inline Vector Cross(const Vector &v1, const Vector &v2) {
00063 return Vector((v1.y * v2.z) - (v1.z * v2.y),
00064 (v1.z * v2.x) - (v1.x * v2.z),
00065 (v1.x * v2.y) - (v1.y * v2.x));
00066 }
00067
00068 inline Vector Cross(const Vector &v1, const Normal &v2) {
00069 return Vector((v1.y * v2.z) - (v1.z * v2.y),
00070 (v1.z * v2.x) - (v1.x * v2.z),
00071 (v1.x * v2.y) - (v1.y * v2.x));
00072 }
00073 inline Vector Cross(const Normal &v1, const Vector &v2) {
00074 return Vector((v1.y * v2.z) - (v1.z * v2.y),
00075 (v1.z * v2.x) - (v1.x * v2.z),
00076 (v1.x * v2.y) - (v1.y * v2.x));
00077 }
00078
00079 inline Vector Normalize(const Vector &v) {
00080 return v / v.Length();
00081 }
00082
00083 inline void CoordinateSystem(const Vector &v1, Vector *v2, Vector *v3) {
00084 if (fabsf(v1.x) > fabsf(v1.y)) {
00085 float invLen = 1.f / sqrtf(v1.x*v1.x + v1.z*v1.z);
00086 *v2 = Vector(-v1.z * invLen, 0.f, v1.x * invLen);
00087 }
00088 else {
00089 float invLen = 1.f / sqrtf(v1.y*v1.y + v1.z*v1.z);
00090 *v2 = Vector(0.f, v1.z * invLen, -v1.y * invLen);
00091 }
00092 *v3 = Cross(v1, *v2);
00093 }
00094 inline float Distance(const Point &p1, const Point &p2) {
00095 return (p1 - p2).Length();
00096 }
00097 inline float DistanceSquared(const Point &p1, const Point &p2) {
00098 return (p1 - p2).LengthSquared();
00099 }
00100
00101 inline Normal Normalize(const Normal &n) {
00102 return n / n.Length();
00103 }
00104 inline float Dot(const Normal &n1, const Vector &v2) {
00105 return n1.x * v2.x + n1.y * v2.y + n1.z * v2.z;
00106 }
00107 inline float Dot(const Vector &v1, const Normal &n2) {
00108 return v1.x * n2.x + v1.y * n2.y + v1.z * n2.z;
00109 }
00110 inline float Dot(const Normal &n1, const Normal &n2) {
00111 return n1.x * n2.x + n1.y * n2.y + n1.z * n2.z;
00112 }
00113 inline float AbsDot(const Normal &n1, const Vector &v2) {
00114 return fabsf(n1.x * v2.x + n1.y * v2.y + n1.z * v2.z);
00115 }
00116 inline float AbsDot(const Vector &v1, const Normal &n2) {
00117 return fabsf(v1.x * n2.x + v1.y * n2.y + v1.z * n2.z);
00118 }
00119 inline float AbsDot(const Normal &n1, const Normal &n2) {
00120 return fabsf(n1.x * n2.x + n1.y * n2.y + n1.z * n2.z);
00121 }
00122
00123
00124
00125 inline Vector SphericalDirection(float sintheta,
00126 float costheta, float phi) {
00127 return Vector(sintheta * cosf(phi),
00128 sintheta * sinf(phi),
00129 costheta);
00130 }
00131 inline Vector SphericalDirection(float sintheta,
00132 float costheta,
00133 float phi,
00134 const Vector &x,
00135 const Vector &y,
00136 const Vector &z) {
00137 return sintheta * cosf(phi) * x +
00138 sintheta * sinf(phi) * y + costheta * z;
00139 }
00140 inline float SphericalTheta(const Vector &v) {
00141 return acosf(Clamp(v.z, -1.f, 1.f));
00142 }
00143 inline float SphericalPhi(const Vector &v) {
00144 float p = atan2f(v.y, v.x);
00145 return (p < 0.f) ? p + 2.f*M_PI : p;
00146 }
00147
00148 }
00149
00150 #endif // LUX_GEOMETRY_H