计算triangle sdf

点点圈 提交于 2019-12-01 20:50:21

 

 

 

发现Houdini行列式竟然会出问题????有人测试过matrix2吗。。。。。

float area_tri(vector2 a, b ,c){
    float m11 = b.x - a.x;
    float m12 = c.x - a.x;
    float m21 = b.y - a.y;
    float m22 = c.y - a.y;
    
    //  THIS WILL CAUSE ERROR -----
    //matrix2 m = set( m11,m12,m21,m22) ;
    //float area = determinant(m);
    //return area * 0.5f;
    //  THIS WILL CAUSE ERROR ------
    
    
    return 0.5f*(a.x*b.y + b.x*c.y + c.x*a.y  -a.x*c.y - b.x*a.y - c.x*b.y);
}

// 

int in_tri(float abs_AF,abs_BT,abs_GM)
{

    if (abs_AF > 0.99999f) return 0; 
    if (abs_BT > 0.99999f) return 0; 
    if (abs_GM > 0.99999f) return 0; 
    if( abs_AF + abs_BT +abs_GM> 1.0f) return 0;
    return 1;
}

// define our triangle
vector2 a = set(-2.0f, 0.0f);
vector2 b = set(2.0f, 0.0f);
vector2 c = set(0.0f, 2.0f);

vector2 inp = set(@P.x, @P.y);
    
float A = area_tri(a, b, c);

float Aa = area_tri(inp, b,c);
float Ab = area_tri(inp, a,c);
float Ac = area_tri(inp, a,b);

float AF =  Aa / A;
float BT  = Ab / A;
float GM =  Ac / A;


float abs_AF = abs(AF);
float abs_BT = abs(BT);
float abs_GM = abs(GM);
f@sdf = 0;
if (in_tri(abs_AF,abs_BT,abs_GM) ) // if exist in tri;
{
    f@sdf =  min(abs_GM, min(abs_AF,abs_BT) );
}
else{
    f@sdf = 0 ;
}

 

 

参考:fundamentals of computer graphics 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!