Blender V2.61 - r43446

curve.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include <math.h>  // floor
00034 #include <string.h>
00035 #include <stdlib.h>  
00036 
00037 #include "MEM_guardedalloc.h"
00038 
00039 #include "BLI_bpath.h"
00040 #include "BLI_blenlib.h"
00041 #include "BLI_math.h"
00042 #include "BLI_utildefines.h"
00043 #include "BLI_ghash.h"
00044 
00045 #include "DNA_curve_types.h"  
00046 #include "DNA_material_types.h"  
00047 
00048 /* for dereferencing pointers */
00049 #include "DNA_key_types.h"  
00050 #include "DNA_scene_types.h"  
00051 #include "DNA_vfont_types.h"  
00052 #include "DNA_object_types.h"
00053 
00054 #include "BKE_animsys.h"
00055 #include "BKE_anim.h"  
00056 #include "BKE_curve.h"  
00057 #include "BKE_displist.h"  
00058 #include "BKE_font.h" 
00059 #include "BKE_global.h" 
00060 #include "BKE_key.h"  
00061 #include "BKE_library.h"  
00062 #include "BKE_main.h"  
00063 #include "BKE_object.h"
00064 #include "BKE_material.h"
00065 
00066 /* globals */
00067 
00068 /* local */
00069 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, 
00070                       short cox, short coy,
00071                       float *labda, float *mu, float *vec);
00072 
00073 void unlink_curve(Curve *cu)
00074 {
00075     int a;
00076     
00077     for(a=0; a<cu->totcol; a++) {
00078         if(cu->mat[a]) cu->mat[a]->id.us--;
00079         cu->mat[a]= NULL;
00080     }
00081     if(cu->vfont) cu->vfont->id.us--; 
00082     cu->vfont= NULL;
00083 
00084     if(cu->vfontb) cu->vfontb->id.us--; 
00085     cu->vfontb= NULL;
00086 
00087     if(cu->vfonti) cu->vfonti->id.us--; 
00088     cu->vfonti= NULL;
00089 
00090     if(cu->vfontbi) cu->vfontbi->id.us--; 
00091     cu->vfontbi= NULL;
00092     
00093     if(cu->key) cu->key->id.us--;
00094     cu->key= NULL;
00095 }
00096 
00097 /* frees editcurve entirely */
00098 void BKE_free_editfont(Curve *cu)
00099 {
00100     if(cu->editfont) {
00101         EditFont *ef= cu->editfont;
00102         
00103         if(ef->oldstr) MEM_freeN(ef->oldstr);
00104         if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
00105         if(ef->textbuf) MEM_freeN(ef->textbuf);
00106         if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
00107         if(ef->copybuf) MEM_freeN(ef->copybuf);
00108         if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
00109         
00110         MEM_freeN(ef);
00111         cu->editfont= NULL;
00112     }
00113 }
00114 
00115 void free_curve_editNurb_keyIndex(EditNurb *editnurb)
00116 {
00117     if (!editnurb->keyindex) {
00118         return;
00119     }
00120     BLI_ghash_free(editnurb->keyindex, NULL, (GHashValFreeFP)MEM_freeN);
00121     editnurb->keyindex= NULL;
00122 }
00123 
00124 void free_curve_editNurb (Curve *cu)
00125 {
00126     if(cu->editnurb) {
00127         freeNurblist(&cu->editnurb->nurbs);
00128         free_curve_editNurb_keyIndex(cu->editnurb);
00129         MEM_freeN(cu->editnurb);
00130         cu->editnurb= NULL;
00131     }
00132 }
00133 
00134 /* don't free curve itself */
00135 void free_curve(Curve *cu)
00136 {
00137     freeNurblist(&cu->nurb);
00138     BLI_freelistN(&cu->bev);
00139     freedisplist(&cu->disp);
00140     BKE_free_editfont(cu);
00141 
00142     free_curve_editNurb(cu);
00143     unlink_curve(cu);
00144     BKE_free_animdata((ID *)cu);
00145     
00146     if(cu->mat) MEM_freeN(cu->mat);
00147     if(cu->str) MEM_freeN(cu->str);
00148     if(cu->strinfo) MEM_freeN(cu->strinfo);
00149     if(cu->bb) MEM_freeN(cu->bb);
00150     if(cu->path) free_path(cu->path);
00151     if(cu->tb) MEM_freeN(cu->tb);
00152 }
00153 
00154 Curve *add_curve(const char *name, int type)
00155 {
00156     Curve *cu;
00157 
00158     cu= alloc_libblock(&G.main->curve, ID_CU, name);
00159     
00160     cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
00161     cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
00162     cu->pathlen= 100;
00163     cu->resolu= cu->resolv= (type == OB_SURF) ? 4 : 12;
00164     cu->width= 1.0;
00165     cu->wordspace = 1.0;
00166     cu->spacing= cu->linedist= 1.0;
00167     cu->fsize= 1.0;
00168     cu->ulheight = 0.05;    
00169     cu->texflag= CU_AUTOSPACE;
00170     cu->smallcaps_scale= 0.75f;
00171     cu->twist_mode= CU_TWIST_MINIMUM;   // XXX: this one seems to be the best one in most cases, at least for curve deform...
00172     cu->type= type;
00173     
00174     cu->bb= unit_boundbox();
00175     
00176     if(type==OB_FONT) {
00177         cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
00178         cu->vfont->id.us+=4;
00179         cu->str= MEM_mallocN(12, "str");
00180         BLI_strncpy(cu->str, "Text", 12);
00181         cu->len= cu->pos= 4;
00182         cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
00183         cu->totbox= cu->actbox= 1;
00184         cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
00185         cu->tb[0].w = cu->tb[0].h = 0.0;
00186     }
00187     
00188     return cu;
00189 }
00190 
00191 Curve *copy_curve(Curve *cu)
00192 {
00193     Curve *cun;
00194     int a;
00195     
00196     cun= copy_libblock(&cu->id);
00197     cun->nurb.first= cun->nurb.last= NULL;
00198     duplicateNurblist( &(cun->nurb), &(cu->nurb));
00199 
00200     cun->mat= MEM_dupallocN(cu->mat);
00201     for(a=0; a<cun->totcol; a++) {
00202         id_us_plus((ID *)cun->mat[a]);
00203     }
00204     
00205     cun->str= MEM_dupallocN(cu->str);
00206     cun->strinfo= MEM_dupallocN(cu->strinfo);   
00207     cun->tb= MEM_dupallocN(cu->tb);
00208     cun->bb= MEM_dupallocN(cu->bb);
00209     
00210     cun->key= copy_key(cu->key);
00211     if(cun->key) cun->key->from= (ID *)cun;
00212     
00213     cun->disp.first= cun->disp.last= NULL;
00214     cun->bev.first= cun->bev.last= NULL;
00215     cun->path= NULL;
00216 
00217     cun->editnurb= NULL;
00218     cun->editfont= NULL;
00219     cun->selboxes= NULL;
00220 
00221 #if 0   // XXX old animation system
00222     /* single user ipo too */
00223     if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
00224 #endif // XXX old animation system
00225 
00226     id_us_plus((ID *)cun->vfont);
00227     id_us_plus((ID *)cun->vfontb);  
00228     id_us_plus((ID *)cun->vfonti);
00229     id_us_plus((ID *)cun->vfontbi);
00230     
00231     return cun;
00232 }
00233 
00234 static void extern_local_curve(Curve *cu)
00235 {   
00236     id_lib_extern((ID *)cu->vfont);
00237     id_lib_extern((ID *)cu->vfontb);    
00238     id_lib_extern((ID *)cu->vfonti);
00239     id_lib_extern((ID *)cu->vfontbi);
00240     
00241     if(cu->mat) {
00242         extern_local_matarar(cu->mat, cu->totcol);
00243     }
00244 }
00245 
00246 void make_local_curve(Curve *cu)
00247 {
00248     Main *bmain= G.main;
00249     Object *ob;
00250     int is_local= FALSE, is_lib= FALSE;
00251     
00252     /* - when there are only lib users: don't do
00253      * - when there are only local users: set flag
00254      * - mixed: do a copy
00255      */
00256     
00257     if(cu->id.lib==NULL) return;
00258 
00259     if(cu->id.us==1) {
00260         id_clear_lib_data(bmain, &cu->id);
00261         extern_local_curve(cu);
00262         return;
00263     }
00264 
00265     for(ob= bmain->object.first; ob && ELEM(0, is_lib, is_local); ob= ob->id.next) {
00266         if(ob->data == cu) {
00267             if(ob->id.lib) is_lib= TRUE;
00268             else is_local= TRUE;
00269         }
00270     }
00271 
00272     if(is_local && is_lib == FALSE) {
00273         id_clear_lib_data(bmain, &cu->id);
00274         extern_local_curve(cu);
00275     }
00276     else if(is_local && is_lib) {
00277         Curve *cu_new= copy_curve(cu);
00278         cu_new->id.us= 0;
00279 
00280         BKE_id_lib_local_paths(bmain, cu->id.lib, &cu_new->id);
00281 
00282         for(ob= bmain->object.first; ob; ob= ob->id.next) {
00283             if(ob->data==cu) {
00284                 if(ob->id.lib==NULL) {
00285                     ob->data= cu_new;
00286                     cu_new->id.us++;
00287                     cu->id.us--;
00288                 }
00289             }
00290         }
00291     }
00292 }
00293 
00294 /* Get list of nurbs from editnurbs structure */
00295 ListBase *curve_editnurbs(Curve *cu)
00296 {
00297     if (cu->editnurb) {
00298         return &cu->editnurb->nurbs;
00299     }
00300 
00301     return NULL;
00302 }
00303 
00304 short curve_type(Curve *cu)
00305 {
00306     Nurb *nu;
00307     int type= cu->type;
00308 
00309     if(cu->vfont) {
00310         return OB_FONT;
00311     }
00312 
00313     if(!cu->type) {
00314         type= OB_CURVE;
00315 
00316         for (nu= cu->nurb.first; nu; nu= nu->next) {
00317             if(nu->pntsv>1) {
00318                 type= OB_SURF;
00319             }
00320         }
00321     }
00322 
00323     return type;
00324 }
00325 
00326 void update_curve_dimension(Curve *cu)
00327 {
00328     ListBase *nurbs= BKE_curve_nurbs(cu);
00329     Nurb *nu= nurbs->first;
00330 
00331     if(cu->flag&CU_3D) {
00332         for( ; nu; nu= nu->next) {
00333             nu->flag &= ~CU_2D;
00334         }
00335     }
00336     else {
00337         for( ; nu; nu= nu->next) {
00338             nu->flag |= CU_2D;
00339             test2DNurb(nu);
00340 
00341             /* since the handles are moved they need to be auto-located again */
00342             if(nu->type == CU_BEZIER)
00343                 calchandlesNurb(nu);
00344         }
00345     }
00346 }
00347 
00348 void test_curve_type(Object *ob)
00349 {
00350     ob->type= curve_type(ob->data);
00351 
00352     if(ob->type==OB_CURVE)
00353         update_curve_dimension((Curve *)ob->data);
00354 }
00355 
00356 void tex_space_curve(Curve *cu)
00357 {
00358     DispList *dl;
00359     BoundBox *bb;
00360     float *fp, min[3], max[3];
00361     int tot, doit= 0;
00362     
00363     if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
00364     bb= cu->bb;
00365     
00366     INIT_MINMAX(min, max);
00367 
00368     dl= cu->disp.first;
00369     while(dl) {
00370         
00371         if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
00372         else tot= dl->nr*dl->parts;
00373         
00374         if(tot) doit= 1;
00375         fp= dl->verts;
00376         while(tot--) {
00377             DO_MINMAX(fp, min, max);
00378             fp += 3;
00379         }
00380         dl= dl->next;
00381     }
00382 
00383     if(!doit) {
00384         min[0] = min[1] = min[2] = -1.0f;
00385         max[0] = max[1] = max[2] = 1.0f;
00386     }
00387 
00388     boundbox_set_from_min_max(bb, min, max);
00389 
00390     if(cu->texflag & CU_AUTOSPACE) {
00391         mid_v3_v3v3(cu->loc, min, max);
00392         cu->size[0]= (max[0]-min[0])/2.0f;
00393         cu->size[1]= (max[1]-min[1])/2.0f;
00394         cu->size[2]= (max[2]-min[2])/2.0f;
00395 
00396         cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0f;
00397 
00398         if(cu->size[0]==0.0f) cu->size[0]= 1.0f;
00399         else if(cu->size[0]>0.0f && cu->size[0]<0.00001f) cu->size[0]= 0.00001f;
00400         else if(cu->size[0]<0.0f && cu->size[0]> -0.00001f) cu->size[0]= -0.00001f;
00401     
00402         if(cu->size[1]==0.0f) cu->size[1]= 1.0f;
00403         else if(cu->size[1]>0.0f && cu->size[1]<0.00001f) cu->size[1]= 0.00001f;
00404         else if(cu->size[1]<0.0f && cu->size[1]> -0.00001f) cu->size[1]= -0.00001f;
00405     
00406         if(cu->size[2]==0.0f) cu->size[2]= 1.0f;
00407         else if(cu->size[2]>0.0f && cu->size[2]<0.00001f) cu->size[2]= 0.00001f;
00408         else if(cu->size[2]<0.0f && cu->size[2]> -0.00001f) cu->size[2]= -0.00001f;
00409 
00410     }
00411 }
00412 
00413 int count_curveverts(ListBase *nurb)
00414 {
00415     Nurb *nu;
00416     int tot=0;
00417     
00418     nu= nurb->first;
00419     while(nu) {
00420         if(nu->bezt) tot+= 3*nu->pntsu;
00421         else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
00422         
00423         nu= nu->next;
00424     }
00425     return tot;
00426 }
00427 
00428 int count_curveverts_without_handles(ListBase *nurb)
00429 {
00430     Nurb *nu;
00431     int tot=0;
00432     
00433     nu= nurb->first;
00434     while(nu) {
00435         if(nu->bezt) tot+= nu->pntsu;
00436         else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
00437         
00438         nu= nu->next;
00439     }
00440     return tot;
00441 }
00442 
00443 /* **************** NURBS ROUTINES ******************** */
00444 
00445 void freeNurb(Nurb *nu)
00446 {
00447 
00448     if(nu==NULL) return;
00449 
00450     if(nu->bezt) MEM_freeN(nu->bezt);
00451     nu->bezt= NULL;
00452     if(nu->bp) MEM_freeN(nu->bp);
00453     nu->bp= NULL;
00454     if(nu->knotsu) MEM_freeN(nu->knotsu);
00455     nu->knotsu= NULL;
00456     if(nu->knotsv) MEM_freeN(nu->knotsv);
00457     nu->knotsv= NULL;
00458     /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
00459 
00460     MEM_freeN(nu);
00461 
00462 }
00463 
00464 
00465 void freeNurblist(ListBase *lb)
00466 {
00467     Nurb *nu, *next;
00468 
00469     if(lb==NULL) return;
00470 
00471     nu= lb->first;
00472     while(nu) {
00473         next= nu->next;
00474         freeNurb(nu);
00475         nu= next;
00476     }
00477     lb->first= lb->last= NULL;
00478 }
00479 
00480 Nurb *duplicateNurb(Nurb *nu)
00481 {
00482     Nurb *newnu;
00483     int len;
00484 
00485     newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
00486     if(newnu==NULL) return NULL;
00487     memcpy(newnu, nu, sizeof(Nurb));
00488 
00489     if(nu->bezt) {
00490         newnu->bezt=
00491             (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
00492         memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
00493     }
00494     else {
00495         len= nu->pntsu*nu->pntsv;
00496         newnu->bp=
00497             (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
00498         memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
00499         
00500         newnu->knotsu= newnu->knotsv= NULL;
00501         
00502         if(nu->knotsu) {
00503             len= KNOTSU(nu);
00504             if(len) {
00505                 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
00506                 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
00507             }
00508         }
00509         if(nu->pntsv>1 && nu->knotsv) {
00510             len= KNOTSV(nu);
00511             if(len) {
00512                 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
00513                 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
00514             }
00515         }
00516     }
00517     return newnu;
00518 }
00519 
00520 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
00521 {
00522     Nurb *nu, *nun;
00523     
00524     freeNurblist(lb1);
00525     
00526     nu= lb2->first;
00527     while(nu) {
00528         nun= duplicateNurb(nu);
00529         BLI_addtail(lb1, nun);
00530         
00531         nu= nu->next;
00532     }
00533 }
00534 
00535 void test2DNurb(Nurb *nu)
00536 {
00537     BezTriple *bezt;
00538     BPoint *bp;
00539     int a;
00540     
00541     if((nu->flag & CU_2D)==0)
00542         return;
00543 
00544     if(nu->type == CU_BEZIER) {
00545         a= nu->pntsu;
00546         bezt= nu->bezt;
00547         while(a--) {
00548             bezt->vec[0][2]= 0.0; 
00549             bezt->vec[1][2]= 0.0; 
00550             bezt->vec[2][2]= 0.0;
00551             bezt++;
00552         }
00553     }
00554     else {
00555         a= nu->pntsu*nu->pntsv;
00556         bp= nu->bp;
00557         while(a--) {
00558             bp->vec[2]= 0.0;
00559             bp++;
00560         }
00561     }
00562 }
00563 
00564 void minmaxNurb(Nurb *nu, float *min, float *max)
00565 {
00566     BezTriple *bezt;
00567     BPoint *bp;
00568     int a;
00569 
00570     if(nu->type == CU_BEZIER) {
00571         a= nu->pntsu;
00572         bezt= nu->bezt;
00573         while(a--) {
00574             DO_MINMAX(bezt->vec[0], min, max);
00575             DO_MINMAX(bezt->vec[1], min, max);
00576             DO_MINMAX(bezt->vec[2], min, max);
00577             bezt++;
00578         }
00579     }
00580     else {
00581         a= nu->pntsu*nu->pntsv;
00582         bp= nu->bp;
00583         while(a--) {
00584             DO_MINMAX(bp->vec, min, max);
00585             bp++;
00586         }
00587     }
00588 }
00589 
00590 /* be sure to call makeknots after this */
00591 void addNurbPoints(Nurb *nu, int number)
00592 {
00593     BPoint *tmp= nu->bp;
00594     int i;
00595     nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
00596 
00597     if(tmp) {
00598         memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
00599         MEM_freeN(tmp);
00600     }
00601 
00602     memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
00603 
00604     for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
00605         tmp->radius= 1.0f;
00606     }
00607 
00608     nu->pntsu += number;
00609 }
00610 
00611 void addNurbPointsBezier(Nurb *nu, int number)
00612 {
00613     BezTriple *tmp= nu->bezt;
00614     int i;
00615     nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
00616 
00617     if(tmp) {
00618         memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
00619         MEM_freeN(tmp);
00620     }
00621 
00622     memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
00623 
00624     for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
00625         tmp->radius= 1.0f;
00626     }
00627 
00628     nu->pntsu += number;
00629 }
00630 
00631 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
00632 
00633 
00634 static void calcknots(float *knots, const short pnts, const short order, const short flag)
00635 {
00636     /* knots: number of pnts NOT corrected for cyclic */
00637     const int pnts_order= pnts + order;
00638     float k;
00639     int a;
00640 
00641     switch(flag & (CU_NURB_ENDPOINT|CU_NURB_BEZIER)) {
00642     case CU_NURB_ENDPOINT:
00643         k= 0.0;
00644         for(a=1; a <= pnts_order; a++) {
00645             knots[a-1]= k;
00646             if(a >= order && a <= pnts) k+= 1.0f;
00647         }
00648         break;
00649     case CU_NURB_BEZIER:
00650         /* Warning, the order MUST be 2 or 4,
00651          * if this is not enforced, the displist will be corrupt */
00652         if(order==4) {
00653             k= 0.34;
00654             for(a=0; a < pnts_order; a++) {
00655                 knots[a]= floorf(k);
00656                 k+= (1.0f/3.0f);
00657             }
00658         }
00659         else if(order==3) {
00660             k= 0.6f;
00661             for(a=0; a < pnts_order; a++) {
00662                 if(a >= order && a <= pnts) k+= 0.5f;
00663                 knots[a]= floorf(k);
00664             }
00665         }
00666         else {
00667             printf("bez nurb curve order is not 3 or 4, should never happen\n");
00668         }
00669         break;
00670     default:
00671         for(a=0; a < pnts_order; a++) {
00672             knots[a]= (float)a;
00673         }
00674         break;
00675     }
00676 }
00677 
00678 static void makecyclicknots(float *knots, short pnts, short order)
00679 /* pnts, order: number of pnts NOT corrected for cyclic */
00680 {
00681     int a, b, order2, c;
00682 
00683     if(knots==NULL) return;
00684 
00685     order2=order-1;
00686 
00687     /* do first long rows (order -1), remove identical knots at endpoints */
00688     if(order>2) {
00689         b= pnts+order2;
00690         for(a=1; a<order2; a++) {
00691             if(knots[b]!= knots[b-a]) break;
00692         }
00693         if(a==order2) knots[pnts+order-2]+= 1.0f;
00694     }
00695 
00696     b= order;
00697         c=pnts + order + order2;
00698     for(a=pnts+order2; a<c; a++) {
00699         knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
00700         b--;
00701     }
00702 }
00703 
00704 
00705 
00706 static void makeknots(Nurb *nu, short uv)
00707 {
00708     if(nu->type == CU_NURBS) {
00709         if(uv == 1) {
00710             if(nu->knotsu) MEM_freeN(nu->knotsu);
00711             if(check_valid_nurb_u(nu)) {
00712                 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
00713                 if(nu->flagu & CU_NURB_CYCLIC) {
00714                     calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0);  /* cyclic should be uniform */
00715                     makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
00716                 } else {
00717                     calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
00718                 }
00719             }
00720             else nu->knotsu= NULL;
00721         
00722         } else if(uv == 2) {
00723             if(nu->knotsv) MEM_freeN(nu->knotsv);
00724             if(check_valid_nurb_v(nu)) {
00725                 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
00726                 if(nu->flagv & CU_NURB_CYCLIC) {
00727                     calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0);  /* cyclic should be uniform */
00728                     makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
00729                 } else {
00730                     calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
00731                 }
00732             }
00733             else nu->knotsv= NULL;
00734         }
00735     }
00736 }
00737 
00738 void nurbs_knot_calc_u(Nurb *nu)
00739 {
00740     makeknots(nu, 1);
00741 }
00742 
00743 void nurbs_knot_calc_v(Nurb *nu)
00744 {
00745     makeknots(nu, 2);
00746 }
00747 
00748 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
00749 {
00750     float d, e;
00751     int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
00752 
00753     orderpluspnts= order+pnts;
00754         opp2 = orderpluspnts-1;
00755 
00756     /* this is for float inaccuracy */
00757     if(t < knots[0]) t= knots[0];
00758     else if(t > knots[opp2]) t= knots[opp2];
00759 
00760     /* this part is order '1' */
00761         o2 = order + 1;
00762     for(i=0;i<opp2;i++) {
00763         if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
00764             basis[i]= 1.0;
00765             i1= i-o2;
00766             if(i1<0) i1= 0;
00767             i2= i;
00768             i++;
00769             while(i<opp2) {
00770                 basis[i]= 0.0;
00771                 i++;
00772             }
00773             break;
00774         }
00775         else basis[i]= 0.0;
00776     }
00777     basis[i]= 0.0;
00778     
00779     /* this is order 2,3,... */
00780     for(j=2; j<=order; j++) {
00781 
00782         if(i2+j>= orderpluspnts) i2= opp2-j;
00783 
00784         for(i= i1; i<=i2; i++) {
00785             if(basis[i]!=0.0f)
00786                 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
00787             else
00788                 d= 0.0f;
00789 
00790             if(basis[i+1] != 0.0f)
00791                 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
00792             else
00793                 e= 0.0;
00794 
00795             basis[i]= d+e;
00796         }
00797     }
00798 
00799     *start= 1000;
00800     *end= 0;
00801 
00802     for(i=i1; i<=i2; i++) {
00803         if(basis[i] > 0.0f) {
00804             *end= i;
00805             if(*start==1000) *start= i;
00806         }
00807     }
00808 }
00809 
00810 
00811 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
00812 /* coord_array  has to be 3*4*resolu*resolv in size, and zero-ed */
00813 {
00814     BPoint *bp;
00815     float *basisu, *basis, *basisv, *sum, *fp, *in;
00816     float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
00817     int i, j, iofs, jofs, cycl, len, curu, curv;
00818     int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
00819     
00820     int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
00821     
00822     if(nu->knotsu==NULL || nu->knotsv==NULL) return;
00823     if(nu->orderu>nu->pntsu) return;
00824     if(nu->orderv>nu->pntsv) return;
00825     if(coord_array==NULL) return;
00826     
00827     /* allocate and initialize */
00828     len = totu * totv;
00829     if(len==0) return;
00830     
00831 
00832     
00833     sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
00834     
00835     len= totu*totv;
00836     if(len==0) {
00837         MEM_freeN(sum);
00838         return;
00839     }
00840 
00841     bp= nu->bp;
00842     i= nu->pntsu*nu->pntsv;
00843     ratcomp=0;
00844     while(i--) {
00845         if(bp->vec[3] != 1.0f) {
00846             ratcomp= 1;
00847             break;
00848         }
00849         bp++;
00850     }
00851     
00852     fp= nu->knotsu;
00853     ustart= fp[nu->orderu-1];
00854     if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
00855     else uend= fp[nu->pntsu];
00856     ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
00857     
00858     basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
00859 
00860     fp= nu->knotsv;
00861     vstart= fp[nu->orderv-1];
00862     
00863     if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
00864     else vend= fp[nu->pntsv];
00865     vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
00866     
00867     len= KNOTSV(nu);
00868     basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
00869     jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
00870     jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
00871 
00872     /* precalculation of basisv and jstart,jend */
00873     if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1; 
00874     else cycl= 0;
00875     v= vstart;
00876     basis= basisv;
00877     curv= totv;
00878     while(curv--) {
00879         basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
00880         basis+= KNOTSV(nu);
00881         v+= vstep;
00882     }
00883 
00884     if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1; 
00885     else cycl= 0;
00886     in= coord_array;
00887     u= ustart;
00888     curu= totu;
00889     while(curu--) {
00890 
00891         basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
00892 
00893         basis= basisv;
00894         curv= totv;
00895         while(curv--) {
00896 
00897             jsta= jstart[curv];
00898             jen= jend[curv];
00899 
00900             /* calculate sum */
00901             sumdiv= 0.0;
00902             fp= sum;
00903 
00904             for(j= jsta; j<=jen; j++) {
00905 
00906                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
00907                 else jofs= j;
00908                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
00909 
00910                 for(i= istart; i<=iend; i++, fp++) {
00911 
00912                     if(i>= nu->pntsu) {
00913                         iofs= i- nu->pntsu;
00914                         bp= nu->bp+ nu->pntsu*jofs+iofs;
00915                     }
00916                     else bp++;
00917 
00918                     if(ratcomp) {
00919                         *fp= basisu[i]*basis[j]*bp->vec[3];
00920                         sumdiv+= *fp;
00921                     }
00922                     else *fp= basisu[i]*basis[j];
00923                 }
00924             }
00925         
00926             if(ratcomp) {
00927                 fp= sum;
00928                 for(j= jsta; j<=jen; j++) {
00929                     for(i= istart; i<=iend; i++, fp++) {
00930                         *fp/= sumdiv;
00931                     }
00932                 }
00933             }
00934 
00935             /* one! (1.0) real point now */
00936             fp= sum;
00937             for(j= jsta; j<=jen; j++) {
00938 
00939                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
00940                 else jofs= j;
00941                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
00942 
00943                 for(i= istart; i<=iend; i++, fp++) {
00944 
00945                     if(i>= nu->pntsu) {
00946                         iofs= i- nu->pntsu;
00947                         bp= nu->bp+ nu->pntsu*jofs+iofs;
00948                     }
00949                     else bp++;
00950 
00951                     if(*fp != 0.0f) {
00952                         in[0]+= (*fp) * bp->vec[0];
00953                         in[1]+= (*fp) * bp->vec[1];
00954                         in[2]+= (*fp) * bp->vec[2];
00955                     }
00956                 }
00957             }
00958 
00959             in+=3;
00960             basis+= KNOTSV(nu);
00961         }
00962         u+= ustep;
00963         if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
00964     }
00965 
00966     /* free */
00967     MEM_freeN(sum);
00968     MEM_freeN(basisu);
00969     MEM_freeN(basisv);
00970     MEM_freeN(jstart);
00971     MEM_freeN(jend);
00972 }
00973 
00974 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
00975 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
00976  * tilt_array and radius_array will be written to if valid */
00977 {
00978     BPoint *bp;
00979     float u, ustart, uend, ustep, sumdiv;
00980     float *basisu, *sum, *fp;
00981     float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
00982     int i, len, istart, iend, cycl;
00983 
00984     if(nu->knotsu==NULL) return;
00985     if(nu->orderu>nu->pntsu) return;
00986     if(coord_array==NULL) return;
00987 
00988     /* allocate and initialize */
00989     len= nu->pntsu;
00990     if(len==0) return;
00991     sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
00992     
00993     resolu= (resolu*SEGMENTSU(nu));
00994     
00995     if(resolu==0) {
00996         MEM_freeN(sum);
00997         return;
00998     }
00999 
01000     fp= nu->knotsu;
01001     ustart= fp[nu->orderu-1];
01002     if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
01003     else uend= fp[nu->pntsu];
01004     ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
01005     
01006     basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
01007 
01008     if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1; 
01009     else cycl= 0;
01010 
01011     u= ustart;
01012     while(resolu--) {
01013 
01014         basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
01015         /* calc sum */
01016         sumdiv= 0.0;
01017         fp= sum;
01018         bp= nu->bp+ istart-1;
01019         for(i= istart; i<=iend; i++, fp++) {
01020 
01021             if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
01022             else bp++;
01023 
01024             *fp= basisu[i]*bp->vec[3];
01025             sumdiv+= *fp;
01026         }
01027         if(sumdiv != 0.0f) if(sumdiv < 0.999f || sumdiv > 1.001f) {
01028             /* is normalizing needed? */
01029             fp= sum;
01030             for(i= istart; i<=iend; i++, fp++) {
01031                 *fp/= sumdiv;
01032             }
01033         }
01034 
01035         /* one! (1.0) real point */
01036         fp= sum;
01037         bp= nu->bp+ istart-1;
01038         for(i= istart; i<=iend; i++, fp++) {
01039 
01040             if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
01041             else bp++;
01042 
01043             if(*fp != 0.0f) {
01044                 
01045                 coord_fp[0]+= (*fp) * bp->vec[0];
01046                 coord_fp[1]+= (*fp) * bp->vec[1];
01047                 coord_fp[2]+= (*fp) * bp->vec[2];
01048                 
01049                 if (tilt_fp)
01050                     (*tilt_fp) += (*fp) * bp->alfa;
01051                 
01052                 if (radius_fp)
01053                     (*radius_fp) += (*fp) * bp->radius;
01054 
01055                 if (weight_fp)
01056                     (*weight_fp) += (*fp) * bp->weight;
01057                 
01058             }
01059         }
01060 
01061         coord_fp = (float *)(((char *)coord_fp) + stride);
01062         
01063         if (tilt_fp)    tilt_fp = (float *)(((char *)tilt_fp) + stride);
01064         if (radius_fp)  radius_fp = (float *)(((char *)radius_fp) + stride);
01065         if (weight_fp)  weight_fp = (float *)(((char *)weight_fp) + stride);
01066         
01067         u+= ustep;
01068     }
01069 
01070     /* free */
01071     MEM_freeN(sum);
01072     MEM_freeN(basisu);
01073 }
01074 
01075 /* forward differencing method for bezier curve */
01076 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
01077 {
01078     float rt0,rt1,rt2,rt3,f;
01079     int a;
01080 
01081     f= (float)it;
01082     rt0= q0;
01083     rt1= 3.0f*(q1-q0)/f;
01084     f*= f;
01085     rt2= 3.0f*(q0-2.0f*q1+q2)/f;
01086     f*= it;
01087     rt3= (q3-q0+3.0f*(q1-q2))/f;
01088 
01089     q0= rt0;
01090     q1= rt1+rt2+rt3;
01091     q2= 2*rt2+6*rt3;
01092     q3= 6*rt3;
01093 
01094     for(a=0; a<=it; a++) {
01095         *p= q0;
01096         p = (float *)(((char *)p)+stride);
01097         q0+= q1;
01098         q1+= q2;
01099         q2+= q3;
01100     }
01101 }
01102 
01103 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
01104 {
01105     /* note that these are not purpendicular to the curve
01106      * they need to be rotated for this,
01107      *
01108      * This could also be optimized like forward_diff_bezier */
01109     int a;
01110     for(a=0; a<=it; a++) {
01111         float t = (float)a / (float)it;
01112 
01113         int i;
01114         for(i=0; i<3; i++) {
01115             p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
01116         }
01117         normalize_v3(p);
01118         p = (float *)(((char *)p)+stride);
01119     }
01120 }
01121 
01122 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
01123 
01124 float *make_orco_surf(Object *ob)
01125 {
01126     /* Note: this function is used in convertblender only atm, so
01127      * suppose nonzero curve's render resolution should always be used */
01128     Curve *cu= ob->data;
01129     Nurb *nu;
01130     int a, b, tot=0;
01131     int sizeu, sizev;
01132     int resolu, resolv;
01133     float *fp, *coord_array;
01134     
01135     /* first calculate the size of the datablock */
01136     nu= cu->nurb.first;
01137     while(nu) {
01138         /* as we want to avoid the seam in a cyclic nurbs
01139         texture wrapping, reserve extra orco data space to save these extra needed
01140         vertex based UV coordinates for the meridian vertices.
01141         Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
01142         the renderface/vert construction.
01143         
01144         See also convertblender.c: init_render_surf()
01145         */
01146 
01147         resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
01148         resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
01149         
01150         sizeu = nu->pntsu*resolu;
01151         sizev = nu->pntsv*resolv;
01152         if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
01153         if (nu->flagv & CU_NURB_CYCLIC) sizev++;
01154         if(nu->pntsv>1) tot+= sizeu * sizev;
01155         
01156         nu= nu->next;
01157     }
01158     /* makeNurbfaces wants zeros */
01159     fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
01160     
01161     nu= cu->nurb.first;
01162     while(nu) {
01163         resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
01164         resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
01165 
01166         if(nu->pntsv>1) {
01167             sizeu = nu->pntsu*resolu;
01168             sizev = nu->pntsv*resolv;
01169             if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
01170             if (nu->flagv & CU_NURB_CYCLIC) sizev++;
01171             
01172             if(cu->flag & CU_UV_ORCO) {
01173                 for(b=0; b< sizeu; b++) {
01174                     for(a=0; a< sizev; a++) {
01175                         
01176                         if(sizev <2) fp[0]= 0.0f;
01177                         else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
01178                         
01179                         if(sizeu <2) fp[1]= 0.0f;
01180                         else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
01181                         
01182                         fp[2]= 0.0;
01183                         
01184                         fp+= 3;
01185                     }
01186                 }
01187             }
01188             else {
01189                 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
01190                 float *tdata= _tdata;
01191                 
01192                 makeNurbfaces(nu, tdata, 0, resolu, resolv);
01193                 
01194                 for(b=0; b<sizeu; b++) {
01195                     int use_b= b;
01196                     if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
01197                         use_b= 0;
01198                     
01199                     for(a=0; a<sizev; a++) {
01200                         int use_a= a;
01201                         if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
01202                             use_a= 0;
01203                         
01204                         tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
01205                         
01206                         fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
01207                         fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
01208                         fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
01209                         fp+= 3;
01210                     }
01211                 }
01212                 
01213                 MEM_freeN(_tdata);
01214             }
01215         }
01216         nu= nu->next;
01217     }
01218     
01219     return coord_array;
01220 }
01221 
01222 
01223     /* NOTE: This routine is tied to the order of vertex
01224      * built by displist and as passed to the renderer.
01225      */
01226 float *make_orco_curve(Scene *scene, Object *ob)
01227 {
01228     Curve *cu = ob->data;
01229     DispList *dl;
01230     int u, v, numVerts;
01231     float *fp, *coord_array;
01232     ListBase disp = {NULL, NULL};
01233 
01234     makeDispListCurveTypes_forOrco(scene, ob, &disp);
01235 
01236     numVerts = 0;
01237     for (dl=disp.first; dl; dl=dl->next) {
01238         if (dl->type==DL_INDEX3) {
01239             numVerts += dl->nr;
01240         } else if (dl->type==DL_SURF) {
01241             /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
01242             if (dl->flag & DL_CYCL_U) {
01243                 if (dl->flag & DL_CYCL_V)
01244                     numVerts += (dl->parts+1)*(dl->nr+1);
01245                 else
01246                     numVerts += dl->parts*(dl->nr+1);
01247             }
01248             else
01249                 numVerts += dl->parts*dl->nr;
01250         }
01251     }
01252 
01253     fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
01254     for (dl=disp.first; dl; dl=dl->next) {
01255         if (dl->type==DL_INDEX3) {
01256             for (u=0; u<dl->nr; u++, fp+=3) {
01257                 if (cu->flag & CU_UV_ORCO) {
01258                     fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
01259                     fp[1]= 0.0;
01260                     fp[2]= 0.0;
01261                 } else {
01262                     copy_v3_v3(fp, &dl->verts[u*3]);
01263 
01264                     fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
01265                     fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
01266                     fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
01267                 }
01268             }
01269         } else if (dl->type==DL_SURF) {
01270             int sizeu= dl->nr, sizev= dl->parts;
01271             
01272             /* exception as handled in convertblender.c too */
01273             if (dl->flag & DL_CYCL_U) {
01274                 sizeu++;
01275                 if (dl->flag & DL_CYCL_V)
01276                     sizev++;
01277             }
01278             
01279             for (u=0; u<sizev; u++) {
01280                 for (v=0; v<sizeu; v++,fp+=3) {
01281                     if (cu->flag & CU_UV_ORCO) {
01282                         fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
01283                         fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
01284                         fp[2]= 0.0;
01285                     } else {
01286                         float *vert;
01287                         int realv= v % dl->nr;
01288                         int realu= u % dl->parts;
01289                         
01290                         vert= dl->verts + 3*(dl->nr*realu + realv);
01291                         copy_v3_v3(fp, vert);
01292 
01293                         fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
01294                         fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
01295                         fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
01296                     }
01297                 }
01298             }
01299         }
01300     }
01301 
01302     freedisplist(&disp);
01303 
01304     return coord_array;
01305 }
01306 
01307 
01308 /* ***************** BEVEL ****************** */
01309 
01310 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
01311 {
01312     DispList *dl, *dlnew;
01313     Curve *bevcu, *cu;
01314     float *fp, facx, facy, angle, dangle;
01315     int nr, a;
01316 
01317     cu= ob->data;
01318     disp->first = disp->last = NULL;
01319 
01320     /* if a font object is being edited, then do nothing */
01321 // XXX  if( ob == obedit && ob->type == OB_FONT ) return;
01322 
01323     if(cu->bevobj) {
01324         if (cu->bevobj->type!=OB_CURVE) return;
01325 
01326         bevcu= cu->bevobj->data;
01327         if(bevcu->ext1==0.0f && bevcu->ext2==0.0f) {
01328             ListBase bevdisp= {NULL, NULL};
01329             facx= cu->bevobj->size[0];
01330             facy= cu->bevobj->size[1];
01331 
01332             if (forRender) {
01333                 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
01334                 dl= bevdisp.first;
01335             } else {
01336                 dl= cu->bevobj->disp.first;
01337                 if(dl==NULL) {
01338                     makeDispListCurveTypes(scene, cu->bevobj, 0);
01339                     dl= cu->bevobj->disp.first;
01340                 }
01341             }
01342 
01343             while(dl) {
01344                 if ELEM(dl->type, DL_POLY, DL_SEGM) {
01345                     dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
01346                     *dlnew= *dl;
01347                     dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
01348                     memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
01349 
01350                     if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
01351 
01352                     BLI_addtail(disp, dlnew);
01353                     fp= dlnew->verts;
01354                     nr= dlnew->parts*dlnew->nr;
01355                     while(nr--) {
01356                         fp[2]= fp[1]*facy;
01357                         fp[1]= -fp[0]*facx;
01358                         fp[0]= 0.0;
01359                         fp+= 3;
01360                     }
01361                 }
01362                 dl= dl->next;
01363             }
01364 
01365             freedisplist(&bevdisp);
01366         }
01367     }
01368     else if(cu->ext1==0.0f && cu->ext2==0.0f) {
01369         ;
01370     }
01371     else if(cu->ext2==0.0f) {
01372         dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
01373         dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
01374         BLI_addtail(disp, dl);
01375         dl->type= DL_SEGM;
01376         dl->parts= 1;
01377         dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
01378         dl->nr= 2;
01379         
01380         fp= dl->verts;
01381         fp[0]= fp[1]= 0.0;
01382         fp[2]= -cu->ext1;
01383         fp[3]= fp[4]= 0.0;
01384         fp[5]= cu->ext1;
01385     }
01386     else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f)  { // we make a full round bevel in that case
01387         
01388         nr= 4+ 2*cu->bevresol;
01389            
01390         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
01391         dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
01392         BLI_addtail(disp, dl);
01393         dl->type= DL_POLY;
01394         dl->parts= 1;
01395         dl->flag= DL_BACK_CURVE;
01396         dl->nr= nr;
01397 
01398         /* a circle */
01399         fp= dl->verts;
01400         dangle= (2.0f*(float)M_PI/(nr));
01401         angle= -(nr-1)*dangle;
01402         
01403         for(a=0; a<nr; a++) {
01404             fp[0]= 0.0;
01405             fp[1]= (cosf(angle)*(cu->ext2));
01406             fp[2]= (sinf(angle)*(cu->ext2)) - cu->ext1;
01407             angle+= dangle;
01408             fp+= 3;
01409         }
01410     }
01411     else {
01412         short dnr;
01413         
01414         /* bevel now in three parts, for proper vertex normals */
01415         /* part 1, back */
01416 
01417         if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
01418             dnr= nr= 2+ cu->bevresol;
01419             if( (cu->flag & (CU_FRONT|CU_BACK))==0)
01420                 nr= 3+ 2*cu->bevresol;
01421 
01422             dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
01423             dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
01424             BLI_addtail(disp, dl);
01425             dl->type= DL_SEGM;
01426             dl->parts= 1;
01427             dl->flag= DL_BACK_CURVE;
01428             dl->nr= nr;
01429 
01430             /* half a circle */
01431             fp= dl->verts;
01432             dangle= (0.5*M_PI/(dnr-1));
01433             angle= -(nr-1)*dangle;
01434 
01435             for(a=0; a<nr; a++) {
01436                 fp[0]= 0.0;
01437                 fp[1]= (float)(cosf(angle)*(cu->ext2));
01438                 fp[2]= (float)(sinf(angle)*(cu->ext2)) - cu->ext1;
01439                 angle+= dangle;
01440                 fp+= 3;
01441             }
01442         }
01443         
01444         /* part 2, sidefaces */
01445         if(cu->ext1!=0.0f) {
01446             nr= 2;
01447             
01448             dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
01449             dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
01450             BLI_addtail(disp, dl);
01451             dl->type= DL_SEGM;
01452             dl->parts= 1;
01453             dl->nr= nr;
01454             
01455             fp= dl->verts;
01456             fp[1]= cu->ext2;
01457             fp[2]= -cu->ext1;
01458             fp[4]= cu->ext2;
01459             fp[5]= cu->ext1;
01460             
01461             if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
01462                 dl= MEM_dupallocN(dl);
01463                 dl->verts= MEM_dupallocN(dl->verts);
01464                 BLI_addtail(disp, dl);
01465                 
01466                 fp= dl->verts;
01467                 fp[1]= -fp[1];
01468                 fp[2]= -fp[2];
01469                 fp[4]= -fp[4];
01470                 fp[5]= -fp[5];
01471             }
01472         }
01473         
01474         /* part 3, front */
01475         if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
01476             dnr= nr= 2+ cu->bevresol;
01477             if( (cu->flag & (CU_FRONT|CU_BACK))==0)
01478                 nr= 3+ 2*cu->bevresol;
01479 
01480             dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
01481             dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
01482             BLI_addtail(disp, dl);
01483             dl->type= DL_SEGM;
01484             dl->flag= DL_FRONT_CURVE;
01485             dl->parts= 1;
01486             dl->nr= nr;
01487 
01488             /* half a circle */
01489             fp= dl->verts;
01490             angle= 0.0;
01491             dangle= (0.5*M_PI/(dnr-1));
01492 
01493             for(a=0; a<nr; a++) {
01494                 fp[0]= 0.0;
01495                 fp[1]= (float)(cosf(angle)*(cu->ext2));
01496                 fp[2]= (float)(sinf(angle)*(cu->ext2)) + cu->ext1;
01497                 angle+= dangle;
01498                 fp+= 3;
01499             }
01500         }
01501     }
01502 }
01503 
01504 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
01505 {
01506     /* return:
01507         -1: colliniar
01508          0: no intersection of segments
01509          1: exact intersection of segments
01510          2: cross-intersection of segments
01511     */
01512     float deler;
01513 
01514     deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
01515     if(deler==0.0f) return -1;
01516 
01517     *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
01518     *labda= -(*labda/deler);
01519 
01520     deler= v3[coy]-v4[coy];
01521     if(deler==0) {
01522         deler=v3[cox]-v4[cox];
01523         *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
01524     } else {
01525         *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
01526     }
01527     vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
01528     vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
01529 
01530     if(*labda>=0.0f && *labda<=1.0f && *mu>=0.0f && *mu<=1.0f) {
01531         if(*labda==0.0f || *labda==1.0f || *mu==0.0f || *mu==1.0f) return 1;
01532         return 2;
01533     }
01534     return 0;
01535 }
01536 
01537 
01538 static short bevelinside(BevList *bl1,BevList *bl2)
01539 {
01540     /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
01541     /* returns '1' if correct hole  */
01542     BevPoint *bevp, *prevbevp;
01543     float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
01544     int nr, links=0,rechts=0,mode;
01545 
01546     /* take first vertex of possible hole */
01547 
01548     bevp= (BevPoint *)(bl2+1);
01549     hvec1[0]= bevp->vec[0]; 
01550     hvec1[1]= bevp->vec[1]; 
01551     hvec1[2]= 0.0;
01552     copy_v3_v3(hvec2,hvec1);
01553     hvec2[0]+=1000;
01554 
01555     /* test it with all edges of potential surounding poly */
01556     /* count number of transitions left-right  */
01557 
01558     bevp= (BevPoint *)(bl1+1);
01559     nr= bl1->nr;
01560     prevbevp= bevp+(nr-1);
01561 
01562     while(nr--) {
01563         min= prevbevp->vec[1];
01564         max= bevp->vec[1];
01565         if(max<min) {
01566             min= max;
01567             max= prevbevp->vec[1];
01568         }
01569         if(min!=max) {
01570             if(min<=hvec1[1] && max>=hvec1[1]) {
01571                 /* there's a transition, calc intersection point */
01572                 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
01573                 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
01574                        only allow for one situation: we choose lab= 1.0
01575                  */
01576                 if(mode >= 0 && lab != 0.0f) {
01577                     if(vec[0]<hvec1[0]) links++;
01578                     else rechts++;
01579                 }
01580             }
01581         }
01582         prevbevp= bevp;
01583         bevp++;
01584     }
01585     
01586     if( (links & 1) && (rechts & 1) ) return 1;
01587     return 0;
01588 }
01589 
01590 
01591 struct bevelsort {
01592     float left;
01593     BevList *bl;
01594     int dir;
01595 };
01596 
01597 static int vergxcobev(const void *a1, const void *a2)
01598 {
01599     const struct bevelsort *x1=a1,*x2=a2;
01600 
01601     if( x1->left > x2->left ) return 1;
01602     else if( x1->left < x2->left) return -1;
01603     return 0;
01604 }
01605 
01606 /* this function cannot be replaced with atan2, but why? */
01607 
01608 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
01609 {
01610     float t01, t02, x3, y3;
01611 
01612     t01= (float)sqrt(x1*x1+y1*y1);
01613     t02= (float)sqrt(x2*x2+y2*y2);
01614     if(t01==0.0f) t01= 1.0f;
01615     if(t02==0.0f) t02= 1.0f;
01616 
01617     x1/=t01; 
01618     y1/=t01;
01619     x2/=t02; 
01620     y2/=t02;
01621 
01622     t02= x1*x2+y1*y2;
01623     if(fabs(t02)>=1.0) t02= .5*M_PI;
01624     else t02= (saacos(t02))/2.0f;
01625 
01626     t02= (float)sin(t02);
01627     if(t02==0.0f) t02= 1.0f;
01628 
01629     x3= x1-x2;
01630     y3= y1-y2;
01631     if(x3==0 && y3==0) {
01632         x3= y1;
01633         y3= -x1;
01634     } else {
01635         t01= (float)sqrt(x3*x3+y3*y3);
01636         x3/=t01; 
01637         y3/=t01;
01638     }
01639 
01640     *sina= -y3/t02;
01641     *cosa= x3/t02;
01642 
01643 }
01644 
01645 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
01646 {
01647     BezTriple *pprev, *next, *last;
01648     float fac, dfac, t[4];
01649     int a;
01650     
01651     if(tilt_array==NULL && radius_array==NULL)
01652         return;
01653     
01654     last= nu->bezt+(nu->pntsu-1);
01655     
01656     /* returns a point */
01657     if(prevbezt==nu->bezt) {
01658         if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
01659         else pprev= prevbezt;
01660     }
01661     else pprev= prevbezt-1;
01662     
01663     /* next point */
01664     if(bezt==last) {
01665         if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
01666         else next= bezt;
01667     }
01668     else next= bezt+1;
01669     
01670     fac= 0.0;
01671     dfac= 1.0f/(float)resolu;
01672     
01673     for(a=0; a<resolu; a++, fac+= dfac) {
01674         if (tilt_array) {
01675             if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
01676                 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
01677             } else {
01678                 key_curve_position_weights(fac, t, nu->tilt_interp);
01679                 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
01680             }
01681             
01682             tilt_array = (float *)(((char *)tilt_array) + stride); 
01683         }
01684         
01685         if (radius_array) {
01686             if (nu->radius_interp==KEY_CU_EASE) {
01687                 /* Support 2.47 ease interp
01688                  * Note! - this only takes the 2 points into account,
01689                  * giving much more localized results to changes in radius, sometimes you want that */
01690                 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
01691             } else {
01692                 
01693                 /* reuse interpolation from tilt if we can */
01694                 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
01695                     key_curve_position_weights(fac, t, nu->radius_interp);
01696                 }
01697                 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
01698             }
01699             
01700             radius_array = (float *)(((char *)radius_array) + stride); 
01701         }
01702 
01703         if(weight_array) {
01704             /* basic interpolation for now, could copy tilt interp too  */
01705             *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
01706 
01707             weight_array = (float *)(((char *)weight_array) + stride);
01708         }
01709     }
01710 }
01711 
01712 /* make_bevel_list_3D_* funcs, at a minimum these must
01713  * fill in the bezp->quat and bezp->dir values */
01714 
01715 /* correct non-cyclic cases by copying direction and rotation
01716  * values onto the first & last end-points */
01717 static void bevel_list_cyclic_fix_3D(BevList *bl)
01718 {
01719     BevPoint *bevp, *bevp1;
01720 
01721     bevp= (BevPoint *)(bl+1);
01722     bevp1= bevp+1;
01723     copy_qt_qt(bevp->quat, bevp1->quat);
01724     copy_v3_v3(bevp->dir, bevp1->dir);
01725     copy_v3_v3(bevp->tan, bevp1->tan);
01726     bevp= (BevPoint *)(bl+1);
01727     bevp+= (bl->nr-1);
01728     bevp1= bevp-1;
01729     copy_qt_qt(bevp->quat, bevp1->quat);
01730     copy_v3_v3(bevp->dir, bevp1->dir);
01731     copy_v3_v3(bevp->tan, bevp1->tan);
01732 }
01733 /* utility for make_bevel_list_3D_* funcs */
01734 static void bevel_list_calc_bisect(BevList *bl)
01735 {
01736     BevPoint *bevp2, *bevp1, *bevp0;
01737     int nr;
01738 
01739     bevp2= (BevPoint *)(bl+1);
01740     bevp1= bevp2+(bl->nr-1);
01741     bevp0= bevp1-1;
01742 
01743     nr= bl->nr;
01744     while(nr--) {
01745         /* totally simple */
01746         bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
01747 
01748         bevp0= bevp1;
01749         bevp1= bevp2;
01750         bevp2++;
01751     }
01752 }
01753 static void bevel_list_flip_tangents(BevList *bl)
01754 {
01755     BevPoint *bevp2, *bevp1, *bevp0;
01756     int nr;
01757 
01758     bevp2= (BevPoint *)(bl+1);
01759     bevp1= bevp2+(bl->nr-1);
01760     bevp0= bevp1-1;
01761 
01762     nr= bl->nr;
01763     while(nr--) {
01764         if(RAD2DEGF(angle_v2v2(bevp0->tan, bevp1->tan)) > 90.0f)
01765             negate_v3(bevp1->tan);
01766 
01767         bevp0= bevp1;
01768         bevp1= bevp2;
01769         bevp2++;
01770     }
01771 }
01772 /* apply user tilt */
01773 static void bevel_list_apply_tilt(BevList *bl)
01774 {
01775     BevPoint *bevp2, *bevp1;
01776     int nr;
01777     float q[4];
01778 
01779     bevp2= (BevPoint *)(bl+1);
01780     bevp1= bevp2+(bl->nr-1);
01781 
01782     nr= bl->nr;
01783     while(nr--) {
01784         axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
01785         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
01786         normalize_qt(bevp1->quat);
01787 
01788         bevp1= bevp2;
01789         bevp2++;
01790     }
01791 }
01792 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
01793 static void bevel_list_smooth(BevList *bl, int smooth_iter)
01794 {
01795     BevPoint *bevp2, *bevp1, *bevp0;
01796     int nr;
01797 
01798     float q[4];
01799     float bevp0_quat[4];
01800     int a;
01801 
01802     for(a=0; a < smooth_iter; a++) {
01803 
01804         bevp2= (BevPoint *)(bl+1);
01805         bevp1= bevp2+(bl->nr-1);
01806         bevp0= bevp1-1;
01807 
01808         nr= bl->nr;
01809 
01810         if(bl->poly== -1) { /* check its not cyclic */
01811             /* skip the first point */
01812             /* bevp0= bevp1; */
01813             bevp1= bevp2;
01814             bevp2++;
01815             nr--;
01816 
01817             bevp0= bevp1;
01818             bevp1= bevp2;
01819             bevp2++;
01820             nr--;
01821 
01822         }
01823 
01824         copy_qt_qt(bevp0_quat, bevp0->quat);
01825 
01826         while(nr--) {
01827             /* interpolate quats */
01828             float zaxis[3] = {0,0,1}, cross[3], q2[4];
01829             interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
01830             normalize_qt(q);
01831 
01832             mul_qt_v3(q, zaxis);
01833             cross_v3_v3v3(cross, zaxis, bevp1->dir);
01834             axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
01835             normalize_qt(q2);
01836 
01837             copy_qt_qt(bevp0_quat, bevp1->quat);
01838             mul_qt_qtqt(q, q2, q);
01839             interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
01840             normalize_qt(bevp1->quat);
01841 
01842 
01843             /* bevp0= bevp1; */ /* UNUSED */
01844             bevp1= bevp2;
01845             bevp2++;
01846         }
01847     }
01848 }
01849 
01850 static void make_bevel_list_3D_zup(BevList *bl)
01851 {
01852     BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
01853     int nr;
01854 
01855     bevp2= (BevPoint *)(bl+1);
01856     bevp1= bevp2+(bl->nr-1);
01857     bevp0= bevp1-1;
01858 
01859     nr= bl->nr;
01860     while(nr--) {
01861         /* totally simple */
01862         bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
01863         vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
01864 
01865         bevp0= bevp1;
01866         bevp1= bevp2;
01867         bevp2++;
01868     }
01869 }
01870 
01871 static void make_bevel_list_3D_minimum_twist(BevList *bl)
01872 {
01873     BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
01874     int nr;
01875     float q[4];
01876 
01877     bevel_list_calc_bisect(bl);
01878 
01879     bevp2= (BevPoint *)(bl+1);
01880     bevp1= bevp2+(bl->nr-1);
01881     bevp0= bevp1-1;
01882 
01883     nr= bl->nr;
01884     while(nr--) {
01885 
01886         if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
01887             vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
01888         }
01889         else {
01890             float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
01891 
01892             if(angle > 0.0f) { /* otherwise we can keep as is */
01893                 float cross_tmp[3];
01894                 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
01895                 axis_angle_to_quat(q, cross_tmp, angle);
01896                 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
01897             }
01898             else {
01899                 copy_qt_qt(bevp1->quat, bevp0->quat);
01900             }
01901         }
01902 
01903         bevp0= bevp1;
01904         bevp1= bevp2;
01905         bevp2++;
01906     }
01907 
01908     if(bl->poly != -1) { /* check for cyclic */
01909 
01910         /* Need to correct for the start/end points not matching
01911          * do this by calculating the tilt angle difference, then apply
01912          * the rotation gradually over the entire curve
01913          *
01914          * note that the split is between last and second last, rather than first/last as youd expect.
01915          *
01916          * real order is like this
01917          * 0,1,2,3,4 --> 1,2,3,4,0
01918          *
01919          * this is why we compare last with second last
01920          * */
01921         float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
01922 
01923         BevPoint *bevp_first;
01924         BevPoint *bevp_last;
01925 
01926 
01927         bevp_first= (BevPoint *)(bl+1);
01928         bevp_first+= bl->nr-1;
01929         bevp_last = bevp_first;
01930         bevp_last--;
01931 
01932         /* quats and vec's are normalized, should not need to re-normalize */
01933         mul_qt_v3(bevp_first->quat, vec_1);
01934         mul_qt_v3(bevp_last->quat, vec_2);
01935         normalize_v3(vec_1);
01936         normalize_v3(vec_2);
01937 
01938         /* align the vector, can avoid this and it looks 98% OK but
01939          * better to align the angle quat roll's before comparing */
01940         {
01941             cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
01942             angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
01943             axis_angle_to_quat(q, cross_tmp, angle);
01944             mul_qt_v3(q, vec_2);
01945         }
01946 
01947         angle= angle_normalized_v3v3(vec_1, vec_2);
01948 
01949         /* flip rotation if needs be */
01950         cross_v3_v3v3(cross_tmp, vec_1, vec_2);
01951         normalize_v3(cross_tmp);
01952         if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
01953             angle = -angle;
01954 
01955         bevp2= (BevPoint *)(bl+1);
01956         bevp1= bevp2+(bl->nr-1);
01957         bevp0= bevp1-1;
01958 
01959         nr= bl->nr;
01960         while(nr--) {
01961             ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
01962 
01963             axis_angle_to_quat(q, bevp1->dir, ang_fac);
01964             mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
01965 
01966             bevp0= bevp1;
01967             bevp1= bevp2;
01968             bevp2++;
01969         }
01970     }
01971 }
01972 
01973 static void make_bevel_list_3D_tangent(BevList *bl)
01974 {
01975     BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
01976     int nr;
01977 
01978     float bevp0_tan[3], cross_tmp[3];
01979 
01980     bevel_list_calc_bisect(bl);
01981     if(bl->poly== -1) /* check its not cyclic */
01982         bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
01983     bevel_list_flip_tangents(bl);
01984 
01985     /* correct the tangents */
01986     bevp2= (BevPoint *)(bl+1);
01987     bevp1= bevp2+(bl->nr-1);
01988     bevp0= bevp1-1;
01989 
01990     nr= bl->nr;
01991     while(nr--) {
01992 
01993         cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
01994         cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
01995         normalize_v3(bevp1->tan);
01996 
01997         bevp0= bevp1;
01998         bevp1= bevp2;
01999         bevp2++;
02000     }
02001 
02002 
02003     /* now for the real twist calc */
02004     bevp2= (BevPoint *)(bl+1);
02005     bevp1= bevp2+(bl->nr-1);
02006     bevp0= bevp1-1;
02007 
02008     copy_v3_v3(bevp0_tan, bevp0->tan);
02009 
02010     nr= bl->nr;
02011     while(nr--) {
02012 
02013         /* make perpendicular, modify tan in place, is ok */
02014         float cross_tmp[3];
02015         float zero[3] = {0,0,0};
02016 
02017         cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
02018         normalize_v3(cross_tmp);
02019         tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
02020 
02021         /* bevp0= bevp1; */ /* UNUSED */
02022         bevp1= bevp2;
02023         bevp2++;
02024     }
02025 }
02026 
02027 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
02028 {
02029     switch(twist_mode) {
02030     case CU_TWIST_TANGENT:
02031         make_bevel_list_3D_tangent(bl);
02032         break;
02033     case CU_TWIST_MINIMUM:
02034         make_bevel_list_3D_minimum_twist(bl);
02035         break;
02036     default: /* CU_TWIST_Z_UP default, pre 2.49c */
02037         make_bevel_list_3D_zup(bl);
02038     }
02039 
02040     if(bl->poly== -1) /* check its not cyclic */
02041         bevel_list_cyclic_fix_3D(bl);
02042 
02043     if(smooth_iter)
02044         bevel_list_smooth(bl, smooth_iter);
02045 
02046     bevel_list_apply_tilt(bl);
02047 }
02048 
02049 
02050 
02051 /* only for 2 points */
02052 static void make_bevel_list_segment_3D(BevList *bl)
02053 {
02054     float q[4];
02055 
02056     BevPoint *bevp2= (BevPoint *)(bl+1);
02057     BevPoint *bevp1= bevp2+1;
02058 
02059     /* simple quat/dir */
02060     sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
02061     normalize_v3(bevp1->dir);
02062 
02063     vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
02064 
02065     axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
02066     mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
02067     normalize_qt(bevp1->quat);
02068     copy_v3_v3(bevp2->dir, bevp1->dir);
02069     copy_qt_qt(bevp2->quat, bevp1->quat);
02070 }
02071 
02072 
02073 
02074 void makeBevelList(Object *ob)
02075 {
02076     /*
02077      - convert all curves to polys, with indication of resol and flags for double-vertices
02078      - possibly; do a smart vertice removal (in case Nurb)
02079      - separate in individual blicks with BoundBox
02080      - AutoHole detection
02081     */
02082     Curve *cu;
02083     Nurb *nu;
02084     BezTriple *bezt, *prevbezt;
02085     BPoint *bp;
02086     BevList *bl, *blnew, *blnext;
02087     BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
02088     float min, inp, x1, x2, y1, y2;
02089     struct bevelsort *sortdata, *sd, *sd1;
02090     int a, b, nr, poly, resolu = 0, len = 0;
02091     int do_tilt, do_radius, do_weight;
02092     
02093     /* this function needs an object, because of tflag and upflag */
02094     cu= ob->data;
02095 
02096     /* do we need to calculate the radius for each point? */
02097     /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
02098     
02099     /* STEP 1: MAKE POLYS  */
02100 
02101     BLI_freelistN(&(cu->bev));
02102     if(cu->editnurb && ob->type!=OB_FONT) {
02103         ListBase *nurbs= curve_editnurbs(cu);
02104         nu= nurbs->first;
02105     } else nu= cu->nurb.first;
02106     
02107     while(nu) {
02108         
02109         /* check if we will calculate tilt data */
02110         do_tilt = CU_DO_TILT(cu, nu);
02111         do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
02112         do_weight = 1;
02113         
02114         /* check we are a single point? also check we are not a surface and that the orderu is sane,
02115          * enforced in the UI but can go wrong possibly */
02116         if(!check_valid_nurb_u(nu)) {
02117             bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
02118             BLI_addtail(&(cu->bev), bl);
02119             bl->nr= 0;
02120         } else {
02121             if(G.rendering && cu->resolu_ren!=0) 
02122                 resolu= cu->resolu_ren;
02123             else
02124                 resolu= nu->resolu;
02125             
02126             if(nu->type == CU_POLY) {
02127                 len= nu->pntsu;
02128                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
02129                 BLI_addtail(&(cu->bev), bl);
02130     
02131                 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
02132                 else bl->poly= -1;
02133                 bl->nr= len;
02134                 bl->dupe_nr= 0;
02135                 bevp= (BevPoint *)(bl+1);
02136                 bp= nu->bp;
02137     
02138                 while(len--) {
02139                     copy_v3_v3(bevp->vec, bp->vec);
02140                     bevp->alfa= bp->alfa;
02141                     bevp->radius= bp->radius;
02142                     bevp->weight= bp->weight;
02143                     bevp->split_tag= TRUE;
02144                     bevp++;
02145                     bp++;
02146                 }
02147             }
02148             else if(nu->type == CU_BEZIER) {
02149     
02150                 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
02151                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
02152                 BLI_addtail(&(cu->bev), bl);
02153     
02154                 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
02155                 else bl->poly= -1;
02156                 bevp= (BevPoint *)(bl+1);
02157     
02158                 a= nu->pntsu-1;
02159                 bezt= nu->bezt;
02160                 if(nu->flagu & CU_NURB_CYCLIC) {
02161                     a++;
02162                     prevbezt= nu->bezt+(nu->pntsu-1);
02163                 }
02164                 else {
02165                     prevbezt= bezt;
02166                     bezt++;
02167                 }
02168                 
02169                 while(a--) {
02170                     if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
02171 
02172                         copy_v3_v3(bevp->vec, prevbezt->vec[1]);
02173                         bevp->alfa= prevbezt->alfa;
02174                         bevp->radius= prevbezt->radius;
02175                         bevp->weight= prevbezt->weight;
02176                         bevp->split_tag= TRUE;
02177                         bevp->dupe_tag= FALSE;
02178                         bevp++;
02179                         bl->nr++;
02180                         bl->dupe_nr= 1;
02181                     }
02182                     else {
02183                         /* always do all three, to prevent data hanging around */
02184                         int j;
02185                         
02186                         /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
02187                         for(j=0; j<3; j++) {
02188                             forward_diff_bezier(    prevbezt->vec[1][j],    prevbezt->vec[2][j],
02189                                                     bezt->vec[0][j],        bezt->vec[1][j],
02190                                                     &(bevp->vec[j]), resolu, sizeof(BevPoint));
02191                         }
02192                         
02193                         /* if both arrays are NULL do nothiong */
02194                         alfa_bezpart(   prevbezt, bezt, nu,
02195                                          do_tilt    ? &bevp->alfa : NULL,
02196                                          do_radius  ? &bevp->radius : NULL,
02197                                          do_weight  ? &bevp->weight : NULL,
02198                                          resolu, sizeof(BevPoint));
02199 
02200                         
02201                         if(cu->twist_mode==CU_TWIST_TANGENT) {
02202                             forward_diff_bezier_cotangent(
02203                                                     prevbezt->vec[1],   prevbezt->vec[2],
02204                                                     bezt->vec[0],       bezt->vec[1],
02205                                                     bevp->tan, resolu, sizeof(BevPoint));
02206                         }
02207 
02208                         /* indicate with handlecodes double points */
02209                         if(prevbezt->h1==prevbezt->h2) {
02210                             if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
02211                         }
02212                         else {
02213                             if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
02214                             else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
02215                         }
02216                         bl->nr+= resolu;
02217                         bevp+= resolu;
02218                     }
02219                     prevbezt= bezt;
02220                     bezt++;
02221                 }
02222                 
02223                 if((nu->flagu & CU_NURB_CYCLIC)==0) {       /* not cyclic: endpoint */
02224                     copy_v3_v3(bevp->vec, prevbezt->vec[1]);
02225                     bevp->alfa= prevbezt->alfa;
02226                     bevp->radius= prevbezt->radius;
02227                     bevp->weight= prevbezt->weight;
02228                     bl->nr++;
02229                 }
02230             }
02231             else if(nu->type == CU_NURBS) {
02232                 if(nu->pntsv==1) {
02233                     len= (resolu*SEGMENTSU(nu));
02234                     
02235                     bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
02236                     BLI_addtail(&(cu->bev), bl);
02237                     bl->nr= len;
02238                     bl->dupe_nr= 0;
02239                     if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
02240                     else bl->poly= -1;
02241                     bevp= (BevPoint *)(bl+1);
02242                     
02243                     makeNurbcurve(  nu, &bevp->vec[0],
02244                                     do_tilt     ? &bevp->alfa : NULL,
02245                                     do_radius   ? &bevp->radius : NULL,
02246                                     do_weight   ? &bevp->weight : NULL,
02247                                     resolu, sizeof(BevPoint));
02248                 }
02249             }
02250         }
02251         nu= nu->next;
02252     }
02253 
02254     /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
02255     bl= cu->bev.first;
02256     while(bl) {
02257         if (bl->nr) { /* null bevel items come from single points */
02258             nr= bl->nr;
02259             bevp1= (BevPoint *)(bl+1);
02260             bevp0= bevp1+(nr-1);
02261             nr--;
02262             while(nr--) {
02263                 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
02264                     if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
02265                         if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
02266                             bevp0->dupe_tag= TRUE;
02267                             bl->dupe_nr++;
02268                         }
02269                     }
02270                 }
02271                 bevp0= bevp1;
02272                 bevp1++;
02273             }
02274         }
02275         bl= bl->next;
02276     }
02277     bl= cu->bev.first;
02278     while(bl) {
02279         blnext= bl->next;
02280         if(bl->nr && bl->dupe_nr) {
02281             nr= bl->nr- bl->dupe_nr+1;  /* +1 because vectorbezier sets flag too */
02282             blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
02283             memcpy(blnew, bl, sizeof(BevList));
02284             blnew->nr= 0;
02285             BLI_remlink(&(cu->bev), bl);
02286             BLI_insertlinkbefore(&(cu->bev),blnext,blnew);  /* to make sure bevlijst is tuned with nurblist */
02287             bevp0= (BevPoint *)(bl+1);
02288             bevp1= (BevPoint *)(blnew+1);
02289             nr= bl->nr;
02290             while(nr--) {
02291                 if(bevp0->dupe_tag==0) {
02292                     memcpy(bevp1, bevp0, sizeof(BevPoint));
02293                     bevp1++;
02294                     blnew->nr++;
02295                 }
02296                 bevp0++;
02297             }
02298             MEM_freeN(bl);
02299             blnew->dupe_nr= 0;
02300         }
02301         bl= blnext;
02302     }
02303 
02304     /* STEP 3: POLYS COUNT AND AUTOHOLE */
02305     bl= cu->bev.first;
02306     poly= 0;
02307     while(bl) {
02308         if(bl->nr && bl->poly>=0) {
02309             poly++;
02310             bl->poly= poly;
02311             bl->hole= 0;
02312         }
02313         bl= bl->next;
02314     }
02315     
02316 
02317     /* find extreme left points, also test (turning) direction */
02318     if(poly>0) {
02319         sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
02320         bl= cu->bev.first;
02321         while(bl) {
02322             if(bl->poly>0) {
02323 
02324                 min= 300000.0;
02325                 bevp= (BevPoint *)(bl+1);
02326                 nr= bl->nr;
02327                 while(nr--) {
02328                     if(min>bevp->vec[0]) {
02329                         min= bevp->vec[0];
02330                         bevp1= bevp;
02331                     }
02332                     bevp++;
02333                 }
02334                 sd->bl= bl;
02335                 sd->left= min;
02336 
02337                 bevp= (BevPoint *)(bl+1);
02338                 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
02339                 else bevp0= bevp1-1;
02340                 bevp= bevp+ (bl->nr-1);
02341                 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
02342                 else bevp2= bevp1+1;
02343 
02344                 inp= (bevp1->vec[0]- bevp0->vec[0]) * (bevp0->vec[1]- bevp2->vec[1]) + (bevp0->vec[1]- bevp1->vec[1]) * (bevp0->vec[0]- bevp2->vec[0]);
02345 
02346                 if(inp > 0.0f) sd->dir= 1;
02347                 else sd->dir= 0;
02348 
02349                 sd++;
02350             }
02351 
02352             bl= bl->next;
02353         }
02354         qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
02355 
02356         sd= sortdata+1;
02357         for(a=1; a<poly; a++, sd++) {
02358             bl= sd->bl;     /* is bl a hole? */
02359             sd1= sortdata+ (a-1);
02360             for(b=a-1; b>=0; b--, sd1--) {  /* all polys to the left */
02361                 if(bevelinside(sd1->bl, bl)) {
02362                     bl->hole= 1- sd1->bl->hole;
02363                     break;
02364                 }
02365             }
02366         }
02367 
02368         /* turning direction */
02369         if((cu->flag & CU_3D)==0) {
02370             sd= sortdata;
02371             for(a=0; a<poly; a++, sd++) {
02372                 if(sd->bl->hole==sd->dir) {
02373                     bl= sd->bl;
02374                     bevp1= (BevPoint *)(bl+1);
02375                     bevp2= bevp1+ (bl->nr-1);
02376                     nr= bl->nr/2;
02377                     while(nr--) {
02378                         SWAP(BevPoint, *bevp1, *bevp2);
02379                         bevp1++;
02380                         bevp2--;
02381                     }
02382                 }
02383             }
02384         }
02385         MEM_freeN(sortdata);
02386     }
02387 
02388     /* STEP 4: 2D-COSINES or 3D ORIENTATION */
02389     if((cu->flag & CU_3D)==0) {
02390         /* note: bevp->dir and bevp->quat are not needed for beveling but are
02391          * used when making a path from a 2D curve, therefor they need to be set - Campbell */
02392         bl= cu->bev.first;
02393         while(bl) {
02394 
02395             if(bl->nr < 2) {
02396                 /* do nothing */
02397             }
02398             else if(bl->nr==2) {    /* 2 pnt, treat separate */
02399                 bevp2= (BevPoint *)(bl+1);
02400                 bevp1= bevp2+1;
02401 
02402                 x1= bevp1->vec[0]- bevp2->vec[0];
02403                 y1= bevp1->vec[1]- bevp2->vec[1];
02404 
02405                 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
02406                 bevp2->sina= bevp1->sina;
02407                 bevp2->cosa= bevp1->cosa;
02408 
02409                 /* fill in dir & quat */
02410                 make_bevel_list_segment_3D(bl);
02411             }
02412             else {
02413                 bevp2= (BevPoint *)(bl+1);
02414                 bevp1= bevp2+(bl->nr-1);
02415                 bevp0= bevp1-1;
02416 
02417                 nr= bl->nr;
02418                 while(nr--) {
02419                     x1= bevp1->vec[0]- bevp0->vec[0];
02420                     x2= bevp1->vec[0]- bevp2->vec[0];
02421                     y1= bevp1->vec[1]- bevp0->vec[1];
02422                     y2= bevp1->vec[1]- bevp2->vec[1];
02423 
02424                     calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
02425 
02426                     /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
02427                      * no need for tricky tilt calculation as with 3D curves */
02428                     bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
02429                     vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
02430                     /* done with inline make_bevel_list_3D_zup */
02431 
02432                     bevp0= bevp1;
02433                     bevp1= bevp2;
02434                     bevp2++;
02435                 }
02436 
02437                 /* correct non-cyclic cases */
02438                 if(bl->poly== -1) {
02439                     bevp= (BevPoint *)(bl+1);
02440                     bevp1= bevp+1;
02441                     bevp->sina= bevp1->sina;
02442                     bevp->cosa= bevp1->cosa;
02443                     bevp= (BevPoint *)(bl+1);
02444                     bevp+= (bl->nr-1);
02445                     bevp1= bevp-1;
02446                     bevp->sina= bevp1->sina;
02447                     bevp->cosa= bevp1->cosa;
02448 
02449                     /* correct for the dir/quat, see above why its needed */
02450                     bevel_list_cyclic_fix_3D(bl);
02451                 }
02452             }
02453             bl= bl->next;
02454         }
02455     }
02456     else { /* 3D Curves */
02457         bl= cu->bev.first;
02458         while(bl) {
02459 
02460             if(bl->nr < 2) {
02461                 /* do nothing */
02462             }
02463             else if(bl->nr==2) {    /* 2 pnt, treat separate */
02464                 make_bevel_list_segment_3D(bl);
02465             }
02466             else {
02467                 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
02468             }
02469             bl= bl->next;
02470         }
02471     }
02472 }
02473 
02474 /* ****************** HANDLES ************** */
02475 
02476 /*
02477  *   handlecodes:
02478  *      0: nothing,  1:auto,  2:vector,  3:aligned
02479  */
02480 
02481 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
02482 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
02483 {
02484     float *p1,*p2,*p3, pt[3];
02485     float dvec_a[3], dvec_b[3];
02486     float len, len_a, len_b;
02487     const float eps= 1e-5;
02488 
02489     if(bezt->h1==0 && bezt->h2==0) {
02490         return;
02491     }
02492 
02493     p2= bezt->vec[1];
02494 
02495     if(prev==NULL) {
02496         p3= next->vec[1];
02497         pt[0]= 2.0f*p2[0] - p3[0];
02498         pt[1]= 2.0f*p2[1] - p3[1];
02499         pt[2]= 2.0f*p2[2] - p3[2];
02500         p1= pt;
02501     }
02502     else {
02503         p1= prev->vec[1];
02504     }
02505 
02506     if(next==NULL) {
02507         pt[0]= 2.0f*p2[0] - p1[0];
02508         pt[1]= 2.0f*p2[1] - p1[1];
02509         pt[2]= 2.0f*p2[2] - p1[2];
02510         p3= pt;
02511     }
02512     else {
02513         p3= next->vec[1];
02514     }
02515 
02516     sub_v3_v3v3(dvec_a, p2, p1);
02517     sub_v3_v3v3(dvec_b, p3, p2);
02518 
02519     if (mode != 0) {
02520         len_a= dvec_a[0];
02521         len_b= dvec_b[0];
02522     }
02523     else {
02524         len_a= len_v3(dvec_a);
02525         len_b= len_v3(dvec_b);
02526     }
02527 
02528     if(len_a==0.0f) len_a=1.0f;
02529     if(len_b==0.0f) len_b=1.0f;
02530 
02531 
02532     if(ELEM(bezt->h1,HD_AUTO,HD_AUTO_ANIM) || ELEM(bezt->h2,HD_AUTO,HD_AUTO_ANIM)) {    /* auto */
02533         float tvec[3];
02534         tvec[0]= dvec_b[0]/len_b + dvec_a[0]/len_a;
02535         tvec[1]= dvec_b[1]/len_b + dvec_a[1]/len_a;
02536         tvec[2]= dvec_b[2]/len_b + dvec_a[2]/len_a;
02537         len= len_v3(tvec) * 2.5614f;
02538 
02539         if(len!=0.0f) {
02540             int leftviolate=0, rightviolate=0;  /* for mode==2 */
02541             
02542             if(len_a>5.0f*len_b) len_a= 5.0f*len_b;
02543             if(len_b>5.0f*len_a) len_b= 5.0f*len_a;
02544             
02545             if(ELEM(bezt->h1,HD_AUTO,HD_AUTO_ANIM)) {
02546                 len_a/=len;
02547                 madd_v3_v3v3fl(p2-3, p2, tvec, -len_a);
02548                 
02549                 if((bezt->h1==HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
02550                     float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
02551                     float ydiff2= next->vec[1][1] - bezt->vec[1][1];
02552                     if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
02553                         bezt->vec[0][1]= bezt->vec[1][1];
02554                     }
02555                     else { /* handles should not be beyond y coord of two others */
02556                         if(ydiff1 <= 0.0f) {
02557                             if(prev->vec[1][1] > bezt->vec[0][1]) {
02558                                 bezt->vec[0][1]= prev->vec[1][1]; 
02559                                 leftviolate= 1;
02560                             }
02561                         }
02562                         else {
02563                             if(prev->vec[1][1] < bezt->vec[0][1]) {
02564                                 bezt->vec[0][1]= prev->vec[1][1]; 
02565                                 leftviolate= 1;
02566                             }
02567                         }
02568                     }
02569                 }
02570             }
02571             if(ELEM(bezt->h2,HD_AUTO,HD_AUTO_ANIM)) {
02572                 len_b/=len;
02573                 madd_v3_v3v3fl(p2+3, p2, tvec,  len_b);
02574                 
02575                 if((bezt->h2==HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
02576                     float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
02577                     float ydiff2= next->vec[1][1] - bezt->vec[1][1];
02578                     if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
02579                         bezt->vec[2][1]= bezt->vec[1][1];
02580                     }
02581                     else { /* andles should not be beyond y coord of two others */
02582                         if(ydiff1 <= 0.0f) {
02583                             if(next->vec[1][1] < bezt->vec[2][1]) {
02584                                 bezt->vec[2][1]= next->vec[1][1]; 
02585                                 rightviolate= 1;
02586                             }
02587                         }
02588                         else {
02589                             if(next->vec[1][1] > bezt->vec[2][1]) {
02590                                 bezt->vec[2][1]= next->vec[1][1]; 
02591                                 rightviolate= 1;
02592                             }
02593                         }
02594                     }
02595                 }
02596             }
02597             if(leftviolate || rightviolate) { /* align left handle */
02598                 float h1[3], h2[3];
02599                 float dot;
02600                 
02601                 sub_v3_v3v3(h1, p2-3, p2);
02602                 sub_v3_v3v3(h2, p2, p2+3);
02603 
02604                 len_a= normalize_v3(h1);
02605                 len_b= normalize_v3(h2);
02606 
02607                 dot= dot_v3v3(h1, h2);
02608 
02609                 if(leftviolate) {
02610                     mul_v3_fl(h1, dot * len_b);
02611                     sub_v3_v3v3(p2+3, p2, h1);
02612                 }
02613                 else {
02614                     mul_v3_fl(h2, dot * len_a);
02615                     add_v3_v3v3(p2-3, p2, h2);
02616                 }
02617             }
02618             
02619         }
02620     }
02621 
02622     if(bezt->h1==HD_VECT) { /* vector */
02623         madd_v3_v3v3fl(p2-3, p2, dvec_a, -1.0f/3.0f);
02624     }
02625     if(bezt->h2==HD_VECT) {
02626         madd_v3_v3v3fl(p2+3, p2, dvec_b,  1.0f/3.0f);
02627     }
02628 
02629     len_b= len_v3v3(p2, p2+3);
02630     len_a= len_v3v3(p2, p2-3);
02631     if(len_a==0.0f) len_a= 1.0f;
02632     if(len_b==0.0f) len_b= 1.0f;
02633 
02634     if(bezt->f1 & SELECT) { /* order of calculation */
02635         if(bezt->h2==HD_ALIGN) { /* aligned */
02636             if(len_a>eps) {
02637                 len= len_b/len_a;
02638                 p2[3]= p2[0]+len*(p2[0] - p2[-3]);
02639                 p2[4]= p2[1]+len*(p2[1] - p2[-2]);
02640                 p2[5]= p2[2]+len*(p2[2] - p2[-1]);
02641             }
02642         }
02643         if(bezt->h1==HD_ALIGN) {
02644             if(len_b>eps) {
02645                 len= len_a/len_b;
02646                 p2[-3]= p2[0]+len*(p2[0] - p2[3]);
02647                 p2[-2]= p2[1]+len*(p2[1] - p2[4]);
02648                 p2[-1]= p2[2]+len*(p2[2] - p2[5]);
02649             }
02650         }
02651     }
02652     else {
02653         if(bezt->h1==HD_ALIGN) {
02654             if(len_b>eps) {
02655                 len= len_a/len_b;
02656                 p2[-3]= p2[0]+len*(p2[0] - p2[3]);
02657                 p2[-2]= p2[1]+len*(p2[1] - p2[4]);
02658                 p2[-1]= p2[2]+len*(p2[2] - p2[5]);
02659             }
02660         }
02661         if(bezt->h2==HD_ALIGN) {    /* aligned */
02662             if(len_a>eps) {
02663                 len= len_b/len_a;
02664                 p2[3]= p2[0]+len*(p2[0] - p2[-3]);
02665                 p2[4]= p2[1]+len*(p2[1] - p2[-2]);
02666                 p2[5]= p2[2]+len*(p2[2] - p2[-1]);
02667             }
02668         }
02669     }
02670 }
02671 
02672 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
02673 {
02674     BezTriple *bezt, *prev, *next;
02675     short a;
02676 
02677     if(nu->type != CU_BEZIER) return;
02678     if(nu->pntsu<2) return;
02679     
02680     a= nu->pntsu;
02681     bezt= nu->bezt;
02682     if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
02683     else prev= NULL;
02684     next= bezt+1;
02685 
02686     while(a--) {
02687         calchandleNurb(bezt, prev, next, 0);
02688         prev= bezt;
02689         if(a==1) {
02690             if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
02691             else next= NULL;
02692         }
02693         else next++;
02694 
02695         bezt++;
02696     }
02697 }
02698 
02699 
02700 void testhandlesNurb(Nurb *nu)
02701 {
02702     /* use when something has changed with handles.
02703     it treats all BezTriples with the following rules:
02704     PHASE 1: do types have to be altered?
02705        Auto handles: become aligned when selection status is NOT(000 || 111)
02706        Vector handles: become 'nothing' when (one half selected AND other not)
02707     PHASE 2: recalculate handles
02708     */
02709     BezTriple *bezt;
02710     short flag, a;
02711 
02712     if(nu->type != CU_BEZIER) return;
02713 
02714     bezt= nu->bezt;
02715     a= nu->pntsu;
02716     while(a--) {
02717         flag= 0;
02718         if(bezt->f1 & SELECT) flag++;
02719         if(bezt->f2 & SELECT) flag += 2;
02720         if(bezt->f3 & SELECT) flag += 4;
02721         
02722         if( !(flag==0 || flag==7) ) {
02723             if(ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {   /* auto */
02724                 bezt->h1= HD_ALIGN;
02725             }
02726             if(ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {   /* auto */
02727                 bezt->h2= HD_ALIGN;
02728             }
02729             
02730             if(bezt->h1==HD_VECT) {   /* vector */
02731                 if(flag < 4) bezt->h1= 0;
02732             }
02733             if(bezt->h2==HD_VECT) {   /* vector */
02734                 if( flag > 3) bezt->h2= 0;
02735             }
02736         }
02737         bezt++;
02738     }
02739     
02740     calchandlesNurb(nu);
02741 }
02742 
02743 void autocalchandlesNurb(Nurb *nu, int flag)
02744 {
02745     /* checks handle coordinates and calculates type */
02746     
02747     BezTriple *bezt2, *bezt1, *bezt0;
02748     int i, align, leftsmall, rightsmall;
02749 
02750     if(nu==NULL || nu->bezt==NULL) return;
02751     
02752     bezt2 = nu->bezt;
02753     bezt1 = bezt2 + (nu->pntsu-1);
02754     bezt0 = bezt1 - 1;
02755     i = nu->pntsu;
02756 
02757     while(i--) {
02758         
02759         align= leftsmall= rightsmall= 0;
02760         
02761         /* left handle: */
02762         if(flag==0 || (bezt1->f1 & flag) ) {
02763             bezt1->h1= 0;
02764             /* distance too short: vectorhandle */
02765             if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001f) {
02766                 bezt1->h1= HD_VECT;
02767                 leftsmall= 1;
02768             }
02769             else {
02770                 /* aligned handle? */
02771                 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001f) {
02772                     align= 1;
02773                     bezt1->h1= HD_ALIGN;
02774                 }
02775                 /* or vector handle? */
02776                 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001f)
02777                     bezt1->h1= HD_VECT;
02778                 
02779             }
02780         }
02781         /* right handle: */
02782         if(flag==0 || (bezt1->f3 & flag) ) {
02783             bezt1->h2= 0;
02784             /* distance too short: vectorhandle */
02785             if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001f) {
02786                 bezt1->h2= HD_VECT;
02787                 rightsmall= 1;
02788             }
02789             else {
02790                 /* aligned handle? */
02791                 if(align) bezt1->h2= HD_ALIGN;
02792 
02793                 /* or vector handle? */
02794                 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001f)
02795                     bezt1->h2= HD_VECT;
02796                 
02797             }
02798         }
02799         if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
02800         if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
02801         
02802         /* undesired combination: */
02803         if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
02804         if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
02805         
02806         bezt0= bezt1;
02807         bezt1= bezt2;
02808         bezt2++;
02809     }
02810 
02811     calchandlesNurb(nu);
02812 }
02813 
02814 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
02815 {
02816     Nurb *nu;
02817     
02818     nu= editnurb->first;
02819     while(nu) {
02820         autocalchandlesNurb(nu, flag);
02821         nu= nu->next;
02822     }
02823 }
02824 
02825 void sethandlesNurb(ListBase *editnurb, short code)
02826 {
02827     /* code==1: set autohandle */
02828     /* code==2: set vectorhandle */
02829     /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
02830     /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
02831     /* code==5: Set align, like 3 but no toggle */
02832     /* code==6: Clear align, like 3 but no toggle */
02833     Nurb *nu;
02834     BezTriple *bezt;
02835     short a, ok=0;
02836 
02837     if(code==1 || code==2) {
02838         nu= editnurb->first;
02839         while(nu) {
02840             if(nu->type == CU_BEZIER) {
02841                 bezt= nu->bezt;
02842                 a= nu->pntsu;
02843                 while(a--) {
02844                     if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
02845                         if(bezt->f1 & SELECT) bezt->h1= code;
02846                         if(bezt->f3 & SELECT) bezt->h2= code;
02847                         if(bezt->h1!=bezt->h2) {
02848                             if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
02849                             if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
02850                         }
02851                     }
02852                     bezt++;
02853                 }
02854                 calchandlesNurb(nu);
02855             }
02856             nu= nu->next;
02857         }
02858     }
02859     else {
02860         /* there is 1 handle not FREE: FREE it all, else make ALIGNED  */
02861         
02862         nu= editnurb->first;
02863         if (code == 5) {
02864             ok = HD_ALIGN;
02865         } else if (code == 6) {
02866             ok = HD_FREE;
02867         } else {
02868             /* Toggle */
02869             while(nu) {
02870                 if(nu->type == CU_BEZIER) {
02871                     bezt= nu->bezt;
02872                     a= nu->pntsu;
02873                     while(a--) {
02874                         if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
02875                         if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
02876                         if(ok) break;
02877                         bezt++;
02878                     }
02879                 }
02880                 nu= nu->next;
02881             }
02882             if(ok) ok= HD_FREE;
02883             else ok= HD_ALIGN;
02884         }
02885         nu= editnurb->first;
02886         while(nu) {
02887             if(nu->type == CU_BEZIER) {
02888                 bezt= nu->bezt;
02889                 a= nu->pntsu;
02890                 while(a--) {
02891                     if(bezt->f1 & SELECT) bezt->h1= ok;
02892                     if(bezt->f3 & SELECT) bezt->h2= ok;
02893     
02894                     bezt++;
02895                 }
02896                 calchandlesNurb(nu);
02897             }
02898             nu= nu->next;
02899         }
02900     }
02901 }
02902 
02903 static void swapdata(void *adr1, void *adr2, int len)
02904 {
02905 
02906     if(len<=0) return;
02907 
02908     if(len<65) {
02909         char adr[64];
02910 
02911         memcpy(adr, adr1, len);
02912         memcpy(adr1, adr2, len);
02913         memcpy(adr2, adr, len);
02914     }
02915     else {
02916         char *adr;
02917 
02918         adr= (char *)MEM_mallocN(len, "curve swap");
02919         memcpy(adr, adr1, len);
02920         memcpy(adr1, adr2, len);
02921         memcpy(adr2, adr, len);
02922         MEM_freeN(adr);
02923     }
02924 }
02925 
02926 void switchdirectionNurb(Nurb *nu)
02927 {
02928     BezTriple *bezt1, *bezt2;
02929     BPoint *bp1, *bp2;
02930     float *fp1, *fp2, *tempf;
02931     int a, b;
02932 
02933     if(nu->pntsu==1 && nu->pntsv==1) return;
02934 
02935     if(nu->type == CU_BEZIER) {
02936         a= nu->pntsu;
02937         bezt1= nu->bezt;
02938         bezt2= bezt1+(a-1);
02939         if(a & 1) a+= 1;    /* if odd, also swap middle content */
02940         a/= 2;
02941         while(a>0) {
02942             if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
02943 
02944             swapdata(bezt1->vec[0], bezt1->vec[2], 12);
02945             if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
02946 
02947             SWAP(char, bezt1->h1, bezt1->h2);
02948             SWAP(short, bezt1->f1, bezt1->f3);
02949             
02950             if(bezt1!=bezt2) {
02951                 SWAP(char, bezt2->h1, bezt2->h2);
02952                 SWAP(short, bezt2->f1, bezt2->f3);
02953                 bezt1->alfa= -bezt1->alfa;
02954                 bezt2->alfa= -bezt2->alfa;
02955             }
02956             a--;
02957             bezt1++; 
02958             bezt2--;
02959         }
02960     }
02961     else if(nu->pntsv==1) {
02962         a= nu->pntsu;
02963         bp1= nu->bp;
02964         bp2= bp1+(a-1);
02965         a/= 2;
02966         while(bp1!=bp2 && a>0) {
02967             SWAP(BPoint, *bp1, *bp2);
02968             a--;
02969             bp1->alfa= -bp1->alfa;
02970             bp2->alfa= -bp2->alfa;
02971             bp1++; 
02972             bp2--;
02973         }
02974         if(nu->type == CU_NURBS) {
02975             /* no knots for too short paths */
02976             if(nu->knotsu) {
02977                 /* inverse knots */
02978                 a= KNOTSU(nu);
02979                 fp1= nu->knotsu;
02980                 fp2= fp1+(a-1);
02981                 a/= 2;
02982                 while(fp1!=fp2 && a>0) {
02983                     SWAP(float, *fp1, *fp2);
02984                     a--;
02985                     fp1++; 
02986                     fp2--;
02987                 }
02988                 /* and make in increasing order again */
02989                 a= KNOTSU(nu);
02990                 fp1= nu->knotsu;
02991                 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
02992                 while(a--) {
02993                     fp2[0]= fabs(fp1[1]-fp1[0]);
02994                     fp1++;
02995                     fp2++;
02996                 }
02997         
02998                 a= KNOTSU(nu)-1;
02999                 fp1= nu->knotsu;
03000                 fp2= tempf;
03001                 fp1[0]= 0.0;
03002                 fp1++;
03003                 while(a--) {
03004                     fp1[0]= fp1[-1]+fp2[0];
03005                     fp1++;
03006                     fp2++;
03007                 }
03008                 MEM_freeN(tempf);
03009             }
03010         }
03011     }
03012     else {
03013         
03014         for(b=0; b<nu->pntsv; b++) {
03015         
03016             bp1= nu->bp+b*nu->pntsu;
03017             a= nu->pntsu;
03018             bp2= bp1+(a-1);
03019             a/= 2;
03020             
03021             while(bp1!=bp2 && a>0) {
03022                 SWAP(BPoint, *bp1, *bp2);
03023                 a--;
03024                 bp1++; 
03025                 bp2--;
03026             }
03027         }
03028     }
03029 }
03030 
03031 
03032 float (*curve_getVertexCos(Curve *UNUSED(cu), ListBase *lb, int *numVerts_r))[3]
03033 {
03034     int i, numVerts = *numVerts_r = count_curveverts(lb);
03035     float *co, (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "cu_vcos");
03036     Nurb *nu;
03037 
03038     co = cos[0];
03039     for (nu=lb->first; nu; nu=nu->next) {
03040         if (nu->type == CU_BEZIER) {
03041             BezTriple *bezt = nu->bezt;
03042 
03043             for (i=0; i<nu->pntsu; i++,bezt++) {
03044                 copy_v3_v3(co, bezt->vec[0]); co+=3;
03045                 copy_v3_v3(co, bezt->vec[1]); co+=3;
03046                 copy_v3_v3(co, bezt->vec[2]); co+=3;
03047             }
03048         } else {
03049             BPoint *bp = nu->bp;
03050 
03051             for (i=0; i<nu->pntsu*nu->pntsv; i++,bp++) {
03052                 copy_v3_v3(co, bp->vec); co+=3;
03053             }
03054         }
03055     }
03056 
03057     return cos;
03058 }
03059 
03060 void curve_applyVertexCos(Curve *UNUSED(cu), ListBase *lb, float (*vertexCos)[3])
03061 {
03062     float *co = vertexCos[0];
03063     Nurb *nu;
03064     int i;
03065 
03066     for (nu=lb->first; nu; nu=nu->next) {
03067         if (nu->type == CU_BEZIER) {
03068             BezTriple *bezt = nu->bezt;
03069 
03070             for (i=0; i<nu->pntsu; i++,bezt++) {
03071                 copy_v3_v3(bezt->vec[0], co); co+=3;
03072                 copy_v3_v3(bezt->vec[1], co); co+=3;
03073                 copy_v3_v3(bezt->vec[2], co); co+=3;
03074             }
03075         } else {
03076             BPoint *bp = nu->bp;
03077 
03078             for (i=0; i<nu->pntsu*nu->pntsv; i++,bp++) {
03079                 copy_v3_v3(bp->vec, co); co+=3;
03080             }
03081         }
03082     }
03083 }
03084 
03085 float (*curve_getKeyVertexCos(Curve *UNUSED(cu), ListBase *lb, float *key))[3]
03086 {
03087     int i, numVerts = count_curveverts(lb);
03088     float *co, (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "cu_vcos");
03089     Nurb *nu;
03090 
03091     co = cos[0];
03092     for (nu=lb->first; nu; nu=nu->next) {
03093         if (nu->type == CU_BEZIER) {
03094             BezTriple *bezt = nu->bezt;
03095 
03096             for (i=0; i<nu->pntsu; i++,bezt++) {
03097                 copy_v3_v3(co, key); co+=3; key+=3;
03098                 copy_v3_v3(co, key); co+=3; key+=3;
03099                 copy_v3_v3(co, key); co+=3; key+=3;
03100                 key+=3; /* skip tilt */
03101             }
03102         }
03103         else {
03104             BPoint *bp = nu->bp;
03105 
03106             for(i=0; i<nu->pntsu*nu->pntsv; i++,bp++) {
03107                 copy_v3_v3(co, key); co+=3; key+=3;
03108                 key++; /* skip tilt */
03109             }
03110         }
03111     }
03112 
03113     return cos;
03114 }
03115 
03116 void curve_applyKeyVertexTilts(Curve *UNUSED(cu), ListBase *lb, float *key)
03117 {
03118     Nurb *nu;
03119     int i;
03120 
03121     for(nu=lb->first; nu; nu=nu->next) {
03122         if(nu->type == CU_BEZIER) {
03123             BezTriple *bezt = nu->bezt;
03124 
03125             for(i=0; i<nu->pntsu; i++,bezt++) {
03126                 key+=3*3;
03127                 bezt->alfa= *key;
03128                 key+=3;
03129             }
03130         }
03131         else {
03132             BPoint *bp = nu->bp;
03133 
03134             for(i=0; i<nu->pntsu*nu->pntsv; i++,bp++) {
03135                 key+=3;
03136                 bp->alfa= *key;
03137                 key++;
03138             }
03139         }
03140     }
03141 }
03142 
03143 int check_valid_nurb_u( struct Nurb *nu )
03144 {
03145     if (nu==NULL)                       return 0;
03146     if (nu->pntsu <= 1)                 return 0;
03147     if (nu->type != CU_NURBS)           return 1; /* not a nurb, lets assume its valid */
03148     
03149     if (nu->pntsu < nu->orderu)         return 0;
03150     if (((nu->flag & CU_NURB_CYCLIC)==0) && (nu->flagu & CU_NURB_BEZIER)) { /* Bezier U Endpoints */
03151         if (nu->orderu==4) {
03152             if (nu->pntsu < 5)          return 0; /* bezier with 4 orderu needs 5 points */
03153         } else if (nu->orderu != 3)     return 0; /* order must be 3 or 4 */
03154     }
03155     return 1;
03156 }
03157 int check_valid_nurb_v( struct Nurb *nu)
03158 {
03159     if (nu==NULL)                       return 0;
03160     if (nu->pntsv <= 1)                 return 0;
03161     if (nu->type != CU_NURBS)           return 1; /* not a nurb, lets assume its valid */
03162     
03163     if (nu->pntsv < nu->orderv)         return 0;
03164     if (((nu->flag & CU_NURB_CYCLIC)==0) && (nu->flagv & CU_NURB_BEZIER)) { /* Bezier V Endpoints */
03165         if (nu->orderv==4) {
03166             if (nu->pntsv < 5)          return 0; /* bezier with 4 orderu needs 5 points */
03167         } else if (nu->orderv != 3)     return 0; /* order must be 3 or 4 */
03168     }
03169     return 1;
03170 }
03171 
03172 int clamp_nurb_order_u( struct Nurb *nu )
03173 {
03174     int change = 0;
03175     if(nu->pntsu<nu->orderu) {
03176         nu->orderu= nu->pntsu;
03177         change= 1;
03178     }
03179     if(((nu->flagu & CU_NURB_CYCLIC)==0) && (nu->flagu & CU_NURB_BEZIER)) {
03180         CLAMP(nu->orderu, 3,4);
03181         change= 1;
03182     }
03183     return change;
03184 }
03185 
03186 int clamp_nurb_order_v( struct Nurb *nu)
03187 {
03188     int change = 0;
03189     if(nu->pntsv<nu->orderv) {
03190         nu->orderv= nu->pntsv;
03191         change= 1;
03192     }
03193     if(((nu->flagv & CU_NURB_CYCLIC)==0) && (nu->flagv & CU_NURB_BEZIER)) {
03194         CLAMP(nu->orderv, 3,4);
03195         change= 1;
03196     }
03197     return change;
03198 }
03199 
03200 /* Get edit nurbs or normal nurbs list */
03201 ListBase *BKE_curve_nurbs(Curve *cu)
03202 {
03203     if (cu->editnurb) {
03204         return curve_editnurbs(cu);
03205     }
03206 
03207     return &cu->nurb;
03208 }
03209 
03210 
03211 /* basic vertex data functions */
03212 int minmax_curve(Curve *cu, float min[3], float max[3])
03213 {
03214     ListBase *nurb_lb= BKE_curve_nurbs(cu);
03215     Nurb *nu;
03216 
03217     for(nu= nurb_lb->first; nu; nu= nu->next)
03218         minmaxNurb(nu, min, max);
03219 
03220     return (nurb_lb->first != NULL);
03221 }
03222 
03223 int curve_center_median(Curve *cu, float cent[3])
03224 {
03225     ListBase *nurb_lb= BKE_curve_nurbs(cu);
03226     Nurb *nu;
03227     int total= 0;
03228 
03229     zero_v3(cent);
03230 
03231     for(nu= nurb_lb->first; nu; nu= nu->next) {
03232         int i;
03233 
03234         if(nu->type == CU_BEZIER) {
03235             BezTriple *bezt;
03236             i= nu->pntsu;
03237             total += i * 3;
03238             for(bezt= nu->bezt; i--; bezt++) {
03239                 add_v3_v3(cent, bezt->vec[0]);
03240                 add_v3_v3(cent, bezt->vec[1]);
03241                 add_v3_v3(cent, bezt->vec[2]);
03242             }
03243         }
03244         else {
03245             BPoint *bp;
03246             i= nu->pntsu*nu->pntsv;
03247             total += i;
03248             for(bp= nu->bp; i--; bp++) {
03249                 add_v3_v3(cent, bp->vec);
03250             }
03251         }
03252     }
03253 
03254     mul_v3_fl(cent, 1.0f/(float)total);
03255 
03256     return (total != 0);
03257 }
03258 
03259 int curve_center_bounds(Curve *cu, float cent[3])
03260 {
03261     float min[3], max[3];
03262     INIT_MINMAX(min, max);
03263     if(minmax_curve(cu, min, max)) {
03264         mid_v3_v3v3(cent, min, max);
03265         return 1;
03266     }
03267 
03268     return 0;
03269 }
03270 
03271 void curve_translate(Curve *cu, float offset[3], int do_keys)
03272 {
03273     ListBase *nurb_lb= BKE_curve_nurbs(cu);
03274     Nurb *nu;
03275     int i;
03276 
03277     for(nu= nurb_lb->first; nu; nu= nu->next) {
03278         BezTriple *bezt;
03279         BPoint *bp;
03280 
03281         if(nu->type == CU_BEZIER) {
03282             i= nu->pntsu;
03283             for(bezt= nu->bezt; i--; bezt++) {
03284                 add_v3_v3(bezt->vec[0], offset);
03285                 add_v3_v3(bezt->vec[1], offset);
03286                 add_v3_v3(bezt->vec[2], offset);
03287             }
03288         }
03289         else {
03290             i= nu->pntsu*nu->pntsv;
03291             for(bp= nu->bp; i--; bp++) {
03292                 add_v3_v3(bp->vec, offset);
03293             }
03294         }
03295     }
03296 
03297     if (do_keys && cu->key) {
03298         KeyBlock *kb;
03299         for (kb=cu->key->block.first; kb; kb=kb->next) {
03300             float *fp= kb->data;
03301             for (i= kb->totelem; i--; fp+=3) {
03302                 add_v3_v3(fp, offset);
03303             }
03304         }
03305     }
03306 }
03307 
03308 void curve_delete_material_index(Curve *cu, int index)
03309 {
03310     const int curvetype= curve_type(cu);
03311 
03312     if(curvetype == OB_FONT) {
03313         struct CharInfo *info= cu->strinfo;
03314         int i;
03315         for(i= cu->len-1; i >= 0; i--, info++) {
03316             if (info->mat_nr && info->mat_nr>=index) {
03317                 info->mat_nr--;
03318             }
03319         }
03320     }
03321     else {
03322         Nurb *nu;
03323 
03324         for (nu= cu->nurb.first; nu; nu= nu->next) {
03325             if(nu->mat_nr && nu->mat_nr>=index) {
03326                 nu->mat_nr--;
03327                 if (curvetype == OB_CURVE) nu->charidx--;
03328             }
03329         }
03330     }
03331 }