cc

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  38 随笔 :: 14 文章 :: 21 评论 :: 0 Trackbacks

class CVector 
{
public:
 union
 {
  float vec[3];
  struct  { float x,y,z;};
 };
}

class CCommonTools 
{
public:
 CCommonTools();
 virtual ~CCommonTools();
public:
 static bool ValidPoint(CVector &LinePoint, CVector &LineV,
                                  CVector &TrianglePoint1, CVector &TrianglePoint2, CVector &TrianglePoint3,CVector &result);
 static float Area(float a, float b, float c);
 static float Distance(CVector &p1, CVector &p2);
};

///////////////////////////////

CCommonTools::CCommonTools()
{

}

CCommonTools::~CCommonTools()
{

}
//计算p1到p2的距离的平方
float CCommonTools::Distance(CVector &p1, CVector &p2)
{
 float dist;
 dist = ((p2.x-p1.x)*(p2.x-p1.x)
  + (p2.y-p1.y)*(p2.y-p1.y)
  + (p2.z-p1.z)*(p2.z-p1.z));
 return (float)sqrt(dist);
}
//利用海伦公式求变成为a,b,c的三角形的面积
float CCommonTools::Area(float a, float b, float c)
{
 float s = (a+b+c)/2;
 return (float)sqrt(s*(s-a)*(s-b)*(s-c));
}
bool CCommonTools::ValidPoint(CVector &LinePoint1, CVector &LinePoint2, CVector &TrianglePoint1, CVector

&TrianglePoint2,CVector &TrianglePoint3,CVector &result)
{
  //三角形所在平面的法向量
  CVector TriangleV;
  //三角形的边方向向量
  CVector VP12, VP13;
  //直线与平面的交点
  CVector CrossPoint;
  //平面方程常数项
  float TriD;
  CVector LineV = LinePoint2 - LinePoint1;
  /*-------计算平面的法向量及常数项-------*/
  //point1->point2
  VP12.x = TrianglePoint2.x - TrianglePoint1.x;
  VP12.y = TrianglePoint2.y - TrianglePoint1.y;
  VP12.z = TrianglePoint2.z - TrianglePoint1.z;
  //point1->point3
  VP13.x = TrianglePoint3.x - TrianglePoint1.x;
  VP13.y = TrianglePoint3.y - TrianglePoint1.y;
  VP13.z = TrianglePoint3.z - TrianglePoint1.z;
  //VP12xVP13
  TriangleV.x = VP12.y*VP13.z - VP12.z*VP13.y;
  TriangleV.y = -(VP12.x*VP13.z - VP12.z*VP13.x);
  TriangleV.z= VP12.x*VP13.y - VP12.y*VP13.x;
  //计算常数项
  TriD = -(TriangleV.x*TrianglePoint1.x
    + TriangleV.y*TrianglePoint1.y
    + TriangleV.z*TrianglePoint1.z);
  /*-------求解直线与平面的交点坐标---------*/
  /* 思路:
   *     首先将直线方程转换为参数方程形式,然后代入平面方程,求得参数t,
   * 将t代入直线的参数方程即可求出交点坐标
  */
  float tempU, tempD;  //临时变量
  tempU = TriangleV.x*LinePoint1.x + TriangleV.y*LinePoint1.y
    + TriangleV.z*LinePoint1.z + TriD;
  tempD = TriangleV.x*LineV.x + TriangleV.y*LineV.y + TriangleV.z*LineV.z;
  //直线与平面平行或在平面上
  if(tempD == 0.0)
  {
  // printf("The line is parallel with the plane.\n");
   return false;
  }
  //计算参数t
  float t = -tempU/tempD;
  //计算交点坐标
  CrossPoint.x = LineV.x*t + LinePoint1.x;
  CrossPoint.y = LineV.y*t + LinePoint1.y;
  CrossPoint.z = LineV.z*t + LinePoint1.z;
  /*----------判断交点是否在三角形内部---------*/

  //计算三角形三条边的长度
  float d12 = Distance(TrianglePoint1, TrianglePoint2);
  float d13 = Distance(TrianglePoint1, TrianglePoint3);
  float d23 = Distance(TrianglePoint2, TrianglePoint3);
  //计算交点到三个顶点的长度
  float c1 = Distance(CrossPoint, TrianglePoint1);
  float c2 = Distance(CrossPoint, TrianglePoint2);
  float c3 = Distance(CrossPoint, TrianglePoint3);
  //求三角形及子三角形的面积
  float areaD = Area(d12, d13, d23);  //三角形面积
  float area1 = Area(c1, c2, d12);    //子三角形1
  float area2 = Area(c1, c3, d13);    //子三角形2
  float area3 = Area(c2, c3, d23);    //子三角形3
  //根据面积判断点是否在三角形内部
  if(fabs(area1+area2+area3-areaD) > 0.001)
  {
  return false;
  }


  result = CrossPoint;
  return true;
}

这几天同学问我如何判断空间中的线段和三角面片是否相交,我想这个也许对其他人也有点用处。

上面的代码是判断两点构成的直线和三点构成的面片是否相交,要判断线段的话,需要再判断交点是否在线段的两个端点之间:  交点和两个端点可以形成两个向量,判断这两个向量的方向即可。

posted on 2008-07-17 09:26 醒目西西 阅读(2008) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理