3

I have a polyline composed of multiple line segments. The line is very complex and squiggles all over the place in 3D, so for simplicity's sake let's say it looks something like this

line

I want to render it in 3D. Currently, I do a very simple process where I just generate a cylinder for each segment:

line with extruded segments

This is decent, but looks bad where the line changes direction. It is also wasteful - each of the direction changes requires twices as many vertices as is strictly necessary. I would be much happier with an approach that generated shapes like this:

line with a single extruded polycylinder

At first I didn't think it would be too hard, but the more I've worked on it the more I've found it to be surprisingly nontrivial. I'm working in C#, and if this were in 2D I would just use Clipper, but I can't find any libraries or resources for how to solve this problem in 3D. It's okay if the solution isn't always perfect or sometimes leads to self-intersections or things of that nature. Anyone have any guidance?

4
  • maybe this ? github.com/mattdesl/extrude-polyline
    – aybe
    Commented Mar 3, 2021 at 23:09
  • github.com/search?l=C%23&q=extrude&type=Repositories
    – aybe
    Commented Mar 3, 2021 at 23:11
  • Can you show some of the code on how the line segments are defined and how the geometry of each cylinder is calculated. Commented Mar 4, 2021 at 0:23
  • @AndréPopovitch that is pretty smart code although the resulting cylinder is a mesh of extruded n-gon inscribed to the cylinder. The next step would be an additional loop that extends/trims the mesh so that two segments share nodes. The complication here is the orientation of the n-gon is random (see normal vector), so two consecutive "cylinders" would be at an arbitrary angle to each other making the intersection of the two n-gons so much more complex. Commented Mar 4, 2021 at 13:25

2 Answers 2

1

So in a mathematical sense, the intersection of two cylinders is an ellipse. If I give you where the semi-major axis point on the ellipse is and the semi-minor axis you could calculate any number (like numsides) nodes on the ellipse.

Take the node connecting two segments located at a point p and define the two vectors of the ellipse as follows. a is the semi-major axis and b is the semi-minor axis

fig1

Each joining line segment has unit directions vectors e_1 and e_2 and the cylinder has radius R.

Then the intersection ellipse would be defined from the vectors a and b:

fig2

Then find a point c around the ellipse use the following parameterization with t = 0..1

fig3

Here is some C# code that calculates numsides points around the ellipse

// Vectors p, a, b defined
for(int i=0; i<numsides; i++)
{
    double t = (1.0*i)/numsides
    Vector c = p + a*Math.Cos(2*Math.PI*t) + b*Math.Sin(2*Math.PI*t)
    // use/store c as needed for the mesh generation
}
1
  • 1
    Wow, thanks! Since posting this question I came across this link: songho.ca/opengl/gl_cylinder.html#pipe , which I think is a similar approach (computing the intersection of each line of the prism with the plane where the two cylinders should meet). I think your solution will work better, though, I'm going to try implementing it right now and mark it as solved once I do Commented Mar 4, 2021 at 21:40
1

I found this site which had an elegant solution. Starting with some points around the first line segment making up your polyline, you compute the intersection of the line parallel to the current segment that passes through each point and the plane formed by the intersection of the two current line segments.

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.