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
43glm::mat4 CalcModelMatrix( const SFVEC3F& aOffset, const SFVEC3F& aRotation, const SFVEC3F& aScale );
44
51SFVEC3F CalcModelRotation( const glm::mat3& aRotation );
52
64SFVEC3F InterpolateModelRotation( const SFVEC3F& aStart, const SFVEC3F& aEnd, float aT );
65
66
74inline SFVEC3F SphericalToCartesian( float aInclination, float aAzimuth )
75{
76 float sinInc = glm::sin( aInclination );
77
78 return SFVEC3F( sinInc * glm::cos( aAzimuth ), sinInc * glm::sin( aAzimuth ),
79 glm::cos( aInclination ) );
80}
81
82
87{
88 // It was experienced that this function is slow! do not use it :/
89 // SFVEC3F b( (rand()/(float)RAND_MAX) - 0.5f,
90 // (rand()/(float)RAND_MAX) - 0.5f,
91 // (rand()/(float)RAND_MAX) - 0.5f );
92
93 SFVEC3F b( Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f, Fast_RandFloat() * 0.5f );
94
95 return b;
96}
97
98
99// https://pathtracing.wordpress.com/2011/03/03/cosine-weighted-hemisphere/
101{
102 const float Xi1 = (float) rand() / (float) RAND_MAX;
103 const float Xi2 = (float) rand() / (float) RAND_MAX;
104
105 const float theta = acos( sqrt( 1.0f - Xi1 ) );
106 const float phi = 2.0f * glm::pi<float>() * Xi2;
107
108 const float xs = sinf( theta ) * cosf( phi );
109 const float ys = cosf( theta );
110 const float zs = sinf( theta ) * sinf( phi );
111
112 const SFVEC3F y( n.x, n.y, n.z );
113 SFVEC3F h = y;
114
115 if( fabs( h.x ) <= fabs( h.y ) && fabs( h.x ) <= fabs( h.z ) )
116 h.x= 1.0f;
117 else if( fabs( h.y ) <= fabs( h.x ) && fabs( h.y ) <= fabs( h.z ) )
118 h.y= 1.0f;
119 else
120 h.z= 1.0f;
121
122
123 const SFVEC3F x = glm::normalize( glm::cross( h, y ) );
124 const SFVEC3F z = glm::normalize( glm::cross( x, y ) );
125
126 SFVEC3F direction = xs * x + ys * y + zs * z;
127 return glm::normalize( direction );
128}
129
130
143inline bool Refract( const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout,
144 SFVEC3F& aOutVector )
145{
146 float cosThetaI = -glm::dot( aNormal, aInVector );
147 float sin2ThetaI = glm::max( 0.0f, 1.0f - cosThetaI * cosThetaI );
148 float sin2ThetaT = aRin_over_Rout * aRin_over_Rout * sin2ThetaI;
149
150 // Handle total internal reflection for transmission
151 if( sin2ThetaT >= 1.0f )
152 return false;
153
154 float cosThetaT = sqrtf( 1.0f - sin2ThetaT );
155
156 aOutVector = glm::normalize( aRin_over_Rout * aInVector +
157 ( aRin_over_Rout * cosThetaI - cosThetaT ) *
158 aNormal );
159
160 return true;
161}
162
163
164inline float mapf( float x, float in_min, float in_max, float out_min, float out_max )
165{
166 x = glm::clamp( x, in_min, in_max );
167
168 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
169}
170
171inline float RGBtoGray( const SFVEC3F &aColor )
172{
173 return (aColor.r * 0.2126f +
174 aColor.g * 0.7152f +
175 aColor.b * 0.0722f);
176}
177
178inline SFVEC3F MaterialDiffuseToColorCAD( const SFVEC3F &aDiffuseColor )
179{
180 // convert to a discret scale of grays
181 const float luminance = glm::min(
182 ( ( (float) ( (unsigned int) ( 4.0f * RGBtoGray( aDiffuseColor ) ) ) + 0.5f ) / 4.0f )
183 * 1.0f,
184 1.0f );
185
186 const float maxValue = glm::max( glm::max( glm::max( aDiffuseColor.r, aDiffuseColor.g ),
187 aDiffuseColor.b ), FLT_EPSILON );
188
189 return ( aDiffuseColor / SFVEC3F( maxValue ) ) * 0.125f + luminance * 0.875f;
190}
191
192
193// http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiJ4KngqMiIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MCwiZXEiOiItKCh4LTEpXjIpKjIrMSIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0xLjM4NzUwMDAwMDAwMDAwMDIiLCIxLjg2MjQ5OTk5OTk5OTk5OTgiLCItMC43IiwiMS4zIl19XQ--
194inline float QuadricEasingInOut( float t )
195{
196 if( t <= 0.5f )
197 {
198 return t * t * 2.0f;
199 }
200 else
201 {
202 t = t - 1.0f;
203
204 return -2.0f * ( t * t ) + 1.0f;
205 }
206}
207
208
209// http://www.wolframalpha.com/input/?i=t%5E2(3-2t)
210inline float BezierBlend( float t )
211{
212 return t * t * ( 3.0f - 2.0f * t );
213}
214
215#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:164
float RGBtoGray(const SFVEC3F &aColor)
Definition 3d_math.h:171
SFVEC3F CosWeightedRandomHemisphereDirection(const SFVEC3F &n)
Definition 3d_math.h:100
SFVEC3F InterpolateModelRotation(const SFVEC3F &aStart, const SFVEC3F &aEnd, float aT)
Interpolate between two model orientations along the shortest arc.
Definition 3d_math.cpp:73
SFVEC3F CalcModelRotation(const glm::mat3 &aRotation)
Recover the FP_3DMODEL angles that CalcModelMatrix() would turn into aRotation.
Definition 3d_math.cpp:47
glm::mat4 CalcModelMatrix(const SFVEC3F &aOffset, const SFVEC3F &aRotation, const SFVEC3F &aScale)
Build the model-to-footprint transform used by rendering and picking.
Definition 3d_math.cpp:34
float BezierBlend(float t)
Definition 3d_math.h:210
SFVEC3F MaterialDiffuseToColorCAD(const SFVEC3F &aDiffuseColor)
Definition 3d_math.h:178
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:143
SFVEC3F UniformRandomHemisphereDirection()
Definition 3d_math.h:86
float QuadricEasingInOut(float t)
Definition 3d_math.h:194
SFVEC3F SphericalToCartesian(float aInclination, float aAzimuth)
https://en.wikipedia.org/wiki/Spherical_coordinate_system
Definition 3d_math.h:74
glm::vec3 SFVEC3F
Definition xv3d_types.h:40