FasTC/Base/include/Vector3.h
2014-02-16 18:28:37 -05:00

133 lines
3.8 KiB
C++

/*******************************************************************************
* Copyright (c) 2012 Pavel Krajcevski
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
******************************************************************************/
#ifndef BASE_INCLUDE_VECTOR3_H_
#define BASE_INCLUDE_VECTOR3_H_
#include "Vector2.h"
# define _VEX_VEC3_SWIZZLE_DEF(X, Y, Z) \
Vector3<T> X##Y##Z() const { return Vector3<T>( X(), Y(), Z() ); }
namespace FasTC {
template <typename T>
class Vector3 : public VectorBase<T, 3> {
public:
Vector3() { }
Vector3(T x, T y, T z) {
X() = x;
Y() = y;
Z() = z;
}
explicit Vector3(const T *_vec) {
for(int i = 0; i < 3; i++) {
this->vec[i] = _vec[i];
}
}
// Overloaded functions
template<typename _T>
Vector3(const Vector3<_T> &v) : VectorBase<T, 3>(v) { }
template<typename _T>
Vector3<T> &operator=(const Vector3<_T> &v) {
VectorBase<T, 3>::operator=(v);
return *this;
}
template<typename _T>
Vector3<T> &operator=(const _T *v) {
VectorBase<T, 3>::operator=(v);
return *this;
}
// Accessors
T &X() { return (*this)[0]; }
const T &X() const { return (*this)[0]; }
T &Y() { return (*this)[1]; }
const T &Y() const { return (*this)[1]; }
T &Z() { return (*this)[2]; }
const T &Z() const { return (*this)[2]; }
// Vector operations
template<typename _T>
Vector3<T> Cross(const Vector3<_T> &v) {
return Vector3<T>(
Y() * v.Z() - v.Y() * Z(),
Z() * v.X() - v.Z() * X(),
X() * v.Y() - v.X() * Y()
);
}
// Swizzle
_VEX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