AP_Math: moved a lot of vector templates to cpp from .h

this reduces the code size quite a lot on AVR
This commit is contained in:
Andrew Tridgell 2013-04-12 10:04:18 +10:00
parent 78eb12a4ac
commit 43c3c60de2
4 changed files with 278 additions and 175 deletions

View File

@ -25,4 +25,116 @@ float Vector2<T>::length(void) const
return pythagorous2(x, y);
}
// dot product
template <typename T>
T Vector2<T>::operator *(const Vector2<T> &v) const
{
return x*v.x + y*v.y;
}
template <typename T>
Vector2<T> &Vector2<T>::operator *=(const T num)
{
x*=num; y*=num;
return *this;
}
template <typename T>
Vector2<T> &Vector2<T>::operator /=(const T num)
{
x /= num; y /= num;
return *this;
}
template <typename T>
Vector2<T> &Vector2<T>::operator -=(const Vector2<T> &v)
{
x -= v.x; y -= v.y;
return *this;
}
template <typename T>
bool Vector2<T>::is_nan(void) const
{
return isnan(x) || isnan(y);
}
template <typename T>
bool Vector2<T>::is_inf(void) const
{
return isinf(x) || isinf(y);
}
template <typename T>
Vector2<T> &Vector2<T>::operator +=(const Vector2<T> &v)
{
x+=v.x; y+=v.y;
return *this;
}
template <typename T>
Vector2<T> Vector2<T>::operator /(const T num) const
{
return Vector2<T>(x/num, y/num);
}
template <typename T>
Vector2<T> Vector2<T>::operator *(const T num) const
{
return Vector2<T>(x*num, y*num);
}
template <typename T>
Vector2<T> Vector2<T>::operator -(const Vector2<T> &v) const
{
return Vector2<T>(x-v.x, y-v.y);
}
template <typename T>
Vector2<T> Vector2<T>::operator +(const Vector2<T> &v) const
{
return Vector2<T>(x+v.x, y+v.y);
}
template <typename T>
Vector2<T> Vector2<T>::operator -(void) const
{
return Vector2<T>(-x,-y);
}
template <typename T>
bool Vector2<T>::operator ==(const Vector2<T> &v) const
{
return (x==v.x && y==v.y);
}
template <typename T>
bool Vector2<T>::operator !=(const Vector2<T> &v) const
{
return (x!=v.x && y!=v.y);
}
template <typename T>
float Vector2<T>::angle(const Vector2<T> &v2) const
{
return acosf(((*this)*v2) / (this->length()*v2.length()));
}
// only define for float
template float Vector2<float>::length(void) const;
template float Vector2<float>::operator *(const Vector2<float> &v) const;
template Vector2<float> &Vector2<float>::operator *=(const float num);
template Vector2<float> &Vector2<float>::operator /=(const float num);
template Vector2<float> &Vector2<float>::operator -=(const Vector2<float> &v);
template Vector2<float> &Vector2<float>::operator +=(const Vector2<float> &v);
template Vector2<float> Vector2<float>::operator /(const float num) const;
template Vector2<float> Vector2<float>::operator *(const float num) const;
template Vector2<float> Vector2<float>::operator +(const Vector2<float> &v) const;
template Vector2<float> Vector2<float>::operator -(const Vector2<float> &v) const;
template Vector2<float> Vector2<float>::operator -(void) const;
template bool Vector2<float>::operator ==(const Vector2<float> &v) const;
template bool Vector2<float>::operator !=(const Vector2<float> &v) const;
template bool Vector2<float>::is_nan(void) const;
template bool Vector2<float>::is_inf(void) const;
template float Vector2<float>::angle(const Vector2<float> &v) const;

View File

