omegalib  13.1
omicron::Math Class Reference

Class to provide access to common mathematical functions. More...

#include <Math.h>

Public Member Functions

 Math (unsigned int trigTableSize=4096)
 Default constructor. More...
 
 ~Math ()
 Default destructor. More...
 

Static Public Member Functions

static int iabs (int iValue)
 
static int iceil (real fValue)
 
static int ifloor (real fValue)
 
static int isign (int iValue)
 
static real abs (real fValue)
 
static real acos (real fValue)
 
static real asin (real fValue)
 
static real atan (real fValue)
 
static real atan2 (real fY, real fX)
 
static real ceil (real fValue)
 
static bool isNaN (real f)
 
static real cos (real fValue, bool useTables=false)
 Cosine function. More...
 
static real exp (real fValue)
 
static real floor (real fValue)
 
static real log (real fValue)
 
static real log2 (real fValue)
 
static real logN (real base, real fValue)
 
static real pow (real fBase, real fExponent)
 
static real sign (real fValue)
 
static real sin (real fValue, bool useTables=false)
 Sine function. More...
 
static real sqr (real fValue)
 
static real sqrt (real fValue)
 
static real unitRandom ()
 
static real rangeRandom (real fLow, real fHigh)
 
static real symmetricRandom ()
 
static real tan (real fValue, bool useTables=false)
 Tangent function. More...
 
static real degreesToRadians (real degrees)
 
static real radiansToDegrees (real radians)
 
static bool pointInTri2D (const Vector2f &p, const Vector2f &a, const Vector2f &b, const Vector2f &c)
 Checks whether a given point is inside a triangle, in a 2-dimensional (Cartesian) space. More...
 
static bool pointInTri3D (const Vector3f &p, const Vector3f &a, const Vector3f &b, const Vector3f &c, const Vector3f &normal)
 Checks whether a given 3D point is inside a triangle. More...
 
static std::pair< bool, realintersects (const Ray &ray, const Plane &plane)
 Ray / plane intersection, returns boolean result and distance. More...
 
static std::pair< bool, realintersects (const Ray &ray, const Sphere &sphere, bool discardInside=true)
 Ray / sphere intersection, returns boolean result and distance. More...
 
static std::pair< bool, realintersects (const Ray &ray, const AlignedBox3 &box)
 Ray / box intersection, returns boolean result and distance. More...
 
static bool intersects (const Ray &ray, const AlignedBox3 &box, real *d1, real *d2)
 Ray / box intersection, returns boolean result and two intersection distance. More...
 
static std::pair< bool, realintersects (const Ray &ray, const Vector3f &a, const Vector3f &b, const Vector3f &c, const Vector3f &normal, bool positiveSide=true, bool negativeSide=true)
 Ray / triangle intersection, returns boolean result and distance. More...
 
static std::pair< bool, realintersects (const Ray &ray, const Vector3f &a, const Vector3f &b, const Vector3f &c, bool positiveSide=true, bool negativeSide=true)
 Ray / triangle intersection, returns boolean result and distance. More...
 
static bool intersects (const Sphere &sphere, const AlignedBox3 &box)
 Sphere / box intersection test. More...
 
static bool intersects (const Plane &plane, const AlignedBox3 &box)
 Plane / box intersection test. More...
 
static std::pair< bool, realintersects (const Ray &ray, const std::vector< Plane > &planeList, bool normalIsOutside)
 Ray / convex plane list intersection test. More...
 
static std::pair< bool, realintersects (const Ray &ray, const std::list< Plane > &planeList, bool normalIsOutside)
 Ray / convex plane list intersection test. More...
 
static bool intersects (const Sphere &sphere, const Plane &plane)
 Sphere / plane intersection test. More...
 
static bool floatEqual (real a, real b, real tolerance=std::numeric_limits< real >::epsilon())
 Compare 2 reals, using tolerance for inaccuracies. More...
 
static Vector3f calculateTangentSpaceVector (const Vector3f &position1, const Vector3f &position2, const Vector3f &position3, real u1, real v1, real u2, real v2, real u3, real v3)
 Calculates the tangent space vector for a given set of positions / texture coords. More...
 
