4 Vec3::Vec3() : x(0.0), y(0.0), z(0.0)
8 Vec3::Vec3(const float& x, const float& y, const float& z)
15 const float* Vec3::GetVec3() const
17 float* ret = new float[3];
24 float* Vec3::GetVec3()
26 float* ret = new float[3];
33 Vec3& Vec3::operator=(const Vec3& vec)
41 float& Vec3::operator[](unsigned int i)
43 return (i == 0 ? x : i == 1 ? y : z);
46 const float& Vec3::operator[](unsigned int i) const
48 return (i == 0 ? this->x : i == 1 ? this->y : this->z);
51 Vec3 Vec3::operator+() const
56 Vec3 Vec3::operator-() const
58 return Vec3(-this->x, -this->y, -this->z);
61 Vec3& Vec3::operator+=(const Vec3& b)
69 Vec3& Vec3::operator-=(const Vec3& b)
77 Vec3& Vec3::operator*=(const float& k)
85 Vec3& Vec3::operator/=(const float& k)
93 Vec3 Vec3::operator+(const Vec3& b)
95 return Vec3(this->x + b.x, this->y + b.y, this->z + b.z);
98 Vec3 Vec3::operator-(const Vec3& b)
100 return Vec3(this->x - b.x, this->y - b.y, this->z - b.z);
103 Vec3 Vec3::operator*(const float& k)
105 return Vec3(this->x * k, this->y * k, this->z * k);
108 Vec3 Vec3::operator/(const float& k)
110 return Vec3(this->x / k, this->y / k, this->z / k);
113 bool Vec3::operator==(const Vec3& b) const
115 //double epsilon = std::numeric_limits<double>::epsilon();
116 //return (fabs(this->x - b.x) <= epsilon) && (fabs(this->y - b.y) <= epsilon)
117 //&& (fabs(this->z - b.z) <= epsilon);
118 return this->x == b.x && this->y == b.y && this->z == b.z;
121 bool Vec3::operator!=(const Vec3& b) const
123 return (!(*this == b));
126 bool Vec3::operator<(const Vec3& b) const
130 else if(this->x > b.x)
136 else if(this->y > b.y)
147 bool Vec3::operator>(const Vec3& b)
151 else if(this->x < b.x)
157 else if(this->y < b.y)
168 float Vec3::Norm() const
170 return sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
173 void Vec3::Normalize()
175 float norm = sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
176 this->x = this->x / norm;
177 this->y = this->y / norm;
178 this->z = this->z / norm;
181 void Vec3::set(const float& x, const float& y, const float& z)
188 float Vec3::Dot(const Vec3& b) const
190 return (this->x * b.x + this->y * b.y + this->z * b.z);
193 Vec3 Vec3::Orthogonal(const Vec3& b) const
195 Vec3 u = Vec3(fabs(b.x), fabs(b.y), fabs(b.z));
198 if ((u.x > u.y) && (u.z > u.y))
213 Vec3 Vec3::Cross(const Vec3& b) const
215 return Vec3(this->y * b.z - this->z * b.y, this->z * b.x - this->x * b.z,
216 this->x * b.y - this->y * b.x);
220 float Vec3::EculideanDistance(const Vec3& b) const
223 pow(this->x - b.x, 2.0) + pow(this->y - b.y, 2.0)
224 + pow(this->z - b.z, 2.0));
228 operator<<(std::ostream& os, const Vec3& coord)
230 os << coord[0] << " " << coord[1] << " " << coord[2];