Browse Source

Improved linearRand: support precision and integers (#230)

Christophe Riccio 11 years ago
parent
commit
5f7862ebec
5 changed files with 301 additions and 29 deletions
  1. 11 0
      glm/detail/func_common.inl
  2. 9 4
      glm/gtc/random.hpp
  3. 261 25
      glm/gtc/random.inl
  4. 1 0
      readme.txt
  5. 19 0
      test/gtc/gtc_random.cpp

+ 11 - 0
glm/detail/func_common.inl

@@ -311,6 +311,17 @@ namespace detail
 		return std::modf(x, &i);
 	}
 
+	template <typename T, precision P>
+	GLM_FUNC_QUALIFIER detail::tvec1<T, P> modf
+	(
+		detail::tvec1<T, P> const & x,
+		detail::tvec1<T, P> & i
+	)
+	{
+		return detail::tvec1<T, P>(
+			modf(x.x, i.x));
+	}
+
 	template <typename T, precision P>
 	GLM_FUNC_QUALIFIER detail::tvec2<T, P> modf
 	(

+ 9 - 4
glm/gtc/random.hpp

@@ -58,10 +58,15 @@ namespace glm
 	/// @param Max 
 	/// @tparam genType Value type. Currently supported: half (not recommanded), float or double scalars and vectors.
 	/// @see gtc_random
-	template <typename genType>
-	GLM_FUNC_DECL genType linearRand(
-		genType const & Min,
-		genType const & Max);
+	template <typename genTYpe>
+	GLM_FUNC_DECL genTYpe linearRand(
+		genTYpe const & Min,
+		genTYpe const & Max);
+
+	template <typename T, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_DECL vecType<T, P> linearRand(
+		vecType<T, P> const & Min,
+		vecType<T, P> const & Max);
 
 	/// Generate random numbers in the interval [Min, Max], according a gaussian distribution 
 	/// 

+ 261 - 25
glm/gtc/random.inl

@@ -35,53 +35,289 @@
 namespace glm{
 namespace detail
 {
+	template <typename T, precision P, template <class, precision> class vecType>
+	struct compute_rand
+	{
+		GLM_FUNC_QUALIFIER static vecType<T, P> call();
+	};
+
+	template <precision P>
+	struct compute_rand<uint8, P, detail::tvec1>
+	{
+		GLM_FUNC_QUALIFIER static detail::tvec1<uint8, P> call()
+		{
+			return detail::tvec1<uint8, P>(
+				std::rand()) % std::numeric_limits<uint8>::max();
+		}
+	};
+
+	template <precision P>
+	struct compute_rand<uint8, P, detail::tvec2>
+	{
+		GLM_FUNC_QUALIFIER static detail::tvec2<uint8, P> call()
+		{
+			return detail::tvec2<uint8, P>(
+				std::rand(),
+				std::rand()) % std::numeric_limits<uint8>::max();
+		}
+	};
+
+	template <precision P>
+	struct compute_rand<uint8, P, detail::tvec3>
+	{
+		GLM_FUNC_QUALIFIER static detail::tvec3<uint8, P> call()
+		{
+			return detail::tvec3<uint8, P>(
+				std::rand(),
+				std::rand(),
+				std::rand()) % std::numeric_limits<uint8>::max();
+		}
+	};
+
+	template <precision P>
+	struct compute_rand<uint8, P, detail::tvec4>
+	{
+		GLM_FUNC_QUALIFIER static detail::tvec4<uint8, P> call()
+		{
+			return detail::tvec4<uint8, P>(
+				std::rand(),
+				std::rand(),
+				std::rand(),
+				std::rand()) % std::numeric_limits<uint8>::max();
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_rand<uint16, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<uint16, P> call()
+		{
+			return
+				(vecType<uint16, P>(compute_rand<uint8, P, vecType>::call()) << static_cast<uint16>(8)) |
+				(vecType<uint16, P>(compute_rand<uint8, P, vecType>::call()) << static_cast<uint16>(0));
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_rand<uint32, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<uint32, P> call()
+		{
+			return
+				(vecType<uint32, P>(compute_rand<uint16, P, vecType>::call()) << static_cast<uint32>(16)) |
+				(vecType<uint32, P>(compute_rand<uint16, P, vecType>::call()) << static_cast<uint32>(0));
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_rand<uint64, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<uint64, P> call()
+		{
+			return
+				(vecType<uint64, P>(compute_rand<uint32, P, vecType>::call()) << static_cast<uint64>(32)) |
+				(vecType<uint64, P>(compute_rand<uint32, P, vecType>::call()) << static_cast<uint64>(0));
+		}
+	};
+
+	template <typename T, precision P, template <class, precision> class vecType>
 	struct compute_linearRand
 	{
-		template <typename T>
-		GLM_FUNC_QUALIFIER T operator() (T const & Min, T const & Max) const;
-/*
+		GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & Min, vecType<T, P> const & Max);
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<int8, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<int8, P> call(vecType<int8, P> const & Min, vecType<int8, P> const & Max)
 		{
-			GLM_STATIC_ASSERT(0, "'linearRand' invalid template parameter type. GLM_GTC_random only supports floating-point template types.");
-			return Min;
+			return (vecType<int8, P>(compute_rand<uint8, P, vecType>::call()) % (Max - Min)) + Min;
 		}
-*/
 	};
 
-	template <>
-	GLM_FUNC_QUALIFIER float compute_linearRand::operator()<float> (float const & Min, float const & Max) const
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<uint8, P, vecType>
 	{
-		return float(std::rand()) / float(RAND_MAX) * (Max - Min) + Min;
-	}
+		GLM_FUNC_QUALIFIER static vecType<uint8, P> call(vecType<uint8, P> const & Min, vecType<uint8, P> const & Max)
+		{
+			return (compute_rand<uint8, P, vecType>::call() % (Max - Min)) + Min;
+		}
+	};
 
-	template <>
-	GLM_FUNC_QUALIFIER double compute_linearRand::operator()<double> (double const & Min, double const & Max) const
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<int16, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<int16, P> call(vecType<int16, P> const & Min, vecType<int16, P> const & Max)
+		{
+			return (vecType<int16, P>(compute_rand<uint16, P, vecType>::call()) % (Max - Min)) + Min;
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<uint16, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<uint16, P> call(vecType<uint16, P> const & Min, vecType<uint16, P> const & Max)
+		{
+			return (compute_rand<uint16, P, vecType>::call() % (Max - Min)) + Min;
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<int32, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<int32, P> call(vecType<int32, P> const & Min, vecType<int32, P> const & Max)
+		{
+			return (vecType<int32, P>(compute_rand<uint32, P, vecType>::call()) % (Max - Min)) + Min;
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<uint32, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<uint32, P> call(vecType<uint32, P> const & Min, vecType<uint32, P> const & Max)
+		{
+			return (compute_rand<uint32, P, vecType>::call() % (Max - Min)) + Min;
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<int64, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<int64, P> call(vecType<int64, P> const & Min, vecType<int64, P> const & Max)
+		{
+			return (vecType<int64, P>(compute_rand<uint64, P, vecType>::call()) % (Max - Min)) + Min;
+		}
+	};
+
+	template <precision P, template <class, precision> class vecType>
+	struct compute_linearRand<uint64, P, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<uint64, P> call(vecType<uint64, P> const & Min, vecType<uint64, P> const & Max)
+		{
+			return (compute_rand<uint64, P, vecType>::call() % (Max - Min)) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<float, lowp, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<float, lowp> call(vecType<float, lowp> const & Min, vecType<float, lowp> const & Max)
+		{
+			return vecType<float, lowp>(compute_rand<uint8, lowp, vecType>::call()) / static_cast<float>(std::numeric_limits<uint8>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<float, mediump, vecType>
 	{
-		return double(std::rand()) / double(RAND_MAX) * (Max - Min) + Min;
+		GLM_FUNC_QUALIFIER static vecType<float, mediump> call(vecType<float, mediump> const & Min, vecType<float, mediump> const & Max)
+		{
+			return vecType<float, mediump>(compute_rand<uint16, mediump, vecType>::call()) / static_cast<float>(std::numeric_limits<uint16>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<float, highp, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<float, highp> call(vecType<float, highp> const & Min, vecType<float, highp> const & Max)
+		{
+			return vecType<float, highp>(compute_rand<uint32, highp, vecType>::call()) / static_cast<float>(std::numeric_limits<uint32>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<double, lowp, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<double, lowp> call(vecType<double, lowp> const & Min, vecType<double, lowp> const & Max)
+		{
+			return vecType<double, lowp>(compute_rand<uint16, lowp, vecType>::call()) / static_cast<double>(std::numeric_limits<uint16>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<double, mediump, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<double, mediump> call(vecType<double, mediump> const & Min, vecType<double, mediump> const & Max)
+		{
+			return vecType<double, mediump>(compute_rand<uint32, mediump, vecType>::call()) / static_cast<double>(std::numeric_limits<uint32>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<double, highp, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<double, highp> call(vecType<double, highp> const & Min, vecType<double, highp> const & Max)
+		{
+			return vecType<double, highp>(compute_rand<uint64, highp, vecType>::call()) / static_cast<double>(std::numeric_limits<uint64>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<long double, lowp, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<long double, lowp> call(vecType<long double, lowp> const & Min, vecType<long double, lowp> const & Max)
+		{
+			return vecType<long double, lowp>(compute_rand<uint32, lowp, vecType>::call()) / static_cast<long double>(std::numeric_limits<uint32>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<long double, mediump, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<long double, mediump> call(vecType<long double, mediump> const & Min, vecType<long double, mediump> const & Max)
+		{
+			return vecType<long double, mediump>(compute_rand<uint64, mediump, vecType>::call()) / static_cast<long double>(std::numeric_limits<uint64>::max()) * (Max - Min) + Min;
+		}
+	};
+
+	template <template <class, precision> class vecType>
+	struct compute_linearRand<long double, highp, vecType>
+	{
+		GLM_FUNC_QUALIFIER static vecType<long double, highp> call(vecType<long double, highp> const & Min, vecType<long double, highp> const & Max)
+		{
+			return vecType<long double, highp>(compute_rand<uint64, highp, vecType>::call()) / static_cast<long double>(std::numeric_limits<uint64>::max()) * (Max - Min) + Min;
+		}
+	};
+}//namespace detail
+
+	template <typename T, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> linearRand
+	(
+		vecType<T, P> const & Min,
+		vecType<T, P> const & Max
+	)
+	{
+		return detail::compute_linearRand<T, P, vecType>::call(Min, Max);
 	}
 
 	template <>
-	GLM_FUNC_QUALIFIER long double compute_linearRand::operator()<long double> (long double const & Min, long double const & Max) const
+	GLM_FUNC_QUALIFIER float linearRand<float>
+	(
+		float const & Min,
+		float const & Max
+	)
 	{
-		return (long double)(std::rand()) / (long double)(RAND_MAX) * (Max - Min) + Min;
+		return detail::compute_linearRand<float, highp, detail::tvec1>::call(
+			detail::tvec1<float, highp>(Min),
+			detail::tvec1<float, highp>(Max)).x;
 	}
-}//namespace detail
 
-	template <typename genType> 
-	GLM_FUNC_QUALIFIER genType linearRand
+	template <>
+	GLM_FUNC_QUALIFIER double linearRand<double>
 	(
-		genType const & Min,
-		genType const & Max
+		double const & Min,
+		double const & Max
 	)
 	{
-		return detail::compute_linearRand()(Min, Max);
+		return detail::compute_linearRand<double, highp, detail::tvec1>::call(
+			detail::tvec1<double, highp>(Min),
+			detail::tvec1<double, highp>(Max)).x;
 	}
 
-	VECTORIZE_VEC_VEC(linearRand)
-
-	template <typename genType> 
+	template <typename genType>
 	GLM_FUNC_QUALIFIER genType gaussRand
 	(
-		genType const & Mean,	
+		genType const & Mean,
 		genType const & Deviation
 	)
 	{

+ 1 - 0
readme.txt

@@ -47,6 +47,7 @@ GLM 0.9.6.0: 2014-XX-XX
 - Fixed Visual Studio 14 compiler warnings
 - Added *vec1 support to *vec2 types
 - Limited extended integer type redifinition (#233)
+- Improved linearRand: support precision and integers (#230)
 
 ================================================================================
 GLM 0.9.5.5: 2014-XX-XX

+ 19 - 0
test/gtc/gtc_random.cpp

@@ -18,6 +18,25 @@ int test_linearRand()
 {
 	int Error = 0;
 
+	{
+		glm::i8vec2 A = glm::linearRand(glm::i8vec2(16), glm::i8vec2(32));
+		glm::i16vec2 B = glm::linearRand(glm::i16vec2(16), glm::i16vec2(32));
+		glm::i32vec2 C = glm::linearRand(glm::i32vec2(16), glm::i32vec2(32));
+		glm::i64vec2 D = glm::linearRand(glm::i64vec2(16), glm::i64vec2(32));
+	}
+
+	{
+		glm::u8vec2 A = glm::linearRand(glm::u8vec2(16), glm::u8vec2(32));
+		glm::u16vec2 B = glm::linearRand(glm::u16vec2(16), glm::u16vec2(32));
+		glm::u32vec2 C = glm::linearRand(glm::u32vec2(16), glm::u32vec2(32));
+		glm::u64vec2 D = glm::linearRand(glm::u64vec2(16), glm::u64vec2(32));
+	}
+
+	{
+		glm::f32vec2 A = glm::linearRand(glm::f32vec2(16), glm::f32vec2(32));
+		glm::f64vec2 B = glm::linearRand(glm::f64vec2(16), glm::f64vec2(32));
+	}
+
 	{
 		float ResultFloat = 0.0f;
 		double ResultDouble = 0.0f;