static Matrix4f buildReflectionMatrix (const Plane &p)
 Build a reflection matrix for the passed in plane. More...
 
static Vector4f calculateFaceNormal (const Vector3f &v1, const Vector3f &v2, const Vector3f &v3)
 Calculate a face normal, including the w component which is the offset from the origin. More...
 
static Vector3f calculateBasicFaceNormal (const Vector3f &v1, const Vector3f &v2, const Vector3f &v3)
 Calculate a face normal, no w-information. More...
 
static Vector4f calculateFaceNormalWithoutNormalize (const Vector3f &v1, const Vector3f &v2, const Vector3f &v3)
 Calculate a face normal without normalize, including the w component which is the offset from the origin. More...
 
static Vector3f calculateBasicFaceNormalWithoutNormalize (const Vector3f &v1, const Vector3f &v2, const Vector3f &v3)
 Calculate a face normal without normalize, no w-information. More...
 
static real gaussianDistribution (real x, real offset=0.0f, real scale=1.0f)
 Generates a value based on the Gaussian (normal) distribution function with the given offset and scale parameters. More...
 
static real Clamp (real val, real minval, real maxval)
 Clamp a value within an inclusive range. More...
 
static AffineTransform3 makeViewMatrix (const Vector3f &position, const Quaternion &orientation)
 
static Matrix4f makePerspectiveMatrix (float fov, float aspect, float nearZ, float farZ)
 
static real boundingRadiusFromAABB (const AlignedBox3 &aabb)
 Get a bounding radius value from a bounding box. More...
 
static Quaternion buildRotation (const Vector3f &a, const Vector3f &b, const Vector3f &fallbackAxis)
 Compute a quaternion rotation transforming vector a to vector b. More...
 
static Vector3f quaternionToEuler (const Quaternion &quat)
 Converts a quaternion to euler angles (pitch, yaw, roll) More...
 
static Quaternion quaternionFromEuler (const Vector3f &pitchYawRoll)
 
static Ray unproject (const Vector2f &point, const AffineTransform3 &modelview, const Transform3 &projection, const Rect &viewport)
 
static Ray unprojectNormalized (const Vector2f &point, const AffineTransform3 &modelview, const Transform3 &projection)
 
static Vector3f project (const Vector3f &point, const AffineTransform3 &modelview, const Transform3 &projection, const Rect &viewport)
 
static Vector3f normal (const Vector3f &aa, const Vector3f &bb, const Vector3f &cc)
 
static AffineTransform3 computeMatchingPointsTransform (const Vectors3f &src, const Vectors3f &dst)
 
static void swapMinMax (real &min, real &max)
 

Static Public Attributes

static const real Log2Base
 Stored value of log(2) for frequent use. More...
 
static const real PositiveInfinity
 
static const real NegativeInfinity
 
static const float Pi
 
static const real TwoPi
 
static const real HalfPi
 
static const real DegToRad
 
static const real RadToDeg
 

Protected Member Functions

void buildTrigTables ()
 Private function to build trig tables. More...
 

Static Protected Member Functions

static real SinTable (real fValue)
 
static real TanTable (real fValue)
 

Static Protected Attributes

static int mTrigTableSize
 Size of the trig tables as determined by constructor. More...
 
static real mTrigTableFactor
 Radian -> index factor value ( mTrigTableSize / 2 * Pi ) More...
 
static realmSinTable
 
static realmTanTable
 

Detailed Description

Class to provide access to common mathematical functions.

Remarks
Most of the maths functions are aliased versions of the C runtime library functions. They are aliased here to provide future optimisation opportunities, either from faster RTLs or custom math approximations.
Note

This is based on MgcMath.h from Wild Magic.

Constructor & Destructor Documentation

omicron::Math::Math ( unsigned int  trigTableSize = 4096)

Default constructor.

Parameters
trigTableSizeOptional parameter to set the size of the tables used to implement Sin, Cos, Tan
omicron::Math::~Math ( )

Default destructor.

Member Function Documentation

