André Popovitch
André Popovitch

Reputation: 135

How to compute the vertices for a 3D polyline extrusion?

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?

Upvotes: 3

Views: 808

Answers (2)

André Popovitch
André Popovitch

Reputation: 135

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

Upvotes: 1

John Alexiou
John Alexiou

Reputation: 29254

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
}

Upvotes: 1

Related Questions