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

714 lines
12 KiB
C++

#include "StdAfx.h"
#include "globalFunc.h"
#include "ADSGePoint3d.h"
#include "GeCircle.h"
#include "GePlane.h"
#include "GeLine.h"
#include "EntitySet.h"
#include "GePoly.h"
using namespace GlobalFunc;
ADSGePoint3d::~ADSGePoint3d()
{
}
bool ADSGePoint3d::operator > (const ADSGePoint3d &pt) const
{
double dDis = x - pt.x;
if (dDis >g_Option::instance()._DIST_SNAP)
{
return true;
}
else if (dDis < -g_Option::instance()._DIST_SNAP)
{
return false;
}
else
{
dDis = y - pt.y;
if (dDis > g_Option::instance()._DIST_SNAP)
{
return true;
}
else if (dDis < -g_Option::instance()._DIST_SNAP)
{
return false;
}
else
{
return z-pt.z > g_Option::instance()._DIST_SNAP;
}
}
}
int ADSGePoint3d::Project(const GeLine* pLine, ads_point rp) const
{
//lfy 2010-08-31 投影方法 发现AcGe 没有原方案快
ASSERT(pLine != NULL);
return PointProjectToLine(*this, pLine->StartPoint(), pLine->EndPoint(), rp);
/*AcGeLine3d lineT(pLine->StartPoint().AsAcGePoint3d(),pLine->EndPoint().AsAcGePoint3d());
AcGePointOnCurve3d ptOnCrv;
lineT.getClosestPointTo(asPnt3d(*this),ptOnCrv);
asPnt3d(rp) = ptOnCrv.point3d();
return RTNORM;*/
}
int ADSGePoint3d::Project(const AcGeLine3d* pLine,ads_point rp) const
{
//lfy 2010-08-31 投影发现AcGe 没有原方案快
//ASSERT(pLine != NULL);
//return PointProjectToLine(*this, pLine->, pLine->EndPoint(), rp);
ASSERT(pLine != NULL);
AcGePointOnCurve3d ptOnCrv;
pLine->getClosestPointTo(asPnt3d(*this),ptOnCrv);
asPnt3d(rp) = ptOnCrv.point3d();
return RTNORM;
}
double ADSGePoint3d::Distance(const GeLine* pLine) const
{
//lfy 2010-9-20
ASSERT(pLine != NULL);
ads_real* pt0 = *this;
ads_real* pt1 = pLine->m_startPoint;
ads_real* pt2 = pLine->m_endPoint;
ads_point pt4;
struct STPlanePara plan;
TVector3D vt;
vt[0] = pt2[0] - pt1[0];
vt[1] = pt2[1] - pt1[1];
vt[2] = pt2[2] - pt1[2];
pt4[0]=pt0[0];
pt4[1]=pt0[1];
pt4[2]=pt0[2];
plan.A = vt[0];
plan.B = vt[1];
plan.C = vt[2];
plan.D = 0.0 - (vt[0] * pt0[0] + vt[1] * pt0[1] + vt[2] * pt0[2]);
double l, m, n, t;
double mid_var1, mid_var2;
l = pt2[0] - pt1[0];
m = pt2[1] - pt1[1];
n = pt2[2] - pt1[2];
mid_var1 = l * (plan.A) + m * (plan.B) + n * (plan.C);
if (fabs(mid_var1) < 0.000001)
{
return 0.0;
}
mid_var2 = (plan.D) + pt1[0] * (plan.A) + pt1[1] * (plan.B) + pt1[2] * (plan.C);
t = 0.0 - mid_var2 / mid_var1;
pt4[0] = pt1[0] + l * t;
pt4[1] = pt1[1] + m * t;
pt4[2] = pt1[2] + n * t;
return ads_distance(pt0, pt4);
}
int ADSGePoint3d::Modify(const EntitySet* pEntity) const
{
EntitySet ent = *pEntity;
ent.SetData(10, &x);
return ent.Modify();
}
int ADSGePoint3d::MakeEntity(const EntitySet* pEntity) const
{
EntitySet ent = *pEntity;
RBGroups g(_T("*"));
RBList ebuf = ads_entgetx(ent, (resbuf *)&g);
ebuf.SetData(10, &x);
return ads_entmake(ebuf);
}
int ADSGePoint3d::Project(const GeCircle* pCir, ads_point rp) const
{
//lfy 2010-08-31
ASSERT(pCir != NULL);
GeLine aLine(ADSGePoint3d(x, y, pCir->m_ptCenter.z), pCir->m_ptCenter);
ADSGePoint3d pt1, pt2;
if (aLine.Intersection(pCir, pt1, pt2) == 2)
{
if (ads_distance(&x, pt1) < ads_distance(&x, pt2))
{
pt1 > rp;
}
else
{
pt2 > rp;
}
return RTNORM;
}
return RTERROR;
}
ADSGePoint3d ADSGePoint3d::Project(const GePlane* pPlan) const
{
ASSERT(pPlan != NULL);
ADSGePoint3d ptEnd = *this + TVector3D(pPlan->GetParaA(), pPlan->GetParaB(), pPlan->GetParaC());
GeLine ln(*this, ptEnd);
ADSGePoint3d ptRet;
VERIFY(pPlan->Intersect(ln, ptRet) == TRUE);
return ptRet;
}
void ADSGePoint3d::GetRotationMatrix(ads_real dRot, ads_matrix mat) const
{
ads_matrix matRot =
{
{cos(dRot), -sin(dRot), 0, 0},
{sin(dRot), cos(dRot), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
if (*this == ADSGePoint3d())
{
memcpy(mat, matRot, sizeof(matRot));
}
else
{
ads_matrix matMov =
{
{1, 0, 0, -x},
{0, 1, 0, -y},
{0, 0, 1, -z},
{0, 0, 0, 1}
};
ads_matrix matMov1 =
{
{1, 0, 0, x},
{0, 1, 0, y},
{0, 0, 1, z},
{0, 0, 0, 1}
};
MatrixMultiply(mat, matMov1, matRot, matMov, NULL);
}
}
void ADSGePoint3d::GetScaleMatrix(ads_real nScale, ads_matrix mat) const
{
ads_matrix matScale =
{
{nScale, 0, 0, 0},
{0, nScale, 0, 0},
{0, 0, nScale, 0},
{0, 0, 0, 1}
};
if (*this == ADSGePoint3d())
{
memcpy(mat, matScale, sizeof(ads_matrix));
}
else
{
ads_matrix matMov =
{
{1, 0, 0, -x},
{0, 1, 0, -y},
{0, 0, 1, -z},
{0, 0, 0, 1}
};
ads_matrix matMov1 =
{
{1, 0, 0, x},
{0, 1, 0, y},
{0, 0, 1, z},
{0, 0, 0, 1}
};
MatrixMultiply(mat, matMov1, matScale, matMov, NULL);
}
}
void ADSGePoint3d::GetScaleMatrix(ads_real sx, ads_real sy, ads_real sz, ads_matrix mat) const
{
ads_matrix matScale =
{
{sx, 0, 0, 0},
{0, sy, 0, 0},
{0, 0, sz, 0},
{0, 0, 0, 1}
};
if (*this == ADSGePoint3d())
{
memcpy(mat, matScale, sizeof(ads_matrix));
}
else
{
ads_matrix matMov =
{
{1, 0, 0, -x},
{0, 1, 0, -y},
{0, 0, 1, -z},
{0, 0, 0, 1}
};
ads_matrix matMov1 =
{
{1, 0, 0, x},
{0, 1, 0, y},
{0, 0, 1, z},
{0, 0, 0, 1}
};
MatrixMultiply(mat, matMov1, matScale, matMov, NULL);
}
}
void ADSGePoint3d::GetMoveMatrix(const ADSGePoint3d &ptDest, ads_matrix mat) const
{
TVector3D vt = ptDest - (*this);
ads_matrix matMove =
{
{1, 0, 0, vt.x},
{0, 1, 0, vt.y},
{0, 0, 1, vt.z},
{0, 0, 0, 1}
};
memcpy(mat, matMove, sizeof(ads_matrix));
}
int ADSGePoint3d::Make(const TCHAR *pszLay) const
{
TCHAR szLayer[80] = _T("");
if (pszLay)
{
_tcscpy(szLayer, pszLay);
}
else
{
TGGetVar(_T("clayer"), szLayer);
}
return GlobalFunc::MakeEntity(RTDXF0, _T("POINT"), 8, szLayer, 10, &x, NULL);
}
ADSGePoint3d::ADSGePoint3d(ads_real dx, ads_real dy, ads_real dz)
{
x = dx;
y = dy;
z = dz;
}
ADSGePoint3d::ADSGePoint3d(const ads_point src)
{
x = src[0];
y = src[1];
z = src[2];
}
ADSGePoint3d::ADSGePoint3d(const ADSGePoint3d &ptFrom, double dDrt, double dDist)
{
ads_polar(ptFrom, dDrt, dDist, *this);
}
ADSGePoint3d::ADSGePoint3d(const AcGePoint3d &src)
{
x = src[0];
y = src[1];
z = src[2];
}
ADSGePoint3d::ADSGePoint3d(const AcGePoint2d &src)
{
x = src[0];
y = src[1];
z = 0.0;
}
ADSGePoint3d& ADSGePoint3d::operator = (const AcGePoint3d &src)
{
x = src[0];
y = src[1];
z = src[2];
return *this;
}
ADSGePoint3d& ADSGePoint3d::operator = (const AcGePoint2d &src)
{
x = src[0];
y = src[1];
z = 0.0;
return *this;
}
ADSGePoint3d::ADSGePoint3d(const AcGeVector3d &src)
{
x = src[0];
y = src[1];
z = src[2];
}
ADSGePoint3d::ADSGePoint3d(const AcGeVector2d &src)
{
x = src[0];
y = src[1];
z = 0.0;
}
ADSGePoint3d& ADSGePoint3d::operator = (const AcGeVector3d &src)
{
x = src[0];
y = src[1];
z = src[2];
return *this;
}
ADSGePoint3d& ADSGePoint3d::operator = (const AcGeVector2d &src)
{
x = src[0];
y = src[1];
z = 0.0;
return *this;
}
ADSGePoint3d& ADSGePoint3d::operator = (const ADSGePoint3d & pt)
{
x = pt.x;
y = pt.y;
z = pt.z;
return *this;
}
ADSGePoint3d& ADSGePoint3d::operator = (const ads_point pt)
{
x = pt[0];
y = pt[1];
z = pt[2];
return *this;
}
void ADSGePoint3d::operator > (ads_point pt) const
{
pt[0] = x;
pt[1] = y;
pt[2] = z;
}
void ADSGePoint3d::operator += (const ADSGePoint3d &pt)
{
x += pt.x;
y += pt.y;
z += pt.z;
}
void ADSGePoint3d::operator -= (const ADSGePoint3d &pt)
{
x -= pt.x;
y -= pt.y;
z -= pt.z;
}
void ADSGePoint3d::operator *= (double dScale)
{
x *= dScale;
y *= dScale;
z *= dScale;
}
void ADSGePoint3d::operator /= (double dScale)
{
x /= dScale;
y /= dScale;
z /= dScale;
}
bool ADSGePoint3d::operator == (const ADSGePoint3d & pt) const
{
return (fabs(x - pt.x) < g_Option::instance()._DIST_SNAP && fabs(y - pt.y) < g_Option::instance()._DIST_SNAP
&& fabs(z - pt.z) < g_Option::instance()._DIST_SNAP);
}
bool ADSGePoint3d::operator != (const ADSGePoint3d &pt) const
{
return !((*this) == pt);
}
bool ADSGePoint3d::operator == (const ads_point pt) const
{
return (ads_distance(&x, pt) < g_Option::instance()._DIST_SNAP);
}
bool ADSGePoint3d::operator < (const ADSGePoint3d &pt) const
{
return pt > (*this);
}
ADSGePoint3d::operator ads_real*() const
{
return (ads_real *)&x;
}
ADSGePoint3d::operator void* () const
{
return (void *)&x;
}
ads_real& ADSGePoint3d::operator [] (int i)
{
return *(&x + i);
}
ads_real ADSGePoint3d::operator [] (int i) const
{
return *(&x + i);
}
int ADSGePoint3d::operator !() const
{
return (fabs(x) < 1E-6 && fabs(y) < 1E-6 && fabs(z) < 1E-6) ? 1 : 0;
}
double ADSGePoint3d::Distance(const ADSGePoint3d& pt) const
{
double t1 = pt.x - x;
double t2 = pt.y - y;
double t3 = pt.z - z;
return sqrt(t1 * t1 + t2 * t2 + t3 * t3);
}
double ADSGePoint3d::Distance2d(const ADSGePoint3d& pt) const
{
double XDis = x - pt.x;
double YDis = y - pt.y;
return sqrt(XDis * XDis + YDis * YDis);
}
void ADSGePoint3d::TransformBy(const AcGeMatrix3d &mat)
{
((AcGePoint3d *)this)->transformBy(mat);
}
void ADSGePoint3d::PointTransformBy(ads_matrix xform, ads_point pt, ads_point newpt) const
{
int i, j;
ads_point pt0;
newpt[X] = newpt[Y] = newpt[Z] = 0.0;
Cpoint(pt0, pt);
for (i = X; i <= Z; i++)
{
for (j = X; j <= Z; j++)
{
newpt[i] += xform[i][j] * pt0[j];
}
newpt[i] += xform[i][T];
}
}
ADSGePoint3d ADSGePoint3d::TransformAs(ads_matrix mat) const
{
ADSGePoint3d pt;
PointTransformBy(mat, (double *)&x, pt);
return pt;
}
void ADSGePoint3d::TransformBy(ads_matrix mat)
{
ADSGePoint3d pt;
PointTransformBy(mat, (double *)&x, pt);
*this = pt;
}
void ADSGePoint3d::Mirror(const ads_point src, ads_point dest) const
{
dest[0] = 2 * x - src[0];
dest[1] = 2 * y - src[1];
dest[2] = 2 * z - src[2];
}
void ADSGePoint3d::Mirror(const ADSGePoint3d & src, ADSGePoint3d &dest) const
{
dest.x = 2 * x - src.x;
dest.y = 2 * y - src.y;
dest.z = 2 * z - src.z;
}
void ADSGePoint3d::Rotate(ads_real dang)
{
ads_real dewX = x * cos(dang) - y * sin(dang);
ads_real dewY = x * sin(dang) + y * cos(dang);
x = dewX;
y = dewY;
}
void ADSGePoint3d::Reverse()
{
x = -x;
y = -y;
z = -z;
}
ads_real ADSGePoint3d::Length() const
{
return sqrt(x * x + y * y + z * z);
}
void ADSGePoint3d::Scale(ads_real dScale)
{
x *= dScale;
y *= dScale;
z *= dScale;
}
void ADSGePoint3d::Normalize()
{
double nLen = Length();
if (nLen > 1.0E-9)
{
Scale(1.0 / nLen);
}
}
ADSGePoint3d::operator const AcGePoint3d & ()
{
return *(const AcGePoint3d *)this;
}
ADSGePoint3d::operator AcGePoint3d & () const
{
return *(AcGePoint3d *)this;
}
AcGePoint3d ADSGePoint3d::AsAcGePoint3d() const
{
return AcGePoint3d(x, y, z);
}
ADSGePoint3d::operator const AcGeVector3d& ()
{
return *(const AcGeVector3d *)this;
}
ADSGePoint3d::operator AcGeVector3d& () const
{
return *(AcGeVector3d *)this;
}
AcGeVector3d ADSGePoint3d::AsAcGeVector3d() const
{
return AcGeVector3d(x, y, z);
}
ADSGePoint3d::operator const AcGePoint2d& ()
{
return *(const AcGePoint2d *)this;
}
ADSGePoint3d::operator AcGePoint2d& () const
{
return *(AcGePoint2d *)this;
}
AcGePoint2d ADSGePoint3d::AsAcGePoint2d() const
{
return AcGePoint2d(x, y);
}
ADSGePoint3d::operator const AcGeVector2d& ()
{
return *(const AcGeVector2d *)this;
}
ADSGePoint3d::operator AcGeVector2d& () const
{
return *(AcGeVector2d *)this;
}
AcGeVector2d ADSGePoint3d::AsAcGeVector2d() const
{
return AcGeVector2d(x, y);
}
double ADSGePoint3d::Distance(const ADSGePoint3d& ptSta, const ADSGePoint3d& ptEnd) const
{
ads_real* pt0 = *this;
ads_real* pt1 = ptSta;
ads_real* pt2 = ptEnd;
ads_point pt4;
struct STPlanePara plan;
TVector3D vt;
vt[0] = pt2[0] - pt1[0];
vt[1] = pt2[1] - pt1[1];
vt[2] = pt2[2] - pt1[2];
pt4[0] = pt0[0];
pt4[1] = pt0[1];
pt4[2] = pt0[2];
plan.A = vt[0];
plan.B = vt[1];
plan.C = vt[2];
plan.D = 0.0 - (vt[0] * pt0[0] + vt[1] * pt0[1] + vt[2] * pt0[2]);
double l, m, n, t;
double mid_var1, mid_var2;
l = pt2[0] - pt1[0];
m = pt2[1] - pt1[1];
n = pt2[2] - pt1[2];
mid_var1 = l * (plan.A) + m * (plan.B) + n * (plan.C);
if (fabs(mid_var1) < 0.000001)
{
return 0.0;
}
mid_var2 = (plan.D) + pt1[0] * (plan.A) + pt1[1] * (plan.B) + pt1[2] * (plan.C);
t = 0.0 - mid_var2 / mid_var1;
pt4[0] = pt1[0] + l * t;
pt4[1] = pt1[1] + m * t;
pt4[2] = pt1[2] + n * t;
double dist = ads_distance(pt0, pt4);
return dist;
}