static real omicron::Math::abs ( real  fValue)
inlinestatic
static real omicron::Math::acos ( real  fValue)
static
static real omicron::Math::asin ( real  fValue)
static
static real omicron::Math::atan ( real  fValue)
inlinestatic
static real omicron::Math::atan2 ( real  fY,
real  fX 
)
inlinestatic
static real omicron::Math::boundingRadiusFromAABB ( const AlignedBox3 aabb)
static

Get a bounding radius value from a bounding box.

static Matrix4f omicron::Math::buildReflectionMatrix ( const Plane p)
static

Build a reflection matrix for the passed in plane.

static Quaternion omicron::Math::buildRotation ( const Vector3f a,
const Vector3f b,
const Vector3f fallbackAxis 
)
static

Compute a quaternion rotation transforming vector a to vector b.

void omicron::Math::buildTrigTables ( )
protected

Private function to build trig tables.

static Vector3f omicron::Math::calculateBasicFaceNormal ( const Vector3f v1,
const Vector3f v2,
const Vector3f v3 
)
static

Calculate a face normal, no w-information.

static Vector3f omicron::Math::calculateBasicFaceNormalWithoutNormalize ( const Vector3f v1,
const Vector3f v2,
const Vector3f v3 
)
static

Calculate a face normal without normalize, no w-information.

static Vector4f omicron::Math::calculateFaceNormal ( const Vector3f v1,
const Vector3f v2,
const Vector3f v3 
)
static

Calculate a face normal, including the w component which is the offset from the origin.

static Vector4f omicron::Math::calculateFaceNormalWithoutNormalize ( const Vector3f v1,
const Vector3f v2,
const Vector3f v3 
)
static

Calculate a face normal without normalize, including the w component which is the offset from the origin.

static Vector3f omicron::Math::calculateTangentSpaceVector ( const Vector3f position1,
const Vector3f position2,
const Vector3f position3,
real  u1,
real  v1,
real  u2,
real  v2,
real  u3,
real  v3 
)
static

Calculates the tangent space vector for a given set of positions / texture coords.

static real omicron::Math::ceil ( real  fValue)
inlinestatic
static real omicron::Math::Clamp ( real  val,
real  minval,
real  maxval 
)
inlinestatic

Clamp a value within an inclusive range.

static AffineTransform3 omicron::Math::computeMatchingPointsTransform ( const Vectors3f src,
const Vectors3f dst 
)
static
static real omicron::Math::cos ( real  fValue,
bool  useTables = false 
)
inlinestatic

Cosine function.

Parameters
fValueAngle in radians
useTablesIf true, uses lookup tables rather than calculation - faster but less accurate.
static real omicron::Math::degreesToRadians ( real  degrees)
inlinestatic
static real omicron::Math::exp ( real  fValue)
inlinestatic
static bool omicron::Math::floatEqual ( real  a,
real  b,
real  tolerance = std::numeric_limits< real >::epsilon() 
)
static

Compare 2 reals, using tolerance for inaccuracies.

static real omicron::Math::floor ( real  fValue)
inlinestatic
static real omicron::Math::gaussianDistribution ( real  x,
real  offset = 0.0f,
real  scale = 1.0f 
)
static

Generates a value based on the Gaussian (normal) distribution function with the given offset and scale parameters.

static int omicron::Math::iabs ( int  iValue)
inlinestatic
static int omicron::Math::iceil ( real  fValue)
inlinestatic
static int omicron::Math::ifloor ( real  fValue)
inlinestatic
static std::pair<bool, real> omicron::Math::intersects ( const Ray ray,
const Plane plane 
)
static

Ray / plane intersection, returns boolean result and distance.

static std::pair<bool, real> omicron::Math::intersects ( const Ray ray,
const Sphere sphere,
bool  discardInside = true 
)
static

Ray / sphere intersection, returns boolean result and distance.

static std::pair<bool, real> omicron::Math::intersects ( const Ray ray,
const AlignedBox3 box 
)
static

Ray / box intersection, returns boolean result and distance.

