Skip to content

Instantly share code, notes, and snippets.

@radscheit
Created July 12, 2015 21:59
Show Gist options
  • Save radscheit/2617d0434884017a9e99 to your computer and use it in GitHub Desktop.
Save radscheit/2617d0434884017a9e99 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cassert>
#include <cmath>
#include <stdlib.h>
#include <time.h>
#include <vector>
class Vector3d {
private:
double x_;
double y_;
double z_;
public:
Vector3d(): x_{0}, y_{0}, z_{0} {}
Vector3d(double x, double y, double z): x_{x}, y_{y}, z_{z} {}
const double& operator[] (int index) const {
if (index==0)
return this->x_;
if (index==1)
return this->y_;
return this->z_;
}
double& operator[] (int index) {
if (index==0)
return this->x_;
if (index==1)
return this->y_;
return this->z_;
}
Vector3d operator-() {
return (Vector3d&) *this - ((double)2* *this);
}
Vector3d operator-(const Vector3d& other) const {
Vector3d tmp = *this;
return tmp -= other;
}
bool operator==(const Vector3d& other) const {
return x_ == other.x_ && y_ == other.y_ && z_ == other.z_;
}
bool operator!=(const Vector3d& other) const {
return x_ != other.x_ || y_ != other.y_ || z_ != other.z_;
}
Vector3d& operator+=(const Vector3d& other) {
x_ += other.x_;
y_ += other.y_;
z_ += other.z_;
return *this;
}
Vector3d& operator-=(const Vector3d& other) {
x_ -= other.x_;
y_ -= other.y_;
z_ -= other.z_;
return *this;
}
Vector3d& operator*=(double scalar) {
x_ *= scalar;
y_ *= scalar;
z_ *= scalar;
return *this;
}
Vector3d& operator/=(double scalar) {
assert(scalar != 0);
x_ /= scalar;
y_ /= scalar;
z_ /= scalar;
return *this;
}
Vector3d cross(const Vector3d& other) const {
return Vector3d{
y_ * other.z_ - z_ * other.y_,
z_ * other.x_ - x_ * other.z_,
x_ * other.y_ - y_ * other.x_};
}
double dot(const Vector3d& v1) const
{
return v1.x_ * x_ + v1.y_ * y_ + v1.z_ * z_;
}
double length() const
{
return sqrt(dot(*this));
}
void normalize()
{
*this /= length();
}
void print(Vector3d& v)
{
std::cout << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")" << std::endl;
}
};
std::ostream& operator<<(std::ostream &os, Vector3d &v) {
return os << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")"; }
Vector3d operator*(double scalar, Vector3d V)
{
return Vector3d{scalar*V[0], scalar*V[1], scalar*V[2]};
}
void test() {
// test default constructor
Vector3d v1;
std::cout << "test: default ctor" << std::endl;
std::cout << "(0.0,0.0,0.0):\t" << v1 << std::endl;
assert(v1[0]==0.0 && v1[1]==0.0 && v1[2]==0.0);
//test init-list
std::cout << "\ntest: init-list ctor" << std::endl;
Vector3d v2 {2.0, 3.0, 4.0};
std::cout << "(2.0,3.0,4.0):\t" << v2 << std::endl;
assert(v2[0]==2.0 && v2[1]==3.0 && v2[2]==4.0);
// test assignment
std::cout << "\ntest: assignment" << std::endl;
Vector3d v3;
v3 = v2;
std::cout << "(2.0,3.0,4.0):\t" << v3 << std::endl;
assert(v3[0]==2.0 && v3[1]==3.0 && v3[2]==4.0);
// test init-list for containers
std::cout << "\ntest: init list for container" << std::endl;
std::cout << "the x, y, and z axis:" << std::endl;
std::vector<Vector3d> vc {{1.0,0.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}};
for(auto v : vc) std::cout << v << "\t"; std::cout << std::endl;
assert(vc[0][0]==1.0 && vc[0][1]==0.0 && vc[0][2]==0.0);
assert(vc[1][0]==0.0 && vc[1][1]==1.0 && vc[1][2]==0.0);
assert(vc[2][0]==0.0 && vc[2][1]==0.0 && vc[2][2]==1.0);
// scalar multiplication
std::cout << "\ntest: scalar scaling" << std::endl;
std::cout << "scaling by 0.5" << std::endl;
for(auto& v : vc) v = 0.5*v;
for(auto v : vc) std::cout << v << "\t"; std::cout << std::endl;
assert(vc[0][0]==0.5 && vc[0][1]==0.0 && vc[0][2]==0.0);
assert(vc[1][0]==0.0 && vc[1][1]==0.5 && vc[1][2]==0.0);
assert(vc[2][0]==0.0 && vc[2][1]==0.0 && vc[2][2]==0.5);
// normalization
std::cout << "\ntest: normalization" << std::endl;
std::cout << "scaling by 0.5" << std::endl;
for(auto& v : vc) v.normalize();
for(auto v : vc) std::cout << v << "\t"; std::cout << std::endl;
// vector addition
std::cout << "\ntest: addition" << std::endl;
Vector3d v4 {0,0,0};
for(auto& v : vc) v4 += v;
for(auto v : vc) std::cout << v << "\t";
std::cout << " all added ==> " << v4 << std::endl;
assert(v4[0]==1.0 && v4[1]==1.0 && v4[2]==1.0);
// vector substraction
std::cout << "\ntest: substraction" << std::endl;
Vector3d v5 {0,0,0};
for(auto& v : vc) v5 -= v;
for(auto v : vc) std::cout << v << "\t";
std::cout << " all substracted ==> " << v5 << std::endl;
assert(v5[0]==-1.0 && v5[1]==-1.0 && v5[2]==-1.0);
// unary minus
std::cout << "\ntest: unary minus" << std::endl;
Vector3d v7 {1,2,3};
Vector3d v8;
v8 = -v7;
std::cout << "-" << v7 << " = " << v8 << std::endl;
assert(v8[0]==-1.0 && v8[1]==-2.0 && v8[2]==-3.0);
// cross product
std::cout << "\ntest: cross product" << std::endl;
Vector3d v100 {1,0,0};
Vector3d v010 {0,1,0};
Vector3d v001 = cross(v100, v010);
std::cout << v100 << "x" << v010 << " = " << v001 << std::endl;
assert(v001[0]==0.0 && v001[1]==0.0 && v001[2]==1.0);
// scalar product
std::cout << "\ntest: dot product" << std::endl;
double sp1 = dot(v100,v010);
std::cout << v100 << "." << v010 << " = " << sp1 << std::endl;
assert(sp1==0);
double sp2 = dot(v100,v100);
std::cout << v100 << "." << v100 << " = " << sp2 << std::endl;
assert(sp2==1);
}
int main()
{
test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment