Failed intersection near surface kink

The attached file has a surface and a line. The “Intersect” command says they do not intersect, but if I trim off the end of the line by the surface kink the “Intersect” command says the trimmed line and the surface intersect with a total overlap. What’s going on?

This is in Rhino 8 on Windows.

bad intersect.3dm (59.9 KB)

Hi @Russell_Emerine,

The surface in your file is a single face with a kink (crease) in it. Rhino normally splits kinked surfaces into separate faces when they’re added to the document, because intersections and other operations don’t work reliably across a kink. This one wasn’t split, so it probably came from a script, a plug-in or an import.

To fix it, select the surface and run DivideAlongCreases. After that, Intersect with the line finds the overlap.

How was this surface created?

– Dale

This was created in RhinoCommon by extruding a kinked curve. I added it to the document using the overload that disables splitting kinks because when I am investigating a failed operation I need to see the geometry that made the operation fail. That was the right call, because if I split kinks before adding the surface to the document I’d be left wondering why it works in the Rhino GUI but not in RhinoCommon!

This extrusion was made with Surface.CreateExtrusion. I had a previous version of this where I used Extrusion.Create instead, which splits kinks automatically. The problem I had with that version is that surface intersection sometimes didn’t work. Specifically, intersecting the extrusion with another surface that almost lines up with the internal edge (where the kink was originally) sometimes failed. If I spend some time, I should be able to replicate that issue if you want to see it.

It might be possible to use the kinked version for surface intersection and the split version for curve intersection, but it’s all part of one big workflow, so it’s a bit of a coincidence that I can. Do you have any better ideas?

Hi @Russell_Emerine,

Intersections assume each face is smooth, so a single face with a kink isn’t a reliable input. The usual approach is to split the kinks for every operation:

var brep = Brep.CreateFromSurface(srf);
brep.Faces.SplitKinkyFaces(RhinoMath.DefaultAngleTolerance, true);

What’s the larger workflow here, and what does it need to produce? If I understand the goal, I may be able to suggest an approach that avoids kinked surfaces altogether.

Also, if you can reproduce the surface–surface intersection failure with the split version, please post the file. That’s something we should look at.

– Dale

I’ve found the geometry from the old issue. I remembered the problem wrong. It wasn’t a surface-surface intersection that failed, it was brep-curve intersection like the original post, or rather a failure to split a brep with some curves:

The shape on the left is the output from Extrusion.Create, and splitting it with the curves fails. Intersecting the brep and the curves works fine but has some extra points, and I’m not sure how that affects splitting.

The shape on the right is the output from Surface.CreateExtrusion, and splitting it with the curves succeeds. Intersecting the surface and the curves also works as expected.

The curve in the back is the curve that was extruded to make these surfaces. If I try to use the Gumball to make an extrusion, it splits like Extrusion.Create. I suspect that the curve might not actually have any real kinks to begin with, maybe that’s worth looking into?

On a big picture level, this is all part of a complex mold generation system, which is much too big to describe here. The thing I’m trying to do in this post is split the extrusion with the curves. (I was only talking about intersection in the original post, but I actually want to split, which is a strictly harder operation.)

extrusion splits.3dm (216.5 KB)

Hi @Russell_Emerine,

You’re right, the profile has no real kinks. Extrusion.ToBrep splits wherever the curvature breaks, not only at kinks. It’s the same as running ExtrudeCrv with SplitAtTangents=Yes, which is why the Gumball extrusion comes out the same way. Your profile has three of these breaks:

  • the two places where the lines join the curve. The joins are tangent, but the curvature jumps from flat to curved.
  • one spot in the middle of the curve (around z=54.8) where the curvature suddenly flips direction. The curve is made of many short pieces and isn’t quite smooth there.

The ToBrep documentation says it only splits at kinks so the faces are G1. That’s misleading, and I’ll get the docs corrected.

Your cutting lines run almost exactly along two of the new edges, but about 0.002 mm off them, so the split fails. Split should handle that when the curves are within tolerance, and it doesn’t. I’ve logged that here:

