ixmilia / dxf

MIT License
218 stars 67 forks source link

GetBoundingBox method for DxfSpline always return null #135

Closed mdmozibur closed 4 years ago

mdmozibur commented 4 years ago

Is this method yet to be implemented?

brettfo commented 4 years ago

Calculating a proper bounding box for a spline is difficult and to avoid creating a box that's too big I'm not accounting for the curve's control points (see here).

I don't want to force the control points into the calculation in this library, but if you needed something more workable you could implement it on your end like so:

public static DxfBoundingBox? GetBoundingBoxForEntity(DxfEntity entity)
{
    switch (entity)
    {
        case DxfSpline spline:
            // custom implementation for splines; result will be overly large, but guaranteed to contain everything
            return DxfBoundingBox.FromPoints(spline.FitPoints.Concat(spline.ControlPoints.Select(c => c.Point)));
        default:
            // otherwise fall back to default behavior
            return entity.GetBoundingBox();
    }
}
nzain commented 4 years ago

A spline cannot go beyond its control points, thus the bounding box of the control points definitely covers the spline. However, @brettfo is right: it might be too large. How bad that is depends on your requirements and on the type of splines. Splines with many smooth control points may be very well bboxed that way. Few control points with strong directional changes, however, will produce an overly large bbox.

For Bézier curves and B-splines the following article is... great/lengthy/heavy to read. It talks about bounding boxes as well: https://pomax.github.io/bezierinfo/#boundingbox Essentially, to calculate a precise bbox you have to find the roots, which may not be too complicated for quadratic and cubic curves. For higher degrees this is a royal pain in the backside (his words) and most probably you give up or approximate. The degree in DxfSpline can be anything, I guess. I don't see a way for a generic correct implementation.

sampletext32 commented 4 years ago

So, I shouldn't rely on built-in GetBoundingBox for dxf files with splines?

brettfo commented 4 years ago

So, I shouldn't rely on built-in GetBoundingBox for dxf files with splines?

Correct. Since splines can be arbitrarily complicated, it's currently beyond what I have time (and let's be honest, the mathematical ability) to support. If, however, I received a PR adding spline bounds that works in the general sense and I was convinced it was correct, (e.g., with adequate unit tests and a simplified explanation that I could grasp) I would gladly merge it in.

nzain commented 4 years ago

Calculating the bbox around the control points will often provide a useful approximation. Plus: it's better than nothing 😉

Brett V. Forsgren notifications@github.com schrieb am Di., 22. Sept. 2020, 19:58:

So, I shouldn't rely on built-in GetBoundingBox for dxf files with splines?

Correct. Since splines can be arbitrarily complicated, it's currently beyond what I have time (and let's be honest, the mathematical ability) to support. If, however, I received a PR adding spline bounds that works in the general sense and I was convinced it was correct, (e.g., with adequate unit tests and a simplified explanation that I could grasp) I would gladly merge it in.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ixmilia/dxf/issues/135#issuecomment-696883607, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKQV63RAJVOTHDYSLQ62U3SHDQT5ANCNFSM4OJABWLQ .

sampletext32 commented 4 years ago

@brettfo, actually there is a straight bruteforce-like way to determine spline's bounding box - follow its definition. If you separate spline in some-kind reasonable amount of points - you can simply build a bounding box from them. I have a working solution, but it's very memory dependant. Take a look here is the link float t is normalized position along spline [from 0 to 1]

Here is an explanation of how it works wiki link

nzain commented 4 years ago

The problem here is "reasonable amount of points": This depends on the shape and length and the desired precision. Would you aim for a millimeter precision on a 20km spline? If you take 100 samples of the spline, you'll end up with a sample every 200m. If your spline is a straight line, two points are sufficient. My point is: your solution is another approximation, that might be ok for some users, but not necessarily sufficient for all users.

Bird Egop notifications@github.com schrieb am Sa., 10. Okt. 2020, 18:51:

@brettfo https://github.com/brettfo, actually there is a straight bruteforce-like way to determine spline's bounding box - follow its definition. If you separate spline in some-kind reasonable amount of points

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ixmilia/dxf/issues/135#issuecomment-706578298, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKQV62YO2QRGCWA2DWB73TSKCGI3ANCNFSM4OJABWLQ .