2008-05-28 10:55:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2006 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __VCGLIB_SPHERICAL_HARMONICS_H
|
|
|
|
#define __VCGLIB_SPHERICAL_HARMONICS_H
|
|
|
|
|
2008-08-04 17:38:10 +02:00
|
|
|
#include <climits>
|
|
|
|
|
2008-10-29 16:26:45 +01:00
|
|
|
#include <vcg/math/base.h>
|
|
|
|
#include <vcg/math/random_generator.h>
|
|
|
|
#include <vcg/math/legendre.h>
|
|
|
|
#include <vcg/math/factorial.h>
|
2008-05-28 10:55:04 +02:00
|
|
|
|
|
|
|
namespace vcg{
|
|
|
|
namespace math{
|
|
|
|
|
2008-10-29 16:26:45 +01:00
|
|
|
template <typename ScalarType>
|
2008-12-15 14:16:00 +01:00
|
|
|
class DummyPolarFunctor{
|
2008-10-29 16:26:45 +01:00
|
|
|
public:
|
2008-12-15 14:16:00 +01:00
|
|
|
inline ScalarType operator()(ScalarType theta, ScalarType phi) {return ScalarType(0);}
|
2008-10-29 16:26:45 +01:00
|
|
|
};
|
|
|
|
|
2009-10-14 18:10:42 +02:00
|
|
|
|
|
|
|
template <typename ScalarType, int MAX_BAND = 4>
|
|
|
|
class ScalingFactor
|
|
|
|
{
|
|
|
|
private :
|
|
|
|
|
|
|
|
ScalarType k_factor[MAX_BAND][MAX_BAND];
|
|
|
|
|
|
|
|
static ScalingFactor sf;
|
|
|
|
|
|
|
|
ScalingFactor()
|
|
|
|
{
|
|
|
|
for (unsigned l = 0; l < MAX_BAND; ++l)
|
|
|
|
for (unsigned m = 0; m <= l; ++m)
|
|
|
|
k_factor[l][m] = Sqrt( ( (2.0*l + 1.0) * Factorial<ScalarType>(l-m) ) / (4.0 * M_PI * Factorial<ScalarType>(l + m)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public :
|
|
|
|
static ScalarType K(unsigned l, unsigned m)
|
|
|
|
{
|
|
|
|
return sf.k_factor[l][m];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ScalarType, int MAX_BAND>
|
|
|
|
ScalingFactor<ScalarType, MAX_BAND> ScalingFactor<ScalarType, MAX_BAND>::sf;
|
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
/**
|
2008-10-08 16:01:34 +02:00
|
|
|
* Although the Real Spherical Harmonic Function is correctly defined over any
|
2008-05-28 10:55:04 +02:00
|
|
|
* positive l and any -l <= m <= l, the two internal functions computing the
|
|
|
|
* imaginary and real parts of the Complex Spherical Harmonic Functions are defined
|
|
|
|
* for positive m only.
|
|
|
|
*/
|
2008-10-29 16:26:45 +01:00
|
|
|
template <typename ScalarType, int MAX_BAND = 4>
|
2008-05-28 10:55:04 +02:00
|
|
|
class SphericalHarmonics{
|
|
|
|
|
|
|
|
private :
|
2009-10-14 18:10:42 +02:00
|
|
|
|
|
|
|
static DynamicLegendre<ScalarType, MAX_BAND> legendre;
|
|
|
|
|
|
|
|
static ScalarType scaling_factor(unsigned l, unsigned m)
|
2008-05-28 10:55:04 +02:00
|
|
|
{
|
2009-10-14 18:10:42 +02:00
|
|
|
return ScalingFactor<ScalarType, MAX_BAND>::K(l,m);
|
2008-05-28 10:55:04 +02:00
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
inline static ScalarType complex_spherical_harmonic_re(unsigned l, unsigned m, ScalarType theta, ScalarType phi)
|
|
|
|
{
|
2009-10-14 18:10:42 +02:00
|
|
|
return scaling_factor(l, m) * legendre.AssociatedPolynomial(l, m, Cos(theta), Sin(theta)) * Cos(m * phi);
|
2008-05-28 10:55:04 +02:00
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
inline static ScalarType complex_spherical_harmonic_im(unsigned l, unsigned m, ScalarType theta, ScalarType phi)
|
|
|
|
{
|
2009-10-14 18:10:42 +02:00
|
|
|
return scaling_factor(l, m) * legendre.AssociatedPolynomial(l, m, Cos(theta), Sin(theta)) * Sin(m * phi);
|
2008-05-28 10:55:04 +02:00
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
|
|
|
ScalarType coefficients[MAX_BAND * MAX_BAND];
|
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
public :
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
/**
|
|
|
|
* Returns the Real Spherical Harmonic Function
|
2008-10-08 16:01:34 +02:00
|
|
|
*
|
2008-05-28 10:55:04 +02:00
|
|
|
* l is any positive integer,
|
|
|
|
* m is such that -l <= m <= l
|
|
|
|
* theta is inside [0, PI]
|
|
|
|
* phi is inside [0, 2*PI]
|
|
|
|
*/
|
|
|
|
static ScalarType Real(unsigned l, int m, ScalarType theta, ScalarType phi)
|
|
|
|
{
|
2010-02-11 21:06:14 +01:00
|
|
|
assert((int)-l <= m && m <= (int)l && theta >= 0.0 && theta <= (ScalarType)M_PI && phi >= 0.0 && phi <= (ScalarType)(2.0 * M_PI));
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
if (m > 0) return SQRT_TWO * complex_spherical_harmonic_re(l, m, theta, phi);
|
|
|
|
|
2009-10-14 18:10:42 +02:00
|
|
|
else if (m == 0) return scaling_factor(l, 0) * legendre.Polynomial(l, Cos(theta));
|
2008-05-28 10:55:04 +02:00
|
|
|
|
|
|
|
else return SQRT_TWO * complex_spherical_harmonic_im(l, -m, theta, phi);
|
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-12-15 14:16:00 +01:00
|
|
|
template <typename PolarFunctor>
|
|
|
|
static SphericalHarmonics Project(PolarFunctor * fun, unsigned n_samples)
|
2008-05-28 10:55:04 +02:00
|
|
|
{
|
|
|
|
const ScalarType weight = 4 * M_PI;
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
unsigned sqrt_n_samples = (unsigned int) Sqrt((int)n_samples);
|
|
|
|
unsigned actual_n_samples = sqrt_n_samples * sqrt_n_samples;
|
2008-10-08 16:01:34 +02:00
|
|
|
unsigned n_coeff = MAX_BAND * MAX_BAND;
|
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
ScalarType one_over_n = 1.0/(ScalarType)sqrt_n_samples;
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-12-15 09:49:02 +01:00
|
|
|
MarsenneTwisterRNG rand;
|
2008-05-28 10:55:04 +02:00
|
|
|
SphericalHarmonics sph;
|
|
|
|
|
|
|
|
int i = 0;
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
for (unsigned k = 0; k < n_coeff; k++ ) sph.coefficients[k] = 0;
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
for (unsigned a = 0; a < sqrt_n_samples; ++a )
|
|
|
|
{
|
|
|
|
for (unsigned b = 0; b < sqrt_n_samples; ++b)
|
|
|
|
{
|
2008-12-15 09:49:02 +01:00
|
|
|
ScalarType x = (a + ScalarType(rand.generate01())) * one_over_n;
|
|
|
|
ScalarType y = (b + ScalarType(rand.generate01())) * one_over_n;
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
ScalarType theta = 2.0 * Acos(Sqrt(1.0 - x));
|
|
|
|
ScalarType phi = 2.0 * M_PI * y;
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-11-20 14:27:50 +01:00
|
|
|
for (int l = 0; l < (int)MAX_BAND; ++l)
|
2008-05-28 10:55:04 +02:00
|
|
|
{
|
|
|
|
for (int m = -l; m <= l; ++m)
|
|
|
|
{
|
|
|
|
int index = l * (l+1) + m;
|
2008-10-29 16:26:45 +01:00
|
|
|
sph.coefficients[index] += (*fun)(theta, phi) * Real(l, m, theta, phi);
|
2008-05-28 10:55:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
ScalarType factor = weight / actual_n_samples;
|
|
|
|
for(i = 0; i < (int)n_coeff; ++i)
|
|
|
|
{
|
|
|
|
sph.coefficients[i] *= factor;
|
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
return sph;
|
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2010-01-08 11:36:09 +01:00
|
|
|
static SphericalHarmonics Wrap(ScalarType * _coefficients)
|
|
|
|
{
|
|
|
|
SphericalHarmonics sph;
|
2010-02-11 21:06:14 +01:00
|
|
|
for(int i = 0; i < (int) MAX_BAND * MAX_BAND; ++i) sph.coefficients[i] = _coefficients[i];
|
2010-01-08 11:36:09 +01:00
|
|
|
return sph;
|
|
|
|
}
|
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
ScalarType operator()(ScalarType theta, ScalarType phi)
|
|
|
|
{
|
|
|
|
ScalarType f = 0;
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-11-20 14:27:50 +01:00
|
|
|
for (int l = 0; l < MAX_BAND; ++l)
|
2008-05-28 10:55:04 +02:00
|
|
|
{
|
|
|
|
for (int m = -l; m <= l; ++m)
|
|
|
|
{
|
|
|
|
int index = l * (l+1) + m;
|
|
|
|
f += (coefficients[index] * Real(l, m, theta, phi));
|
|
|
|
}
|
|
|
|
}
|
2008-10-08 16:01:34 +02:00
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-14 18:10:42 +02:00
|
|
|
template <typename ScalarType, int MAX_BAND>
|
|
|
|
DynamicLegendre<ScalarType, MAX_BAND> SphericalHarmonics<ScalarType, MAX_BAND>::legendre;
|
|
|
|
|
2008-05-28 10:55:04 +02:00
|
|
|
}} //namespace vcg::math
|
|
|
|
|
|
|
|
#endif
|