svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20855:20928
[blender.git] / source / blender / blenkernel / intern / curve.c
index c29a92558d644a21eeac5c57efd317efa8e1a799..7dd868278f4c2f71716fcd8c7aec88d9aeb44a06 100644 (file)
 
 /* for dereferencing pointers */
 #include "DNA_ID.h"  
-#include "DNA_vfont_types.h"  
 #include "DNA_key_types.h"  
-#include "DNA_ipo_types.h"  
+#include "DNA_scene_types.h"  
+#include "DNA_vfont_types.h"  
 
-#include "BKE_global.h" 
-#include "BKE_main.h"  
-#include "BKE_utildefines.h"  // VECCOPY
-#include "BKE_object.h"  
-#include "BKE_mesh.h" 
+#include "BKE_animsys.h"
+#include "BKE_anim.h"  
 #include "BKE_curve.h"  
 #include "BKE_displist.h"  
-#include "BKE_ipo.h"  
-#include "BKE_anim.h"  
-#include "BKE_library.h"  
+#include "BKE_font.h" 
+#include "BKE_global.h" 
 #include "BKE_key.h"  
+#include "BKE_library.h"  
+#include "BKE_main.h"  
+#include "BKE_mesh.h" 
+#include "BKE_object.h"  
+#include "BKE_utildefines.h"  // VECCOPY
 
 
 /* globals */
 
-extern ListBase editNurb;  /* editcurve.c */
-
 /* local */
 int cu_isectLL(float *v1, float *v2, float *v3, float *v4, 
                           short cox, short coy, 
@@ -86,20 +85,42 @@ void unlink_curve(Curve *cu)
        cu->vfont= 0;
        if(cu->key) cu->key->id.us--;
        cu->key= 0;
-       if(cu->ipo) cu->ipo->id.us--;
-       cu->ipo= 0;
 }
 
+/* frees editcurve entirely */
+void BKE_free_editfont(Curve *cu)
+{
+       if(cu->editfont) {
+               EditFont *ef= cu->editfont;
+               
+               if(ef->oldstr) MEM_freeN(ef->oldstr);
+               if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
+               if(ef->textbuf) MEM_freeN(ef->textbuf);
+               if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
+               if(ef->copybuf) MEM_freeN(ef->copybuf);
+               if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
+               
+               MEM_freeN(ef);
+               cu->editfont= NULL;
+       }
+}
 
-/* niet curve zelf vrijgeven */
+/* don't free curve itself */
 void free_curve(Curve *cu)
 {
-
        freeNurblist(&cu->nurb);
        BLI_freelistN(&cu->bev);
        freedisplist(&cu->disp);
+       BKE_free_editfont(cu);
        
+       if(cu->editnurb) {
+               freeNurblist(cu->editnurb);
+               MEM_freeN(cu->editnurb);
+               cu->editnurb= NULL;
+       }
+
        unlink_curve(cu);
+       BKE_free_animdata((ID *)cu);
        
        if(cu->mat) MEM_freeN(cu->mat);
        if(cu->str) MEM_freeN(cu->str);
@@ -128,6 +149,18 @@ Curve *add_curve(char *name, int type)
        
        cu->bb= unit_boundbox();
        
+       if(type==OB_FONT) {
+               cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
+               cu->vfont->id.us+=4;
+               cu->str= MEM_mallocN(12, "str");
+               strcpy(cu->str, "Text");
+               cu->pos= 4;
+               cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
+               cu->totbox= cu->actbox= 1;
+               cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
+               cu->tb[0].w = cu->tb[0].h = 0.0;
+       }
+       
        return cu;
 }
 
@@ -157,8 +190,12 @@ Curve *copy_curve(Curve *cu)
        cun->bev.first= cun->bev.last= 0;
        cun->path= 0;
 
+       cun->editnurb= NULL;
+
+#if 0  // XXX old animation system
        /* single user ipo too */
        if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
+#endif // XXX old animation system
 
        id_us_plus((ID *)cun->vfont);
        id_us_plus((ID *)cun->vfontb);  
@@ -554,8 +591,8 @@ static void makecyclicknots(float *knots, short pnts, short order)
 }
 
 