static bool omicron::Math::intersects ( const Ray ray,
const AlignedBox3 box,
real d1,
real d2 
)
static

Ray / box intersection, returns boolean result and two intersection distance.

Parameters
rayThe ray.
boxThe box.
d1A real pointer to retrieve the near intersection distance from the ray origin, maybe null which means don't care about the near intersection distance.
d2A real pointer to retrieve the far intersection distance from the ray origin, maybe null which means don't care about the far intersection distance.
Returns
If the ray is intersects the box, true is returned, and the near intersection distance is return by d1, the far intersection distance is return by d2. Guarantee 0 <= d1 <= d2.
If the ray isn't intersects the box, false is returned, and d1 and d2 is unmodified.
static std::pair<bool, real> omicron::Math::intersects ( const Ray ray,
const Vector3f a,
const Vector3f b,
const Vector3f c,
const Vector3f normal,
bool  positiveSide = true,
bool  negativeSide = true 
)
static

Ray / triangle intersection, returns boolean result and distance.

Parameters
rayThe ray.
aThe triangle's first vertex.
bThe triangle's second vertex.
cThe triangle's third vertex.
normalThe triangle plane's normal (passed in rather than calculated on demand since the caller may already have it), doesn't need normalised since we don't care.
positiveSideIntersect with "positive side" of the triangle
negativeSideIntersect with "negative side" of the triangle
Returns
If the ray is intersects the triangle, a pair of true and the distance between intersection point and ray origin returned.
If the ray isn't intersects the triangle, a pair of false and 0 returned.
static std::pair<bool, real> omicron::Math::intersects ( const Ray ray,
const Vector3f a,
const Vector3f b,
const Vector3f c,
bool  positiveSide = true,
bool  negativeSide = true 
)
static

Ray / triangle intersection, returns boolean result and distance.

Parameters
rayThe ray.
aThe triangle's first vertex.
bThe triangle's second vertex.
cThe triangle's third vertex.
positiveSideIntersect with "positive side" of the triangle
negativeSideIntersect with "negative side" of the triangle
Returns
If the ray is intersects the triangle, a pair of true and the distance between intersection point and ray origin returned.
If the ray isn't intersects the triangle, a pair of false and 0 returned.
static bool omicron::Math::intersects ( const Sphere sphere,
const AlignedBox3 box 
)
static

Sphere / box intersection test.

static bool omicron::Math::intersects ( const Plane plane,
const AlignedBox3 box 
)
static

Plane / box intersection test.

static std::pair<bool, real> omicron::Math::intersects ( const Ray ray,
const std::vector< Plane > &  planeList,
bool  normalIsOutside 
)
static

Ray / convex plane list intersection test.

Parameters
rayThe ray to test with
plaeListList of planes which form a convex volume
normalIsOutsideDoes the normal point outside the volume
static std::pair<bool, real> omicron::Math::intersects ( const Ray ray,
const std::list< Plane > &  planeList,
bool  normalIsOutside 
)
static

Ray / convex plane list intersection test.

Parameters
rayThe ray to test with
plaeListList of planes which form a convex volume
normalIsOutsideDoes the normal point outside the volume
static bool omicron::Math::intersects ( const Sphere sphere,
const Plane plane 
)
static

Sphere / plane intersection test.

Remarks
NB just do a plane.getDistance(sphere.getCenter()) for more detail!
static int omicron::Math::isign ( int  iValue)
static
static bool omicron::Math::isNaN ( real  f)
inlinestatic
static real omicron::Math::log ( real  fValue)
inlinestatic
static real omicron::Math::log2 ( real  fValue)
inlinestatic
static real omicron::Math::logN ( real  base,
real  fValue 
)
inlinestatic
static Matrix4f omicron::Math::makePerspectiveMatrix ( float  fov,
float  aspect,
float  nearZ,
float  farZ 
)
static
static AffineTransform3 omicron::Math::makeViewMatrix ( const Vector3f position,
const Quaternion orientation 
)
static
static Vector3f omicron::Math::normal ( const Vector3f aa,
const Vector3f bb,
const Vector3f cc 
)
static
static bool omicron::Math::pointInTri2D ( const Vector2f p,
const Vector2f a,
const Vector2f b,
const Vector2f c 
)
static

