/*------------------------------------------------------------------------------
 src/lib/dj/djMath.h
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

#ifndef DJ_MATH_H
#define DJ_MATH_H
#include <djBase.h>

namespace dj {

// Constants.
//
static const double pi = 3.1415927;
static const double pi2 = 6.2831853;
static const double piHalf = 1.5707963;
static const double piQuarter = 0.7853981;

// XXX
//
static const float  almostZeroF = FLT_MIN * 2.0;
static const double almostZero = DBL_MIN * 2.0;
static const float  almostHalfF = 0.5 - almostZeroF;
static const double almostHalf = 0.5 - almostZero;
static const float  almostOneF = 1.0 - almostZeroF;
static const double almostOne = 1.0 - almostZero;

// Functions.
//
// References:
//
// * Andrew Glassner, "Graphics Gems".
// * David S. Ebert, F. Kenton Musgrave, Darwyn Peachey, Ken Perlin,
//   Steven Worley, "Texturing & Modeling, A Procedural Approach".
//
template <class T> const T abs(const T &);

int    pow(int in, int value);
float  pow(float in, float value);
double pow(double in, double value);
float  sqrt(float);
double sqrt(double);
float  exp(float);
double exp(double);

float  log(float);
double log(double);
float  log10(float);
double log10(double);
float  antiLog10(float);
double antiLog10(double);

template <class T> const T mod(const T &, const T & mod);
int wrap(int in, int min, int max);
int step(int in, int value);
int pulse(int in, int min, int max);

float  bias(float in, float value);
double bias(double in, double value);
float  gain(float in, float value);
double gain(double in, double value);

template <class T> const T min(const T &, const T &);
template <class T> const T max(const T &, const T &);
template <class T> const T clamp(const T & in, const T & min, const T & max);
  
// Soft clip.
//
// References:
//
// * Ken McGaugh, "Re: [Openexr-devel] Cineon -> half conversion?", OpenEXR
//   Mailing List 3/2/05.
//
float softClip(float in, float value);
double softClip(double in, double value);

// Interpolation.
//
template <class T, class U>
const T lerp(const U & in, const T & min, const T & max);
double smoothStep(double in, double min, double max);
  
// Trigonometry.
//
double sin(double);
double cos(double);
double arcSin(double);
double arcCos(double);
double sec(double);
double coSec(double);
double tan(double);
double coTan(double);
double arcTan(double);
double arcTan(double, double);

// Area.
//
template <class T>
bool intersect(const T & in, const T & position, const T & size);
//bool intersect(const int & in, const int & position, const int & size);
template <class T>
bool intersect(
  const T & positionA, const T & sizeA,
  const T & positionB, const T & sizeB,
  T * position, T * size
);
/*bool intersect(
  const int & positionA, const int & sizeA,
  const int & positionB, const int & sizeB,
  int * position, int * size
);*/
template <class T>
void bound(
  const T & positionA, const T & sizeA,
  const T & positionB, const T & sizeB,
  T * position, T * size
);
/*void bound(
  const int & positionA, const int & sizeA,
  const int & positionB, const int & sizeB,
  int * position, int * size
);*/

// Random numbers.
//
int    rand(int);
int    rand(int min, int max);
double rand();
double rand(double);
double rand(double min, double max);

// Seed random value generator. The current time is used when a value of zero
// or less is used.
//
void randSeed(int = 0);
  
// Comparison.
//
bool isZero(double);
bool compare(double, double);

// Conversion.
//
int    ceil(double);
int    floor(double);
double fraction(double);

double degreesToRadians(double);
double radiansToDegrees(double);

void sphericalToCartesian(
  double latitude, double longitude, double distance,
  double * x, double * y, double * z
);
void cartesianToSpherical(
  double x, double y, double z,
  double * latitude, double * longitude, double * distance
);

}

#include <djMath_.h>
#endif
