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 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, see <https://www.gnu.org/licenses/>.
19 */
20
25
26#ifndef _3D_MATH_H
27#define _3D_MATH_H
28
30#include "3d_fastmath.h"
31
39inline SFVEC3F SphericalToCartesian( float aInclination, float aAzimuth )
40{
41 float sinInc = glm::sin( aInclination );
42
43 return SFVEC3F( sinInc * glm::cos( aAzimuth ), sinInc * glm::sin( aAzimuth ),
44 glm::cos( aInclination ) );
45}
46
47
52{
53 // It was experienced that this function is slow! do not use it :/
54 // SFVEC3F b( (rand()/(float)RAND_MAX) - 0.5f,
55 // (rand()/(float)RAND_MAX) - 0.5f,
56 // (rand()/(float)RAND_MAX) - 0.5f );
57
58 SFVEC3F b( Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f );
59
60 return b;
61}
62
63
64// https://pathtracing.wordpress.com/2011/03/03/cosine-weighted-hemisphere/
66{
67 const float Xi1 = (float) rand() / (float) RAND_MAX;
68 const float Xi2 = (float) rand() / (float) RAND_MAX;
69
70 const float theta = acos( sqrt( 1.0f - Xi1 ) );
71 const float phi = 2.0f * glm::pi<float>() * Xi2;
72
73 const float xs = sinf( theta ) * cosf( phi );
74 const float ys = cosf( theta );
75 const float zs = sinf( theta ) * sinf( phi );
76
77 const SFVEC3F y( n.x, n.y, n.z );
78 SFVEC3F h = y;
79
80 if( fabs( h.x ) <= fabs( h.y ) && fabs( h.x ) <= fabs( h.z ) )
81 h.x= 1.0f;
82 else if( fabs( h.y ) <= fabs( h.x ) && fabs( h.y ) <= fabs( h.z ) )
83 h.y= 1.0f;
84 else
85 h.z= 1.0f;
86
87
88 const SFVEC3F x = glm::normalize( glm::cross( h, y ) );
89 const SFVEC3F z = glm::normalize( glm::cross( x, y ) );
90
91 SFVEC3F direction = xs * x + ys * y + zs * z;
92 return glm::normalize( direction );
93}
94
95
108inline bool Refract( const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout,
109 SFVEC3F& aOutVector )
110{
111 float cosThetaI = -glm::dot( aNormal, aInVector );
112 float sin2ThetaI = glm::max( 0.0f, 1.0f - cosThetaI * cosThetaI );
113 float sin2ThetaT = aRin_over_Rout * aRin_over_Rout * sin2ThetaI;
114
115 // Handle total internal reflection for transmission
116 if( sin2ThetaT >= 1.0f )
117 return false;
118
119 float cosThetaT = sqrtf( 1.0f - sin2ThetaT );
120
121 aOutVector = glm::normalize( aRin_over_Rout * aInVector +
122 ( aRin_over_Rout * cosThetaI - cosThetaT ) *
123 aNormal );
124
125 return true;
126}
127
128
129inline float mapf( float x, float in_min, float in_max, float out_min, float out_max )
130{
131 x = glm::clamp( x, in_min, in_max );
132
133 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
134}
135
136inline float RGBtoGray( const SFVEC3F &aColor )
137{
138 return (aColor.r * 0.2126f +
139 aColor.g * 0.7152f +
140 aColor.b * 0.0722f);
141}
142
143inline SFVEC3F MaterialDiffuseToColorCAD( const SFVEC3F &aDiffuseColor )
144{
145 // convert to a discret scale of grays
146 const float luminance = glm::min(
147 ( ( (float) ( (unsigned int) ( 4.0f * RGBtoGray( aDiffuseColor ) ) ) + 0.5f ) / 4.0f )
148 * 1.0f,
149 1.0f );
150
151 const float maxValue = glm::max( glm::max( glm::max( aDiffuseColor.r, aDiffuseColor.g ),
152 aDiffuseColor.b ), FLT_EPSILON );
153
154 return ( aDiffuseColor / SFVEC3F( maxValue ) ) * 0.125f + luminance * 0.875f;
155}
156
157
158// http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiJ4KngqMiIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MCwiZXEiOiItKCh4LTEpXjIpKjIrMSIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0xLjM4NzUwMDAwMDAwMDAwMDIiLCIxLjg2MjQ5OTk5OTk5OTk5OTgiLCItMC43IiwiMS4zIl19XQ--
159inline float QuadricEasingInOut( float t )
160{
161 if( t <= 0.5f )
162 {
163 return t * t * 2.0f;
164 }
165 else
166 {
167 t = t - 1.0f;
168
169 return -2.0f * ( t * t ) + 1.0f;
170 }
171}
172
173
174// http://www.wolframalpha.com/input/?i=t%5E2(3-2t)
175inline float BezierBlend( float t )
176{
177 return t * t * ( 3.0f - 2.0f * t );
178}
179
180#endif // 3D_MATH_H
float Fast_RandFloat()
Defines math related functions.
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
Definition 3d_math.h:129
float RGBtoGray(const SFVEC3F &aColor)
Definition 3d_math.h:136
SFVEC3F CosWeightedRandomHemisphereDirection(const SFVEC3F &n)
Definition 3d_math.h:65
float BezierBlend(float t)
Definition 3d_math.h:175
SFVEC3F MaterialDiffuseToColorCAD(const SFVEC3F &aDiffuseColor)
Definition 3d_math.h:143
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:108
SFVEC3F UniformRandomHemisphereDirection()
Definition 3d_math.h:51
float QuadricEasingInOut(float t)
Definition 3d_math.h:159
SFVEC3F SphericalToCartesian(float aInclination, float aAzimuth)
https://en.wikipedia.org/wiki/Spherical_coordinate_system
Definition 3d_math.h:39
glm::vec3 SFVEC3F
Definition xv3d_types.h:40