library of assembled shared sources

http://lass.cocamware.com

distribution_transformations.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @defgroup DistributionTransformations
00046  *
00047  *  A set of functions to transform samples between distribution
00048  */
00049 
00050 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_TRANSFORMATIONS_H
00051 #define LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_TRANSFORMATIONS_H
00052 
00053 #include "num_common.h"
00054 #include "../prim/point_2d.h"
00055 #include "../prim/point_3d.h"
00056 
00057 namespace lass
00058 {
00059 namespace num
00060 {
00061 
00062 /** @ingroup DistributionTransformations
00063  */
00064 template <typename T>
00065 const prim::Point3D<T> uniformSphere(const prim::Point2D<T>& sample, T& pdf)
00066 {
00067     const T z = 1 - 2 * sample.x;
00068     const T rho = sqrt(std::max<T>(0, 1 - z * z));
00069     const T theta = 2 * num::NumTraits<T>::pi * sample.y;
00070     pdf = inv(4 * num::NumTraits<T>::pi);
00071     return prim::Point3D<T>(rho * cos(theta), rho * sin(theta), z);
00072 }
00073 
00074 /** @ingroup DistributionTransformations
00075  */
00076 template <typename T>
00077 const prim::Point3D<T> uniformCone(const prim::Point2D<T>& sample, T minCosTheta, T& pdf)
00078 {
00079     const T z = minCosTheta + sample.x * (1 - minCosTheta);
00080     const T rho = sqrt(std::max<T>(0, 1 - z * z));
00081     const T theta = 2 * NumTraits<T>::pi * sample.y;
00082     pdf = inv(2 * NumTraits<T>::pi * (1 - minCosTheta));
00083     return prim::Point3D<T>(rho * cos(theta), rho * sin(theta), z);
00084 }
00085 
00086 /** @ingroup DistributionTransformations
00087  */
00088 template <typename T>
00089 const prim::Point2D<T> uniformDisk(const prim::Point2D<T>& sample, T& pdf)
00090 {
00091     T rho, theta;
00092     const T x = 2 * sample.x - 1;
00093     const T y = 2 * sample.y - 1;
00094     const T pi_4 = num::NumTraits<T>::pi / 4;
00095 
00096     if (x > -y) 
00097     {
00098         if (x > y) 
00099         {
00100             rho = x;
00101             theta = pi_4 * (y / x);
00102         }
00103         else       
00104         {
00105             rho = y;
00106             theta = pi_4 * (2 - (x / y));
00107         }
00108     }
00109     else 
00110     {
00111         if (x < y) 
00112         {   
00113             rho = -x;
00114             theta = pi_4 * (4 + (y / x));
00115         }
00116         else       
00117         {
00118             rho = -y;
00119             if (y != 0)
00120             {
00121                 theta = pi_4 * (6 - (x / y));
00122             }
00123             else
00124             {
00125                 theta = 0;
00126             }
00127         }
00128     }
00129 
00130     pdf = inv(NumTraits<T>::pi);
00131     return prim::Point2D<T>(rho * cos(theta), rho * sin(theta));
00132 }
00133 
00134 /** @ingroup DistributionTransformations
00135  */
00136 template <typename T>
00137 const prim::Point3D<T> cosineHemisphere(const prim::Point2D<T>& sample, T& pdf)
00138 {
00139     const prim::Point2D<T> xy = uniformDisk(sample, pdf);
00140     const T z = num::sqrt(std::max(T(), 1 - xy.position().squaredNorm()));
00141     pdf *= z;
00142     return prim::Point3D<T>(xy.x, xy.y, z);
00143 }
00144 
00145 }
00146 
00147 }
00148 
00149 #endif
00150 
00151 // EOF

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo