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