#include "vec.h" #include <stdio.h> #include <math.h> #include <string> Vec::Vec() { Vec(0.0f, 0.0f, 0.0f); } Vec::Vec(float vx, float vy, float vz) { c[0] = vx; c[1] = vy; c[2] = vz; } Vec::Vec(Vec* v) { Vec(v->x(), v->y(), v->z()); /* TODO: copy registered faces ?? */ } float Vec::x() { return c[0]; } float Vec::y() { return c[1]; } float Vec::z() { return c[2]; } float* Vec::getCoords() { return c; } Vec Vec::operator+(Vec* v) { return Vec(x() + v->x(), y() + v->y(), z() + v->z()); } Vec Vec::operator+(Vec v) { return Vec(x() + v.x(), y() + v.y(), z() + v.z()); } void Vec::add(Vec a) { c[0] += a.x(); c[1] += a.y(); c[2] += a.z(); } void Vec::sub(Vec a) { c[0] -= a.x(); c[1] -= a.y(); c[2] -= a.z(); } void Vec::add(Vec& a) { c[0] += a.x(); c[1] += a.y(); c[2] += a.z(); } void Vec::sub(Vec& a) { c[0] -= a.x(); c[1] -= a.y(); c[2] -= a.z(); } Vec Vec::operator-(Vec& v) { return Vec(x() - v.x(), y() - v.y(), z() - v.z()); } Vec Vec::operator*(float s) { Vec r = Vec(x()*s, y()*s, z()*s); return r; } Vec Vec::operator*(Vec& s) { return Vec(x()*s.x(), y()*s.y(), z()*s.z()); } Vec Vec::operator/(float s) { return Vec(x()/s, y()/s, z()/s); } float Vec::angle(Vec& v) { return acos((*this).dotP(v)/((*this).length()*v.length())); } float Vec::dotP(Vec &v) { return x()*v.x() + y()*v.y() + z()*v.z(); } Vec Vec::cross(Vec& v) { return Vec(y() * v.z() - z() * v.y(), z() * v.x() - x() * v.z(), x() * v.y() - y() * v.x()); } Vec Vec::normalize() { return *this * (1 / length()); } float Vec::length() { return sqrt(x()*x()+y()*y()+z()*z()); } void Vec::print() { printf (" vektor: %f, %f, %f\n", x(), y(), z()); } Vec* Vec::clone() { return new Vec(x(), y(), z()); } void Vec::vardump(std::string whitespace) { printf("%sVec(%f, %f, %f)\n", whitespace.c_str(), c[0], c[1], c[2]); printf("%s linked to %d faces\n", whitespace.c_str(), faces.size()); } void Vec::registerFace(SPoly* p) { faces.push_back(p); }