Checks whether a given point is inside a triangle, in a 2-dimensional (Cartesian) space.

Remarks
The vertices of the triangle must be given in either trigonometrical (anticlockwise) or inverse trigonometrical (clockwise) order.
Parameters
pThe point.
aThe triangle's first vertex.
bThe triangle's second vertex.
cThe triangle's third vertex.
Returns
If the point resides in the triangle, true is returned.
If the point is outside the triangle, false is returned.
static bool omicron::Math::pointInTri3D ( const Vector3f p,
const Vector3f a,
const Vector3f b,
const Vector3f c,
const Vector3f normal 
)
static

Checks whether a given 3D point is inside a triangle.

Remarks
The vertices of the triangle must be given in either trigonometrical (anticlockwise) or inverse trigonometrical (clockwise) order, and the point must be guaranteed to be in the same plane as the triangle
Parameters
pThe point.
aThe triangle's first vertex.
bThe triangle's second vertex.
cThe triangle's third vertex.
normalThe triangle plane's normal (passed in rather than calculated on demand since the caller may already have it)
Returns
If the point resides in the triangle, true is returned.
If the point is outside the triangle, false is returned.
static real omicron::Math::pow ( real  fBase,
real  fExponent 
)
inlinestatic
static Vector3f omicron::Math::project ( const Vector3f point,
const AffineTransform3 modelview,
const Transform3 projection,
const Rect viewport 
)
static
static Quaternion omicron::Math::quaternionFromEuler ( const Vector3f pitchYawRoll)
static
static Vector3f omicron::Math::quaternionToEuler ( const Quaternion quat)
static

Converts a quaternion to euler angles (pitch, yaw, roll)

static real omicron::Math::radiansToDegrees ( real  radians)
inlinestatic
static real omicron::Math::rangeRandom ( real  fLow,
real  fHigh 
)
static
static real omicron::Math::sign ( real  fValue)
static
static real omicron::Math::sin ( real  fValue,
bool  useTables = false 
)
inlinestatic

Sine function.

Parameters
fValueAngle in radians
useTablesIf true, uses lookup tables rather than calculation - faster but less accurate.
static real omicron::Math::SinTable ( real  fValue)
staticprotected
static real omicron::Math::sqr ( real  fValue)
inlinestatic
static real omicron::Math::sqrt ( real  fValue)
inlinestatic
static void omicron::Math::swapMinMax ( real min,
real max 
)
static
static real omicron::Math::symmetricRandom ( )
static
static real omicron::Math::tan ( real  fValue,
bool  useTables = false 
)
inlinestatic

Tangent function.

Parameters
fValueAngle in radians
useTablesIf true, uses lookup tables rather than calculation - faster but less accurate.
static real omicron::Math::TanTable ( real  fValue)
staticprotected
static real omicron::Math::unitRandom ( )
static
static Ray omicron::Math::unproject ( const Vector2f point,
const AffineTransform3 modelview,
const Transform3 projection,
const Rect viewport 
)
static
static Ray omicron::Math::unprojectNormalized ( const Vector2f point,
const AffineTransform3 modelview,
const Transform3 projection 
)
static

Member Data Documentation

const real omicron::Math::DegToRad
static
const real omicron::Math::HalfPi
static
const real omicron::Math::Log2Base
static

Stored value of log(2) for frequent use.

real* omicron::Math::mSinTable
staticprotected
real* omicron::Math::mTanTable
staticprotected
real omicron::Math::mTrigTableFactor
staticprotected

Radian -> index factor value ( mTrigTableSize / 2 * Pi )

int omicron::Math::mTrigTableSize
staticprotected

Size of the trig tables as determined by constructor.

const real omicron::Math::NegativeInfinity
static
const float omicron::Math::Pi
static
const real omicron::Math::PositiveInfinity
static
const real omicron::Math::RadToDeg
static
const real omicron::Math::TwoPi
static

The documentation for this class was generated from the following file: