2007-04-16
This commit is contained in:
1 parent
78c27f03c8
commit
e20673c2cd
120 files changed
+9578
-1036
No files matched your search
@@ -0,0 +1,218 @@
|
||||
#ifndef MMATH_INTERPOLATE_H
|
||||
#define MMATH_INTERPOLATE_H
|
||||
#include <cmath>
|
||||
|
||||
namespace Math {
|
||||
namespace Interpolator {
|
||||
|
||||
/** Interpolate between values - creating data out of nothing.
|
||||
*
|
||||
* The math mainly comes from:
|
||||
* http://local.wasp.uwa.edu.au/~pbourke/other/interpolation/
|
||||
*
|
||||
* Capsuled into classes which store the values to allow usage
|
||||
* as a kind of data - consumer/producer.
|
||||
*
|
||||
* This idea comes from here: */
|
||||
// http://exult.cvs.sourceforge.net/*checkout*/exult/exult/audio/Audio.cc
|
||||
|
||||
template <typename T>
|
||||
class Linear {
|
||||
public:
|
||||
Linear(const T & y1, const T & y2);
|
||||
T getAt(const T & mu) const;
|
||||
void shiftAndFeed(const T & y_i);
|
||||
private:
|
||||
T v1, v2;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Cubic {
|
||||
public:
|
||||
Cubic(const T & y0,
|
||||
const T & y1, const T & y2, const T & y3);
|
||||
Cubic(const T & y1,
|
||||
const T & y2, const T & y3);
|
||||
|
||||
void shiftAndFeed(const T & y_i);
|
||||
void shift();
|
||||
T getAt(const T & mu) const;
|
||||
|
||||
private:
|
||||
void setCoefficients(const T y0,
|
||||
const T y1, const T y2, const T y3);
|
||||
T a0, a1, a2, a3;
|
||||
T v2, v3;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Cosine {
|
||||
public:
|
||||
Cosine(const T & y1, const T & y2);
|
||||
|
||||
T getAt(const T & mu) const;
|
||||
void shiftAndFeed(const T & y_i);
|
||||
|
||||
private:
|
||||
T v1, v2;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Hermite {
|
||||
public:
|
||||
Hermite(const T & y1, const T & y2, const T & y3,
|
||||
const T & _tension, const T & _bias);
|
||||
Hermite(const T & y0, const T & y1, const T & y2,
|
||||
const T & y3, const T & _tension, const T & _bias);
|
||||
|
||||
T getAt(const T & mu) const;
|
||||
void shiftAndFeed(const T & y_i);
|
||||
void shift();
|
||||
void setTension(const T & _tension);
|
||||
void setBias(const T & _bias);
|
||||
|
||||
private:
|
||||
void setCoefficients(const T y0, const T y1, const T y2,
|
||||
const T y3);
|
||||
void updateCoefficients();
|
||||
T m0, m1;
|
||||
T v0, v1, v2, v3;
|
||||
T tension, bias;
|
||||
};
|
||||
|
||||
// implementation
|
||||
|
||||
template <typename T>
|
||||
Linear<T>::Linear(const T & y1, const T & y2) : v1(y1), v2(y2) {}
|
||||
|
||||
template <typename T>
|
||||
T Linear<T>::getAt(const T & mu) const {
|
||||
return v1 * (1 - mu) + v2 * mu;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Linear<T>::shiftAndFeed(const T & y_i) {
|
||||
v1 = v2;
|
||||
v2 = y_i;
|
||||
}
|
||||
|
||||
// Cubic
|
||||
|
||||
template <typename T>
|
||||
Cubic<T>::Cubic(const T & y0,
|
||||
const T & y1, const T & y2, const T & y3) {
|
||||
setCoefficients(y0, y1, y2, y3);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Cubic<T>::Cubic(const T & y1,
|
||||
const T & y2, const T & y3) {
|
||||
setCoefficients(2 * y1 - y2, y1, y2, y3);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Cubic<T>::shiftAndFeed(const T & y_i) {
|
||||
setCoefficients(a3, v2, v3, y_i);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Cubic<T>::shift() {
|
||||
setCoefficients(a3, v2, v3, 2 * v2 - a3);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Cubic<T>::setCoefficients(const T y0,
|
||||
const T y1, const T y2, const T y3) {
|
||||
v2 = y2; v3 = y3;
|
||||
|
||||
a0 = y3 - y2 - y0 + y1;
|
||||
a1 = y0 - y1 - a0;
|
||||
a2 = y2 - y0;
|
||||
a3 = y1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T Cubic<T>::getAt(const T & mu) const {
|
||||
const T mu_sq = mu * mu;
|
||||
return(a0 * mu * mu_sq + a1 * mu_sq + a2 * mu + a3);
|
||||
}
|
||||
|
||||
// Cosine
|
||||
|
||||
template <typename T>
|
||||
Cosine<T>::Cosine(const T & y1, const T & y2) : v1(y1), v2(y2) {}
|
||||
|
||||
template <typename T>
|
||||
T Cosine<T>::getAt(const T & mu) const {
|
||||
T mu2 = (1 - cos(mu * M_PI)) / 2;
|
||||
return v1 * (1 - mu2) + v2 * mu2;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Cosine<T>::shiftAndFeed(const T & y_i) {
|
||||
v1 = v2;
|
||||
v2 = y_i;
|
||||
}
|
||||
|
||||
// Hermite
|
||||
|
||||
template <typename T>
|
||||
Hermite<T>::Hermite(const T & y1, const T & y2, const T & y3,
|
||||
const T & _tension, const T & _bias) : tension(_tension), bias(_bias) {
|
||||
setCoefficients(2 * y1 - y2, y1, y2, y3);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T Hermite<T>::getAt(const T & mu) const {
|
||||
const T mu_sq = mu * mu;
|
||||
const T mu_cu = mu_sq * mu;
|
||||
|
||||
T a0 = 2 * mu_cu - 3 * mu_sq + 1;
|
||||
T a1 = mu_cu - 2 * mu_sq + mu;
|
||||
T a2 = mu_cu - mu_sq;
|
||||
T a3 =-2 * mu_cu + 3 * mu_sq;
|
||||
|
||||
return a0 * v1 + a1 * m0 + a2 * m1 + a3 * v2;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Hermite<T>::setCoefficients(const T y0, const T y1, const T y2,
|
||||
const T y3) {
|
||||
v0 = y0; v1 = y1; v2 = y2; v3 = y3;
|
||||
updateCoefficients();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Hermite<T>::updateCoefficients() {
|
||||
m0 = (v1 - v0) * (1 + bias) * (1 - tension) / 2;
|
||||
m0 += (v2 - v1) * (1 - bias) * (1 - tension) / 2;
|
||||
m1 = (v2 - v1) * (1 + bias) * (1 - tension) / 2;
|
||||
m1 += (v3 - v2) * (1 - bias) * (1 - tension) / 2;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Hermite<T>::setTension(const T & _tension) {
|
||||
tension = _tension;
|
||||
updateCoefficients();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Hermite<T>::setBias(const T & _bias) {
|
||||
bias = _bias;
|
||||
updateCoefficients();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Hermite<T>::shiftAndFeed(const T & y_i) {
|
||||
setCoefficients(v1, v2, v3, y_i);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Hermite<T>::shift() {
|
||||
setCoefficients(v1, v2, v3, 2 * v2 - v1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
+6
-6
@@ -8,7 +8,7 @@
|
||||
//
|
||||
// Check if a point is in this bounding box
|
||||
//
|
||||
bool OBox::IsPointInBox(const Vector3D &InP)
|
||||
bool OBox::IsPointInBox(const Vector3D &InP) const
|
||||
{
|
||||
// Rotate the point into the box's coordinates
|
||||
Vector3D P = Transform(InP, m_M.Inverse());
|
||||
@@ -23,7 +23,7 @@ bool OBox::IsPointInBox(const Vector3D &InP)
|
||||
//
|
||||
// Check if a sphere overlaps any part of this bounding box
|
||||
//
|
||||
bool OBox::IsSphereInBox( const Vector3D &InP, float fRadius)
|
||||
bool OBox::IsSphereInBox( const Vector3D &InP, float fRadius) const
|
||||
{
|
||||
float fDist;
|
||||
float fDistSq = 0;
|
||||
@@ -44,7 +44,7 @@ bool OBox::IsSphereInBox( const Vector3D &InP, float fRadius)
|
||||
//
|
||||
// Check if the bounding box is completely behind a plane( defined by a normal and a point )
|
||||
//
|
||||
bool OBox::BoxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP )
|
||||
bool OBox::BoxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP ) const
|
||||
{
|
||||
// Plane Normal in Box Space
|
||||
Vector3D Norm = rotateVector(InNorm, m_M.Inverse() );
|
||||
@@ -62,7 +62,7 @@ bool OBox::BoxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP )
|
||||
//
|
||||
// Does the Line (L1, L2) intersect the Box?
|
||||
//
|
||||
bool OBox::IsLineInBox( const Vector3D& L1, const Vector3D& L2 )
|
||||
bool OBox::IsLineInBox( const Vector3D& L1, const Vector3D& L2 ) const
|
||||
{
|
||||
// Put line in box space
|
||||
Matrix3D MInv = m_M.Inverse();
|
||||
@@ -90,7 +90,7 @@ bool OBox::IsLineInBox( const Vector3D& L1, const Vector3D& L2 )
|
||||
//
|
||||
// Returns a 3x3 rotation matrix as vectors
|
||||
//
|
||||
inline void OBox::GetInvRot( Vector3D *pvRot )
|
||||
inline void OBox::GetInvRot( Vector3D *pvRot ) const
|
||||
{
|
||||
pvRot[0] = Vector3D( m_M.m[0][0], m_M.m[0][1], m_M.m[0][2] );
|
||||
pvRot[1] = Vector3D( m_M.m[1][0], m_M.m[1][1], m_M.m[1][2] );
|
||||
@@ -101,7 +101,7 @@ inline void OBox::GetInvRot( Vector3D *pvRot )
|
||||
// Check if any part of a box is inside any part of another box
|
||||
// Uses the separating axis test.
|
||||
//
|
||||
bool OBox::IsBoxInBox( OBox &BBox )
|
||||
bool OBox::IsBoxInBox( OBox &BBox ) const
|
||||
{
|
||||
Vector3D SizeA = m_Extent;
|
||||
Vector3D SizeB = BBox.m_Extent;
|
||||
|
||||
+10
-8
@@ -12,6 +12,8 @@ class OBox
|
||||
{ Set( m, extent ); }
|
||||
OBox( const Matrix3D & m, const Vector3D & low, const Vector3D & high )
|
||||
{ Set( m, low, high ); }
|
||||
OBox( const OBox & other)
|
||||
{ Set( other.m_M, other.m_Extent ); }
|
||||
|
||||
void Set( const Matrix3D & m, const Vector3D & extent )
|
||||
{
|
||||
@@ -25,17 +27,17 @@ class OBox
|
||||
m_Extent = 0.5f * (high - low);
|
||||
}
|
||||
|
||||
Vector3D GetSize()
|
||||
Vector3D GetSize() const
|
||||
{ return 2.0f * m_Extent; }
|
||||
Vector3D GetCenterPoint()
|
||||
Vector3D GetCenterPoint() const
|
||||
{ return m_M.GetTranslate(); }
|
||||
void GetInvRot( Vector3D *pvRot );
|
||||
void GetInvRot( Vector3D *pvRot ) const;
|
||||
|
||||
bool IsPointInBox( const Vector3D & p );
|
||||
bool IsBoxInBox( OBox & box );
|
||||
bool IsSphereInBox( const Vector3D & p, float fRadius );
|
||||
bool IsLineInBox( const Vector3D & l1, const Vector3D & l2 );
|
||||
bool BoxOutsidePlane( const Vector3D & normal, const Vector3D & p );
|
||||
bool IsPointInBox( const Vector3D & p ) const ;
|
||||
bool IsBoxInBox( OBox & box ) const ;
|
||||
bool IsSphereInBox( const Vector3D & p, float fRadius ) const ;
|
||||
bool IsLineInBox( const Vector3D & l1, const Vector3D & l2 ) const ;
|
||||
bool BoxOutsidePlane( const Vector3D & normal, const Vector3D & p ) const ;
|
||||
|
||||
// Data
|
||||
Matrix3D m_M;
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#ifndef RECTANGLE_2D_H
|
||||
#define RECTANGLE_2D_H
|
||||
|
||||
#include <list>
|
||||
|
||||
namespace Math {
|
||||
|
||||
template <typename U> struct Rectangle {
|
||||
U x;
|
||||
U y;
|
||||
U w;
|
||||
U h;
|
||||
|
||||
Rectangle() : x(0), y(0), w(0), h(0) {}
|
||||
|
||||
Rectangle(U _x, U _y, U _w, U _h) :
|
||||
x(_x), y(_y), w(_w), h(_h) {}
|
||||
|
||||
Rectangle(const Rectangle<U> & o) :
|
||||
x(o.x), y(o.y), w(o.w), h(o.h) {}
|
||||
|
||||
inline bool operator == (const Rectangle<U> & o) const {
|
||||
return ((x == o.x) && (y == o.y) && (w == o.w) && (h == o.h));
|
||||
}
|
||||
|
||||
inline bool isInside(U _x, U _y) const {
|
||||
return ((_x >= x) && (_y >= y) &&
|
||||
(_x <= x + w) && (_y <= y + h) );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <typename U> bool rectangle_test_inside (
|
||||
const Rectangle<U> & larger,
|
||||
const Rectangle<U> & smaller ) {
|
||||
return (larger.isInside( smaller.x, smaller.y ) &&
|
||||
larger.isInside( smaller.x + smaller.w, smaller.y ) &&
|
||||
larger.isInside( smaller.x, smaller.y + smaller.h ) &&
|
||||
larger.isInside( smaller.x + smaller.w, smaller.y + smaller.h));
|
||||
}
|
||||
|
||||
template <typename U> bool rectangle_test_leftborder (
|
||||
const Rectangle<U> & larger,
|
||||
const Rectangle<U> & smaller ) {
|
||||
return (larger.x == smaller.x);
|
||||
}
|
||||
|
||||
template <typename U> bool rectangle_test_rightborder (
|
||||
const Rectangle<U> & larger,
|
||||
const Rectangle<U> & smaller ) {
|
||||
return (larger.x + larger.w == smaller.x + smaller.w);
|
||||
}
|
||||
|
||||
template <typename U> bool rectangle_test_bottomborder (
|
||||
const Rectangle<U> & larger,
|
||||
const Rectangle<U> & smaller ) {
|
||||
return (larger.y == smaller.y);
|
||||
}
|
||||
|
||||
template <typename U> bool rectangle_test_topborder (
|
||||
const Rectangle<U> & larger,
|
||||
const Rectangle<U> & smaller ) {
|
||||
return (larger.y + larger.h == smaller.y + smaller.h);
|
||||
}
|
||||
|
||||
|
||||
template <typename U> class RectangleGeometry {
|
||||
public:
|
||||
typedef std::list< Rectangle < U > > ListOfRectangleU;
|
||||
/* void Union (ListOfRectangleU & list, const Rectangle< U > & first,
|
||||
const Rectangle< U > & second);
|
||||
void Intersection(ListOfRectangleU & list, const Rectangle< U > & first,
|
||||
const Rectangle< U > & second);
|
||||
*/
|
||||
static void difference(ListOfRectangleU & list, const Rectangle< U > & first,
|
||||
const Rectangle< U > & second);
|
||||
};
|
||||
|
||||
template <typename U> void RectangleGeometry<U>::difference(ListOfRectangleU & list,
|
||||
const Rectangle< U > & first, const Rectangle< U > & second) {
|
||||
// assume first is larger, second is smaller
|
||||
if (! rectangle_test_leftborder<U>(first, second)) {
|
||||
/*
|
||||
### #
|
||||
#x# split off the left col #
|
||||
### #
|
||||
*/
|
||||
list.push_back( Rectangle<U>( first.x, first.y, second.x - first.x, first.h ) );
|
||||
}
|
||||
if (! rectangle_test_rightborder<U>(first, second)) {
|
||||
// split of the right column
|
||||
list.push_back( Rectangle<U>( second.x + second.w, first.y,
|
||||
first.x + first.w - (second.x + second.w), first.h));
|
||||
}
|
||||
if (! rectangle_test_topborder<U>(first, second)) {
|
||||
// remainder of the top row
|
||||
list.push_back( Rectangle<U>( second.x, second.y + second.h,
|
||||
second.w, first.y + first.h - (second.y + second.h)));
|
||||
}
|
||||
if (! rectangle_test_bottomborder<U>(first, second)) {
|
||||
// remainder of the bottom row
|
||||
list.push_back( Rectangle<U>(second.x, first.y,
|
||||
second.w, second.y - first.y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -149,6 +149,23 @@ public:
|
||||
{
|
||||
return fabs (x - v.x) < e && fabs (y - v.y) < e && fabs (z - v.z) < e;
|
||||
}
|
||||
|
||||
// component-wise maximum
|
||||
const Vector max(const Vector & other) const {
|
||||
Vector _res;
|
||||
_res.x = (x > other.x) ? x : other.x;
|
||||
_res.y = (y > other.y) ? y : other.y;
|
||||
_res.z = (z > other.x) ? z : other.z;
|
||||
return _res;
|
||||
}
|
||||
|
||||
const Vector abs() const {
|
||||
Vector _res;
|
||||
_res.x = (x >= 0) ? x : -x;
|
||||
_res.y = (y >= 0) ? y : -y;
|
||||
_res.z = (z >= 0) ? z : -z;
|
||||
return _res;
|
||||
}
|
||||
};
|
||||
//
|
||||
// A 3D position
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "weighted_set.h"
|
||||
|
||||
namespace Math {
|
||||
WeightedSet::WeightedSet(unsigned int seed) :
|
||||
elements(),
|
||||
rng(seed) {
|
||||
}
|
||||
|
||||
void WeightedSet::add(unsigned int w, unsigned int e) {
|
||||
while (w > 0) {
|
||||
elements.push_back(e);
|
||||
--w;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int WeightedSet::getRandom() {
|
||||
//unsigned int rnd = (int) (totalWeight * (rand() / (RAND_MAX + 1.0)));
|
||||
unsigned int rnd = rng.nextUint(elements.size());
|
||||
return elements[rnd];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#include <iostream>
|
||||
int main(int argc, char* argv[]) {
|
||||
Util::Math::WeightedSet set(int(getpid()) ^ int(time(0)));
|
||||
set.add(1, 1);
|
||||
set.add(2, 2);
|
||||
set.add(2, 3);
|
||||
set.add(3, 4);
|
||||
set.add(3, 5);
|
||||
|
||||
std::cout << set.getRandom() << std::endl;
|
||||
}*/
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef WEIGHTED_SET_H
|
||||
#define WEIGHTED_SET_H
|
||||
|
||||
#include <vector>
|
||||
#include "yasli/random.h"
|
||||
|
||||
namespace Math {
|
||||
|
||||
class WeightedSet {
|
||||
public:
|
||||
WeightedSet(unsigned int seed);
|
||||
void add(unsigned int w, unsigned int e);
|
||||
unsigned int getRandom();
|
||||
private:
|
||||
std::vector<unsigned int> elements;
|
||||
Random rng;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user