1/////////////////////////////////////////////////////////////////////////////////// 2/// OpenGL Mathematics (glm.g-truc.net) 3/// 4/// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net) 5/// Permission is hereby granted, free of charge, to any person obtaining a copy 6/// of this software and associated documentation files (the "Software"), to deal 7/// in the Software without restriction, including without limitation the rights 8/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9/// copies of the Software, and to permit persons to whom the Software is 10/// furnished to do so, subject to the following conditions: 11/// 12/// The above copyright notice and this permission notice shall be included in 13/// all copies or substantial portions of the Software. 14/// 15/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21/// THE SOFTWARE. 22/// 23/// @ref gtc_random 24/// @file glm/gtc/random.inl 25/// @date 2011-09-19 / 2012-04-07 26/// @author Christophe Riccio 27/////////////////////////////////////////////////////////////////////////////////// 28 29#include "../geometric.hpp" 30#include "../exponential.hpp" 31#include <cstdlib> 32#include <ctime> 33#include <cassert> 34 35namespace glm{ 36namespace detail 37{ 38 struct compute_linearRand 39 { 40 template <typename T> 41 GLM_FUNC_QUALIFIER T operator() (T const & Min, T const & Max) const; 42/* 43 { 44 GLM_STATIC_ASSERT(0, "'linearRand' invalid template parameter type. GLM_GTC_random only supports floating-point template types."); 45 return Min; 46 } 47*/ 48 }; 49 50 template <> 51 GLM_FUNC_QUALIFIER float compute_linearRand::operator()<float> (float const & Min, float const & Max) const 52 { 53 return float(std::rand()) / float(RAND_MAX) * (Max - Min) + Min; 54 } 55 56 template <> 57 GLM_FUNC_QUALIFIER double compute_linearRand::operator()<double> (double const & Min, double const & Max) const 58 { 59 return double(std::rand()) / double(RAND_MAX) * (Max - Min) + Min; 60 } 61 62 template <> 63 GLM_FUNC_QUALIFIER long double compute_linearRand::operator()<long double> (long double const & Min, long double const & Max) const 64 { 65 return (long double)(std::rand()) / (long double)(RAND_MAX) * (Max - Min) + Min; 66 } 67}//namespace detail 68 69 template <typename genType> 70 GLM_FUNC_QUALIFIER genType linearRand 71 ( 72 genType const & Min, 73 genType const & Max 74 ) 75 { 76 return detail::compute_linearRand()(Min, Max); 77 } 78 79 VECTORIZE_VEC_VEC(linearRand) 80 81 template <typename genType> 82 GLM_FUNC_QUALIFIER genType gaussRand 83 ( 84 genType const & Mean, 85 genType const & Deviation 86 ) 87 { 88 genType w, x1, x2; 89 90 do 91 { 92 x1 = linearRand(genType(-1), genType(1)); 93 x2 = linearRand(genType(-1), genType(1)); 94 95 w = x1 * x1 + x2 * x2; 96 } while(w > genType(1)); 97 98 return x2 * Deviation * Deviation * sqrt((genType(-2) * log(w)) / w) + Mean; 99 } 100 101 VECTORIZE_VEC_VEC(gaussRand) 102 103 template <typename T> 104 GLM_FUNC_QUALIFIER detail::tvec2<T, defaultp> diskRand 105 ( 106 T const & Radius 107 ) 108 { 109 detail::tvec2<T, defaultp> Result(T(0)); 110 T LenRadius(T(0)); 111 112 do 113 { 114 Result = linearRand( 115 detail::tvec2<T, defaultp>(-Radius), 116 detail::tvec2<T, defaultp>(Radius)); 117 LenRadius = length(Result); 118 } 119 while(LenRadius > Radius); 120 121 return Result; 122 } 123 124 template <typename T> 125 GLM_FUNC_QUALIFIER detail::tvec3<T, defaultp> ballRand 126 ( 127 T const & Radius 128 ) 129 { 130 detail::tvec3<T, defaultp> Result(T(0)); 131 T LenRadius(T(0)); 132 133 do 134 { 135 Result = linearRand( 136 detail::tvec3<T, defaultp>(-Radius), 137 detail::tvec3<T, defaultp>(Radius)); 138 LenRadius = length(Result); 139 } 140 while(LenRadius > Radius); 141 142 return Result; 143 } 144 145 template <typename T> 146 GLM_FUNC_QUALIFIER detail::tvec2<T, defaultp> circularRand 147 ( 148 T const & Radius 149 ) 150 { 151 T a = linearRand(T(0), T(6.283185307179586476925286766559f)); 152 return detail::tvec2<T, defaultp>(cos(a), sin(a)) * Radius; 153 } 154 155 template <typename T> 156 GLM_FUNC_QUALIFIER detail::tvec3<T, defaultp> sphericalRand 157 ( 158 T const & Radius 159 ) 160 { 161 T z = linearRand(T(-1), T(1)); 162 T a = linearRand(T(0), T(6.283185307179586476925286766559f)); 163 164 T r = sqrt(T(1) - z * z); 165 166 T x = r * cos(a); 167 T y = r * sin(a); 168 169 return detail::tvec3<T, defaultp>(x, y, z) * Radius; 170 } 171}//namespace glm 172