--- ray/src/common/tcos.c 2011/02/11 00:35:14 3.5 +++ ray/src/common/tcos.c 2013/07/12 05:16:02 3.10 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tcos.c,v 3.5 2011/02/11 00:35:14 greg Exp $"; +static const char RCSid[] = "$Id: tcos.c,v 3.10 2013/07/12 05:16:02 greg Exp $"; #endif /* * Table-based cosine approximation. @@ -18,11 +18,12 @@ static const char RCSid[] = "$Id: tcos.c,v 3.5 2011/02 #include "rtmath.h" +#ifndef __FAST_MATH__ + #ifndef NCOSENTRY -#define NCOSENTRY 256 +#define NCOSENTRY 1024 #endif - double tcos(double x) /* approximate cosine */ { @@ -49,4 +50,25 @@ tcos(double x) /* approximate cosine */ return(costab[(4*NCOSENTRY)-i]); } return(0.); /* should never be reached */ +} + +#endif + +/* Fast arctangent approximation due to Rajan et al. 2006 */ +double +atan2a(double y, double x) +{ + double ratio, aratio, val; + + if (x == 0) + return (y > 0) ? PI/2. : 3./2.*PI; + + aratio = (ratio = y/x) >= 0 ? ratio : -ratio; + + if (aratio > 1.01) + return PI/2. - atan2a(x, y); + + val = PI/4.*ratio - ratio*(aratio - 1.)*(0.2447 + 0.0663*aratio); + + return val + PI*(x < 0); }