Changeset 451 for trunk/src/core


Ignore:
Timestamp:
07/30/15 19:47:02 (10 years ago)
Author:
epyon
Message:
  • math library started
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/random.cc

    r443 r451  
    110110nv::vec2 nv::random::precise_unit_vec2()
    111111{
    112         float angle = frand( glm::pi<float>() * 2.f );
    113         return vec2( glm::cos( angle ), glm::sin( angle ) );
     112        f32 angle = frand( math::pi<f32>() * 2.f );
     113        return vec2( math::cos( angle ), math::sin( angle ) );
    114114}
    115115
    116116nv::vec3 nv::random::precise_unit_vec3()
    117117{
    118         float cos_theta = frange( -1.0f, 1.0f );
    119         float sin_theta = glm::sqrt( 1.0f - cos_theta * cos_theta );
    120         float phi       = frand( 2 * glm::pi<float>() );
     118        f32 cos_theta = frange( -1.0f, 1.0f );
     119        f32 sin_theta = math::sqrt( 1.0f - cos_theta * cos_theta );
     120        f32 phi       = frand( 2 * math::pi<f32>() );
    121121        return vec3(
    122                 sin_theta * glm::sin(phi),
    123                 sin_theta * glm::cos(phi),
     122                sin_theta * math::sin(phi),
     123                sin_theta * math::cos(phi),
    124124                cos_theta
    125125                );
     
    128128nv::vec2 nv::random::fast_disk_point()
    129129{
    130         float r1 = frand();
    131         float r2 = frand();
     130        f32 r1 = frand();
     131        f32 r2 = frand();
    132132        if ( r1 > r2 ) swap( r1, r2 );
    133         float rf = 2*glm::pi<float>()*(r1/r2);
    134         return vec2( r2*glm::cos( rf ), r2*glm::sin( rf ) );
     133        f32 rf = 2* math::pi<f32>()*(r1/r2);
     134        return vec2( r2*math::cos( rf ), r2*math::sin( rf ) );
    135135}
    136136
    137137nv::vec2 nv::random::precise_disk_point()
    138138{
    139         float r = glm::sqrt( frand() );
    140         float rangle = frand( glm::pi<float>() );
    141         return vec2( r*glm::cos( rangle ), r*glm::sin( rangle ) );
     139        f32 r = math::sqrt( frand() );
     140        f32 rangle = frand( math::pi<f32>() );
     141        return vec2( r*math::cos( rangle ), r*math::sin( rangle ) );
    142142}
    143143
    144144nv::vec3 nv::random::fast_sphere_point()
    145145{
    146         float rad     = frand();
    147         float pi      = glm::pi<float>();
    148         float phi     = glm::asin( frange( -1.0f, 1.0f ) ) + pi*.5f;
    149         float theta   = frange( 0.0f, 2 * glm::pi<float>() );
    150         float sin_phi = glm::sin( phi );
     146        f32 rad     = frand();
     147        f32 pi      = math::pi<f32>();
     148        f32 phi     = math::asin( frange( -1.0f, 1.0f ) ) + pi*.5f;
     149        f32 theta   = frange( 0.0f, 2 * math::pi<f32>() );
     150        f32 sin_phi = math::sin( phi );
    151151        return vec3(
    152                 rad * glm::cos(theta) * sin_phi,
    153                 rad * glm::sin(theta) * sin_phi,
    154                 rad * glm::cos(phi)
     152                rad * math::cos(theta) * sin_phi,
     153                rad * math::sin(theta) * sin_phi,
     154                rad * math::cos(phi)
    155155        );
    156156}
     
    158158nv::vec3 nv::random::precise_sphere_point()
    159159{
    160         float radius = glm::pow( frand(), 1.f/3.f );
    161         float cos_theta = frange( -1.0f, 1.0f );
    162         float sin_theta = glm::sqrt( 1.0f - cos_theta * cos_theta );
    163         float phi       = frange( 0.0f, 2 * glm::pi<float>() );
     160        f32 radius = math::pow( frand(), 1.f/3.f );
     161        f32 cos_theta = frange( -1.0f, 1.0f );
     162        f32 sin_theta = math::sqrt( 1.0f - cos_theta * cos_theta );
     163        f32 phi       = frange( 0.0f, 2 * math::pi<f32>() );
    164164        return vec3(
    165                 radius * sin_theta * glm::sin(phi),
    166                 radius * sin_theta * glm::cos(phi),
     165                radius * sin_theta * math::sin(phi),
     166                radius * sin_theta * math::cos(phi),
    167167                radius * cos_theta
    168168                );
     
    199199}
    200200
    201 nv::vec2 nv::random::fast_hollow_disk_point( float iradius, float oradius )
    202 {
    203         float idist2 = iradius * iradius;
    204         float odist2 = oradius * oradius;
    205         float rdist  = glm::sqrt( frange( idist2, odist2 ) );
     201nv::vec2 nv::random::fast_hollow_disk_point( f32 iradius, f32 oradius )
     202{
     203        f32 idist2 = iradius * iradius;
     204        f32 odist2 = oradius * oradius;
     205        f32 rdist  = math::sqrt( frange( idist2, odist2 ) );
    206206        return rdist * precise_unit_vec2();
    207207}
    208208
    209 nv::vec2 nv::random::precise_hollow_disk_point( float iradius, float oradius )
     209nv::vec2 nv::random::precise_hollow_disk_point( f32 iradius, f32 oradius )
    210210{
    211211        return fast_hollow_disk_point( iradius, oradius );
    212212}
    213213
    214 nv::vec3 nv::random::fast_hollow_sphere_point( float iradius, float oradius )
    215 {
    216         float idist3 = iradius * iradius * iradius;
    217         float odist3 = oradius * oradius * oradius;
    218         float rdist  = glm::pow( frange( idist3, odist3 ), 1.f/3.f );
     214nv::vec3 nv::random::fast_hollow_sphere_point( f32 iradius, f32 oradius )
     215{
     216        f32 idist3 = iradius * iradius * iradius;
     217        f32 odist3 = oradius * oradius * oradius;
     218        f32 rdist  = math::pow( frange( idist3, odist3 ), 1.f/3.f );
    219219        return rdist * precise_unit_vec3();
    220220}
    221221
    222 nv::vec3 nv::random::precise_hollow_sphere_point( float iradius, float oradius )
     222nv::vec3 nv::random::precise_hollow_sphere_point( f32 iradius, f32 oradius )
    223223{
    224224        return fast_hollow_sphere_point( iradius, oradius );
     
    232232        vec2 opoint2    = opoint * opoint;
    233233        vec2 odir       = glm::normalize( opoint );
    234         float odist2    = opoint2.x + opoint2.y;
    235 
    236         float low    = iradii2.y * opoint2.x + iradii2.x * opoint2.y;
    237         float idist2 = ((iradii2.x * iradii2.y) / low ) * odist2;
    238 
    239         float rdist     = glm::sqrt( frange( idist2, odist2 ) );
     234        f32 odist2    = opoint2.x + opoint2.y;
     235
     236        f32 low    = iradii2.y * opoint2.x + iradii2.x * opoint2.y;
     237        f32 idist2 = ((iradii2.x * iradii2.y) / low ) * odist2;
     238
     239        f32 rdist     = math::sqrt( frange( idist2, odist2 ) );
    240240        return odir * rdist;   
    241241}
     
    252252        vec3 opoint2    = opoint * opoint;
    253253        vec3 odir       = glm::normalize( opoint );
    254         float odist2    = opoint2.x + opoint2.y + opoint2.z;
    255 
    256         float low    =
     254        f32 odist2    = opoint2.x + opoint2.y + opoint2.z;
     255
     256        f32 low    =
    257257                iradii2.y * iradii2.z * opoint2.x +
    258258                iradii2.x * iradii2.z * opoint2.y +
    259259                iradii2.x * iradii2.y * opoint2.z;
    260         float idist2 = ((iradii2.x * iradii2.y * iradii2.z) / low ) * odist2;
    261 
    262         float odist3 = odist2 * glm::sqrt( odist2 );
    263         float idist3 = idist2 * glm::sqrt( idist2 );
    264 
    265         float rdist     = glm::pow( frange( idist3, odist3 ), 1.f/3.f );
     260        f32 idist2 = ((iradii2.x * iradii2.y * iradii2.z) / low ) * odist2;
     261
     262        f32 odist3 = odist2 * math::sqrt( odist2 );
     263        f32 idist3 = idist2 * math::sqrt( idist2 );
     264
     265        f32 rdist     = math::pow( frange( idist3, odist3 ), 1.f/3.f );
    266266        return odir * rdist;   
    267267}
Note: See TracChangeset for help on using the changeset viewer.