Skip to content

Instantly share code, notes, and snippets.

@reinsteam
Last active April 29, 2017 11:26
Show Gist options
  • Save reinsteam/506c379f65ce4854af837c0c84eb27dc to your computer and use it in GitHub Desktop.
Save reinsteam/506c379f65ce4854af837c0c84eb27dc to your computer and use it in GitHub Desktop.
/*----------------------------------------------------------------------------------------------------------------------
* Let's imagine two spheres with common center and different radiuses. This function computes distance from a point
* on the sphere with smaller radius (RMin) to the surface on the sphere with bigger radius (RMax) in the direction
* define by Mu (cosine between direction from the point on the smaller sphere to the point on the bigger sphere and
* direction from the point on the smaller sphere to the sphere center)
*
* Complexity : 3 mad, 1 sqrt
*--------------------------------------------------------------------------------------------------------------------*/
float DistanceToSphericalLayer(float RMin, float RMinSq, float RMaxSq, float Mu)
{
return RMin * Mu + sqrt(RMinSq * (Mu * Mu - 1.0) + RMaxSq);
}
/*----------------------------------------------------------------------------------------------------------------------
* Complexity : 3 mad, 2 sqrt, 1 sub
*--------------------------------------------------------------------------------------------------------------------*/
float DistanceBetweenSphericalLayers(float RMinSq, float RMaxSq1, float RMaxSq2, float Mu)
{
// Reference :
//const float D1 = RMin * Mu + sqrt(RMinSq * (Mu * Mu - 1.0) + RMaxSq1);
//const float D2 = RMin * Mu + sqrt(RMinSq * (Mu * Mu - 1.0) + RMaxSq2);
//return D2-D1;
const float MuSqMinusOne = Mu * Mu - 1.0;
const float D1Partial = sqrt(RMinSq * MuSqMinusOne + RMaxSq1);
const float D2Partial = sqrt(RMinSq * MuSqMinusOne + RMaxSq2);
return D2Partial - D1Partial;
}
/*----------------------------------------------------------------------------------------------------------------------
* Complexity : 2 mad, 2 sqrt, 1 sub, 1 mul
*--------------------------------------------------------------------------------------------------------------------*/
float DistanceBetweenSphericalLayers2(float RMinSq, float RMaxOverRMinSqMinusOne1, float RMaxOverRMinSqMinusOne2, float Mu)
{
const float MuSq = Mu * Mu;
const float D1Partial = sqrt(MuSq + RMaxOverRMinSqMinusOne1);
const float D2Partial = sqrt(MuSq + RMaxOverRMinSqMinusOne2);
return (D2Partial - D1Partial) * RMin;
}
/*----------------------------------------------------------------------------------------------------------------------
* Complexity : 2 mad, 1 sqrt, 1 mul
*--------------------------------------------------------------------------------------------------------------------*/
float DistanceToSphericalLayer2(float RMin, float RMaxOverRMinSqMinusOne, float Mu)
{
return RMin * Mu + RMin * sqrt(Mu * Mu + RMaxOverRMinSqMinusOne);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment