Given two ON_3dPoint2, how can I determine if they (within the error range) are the same point?
You mean, if the distance between them is smaller than a given tolerance?
Its usually called „EpsilonEquals“, but implementing that on your own is not hard either as you pointed out. Distance smaller than tolerance, although there are some slight variations out there.
yes, It is not hard to implementing.
I have used eigen before, which supports determining whether two vectors are similar by its internal Epsilon.
Eigen::Vector3d v1,v2;
if(v1.isApprox(v2)){
// do something
}
So I wonder if Rhino has a similar method?
As I know, there are different types of tolerance in Rhino, so I don’t know which one is more suitable for me.
in Rhinocommon - yes - search for EpsilonEquals in the docs…
https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.arc/epsilonequals?search=EpsilonEquals
most basic geometry offers the searched functionality.
are you using opennurbs ? rhinocommon ? c++ or c# ?
Hi @Liu2 ,
There are many ways to reach your goal
Simplest solution would be
def points_are_equal(pt1, pt2, tolerance):
distance = pt1.DistanceTo(pt2)
return distance <= tolerance
Or you could consider just each axis by itself
def points_are_equal(pt1, pt2, tolerance):
def almost_equal(a, b, tol):
return abs(a - b) <= tol
x_equal = almost_equal(pt1.X, pt2.X, tolerance)
y_equal = almost_equal(pt1.Y, pt2.Y, tolerance)
z_equal = almost_equal(pt1.Z, pt2.Z, tolerance)
return x_equal and y_equal and z_equal
The most accurate method would be the first.
Hope this helps,
Farouk
This is really simple…
public static bool EpsilonEquals(Point3d p1, Point3d p2, double epsilon = 0.00001)
{
return Math.Abs(p1.X - p2.X) <= epsilon &&
Math.Abs(p1.Y - p2.Y) <= epsilon &&
Math.Abs(p1.Z - p2.Z) <= epsilon;
}
Thank you for your reply!
I am using opennurbs in c++.
What I am most concerned about is not how to implement this method, but how to use the correct tolerance.
I want to make sure my tolerance is same to Rhino default.
This is my current code, and I have no idea which tolerance should I use.
bool isApprox(const ON_3dPoint& p1, const ON_3dPoint& p2, int epsilon=ON_EPSILON){
if(p1.DistanceTo(p2)<epsilon)
return true;
return false;
}
my guess:
ON_ZERO_TOLERANCE
?
from the documentation:
…
In cases when an absolute “zero” tolerance / is required to compare model space coordinates,
…
Note if you use DistanceTo you compute the full distance every-time. If you choose my/Farouks approach by using the Math.Abs, your computation will be more efficient, because it will return false if the x is already out of bounds, before even computing the y and z distance. On top you don’t need the if clause. Just return the statement in the if, otherwise you branch the code for no reason.
There is no one correct tolerance. You must decide, based on the problem you are trying to solve, what tolerance is sufficient.
– Dale