Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Vec4.cpp Source File

Vec4.cpp

00001 /*
00002     File:           Vec4.cpp
00003 
00004     Function:       Implements Vec4.h
00005 
00006     Author(s):      Andrew Willmott
00007 
00008     Copyright:      (c) 1995-2001, Andrew Willmott
00009 
00010 */
00011 
00012 
00013 #include "Vec4.h"
00014 //#include <cctype>
00015 //#include <iomanip>
00016 
00017 
00018 Vec4 &Vec4::MakeUnit(Int n, Real k)
00019 {
00020     if (n == 0)
00021     { elt[0] = k; elt[1] = vl_zero; elt[2] = vl_zero; elt[3] = vl_zero; }
00022     else if (n == 1)
00023     { elt[0] = vl_zero; elt[1] = k; elt[2] = vl_zero; elt[3] = vl_zero; }
00024     else if (n == 2)
00025     { elt[0] = vl_zero; elt[1] = vl_zero; elt[2] = k; elt[3] = vl_zero; }
00026     else if (n == 3)
00027     { elt[0] = vl_zero; elt[1] = vl_zero; elt[2] = vl_zero; elt[3] = k; }
00028     else
00029         _Error("(Vec4::MakeUnit) illegal unit vector");
00030 
00031     return(SELF);
00032 }
00033 
00034 bool Vec4::operator == (const Vec4 &a) const
00035 {
00036     return(elt[0] == a[0] && elt[1] == a[1] && elt[2] == a[2] && elt[3] == a[3]);
00037 }
00038 
00039 bool Vec4::operator != (const Vec4 &a) const
00040 {
00041     return(elt[0] != a[0] || elt[1] != a[1] || elt[2] != a[2] || elt[3] != a[3]);
00042 }
00043 
00044 Vec4 cross(const Vec4 &a, const Vec4 &b, const Vec4 &c)
00045 {
00046     Vec4 result;
00047     // XXX can this be improved? Look at assembly.
00048 #define ROW(i)       a[i], b[i], c[i]
00049 #define DET(i,j,k)   dot(Vec3(ROW(i)), cross(Vec3(ROW(j)), Vec3(ROW(k))))
00050 
00051     result[0] =  DET(1,2,3);
00052     result[1] = -DET(0,2,3);
00053     result[2] =  DET(0,1,3);
00054     result[3] = -DET(0,1,2);
00055 
00056     return(result);
00057 
00058 #undef ROW
00059 #undef DET
00060 }
00061 
00062 Vec3 proj(const Vec4 &v)
00063 {
00064     Vec3 result;
00065 
00066     Assert(v[3] != 0, "(Vec4/proj) last elt. is zero");
00067 
00068     result[0] = v[0] / v[3];
00069     result[1] = v[1] / v[3];
00070     result[2] = v[2] / v[3];
00071 
00072     return(result);
00073 }
00074 
00075 /*
00076 ostream &operator << (ostream &s, const Vec4 &v)
00077 {
00078     Int w = s.width();
00079 
00080     return(s << '[' << v[0] << ' ' << setw(w) << v[1] << ' '
00081         << setw(w) << v[2] << ' ' << setw(w) << v[3] << ']');
00082 }
00083 
00084 istream &operator >> (istream &s, Vec4 &v)
00085 {
00086     Vec4    result;
00087     Char    c;
00088 
00089     // Expected format: [1 2 3 4]
00090 
00091     while (s >> c && isspace(c))
00092         ;
00093 
00094     if (c == '[')
00095     {
00096         s >> result[0] >> result[1] >> result[2] >> result[3];
00097 
00098         if (!s)
00099         {
00100             cerr << "Error: Expected number while reading vector\n";
00101             return(s);
00102         }
00103 
00104         while (s >> c && isspace(c))
00105             ;
00106 
00107         if (c != ']')
00108         {
00109             s.clear(ios::failbit);
00110             cerr << "Error: Expected ']' while reading vector\n";
00111             return(s);
00112         }
00113     }
00114     else
00115     {
00116         s.clear(ios::failbit);
00117         cerr << "Error: Expected '[' while reading vector\n";
00118         return(s);
00119     }
00120 
00121     v = result;
00122     return(s);
00123 }
00124 */