File Exchange

image thumbnail

Extrude a ribbon/tube and fly through it!

version (4.32 KB) by Teja Muppirala
Extrude a 2D curve along a 3D path to create tubes/ribbons and then fly though it


Updated 01 Sep 2016

View Version History

View License

Sweeps out a 2D cross section (a circle, square, whatever, doesn't even have to be a closed path) through a 3D curve, and returns the generated surface as [X,Y,Z] for use with SURF.
Also, as a fun option, it will fly through the path generated, which looks really cool if you have a fast computer and you maximize the figure. In the DEMO mode, the ROAD cross-section is my favorite. Try changing the colormap as well.

Cite As

Teja Muppirala (2020). Extrude a ribbon/tube and fly through it! (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Basil Khan

Is there a way I can animate the extrusion? Thanks.

hamza bennani

hamza bennani

Hi Teja,
that is an excelent work
i would like to extrude a tube from a 3D curve along a 3d path, tin order to create a tube take into account the warping of the cross-sections , could you help me ?

best regards



Hi Teja,
I'm a student and I'm working on bar's profile. I would extrude a bar from a line, that I draw on MRI slice and I don't want a twisted one. Could you help me?


Francis Chen

Steeve A.

ashwin devotta

Hi Teja,
That is a great work.
I have a slightly different problem at hand. I am trying to parametrically model a twist drill.
In order to create this, I have a 2 dimensional curve in a plane (A). Plane A is obtained by rotating X-Z Plane about Y axis by theta degree. In this case, my base curve contains points in X,Y and Z.
In my case, the trajectory curve is a helical curve.
I want to take the resulting the 3 d curve and obtain a 2 d section in the X-Y plane.

Is there a way to accomodate this situation in your program. I would be using the Algorithm 2 for this purpose. I want this for academic purpose.
Thanks in advance

Ramana Govindasamy

Hi Guys, modification of the code to create a narrowing tube along the axis of extrusion: Additional lines are 169 and 170. Pretty simple!

if norm(z) ~= 0
z = z/norm(z);
q = real(acos(dot(dCvec_prev,dCvec)/norm(dCvec_prev)/norm(dCvec)));

Z = repmat(z,1,npt);
base = base*cos(q) + cross(Z,base)*sin(q)+Z*(1-cos(q))*diag(dot(Z,base));
redr = 0.005*base;
base = base-redr;
camdata = camdata*cos(q) + cross([z z],camdata)*sin(q)+[z z]*(1-cos(q))*diag(dot([z z],camdata));

dCvec_prev = dCvec;

Ramana Govindasamy

Thanks Teja! It is the latter that I am trying to achieve i.e. a curved axis. The curve is defined by 12 (x,y,z) data points which I have interpolated to generate a curve. I will look at modifying the code and give feedback!


Teja Muppirala

Hi Ramana,
I guess there are a couple of things you could do.

If the line to extrude along is straight, you could do something like this using basic MATLAB commands:

[Z,P] = meshgrid(0:1:100,linspace(0,2*pi,101));
R0 = 0.1;
X = R0 * (100-Z) .* cos(P);
Y = R0 * (100-Z) .* sin(P);
h = surf(X,Y,Z)
axis equal

This is an example that just makes a cone along a constant axis.
If you needed the cone to twist around, then I think you would probably have to modify my program a little bit to make the base curve change (get smaller) as you build the surface.


Ramana Govindasamy

Hey Teja! Great work. Quick question, how could one extrude a cone along the newly defined 'z' axis? i'.e. I want the radius of the tube to vary along the length of 'z'.




Thanks a lot. Teja.

Teja Muppirala

Hi daf,
That line of code is Rodrigues' Rotation Formula:

Look at the first equation on that wikipedia page. It is how you rotate a point around an axis of unit length. In my case, instead of using a loop to go over all the base the points one by one, I have kind of vectorized it and done it in one line with a little bit of linear algebra.



sorry! This message should be to Teja. sorry about that


Hi Yuval, thank you for your sharing.But would you please help me the explain the principle of the followwing code:
base = base*cos(q) + cross(Z,base)*sin(q)+Z*(1-cos(q))*diag(dot(Z,base));
Thank you very much!

Rob Campbell

Very clever!

Teja Muppirala

Hi Yuval, thanks for the feedback. I'll update this when I get a chance in the next few days, but all that you need to do to add caps to the ends is add the following in at line 175.

SUR = cat(3,repmat(C(:,1),1,npt),SUR,repmat(C(:,K),1,npt));


Super useful tool for making 3D figures !

Thanks Teja, may the gods of graphics smile upon you.

Feature request: The option to "cap" the end of the tube, so it doesn't look hollow.


Wicked Sick!

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!