<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>Crystal Space 1.2.1: csutil/floatrand.h Source File (Crystal Space 1.2.1 Public API Reference)</title> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <table border="0" cellpadding="0" cellspacing="0" width="100%" class="head"> <tr height="59"> <td class="head" width="202" valign="bottom" style="padding-left:0;"><a href="http://www.crystalspace3d.org/"><img src="csblur.png" width="236" height="59" alt="CrystalSpace" border="0"></a></td> <td class="head"><h2>Public API Reference</h2></td> </tr> <tr height="11"> <td colspan="2" class="headshadow" valign="top" style="padding-left:0;"><img src="csblurb.png" width="236" height="11" alt="" border="0"></td> </tr> </table> <div class="content"> <!-- Generated by Doxygen 1.5.3 --> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="classes.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> </ul> </div> <h1>csutil/floatrand.h</h1><a href="floatrand_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> Copyright (C) 2004 by Jorrit Tyberghein</span> <a name="l00003"></a>00003 <span class="comment"> Copyright (C) 2005 by Eric Sunshine</span> <a name="l00004"></a>00004 <span class="comment"></span> <a name="l00005"></a>00005 <span class="comment"> This library is free software; you can redistribute it and/or</span> <a name="l00006"></a>00006 <span class="comment"> modify it under the terms of the GNU Library General Public</span> <a name="l00007"></a>00007 <span class="comment"> License as published by the Free Software Foundation; either</span> <a name="l00008"></a>00008 <span class="comment"> version 2 of the License, or (at your option) any later version.</span> <a name="l00009"></a>00009 <span class="comment"></span> <a name="l00010"></a>00010 <span class="comment"> This library is distributed in the hope that it will be useful,</span> <a name="l00011"></a>00011 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <a name="l00012"></a>00012 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span> <a name="l00013"></a>00013 <span class="comment"> Library General Public License for more details.</span> <a name="l00014"></a>00014 <span class="comment"></span> <a name="l00015"></a>00015 <span class="comment"> You should have received a copy of the GNU Library General Public</span> <a name="l00016"></a>00016 <span class="comment"> License along with this library; if not, write to the Free</span> <a name="l00017"></a>00017 <span class="comment"> Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</span> <a name="l00018"></a>00018 <span class="comment">*/</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="preprocessor">#ifndef __CS_CSUTIL_FLOATRAND_H__</span> <a name="l00021"></a>00021 <span class="preprocessor"></span><span class="preprocessor">#define __CS_CSUTIL_FLOATRAND_H__</span> <a name="l00022"></a>00022 <span class="preprocessor"></span> <a name="l00027"></a>00027 <span class="preprocessor">#include "csextern.h"</span> <a name="l00028"></a>00028 <a name="l00029"></a>00029 <span class="preprocessor">#include "<a class="code" href="vector3_8h.html" title="3D vector.">csgeom/vector3.h</a>"</span> <a name="l00030"></a>00030 <a name="l00034"></a><a class="code" href="classcsRandomFloatGen.html">00034</a> <span class="keyword">class </span>CS_CRYSTALSPACE_EXPORT <a class="code" href="classcsRandomFloatGen.html" title="Fast simple random number generator for floating point values.">csRandomFloatGen</a> <a name="l00035"></a>00035 { <a name="l00036"></a>00036 <span class="keyword">private</span>: <a name="l00037"></a>00037 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> seed; <a name="l00038"></a>00038 <a name="l00039"></a>00039 <span class="keyword">public</span>: <a name="l00041"></a><a class="code" href="classcsRandomFloatGen.html#d03b2241b3697721c3d80db5aa2f96f0">00041</a> <a class="code" href="classcsRandomFloatGen.html" title="Fast simple random number generator for floating point values.">csRandomFloatGen</a> () <a name="l00042"></a>00042 { Initialize(); } <a name="l00044"></a><a class="code" href="classcsRandomFloatGen.html#d4a0b2f17e81731a2b6196772ff46bd2">00044</a> <a class="code" href="classcsRandomFloatGen.html" title="Fast simple random number generator for floating point values.">csRandomFloatGen</a> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> seed) <a name="l00045"></a>00045 { Initialize(seed); } <a name="l00046"></a>00046 <a name="l00048"></a>00048 <span class="keywordtype">void</span> Initialize(); <a name="l00050"></a><a class="code" href="classcsRandomFloatGen.html#d42ee0960504889f0bf029169bfebf39">00050</a> <span class="keywordtype">void</span> Initialize(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> new_seed) <a name="l00051"></a>00051 { seed = new_seed; } <a name="l00052"></a>00052 <a name="l00054"></a><a class="code" href="classcsRandomFloatGen.html#5d96d5b102fc5aa7a7c638db2de7ee4e">00054</a> <span class="keyword">inline</span> <span class="keywordtype">float</span> Get() <a name="l00055"></a>00055 { <a name="l00056"></a>00056 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keyword">const</span> b = 1664525; <a name="l00057"></a>00057 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keyword">const</span> c = 1013904223; <a name="l00058"></a>00058 seed = b * seed + c; <a name="l00059"></a>00059 <span class="keyword">union</span> <a name="l00060"></a>00060 { <a name="l00061"></a>00061 <a class="code" href="group__util.html#g1134b580f8da4de94ca6b1de4d37975e" title="unsigned 32-bit integer (0..4 294 967 295)">uint32</a> jabi; <a name="l00062"></a>00062 <span class="keywordtype">float</span> tastic; <a name="l00063"></a>00063 } pun; <a name="l00064"></a>00064 pun.jabi = 0x3f800000 | (0x007fffff & seed); <a name="l00065"></a>00065 <span class="keywordflow">return</span> pun.tastic - 1.0; <a name="l00066"></a>00066 } <a name="l00067"></a>00067 <a name="l00069"></a><a class="code" href="classcsRandomFloatGen.html#7ee5ae56381fc06f2de7cfff3c67f8f7">00069</a> <span class="keyword">inline</span> <span class="keywordtype">float</span> Get(<span class="keywordtype">float</span> max) <a name="l00070"></a>00070 { <a name="l00071"></a>00071 <span class="keywordflow">return</span> max * Get(); <a name="l00072"></a>00072 } <a name="l00073"></a>00073 <a name="l00075"></a><a class="code" href="classcsRandomFloatGen.html#b50cc7c856fd72725f5239d879510717">00075</a> <span class="keyword">inline</span> <span class="keywordtype">float</span> Get(<span class="keywordtype">float</span> min, <span class="keywordtype">float</span> max) <a name="l00076"></a>00076 { <a name="l00077"></a>00077 <span class="keywordtype">float</span> <span class="keyword">const</span> w = max - min; <a name="l00078"></a>00078 <span class="keywordflow">return</span> min + w * Get(); <a name="l00079"></a>00079 } <a name="l00080"></a>00080 <a name="l00082"></a><a class="code" href="classcsRandomFloatGen.html#e31f74b92486b0944e9cb10757d68d2a">00082</a> <span class="keyword">inline</span> <span class="keywordtype">float</span> GetAngle() <a name="l00083"></a>00083 { <a name="l00084"></a>00084 <span class="keywordflow">return</span> TWO_PI * Get(); <a name="l00085"></a>00085 } <a name="l00086"></a>00086 }; <a name="l00087"></a>00087 <a name="l00091"></a><a class="code" href="classcsRandomVectorGen.html">00091</a> <span class="keyword">class </span>CS_CRYSTALSPACE_EXPORT <a class="code" href="classcsRandomVectorGen.html" title="Random number generator that generates random vector with spherical distribution...">csRandomVectorGen</a> <a name="l00092"></a>00092 { <a name="l00093"></a>00093 <span class="keyword">public</span>: <a name="l00094"></a>00094 <span class="keyword">public</span>: <a name="l00096"></a><a class="code" href="classcsRandomVectorGen.html#081e32dcb1279fa8a29edebc71c8c2cb">00096</a> <a class="code" href="classcsRandomVectorGen.html" title="Random number generator that generates random vector with spherical distribution...">csRandomVectorGen</a> () <a name="l00097"></a>00097 : floatGen () <a name="l00098"></a>00098 {} <a name="l00100"></a><a class="code" href="classcsRandomVectorGen.html#525e5da21cf145838e101aa388dd78ae">00100</a> <a class="code" href="classcsRandomVectorGen.html" title="Random number generator that generates random vector with spherical distribution...">csRandomVectorGen</a> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> seed) <a name="l00101"></a>00101 : floatGen (seed) <a name="l00102"></a>00102 {} <a name="l00103"></a>00103 <a name="l00105"></a><a class="code" href="classcsRandomVectorGen.html#9d297562847c0d684e2d3d5d3175b53e">00105</a> <span class="keywordtype">void</span> Initialize() <a name="l00106"></a>00106 { <a name="l00107"></a>00107 floatGen.Initialize (); <a name="l00108"></a>00108 } <a name="l00110"></a><a class="code" href="classcsRandomVectorGen.html#758b682d9ca89964f03e7a2424b8f8b5">00110</a> <span class="keywordtype">void</span> Initialize(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> new_seed) <a name="l00111"></a>00111 { <a name="l00112"></a>00112 floatGen.Initialize (new_seed); <a name="l00113"></a>00113 } <a name="l00114"></a>00114 <a name="l00116"></a><a class="code" href="classcsRandomVectorGen.html#d5218c843f0f3acb08648fcc667ba67f">00116</a> <span class="keyword">inline</span> <a class="code" href="classcsVector3.html" title="A 3D vector.">csVector3</a> Get () <a name="l00117"></a>00117 { <a name="l00118"></a>00118 <span class="comment">/* </span> <a name="l00119"></a>00119 <span class="comment"> This is a variant of the algorithm for computing a random point</span> <a name="l00120"></a>00120 <span class="comment"> on the unit sphere; the algorithm is suggested in Knuth, v2,</span> <a name="l00121"></a>00121 <span class="comment"> 3rd ed, p136; and attributed to Robert E Knop, CACM, 13 (1970),</span> <a name="l00122"></a>00122 <span class="comment"> 326. </span> <a name="l00123"></a>00123 <span class="comment"> */</span> <a name="l00124"></a>00124 <a class="code" href="classcsVector3.html" title="A 3D vector.">csVector3</a> ret (0.0f); <a name="l00125"></a>00125 <span class="keywordtype">float</span> s, a; <a name="l00126"></a>00126 <a name="l00127"></a>00127 <span class="keywordflow">do</span> <a name="l00128"></a>00128 { <a name="l00129"></a>00129 ret.<a class="code" href="classcsVector3.html#823fdba459ff91e8a5e6834ded7c815f" title="The X component of the vector.">x</a> = 2.0f * floatGen.Get () - 1.0f; <a name="l00130"></a>00130 ret.<a class="code" href="classcsVector3.html#e0a74264b61eb5d61da63b3c101af719" title="The Y component of the vector.">y</a> = 2.0f * floatGen.Get () - 1.0f; <a name="l00131"></a>00131 s = ret.<a class="code" href="classcsVector3.html#4f8fb0ed2df4d38898bbcda746c47552" title="Return the squared norm (magnitude) of this vector.">SquaredNorm</a> (); <a name="l00132"></a>00132 } <span class="keywordflow">while</span>(s > 1.0f); <a name="l00133"></a>00133 <a name="l00134"></a>00134 ret.<a class="code" href="classcsVector3.html#3f761bd195c7ccec59ea863a87a1ad05" title="The Z component of the vector.">z</a> = 2.0f * s - 1.0f; <a name="l00135"></a>00135 <a name="l00136"></a>00136 a = 2.0f * sqrtf (1.0f - s); <a name="l00137"></a>00137 ret.<a class="code" href="classcsVector3.html#823fdba459ff91e8a5e6834ded7c815f" title="The X component of the vector.">x</a> *= a; <a name="l00138"></a>00138 ret.<a class="code" href="classcsVector3.html#e0a74264b61eb5d61da63b3c101af719" title="The Y component of the vector.">y</a> *= a; <a name="l00139"></a>00139 <a name="l00140"></a>00140 <span class="comment">// float x1, x2, x1sq, x2sq;</span> <a name="l00141"></a>00141 <span class="comment">// while (!haveNrs)</span> <a name="l00142"></a>00142 <span class="comment">// {</span> <a name="l00143"></a>00143 <span class="comment">// x1 = floatGen.Get (-1, 1);</span> <a name="l00144"></a>00144 <span class="comment">// x2 = floatGen.Get (-1, 1);</span> <a name="l00145"></a>00145 <span class="comment">// x1sq = x1*x1;</span> <a name="l00146"></a>00146 <span class="comment">// x2sq = x2*x2;</span> <a name="l00147"></a>00147 <span class="comment">// if ( (x1sq)+(x2sq) < 1) haveNrs = true;</span> <a name="l00148"></a>00148 <span class="comment">// }</span> <a name="l00149"></a>00149 <span class="comment">// csVector3 ret;</span> <a name="l00150"></a>00150 <span class="comment">// ret.x = 2*x1*sqrtf(1-x1sq-x2sq);</span> <a name="l00151"></a>00151 <span class="comment">// ret.y = 2*x2*sqrtf(1-x1sq-x2sq);</span> <a name="l00152"></a>00152 <span class="comment">// ret.z = 1-2*(x1sq+x2sq);</span> <a name="l00153"></a>00153 <a name="l00154"></a>00154 <span class="keywordflow">return</span> ret; <a name="l00155"></a>00155 } <a name="l00156"></a>00156 <a name="l00157"></a>00157 <span class="keyword">private</span>: <a name="l00158"></a>00158 <a class="code" href="classcsRandomFloatGen.html" title="Fast simple random number generator for floating point values.">csRandomFloatGen</a> floatGen; <a name="l00159"></a>00159 }; <a name="l00160"></a>00160 <a name="l00161"></a>00161 <span class="preprocessor">#endif // __CS_CSUTIL_FLOATRAND_H__</span> </pre></div><hr size="1"><address><small>Generated for Crystal Space 1.2.1 by <a href="http://www.doxygen.org/index.html">doxygen</a> 1.5.3 </small></address> </div></body> </html>