From: Roland Hieber Date: Tue, 12 Jan 2010 23:12:44 +0000 (+0100) Subject: vector class X-Git-Url: https://git.rohieb.name/MicroTrace.git/commitdiff_plain/5fb5dec08b41015ffe80a2607d244c248e21c4ad vector class --- diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..571a5eb --- /dev/null +++ b/.cproject @@ -0,0 +1,209 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c86eb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +MicroTrace diff --git a/.project b/.project new file mode 100644 index 0000000..6a4bde3 --- /dev/null +++ b/.project @@ -0,0 +1,78 @@ + + + MicroTrace01 + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.autoBuildTarget + all + + + org.eclipse.cdt.make.core.buildArguments + + + + org.eclipse.cdt.make.core.buildCommand + make + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.fullBuildTarget + all + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + + + + + + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.core.cnature + + diff --git a/Vec3f.cxx b/Vec3f.cxx index c063913..8dfa3d7 100644 --- a/Vec3f.cxx +++ b/Vec3f.cxx @@ -2,162 +2,143 @@ #include "Vec3f.hxx" -Vec3f::Vec3f() -{ +Vec3f::Vec3f() { +} +Vec3f::Vec3f(float x, float y, float z) { + m_values[0] = x; + m_values[0] = y; + m_values[0] = z; } -Vec3f::Vec3f(float x, float y, float z) -{ +Vec3f::~Vec3f() { +} +Vec3f::Vec3f(const Vec3f& o) { + for (size_t i = 0; i < DIM; i++) { + m_values[i] = o.m_values[i]; + } } -Vec3f::~Vec3f() -{ +Vec3f& Vec3f::operator=(const Vec3f& o) { + for (size_t i = 0; i < DIM; i++) { + m_values[i] = o.m_values[i]; + } + return *this; } -Vec3f::Vec3f(const Vec3f& o) -{ +float Vec3f::operator|(const Vec3f& o) { + return dot(o); } -Vec3f& -Vec3f::operator=(const Vec3f& o) -{ - - return *this; +float Vec3f::dot(const Vec3f& o) { + float prod = 0; + for (size_t i = 0; i < DIM; i++) { + prod += m_values[i] * o.m_values[i]; + } + return prod; } -float -Vec3f::operator|(const Vec3f& o) -{ - - return 0.0f; +Vec3f Vec3f::operator%(const Vec3f& o) { + return cross(o); } -float -Vec3f::dot(const Vec3f& o) -{ - - return 0.0f; +Vec3f Vec3f::cross(const Vec3f& o) { + return Vec3f(m_values[1] * o.m_values[2] - m_values[2] * o.m_values[1], + m_values[2] * o.m_values[0] - m_values[0] * o.m_values[2], m_values[0] + * o.m_values[1] - m_values[1] * o.m_values[0]); } - -Vec3f -Vec3f::operator%(const Vec3f& o) -{ - return Vec3f(); -} +void Vec3f::normalize() { -Vec3f -Vec3f::cross(const Vec3f& o) -{ - - return Vec3f(); -} - -void -Vec3f::normalize() -{ - } -float -Vec3f::norm() const -{ - return 0.0f; +float Vec3f::norm() const { + return sqrt(m_values[0] * m_values[0] + m_values[1] * m_values[1] + + m_values[2] * m_values[2]); } -Vec3f -Vec3f::operator*(const float t) const -{ - return Vec3f(); +Vec3f Vec3f::operator*(const float t) const { + Vec3f v(*this); + return v *= t; } -Vec3f& -Vec3f::operator*=(const float t) -{ - return *this; +Vec3f& Vec3f::operator*=(const float t) { + for (unsigned int i = 0; i < 3; ++i) { + m_values[i] *= f; + } + return *this; } -Vec3f -Vec3f::operator/(const float t) const -{ - return Vec3f(); +Vec3f Vec3f::operator/(const float t) const { + Vec3f v(*this); + return v /= f; } -Vec3f& -Vec3f::operator/=(const float t) -{ - return *this; +Vec3f& Vec3f::operator/=(const float t) { + for (unsigned int i = 0; i < 3; ++i) { + m_values[i] /= f; + } + return *this; } // example implementation of a standard operator -Vec3f -Vec3f::operator+(const Vec3f& o) const -{ - Vec3f v(*this); - - return v += o; +Vec3f Vec3f::operator+(const Vec3f& o) const { + Vec3f v(*this); + return v += o; } // example implementation of an in-place operator -Vec3f& -Vec3f::operator+=(const Vec3f& o) -{ - for(unsigned int i = 0; i < 3; ++i) - { - m_values[i] += o.m_values[i]; - } - - return *this; +Vec3f& Vec3f::operator+=(const Vec3f& o) { + for (unsigned int i = 0; i < 3; ++i) { + m_values[i] += o.m_values[i]; + } + return *this; } - -Vec3f -Vec3f::operator-(const Vec3f& o) const -{ - return Vec3f(); + +Vec3f Vec3f::operator-(const Vec3f& o) const { + Vec3f v(*this); + return v -= o; } -Vec3f& -Vec3f::operator-=(const Vec3f& o) -{ - return *this; +Vec3f& Vec3f::operator-=(const Vec3f& o) { + for (unsigned int i = 0; i < 3; ++i) { + m_values[i] -= o.m_values[i]; + } + return *this; } - -Vec3f -Vec3f::operator*(const Vec3f& o) const -{ - return Vec3f(); + +Vec3f Vec3f::operator*(const Vec3f& o) const { + Vec3f v(*this); + return v *= o; } -Vec3f& -Vec3f::operator*=(const Vec3f& o) -{ - return *this; +Vec3f& Vec3f::operator*=(const Vec3f& o) { + for (unsigned int i = 0; i < 3; ++i) { + m_values[i] *= o.m_values[i]; + } + return *this; } -Vec3f -Vec3f::operator/(const Vec3f& o) const -{ - return Vec3f(); +Vec3f Vec3f::operator/(const Vec3f& o) const { + Vec3f v(*this); + return v /= o; } -Vec3f& -Vec3f::operator/=(const Vec3f& o) -{ - return *this; +Vec3f& Vec3f::operator/=(const Vec3f& o) { + for (unsigned int i = 0; i < 3; ++i) { + m_values[i] /= o.m_values[i]; + } + return *this; } -float -Vec3f::operator[](unsigned int i) const -{ - return 0.0f; +float Vec3f::operator[](unsigned int i) const { + assert(i < DIM); + return m_values[i]; } -float& -Vec3f::operator[](unsigned int i) -{ - assert(i < 3); - return m_values[i]; +float& Vec3f::operator[](unsigned int i) { + assert(i < DIM); + return m_values[i]; } diff --git a/Vec3f.hxx b/Vec3f.hxx index 74f304b..7f0c77d 100644 --- a/Vec3f.hxx +++ b/Vec3f.hxx @@ -4,7 +4,6 @@ #include #include - #ifndef MIN #define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) @@ -16,70 +15,70 @@ #define Epsilon 1E-4 #define Infinity HUGE_VAL +// dimension +#define DIM 3 + //! Standard operators and useful methods for 3d vectors //! Fill in the missing parts in Vec3f.cxx -class Vec3f -{ +class Vec3f { public: - Vec3f(); - Vec3f(float x, float y, float z); - - ~Vec3f(); - - Vec3f(const Vec3f& o); - Vec3f& operator=(const Vec3f& o); - - //! dot product - float operator|(const Vec3f& o); - float dot(const Vec3f& o); - - //! cross product - Vec3f operator%(const Vec3f& o); - Vec3f cross(const Vec3f& o); - - //! normalize vector - void normalize(); - //! length of a vector - float norm() const; - - //! (self-)multiply with scalar - Vec3f operator*(const float t) const; - Vec3f& operator*=(const float t); - - //! (self-)division by scalar - Vec3f operator/(const float t) const; - Vec3f& operator/=(const float t); - - //! vector (self-)addition - Vec3f operator+(const Vec3f& o) const; - Vec3f& operator+=(const Vec3f& o); - - //! vector (self-)subtraction - Vec3f operator-(const Vec3f& o) const; - Vec3f& operator-=(const Vec3f& o); - - //! component-wise multiplication of two vectors - Vec3f operator*(const Vec3f& o) const; - Vec3f& operator*=(const Vec3f& o); - - //! component-wise division of two vectors - Vec3f operator/(const Vec3f& o) const; - Vec3f& operator/=(const Vec3f& o); - - //! element access functions ( read-only and read-write ) - float operator[](unsigned int i) const; - float& operator[](unsigned int i); - + Vec3f(); + Vec3f(float x, float y, float z); + + ~Vec3f(); + + Vec3f(const Vec3f& o); + Vec3f& operator=(const Vec3f& o); + + //! dot product + float operator|(const Vec3f& o); + float dot(const Vec3f& o); + + //! cross product + Vec3f operator%(const Vec3f& o); + Vec3f cross(const Vec3f& o); + + //! normalize vector + void normalize(); + //! length of a vector + float norm() const; + + //! (self-)multiply with scalar + Vec3f operator*(const float t) const; + Vec3f& operator*=(const float t); + + //! (self-)division by scalar + Vec3f operator/(const float t) const; + Vec3f& operator/=(const float t); + + //! vector (self-)addition + Vec3f operator+(const Vec3f& o) const; + Vec3f& operator+=(const Vec3f& o); + + //! vector (self-)subtraction + Vec3f operator-(const Vec3f& o) const; + Vec3f& operator-=(const Vec3f& o); + + //! component-wise multiplication of two vectors + Vec3f operator*(const Vec3f& o) const; + Vec3f& operator*=(const Vec3f& o); + + //! component-wise division of two vectors + Vec3f operator/(const Vec3f& o) const; + Vec3f& operator/=(const Vec3f& o); + + //! element access functions ( read-only and read-write ) + float operator[](unsigned int i) const; + float& operator[](unsigned int i); + private: - float m_values[3]; + float m_values[3]; }; -inline std::ostream& operator<<(std::ostream& o, const Vec3f& v) -{ - o << "(" << v[0] << "," << v[1] << "," << v[2] << ")"; - return o; +inline std::ostream& operator<<(std::ostream& o, const Vec3f& v) { + o << "(" << v[0] << "," << v[1] << "," << v[2] << ")"; + return o; } #endif -