project: COLLADA 2 BIN

Details

Program that converts geometric and animation 3D scene data, from a COLLADA "*.dae" file to a binary file, with a custom format.

  • current version: 1.0
  • last updated: February, 2019
Download source

src > math > functions.cpp

View raw
#include <stdlib.h>
#include "./functions.h"

namespace math
{

  //absolute value
  int fABS(const int n)
  {
    int const mask = n >> (sizeof(int) * cBYTE_SIZE - 1);
    return ((n ^ mask) - mask);
  }

  float fFABS(const float x)
  {
    return (x < 0)? -x: x;
  }


  int vRSEED = 123456789;
  void fRAND_SEED(const int seed){
    vRSEED=seed;
  }

  int fRAND()
  {
    return vRSEED = (cRAND_A * vRSEED + cRAND_C) & cRAND_MAX;
  }

  float fRANDF()
  {
    return ((float)fRAND()/(float)(cRAND_MAX));
  }

  //newton-raphson sqrtf
  float fSQRTF(const float x)
  {
    float guess = 1;
    while(fFABS(guess*guess - x) >=  cEPS_SQRT)
      guess = ((x/guess) + guess) / 2;

    return guess;
  }

  //fast sin/cos/tan functions
  float* cos_look= NULL;
  float* sin_look= NULL;
  float* tan_look= NULL;

  //params for order
  void fBUILD_ANGLE_TABLES(){
    //init lookup tables
    sin_look= new float[361]; // 1 extra so we can store 0-360 inclusive
    cos_look= new float[361];
    tan_look= new float[361];
    for(int i= 1; i<=89; i++){
      float x= cDEG2RAD* i;
      //taylor sin
      sin_look[i]= x - (x*x*x)/6 + (x*x*x*x*x)/120 - (x*x*x*x*x*x*x)/5040;// + (x*x*x*x*x*x*x*x*x)/362880;
      sin_look[180-i]= sin_look[i];
      sin_look[180+i]= -sin_look[i];
      sin_look[360-i]= -sin_look[i];

      //taylor cos
      cos_look[i]= 1 - (x*x)/2 + (x*x*x*x)/24 - (x*x*x*x*x*x)/720;// + (x*x*x*x*x*x*x)/40320;
      cos_look[180-i]=-cos_look[i];
      cos_look[180+i]=-cos_look[i];
      cos_look[360-i]= cos_look[i];

    }
    sin_look[0]= sin_look[360]= sin_look[180]= cos_look[90]= cos_look[270]= 0;
    sin_look[90]= cos_look[0]= cos_look[360]= 1;
    sin_look[270]= cos_look[180]= -1;
      
    for(int i=0; i<360; i++){
      tan_look[i]= (cos_look[i]> cEPS_E6)? sin_look[i]/cos_look[i]: cQNAN;
    }
      
    tan_look[360]= tan_look[0];
  }

  float fFAST_ANGLE(const float theta, float* table){
    float theta_deg= cRAD2DEG*theta;
    int theta_int = (int)theta_deg;    
    float theta_frac = theta_deg - theta_int;
    theta_int%= 360;
    if (theta_int < 0) theta_int+= 360;
    //mLOG("%d", theta_int);
    //interpolate value from table
    return (table[theta_int] +
            theta_frac*(table[theta_int+1] - table[theta_int])
            );
  }

  float fFAST_SIN(const float theta)
  {
    return fFAST_ANGLE(theta, sin_look);
  }

  float fFAST_COS(const float theta)
  {
    return fFAST_ANGLE(theta, cos_look);
  }

  float fFAST_TAN(const float theta)
  {
    return fFAST_ANGLE(theta, tan_look);
  }

  void fFAST_SINCOS(const float theta, float* sin, float* cos){
    *sin= fFAST_SIN(theta);
    *cos= fFAST_COS(theta);
  }

}