综述
C+±计算三角形面片的法线代码;随手记下来,免得找不到
库
依然是CGAL 11
代码
Point calTriNormal( Point ver1, Point ver2, Point ver3 )
{
double temp1[3], temp2[3],normal[3];
double length = 0.0;
temp1[0] = ver1[0] - ver2[0];
temp1[1] = ver1[1] - ver2[1];
temp1[2] = ver1[2] -ver2[2];
temp2[0] = ver2[0] - ver3[0];
temp2[1] = ver2[1] - ver3[1];
temp2[2] = ver2[2] - ver3[2];
//计算法线
normal[0] =temp1[1]* temp2[2] - temp1[2]* temp2[1] ;
normal[1] = -(temp1[0]* temp2[2] - temp1[2]* temp2[0]) ;
normal[2] =temp1[0]* temp2[1] - temp1[1] * temp2[0] ;
//法线单位化
length = sqrt(normal[0] * normal[0] + normal[1]* normal[1]+ normal[2] * normal[2]);
if(length == 0.0f){length = 1.0f;}
normal[0]/= length ;
normal[1]/= length ;
normal[2]/= length ;
Point e_normal(normal[0],normal[1],normal[2]);
return e_normal;
}
使用
int main(int argc, char* argv[])
{
Point a(1,0,0);
Point b(0,1,0);
Point c(0,0,1);
cout << calTriNormal(a,b,c) << endl;
return 0;
}