In the meantime, since the profile has no kinks, build a single surface and split only at real kinks:

var brep = Brep.CreateFromSurface(Surface.CreateExtrusion(profile, direction));
brep.Faces.SplitKinkyFaces(RhinoMath.DefaultAngleTolerance, true);

With your profile this leaves one face, and splitting it with your curves works.

– Dale

Thanks for making the ticket and noting the documentation change.

I added the SplitKinkyFaces line to my code and now I’m facing a different issue where I make an extrusion surface, SplitKinkyFaces turns it into a brep with two faces, and the Brep.Split call afterwards successfully splits the faces but also splits along the internal edge, making four pieces instead of the expected two. This is despite the splitting curves only crossing the internal edge at one point. When I remove the SplitKinkyFaces line it ends up with a kink but works without issues.

For some reason, I’m only able to replicate this in the codebase I’m working in. If I add the extrusion and curves to the document and try to split them there, the split works as it should. I’ve also tried making a custom command that just does the relevant parts to the extrusion process, and the split works as it should again. I’m going to keep trying to make something replicable by adding more details to my custom command. For now, all I can do is say that there is a problem.

Hi @Russell_Emerine,

Adding objects to the document can change them (mentioned above), which may be why the split works there. To capture exactly what your code passes to Split, write the objects straight to a file instead of adding them to the document:

var file = new Rhino.FileIO.File3dm();
file.Objects.AddBrep(brep);
foreach (var crv in cutters)
  file.Objects.AddCurve(crv);
file.Write(@"C:\temp\split_repro.3dm", 8);

Call this right before your Brep.Split call and post the file. Please also tell me which Split overload you’re calling and what tolerance you pass.

– Dale

Here’s the file.

bad flange split.3dm (23.9 KB)

Unfortunately writing the geometry directly to a file also magically fixes the issue. Here’s the snippet of my code where I write to the file:

						Brep[] pieces = throughFlange.Split(
							[edge.m_Start.m_Curve, edge.m_Curve, edge.m_End.m_Curve],
							0.1
						);
						RhinoApp.WriteLine($"original split: {pieces.Length}");
						using Rhino.FileIO.File3dm file = new();
						file.Objects.AddBrep(throughFlange);
						file.Objects.AddCurve(edge.m_Start.m_Curve);
						file.Objects.AddCurve(edge.m_Curve);
						file.Objects.AddCurve(edge.m_End.m_Curve);
						file.Write("C:\\Users\\RussellEmerine\\Layup\\models\\bad operations\\bad flange split.3dm", 8);

and here’s the snippet where I read from the file, how I imagine you would try:

					using Rhino.FileIO.File3dm file = Rhino.FileIO.File3dm.Read("C:\\Users\\RussellEmerine\\Layup\\models\\bad operations\\bad flange split.3dm");
					Brep brep = null;
					List<Curve> curves = [];
					foreach (var o in file.Objects)
					{
						switch (o.Geometry)
						{
							case Brep ob:
								brep = ob;
								break;
							case Curve oc:
								curves.Add(oc);
								break;
						}
					}
                    Brep[] pieces = brep.Split(
						curves,
                        0.1
                    );
                    RhinoApp.WriteLine($"file split: {pieces.Length}");

I’m getting 4 from the first snippet and 2 from the second snippet. I think there should be a way to add geometry to the document without changing it at all so that plugin developers can debug things like this reliably.

I know 0.1 is a really loose tolerance. I intend to make it tighter when I can verify that doing so doesn’t break things that currently work. I tried changing the tolerance for this specific issue and it didn’t change the behavior.

Hi @Russell_Emerine,

The Brep in your file splits into 2 pieces for me as well.

Could you run these right before your Split call and post the results?

// 1. Validity
throughFlange.IsValidWithLog(out string log);
RhinoApp.WriteLine(log);

// 2. Exact copy
var copy = throughFlange.DuplicateBrep();
RhinoApp.WriteLine($"copy: {copy.Split(cutters, 0.1).Length}");

