summaryrefslogtreecommitdiff
path: root/hw6/include/random_number.h
blob: 328de25d02f9a14f668e4200ac8e57e1ab5e570c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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_ */