Files
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

213 lines
4.1 KiB
C++

//-----------------------------------------------------------------------------+
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
// = FileName : TPGeLine3D.cpp
// = Version : ver2.0
// = Author : wlw
// = CreateDate : 2002-09-09
// = Description: 类的定义
// = Maintainers:
//
//-----------------------------------------------------------------------------+
#include "StdAfx.h"
#include "TPGeLine3D.h"
#include "TGMatrix.h"
TPGeLine3D::TPGeLine3D():a(x),b(y),c(z)
{
}
TPGeLine3D::TPGeLine3D(const TGVector3D& vt,const TGPoint& pt):TGVector3D(vt),basePoint(pt),a(x),b(y),c(z)
{
}
TPGeLine3D::TPGeLine3D(const TGPoint& pt0,const TGPoint& pt1):TGVector3D(pt0,pt1),basePoint(pt0),a(x),b(y),c(z)
{
}
TPGeLine3D::TPGeLine3D(const TPGeLine3D& line):TGVector3D(line),basePoint(line.basePoint),a(x),b(y),c(z)
{
}
TPGeLine3D::~TPGeLine3D()
{
}
TPGeLine3D& TPGeLine3D::operator = (const TPGeLine3D& line)
{
a = line.a;
b = line.b;
c = line.c;
basePoint = line.basePoint;
return *this;
}
bool TPGeLine3D::IsValid() const
{
return !IsNullVector();
}
void TPGeLine3D::Pa2Pt(double pa,TGPoint& pt) const
{
pt.x = basePoint.x + a * pa;
pt.y = basePoint.y + b * pa;
pt.z = basePoint.z + c * pa;
}
void TPGeLine3D::Pt2Pa(const TGPoint& pt,double& pa) const
{
if(fabs(a) > fabs(b) && fabs(a) > fabs(c))
{
pa = (pt.x - basePoint.x) / a;
}
else if(fabs(b)>fabs(c))
{
pa = (pt.y - basePoint.y) / b;
}
else
{
pa = (pt.z - basePoint.z) / c;
}
}
bool TPGeLine3D::IsInSamePlane(const TPGeLine3D& line) const
{
TGVector3D vt(basePoint,line.basePoint);
return (vt*(*this)).IsParallel(vt*line);
}
bool TPGeLine3D::IsSameLine(const TPGeLine3D& line,bool bSameDirection,bool bNormalized) const
{
bool r = IsParallel(line,bNormalized) && HitTest(line.basePoint,bNormalized).Is(GR_IN);
if(bSameDirection)
{
return r && IsSameDirection(line,bNormalized);
}
else
{
return r;
}
}
GRelation TPGeLine3D::HitTest(const TGPoint& pt,bool bNormalized) const
{
TGVector3D vt1(basePoint,pt);
if(bNormalized)
{//vt1最多进行一次规范化
vt1.Normalize();
}
if(IsParallel(vt1,bNormalized))
{
return GR_IN;
}
else
{
return GR_OUT;
}
}
int TPGeLine3D::Intersect(const TPGeLine3D& line,double& pa,bool bNormalized) const
{
if(bNormalized)
{
return LineInt3D(basePoint,*this,line.basePoint,line,pa);
}
TGVector3D vt1(*this),vt2(line);
double l1 = vt1.Length();
vt1 /= l1;
vt2.Normalize();
int iRet = LineInt3D(basePoint,vt1,line.basePoint,vt2,pa);
pa /= l1;
return iRet;
}
int TPGeLine3D::Intersect(const TPGeLine3D& line,TGPoint& pt,bool bNormalized) const
{
double pa;
int res;
if(bNormalized)
{
res = LineInt3D(basePoint,*this,line.basePoint,line,pa);
}
else
{
TGVector3D vt1(*this),vt2(line);
double l1 = vt1.Length();
vt1 /= l1;
vt2.Normalize();
res = LineInt3D(basePoint,vt1,line.basePoint,vt2,pa);
pa /= l1;
}
if(res == INT_1PT)
{
Pa2Pt(pa,pt);
}
return res;
}
int TPGeLine3D::Intersect(const TPGeLine3D& line,double& pa1,double& pa2,bool bNormalized) const
{
TGPoint pt;
int res = Intersect(line,pt,bNormalized);
if(res == INT_1PT)
{
Pt2Pa(pt,pa1);
line.Pt2Pa(pt,pa2);
}
return res;
}
void TPGeLine3D::TransformBy(const TGMatrix* pMatrix)
{
ASSERT(pMatrix != NULL);
pMatrix->Transform(*this);
pMatrix->Transform3D(basePoint);
}
int TPGeLine3D::LineInt3D(const TGPoint &pt1,const TGVector3D &vt1, const TGPoint &pt2,const TGVector3D &vt2, double &pa1)const
{
TGVector3D vt3(pt1,pt2);
vt3.Normalize();
if (!(vt1 * vt3).IsParallel(vt2 * vt3,true))
{
return INT_NULL;
}
double temp = (vt2.y * vt1.x - vt2.x * vt1.y);
if (!EQUAL_ZERO(temp))
{
pa1 = (vt2.y * pt2.x - vt2.y * pt1.x - vt2.x * pt2.y + vt2.x * pt1.y) / temp;
return INT_1PT;
}
temp = (vt2.z * vt1.x - vt2.x * vt1.z);
if (!EQUAL_ZERO(temp))
{
pa1 = (vt2.z * pt2.x - vt2.z * pt1.x - vt2.x * pt2.z + vt2.x * pt1.z) / temp;
return INT_1PT;
}
temp = (vt2.y * vt1.z - vt2.z * vt1.y);
if (!EQUAL_ZERO(temp))
{
pa1 = (vt2.y * pt2.z - vt2.y * pt1.z - vt2.z * pt2.y + vt2.z * pt1.y) / temp;
return INT_1PT;
}
if (vt1.IsParallel(vt3,true))
{
return INT_LINE;
}
else
{
return INT_NULL;
}
}