Find curves that are contained within another curve

I am looking for a script, RVB, to find curves that are contained within another curve

All the curves lay on the same plane

All the curves are closed and do not intersect, no self-intersections

I am not concerned with multiple nesting, curves within at a second level, I can have the script that finds nested curves move them to a new layer and rerun the script on that layer

No script, but curious on what rules you would choose to identify them.
The first I could come up with:

  • If two curves are used in a ruled surface,
  • AND the surface A = 0
    Then they must be completely on top of eachother

But I guess you have better rules :slight_smile:

I will post on image later, but the idea is simple

Draw two, separate circles on a sheet of paper

Not touching and not one inside the other

Any point on circle A is outside B

Draw the third circle, C in side B

Now any point on C is inside B

But the curves are not simple circles, C could be in a tight loop of a complex shape

in this image, the magenta curve is inside the yellow curve

.rvb rhinoscript visual basic script
PlanarClosedCurveContainment

rhinocommon (c# and phyton)
PlanarClosedCurveRelationship

rhinoscriptsyntax (python)
PlanarClosedCurveContainment

the same is available for point containment - if you already know that there are no intersections this will be much faster - just test a single point of the curve

hope this helps - kind regards - tom

thanks!, any thoughts and how to use it? I mean, in the script, it will have to test every curve against every other (on a given layer), but it would be redundant if I just step through all the curves, and yes, there are not intersections, self or otherwise, I am fixing that manually, and it is very tedious

what s the result ? the next step to do with the curves ?
if CurveB is inside CurveA then what should happen with it ?

how many curves ?

do you need the script only once ?

maybe there is a nice workaround with CurveBoolean or similar ?

there are 36422 curves, they are on 7 separate layers so I can deal with a few thousand at a time

there are maybe 300 or so self intersecting curves per layer, I am about 1/2 done dealing with those, manualy

I will do some more ‘processing’ with these, then export to Illustrator

Part of the process it to extrude them to a point, Rhino won’t do that if there are self intersecting curves (it asks if you want to continue, but then does not)

the script to find ‘nested’ curves, if I find any, I will have the script move them to a new layer

the above link i posted has a rvb sample - this should get you started.

Const rhObjectCurve = 4

Dim strCurve1, strCurve2, intResult

strCurve1 = Rhino.GetObject("Select first curve", rhObjectCurve)
strCurve2 = Rhino.GetObject("Select second curve", rhObjectCurve)

If Rhino.IsCurvePlanar(strCurve1) And Rhino.IsCurvePlanar(strCurve2) Then
    If Rhino.IsCurveClosed(strCurve1) And Rhino.IsCurveClosed(strCurve2) Then
        If Rhino.IsCurveInPlane(strCurve1) And Rhino.IsCurveInPlane(strCurve2) Then

            intResult = Rhino.PlanarClosedCurveContainment(strCurve1, strCurve2)

            If intResult = 0 Then
                Rhino.Print "The regions bounded by the curves are disjoint."
            ElseIf intResult = 1 Then
                Rhino.Print "The two curves intersect."
            ElseIf intResult = 2 Then
                Rhino.Print "The region bounded by Curve1 is inside of Curve2."
            Else
                Rhino.Print "The region bounded by Curve2 is inside of Curve1."
            End If

        End If
    End If
End If

but:

with brut force
36422 * 36421 / 2 = 663 Mio pairs / comparisons (each curve against each other)

so you will need some nice logic.
make sure, as soon as a curve is identified inside another not to compare it against any outside curves, you will need some nice logic to avoid unnecessary comparisons.
some initial bouningbox checking might help for performance. c# / python - rhinocommon will be nicer for this kind of algorithms… (there is also rtree)
This might be quite challenging with vbscript.

share some code and sample geometry to get more help, so we see more precisely where you stuck.

happy code - kind regrads - tom

That was my questions, how to optimize a bit

I do have the relevant curves on there layers, they have different ‘meanings’, so each curve only gets tested against maybe a fourth of the total

And I have one thought, starting with the first curve, check to see if it is on the current layer, then start testing it, stop the test if is ‘inside’, at that point, assign it to a different layer, then it won’t be evaluated again

I am very familiar with VB, never got around to the other languages

ColorNestedInsideOutside.py (5.3 KB)

This is an old script from my Python library, I’m just curious how fast it runs on your 36K curves. Probably not very. I did not try to optimize it much. To do so I think it would require something like an rTree structure (based on physical locations of objects) to restrict the search radius for each object and thus eliminate searching objects further away.

Another tactic I have used in the past is to artifically create a selection grid - sort of a manual “tree” and only work on say the objects in the grid square plus the neighbors. But the grid needs to be sized appropriately to the objects i.e. you want the grid squares to be large enough so that they contain some full objects but not too large that they contain many of them.

Thanks everyone

I have a working script

For optimization, I think I can manually separate into smaller areas

And I only need to run this once, per area, so I think it will work

I tested it on a set of about 2000 curves and it took less than 10 minutes

I think it will be faster if I turn redraw off

I tested mine on 4000, it took about 4 minutes. It’s not really optimized either.

Blue are curves that contain other curves, red are curves that don’t.

With some kind of grid optimization, it would probably run in a second or two… It was also designed to detect multiple nesting levels, if there were only two then it would also be a lot faster.

related topic

maybe python vs vb, maybe curve complexity

thanks, I will have a look at this one as well