-/* type - 0: uniform, 1: endpoints, 2: bezier, note, cyclic nurbs are always uniform */
-void makeknots(Nurb *nu, short uv, short type)
+
+void makeknots(Nurb *nu, short uv)
 {
        if( (nu->type & 7)==CU_NURBS ) {
                if(uv == 1) {
@@ -566,7 +603,7 @@ void makeknots(Nurb *nu, short uv, short type)
                                        calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0);  /* cyclic should be uniform */
                                        makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
                                } else {
-                                       calcknots(nu->knotsu, nu->pntsu, nu->orderu, type);
+                                       calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu>>1);
                                }
                        }
                        else nu->knotsu= NULL;
@@ -579,7 +616,7 @@ void makeknots(Nurb *nu, short uv, short type)
                                        calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0);  /* cyclic should be uniform */
                                        makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
                                } else {
-                                       calcknots(nu->knotsv, nu->pntsv, nu->orderv, type);
+                                       calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv>>1);
                                }
                        }
                        else nu->knotsv= NULL;
@@ -832,7 +869,8 @@ void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radiu
        if(len==0) return;
        sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
        
-       resolu= (resolu*SEGMENTSU(nu))+1;
+       resolu= (resolu*SEGMENTSU(nu));
+       
        if(resolu==0) {
                MEM_freeN(sum);
                return;
@@ -842,7 +880,8 @@ void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radiu
        ustart= fp[nu->orderu-1];
        if(nu->flagu & CU_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
        else uend= fp[nu->pntsu];
-       ustep= (uend-ustart)/(resolu-1);
+       ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_CYCLIC) ? 0 : 1));
+       
        basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
 
        if(nu->flagu & CU_CYCLIC) cycl= nu->orderu-1; 
@@ -1031,7 +1070,7 @@ float *make_orco_surf(Object *ob)
        /* NOTE: This routine is tied to the order of vertex
         * built by displist and as passed to the renderer.
         */
-float *make_orco_curve(Object *ob)
+float *make_orco_curve(Scene *scene, Object *ob)
 {
        Curve *cu = ob->data;
        DispList *dl;
@@ -1041,7 +1080,7 @@ float *make_orco_curve(Object *ob)
 
        if (!(cu->flag&CU_UV_ORCO) && cu->key && cu->key->refkey) {
                cp_cu_key(cu, cu->key->refkey, 0, count_curveverts(&cu->nurb));
-               makeDispListCurveTypes(ob, 1);
+               makeDispListCurveTypes(scene, ob, 1);
                remakeDisp = 1;
        }
 
@@ -1097,9 +1136,12 @@ float *make_orco_curve(Object *ob)
                                                fp[1]= 2.0f*v/(dl->nr-1) - 1.0f;
                                                fp[2]= 0.0;
                                        } else {
+                                               float *vert;
                                                int realv= v % dl->nr;
-
-                                               VECCOPY(fp, &dl->verts[(dl->nr*u + realv)*3]);
+                                               int realu= u % dl->parts;
+                                               
+                                               vert= dl->verts + 3*(dl->nr*realu + realv);
+                                               VECCOPY(fp, vert);
 
                                                fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
                                                fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
@@ -1111,7 +1153,7 @@ float *make_orco_curve(Object *ob)
        }
 
        if (remakeDisp) {
-               makeDispListCurveTypes(ob, 0);
+               makeDispListCurveTypes(scene, ob, 0);
        }
 
        return coord_array;
@@ -1120,7 +1162,7 @@ float *make_orco_curve(Object *ob)
 
 /* ***************** BEVEL ****************** */
 
