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)
 SphericalToCartesian. More...
 
SFVEC3F UniformRandomHemisphereDirection ()
 
SFVEC3F CosWeightedRandomHemisphereDirection (const SFVEC3F &n)
 
bool Refract (const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout, SFVEC3F &aOutVector)
 Refract 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

float BezierBlend ( float  t)
inline

Definition at line 188 of file 3d_math.h.

Referenced by CTRACK_BALL::Interpolate().

189 {
190  return t * t * ( 3.0f - 2.0f * t );
191 }
SFVEC3F CosWeightedRandomHemisphereDirection ( const SFVEC3F n)
inline

Definition at line 70 of file 3d_math.h.

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

Definition at line 136 of file 3d_math.h.

Referenced by CINFO3D_VISU::GetNrSegmentsCircle(), and C3D_RENDER_OGL_LEGACY::setupMaterials().

141 {
142  x = glm::clamp( x, in_min, in_max );
143 
144  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
145 }
SFVEC3F MaterialDiffuseToColorCAD ( const SFVEC3F aDiffuseColor)
inline

Definition at line 154 of file 3d_math.h.

References max, min, and RGBtoGray().

Referenced by C3D_RENDER_RAYTRACING::add_3D_models(), and C_OGL_3DMODEL::C_OGL_3DMODEL().

155 {
156  // convert to a discret scale of grays
157  const float luminance = glm::min( (((float)((unsigned int) ( 4.0f *
158  RGBtoGray( aDiffuseColor ) ) ) + 0.5f) /
159  4.0f) * 1.0f,
160  1.0f );
161 
162  const float maxValue = glm::max( glm::max( glm::max( aDiffuseColor.r,
163  aDiffuseColor.g),
164  aDiffuseColor.b ),
165  FLT_EPSILON );
166 
167  return (aDiffuseColor / SFVEC3F(maxValue) ) * 0.125f + luminance* 0.875f;
168 }
float RGBtoGray(const SFVEC3F &aColor)
Definition: 3d_math.h:147
#define max(a, b)
Definition: auxiliary.h:86
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
#define min(a, b)
Definition: auxiliary.h:85
float QuadricEasingInOut ( float  t)
inline

Definition at line 172 of file 3d_math.h.

Referenced by CTRACK_BALL::Interpolate().

173 {
174  if( t <= 0.5f )
175  {
176  return t * t * 2.0f;
177  }
178  else
179  {
180  t = t - 1.0f;
181 
182  return -2.0f * (t * t) + 1.0f;
183  }
184 }
bool Refract ( const SFVEC3F aInVector,
const SFVEC3F aNormal,
float  aRin_over_Rout,
SFVEC3F aOutVector 
)
inline

Refract 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 113 of file 3d_math.h.

References max.

Referenced by C3D_RENDER_RAYTRACING::shadeHit().

117 {
118  float cosThetaI = -glm::dot( aNormal, aInVector );
119  float sin2ThetaI = glm::max( 0.0f, 1.0f - cosThetaI * cosThetaI );
120  float sin2ThetaT = aRin_over_Rout * aRin_over_Rout * sin2ThetaI;
121 
122  // Handle total internal reflection for transmission
123  if( sin2ThetaT >= 1.0f )
124  return false;
125 
126  float cosThetaT = sqrtf( 1.0f - sin2ThetaT );
127 
128  aOutVector = glm::normalize( aRin_over_Rout * aInVector +
129  ( aRin_over_Rout * cosThetaI - cosThetaT ) *
130  aNormal );
131 
132  return true;
133 }
#define max(a, b)
Definition: auxiliary.h:86
float RGBtoGray ( const SFVEC3F aColor)
inline

Definition at line 147 of file 3d_math.h.

Referenced by C3D_RENDER_RAYTRACING::add_3D_models(), and MaterialDiffuseToColorCAD().

148 {
149  return (aColor.r * 0.2126f +
150  aColor.g * 0.7152f +
151  aColor.b * 0.0722f);
152 }
SFVEC3F SphericalToCartesian ( float  aInclination,
float  aAzimuth 
)
inline

SphericalToCartesian.

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

Definition at line 43 of file 3d_math.h.

Referenced by init_lights(), and C3D_RENDER_RAYTRACING::reload().

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

Definition at line 54 of file 3d_math.h.

References Fast_RandFloat().

Referenced by C3D_RENDER_RAYTRACING::shadeHit().

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