2011-09-18 07:05:09 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2012-01-09 11:20:01 +00:00
|
|
|
// OpenGL Mathematics Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
|
2011-09-18 07:05:09 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2011-09-18 09:45:57 +00:00
|
|
|
// Created : 2011-09-19
|
|
|
|
// Updated : 2011-09-19
|
2011-09-18 07:05:09 +00:00
|
|
|
// Licence : This source is under MIT licence
|
2011-09-18 09:45:57 +00:00
|
|
|
// File : test/gtc/random.cpp
|
2011-09-18 07:05:09 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <glm/glm.hpp>
|
2011-09-22 19:25:50 +00:00
|
|
|
#include <glm/gtc/random.hpp>
|
2012-04-07 01:30:26 +00:00
|
|
|
#include <glm/gtc/epsilon.hpp>
|
2011-09-18 07:05:09 +00:00
|
|
|
#include <iostream>
|
2013-02-10 18:25:11 +00:00
|
|
|
#if(GLM_LANG & GLM_LANG_CXX0X)
|
|
|
|
# include <array>
|
|
|
|
#endif
|
2011-09-18 07:05:09 +00:00
|
|
|
|
2011-09-23 08:14:25 +00:00
|
|
|
int test_linearRand()
|
2011-09-18 07:05:09 +00:00
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
float ResultFloat = 0.0f;
|
|
|
|
double ResultDouble = 0.0f;
|
|
|
|
for(std::size_t i = 0; i < 100000; ++i)
|
|
|
|
{
|
2011-09-22 19:25:50 +00:00
|
|
|
ResultFloat += glm::linearRand(-1.0f, 1.0f);
|
|
|
|
ResultDouble += glm::linearRand(-1.0, 1.0);
|
2011-09-18 07:05:09 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 18:43:23 +00:00
|
|
|
Error += glm::epsilonEqual(ResultFloat, 0.0f, 0.0001f);
|
|
|
|
Error += glm::epsilonEqual(ResultDouble, 0.0, 0.0001);
|
2011-09-18 12:48:19 +00:00
|
|
|
assert(!Error);
|
2011-09-18 07:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
2011-09-24 22:36:42 +00:00
|
|
|
|
|
|
|
int test_circularRand()
|
2011-09-18 07:05:09 +00:00
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::size_t Max = 100000;
|
|
|
|
float ResultFloat = 0.0f;
|
|
|
|
double ResultDouble = 0.0f;
|
2011-09-24 22:36:42 +00:00
|
|
|
double Radius = 2.0f;
|
|
|
|
|
2011-09-18 07:05:09 +00:00
|
|
|
for(std::size_t i = 0; i < Max; ++i)
|
|
|
|
{
|
2011-09-24 22:36:42 +00:00
|
|
|
ResultFloat += glm::length(glm::circularRand(1.0f));
|
|
|
|
ResultDouble += glm::length(glm::circularRand(Radius));
|
2011-09-18 07:05:09 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 18:43:23 +00:00
|
|
|
Error += glm::epsilonEqual(ResultFloat, float(Max), 0.01f) ? 0 : 1;
|
|
|
|
Error += glm::epsilonEqual(ResultDouble, double(Max) * double(Radius), 0.01) ? 0 : 1;
|
2011-09-18 07:05:09 +00:00
|
|
|
assert(!Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
2011-09-24 22:36:42 +00:00
|
|
|
int test_sphericalRand()
|
2011-09-18 07:05:09 +00:00
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::size_t Max = 100000;
|
|
|
|
float ResultFloatA = 0.0f;
|
|
|
|
float ResultFloatB = 0.0f;
|
|
|
|
float ResultFloatC = 0.0f;
|
|
|
|
double ResultDoubleA = 0.0f;
|
|
|
|
double ResultDoubleB = 0.0f;
|
|
|
|
double ResultDoubleC = 0.0f;
|
2011-09-25 01:25:26 +00:00
|
|
|
|
2011-09-18 07:05:09 +00:00
|
|
|
for(std::size_t i = 0; i < Max; ++i)
|
|
|
|
{
|
2011-09-24 22:36:42 +00:00
|
|
|
ResultFloatA += glm::length(glm::sphericalRand(1.0f));
|
|
|
|
ResultDoubleA += glm::length(glm::sphericalRand(1.0));
|
|
|
|
ResultFloatB += glm::length(glm::sphericalRand(2.0f));
|
|
|
|
ResultDoubleB += glm::length(glm::sphericalRand(2.0));
|
|
|
|
ResultFloatC += glm::length(glm::sphericalRand(3.0f));
|
|
|
|
ResultDoubleC += glm::length(glm::sphericalRand(3.0));
|
2011-09-18 07:05:09 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 18:43:23 +00:00
|
|
|
Error += glm::epsilonEqual(ResultFloatA, float(Max), 0.01f) ? 0 : 1;
|
|
|
|
Error += glm::epsilonEqual(ResultDoubleA, double(Max), 0.0001) ? 0 : 1;
|
|
|
|
Error += glm::epsilonEqual(ResultFloatB, float(Max * 2), 0.01f) ? 0 : 1;
|
|
|
|
Error += glm::epsilonEqual(ResultDoubleB, double(Max * 2), 0.0001) ? 0 : 1;
|
|
|
|
Error += glm::epsilonEqual(ResultFloatC, float(Max * 3), 0.01f) ? 0 : 1;
|
|
|
|
Error += glm::epsilonEqual(ResultDoubleC, double(Max * 3), 0.01) ? 0 : 1;
|
2011-09-18 12:48:19 +00:00
|
|
|
assert(!Error);
|
2011-09-18 07:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
2011-09-24 22:36:42 +00:00
|
|
|
|
2011-09-24 22:51:49 +00:00
|
|
|
int test_diskRand()
|
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
float ResultFloat = 0.0f;
|
|
|
|
double ResultDouble = 0.0f;
|
2011-09-25 01:25:26 +00:00
|
|
|
|
2011-09-24 22:51:49 +00:00
|
|
|
for(std::size_t i = 0; i < 100000; ++i)
|
|
|
|
{
|
|
|
|
ResultFloat += glm::length(glm::diskRand(2.0f));
|
|
|
|
ResultDouble += glm::length(glm::diskRand(2.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
Error += ResultFloat < 200000.f ? 0 : 1;
|
|
|
|
Error += ResultDouble < 200000.0 ? 0 : 1;
|
|
|
|
assert(!Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_ballRand()
|
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
2011-09-25 01:25:26 +00:00
|
|
|
{
|
|
|
|
float ResultFloat = 0.0f;
|
|
|
|
double ResultDouble = 0.0f;
|
2011-09-24 22:51:49 +00:00
|
|
|
|
2011-09-25 01:25:26 +00:00
|
|
|
for(std::size_t i = 0; i < 100000; ++i)
|
|
|
|
{
|
|
|
|
ResultFloat += glm::length(glm::ballRand(2.0f));
|
|
|
|
ResultDouble += glm::length(glm::ballRand(2.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
Error += ResultFloat < 200000.f ? 0 : 1;
|
|
|
|
Error += ResultDouble < 200000.0 ? 0 : 1;
|
|
|
|
assert(!Error);
|
|
|
|
}
|
2011-09-24 22:51:49 +00:00
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
2013-02-10 18:25:11 +00:00
|
|
|
#if(GLM_LANG & GLM_LANG_CXX0X)
|
|
|
|
int test_grid()
|
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
|
|
|
typedef std::array<int, 8> colors;
|
|
|
|
typedef std::array<int, 8 * 8> grid;
|
|
|
|
|
|
|
|
grid Grid;
|
|
|
|
colors Colors;
|
|
|
|
|
|
|
|
grid GridBest;
|
|
|
|
colors ColorsBest;
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
for(std::size_t i = 0; i < Grid.size(); ++i)
|
|
|
|
Grid[i] = int(glm::linearRand(0.0, 8.0 * 8.0 * 8.0 - 1.0) / 64.0);
|
|
|
|
|
|
|
|
for(std::size_t i = 0; i < Grid.size(); ++i)
|
|
|
|
++Colors[Grid[i]];
|
|
|
|
|
|
|
|
bool Exit = true;
|
|
|
|
for(std::size_t i = 0; i < Colors.size(); ++i)
|
|
|
|
{
|
|
|
|
if(Colors[i] == 8)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Exit = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Exit == true)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-18 07:05:09 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
2011-09-23 08:14:25 +00:00
|
|
|
Error += test_linearRand();
|
2011-09-24 22:36:42 +00:00
|
|
|
Error += test_circularRand();
|
|
|
|
Error += test_sphericalRand();
|
2011-09-24 22:51:49 +00:00
|
|
|
Error += test_diskRand();
|
|
|
|
Error += test_ballRand();
|
2011-09-18 07:05:09 +00:00
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|