KiCad PCB EDA Suite
Loading...
Searching...
No Matches
3d_math.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015-2016 Mario Luzeiro <[email protected]>
5 * Copyright (C) 1992-2020 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
30#ifndef _3D_MATH_H
31#define _3D_MATH_H
32
34#include "3d_fastmath.h"
35
43inline SFVEC3F SphericalToCartesian( float aInclination, float aAzimuth )
44{
45 float sinInc = glm::sin( aInclination );
46
47 return SFVEC3F( sinInc * glm::cos( aAzimuth ), sinInc * glm::sin( aAzimuth ),
48 glm::cos( aInclination ) );
49}
50
51
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}
66
67
68// https://pathtracing.wordpress.com/2011/03/03/cosine-weighted-hemisphere/
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}
98
99
112inline bool Refract( const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout,
113 SFVEC3F& aOutVector )
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}
131
132
133inline float mapf( float x, float in_min, float in_max, float out_min, float out_max )
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}
139
140inline float RGBtoGray( const SFVEC3F &aColor )
141{
142 return (aColor.r * 0.2126f +
143 aColor.g * 0.7152f +
144 aColor.b * 0.0722f);
145}
146
147inline SFVEC3F MaterialDiffuseToColorCAD( const SFVEC3F &aDiffuseColor )
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}
160
161
162// http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiJ4KngqMiIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MCwiZXEiOiItKCh4LTEpXjIpKjIrMSIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0xLjM4NzUwMDAwMDAwMDAwMDIiLCIxLjg2MjQ5OTk5OTk5OTk5OTgiLCItMC43IiwiMS4zIl19XQ--
163inline float QuadricEasingInOut( float t )
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}
176
177
178// http://www.wolframalpha.com/input/?i=t%5E2(3-2t)
179inline float BezierBlend( float t )
180{
181 return t * t * ( 3.0f - 2.0f * t );
182}
183
184#endif // 3D_MATH_H
float Fast_RandFloat()
Definition: 3d_fastmath.cpp:45
Defines math related functions.
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
Definition: 3d_math.h:133
float RGBtoGray(const SFVEC3F &aColor)
Definition: 3d_math.h:140
SFVEC3F CosWeightedRandomHemisphereDirection(const SFVEC3F &n)
Definition: 3d_math.h:69
float BezierBlend(float t)
Definition: 3d_math.h:179
SFVEC3F MaterialDiffuseToColorCAD(const SFVEC3F &aDiffuseColor)
Definition: 3d_math.h:147
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....
Definition: 3d_math.h:112
SFVEC3F UniformRandomHemisphereDirection()
Definition: 3d_math.h:55
float QuadricEasingInOut(float t)
Definition: 3d_math.h:163
SFVEC3F SphericalToCartesian(float aInclination, float aAzimuth)
https://en.wikipedia.org/wiki/Spherical_coordinate_system
Definition: 3d_math.h:43
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44