|
/// <summary> Class providing general mathimatical functions. </summary>
class PointSorter
{
#pragma region " Constructor "
public:
PointSorter(const AcGePoint3d& anchor, AcGePoint3dArray& points)
{
mAnchor = anchor;
AcGePoint3d* arr = points.asArrayPtr();
auto size = points.length() / sizeof(AcGePoint3d);
std::sort(arr, arr + size, [this](const AcGePoint3d& l, const AcGePoint3d& r) { return doCompare(l, r); });
}
PointSorter(const AcGeVector3d& axis, const AcGePoint3d& anchor, AcGePoint3dArray& points)
{
mAxis = axis;
mAnchor = anchor;
AcGePoint3d* arr = points.asArrayPtr();
auto size = points.length() / sizeof(AcGePoint3d);
std::sort(arr, arr + size, [this](const AcGePoint3d& l, const AcGePoint3d& r) { return doCompareAlongAxis(l, r); });
}
#pragma endregion
#pragma region " Private Methods "
private:
int doCompare(const AcGePoint3d& pt1, const AcGePoint3d& pt2)
{
auto vecA = pt1 - mAnchor;
auto vecB = pt2 - mAnchor;
return MathUtilities::Compare(vecA.length(), vecB.length());
}
int doCompareAlongAxis(const AcGePoint3d& pt1, const AcGePoint3d& pt2)
{
auto vecA = pt1 - mAnchor;
auto vecB = pt2 - mAnchor;
if (vecA.isCodirectionalTo(vecB))
return MathUtilities::Compare(vecA.length(), vecB.length());
else if (vecA.isCodirectionalTo(mAxis))
return 1;
return -1;
}
#pragma endregion
#pragma region " Members "
private:
AcGeVector3d mAxis;
AcGePoint3d mAnchor;
#pragma endregion
};
Assuming that your MathUtilities::Compare(l,r) returns -1 if l<r and +1 if l>r
PointSorter(const AcGePoint3d& anchor, AcGePoint3dArray& points)
{
mAnchor = anchor;
AcGePoint3d* arr = points.asArrayPtr();
//auto size = points.length() / sizeof(AcGePoint3d); //WRONG!
auto size = points.length();
std::sort(arr, arr + size,
[this](const AcGePoint3d& l, const AcGePoint3d& r)
-> bool { return (doCompare(l, r) < 0); } // true if l<r, false if l>=r
);
}
PointSorter(const AcGeVector3d& axis, const AcGePoint3d& anchor, AcGePoint3dArray& points)
{
mAxis = axis;
mAnchor = anchor;
AcGePoint3d* arr = points.asArrayPtr();
// auto size = points.length() / sizeof(AcGePoint3d); //WRONG!
auto size = points.length();
std::sort(arr, arr + size,
[this](const AcGePoint3d& l, const AcGePoint3d& r)
-> bool { return (doCompareAlongAxis(l, r) < 0); } // true if l<r, false if l>=r
);
} |
|