// 3. In-memory write/read
var rt = (Brep)GeometryBase.FromJSON(throughFlange.ToJSON(null));
RhinoApp.WriteLine($"round trip: {rt.Split(cutters, 0.1).Length}");

Please also post the code that builds throughFlange from the extrusion: which calls, in what order, and with what tolerances.

Thanks,

– Dale

I altered it to do if (throughFlange.IsValidWithLog(out string log)) { RhinoApp.WriteLine("it was valid"); } and it gave the output:

it was valid
copy: 4
round trip: 2

I’ve finally managed to extract code to replicate the problem. The code I’m providing here has some hard-coded numbers from the surrounding context, so don’t mind the mysterious 3109.723 and 2.1. It turns out the trim step is crucial to making it replicate the error.

Call the following command with the curve and point in the attached file:

using System;
using System.Collections.Generic;
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;

namespace LayupRH.Commands
{
	public class MyExtendSurface : Command
	{
		public MyExtendSurface()
		{
			Instance = this;
		}

		public static MyExtendSurface Instance { get; private set; }

		public override string EnglishName => "MyExtendSurface";

		protected override Result RunCommand(RhinoDoc doc, RunMode mode)
		{
			Vector3d dir = -Vector3d.ZAxis * 3109.723;

			using GetObject go = new();
			go.GeometryFilter = ObjectType.Curve;
			if (go.Get() == GetResult.Cancel)
			{
				return Result.Cancel;
			}
			Curve curve = go.Object(0).Curve();

			using GetPoint gp = new();
			if (gp.Get() == GetResult.Cancel)
			{
				return Result.Cancel;
			}
			Point3d point = gp.Point();

			Curve copy = curve.DuplicateCurve();
			copy.Translate(-dir);

            Curve extended =
                copy.Extend(CurveEnd.Both, dir.Length, CurveExtensionStyle.Line)
                ?? throw new Exception("Curve extension failed");

            Brep extrusion = Surface.CreateExtrusion(extended, 2 * dir)?.ToBrep() ?? throw new Exception ("Extrusion failed");
            extrusion.Faces.SplitKinkyFaces();

            Brep[] trims = extrusion.Trim(Brep.CreateFromSphere(new(point, dir.Length / 2.1)), 0.01) ?? throw new Exception("Trim failed");
			if (trims.Length != 1)
			{
				throw new Exception("trim wasn't length 1");
			}
			Brep trim = trims[0];

			List<Curve> cutters = [new LineCurve(new Line(curve.PointAtStart, dir)), curve, new LineCurve(new Line(curve.PointAtEnd, dir))];
			Brep[] splits = trim.Split(cutters, 0.01) ?? throw new Exception("Split failed");
			RhinoApp.WriteLine($"Successful split with {splits.Length} pieces");
			foreach (Brep piece in splits)
			{
				doc.Objects.Add(piece);
			}
			doc.Views.Redraw();

			return Result.Success;
		}
	}
}

extend surface input.3dm (33.2 KB)

Now that we have something to work with I hope to figure out what’s going on. Thanks,
- Russell

Hi @Russell_Emerine,

Thanks, that’s what I needed. I can reproduce it with your command and file: 4 pieces in memory, 2 after a round trip.

Building the surface as an Extrusion and splitting it at the kinks gives 2 pieces here:

Brep extrusion = Extrusion.Create(extended, 2 * dir.Length, false)?.ToBrep(true)
    ?? throw new Exception("Extrusion failed");

Don’t call SplitKinkyFaces after this. Extrusion.Create extrudes along the normal of the curve’s plane, so check the direction and flip the sign of the height if it goes the wrong way.

I will log a ticket.

– Dale

This is exactly the situation where you told me to call SplitKinkyFaces earlier. According to the 2d and 6th posts in the thread, when the surface has a kink in it curve-surface intersection becomes unreliable. Am I supposed to call SplitKinkyFaces or not?