Last active
December 11, 2017 12:36
-
-
Save ivanpopelyshev/2a75479075286deb8ee5dc1fb2e07f09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vec2 invBilinear( in vec2 p, in vec2 a, in vec2 b, in vec2 c, in vec2 d ) | |
{ | |
vec2 e = b-a; | |
vec2 f = d-a; | |
vec2 g = a-b+c-d; | |
vec2 h = p-a; | |
float k2 = cross2d( g, f ); | |
float k1 = cross2d( e, f ) + cross2d( h, g ); | |
float k0 = cross2d( h, e ); | |
float k2u = cross2d( e, g ); | |
float k1u = cross2d( e, f ) + cross2d( g, h ); | |
float k0u = cross2d( h, f); | |
float v1, u1, v2, u2; | |
if (abs(k2) < 1e-5) | |
{ | |
v1 = -k0 / k1; | |
u1 = (h.x - f.x*v1)/(e.x + g.x*v1); | |
} | |
else if (abs(k2u) < 1e-5) | |
{ | |
u1 = k0u / k1u; | |
v1 = (h.y - e.y*u1)/(f.y + g.y*u1); | |
} | |
else | |
{ | |
float w = k1*k1 - 4.0*k0*k2; | |
if( w<0.0 ) return vec2(-1.0); | |
w = sqrt( w ); | |
v1 = (-k1 - w)/(2.0*k2); | |
v2 = (-k1 + w)/(2.0*k2); | |
u1 = (-k1u - w)/(2.0*k2u); | |
u2 = (-k1u + w)/(2.0*k2u); | |
} | |
bool b1 = v1>0.0 && v1<1.0 && u1>0.0 && u1<1.0; | |
bool b2 = v2>0.0 && v2<1.0 && u2>0.0 && u2<1.0; | |
vec2 res = vec2(-1.0); | |
if( b1 && !b2 ) res = vec2( u1, v1 ); | |
if( !b1 && b2 ) res = vec2( u2, v2 ); | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment