Draw circle perpendicular to a line...

Hi,

I want to programatically draw a circle, at a given point along a line (or spline), so that the circle is perpendicular to the line at that point.

 

So, almost like extruding a shape along a linear element, where the shape (a planar shape) has to be placed perpendicular to the linear element.

 

Been looking what methods are available, but cant find one that makes any sense.

BTW - doing this in C# .net, so been looking at the VBA help.

Thanks!

Parents
  • Math basics or misunderstanding ?

    Each tangent of a circle is perpendicular to it's radius. There is no other given thing for a circle.

    So if your center(point) is outside the line, each circle that has a radius of the minimum distance between the center and the line is perpendicular (or almost not, because there are countless other tangents :) ).

    It might be a good idea to draw an example of what you mean.



  • Hi Michael,

    Misunderstanding i think. Sorry i didnt post a picture - an isometric view below:

    The blue line is the target line, in this case, simply a vertical line. The circle is now dead centre on the line, and it's radii are

    perpendicular to the line, at the insertion point. I know there is a term for the perpendicular vector, but it has escaped me.

     

    So, what i want is, for whatever modification i make to the blue line (move its start/end points), i want that circle to remain fixed as it is currently to the line.

     

     

     

  • Someone who's working in 3D :-)

    OK, that makes it clearer. I currently have some code in mdl on my computer in the company that aligns a normal (from polygon or similar) with a line. This should work here even.

    I'll be back on Monday, but you might need someone else for the C# part :)

    Michael



  • PlanarElement Interface

    The PlanarElement interface provides what you want. A circle is a closed element that supports that interface. PlanarElement.Normal yields a normal vector as a Point3d. Use methods that act on Point3d data to create a set of vertices, then create your LineElement from those vertices.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • John, if I understand your needs, you want to create that circle and not that line, so it think you want to look into example about frenet frame.

    Dan

  • Hi Dan, yep, thats exactly what i want to do. Im not too worried about the line, its just the circle or planar element.

    Frenet frame? MS example or should i google it?

  • Thanks Jon, I will take a look at PlanarElement, see if that offers any solutions.

  • johnds said:

    Hi Dan, yep, thats exactly what i want to do. Im not too worried about the line, its just the circle or planar element.

    Frenet frame? MS example or should i google it?

     

    You have a lucky day, I have found one of my examples from time when I wanted to be a MVBA teacher...

    I modified it little bit to create perp. circles around all traversable elements in selection...

    Select any linear elements, call CirclesAlongSelection and you will see what will happen.

    If you will need some help with converting from VBA to C#, ask it here...

    If you would want to avoid using of BSplines, let's say... There is another option to get frame. We can calculate it from tangent vector...

     

    Sub CirclesAlongSelection()

        Dim sel As ElementEnumerator
        Dim norm As Point3d
        Dim bspl As BsplineCurve
        Dim dst As Double, length As Double
        
        ' get selected elements into enumerator
        Set sel = ActiveModelReference.GetSelectedElements
        
        ' world's normal
        norm = Point3dFromXYZ(001)
        
        ' while any element in selection
        While sel.MoveNext
          ' only if it is convertable to BSpline
          If sel.Current.IsTraversableElement Then
            ' convert it to BSpline curve
            Set bspl = New BsplineCurve
            bspl.FromElement sel.Current
            
            ' compute length of curve
            length = bspl.ComputeCurveLength()
            
            ' place perpendicular cirlces along element
            ' step is 1 unit, radius is 3 units
            For dst = 0 To length Step 1
                Call DrawCircleAtDistance(bspl, 3, dst, norm)
            Next dst
            
          End If
        Wend
        
    End Sub

    Sub DrawCircleAtDistance(bspl As BsplineCurve, radius As Double, _
                             distance As Double, lNorm As Point3d)

        Dim pnt As Point3d
        Dim ff As Matrix3d
        Dim param As Double, curv As Double, tors As Double
        Dim ell As EllipseElement
        Dim ellPts(2As Point3d
        
        ' get point at distance and its parameter
        pnt = bspl.EvaluatePointAtDistance(param, distance)
        
        ' evaluate frenet frame at parameter
        ' frenetFrame stores in its rows the tangent (RowX),
        ' main normal (RowY) and binormal (RowZ) of the curve
        ' at the given parameter
        bspl.EvaluatePointFrame ff, curv, tors, param, lNorm
         
        ' calculate points of circle
        ellPts(0) = Point3dAddScaled(pnt, ff.RowY, radius)
        ellPts(1) = Point3dAddScaled(pnt, ff.RowZ, -radius)
        ellPts(2) = Point3dAddScaled(pnt, ff.RowY, -radius)
        
        ' create circle
        Set ell = CreateEllipseElement1(Nothing, ellPts(0), ellPts(1), ellPts(2))
         
        ' if circle is valid element, add it to design file
        If (Not ell Is NothingThen
            ActiveModelReference.AddElement ell
        End If

    End Sub
  • perfect Dan :)

    I would have went the other way, creating the circle and align it's normal with the given line. But that's because I have some routines to do so :)

    Your code will enable him to use it for more than lines and it should be fast enough, although the bspline calculation is somewhat overkill if he really only has lines.



  • Thanks :-)...

    In MDL it is also possible to use mdlElmdscr_pointAtDistance to get point and tangent. Then, we have a 2d world normal and tangent, so we can, using crossproduct, calculate normal. And if we have a normal and tangent, another crossproduct will give us binormal, so BSplines and frenet frame is not needed, but it was a nice example for MVBA training...

    Dan

  • Hi guys,

    Dan, your example was great, but unfortunately EvaluatePointFrame is only available on BSplines.

    I need to do the same thing on lines, circles, elipses, shapes, etc. And I need 3D coordinates.

    So, how would I, for example, draw that circles on, say, an ellipse, just like what you did using FrenetFrame on the spline?

  • John, have you tried to run that example on selection with ANY LINEAR ELEMENTS ( lines, circles, ellipses, shapes, etc. )?

    I can say, YOU HAVE NOT!!!

    That example perfectly works on any of element types you listed.

    If you look more close into that example, you will find that it handles all elements convertable to BSpline, not only BSplineElements.

    The logic is:

    1. Get any traversable (line, circle, ellipse, shape, etc.) element
    2. Convert it to BSplineCurve
    3. Get point on curve
    4. Get parameter for this point
    5. Get frenet frame for this parameter
    6. Calculate points of circle
    7. Draw circle

    Dan

Reply
  • John, have you tried to run that example on selection with ANY LINEAR ELEMENTS ( lines, circles, ellipses, shapes, etc. )?

    I can say, YOU HAVE NOT!!!

    That example perfectly works on any of element types you listed.

    If you look more close into that example, you will find that it handles all elements convertable to BSpline, not only BSplineElements.

    The logic is:

    1. Get any traversable (line, circle, ellipse, shape, etc.) element
    2. Convert it to BSplineCurve
    3. Get point on curve
    4. Get parameter for this point
    5. Get frenet frame for this parameter
    6. Calculate points of circle
    7. Draw circle

    Dan

Children
  • Hi Dan,

    Nope, i did not try using your sample on elements other than Bspline. I was not aware that you could convert other types to Bspline.

    So, what I did also was try to see if the other types had the same method on them, which they did'nt. Strange why MS did not provide such a method on the other types, rather than having to use the technique you showed in your example.

    I mean, will an ellipse or a shape, converted to a BSpline, give the same thing as the original??

    interesting.

    John.

  • From my vault, I just assembled this routine...

    Is roughly what you are looking for?

    Regards, Marcos

    CirclePerp.ma
  • B-Splines: everyman's curve

    Unknown said:
    Strange why MS did not provide such a method on the other types, rather than having to use the technique you showed in your example.

    Are you suggesting that a family of functions mdlLine_doSomething, mdlEllipse_doSomething, mdlShape_doSomething, mdlComplexShape_doSomething, etc. would be more digestible than mdlBSpline_doSomething? No — it's easier to convert everything to a super-curve that provides the geometric capability of each type of element plus the ability to be manipulated easily. To put it another way, B-Splines provide an elegant solution to complex problems.

    Unknown said:
    I mean, will an ellipse or a shape, converted to a BSpline, give the same thing as the original?

    One way is to try it, which you can do quite easily in MicroStation without writing any code.

    It's a bit like asking "If I treat a circle as an ellipse, can I be sure that it works the same?" Yes, because a circle is just a special case of an ellipse. Similarly, a B-Spline is a generalisation of the concept of a curve (where a straight line is just a special case of a curve). The only way you can be absolutely convinced is to learn the mathematics of B-Splines.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • Unknown said:
    Are you suggesting that a family of functions mdlLine_doSomething, mdlEllipse_doSomething, mdlShape_doSomething, mdlComplexShape_doSomething, etc. would be more digestible than mdlBSpline_doSomething?

    Yes Jon, why not? Just like PointAtDistance is available to most of the element types because of some super-interface, why not EvaluatePointFrame?

    I think it is ridiculous to have some other type (BSpline for instance) try to do the work that should be handled by the actual types that should provide their own implementation of how to evaluate a point frame. So now, BSpline type has lots of extra baggage to convert all the other types to a spline, just so that one method would not be spread across all those other types?

    Comon, Jon, an Ellipse is definitely not a spline, and the closest spline to fit the curvature of an ellipse would probably contain thousands of poles/control points (well, at least that's what happens in AutoCAD, and I'm not a fan of acad.)

     

    But anyway, I will take your advice and see how Microstation does the conversion/approximation, I dont know, maybe it does a spot on job. Thanks for the tip.

  • John,

    I'm confused. Did you get an answer to your original question?

    If not then look at mdlProject_perpendicular. Given a pick point on an element descriptor, it will return a point on the element, the tangent at that point and a perpendicular at that point. I've never needed anything but NULL for inputRotMatrix but I usually work in 2D. The docs don't say but I'd guess that just defaults to the view rotation matrix. The pseudocode I'd try is:

    1. mdlProject_perpendicular(&pointOn,&tangent,&perpen0,...);
    2. mdlVec_normalize(&tangent) ;// just to be sure
    3. mdlVec_normalize(&perpen0);
    4. mdlVec_crossProduct(&perpen1,&tangent,&perpen0); //get a vec -90 degrees from perpen but still perpendicular to element
    5. mdlVec_crossProduct(&perpen2,&perpen0,&tangent); //get a vec 90 degrees from perpen but still perpendicular to element
    6. mdlVec_scaleInPlace(&perpen0,radius);
    7. mdlVec_scaleInPlace(&perpen1,radius);
    8. mdlVec_scaleInPlace(&perpen2,radius);
    9. mdlCircle_createBy3Pts(&perpen1,&perpen0,&perpen2);

    There are other ways to create a circle with the pointOn, tangent and perpendicular and there may be a currtrans step or two I've left out but you get the idea.

    Regards,

    Charles Griffith

  • Charles -- a good suggestion, but unfortunately this is really a VBA question posted in the wrong Forum.

     
    Regards, Jon Summers
    LA Solutions

  • johnds said:
    Just like PointAtDistance is available to most of the element types because of some super-interface, why not IEvaluatePointFrame?

    Submit a Customer Request (CR) to Bentley. When/if they will respond is uncertain, because it depends on how many requests they get for something similar. I should leave out the ridiculous part, though 8-)

    johnds said:
     … that's what happens in AutoCAD, and I'm not a fan of AutoCAD.)

    Usually people are telling how great is another CAD system, and why can't MicroStation do the same. It's refreshing to hear that another product may not be as smart.

    johnds said:
    An Ellipse is definitely not a spline, and the closest spline to fit the curvature of an ellipse would probably contain thousands of poles/control points.

    I've attached a DGN model with a circle (red) and a B-Spline (green). I created the B-Spline using MicroStation's Convert to B-Spline tool. I hope you'll agree that the B-Spline matches the circle pretty well. It contains 7 poles and 6 knots — easier to count than thousands.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

    B-Spline Circle.dgn
  • johnds said:
    Just like PointAtDistance is available to most of the element types because of some super-interface, why not EvaluatePointFrame?

    Because PointFrame is based on parameters and another element does not have parameters. But skip this stupid discussion about something we can not solve. Following is VBA code (based on my previous suggestion)with same functionality, but without using of BSplines.

    Dan

    Declare Function mdlElmdscr_pointAtDistance Lib "stdmdlbltin.dll" _
        (ByRef position As Point3d, ByRef tangent As Point3d, ByVal inputDistance As Double, _
        ByVal edP As LongByVal inputTolerance As DoubleAs Long


    Sub
     CirclesAlongSelection()

        Dim sel As ElementEnumerator
        Dim norm As Point3d
        Dim bspl As BsplineCurve
        Dim dst As Double, length As Double

        
        ' get selected elements into enumerator

        Set sel = ActiveModelReference.GetSelectedElements
        
        ' worlds normal

        norm = Point3dFromXYZ(001)
        
        ' while any element in selection

        While (sel.MoveNext)
          ' only if it is traversable

          If (sel.Current.IsTraversableElement) Then

            ' get length of curve

            If (sel.Current.IsChainableElement) Then

                length = sel.Current.AsChainableElement.length
            ElseIf (sel.Current.IsClosedElement) Then

                length = sel.Current.AsClosedElement.Perimeter
            Else

                length = 0

            End If

            
            ' place perpendicular cirlces along element

            ' step is 1 unit, radius is 3 units

            For dst = 0 To length Step 1

                Call DrawCircleAtDistanceElement(sel.Current, 3, dst, norm)
            Next dst
            
          End If

        Wend

        
    End Sub



    Sub
     DrawCircleAtDistanceElement(elem As Element, radius As Double, _
                                    distance As Double, lNorm As Point3d)

        Dim pnt As Point3d
        Dim ff As Matrix3d
        Dim param As Double, curv As Double, tors As Double

        Dim ell As EllipseElement
        Dim ellPts(2As Point3d
        
        ' get point at distance and frenet frame,

        ' frenetFrame stores in its rows the tangent (RowX),

        ' main normal (RowY) and binormal (RowZ) of the curve

        ' at the given parameter

        pnt = EvaluatePointFrame(ff, elem, distance, lNorm)
             
        ' calculate points of circle

        ellPts(0) = Point3dAddScaled(pnt, ff.RowY, radius)
        ellPts(1) = Point3dAddScaled(pnt, ff.RowZ, -radius)
        ellPts(2) = Point3dAddScaled(pnt, ff.RowY, -radius)
        
        ' create circle

        Set ell = CreateEllipseElement1(Nothing, ellPts(0), ellPts(1), ellPts(2))
         
        ' if circle is valid element, add it to design file

        If (Not ell Is NothingThen

            ActiveModelReference.AddElement ell
        End If


    End
     Sub

    Function
     EvaluatePointFrame(ByRef frame As Matrix3d, elem As Element, _
        distance As DoubleByRef worldNormal As Point3d) As Point3d
        
        If 0 = mdlElmdscr_pointAtDistance(EvaluatePointFrame, frame.RowX, _
                                          distance, elem.MdlElementDescrP, -1Then

            If (Point3dAreVectorsParallel(frame.RowX, worldNormal)) Then

                If (Not Point3dAreVectorsParallel(worldNormal, Point3dFromXYZ(100))) Then

                    frame.RowY = Point3dCrossProduct(frame.RowX, Point3dFromXYZ(100))
                ElseIf (Not Point3dAreVectorsParallel(worldNormal, Point3dFromXYZ(010))) Then

                    frame.RowY = Point3dCrossProduct(frame.RowX, Point3dFromXYZ(010))
                Else

                    frame.RowY = Point3dCrossProduct(frame.RowX, Point3dFromXYZ(001))
                End If

            Else

                frame.RowY = Point3dCrossProduct(frame.RowX, worldNormal)
            End If

           
    frame.RowY = Point3dNormalize(frame.RowY)
            frame.RowZ = Point3dCrossProduct(frame.RowX, frame.RowY)
            
        End If

        
    End Function

  • Unknown said:
    This is really a VBA question

    I wouldn't think the implementation language is an issue since each function mentioned has a documented VBA Wrapper Declaration and the initial question relates to C#. The key is to use mdlProject_perpendicular to get the principal parameters to build a planar element (a circle or anything else) perpendicular to the path element. This approach can be generalized to any path element available in MicroStation. A little effort with this function along with most of what Dan has suggested should yield the desired result without the need to convert the path to B-spline.

    For curvi-linear paths, B-spline is a nice approximation but it would not be my choice for conic sections (ellipse, parabola, etc.) or for shapes that may contain discontinuities in the path first derivative (square and other polygons). If b-spline representations of conic sections were as good as your circle example suggests, then why are there so many scholarly papers on the subject of "b-spline approximation to conic curves"? The answer is that while the approximation may fool the eye, it is still an approximation and the error bounds can prove unacceptable in many application domains. 

  • Unknown said:

    I wouldn't think the implementation language is an issue since each function mentioned has a documented VBA Wrapper Declaration and the initial question relates to C#. The key is to use mdlProject_perpendicular to get the principal parameters to build a planar element (a circle or anything else) perpendicular to the path element. This approach can be generalized to any path element available in MicroStation. A little effort with this function along with most of what Dan has suggested should yield the desired result without the need to convert the path to B-spline.

    For curvi-linear paths, B-spline is a nice approximation but it would not be my choice for conic sections (ellipse, parabola, etc.) or for shapes that may contain discontinuities in the path first derivative (square and other polygons). If b-spline representations of conic sections were as good as your circle example suggests, then why are there so many scholarly papers on the subject of "b-spline approximation to conic curves"? The answer is that while the approximation may fool the eye, it is still an approximation and the error bounds can prove unacceptable in many application domains. 

     

    Charles, you suggested MDL function mdlProject_perpendicular, but BSpline approximation is not good for you. I just want to inform you, that method you suggested uses approximation as well and I do not think that more accurate. Look at its last argument. It is inputTolerance, this suggests, that before any evaluation of perpendicular point is made, input element is approximated. It is stroked by this tolerance, so it also may be (on ellipses for example) less accurate than BSPline approximation...

     

    Dan