KiCad PCB EDA Suite
3d_math.h File Reference

Defines math related functions. More...

Go to the source code of this file.

Functions

SFVEC3F SphericalToCartesian (float aInclination, float aAzimuth)
 https://en.wikipedia.org/wiki/Spherical_coordinate_system More...
 
SFVEC3F UniformRandomHemisphereDirection ()
 
SFVEC3F CosWeightedRandomHemisphereDirection (const SFVEC3F &n)
 
bool Refract (const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout, SFVEC3F &aOutVector)
 Based on: https://github.com/mmp/pbrt-v3/blob/master/src/core/reflection.h See also: http://www.flipcode.com/archives/Raytracing_Topics_Techniques-Part_3_Refractions_and_Beers_Law.shtml. More...
 
float mapf (float x, float in_min, float in_max, float out_min, float out_max)
 
float RGBtoGray (const SFVEC3F &aColor)
 
SFVEC3F MaterialDiffuseToColorCAD (const SFVEC3F &aDiffuseColor)
 
float QuadricEasingInOut (float t)
 
float BezierBlend (float t)
 

Detailed Description

Defines math related functions.

Definition in file 3d_math.h.

Function Documentation

◆ BezierBlend()

float BezierBlend ( float  t)
inline

Definition at line 179 of file 3d_math.h.

180 {
181  return t * t * ( 3.0f - 2.0f * t );
182 }

Referenced by TRACK_BALL::Interpolate().

◆ CosWeightedRandomHemisphereDirection()

SFVEC3F CosWeightedRandomHemisphereDirection ( const SFVEC3F n)
inline

Definition at line 69 of file 3d_math.h.

