SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLRaySamples2D.cpp
Go to the documentation of this file.
1 /**
2  * \file SLRaySamples2D.cpp
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <chrono>
11 #include <assert.h>
12 #include <SLRaySamples2D.h>
13 
14 //-----------------------------------------------------------------------------
15 //! Resets the sample point array by the sqrt of the no. of samples
16 void SLRaySamples2D::samples(SLuint x, SLuint y, SLbool evenlyDistributed)
17 {
18  assert(x > 0 && y > 0);
19  _samplesX = x;
20  _samplesY = y;
21  _samples = x * y;
22  _points.resize(_samples);
23  if (_samples > 1) distribConcentric(evenlyDistributed);
24 }
25 //-----------------------------------------------------------------------------
26 /*!
27 Makes concentric 2D-samples points within a circle of certain radius.
28 With the parameter evenlyDistributed=false will the samplepoints be
29 denser towards the center.
30 */
32 {
33  if (_points.size())
34  {
35  SLfloat halfDeltaPhi = Utils::TWOPI / _samplesY * 0.5f;
36  SLfloat phi, r, last_r = 1.0f;
37 
38  // Loop over radius r and angle phi
39  for (SLint iR = (SLint)_samplesX - 1; iR >= 0; --iR)
40  {
41  r = ((SLfloat)iR) / _samplesX;
42  if (evenlyDistributed) r = sqrt(r);
43  r += (last_r - r) * 0.5f;
44 
45  // every 2nd circle is rotated by have delta phi for better distribution
46  SLfloat iModPhi = (iR % 2) * halfDeltaPhi;
47  for (SLint iPhi = (SLint)_samplesY - 1; iPhi >= 0; --iPhi)
48  {
49  phi = Utils::TWOPI * ((SLfloat)iPhi) / _samplesY + iModPhi;
50  point((SLuint)iR, (SLuint)iPhi, SLVec2f(r * cos(phi), r * sin(phi)));
51  }
52  last_r = r;
53  }
54  }
55 }
56 //-----------------------------------------------------------------------------
57 /*! Concentric mapping of a x,y-position
58 Code taken from Peter Shirley out of "Realistic Ray Tracing"
59 */
61  SLfloat y) // [0 < y <=1]
62 {
63  SLfloat phi, r, u, v;
64  SLfloat a = 2 * x - 1;
65  SLfloat b = 2 * y - 1;
66 
67  if (a > -b)
68  {
69  if (a > b)
70  {
71  r = a;
72  phi = (Utils::PI / 4) * (b / a);
73  }
74  else
75  {
76  r = b;
77  phi = (Utils::PI / 4) * (2 - a / b);
78  }
79  }
80  else
81  {
82  if (a < b)
83  {
84  r = -a;
85  phi = (Utils::PI / 4) * (4 + b / a);
86  }
87  else
88  {
89  r = -b;
90  if (b != 0)
91  {
92  phi = (Utils::PI / 4) * (6 - a / b);
93  }
94  else
95  phi = 0;
96  }
97  }
98  u = r * cos(phi);
99  v = r * sin(phi);
100  return SLVec2f(u, v);
101 }
102 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
int SLint
Definition: SL.h:170
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLuint samples()
SLVec2f mapSquareToDisc(SLfloat x, SLfloat y)
void distribConcentric(SLbool evenlyDistributed)
SLVVec2f _points
samplepoints for distributed tracing
SLuint _samples
No. of samples = samplesX x samplesY.
SLuint _samplesX
No. of samples in x direction.
void point(SLuint x, SLuint y, SLVec2f point)
SLuint _samplesY
No. of samples in y direction.
static const float PI
Definition: Utils.h:237
static const float TWOPI
Definition: Utils.h:240