00001 #include "SFVec3f.h"
00002 #include "SFPoint3f.h"
00003 #include "SFString.h"
00004
00005 #include <sstream>
00006
00007 using namespace X3DTK;
00008 using namespace std;
00009
00010
00011 SFVec3f::SFVec3f()
00012 {
00013 x = 0.0f;
00014 y = 0.0f;
00015 z = 0.0f;
00016 }
00017
00018 SFVec3f::SFVec3f(float x, float y, float z)
00019 {
00020 this->x = x;
00021 this->y = y;
00022 this->z = z;
00023 }
00024
00025 SFVec3f::SFVec3f(const SFVec3f &v)
00026 {
00027 x = v.x;
00028 y = v.y;
00029 z = v.z;
00030 }
00031
00032 SFVec3f &SFVec3f::operator= (SFVec3f v)
00033 {
00034 x = v.x;
00035 y = v.y;
00036 z = v.z;
00037 return *this;
00038 }
00039
00040 SFVec3f::SFVec3f(const SFString &s)
00041 {
00042 istringstream iss(s, istringstream::in);
00043 iss >> x >> y >> z;
00044 }
00045
00046 SFVec3f::SFVec3f(const SFPoint3f &P)
00047 {
00048 x = P.x;
00049 y = P.y;
00050 z = P.z;
00051 }
00052
00053
00054
00055 bool X3DTK::operator== (const SFVec3f &v1, const SFVec3f &v2)
00056 {
00057 return ((v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z));
00058 }
00059
00060 bool X3DTK::operator!= (const SFVec3f &v1, const SFVec3f &v2)
00061 {
00062 return ((v1.x != v2.x) || (v1.y != v2.y) || (v1.z != v2.z));
00063 }
00064
00065 SFVec3f X3DTK::operator+ (const SFVec3f &v1, const SFVec3f &v2)
00066 {
00067 SFVec3f res;
00068 res.x = v1.x + v2.x;
00069 res.y = v1.y + v2.y;
00070 res.z = v1.z + v2.z;
00071 return res;
00072 }
00073
00074 SFVec3f X3DTK::operator- (const SFVec3f &v1, const SFVec3f &v2)
00075 {
00076 SFVec3f res;
00077 res.x = v1.x - v2.x;
00078 res.y = v1.y - v2.y;
00079 res.z = v1.z - v2.z;
00080 return res;
00081 }
00082
00083
00084 float X3DTK::operator* (const SFVec3f &v1, const SFVec3f &v2)
00085 {
00086 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
00087 }
00088
00089 SFVec3f X3DTK::operator* (const float a, const SFVec3f &v)
00090 {
00091 SFVec3f res;
00092 res.x = a * v.x;
00093 res.y = a * v.y;
00094 res.z = a * v.z;
00095 return res;
00096 }
00097
00098
00099 SFVec3f X3DTK::crossprod(const SFVec3f &v1, const SFVec3f &v2)
00100 {
00101 SFVec3f res;
00102 res.x = v1.y * v2.z - v1.z * v2.y;
00103 res.y = -v1.x * v2.z + v1.z * v2.x;
00104 res.z = v1.x * v2.y - v1.y * v2.x;
00105 return res;
00106 }
00107
00108
00109 float SFVec3f::norm() const
00110 {
00111 return (float)sqrt(x * x + y * y + z * z);
00112 }
00113
00114 SFVec3f SFVec3f::normalize()
00115 {
00116 float r = sqrtf(x * x + y * y + z * z);
00117 if (r != 0.0f)
00118 {
00119 x /= r;
00120 y /= r;
00121 z /= r;
00122 }
00123 return *this;
00124 }
00125
00126 SFVec3f SFVec3f::normalized() const
00127 {
00128 float r = sqrtf(x * x + y * y + z * z);
00129 if (r != 0.0f)
00130 return SFVec3f(x/r, y/r, z/r);
00131 return SFVec3f(0.0f, 0.0f, 0.0f);
00132 }
00133