Measure of distances in VBA

Hi everybody again ,

I have a doubt that may be anybody can help me:

When I have two elements connected between them vertex to vertex or vertex to any point of the another line , the distance must be = 0 , but I see that if I use the equation of the line like using the method ComputeMinimumDistance of the BsplineCurve , it returns me values more bigger than 0 , like  exponent -4 or similars in any cases.

Usually I obtain , when two elements are connect , values like Exp -12 or similar , but in any cases like I say of -4 aprox.

Anybody knows if there is any solution to it ,  like configure the work units or similar ???

If more information is needed or I have no explained me correctly , please let me know and I will explain better.

Thanks for your help in advanced.

Regards !!

  • Unknown said:
    When I have two elements connected between them vertex to vertex or vertex to any point of the another line , the distance must be = 0

    MicroStation 7 (MicroStation/J and earlier) used the IGDS 32-bit integer coordinate system.  With integer coordinates it was possible for a distance to be exactly zero.

    MicroStation 8 uses floating-point coordinates.  Any floating-point computation is prone to arithmetic errors due to approximations in the computer representation of real numbers.  Most computers comply with IEEE standard 754.  For further information refer to any text book that covers computational theory.

    Unknown said:
    Usually I obtain values like Exp -12

    A common term for a minimum acceptable error is epsilon (Greek ε).  That is, if the absolute value of a computation is less than ε then the result is considered to be zero.  The value of ε is application-dependent, but for practical purposes in CAD 10-12 is small enough.  Probably 10-8 is small enough for most uses.

    Unknown said:
    in any some cases like I say of -4

    Unfortunately a B-spline can only approximate a straight line.  You're probably seeing that large error because of limitations in the B-spline maths.  If precision is important to you, then you're going to have to invest in learning geometric algebra in order to write your own algorithms to achieve what you want.

     
    Regards, Jon Summers
    LA Solutions

  • Thanks Joan , always is so apreciated your help ...

    Really I have wrote my own algorithm aplying equation of a line projecting the point to a perpendicular distance to the line to calculate the distance minimum between a point and a line , and it returns me the same value that the function of B spline maths ...

    Thanks for your help Jon , I will investigate it ....
  • Hi Xavi García,

    Jon wrote the most important facts I think, so just a few more comments:

    To understand what floating point number are, how they are implemented and what risks and threats exist there is mandatory knowledge for any CAD computation in my opinion. No deep detail experience is rewquired, just to understand basics and accept the most imporant things (like "test numbers equality is nonse ;-) and to know best practices. Wiki page mentioned by Jon is good start, but you can find plenty of articles and blogs on Internet (e.g. this one).

    Unknown said:
    When I have two elements connected between them vertex to vertex or vertex to any point of the another line , the distance must be = 0

    No, no and once more no ;-) ... well, in theory on paper the distance is zero, but the core question is how it's implemented in a particular software plus how it's maintained (or crippled) by API. And because no floating point number is exact, is e.g. 1E-10 still valid value or it's zero? If your master units are meters, if working in GIS and cadastre, anything under 1 mm is "zero" because it's below precision how data are captured. So 1E-4 (0.1 mm) can be always evaluated as 0 ... but it's not how simple API works.

    Unknown said:
    like configure the work units or similar

    From general perspective, if any code depends on units configuration, it's bad and fragile code.

    If you work with coordinates comming from differenct source (e.g. two elements), you should not use "simple math methods" like Point2dEqual, but Point2dEqualTolerance method, because it allows to define what is "below reasonable precision". Or you can implement own methods.

    BTW It can be valuable to check some API or solution where precision and difference between math and implementation is important. I am talking about topology and GIS libraries, because any topology is always calculated with "what is enough precision" in mind and it's reflected in API also.

    Unknown said:
     ComputeMinimumDistance of the BsplineCurve

    As Jon wrote, BSpline is calculated element and it cannot be treated in the same way as e.g. line or line string.

    With regards,

    Jan

  • OK Jon , I understand for what you say that a distance of 0.1mm must be considered 0 for microstation ??? I supose that a value of a exponent -10 or -12 is considered like a 0 , but a -4 ....

    Like I say , I have implemented my own method to calculate the distance , aplying the projection perpendicular of a point over a line , and it returns me the same value than methods like computeMinimumdistance of Bsplit , I must understand that I will not obtain a better precision of the connection ???.

    Thanks so much for your help Jan !!!
  • Unknown said:
    that a distance of 0.1mm must be considered 0 for microstation ???

    Not for MicroStation. MicroStation is not involved in such evaluation! MicroStation can be treated as "element storage" and it's defined that element coordinates (like in other CAD systems) are stored as floating decimal point numbers accordingly to IEEE standard. It's completely your responsibility to define what precision is critical for you (but it cannot be better than precision ensured by the storage itself). Every tool in MicroStation does the same: It works based on the precision specification ... e.g. element placemetn is not critical, but 3D modeling using Parasolid require some extra precision settings.

    Unknown said:
    I supose that a value of a exponent -10 or -12 is considered like a 0 , but a -4

    Again, it's you responsibility to research precision condition. Don't suppose, do serious math evaluation: What are your coordinates (in floating point math numbers close to zero have higher precision than big values numbers), what is required precision, with what precision you API works...

    Unknown said:
    I must understand that I will not obtain a better precision of the connection

    What is better precision? It sounds to me like magic things or nonsense form math point of view:

    • If there is line endpoint, I can probably easily make assignment like end-point-line-A = end-point-line-B, because it's only about to copy values from one structure to another.
    • But if there is any more complex element (arc, curve...), the coordinates are "not free" because of element constrains ... arc and circle has to have the same distance from center point, curve is calculated somehow, so it's not possible to do simple "structure copy" line-endpoint to curve-endpoint.
    • Any coordinate based on calculation like snap to curve, project to element etc. is always calcualted with limited precision.

    At the end, to work with B-splines required pretty good konwledge of B-splines math itself and also at least basics of precision concept in path extended by floating point implementation issues knowledge (and VBA limitations knowledge in this case also). And maybe it's the reson why so many software (GIS / topology / analysis, modeling and visualization etc.) work with linear geometry only.

    Regards,

      Jan

  • Unknown said:
    I have written my own algorithm apllying equation of a line projecting the point to a perpendicular distance to the line

    Before you rush into your own algorithm design, look carefully at the API.  VBA provides the Ray3d and Segment3d UDTs1, and a number of methods that work with those...

    • Ray3dClosestPoint...
    • Ray3dFrom...
    • Ray3dRay3dClosest...
    • Ray3dRay3dIntersectXY
    • Segment3dClosestPoint...
    • Segment3dFrom...
    • Segment3dSegment3dClosest...

    Note 1: UDT = User Defined Type

     
    Regards, Jon Summers
    LA Solutions

  • It's not just MicroStation. I do development for AutoCAD Civil 3D as well as MicroStation and have the same kinds of issues with floating point numbers in both. It doesn't matter if it's a calculation along a b-spline in MicroStation or distance from a Survey Point to an Alignment in Civil 3D.

    I wrote a VBA tip a few years ago on one way to handle it.
    envisioncad.com/.../comparing-floating-point-numbers

    Rod Wing
    Senior Systems Analyst

  • Unknown said:
    I wrote a VBA tip a few years ago on one way to handle it

    Good tip!

    MicroStation VBA has functions that do something similar for the UDTs.  For example...

    • Point2dEqualTolerance (p1, p2, tolerance)
    • Point3dEqualTolerance (p1, p2, tolerance)
    • Vector3dEqualTolerance (v1, v2, tolerance)
    • BsplineCurve.ComputeMinimumDistance (ClosestPoint, ClosestParameter, FromPoint, Rotation [, Tolerance])
    • Range3dEqualTolerance (Range0, Range1, Tolerance)
    • IntersectRay3d (IntersectionPoint, Parameters, Ray [, Tolerance])

     
    Regards, Jon Summers
    LA Solutions

  • Unknown said:

    RodWing
    I wrote a VBA tip a few years ago on one way to handle it

    Good tip!

    MicroStation VBA has functions that do something similar for the UDTs.  For example...

    • Point2dEqualTolerance (p1, p2, tolerance)
    • Point3dEqualTolerance (p1, p2, tolerance)
    • Vector3dEqualTolerance (v1, v2, tolerance)
    • BsplineCurve.ComputeMinimumDistance (ClosestPoint, ClosestParameter, FromPoint, Rotation [, Tolerance])
    • Range3dEqualTolerance (Range0, Range1, Tolerance)
    • IntersectRay3d (IntersectionPoint, Parameters, Ray [, Tolerance])

    Thank you.

    Unfortunately the AutoCAD and Civil 3D API's lack a lot of those types of functions which means I have to develop my own.

    To help make modules more portable between AutoCAD and MicroStation I will use my own utility functions in MicroStation applications even if there is something comparable available in the MicroStation API.

    I wrote a tip on that too! :)

    http://envisioncad.com/tips/create-portable-functions/

    Rod Wing
    Senior Systems Analyst

  • Thank you very much , I think that I can use the function you have posted "IsPointOnLine" to determinate if a Point is connected to a line , but if the precision of the distance between point and line is the precision of the value of the variable Zero I'm with the same problem ...

    I'll try with it !!

    Thanks again for your help Jon , Jan and Rodwing !!!