You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file 3d_math.h
  26. * @brief Defines math related functions
  27. */
  28. #ifndef _3D_MATH_H
  29. #define _3D_MATH_H
  30. #include <plugins/3dapi/xv3d_types.h>
  31. #include "3d_fastmath.h"
  32. /**
  33. * https://en.wikipedia.org/wiki/Spherical_coordinate_system
  34. *
  35. * @param aInclination θ [0, π]
  36. * @param aAzimuth φ [0, 2π]
  37. * @return Cartesian coordinates
  38. */
  39. inline SFVEC3F SphericalToCartesian( float aInclination, float aAzimuth )
  40. {
  41. float sinInc = glm::sin( aInclination );
  42. return SFVEC3F( sinInc * glm::cos( aAzimuth ), sinInc * glm::sin( aAzimuth ),
  43. glm::cos( aInclination ) );
  44. }
  45. /**
  46. * @todo This is not correct because it is not a gaussian random.
  47. */
  48. inline SFVEC3F UniformRandomHemisphereDirection()
  49. {
  50. // It was experienced that this function is slow! do not use it :/
  51. // SFVEC3F b( (rand()/(float)RAND_MAX) - 0.5f,
  52. // (rand()/(float)RAND_MAX) - 0.5f,
  53. // (rand()/(float)RAND_MAX) - 0.5f );
  54. SFVEC3F b( Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f );
  55. return b;
  56. }
  57. // https://pathtracing.wordpress.com/2011/03/03/cosine-weighted-hemisphere/
  58. inline SFVEC3F CosWeightedRandomHemisphereDirection( const SFVEC3F& n )
  59. {
  60. const float Xi1 = (float) rand() / (float) RAND_MAX;
  61. const float Xi2 = (float) rand() / (float) RAND_MAX;
  62. const float theta = acos( sqrt( 1.0f - Xi1 ) );
  63. const float phi = 2.0f * glm::pi<float>() * Xi2;
  64. const float xs = sinf( theta ) * cosf( phi );
  65. const float ys = cosf( theta );
  66. const float zs = sinf( theta ) * sinf( phi );
  67. const SFVEC3F y( n.x, n.y, n.z );
  68. SFVEC3F h = y;
  69. if( fabs( h.x ) <= fabs( h.y ) && fabs( h.x ) <= fabs( h.z ) )
  70. h.x= 1.0f;
  71. else if( fabs( h.y ) <= fabs( h.x ) && fabs( h.y ) <= fabs( h.z ) )
  72. h.y= 1.0f;
  73. else
  74. h.z= 1.0f;
  75. const SFVEC3F x = glm::normalize( glm::cross( h, y ) );
  76. const SFVEC3F z = glm::normalize( glm::cross( x, y ) );
  77. SFVEC3F direction = xs * x + ys * y + zs * z;
  78. return glm::normalize( direction );
  79. }
  80. /**
  81. * Based on:
  82. * https://github.com/mmp/pbrt-v3/blob/master/src/core/reflection.h
  83. * See also:
  84. * http://www.flipcode.com/archives/Raytracing_Topics_Techniques-Part_3_Refractions_and_Beers_Law.shtml
  85. *
  86. * @param aInVector incoming vector.
  87. * @param aNormal normal in the intersection point.
  88. * @param aRin_over_Rout incoming refraction index / out refraction index.
  89. * @param aOutVector the refracted vector.
  90. * @return true
  91. */
  92. inline bool Refract( const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout,
  93. SFVEC3F& aOutVector )
  94. {
  95. float cosThetaI = -glm::dot( aNormal, aInVector );
  96. float sin2ThetaI = glm::max( 0.0f, 1.0f - cosThetaI * cosThetaI );
  97. float sin2ThetaT = aRin_over_Rout * aRin_over_Rout * sin2ThetaI;
  98. // Handle total internal reflection for transmission
  99. if( sin2ThetaT >= 1.0f )
  100. return false;
  101. float cosThetaT = sqrtf( 1.0f - sin2ThetaT );
  102. aOutVector = glm::normalize( aRin_over_Rout * aInVector +
  103. ( aRin_over_Rout * cosThetaI - cosThetaT ) *
  104. aNormal );
  105. return true;
  106. }
  107. inline float mapf( float x, float in_min, float in_max, float out_min, float out_max )
  108. {
  109. x = glm::clamp( x, in_min, in_max );
  110. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  111. }
  112. inline float RGBtoGray( const SFVEC3F &aColor )
  113. {
  114. return (aColor.r * 0.2126f +
  115. aColor.g * 0.7152f +
  116. aColor.b * 0.0722f);
  117. }
  118. inline SFVEC3F MaterialDiffuseToColorCAD( const SFVEC3F &aDiffuseColor )
  119. {
  120. // convert to a discret scale of grays
  121. const float luminance = glm::min(
  122. ( ( (float) ( (unsigned int) ( 4.0f * RGBtoGray( aDiffuseColor ) ) ) + 0.5f ) / 4.0f )
  123. * 1.0f,
  124. 1.0f );
  125. const float maxValue = glm::max( glm::max( glm::max( aDiffuseColor.r, aDiffuseColor.g ),
  126. aDiffuseColor.b ), FLT_EPSILON );
  127. return ( aDiffuseColor / SFVEC3F( maxValue ) ) * 0.125f + luminance * 0.875f;
  128. }
  129. // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiJ4KngqMiIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MCwiZXEiOiItKCh4LTEpXjIpKjIrMSIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0xLjM4NzUwMDAwMDAwMDAwMDIiLCIxLjg2MjQ5OTk5OTk5OTk5OTgiLCItMC43IiwiMS4zIl19XQ--
  130. inline float QuadricEasingInOut( float t )
  131. {
  132. if( t <= 0.5f )
  133. {
  134. return t * t * 2.0f;
  135. }
  136. else
  137. {
  138. t = t - 1.0f;
  139. return -2.0f * ( t * t ) + 1.0f;
  140. }
  141. }
  142. // http://www.wolframalpha.com/input/?i=t%5E2(3-2t)
  143. inline float BezierBlend( float t )
  144. {
  145. return t * t * ( 3.0f - 2.0f * t );
  146. }
  147. #endif // 3D_MATH_H