@ -46,82 +46,55 @@ struct Vector2
}
// test for equality
bool operator==(const Vector2<T> &v)
{
return (x==v.x && y==v.y);
}
bool operator ==(const Vector2<T> &v) const;
// test for inequality
bool operator!=(const Vector2<T> &v)
{
return (x!=v.x || y!=v.y);
}
bool operator !=(const Vector2<T> &v) const;
// negation
Vector2<T> operator -(void) const
{
return Vector2<T>(-x, -y);
}
Vector2<T> operator -(void) const;
// addition
Vector2<T> operator +(const Vector2<T> &v) const
{
return Vector2<T>(x+v.x, y+v.y);
}
Vector2<T> operator +(const Vector2<T> &v) const;
// subtraction
Vector2<T> operator -(const Vector2<T> &v) const
{
return Vector2<T>(x-v.x, y-v.y);
}
Vector2<T> operator -(const Vector2<T> &v) const;
// uniform scaling
Vector2<T> operator *(const T num) const
{
Vector2<T> temp(*this);
return temp*=num;
}
Vector2<T> operator *(const T num) const;
// uniform scaling
Vector2<T> operator /(const T num) const
{
Vector2<T> temp(*this);
return temp/=num;
}
Vector2<T> operator /(const T num) const;
// addition
Vector2<T> &operator +=(const Vector2<T> &v)
{
x+=v.x; y+=v.y;
return *this;
}
Vector2<T> &operator +=(const Vector2<T> &v);
// subtraction
Vector2<T> &operator -=(const Vector2<T> &v)
{
x-=v.x; y-=v.y;
return *this;
}
Vector2<T> &operator -=(const Vector2<T> &v);
// uniform scaling
Vector2<T> &operator *=(const T num)
{
x*=num; y*=num;
return *this;
}
Vector2<T> &operator *=(const T num);
// uniform scaling
Vector2<T> &operator /=(const T num)
{
x/=num; y/=num;
return *this;
}
Vector2<T> &operator /=(const T num);
// dot product
T operator *(const Vector2<T> &v) const
{
return x*v.x + y*v.y;
}
T operator *(const Vector2<T> &v) const;
// cross product
Vector2<T> operator %(const Vector2<T> &v) const;
// computes the angle between this vector and another vector
float angle(const Vector2<T> &v2) const;
// computes the angle in radians between the origin and this vector
T angle(void) const;
// check if any elements are NAN
bool is_nan(void) const;
// check if any elements are infinity
bool is_inf(void) const;
// gets the length of this vector squared
T length_squared() const
@ -163,31 +136,6 @@ struct Vector2
{
return v * (*this * v)/(v*v);
}
// computes the angle between 2 arbitrary vectors
T angle(const Vector2<T> &v1, const Vector2<T> &v2)
{
return (T)acosf((v1*v2) / (v1.length()*v2.length()));
}
// computes the angle in radians between the origin and this vector
T angle(void)
{
return (T)atan2f(y, x);
}
// computes the angle between this vector and another vector
T angle(const Vector2<T> &v2)
{
return (T)acosf(((*this)*v2) / (this->length()*v2.length()));
}
// computes the angle between 2 normalized arbitrary vectors
T angle_normalized(const Vector2<T> &v1, const Vector2<T> &v2)
{
return (T)acosf(v1*v2);
}
};
typedef Vector2<int16_t> Vector2i;

View File

@ -184,8 +184,110 @@ float Vector3<T>::length(void) const
return pythagorous3(x, y, z);
}
// only define for signed numbers
template <typename T>
Vector3<T> &Vector3<T>::operator *=(const T num)
{
x*=num; y*=num; z*=num;
return *this;
}
template <typename T>
Vector3<T> &Vector3<T>::operator /=(const T num)
{
x /= num; y /= num; z /= num;
return *this;
}
template <typename T>
Vector3<T> &Vector3<T>::operator -=(const Vector3<T> &v)
{
x -= v.x; y -= v.y; z -= v.z;
return *this;
}
template <typename T>
bool Vector3<T>::is_nan(void) const
{
return isnan(x) || isnan(y) || isnan(z);
}
template <typename T>
bool Vector3<T>::is_inf(void) const
{
return isinf(x) || isinf(y) || isinf(z);
}
template <typename T>
Vector3<T> &Vector3<T>::operator +=(const Vector3<T> &v)
{
x+=v.x; y+=v.y; z+=v.z;
return *this;
}
template <typename T>
Vector3<T> Vector3<T>::operator /(const T num) const
{
return Vector3<T>(x/num, y/num, z/num);
}
template <typename T>
Vector3<T> Vector3<T>::operator *(const T num) const
{
return Vector3<T>(x*num, y*num, z*num);
}
template <typename T>
Vector3<T> Vector3<T>::operator -(const Vector3<T> &v) const
{
return Vector3<T>(x-v.x, y-v.y, z-v.z);
}
template <typename T>
Vector3<T> Vector3<T>::operator +(const Vector3<T> &v) const
{
return Vector3<T>(x+v.x, y+v.y, z+v.z);
}
template <typename T>
Vector3<T> Vector3<T>::operator -(void) const
{
return Vector3<T>(-x,-y,-z);
}
template <typename T>
bool Vector3<T>::operator ==(const Vector3<T> &v) const
{
return (x==v.x && y==v.y && z==v.z);
}
template <typename T>
bool Vector3<T>::operator !=(const Vector3<T> &v) const
{
return (x!=v.x && y!=v.y && z!=v.z);
}
template <typename T>
float Vector3<T>::angle(const Vector3<T> &v2) const
{
return acosf(((*this)*v2) / (this->length()*v2.length()));
}
// only define for float
template void Vector3<float>::rotate(enum Rotation);
template float Vector3<float>::length(void) const;
template Vector3<float> Vector3<float>::operator %(const Vector3<float> &v) const;
template float Vector3<float>::operator *(const Vector3<float> &v) const;
template Vector3<float> &Vector3<float>::operator *=(const float num);
template Vector3<float> &Vector3<float>::operator /=(const float num);
template Vector3<float> &Vector3<float>::operator -=(const Vector3<float> &v);
template Vector3<float> &Vector3<float>::operator +=(const Vector3<float> &v);
template Vector3<float> Vector3<float>::operator /(const float num) const;
template Vector3<float> Vector3<float>::operator *(const float num) const;
template Vector3<float> Vector3<float>::operator +(const Vector3<float> &v) const;
template Vector3<float> Vector3<float>::operator -(const Vector3<float> &v) const;
template Vector3<float> Vector3<float>::operator -(void) const;
template bool Vector3<float>::operator ==(const Vector3<float> &v) const;
template bool Vector3<float>::operator !=(const Vector3<float> &v) const;
template bool Vector3<float>::is_nan(void) const;
template bool Vector3<float>::is_inf(void) const;
template float Vector3<float>::angle(const Vector3<float> &v) const;

