Using NURBS library in Matlab

  1. Get the NURBS library from here
  2. Extract the files to your hardisk, for example, C:\Matlab
  3. So, your NURBS lib folder will be on C:\Matlab\nurbs-1.3.1 folder,
  4. Create new m-file, TestNURBSlib.m, the control points data are taken from An Introduction to NURBS: With Historical Perspective book, pp. 215

    Here is the control points data :

    Control Points X Y Z w
    B1,1 -100 -100 0 1
    B1,2 -100 -50 0 1
    B1,3 -100 0 0 1
    B1,4 -100 50 0 1
    B1,5 -100 100 0 1
             
    B2,1 -50 -100 0 1
    B2,2 -50 -50 25 1
    B2,3 -50 0 50 1
    B2,4 -50 50 100 1
    B2,5 -50 100 0 1
             
    B3,1 0 -100 0 1
    B3,2 0 -50 25 1
    B3,3 0 0 50 1
    B3,4 0 50 25 1
    B3,5 0 100 0 1
             
    B4,1 50 -100 0 1
    B4,2 50 -50 25 1
    B4,3 50 0 150 1
    B4,5 50 100 0 1
    B4,4 50 50 25 1
             
    B5,1 100 -100 0 1
    B5,2 100 -50 0 1
    B5,3 100 0 0 1
    B5,4 100 50 0 1
    B5,5 100 100 0 1

    NOTE: I was modified B2,4 [-50 50 25] into B2,4 [-50 50 100]

  5. Arrange the control points data into a matrix in the form as shown below,
    pnts(:,:,1st row Control Point ) = [ B1,1-x B1,2-x B1,3-x B1,4-x B1,5-x;
        B1,1-y B1,2-y B1,3-y B1,4-y B1,5-y;
        B1,1-z B1,2-z B1,3-z B1,4-z B1,5-z;
        B1,1-w B1,2-w B1,3-w B1,4-w B1,5-w;
    pnts(:,:,2nd row Control Point ) = [B2,1-x B2,2-x B2,3-x B2,4-x B2,5-x;
        B2,1-y B2,2-y B2,3-y B2,4-y B2,5-y;
        B2,1-z B2,2-z B2,3-z B2,4-z B2,5-z;
        B2,1-w B2,2-w B2,3-w B2,4-w B2,5-w;
    
  6. The complete source code :
    clear all;
    clc;
    
    addpath nurbs-1.3.1/nurbs/inst -BEGIN;
    
    pnts = zeros(4,5,5);
    pnts(:,:,1) = [ -100 -100 -100 -100 -100;
        -100  -50 0 50 100;
        0 0 0 0 0;
        1 1 1 1 1];
    pnts(:,:,2) = [ -50 -50 -50 -50 -50; % b2,4
        -100 -50 0 50 100;
        0 25 50 100 0;
        1 1 1 1 1];
    
    pnts(:,:,3) = [0 0 0 0 0;
        -100 -50 0 50 100;
        0 25 50 25 0;
        1 1 1 1 1]; 
        
    pnts(:,:,4) = [50 50 50 50 50;
        -100 -50 0 50 100;
        0 25 150 25 0;
        1 1 1 1 1];
        
    pnts(:,:,5) = [ 100 100 100 100 100;
        -100 -50 0 50 100;
        0 0 0 0 0;
        1 1 1 1 1];
    
     knots{1} = [0 0 0 1/3 2/3 1 1 1];
     knots{2} = [0 0 0 1/3 2/3 1 1 1];
    
     srf = nrbmak(pnts,knots);
     nrbplot(srf,[20 20]);hold on;
     title('NURBS surface');
     
     % create plot for the control points
     for i = 1:5
            % U direction
            plot3([pnts(1,1,i) pnts(1,2,i) pnts(1,3,i) pnts(1,4,i) pnts(1,5,i)],[pnts(2,1,i) pnts(2,2,i) pnts(2,3,i) pnts(2,4,i) pnts(2,5,i)],[pnts(3,1,i) pnts(3,2,i) pnts(3,3,i) pnts(3,4,i) pnts(3,5,i)],'b.-');
            % V direction
            plot3([pnts(1,i,1) pnts(1,i,2) pnts(1,i,3) pnts(1,i,4) pnts(1,i,5)],[pnts(2,i,1) pnts(2,i,2) pnts(2,i,3) pnts(2,i,4) pnts(2,i,5)],[pnts(3,i,1) pnts(3,i,2) pnts(3,i,3) pnts(3,i,4) pnts(3,i,5)],'r.-');
     end
     
     axis([-100 100 -100 100 0 200])
     
     hold off;
    
  7. Here is the resulting NURBS surface

    NURBS Surface

    NURBS Surface

    NOTE : For NURBS surface that has control point weight (w != 1 and w > 0), The corresponding control point must be multiplied by the weight, For example, the following control points,

    pnts = zeros(4,5,5);
    pnts(:,:,1) = [ -100 -100 -100 -100 -100;
        -100  -50 0 50 100;
        0 0 0 0 0;
        1 1 1 1 1];
    pnts(:,:,2) = [ -50 -50 -50 -50 -50; % b2,4
        -100 -50 0 50 100;
        0 25 50 100 0;
        1 1 1 1 1];
    
    pnts(:,:,3) = [0 0 0 0 0;
        -100 -50 0 50 100;
        0 25 50 25 0;
        1 1 1 1 1]; 
    
    % change the B4,3 weight into 30
    pnts(:,:,4) = [50 50 30*50 50 50;
        -100 -50 30*0 50 100;
        0 25 30*150 25 0;
        1 1 30 1 1];
        
    pnts(:,:,5) = [ 100 100 100 100 100;
        -100 -50 0 50 100;
        0 0 0 0 0;
        1 1 1 1 1];
    

    will construct the following NURBS surface,

    NURBS Surface

    NURBS Surface

    Happy NURBS-ing :D

Advertisements

26 thoughts on “Using NURBS library in Matlab

  1. Really it is very very important and thank you for that , if you can show more examples on cantilever beam and steel plates with hole it would be very nice , because a lot of civil engineers are waiting that .

    • you’re welcome. Regarding your request, unfortunately, i have no experience in constructing cantilever beam and steel plates.

  2. Hi,
    Thank you for your example, it is very usefull for Nurbs, but there was a mistake in the line 34 with: srf = nrbmak(pnts,knots);

    Please let me know how to repair it?

    DUONG

    • this is my matrix size, now i find how i can create my vector , U vector dimension = degree + number of control point… but i don’t know the value that i must insert in the U vector!!!

    • Oh, I c, regarding the number of knot vector, the dimension of knot vector is degree + number of control point + 1 (http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/B-spline/bspline-curve.html). There are 4 ways how to generate the knot value, the most used are uniform and averaging knot vector. I assume that your curve is clamped type, thus for uniform knot vector is generated from following eq:
      Eq. 1,

      while the averaging knot vector is generated based on equation below:
      Eq. 2
      with m is number of knots, ui is knot value, k is order of the curve, and d is degree of the curve.

  3. Thank you very much for your example,
    I am quite new with Matlab, I tried to run the code, I got the following error on line 34:
    Undefined function or variable ‘nrbmak’.
    I extracted the NURBS library in Matlab folder.

    • Hello Kadhim,
      I confront to same problem. Did you solve it?
      If you solve it, could you let me know?
      Thank you so much for your interest.
      Cuneythaspolat,
      My mail address:cuneyt.haspolat@hotmail.com

  4. Warning: Name is nonexistent or not a directory: nurbs-1.3.1\nurbs\inst
    > In path (line 109)
    In addpath (line 88)
    In nurbs (line 5)
    Undefined function or variable ‘nrbmak’.

    Error in nurbs (line 35)
    srf = nrbmak(pnts,knots);

    these are my error and warning messages.
    I extracted the library file.
    I think, I need to change path, that is what I did. But it doesn’t work. Could you let me know about that error.

    • hi, cuney, have you tried changing your path into full path, e.g. C:\\matlab\nurbs-1.3.1\ blabla\ instead of nurbs-1.3.1\nurbs\inst ?

    • Thank you so much. It is working now. I try to create 2d curve. I set valus of z axis as 0. But it is not look good. Do you have any suggestion for nurbs curve?
      Thank you so much for your interest.

    • hmmm,well,”not look good” term is depend on your institution,it may different with others, could you post your control points and its weights?, or better post your code if it possible.

  5. Actually I did something. I can create a nurbs curve. I have two questions.
    1)When number of x and y elements is increased, should I change order manually? (Is there any way without manual)
    2)How can I control accuracy rate? (For example, chord error adjusting)

    Thank you so much for your interest.
    My code given below.

    addpath C:/Matlab/nurbs-1.3.10/nurbs/inst -BEGIN;

    pnts = [0.5 1.5 4.5 3.0 7.5 6.0 8.5 10.0 12.0 14.0 16.0;
    3.0 5.5 5.5 1.5 1.5 4.0 4.5 5.5 6.5 7.5 8.5;
    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0];
    crv = nrbmak(pnts,[0 0 0 0 0 0 0 1/4 1/2 3/4 3/4 1 1 1 1 1 1 1]);

    % plot the control points
    plot(crv.coefs(1,:),crv.coefs(2,:),’ro’);
    title(‘Arbitrary Test 2D Curve.’);

    hold on;
    plot(crv.coefs(1,:),crv.coefs(2,:),’r–‘);

    % plot the nurbs curve
    nrbplot(crv,48);
    hold off;

    crv.knots(3)=0.1;
    figure
    % plot the control points
    plot(crv.coefs(1,:),crv.coefs(2,:),’ro’);
    title(‘Arbitrary Test 2D Curve.’);
    hold on;
    plot(crv.coefs(1,:),crv.coefs(2,:),’r–‘);

    % plot the nurbs curve
    nrbplot(crv,48);
    hold off;

    • hi cuneythaspolat,

      1. couldn’t understand with your question regarding the order, is it order of the x,y data OR order of the curve a.k.a degree. If degree, the minimum number of control points to generate the curve is degree+1, while the x,y data is not to be sorted

      2. couldn’t understand your second question as well about the accuracy rate. Did you generating an approximation / interpolation curve instead of construct the curve from known control points?

  6. hi Masdel,
    I cant deny, these questions is not clear.
    As you said in 2nd question, I try to generate approximation curve from known curve points. Approximation curve is created by using Chord error or some error term to approximate. Do you know how I can use chord error to create approximation curve.

    I meant degree which is number of control points plus 1 for order. I want to use other points to create my curve. For example, I want to add 18.0 20.0; 10.0 12.0;0 0; at pnts which is given below. What should I change at crv which is given below.
    pnts = [0.5 1.5 4.5 3.0 7.5 6.0 8.5 10.0 12.0 14.0 16.0;
    3.0 5.5 5.5 1.5 1.5 4.0 4.5 5.5 6.5 7.5 8.5;
    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0];
    crv = nrbmak(pnts,[0 0 0 0 0 0 0 1/4 1/2 3/4 3/4 1 1 1 1 1 1 1]);

    • well, if im not mistaken, cmiiw, you want to approximate known points from given control points. if so, there are many options, for example,
      1. you need to manipulate the control points so that your known points is lay on the generated curve (but the control point doesnt need too). this way can be achieved by moving your control points, knot insertion (will increased your control points as well) or adding new control points. ofc, the error is defined by taking difference between your known points and generated curve in certain parameter (not your control points)
      2. if the known points is not too far with the generted curve, you might minimize the error by changing the weights of each control points
      3. etc

      good luckkk!!

  7. As you said, I want to approximate curve by using known points but not control points. I guess, I need knot vector and control points. So, I need to create proper knot vector and control points according to known points. Then, I create nurbs curve by using control points and knot vector. (I dont know how I can use toolbox to do this procedure)

    For 2nd stage, if my approximation (estimation) is not quite good, I manipulate the control points or another parameter.

    Btw, I try to do automatic system ,so I cannot specify knot vector or another parameters manually.

    I will be waiting your answer.
    Thank you so much for your interest.

    • So your input will be, degree of the curve ( known as d ), your known points ( to be simple, I called data points )
      from those input you need to compute (put in order) to construct your NURBS curve,
      1. Parameterize the data points (uniform / exponential / centripetal method), the number is depend on your LOD (length of details)
      2. Generate your knot vector (usually clamped knot vector) (U)
      3. Generate basis function of the d-th degree (R, i would say)
      4. to simplify, degrade the NURBS curve into B-Spline curve by set all the weights into 1, and using gaussian method, solve the linear equation system, (generated by R), to get you control points of the B-Spline curve
      5. Generate your B-Spline curve
      6. If your generated B-Spline curve is far from your data points, (distance error in euclidean space), then manipulate the weight of your curve and generate a new NURBS curve

  8. I look to democurve.m which is available in toolbox.
    Democurve.m is given below.

    %%%%%%%%%%%%%%%%%%%%%%%%%%
    pnts = [0.5 1.5 4.5 3.0 7.5 6.0 8.5;
    3.0 5.5 5.5 1.5 1.5 4.0 4.5;
    0.0 0.0 0.0 0.0 0.0 0.0 0.0];

    crv = nrbmak(pnts,[0 0 0 1/4 1/2 3/4 3/4 1 1 1]);
    % crv = nrbtestcrv;

    % plot the control points
    plot(crv.coefs(1,:),crv.coefs(2,:),’ro’);
    title(‘Arbitrary Test 2D Curve.’);
    hold on;
    plot(crv.coefs(1,:),crv.coefs(2,:),’r–‘);

    % plot the nurbs curve
    nrbplot(crv,48);
    hold off;

    crv.knots(3)=0.1;
    figure
    % plot the control points
    plot(crv.coefs(1,:),crv.coefs(2,:),’ro’);
    title(‘Arbitrary Test 2D Curve.’);
    hold on;
    plot(crv.coefs(1,:),crv.coefs(2,:),’r–‘);

    % plot the nurbs curve
    nrbplot(crv,48);
    hold off;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    pnts: control points
    crv= nrbmak(pnts,knot vector); nurbs curve is created by using control points and knot vector.

    In my situation, I have only data points. How can I create nurbs curve by using data points for this code.
    Is it possible to implement your steps (parameterization generation knot vector …) by using toolbox.

    My data points given below.
    X Y
    335.376 234
    335.376 233
    335.376 213
    188.369 222.8
    188.297 222.819
    335.376 233
    336.374 233.066

    • yes, it is possible, but, the steps that i mentioned before is applicable for B-Spline curve. i suggest you to read piegl’s book regarding this steps.

  9. Hi Masdel,

    This script doesn’t have weight functions, am I right?

    I need to multiply values of control points?

    Thanks,

    • Thank you! I have an assignment, I will engrave my name’s first letter on the NURBS surface. I am thinking to increase the number of control points for more precision. Do you think is it possible in this way?

    • Yes it is, but, don’t forget more control points will increase ur surface rendering as well. Minimum number of control points without loose ur precision is more way better

      Good luck !!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s