280 lines
6.0 KiB
C++
280 lines
6.0 KiB
C++
#include "StdAfx.h"
|
|
#include "GePlane.h"
|
|
#include "ADSGePoint3d.h"
|
|
#include "GeCurveInline.h"
|
|
|
|
GePlane::GePlane()
|
|
: m_dParaA(0.0), m_dParaB(0.0), m_dParaC(1.0), m_dParaD(0.0)
|
|
{
|
|
|
|
}
|
|
|
|
GePlane::GePlane(double aa, double bb, double cc, double dd)
|
|
: m_dParaA(aa), m_dParaB(bb), m_dParaC(cc), m_dParaD(dd)
|
|
{
|
|
|
|
}
|
|
|
|
GePlane::GePlane(const GePlane &src)
|
|
: m_dParaA(src.m_dParaA), m_dParaB(src.m_dParaB),
|
|
m_dParaC(src.m_dParaC), m_dParaD(src.m_dParaD)
|
|
{
|
|
|
|
}
|
|
|
|
GePlane::GePlane(const ADSGePoint3d &ptOrg, const TVector3D &vtNorm)
|
|
{
|
|
m_dParaA = vtNorm[0];
|
|
m_dParaB = vtNorm[1];
|
|
m_dParaC = vtNorm[2];
|
|
|
|
const TVector3D &vt=vtNorm;
|
|
const ADSGePoint3d &pt=ptOrg;
|
|
|
|
m_dParaD = 0.0 - (vt[0] * pt[0] + vt[1] * pt[1] + vt[2] * pt[2]);
|
|
}
|
|
|
|
|
|
GePlane::~GePlane()
|
|
{
|
|
|
|
}
|
|
|
|
// 3点确定一平面方程
|
|
BOOL GePlane::SetFrom3p(const ADSGePoint3d &pt1, const ADSGePoint3d &pt2, const ADSGePoint3d &pt3)
|
|
{
|
|
TVector3D vt1 = pt2 - pt1;
|
|
TVector3D vt2 = pt3 - pt1;
|
|
using namespace GlobalFunc;
|
|
TVector3D vtNorm = vt1 * vt2;
|
|
|
|
if (vtNorm.Length() < 1.0E-9)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
vtNorm.Normalize();
|
|
|
|
m_dParaA = vtNorm[0];
|
|
m_dParaB = vtNorm[1];
|
|
m_dParaC = vtNorm[2];
|
|
|
|
const TVector3D &vt = vtNorm;
|
|
const ADSGePoint3d &pt = pt1;
|
|
|
|
m_dParaD = 0.0 - (vt[0] * pt[0] + vt[1] * pt[1] + vt[2] * pt[2]);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// 平面与线求交,考虑了线是否为无限长
|
|
BOOL GePlane::Intersect(const GeLine &ln, ADSGePoint3d &ptInt) const
|
|
{
|
|
const ADSGePoint3d &pt1 = ln.GetStartPoint();
|
|
const ADSGePoint3d &pt2 = ln.GetEndPoint();
|
|
ads_real l, m, n, t;
|
|
ads_real mid_var1, mid_var2;
|
|
|
|
l = pt2[0] - pt1[0];
|
|
m = pt2[1] - pt1[1];
|
|
n = pt2[2] - pt1[2];
|
|
|
|
mid_var1 = l * m_dParaA + m * m_dParaB + n * m_dParaC;
|
|
if (fabs(mid_var1) < 1.0E-9)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
mid_var2 = m_dParaD + pt1[0] * m_dParaA +
|
|
pt1[1] * m_dParaB + pt1[2] * m_dParaC;
|
|
t = 0.0 - mid_var2 / mid_var1;
|
|
|
|
ptInt[0] = pt1[0] + l * t;
|
|
ptInt[1] = pt1[1] + m * t;
|
|
ptInt[2] = pt1[2] + n * t;
|
|
|
|
if (ln.GetLineFlag() != LS_INFINITE)
|
|
{
|
|
int nPrFlag = ptInt & ln;
|
|
if (nPrFlag == PR_OUTSIDE)
|
|
{
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// 平面与平面求交,求交线(无限长)
|
|
BOOL GePlane::Intersect(const GePlane &plan, GeLine &ln) const
|
|
{
|
|
AcGePlane plane1(m_dParaA, m_dParaB,
|
|
m_dParaC, m_dParaD);
|
|
AcGePlane plan2(plan.GetParaA(), plan.GetParaB(), plan.GetParaC(), plan.GetParaD());
|
|
AcGeLine3d ln3d;
|
|
|
|
BOOL bRet = plane1.intersectWith(plan2, ln3d);
|
|
if (bRet)
|
|
{
|
|
AcGeVector3d vt = ln3d.direction();
|
|
|
|
ln.SetLineFlag(LS_INFINITE);
|
|
ln.SetStartPoint(ln3d.pointOnLine());
|
|
ln.SetEndPoint(ln.GetStartPoint() + vt);
|
|
}
|
|
|
|
return bRet;
|
|
}
|
|
|
|
GePlane & GePlane::operator = (const GePlane &src)
|
|
{
|
|
m_dParaA = src.GetParaA();
|
|
m_dParaB = src.GetParaB();
|
|
m_dParaC = src.GetParaC();
|
|
m_dParaD = src.GetParaD();
|
|
|
|
return *this;
|
|
}
|
|
|
|
double GePlane::GetParaA() const
|
|
{
|
|
return m_dParaA;
|
|
}
|
|
|
|
void GePlane::SetParaA(const double dVal)
|
|
{
|
|
m_dParaA = dVal;
|
|
}
|
|
|
|
double GePlane::GetParaB() const
|
|
{
|
|
return m_dParaB;
|
|
}
|
|
|
|
void GePlane::SetParaB(const double dVal)
|
|
{
|
|
m_dParaB = dVal;
|
|
}
|
|
|
|
double GePlane::GetParaC() const
|
|
{
|
|
return m_dParaC;
|
|
}
|
|
|
|
void GePlane::SetParaC(const double dVal)
|
|
{
|
|
m_dParaC = dVal;
|
|
}
|
|
|
|
double GePlane::GetParaD() const
|
|
{
|
|
return m_dParaD;
|
|
}
|
|
|
|
void GePlane::SetParaD(const double dVal)
|
|
{
|
|
m_dParaD = dVal;
|
|
}
|
|
|
|
BOOL GePlane::IsValid() const
|
|
{
|
|
return (ADSGePoint3d(m_dParaA, m_dParaB, m_dParaC).Length() > 1.0E-9);
|
|
}
|
|
|
|
BOOL GePlane::operator !() const
|
|
{
|
|
return (!IsValid());
|
|
}
|
|
|
|
TVector3D GePlane::GetNormal() const
|
|
{
|
|
TVector3D vt(m_dParaA, m_dParaB, m_dParaC);
|
|
vt.Normalize();
|
|
return vt;
|
|
}
|
|
|
|
double GePlane::GetAngle(TVector3D vtLight)const
|
|
{
|
|
AcGeVector3d vtNormal = GetNormal().AsAcGeVector3d();
|
|
vtLight.Reverse();
|
|
|
|
AcGeVector3d vt = vtLight.AsAcGeVector3d();
|
|
|
|
return vt.angleTo(vtNormal);
|
|
}
|
|
|
|
bool GePlane::IsReflect(TVector3D vtLight)const
|
|
{
|
|
double dAng = GetAngle(vtLight);
|
|
return dAng < PI / 2 - 1E-4;
|
|
}
|
|
|
|
bool GePlane::Reflect(TVector3D vtLight,TVector3D& vtRes)const
|
|
{
|
|
AcGeVector3d vt = vtLight.AsAcGeVector3d();
|
|
vtRes = TVector3D(vt.mirror(GetNormal().AsAcGeVector3d()));
|
|
|
|
return IsReflect(vtLight);
|
|
}
|
|
|
|
bool GePlane::IsKindOf(GeEntity::TEntityId entType) const
|
|
{
|
|
switch(entType)
|
|
{
|
|
case GeEntity::eEntity:
|
|
case GeEntity::ePlane:
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
GeEntity::TEntityId GePlane::Type() const
|
|
{
|
|
return GeEntity::ePlane;
|
|
}
|
|
|
|
void GePlane::CopyFrom(const GeEntity* pImpSrc)
|
|
{
|
|
if (this != pImpSrc && pImpSrc->IsKindOf(GeEntity::ePlane))
|
|
{
|
|
m_dParaA = ((GePlane*)pImpSrc)->GetParaA();
|
|
m_dParaA = ((GePlane*)pImpSrc)->GetParaB();
|
|
m_dParaA = ((GePlane*)pImpSrc)->GetParaC();
|
|
m_dParaA = ((GePlane*)pImpSrc)->GetParaD();
|
|
}
|
|
}
|
|
|
|
BOOL GetShadowMatrix(const GePlane&plane, const TVector3D &vtLight, AcGeMatrix3d &mShadowMatrix)
|
|
{
|
|
double lightPos[4] = {vtLight[0], vtLight[1], vtLight[2], 0.0f};
|
|
double dot = plane.GetParaA() * lightPos[0]
|
|
+ plane.GetParaB() * lightPos[1]
|
|
+ plane.GetParaC() * lightPos[2]
|
|
+ plane.GetParaD() * lightPos[3];
|
|
if (dot>-1.0E-9)
|
|
{
|
|
return FALSE; //光线平行或同向于平面
|
|
}
|
|
|
|
mShadowMatrix(0,0) = (1.0 - lightPos[0] * plane.GetParaA() / dot);
|
|
mShadowMatrix(0,1) = (-lightPos[0] * plane.GetParaB() / dot);
|
|
mShadowMatrix(0,2) = (-lightPos[0] * plane.GetParaC() / dot);
|
|
mShadowMatrix(0,3) = (-lightPos[0] * plane.GetParaD() / dot);
|
|
|
|
mShadowMatrix(1,0) = (-lightPos[1] * plane.GetParaA() / dot);
|
|
mShadowMatrix(1,1) = (1.0 - lightPos[1] * plane.GetParaB() / dot);
|
|
mShadowMatrix(1,2) = (-lightPos[1] * plane.GetParaC() / dot);
|
|
mShadowMatrix(1,3) = (-lightPos[1] * plane.GetParaD() / dot);
|
|
|
|
mShadowMatrix(2,0) = (-lightPos[2] * plane.GetParaA() / dot);
|
|
mShadowMatrix(2,1) = (-lightPos[2] * plane.GetParaB() / dot);
|
|
mShadowMatrix(2,2) = (1.0 - lightPos[2] * plane.GetParaC() / dot);
|
|
mShadowMatrix(2,3) = (-lightPos[2] * plane.GetParaD() / dot);
|
|
|
|
mShadowMatrix(3,0) = 0.0;
|
|
mShadowMatrix(3,1) = 0.0;
|
|
mShadowMatrix(3,2) = 0.0;
|
|
mShadowMatrix(3,3) = 1.0;
|
|
return TRUE;
|
|
}
|