OpenMesh 提供了许多可供使用的向量操作函数,使用特别方便。
计算距离:
从官方文档可以看到OpenMesh提供了5个函数,分别为
Scalar length() const //compute euclidean norm
Scalar norm() const //compute euclidean norm
Scalar sqrnorm() const //compute squared euclidean norm
Scalar l1_norm() const //compute L1 (Manhattan) norm
Scalar l8_norm() const //compute l8_norm
test:
MyMesh::Normal p(,-,), q(,,);
cout<<"length : "<<(p-q).length()<<endl;
cout<<"norm : "<<(p-q).norm()<<endl;
cout<<"sqrnorm : "<<(p-q).sqrnorm()<<endl;
cout<<"l1_norm : "<<(p-q).l1_norm()<<endl;
cout<<"l8_norm : "<<(p-q).l8_norm()<<endl;
result:
对于二维空间上的点(也可看做向量,起点为原点) p(x1,y1), q(x2,y2)
欧几里得距离 norm =( (x2-x1)2 + (y2-y1)2 )½
曼哈顿距离(L1距离) l1_norm = |x2-x1| + |y2-y1|
切比雪夫距离(L∞距离) l8_norm = max{|x2-x1| , |y2-y1|}
max & min
test:
MyMesh::Normal p(,-,), q(,,);
cout<<"max : "<<p.max()<<endl;
cout<<"max_abs : "<<p.max_abs()<<endl; cout<<"maximize : "<<p.maximize(q)<<endl;
cout<<p<<endl<<q<<endl;
cout<<"maximized : "<<p.maximized(q)<<endl;
cout<<p<<endl<<q<<endl;
cout<<"maximized : "<<q.maximized(p)<<endl;
cout<<p<<endl<<q<<endl;
result:
从result来看,max 和 max_abs 很简单,不用多说。对于 p.maximize(q) 返回值是向量p与q对应位置的最大值组成的向量,而 p.maximized(q) 判断向量p是否经历了maximize,返回值为0则向量p不变,未经过maximize, 返回值为1则向量p改变,即经过maximize。
对于min,有同样的操作,不再赘述。
向量单位化
OpenMesh提供了三个单位化的函数
vector type & normalize() //normalize vector, return normalized vector
const vector type normalized() //return normalized vector
vector type & normalize_cond() //normalize vector, return normalized vector and avoids div by zero
外加单位化的定义(长度为1),有四种单位化方法。
test:
MyMesh::Normal p1(,-,), p2(,-,), p3(,-,), p4(,-,); MyMesh::Normal q1 = p1.normalize();
cout<<"p1 : "<<p1.length()<<" "<<p1<<endl;
cout<<"q1 : "<<q1.length()<<" "<<q1<<endl<<endl; MyMesh::Normal q2 = p2.normalized();
cout<<"p2 : "<<p2.length()<<" "<<p2<<endl;
cout<<"q2 : "<<q2.length()<<" "<<q2<<endl<<endl; MyMesh::Normal q3 = p3/(p3.length());
cout<<"p3 : "<<p3.length()<<" "<<p3<<endl;
cout<<"q3 : "<<q3.length()<<" "<<q3<<endl<<endl; MyMesh::Normal q4 = p4.normalize_cond();
cout<<"p4 : "<<p4.length()<<" "<<p4<<endl;
cout<<"q4 : "<<q4.length()<<" "<<q4<<endl<<endl<<endl;
result:
从结果看出,1和4效果一样,1和2效果不同,需要注意!!!
点乘叉乘(内积外积)
test:
1 MyMesh::Normal p(,-,), q(,,);
cout<<"dot product : "<< (p | q) <<endl;
cout<<"cross product : "<< (p % q) <<endl;
result:
向量 p(x1,y1,z1), q(x2,y2,z2)
点乘(内积):x1*x2 + y1*y2 + z1*z2
叉乘(外积):(y1*z2-y2*z1, x2*z1-x1*z2, x1*y2-x2*y1) (来自行列式表示的化简,cnblog不能打公式么???)