| 1 |
/* |
| 2 |
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#ifndef FACTOR_INCLUDED |
| 30 |
#define FACTOR_INCLUDED |
| 31 |
|
| 32 |
#include <math.h> |
| 33 |
#include <complex> |
| 34 |
namespace PoissonRecon |
| 35 |
{ |
| 36 |
static const double SQRT_3 = 1.7320508075688772935; |
| 37 |
|
| 38 |
inline int Factor( double a1 , double a0 , std::complex< double > roots[1] , double EPS ) |
| 39 |
{ |
| 40 |
if( fabs(a1)<=EPS ) return 0; |
| 41 |
roots[0] = std::complex< double >( -a0/a1 , 0 ); |
| 42 |
return 1; |
| 43 |
} |
| 44 |
inline int Factor( double a2 , double a1 , double a0 , std::complex< double > roots[2] , double EPS ) |
| 45 |
{ |
| 46 |
double d; |
| 47 |
if( fabs(a2)<=EPS ) return Factor( a1 , a0 , roots , EPS ); |
| 48 |
|
| 49 |
d = a1*a1 - 4*a0*a2; |
| 50 |
a1 /= (2*a2); |
| 51 |
if( d<0 ) |
| 52 |
{ |
| 53 |
d=sqrt(-d)/(2*a2); |
| 54 |
roots[0] = std::complex< double >( -a1 , -d ); |
| 55 |
roots[1] = std::complex< double >( -a1 , d ); |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
d = sqrt(d)/(2*a2); |
| 60 |
roots[0] = std::complex< double >( -a1-d , 0 ); |
| 61 |
roots[1] = std::complex< double >( -a1+d , 0 ); |
| 62 |
} |
| 63 |
return 2; |
| 64 |
} |
| 65 |
// Solution taken from: http://mathworld.wolfram.com/CubicFormula.html |
| 66 |
// and http://www.csit.fsu.edu/~burkardt/f_src/subpak/subpak.f90 |
| 67 |
inline int Factor( double a3 , double a2 , double a1 , double a0 , std::complex< double > roots[3] , double EPS ) |
| 68 |
{ |
| 69 |
double q,r,r2,q3; |
| 70 |
|
| 71 |
if( fabs(a3)<=EPS ) return Factor( a2 , a1 , a0 , roots , EPS ); |
| 72 |
a2 /= a3 , a1 /= a3 , a0 /= a3; |
| 73 |
|
| 74 |
q = -(3*a1-a2*a2)/9; |
| 75 |
r = -(9*a2*a1-27*a0-2*a2*a2*a2)/54; |
| 76 |
r2 = r*r; |
| 77 |
q3 = q*q*q; |
| 78 |
|
| 79 |
if(r2<q3) |
| 80 |
{ |
| 81 |
double sqrQ = sqrt(q); |
| 82 |
double theta = acos ( r / (sqrQ*q) ); |
| 83 |
double cTheta=cos(theta/3)*sqrQ; |
| 84 |
double sTheta=sin(theta/3)*sqrQ*SQRT_3/2; |
| 85 |
roots[0] = std::complex< double >( -2*cTheta , 0 ); |
| 86 |
roots[1] = std::complex< double >( -2*(-cTheta*0.5-sTheta) , 0 ); |
| 87 |
roots[2] = std::complex< double >( -2*(-cTheta*0.5+sTheta) , 0 ); |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
double t , s1 , s2 , sqr=sqrt(r2-q3); |
| 92 |
t = -r+sqr; |
| 93 |
if(t<0) s1 = -pow( -t , 1.0/3 ); |
| 94 |
else s1 = pow( t , 1.0/3 ); |
| 95 |
t = -r-sqr; |
| 96 |
if( t<0 ) s2 = -pow( -t , 1.0/3 ); |
| 97 |
else s2 = pow( t , 1.0/3 ); |
| 98 |
roots[0] = std::complex< double >( s1+s2 , 0 ); |
| 99 |
s1 /= 2 , s2 /= 2; |
| 100 |
roots[1] = std::complex< double >( -s1-s2 , SQRT_3*(s1-s2) ); |
| 101 |
roots[2] = std::complex< double >( -s1-s2 , -SQRT_3*(s1-s2) ); |
| 102 |
} |
| 103 |
roots[0] -= a2/3; |
| 104 |
roots[1] -= a2/3; |
| 105 |
roots[2] -= a2/3; |
| 106 |
return 3; |
| 107 |
} |
| 108 |
// Solution taken from: http://mathworld.wolfram.com/QuarticEquation.html |
| 109 |
// and http://www.csit.fsu.edu/~burkardt/f_src/subpak/subpak.f90 |
| 110 |
inline int Factor( double a4 , double a3 , double a2 , double a1 , double a0 , std::complex< double > roots[4] , double EPS ) |
| 111 |
{ |
| 112 |
std::complex< double > R , D , E , R2; |
| 113 |
|
| 114 |
if( fabs(a4)<EPS ) return Factor( a3 , a2 , a1 , a0 , roots , EPS ); |
| 115 |
a3 /= a4 , a2 /= a4 , a1 /= a4 , a0 /= a4; |
| 116 |
|
| 117 |
Factor( 1.0 , -a2 , a3*a1-4.0*a0 , -a3*a3*a0+4.0*a2*a0-a1*a1 , roots , EPS ); |
| 118 |
|
| 119 |
R2 = std::complex< double >( a3*a3/4.0-a2+roots[0].real() , 0 ); |
| 120 |
R = sqrt( R2 ); |
| 121 |
if( fabs( R.real() )>10e-8 ) |
| 122 |
{ |
| 123 |
std::complex< double > temp1 , temp2 , p1 , p2; |
| 124 |
|
| 125 |
p1 = std::complex< double >( a3*a3*0.75-2.0*a2-R2.real() , 0 ); |
| 126 |
|
| 127 |
temp2 = std::complex< double >( (4.0*a3*a2-8.0*a1-a3*a3*a3)/4.0 , 0 ); |
| 128 |
p2 = temp2 / R; |
| 129 |
temp1 = p1+p2; |
| 130 |
temp2 = p1-p2; |
| 131 |
D = sqrt( temp1 ); |
| 132 |
E = sqrt( temp2 ); |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
R = std::complex< double >( 0 , 0 ); |
| 137 |
std::complex< double > temp1 , temp2; |
| 138 |
temp1 = std::complex< double >( roots[0].real()*roots[0].real()-4.0*a0 , 0 ); |
| 139 |
temp2 = sqrt( temp1 ); |
| 140 |
|
| 141 |
temp1 = std::complex< double >( a3*a3*0.75-2.0*a2+2.0*temp2.real() , 2.0*temp2.imag() ); |
| 142 |
D = sqrt( temp1 ); |
| 143 |
|
| 144 |
temp1 = std::complex< double >( a3*a3*0.75-2.0*a2-2.0*temp2.real() , -2.0*temp2.imag() ); |
| 145 |
E = sqrt( temp1 ); |
| 146 |
} |
| 147 |
|
| 148 |
roots[0] = R/2. + D/2. - a3/4; |
| 149 |
roots[1] = R/2. - D/2. - a3/4; |
| 150 |
roots[2] = -R/2. + E/2. - a3/4; |
| 151 |
roots[3] = -R/2. - E/2. - a3/4; |
| 152 |
|
| 153 |
return 4; |
| 154 |
} |
| 155 |
} |
| 156 |
#endif // FACTOR_INCLUDED |