summaryrefslogtreecommitdiff
path: root/hw6/include/random_number.h
diff options
context:
space:
mode:
Diffstat (limited to 'hw6/include/random_number.h')
-rw-r--r--hw6/include/random_number.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/hw6/include/random_number.h b/hw6/include/random_number.h
new file mode 100644
index 0000000..328de25
--- /dev/null
+++ b/hw6/include/random_number.h
@@ -0,0 +1,50 @@
+/******************************************************************/
+/* This file is part of the homework assignments for CSCI-427/527 */
+/* at The College of William & Mary and authored by Pieter Peers. */
+/* No part of this file, whether altered or in original form, can */
+/* be distributed or used outside the context of CSCI-427/527 */
+/* without consent of either the College of William & Mary or */
+/* Pieter Peers. */
+/******************************************************************/
+#ifndef _RANDOM_NUMBER_H_
+#define _RANDOM_NUMBER_H_
+
+#include <random>
+#include "vec3d.h"
+#include "constants.h"
+#include "coordinateTransform.h"
+
+static std::random_device rd;
+static std::mt19937 rnd;
+
+static unsigned int random_int(unsigned int up=std::numeric_limits<unsigned int>::max())
+{
+ std::uniform_int_distribution<unsigned int> dist(0, up);
+ return dist(rnd);
+}
+
+
+static float random_float(float up=1.0f)
+{
+ std::uniform_real_distribution<float> dist(0, up);
+ return dist(rnd);
+}
+
+
+static vec3d random_direction(const vec3d& normal, float r1=random_float(), float r2=random_float())
+{
+ // generate in standard coordinate system (i.e., Z=(0,0,1))
+ vec3d out(cos(2.0f*PI*r1) * sqrt(std::max(1.0f-r2*r2, 0.0f)),
+ sin(2.0f*PI*r1) * sqrt(std::max(1.0f-r2*r2, 0.0f)),
+ r2);
+
+ // transform to global coordinates
+ coordinateTransformation trans(normal);
+ out = trans.transformDirection(out);
+
+ // Done (PDF = 1/(2.0f*PI))
+ return out;
+}
+
+
+#endif /* _RANDOM_NUMBER_H_ */