-void makebevelcurve(Object *ob, ListBase *disp)
+void makebevelcurve(Scene *scene, Object *ob, ListBase *disp)
 {
        DispList *dl, *dlnew;
        Curve *bevcu, *cu;
@@ -1131,7 +1173,7 @@ void makebevelcurve(Object *ob, ListBase *disp)
        disp->first = disp->last = NULL;
 
        /* if a font object is being edited, then do nothing */
-       if( ob == G.obedit && ob->type == OB_FONT ) return;
+// XXX if( ob == obedit && ob->type == OB_FONT ) return;
 
        if(cu->bevobj && cu->bevobj!=ob) {
                if(cu->bevobj->type==OB_CURVE) {
@@ -1142,7 +1184,7 @@ void makebevelcurve(Object *ob, ListBase *disp)
 
                                dl= bevcu->disp.first;
                                if(dl==0) {
-                                       makeDispListCurveTypes(cu->bevobj, 0);
+                                       makeDispListCurveTypes(scene, cu->bevobj, 0);
                                        dl= bevcu->disp.first;
                                }
                                while(dl) {
@@ -1524,14 +1566,14 @@ void makeBevelList(Object *ob)
        /* STEP 1: MAKE POLYS  */
 
        BLI_freelistN(&(cu->bev));
-       if(ob==G.obedit && ob->type!=OB_FONT) nu= editNurb.first;
+       if(cu->editnurb && ob->type!=OB_FONT) nu= cu->editnurb->first;
        else nu= cu->nurb.first;
        
        while(nu) {
                
                /* check if we will calculate tilt data */
-               do_tilt = ((nu->type & CU_2D) && (cu->flag & CU_3D)==0) ? 0 : 1;
-               do_radius = (do_tilt || cu->bevobj) ? 1 : 0; /* normal display uses the radius, better just to calculate them */
+               do_tilt = CU_DO_TILT(cu, nu);
+               do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
                
                /* check we are a single point? also check we are not a surface and that the orderu is sane,
                 * enforced in the UI but can go wrong possibly */
@@ -1678,7 +1720,8 @@ void makeBevelList(Object *ob)
                        }
                        else if((nu->type & 7)==CU_NURBS) {
                                if(nu->pntsv==1) {
-                                       len= (resolu*SEGMENTSU(nu))+1;
+                                       len= (resolu*SEGMENTSU(nu));
+                                       
                                        bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
                                        BLI_addtail(&(cu->bev), bl);
                                        bl->nr= len;
@@ -1970,7 +2013,7 @@ void makeBevelList(Object *ob)
  *             1: nothing,  1:auto,  2:vector,  3:aligned
  */
 
-/* mode: is not zero when IpoCurve, is 2 when forced horizontal for autohandles */
+/* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
 {
        float *p1,*p2,*p3, pt[3];
@@ -2301,18 +2344,18 @@ void autocalchandlesNurb(Nurb *nu, int flag)
        calchandlesNurb(nu);
 }
 
-void autocalchandlesNurb_all(int flag)
+void autocalchandlesNurb_all(ListBase *editnurb, int flag)
 {
        Nurb *nu;
        
-       nu= editNurb.first;
+       nu= editnurb->first;
        while(nu) {
                autocalchandlesNurb(nu, flag);
                nu= nu->next;
        }
 }
 
-void sethandlesNurb(short code)
+void sethandlesNurb(ListBase *editnurb, short code)
 {
        /* code==1: set autohandle */
        /* code==2: set vectorhandle */
@@ -2325,9 +2368,9 @@ void sethandlesNurb(short code)
        short a, ok=0;
 
        if(code==1 || code==2) {
-               nu= editNurb.first;
+               nu= editnurb->first;
                while(nu) {
-                       if( (nu->type & 7)==1) {
+                       if( (nu->type & 7)==CU_BEZIER) {
                                bezt= nu->bezt;
                                a= nu->pntsu;
                                while(a--) {
@@ -2349,7 +2392,7 @@ void sethandlesNurb(short code)
        else {
                /* there is 1 handle not FREE: FREE it all, else make ALIGNED  */
                
-               nu= editNurb.first;
+               nu= editnurb->first;
                if (code == 5) {
                        ok = HD_ALIGN;
                } else if (code == 6) {
@@ -2357,7 +2400,7 @@ void sethandlesNurb(short code)
                } else {
                        /* Toggle */
                        while(nu) {
-                               if( (nu->type & 7)==1) {
+                               if( (nu->type & 7)==CU_BEZIER) {
                                        bezt= nu->bezt;
                                        a= nu->pntsu;
                                        while(a--) {
@@ -2372,9 +2415,9 @@ void sethandlesNurb(short code)
                        if(ok) ok= HD_FREE;
                        else ok= HD_ALIGN;
                }
-               nu= editNurb.first;
+               nu= editnurb->first;
                while(nu) {
-                       if( (nu->type & 7)==1) {
+                       if( (nu->type & 7)==CU_BEZIER) {
                                bezt= nu->bezt;
                                a= nu->pntsu;
                                while(a--) {