一、概述
最近学习如何判断两根曲线是否连接(不是相交,两条直线有一个端点重合),网上说到两种方法,一种是第一种方法,UF_MODL_ask_minimum_dist_3函数判断两个对象的距离,测得的距离等于零,就是接触的(当然可能是相连或者相交,根据实际情况使用)。第二种方法,通过UF_CURVE_ask_spline_data函数得到曲线两端的端点坐标,比对端点坐标,如果相同,则相互连接。
二、代码分析
1、UF_MODL_ask_minimum_dist_3判断两个对象的距离
代码解析:
UF_initialize();
//创建两条直线
UF_CURVE_line_t Line1;
Line1.start_point[0] = 0.0;
Line1.start_point[1] = 0.0;
Line1.start_point[2] = 0.0;
Line1.end_point[0] = 100.0;
Line1.end_point[1] = 100.0;
Line1.end_point[2] = 0.0;
tag_t Line1TAG = NULL_TAG;
UF_CURVE_create_line(&Line1, &Line1TAG);
UF_CURVE_line_t Line2;
Line2.start_point[0] = 100.0;
Line2.start_point[1] = 80.0;
/*Line2.start_point[2] = 0.0;*/
Line2.start_point[2] = 100.0;
Line2.end_point[0] = 30.0;
Line2.end_point[1] = 100.0;
Line2.end_point[2] = 0.0;
tag_t Line2TAG = NULL_TAG;
UF_CURVE_create_line(&Line2, &Line2TAG);
//判断两个对象的最短距离函数
tag_t tagObj1 = Line1TAG;
tag_t tagObj2 = Line2TAG; //为空
int iGuess2Given = 1; // 为1
double douGuess2[3]={ 0.0,0.0,0.0 };//设置点坐标
double douDis = 0.0;
double douPointOnObj1[3] = { 0 };
double douPointOnObj2[3] = { 0 };
double douAccuracy = 0.0;
UF_MODL_ask_minimum_dist_3(2, tagObj1, tagObj2, 0, NULL, 0, NULL, &douDis, douPointOnObj1, douPointOnObj2, &douAccuracy);
char msg1[256], msg2[256], msg3[256];
sprintf(msg1, "两个点最近的坐标,对象1:%f,%f,%f\n", douPointOnObj1[0], douPointOnObj1[1], douPointOnObj1[2]);
sprintf(msg2, "两个点最近的坐标,对象2:%f,%f,%f\n", douPointOnObj2[0], douPointOnObj2[1], douPointOnObj2[2]);
sprintf(msg3, "最小距离:%f\n", douDis);
print(msg1);
print(msg2);
print(msg3);
UF_terminate();
2、UF_CURVE_ask_spline_data判断是否存在两个端点相等
struct CurveData
{
Point3d start;
Point3d end;
tag_t curve_tag;
// 有参数构造函数
CurveData(const Point3d& startPoint, const Point3d& endPoint, const tag_t& CurveTag)
: start(startPoint), end(endPoint), curve_tag(CurveTag)
{
}
};
//获取曲线的端点
CurveData getVerOfCurve(tag_t curveTag)
{
double point[3] = { 0 };
double target[3] = { 0 };
double pnorm[3] = { 0 };
double bnorm[3] = { 0 };
double a, b;
double sta = 0;
double end = 1;
double stratP[3] = { 0 };
double endP[3] = { 0 };
UF_MODL_ask_curve_props(curveTag, sta, stratP, target, pnorm, bnorm, &a, &b);
UF_MODL_ask_curve_props(curveTag, end, endP, target, pnorm, bnorm, &a, &b);
return CurveData(Point3d(stratP[0], stratP[1], stratP[2]), Point3d(endP[0], endP[1], endP[2]), curveTag);
}
// 检查两个点是否相同
bool IsSamePoint(const Point3d& a, const Point3d& b)
{
// 可以根据实际情况调整精度
const double tolerance = 1e-6;
return (std::abs(a.X - b.X) < tolerance && std::abs(a.Y - b.Y) < tolerance && std::abs(a.Z - b.Z) < tolerance);
}
//判断曲线1和2是否相连
bool areLinesConnected(const CurveData& Curve1, const CurveData& Curve2)
{
if (IsSamePoint(Curve1.end, Curve2.start))
{
// 当前线段的终点与另一线段的起点相连记录
return true;
}
else if (IsSamePoint(Curve1.end, Curve2.end))
{
return true;
}
else
{
return false;
}
}
void MyClass::do_it()
{
// TODO: add your code here
UF_initialize();
//创建两条直线
UF_CURVE_line_t Line1;
Line1.start_point[0] = 0.0;
Line1.start_point[1] = 0.0;
Line1.start_point[2] = 0.0;
Line1.end_point[0] = 100.0;
Line1.end_point[1] = 100.0;
Line1.end_point[2] = 0.0;
tag_t Line1TAG = NULL_TAG;
UF_CURVE_create_line(&Line1, &Line1TAG);
UF_CURVE_line_t Line2;
Line2.start_point[0] = 100.0;
Line2.start_point[1] = 100.0;
Line2.start_point[2] = 0.0;
Line2.end_point[0] = 30.0;
Line2.end_point[1] = 100.0;
Line2.end_point[2] = 0.0;
tag_t Line2TAG = NULL_TAG;
UF_CURVE_create_line(&Line2, &Line2TAG);
CurveData thecurvedate1 = getVerOfCurve(Line1TAG);
CurveData thecurvedate2 = getVerOfCurve(Line2TAG);
bool IsCurveConnected = areLinesConnected(thecurvedate1, thecurvedate2);
if (IsCurveConnected==true)
{
uc1601("相连",1);
}
else
{
uc1601("不相连", 1);
}
UF_terminate();
}
三、拓展
可以通过这两个函数UF_CURVE_ask_spline_data判断是否相连,然后通过UF_MODL_ask_minimum_dist_3判断是否相交或者相离。
//判断两条曲线相交、将离,返会0为相交,返回1为相离
int IsCurveIntersectOrSeparation(tag_t curve1Tag, tag_t curve2Tag)
{
//判断两个对象的最短距离函数
tag_t tagObj1 = curve1Tag;
tag_t tagObj2 = curve2Tag;
double douGuess2[3] = { 0.0,0.0,0.0 };//设置点坐标
double douDis = 0.0;
double douPointOnObj1[3] = { 0 };
double douPointOnObj2[3] = { 0 };
double douAccuracy = 0.0;
UF_MODL_ask_minimum_dist_3(2, tagObj1, tagObj2, 0, NULL, 0, NULL, &douDis, douPointOnObj1, douPointOnObj2, &douAccuracy);
if (douDis!=0)
{
return 1;
}
return 0;
}
void MyClass::do_it()
{
// TODO: add your code here
UF_initialize();
//创建两条直线
UF_CURVE_line_t Line1;
Line1.start_point[0] = 0.0;
Line1.start_point[1] = 0.0;
Line1.start_point[2] = 0.0;
Line1.end_point[0] = 100.0;
Line1.end_point[1] = 100.0;
Line1.end_point[2] = 0.0;
tag_t Line1TAG = NULL_TAG;
UF_CURVE_create_line(&Line1, &Line1TAG);
UF_CURVE_line_t Line2;
Line2.start_point[0] = 90.0;
Line2.start_point[1] = 90.0;
Line2.start_point[2] = 0.0;
Line2.end_point[0] = 30.0;
Line2.end_point[1] = 100.0;
Line2.end_point[2] = 0.0;
tag_t Line2TAG = NULL_TAG;
UF_CURVE_create_line(&Line2, &Line2TAG);
CurveData thecurvedate1 = getVerOfCurve(Line1TAG);
CurveData thecurvedate2 = getVerOfCurve(Line2TAG);
bool theIsCurveConnected = areLinesConnected(thecurvedate1, thecurvedate2);
if (theIsCurveConnected==true)
{
uc1601("相连",1);
}
else
{
int theIsCurInterOrSep = IsCurveIntersectOrSeparation(Line1TAG, Line2TAG);
if (theIsCurInterOrSep==1)
{
uc1601("不相交", 1);
}
else if (theIsCurInterOrSep == 0)
{
uc1601("相交", 1);
}
}
UF_terminate();
}
NX二次开发是枯燥的,但同时又是有趣的,就像我,同一份代码,不同的时刻编写的不一样,还记得刚刚开始时,所有代码都是写到一起的,但是后来慢慢开始定义函数,再后来定义结构体,使用迭代器等等,现在也正在封装自己的函数库,方便今后的直接调用。如果你坚持不下去时请多看看我的文章,最近事情比较多,但是一直抽时间写博客。把自己最近学的东西做以下记录,方便他人的同时,更是对自己生活的记录。