View File

@ -66,76 +66,37 @@ public:
}
// test for equality
bool operator ==(const Vector3<T> &v)
{
return (x==v.x && y==v.y && z==v.z);
}
bool operator ==(const Vector3<T> &v) const;
// test for inequality
bool operator !=(const Vector3<T> &v)
{
return (x!=v.x || y!=v.y || z!=v.z);
}
bool operator !=(const Vector3<T> &v) const;
// negation
Vector3<T> operator -(void) const
{
return Vector3<T>(-x,-y,-z);
}
Vector3<T> operator -(void) const;
// addition
Vector3<T> operator +(const Vector3<T> &v) const
{
return Vector3<T>(x+v.x, y+v.y, z+v.z);
}
Vector3<T> operator +(const Vector3<T> &v) const;
// subtraction
Vector3<T> operator -(const Vector3<T> &v) const
{
return Vector3<T>(x-v.x, y-v.y, z-v.z);
}
Vector3<T> operator -(const Vector3<T> &v) const;
// uniform scaling
Vector3<T> operator *(const T num) const
{
Vector3<T> temp(*this);
return temp*=num;
}
Vector3<T> operator *(const T num) const;
// uniform scaling
Vector3<T> operator /(const T num) const
{
Vector3<T> temp(*this);
return temp/=num;
}
Vector3<T> operator /(const T num) const;
// addition
Vector3<T> &operator +=(const Vector3<T> &v)
{
x+=v.x; y+=v.y; z+=v.z;
return *this;
}
Vector3<T> &operator +=(const Vector3<T> &v);
// subtraction
Vector3<T> &operator -=(const Vector3<T> &v)
{
x-=v.x; y-=v.y; z-=v.z;
return *this;
}
Vector3<T> &operator -=(const Vector3<T> &v);
// uniform scaling
Vector3<T> &operator *=(const T num)
{
x*=num; y*=num; z*=num;
return *this;
}
Vector3<T> &operator *=(const T num);
// uniform scaling
Vector3<T> &operator /=(const T num)
{
x/=num; y/=num; z/=num;
return *this;
}
Vector3<T> &operator /=(const T num);
// dot product
T operator *(const Vector3<T> &v) const;
@ -143,6 +104,18 @@ public:
// cross product
Vector3<T> operator %(const Vector3<T> &v) const;
// computes the angle between this vector and another vector
float angle(const Vector3<T> &v2) const;
// check if any elements are NAN
bool is_nan(void) const;
// check if any elements are infinity
bool is_inf(void) const;
// rotate by a standard rotation
void rotate(enum Rotation rotation);
// gets the length of this vector squared
T length_squared() const
{
@ -185,43 +158,11 @@ public:
}
// returns this vector projected onto v
Vector3<T> projected(const Vector3<T> &v)
Vector3<T> projected(const Vector3<T> &v) const
{
return v * (*this * v)/(v*v);
}
// computes the angle between 2 arbitrary vectors
T angle(const Vector3<T> &v1, const Vector3<T> &v2)
{
return (T)acosf((v1*v2) / (v1.length()*v2.length()));
}
// computes the angle between this vector and another vector
T angle(const Vector3<T> &v2)
{
return (T)acosf(((*this)*v2) / (this->length()*v2.length()));
}
// computes the angle between 2 arbitrary normalized vectors
T angle_normalized(const Vector3<T> &v1, const Vector3<T> &v2)
{
return (T)acosf(v1*v2);
}
// check if any elements are NAN
bool is_nan(void)
{
return isnan(x) || isnan(y) || isnan(z);
}
// check if any elements are infinity
bool is_inf(void)
{
return isinf(x) || isinf(y) || isinf(z);
}
// rotate by a standard rotation
void rotate(enum Rotation rotation);
};