70 {
71  const float Xi1 = (float) rand() / (float) RAND_MAX;
72  const float Xi2 = (float) rand() / (float) RAND_MAX;
73 
74  const float theta = acos( sqrt( 1.0f - Xi1 ) );
75  const float phi = 2.0f * glm::pi<float>() * Xi2;
76 
77  const float xs = sinf( theta ) * cosf( phi );
78  const float ys = cosf( theta );
79  const float zs = sinf( theta ) * sinf( phi );
80 
81  const SFVEC3F y( n.x, n.y, n.z );
82  SFVEC3F h = y;
83 
84  if( fabs( h.x ) <= fabs( h.y ) && fabs( h.x ) <= fabs( h.z ) )
85  h.x= 1.0f;
86  else if( fabs( h.y ) <= fabs( h.x ) && fabs( h.y ) <= fabs( h.z ) )
87  h.y= 1.0f;
88  else
89  h.z= 1.0f;
90 
91 
92  const SFVEC3F x = glm::normalize( glm::cross( h, y ) );
93  const SFVEC3F z = glm::normalize( glm::cross( x, y ) );
94 
95  SFVEC3F direction = xs * x + ys * y + zs * z;
96  return glm::normalize( direction );
97 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

◆ mapf()

float mapf ( float  x,
float  in_min,
float  in_max,
float  out_min,
float  out_max 
)
inline

Definition at line 133 of file 3d_math.h.

134 {
135  x = glm::clamp( x, in_min, in_max );
136 
137  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
138 }

Referenced by RENDER_3D_LEGACY::setupMaterials().

◆ MaterialDiffuseToColorCAD()

SFVEC3F MaterialDiffuseToColorCAD ( const SFVEC3F aDiffuseColor)
inline

Definition at line 147 of file 3d_math.h.

148 {
149  // convert to a discret scale of grays
150  const float luminance = glm::min(
151  ( ( (float) ( (unsigned int) ( 4.0f * RGBtoGray( aDiffuseColor ) ) ) + 0.5f ) / 4.0f )
152  * 1.0f,
153  1.0f );
154 
155  const float maxValue = glm::max( glm::max( glm::max( aDiffuseColor.r, aDiffuseColor.g ),
156  aDiffuseColor.b ), FLT_EPSILON );
157 
158  return ( aDiffuseColor / SFVEC3F( maxValue ) ) * 0.125f + luminance * 0.875f;
159 }
float RGBtoGray(const SFVEC3F &aColor)
Definition: 3d_math.h:140
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

References RGBtoGray().

Referenced by RENDER_3D_RAYTRACE::addModels(), MODEL_3D::Draw(), and MODEL_3D::MODEL_3D().

◆ QuadricEasingInOut()

float QuadricEasingInOut ( float  t)
inline

Definition at line 163 of file 3d_math.h.

164 {
165  if( t <= 0.5f )
166  {
167  return t * t * 2.0f;
168  }
169  else
170  {
171  t = t - 1.0f;
172 
173  return -2.0f * ( t * t ) + 1.0f;
174  }
175 }

Referenced by TRACK_BALL::Interpolate().

◆ Refract()

bool Refract ( const SFVEC3F aInVector,
const SFVEC3F aNormal,
float  aRin_over_Rout,
SFVEC3F aOutVector 
)
inline

Based on: https://github.com/mmp/pbrt-v3/blob/master/src/core/reflection.h See also: http://www.flipcode.com/archives/Raytracing_Topics_Techniques-Part_3_Refractions_and_Beers_Law.shtml.

Parameters
aInVectorincoming vector.
aNormalnormal in the intersection point.
aRin_over_Routincoming refraction index / out refraction index.
aOutVectorthe refracted vector.
Returns
true

Definition at line 112 of file 3d_math.h.

114 {
115  float cosThetaI = -glm::dot( aNormal, aInVector );
116  float sin2ThetaI = glm::max( 0.0f, 1.0f - cosThetaI * cosThetaI );
117  float sin2ThetaT = aRin_over_Rout * aRin_over_Rout * sin2ThetaI;
118 
119  // Handle total internal reflection for transmission
120  if( sin2ThetaT >= 1.0f )
121  return false;
122 
123  float cosThetaT = sqrtf( 1.0f - sin2ThetaT );
124 
125  aOutVector = glm::normalize( aRin_over_Rout * aInVector +
126  ( aRin_over_Rout * cosThetaI - cosThetaT ) *
127  aNormal );
128 
129  return true;
130 }

Referenced by RENDER_3D_RAYTRACE::shadeHit().

◆ RGBtoGray()

float RGBtoGray ( const SFVEC3F aColor)
inline

Definition at line 140 of file 3d_math.h.

141 {
142  return (aColor.r * 0.2126f +
143  aColor.g * 0.7152f +
144  aColor.b * 0.0722f);
145 }

Referenced by RENDER_3D_RAYTRACE::getModelMaterial(), and MaterialDiffuseToColorCAD().

◆ SphericalToCartesian()

SFVEC3F SphericalToCartesian ( float  aInclination,
float  aAzimuth 
)
inline

https://en.wikipedia.org/wiki/Spherical_coordinate_system

Parameters
aInclinationθ ∈ [0, π]
aAzimuthφ ∈ [0, 2π]
Returns
Cartesian coordinates

Definition at line 43 of file 3d_math.h.

44 {
45  float sinInc = glm::sin( aInclination );
46 
47  return SFVEC3F( sinInc * glm::cos( aAzimuth ), sinInc * glm::sin( aAzimuth ),
48  glm::cos( aInclination ) );
49 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

Referenced by init_lights(), and RENDER_3D_RAYTRACE::Reload().

◆ UniformRandomHemisphereDirection()

SFVEC3F UniformRandomHemisphereDirection ( )
inline
Todo:
This is not correct because it is not a gaussian random.

Definition at line 55 of file 3d_math.h.

56 {
57  // It was experienced that this function is slow! do not use it :/
58  // SFVEC3F b( (rand()/(float)RAND_MAX) - 0.5f,
59  // (rand()/(float)RAND_MAX) - 0.5f,
60  // (rand()/(float)RAND_MAX) - 0.5f );
61 
62  SFVEC3F b( Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f );
63 
64  return b;
65 }
float Fast_RandFloat()
Definition: 3d_fastmath.cpp:45
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

References Fast_RandFloat().

Referenced by RENDER_3D_RAYTRACE::shadeHit().