- added Mesh->derived and Mesh->decimated DerivedMesh pointers
[blender.git] / source / blender / blenkernel / intern / mesh.c
1
2 /*  mesh.c
3  *
4  *  
5  * 
6  * $Id$
7  *
8  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version. The Blender
14  * Foundation also sells licenses for use in proprietary software under
15  * the Blender License.  See http://www.blender.org/BL/ for information
16  * about this.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28  * All rights reserved.
29  *
30  * The Original Code is: all of this file.
31  *
32  * Contributor(s): none yet.
33  *
34  * ***** END GPL/BL DUAL LICENSE BLOCK *****
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <math.h>
45
46 #include "MEM_guardedalloc.h"
47
48 #include "DNA_ID.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_material_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_image_types.h"
53 #include "DNA_key_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meshdata_types.h"
56
57 #include "BKE_main.h"
58 #include "BKE_DerivedMesh.h"
59 #include "BKE_global.h"
60 #include "BKE_mesh.h"
61 #include "BKE_subsurf.h"
62 #include "BKE_displist.h"
63 #include "BKE_library.h"
64 #include "BKE_material.h"
65 #include "BKE_key.h"
66 /* these 2 are only used by conversion functions */
67 #include "BKE_curve.h"
68 /* -- */
69 #include "BKE_object.h"
70 #include "BKE_utildefines.h"
71 #include "BKE_bad_level_calls.h"
72
73 #include "BLI_blenlib.h"
74 #include "BLI_editVert.h"
75 #include "BLI_arithb.h"
76
77
78
79 int update_realtime_texture(TFace *tface, double time)
80 {
81         Image *ima;
82         int     inc = 0;
83         float   diff;
84         int     newframe;
85
86         ima = tface->tpage;
87
88         if (!ima)
89                 return 0;
90
91         if (ima->lastupdate<0)
92                 ima->lastupdate = 0;
93
94         if (ima->lastupdate>time)
95                 ima->lastupdate=(float)time;
96
97         if(ima->tpageflag & IMA_TWINANIM) {
98                 if(ima->twend >= ima->xrep*ima->yrep) ima->twend= ima->xrep*ima->yrep-1;
99                 
100                 /* check: is the bindcode not in the array? Then free. (still to do) */
101                 
102                 diff = (float)(time-ima->lastupdate);
103
104                 inc = (int)(diff*(float)ima->animspeed);
105
106                 ima->lastupdate+=((float)inc/(float)ima->animspeed);
107
108                 newframe = ima->lastframe+inc;
109
110                 if (newframe > (int)ima->twend)
111                         newframe = (int)ima->twsta-1 + (newframe-ima->twend)%(ima->twend-ima->twsta);
112
113                 ima->lastframe = newframe;
114         }
115         return inc;
116 }
117
118 void unlink_mesh(Mesh *me)
119 {
120         int a;
121         
122         if(me==0) return;
123         
124         for(a=0; a<me->totcol; a++) {
125                 if(me->mat[a]) me->mat[a]->id.us--;
126                 me->mat[a]= 0;
127         }
128         if(me->key) me->key->id.us--;
129         me->key= 0;
130         
131         if(me->texcomesh) me->texcomesh= 0;
132 }
133
134
135 /* do not free mesh itself */
136 void free_mesh(Mesh *me)
137 {
138
139         unlink_mesh(me);
140
141         if(me->mvert) MEM_freeN(me->mvert);
142         if(me->medge) MEM_freeN(me->medge);
143         if(me->mface) MEM_freeN(me->mface);
144         
145         if(me->tface) MEM_freeN(me->tface);
146         if(me->dvert) free_dverts(me->dvert, me->totvert);
147         if(me->mcol) MEM_freeN(me->mcol);
148         if(me->msticky) MEM_freeN(me->msticky);
149
150         if(me->mat) MEM_freeN(me->mat);
151         if(me->orco) MEM_freeN(me->orco);
152
153         if(me->bb) MEM_freeN(me->bb);
154         if(me->disp.first) freedisplist(&me->disp);
155         if(me->derived) me->derived->release(me->derived);
156 }
157
158 void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
159 {
160         /* Assumes dst is already set up */
161         int i;
162
163         if (!src || !dst)
164                 return;
165
166         memcpy (dst, src, copycount * sizeof(MDeformVert));
167         
168         for (i=0; i<copycount; i++){
169                 if (src[i].dw){
170                         dst[i].dw = MEM_callocN (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
171                         memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
172                 }
173         }
174
175 }
176 void free_dverts(MDeformVert *dvert, int totvert)
177 {
178         /* Instead of freeing the verts directly,
179         call this function to delete any special
180         vert data */
181         int     i;
182
183         if (!dvert)
184                 return;
185
186         /* Free any special data from the verts */
187         for (i=0; i<totvert; i++){
188                 if (dvert[i].dw) MEM_freeN (dvert[i].dw);
189         }
190         MEM_freeN (dvert);
191 }
192
193 Mesh *add_mesh()
194 {
195         Mesh *me;
196         
197         me= alloc_libblock(&G.main->mesh, ID_ME, "Mesh");
198         
199         me->size[0]= me->size[1]= me->size[2]= 1.0;
200         me->smoothresh= 30;
201         me->texflag= AUTOSPACE;
202         me->flag= ME_TWOSIDED;
203         me->subdiv= 1;
204         me->subdivr = 1;
205         me->bb= unit_boundbox();
206         
207         return me;
208 }
209
210 Mesh *copy_mesh(Mesh *me)
211 {
212         Mesh *men;
213         int a;
214         
215         men= copy_libblock(me);
216         
217         men->mat= MEM_dupallocN(me->mat);
218         for(a=0; a<men->totcol; a++) {
219                 id_us_plus((ID *)men->mat[a]);
220         }
221         id_us_plus((ID *)men->texcomesh);
222
223         men->mvert= MEM_dupallocN(me->mvert);
224         men->medge= MEM_dupallocN(me->medge);
225         men->mface= MEM_dupallocN(me->mface);
226         men->tface= MEM_dupallocN(me->tface);
227         men->dface= NULL;
228
229         if (me->dvert){
230                 men->dvert = MEM_mallocN (sizeof (MDeformVert)*me->totvert, "MDeformVert");
231                 copy_dverts(men->dvert, me->dvert, me->totvert);
232         }
233
234         men->mcol= MEM_dupallocN(me->mcol);
235         men->msticky= MEM_dupallocN(me->msticky);
236         men->texcomesh= NULL;
237         men->orco= NULL;
238         men->bb= MEM_dupallocN(men->bb);
239         
240         men->disp.first= men->disp.last= NULL;  // dont copy, editmode version has pointers in it
241         
242         men->key= copy_key(me->key);
243         if(men->key) men->key->from= (ID *)men;
244         
245         return men;
246 }
247
248 void make_local_tface(Mesh *me)
249 {
250         TFace *tface;
251         Image *ima;
252         int a;
253         
254         if(me->tface==0) return;
255         
256         a= me->totface;
257         tface= me->tface;
258         while(a--) {
259                 
260                 /* special case: ima always local immediately */
261                 if(tface->tpage) {
262                         ima= tface->tpage;
263                         if(ima->id.lib) {
264                                 ima->id.lib= 0;
265                                 ima->id.flag= LIB_LOCAL;
266                                 new_id(0, (ID *)ima, 0);
267                         }
268                 }
269                 tface++;
270         }
271         
272 }
273
274 void make_local_mesh(Mesh *me)
275 {
276         Object *ob;
277         Mesh *men;
278         int local=0, lib=0;
279
280         /* - only lib users: do nothing
281             * - only local users: set flag
282             * - mixed: make copy
283             */
284         
285         if(me->id.lib==0) return;
286         if(me->id.us==1) {
287                 me->id.lib= 0;
288                 me->id.flag= LIB_LOCAL;
289                 new_id(0, (ID *)me, 0);
290                 
291                 if(me->tface) make_local_tface(me);
292                 
293                 return;
294         }
295         
296         ob= G.main->object.first;
297         while(ob) {
298                 if( me==get_mesh(ob) ) {
299                         if(ob->id.lib) lib= 1;
300                         else local= 1;
301                 }
302                 ob= ob->id.next;
303         }
304         
305         if(local && lib==0) {
306                 me->id.lib= 0;
307                 me->id.flag= LIB_LOCAL;
308                 new_id(0, (ID *)me, 0);
309                 
310                 if(me->tface) make_local_tface(me);
311                 
312         }
313         else if(local && lib) {
314                 men= copy_mesh(me);
315                 men->id.us= 0;
316                 
317                 ob= G.main->object.first;
318                 while(ob) {
319                         if( me==get_mesh(ob) ) {                                
320                                 if(ob->id.lib==0) {
321                                         set_mesh(ob, men);
322                                 }
323                         }
324                         ob= ob->id.next;
325                 }
326         }
327 }
328
329 void boundbox_mesh(Mesh *me, float *loc, float *size)
330 {
331         MVert *mvert;
332         BoundBox *bb;
333         float min[3], max[3];
334         float mloc[3], msize[3];
335         int a;
336         
337         if(me->bb==0) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
338         bb= me->bb;
339         
340         INIT_MINMAX(min, max);
341
342         if (!loc) loc= mloc;
343         if (!size) size= msize;
344         
345         mvert= me->mvert;
346         for(a=0; a<me->totvert; a++, mvert++) {
347                 DO_MINMAX(mvert->co, min, max);
348         }
349
350         if(me->totvert) {
351                 loc[0]= (min[0]+max[0])/2.0f;
352                 loc[1]= (min[1]+max[1])/2.0f;
353                 loc[2]= (min[2]+max[2])/2.0f;
354                 
355                 size[0]= (max[0]-min[0])/2.0f;
356                 size[1]= (max[1]-min[1])/2.0f;
357                 size[2]= (max[2]-min[2])/2.0f;
358         }
359         else {
360                 loc[0]= loc[1]= loc[2]= 0.0;
361                 size[0]= size[1]= size[2]= 0.0;
362         }
363         
364         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= loc[0]-size[0];
365         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= loc[0]+size[0];
366         
367         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= loc[1]-size[1];
368         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= loc[1]+size[1];
369
370         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= loc[2]-size[2];
371         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= loc[2]+size[2];
372 }
373
374 void tex_space_mesh(Mesh *me)
375 {
376         KeyBlock *kb;
377         float *fp, loc[3], size[3], min[3], max[3];
378         int a;
379
380         boundbox_mesh(me, loc, size);
381
382         if(me->texflag & AUTOSPACE) {
383                 if(me->key) {
384                         kb= me->key->refkey;
385                         if (kb) {
386                                 
387                                 INIT_MINMAX(min, max);
388                                 
389                                 fp= kb->data;
390                                 for(a=0; a<kb->totelem; a++, fp+=3) {   
391                                         DO_MINMAX(fp, min, max);
392                                 }
393                                 if(kb->totelem) {
394                                         loc[0]= (min[0]+max[0])/2.0f; loc[1]= (min[1]+max[1])/2.0f; loc[2]= (min[2]+max[2])/2.0f;
395                                         size[0]= (max[0]-min[0])/2.0f; size[1]= (max[1]-min[1])/2.0f; size[2]= (max[2]-min[2])/2.0f;
396                                 }
397                                 else {
398                                         loc[0]= loc[1]= loc[2]= 0.0;
399                                         size[0]= size[1]= size[2]= 0.0;
400                                 }
401                                 
402                         }
403                 }
404
405                 VECCOPY(me->loc, loc);
406                 VECCOPY(me->size, size);
407                 me->rot[0]= me->rot[1]= me->rot[2]= 0.0;
408
409                 if(me->size[0]==0.0) me->size[0]= 1.0;
410                 else if(me->size[0]>0.0 && me->size[0]<0.00001) me->size[0]= 0.00001;
411                 else if(me->size[0]<0.0 && me->size[0]> -0.00001) me->size[0]= -0.00001;
412         
413                 if(me->size[1]==0.0) me->size[1]= 1.0;
414                 else if(me->size[1]>0.0 && me->size[1]<0.00001) me->size[1]= 0.00001;
415                 else if(me->size[1]<0.0 && me->size[1]> -0.00001) me->size[1]= -0.00001;
416         
417                 if(me->size[2]==0.0) me->size[2]= 1.0;
418                 else if(me->size[2]>0.0 && me->size[2]<0.00001) me->size[2]= 0.00001;
419                 else if(me->size[2]<0.0 && me->size[2]> -0.00001) me->size[2]= -0.00001;
420         }
421         
422 }
423
424 void make_orco_displist_mesh(Object *ob, int subdivlvl)
425 {
426         Mesh *me;
427         DerivedMesh *dm;
428         DispListMesh *dlm;
429         int i;
430         
431         me= ob->data;
432
433                 /* if there's a key, set the first one */
434         if(me->key && me->texcomesh==0) {
435                 cp_key(0, me->totvert, me->totvert, (char*) me->mvert->co, me->key, me->key->refkey, 0);
436         }
437
438         dm= subsurf_make_derived_from_mesh(me, subdivlvl, me->flag);
439         dlm= dm->convertToDispListMesh(dm);
440         dm->release(dm);
441         
442                 /* Restore correct key */
443         do_ob_key(ob);
444
445         if (me->orco) MEM_freeN(me->orco);
446         me->orco= MEM_mallocN(dlm->totvert*3*sizeof(float), "mesh displist orco");
447         
448         for(i=0; i<dlm->totvert; i++) {
449                 float *fp= &me->orco[i*3];
450
451                 VECCOPY(fp, dlm->mvert[i].co);
452
453                 fp[0]= (fp[0]-me->loc[0])/me->size[0];
454                 fp[1]= (fp[1]-me->loc[1])/me->size[1];
455                 fp[2]= (fp[2]-me->loc[2])/me->size[2];
456         }
457         
458         displistmesh_free(dlm);
459 }
460
461 void make_orco_mesh(Mesh *me)
462 {
463         MVert *mvert;
464         KeyBlock *kb;
465         float *orco, *fp;
466         int a, totvert;
467         
468         totvert= me->totvert;
469         if(totvert==0) return;
470         orco= me->orco= MEM_mallocN(sizeof(float)*3*totvert, "orco mesh");
471
472         if(me->key && me->texcomesh==0) {
473                 kb= me->key->refkey;
474                 if (kb) {               /***** BUG *****/
475                         fp= kb->data;
476                         
477                         for(a=0; a<totvert; a++, orco+=3) {
478                                 orco[0]= (fp[0]-me->loc[0])/me->size[0];
479                                 orco[1]= (fp[1]-me->loc[1])/me->size[1];
480                                 orco[2]= (fp[2]-me->loc[2])/me->size[2];
481                                 
482                                 /* only increase mvert when totvert <= kb->totelem */
483                                 if(a<kb->totelem) fp+=3;
484                         }
485                 }
486         }
487         else {
488                 if(me->texcomesh) {
489                         me= me->texcomesh;
490                 }       
491         
492                 mvert= me->mvert;
493                 for(a=0; a<totvert; a++, orco+=3) {
494                         orco[0]= (mvert->co[0]-me->loc[0])/me->size[0];
495                         orco[1]= (mvert->co[1]-me->loc[1])/me->size[1];
496                         orco[2]= (mvert->co[2]-me->loc[2])/me->size[2];
497                         
498                         /* only increase mvert when totvert <= me->totvert */
499                         if(a<me->totvert) mvert++;
500                 }
501         }
502 }
503
504 /** rotates the vertices of a face in case v[2] or v[3] (vertex index)
505   * is = 0.
506   * Helaas, the MFace structure has no pointer to its
507   * texture face, therefore, texture can not be fixed inside
508   * this function. 
509   * 
510   * see also blender/src/editmesh.c, fix_faceindices()
511
512   * THIS FUNCTION WILL BE DINOSOURCE. For the moment, another hack
513         is added to fix texture coordinates / vertex colors:
514
515         void test_index_face(MFace *mface, TFace *tface, int nr)
516   */
517
518 void test_index_mface(MFace *mface, int nr)
519 {
520         int a;
521
522         
523         /* first test if the face is legal */
524
525         if(mface->v3 && mface->v3==mface->v4) {
526                 mface->v4= 0;
527                 nr--;
528         }
529         if(mface->v2 && mface->v2==mface->v3) {
530                 mface->v3= mface->v4;
531                 mface->v4= 0;
532                 nr--;
533         }
534         if(mface->v1==mface->v2) {
535                 mface->v2= mface->v3;
536                 mface->v3= mface->v4;
537                 mface->v4= 0;
538                 nr--;
539         }
540
541         /* prevent a zero at wrong index location */
542         if(nr==2) {
543                 if(mface->v2==0) SWAP(int, mface->v1, mface->v2);
544         }
545         else if(nr==3) {
546                 if(mface->v3==0) {
547                         SWAP(int, mface->v1, mface->v2);
548                         SWAP(int, mface->v2, mface->v3);
549                         
550                         a= mface->edcode;
551                         mface->edcode= 0;
552                         if(a & ME_V1V2) mface->edcode |= ME_V3V1;
553                         if(a & ME_V2V3) mface->edcode |= ME_V1V2;
554                         if(a & ME_V3V1) mface->edcode |= ME_V2V3;
555                         
556                         a= mface->puno;
557                         mface->puno &= ~15;
558                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV2;
559                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV3;
560                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
561                 }
562         }
563         else if(nr==4) {
564                 if(mface->v3==0 || mface->v4==0) {
565                         SWAP(int, mface->v1, mface->v3);
566                         SWAP(int, mface->v2, mface->v4);
567                         a= mface->edcode;
568                         mface->edcode= 0;
569                         if(a & ME_V1V2) mface->edcode |= ME_V3V4;
570                         if(a & ME_V2V3) mface->edcode |= ME_V2V3;
571                         if(a & ME_V3V4) mface->edcode |= ME_V1V2;
572                         if(a & ME_V4V1) mface->edcode |= ME_V4V1;
573
574                         a= mface->puno;
575                         mface->puno &= ~15;
576                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV3;
577                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV4;
578                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
579                         if(a & ME_FLIPV4) mface->puno |= ME_FLIPV2;
580                 }
581         }
582 }
583
584 /**     This function should die as soon as there is another mesh
585         structure. Functionality is the same as
586
587                 void test_index_mface()
588
589         but it fixes texture coordinates as well. 
590 */
591
592 #define UVCOPY(t, s) memcpy(t, s, 2 * sizeof(float));
593 void test_index_face(MFace *mface, TFace *tface, int nr)
594 {
595         int a;
596         float tmpuv[2];
597         unsigned int tmpcol;
598
599
600         /* first test if the face is legal */
601
602         if(mface->v3 && mface->v3==mface->v4) {
603                 mface->v4= 0;
604                 nr--;
605         }
606         if(mface->v2 && mface->v2==mface->v3) {
607                 mface->v3= mface->v4;
608                 mface->v4= 0;
609                 nr--;
610         }
611         if(mface->v1==mface->v2) {
612                 mface->v2= mface->v3;
613                 mface->v3= mface->v4;
614                 mface->v4= 0;
615                 nr--;
616         }
617
618         /* prevent a zero at wrong index location */
619         if(nr==2) {
620                 if(mface->v2==0) SWAP(int, mface->v1, mface->v2);
621         }
622         else if(nr==3) {
623                 if(mface->v3==0) {
624                         SWAP(int, mface->v1, mface->v2);
625                         SWAP(int, mface->v2, mface->v3);
626                         /* rotate face UV coordinates, too */
627                         UVCOPY(tmpuv, tface->uv[0]);
628                         UVCOPY(tface->uv[0], tface->uv[1]);
629                         UVCOPY(tface->uv[1], tface->uv[2]);
630                         UVCOPY(tface->uv[2], tmpuv);
631                         /* same with vertex colours */
632                         tmpcol = tface->col[0];
633                         tface->col[0] = tface->col[1];
634                         tface->col[1] = tface->col[2];
635                         tface->col[2] = tmpcol;
636
637                         
638                         a= mface->edcode;
639                         mface->edcode= 0;
640                         if(a & ME_V1V2) mface->edcode |= ME_V3V1;
641                         if(a & ME_V2V3) mface->edcode |= ME_V1V2;
642                         if(a & ME_V3V1) mface->edcode |= ME_V2V3;
643                         
644                         a= mface->puno;
645                         mface->puno &= ~15;
646                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV2;
647                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV3;
648                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
649                 }
650         }
651         else if(nr==4) {
652                 if(mface->v3==0 || mface->v4==0) {
653                         SWAP(int, mface->v1, mface->v3);
654                         SWAP(int, mface->v2, mface->v4);
655                         /* swap UV coordinates */
656                         UVCOPY(tmpuv, tface->uv[0]);
657                         UVCOPY(tface->uv[0], tface->uv[2]);
658                         UVCOPY(tface->uv[2], tmpuv);
659                         UVCOPY(tmpuv, tface->uv[1]);
660                         UVCOPY(tface->uv[1], tface->uv[3]);
661                         UVCOPY(tface->uv[3], tmpuv);
662                         /* swap vertex colours */
663                         tmpcol = tface->col[0];
664                         tface->col[0] = tface->col[2];
665                         tface->col[2] = tmpcol;
666                         tmpcol = tface->col[1];
667                         tface->col[1] = tface->col[3];
668                         tface->col[3] = tmpcol;
669
670                         a= mface->edcode;
671                         mface->edcode= 0;
672                         if(a & ME_V1V2) mface->edcode |= ME_V3V4;
673                         if(a & ME_V2V3) mface->edcode |= ME_V2V3;
674                         if(a & ME_V3V4) mface->edcode |= ME_V1V2;
675                         if(a & ME_V4V1) mface->edcode |= ME_V4V1;
676
677                         a= mface->puno;
678                         mface->puno &= ~15;
679                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV3;
680                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV4;
681                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
682                         if(a & ME_FLIPV4) mface->puno |= ME_FLIPV2;
683                 }
684         }
685 }
686
687 void flipnorm_mesh(Mesh *me)
688 {
689         MFace *mface;
690         MVert *mvert;
691         DispList *dl;
692         float *fp;
693         int a, temp;
694         
695         mvert= me->mvert;
696         a= me->totvert;
697         while(a--) {
698                 mvert->no[0]= -mvert->no[0];
699                 mvert->no[1]= -mvert->no[1];
700                 mvert->no[2]= -mvert->no[2];
701                 mvert++;
702         }
703         
704         mface= me->mface;
705         a= me->totface;
706         while(a--) {
707                 if(mface->v3) {
708                         if(mface->v4) {
709                                 SWAP(int, mface->v4, mface->v1);
710                                 SWAP(int, mface->v3, mface->v2);
711                                 test_index_mface(mface, 4);
712                                 temp= mface->puno;
713                                 mface->puno &= ~15;
714                                 if(temp & ME_FLIPV1) mface->puno |= ME_FLIPV4;
715                                 if(temp & ME_FLIPV2) mface->puno |= ME_FLIPV3;
716                                 if(temp & ME_FLIPV3) mface->puno |= ME_FLIPV2;
717                                 if(temp & ME_FLIPV4) mface->puno |= ME_FLIPV1;
718                         }
719                         else {
720                                 SWAP(int, mface->v3, mface->v1);
721                                 test_index_mface(mface, 3);
722                                 temp= mface->puno;
723                                 mface->puno &= ~15;
724                                 if(temp & ME_FLIPV1) mface->puno |= ME_FLIPV3;
725                                 if(temp & ME_FLIPV2) mface->puno |= ME_FLIPV2;
726                                 if(temp & ME_FLIPV3) mface->puno |= ME_FLIPV1;
727                         }
728                 }
729                 mface++;
730         }
731
732         if(me->disp.first) {
733                 dl= me->disp.first;
734                 fp= dl->nors;
735                 if(fp) {
736                         a= dl->nr;
737                         while(a--) {
738                                 fp[0]= -fp[0];
739                                 fp[1]= -fp[1];
740                                 fp[2]= -fp[2];
741                                 fp+= 3;
742                         }
743                 }
744         }
745 }
746
747 Mesh *get_mesh(Object *ob)
748 {
749         
750         if(ob==0) return 0;
751         if(ob->type==OB_MESH) return ob->data;
752         else return 0;
753 }
754
755 void set_mesh(Object *ob, Mesh *me)
756 {
757         Mesh *old=0;
758         
759         if(ob==0) return;
760         
761         if(ob->type==OB_MESH) {
762                 old= ob->data;
763                 old->id.us--;
764                 ob->data= me;
765                 id_us_plus((ID *)me);
766         }
767         
768         test_object_materials((ID *)me);
769 }
770
771 /* ************** make edges in a Mesh, for outside of editmode */
772
773 struct edgesort {
774         int v1, v2;
775         int flag;
776 };
777
778 /* edges have to be added with lowest index first for sorting */
779 static void to_edgesort(struct edgesort *ed, int v1, int v2, int flag)
780 {
781         if(v1<v2) {
782                 ed->v1= v1; ed->v2= v2;
783         }
784         else {
785                 ed->v1= v2; ed->v2= v1;
786         }
787         ed->flag= flag;
788 }
789
790 static int vergedgesort(const void *v1, const void *v2)
791 {
792         const struct edgesort *x1=v1, *x2=v2;
793
794         if( x1->v1 > x2->v1) return 1;
795         else if( x1->v1 < x2->v1) return -1;
796         else if( x1->v2 > x2->v2) return 1;
797         else if( x1->v2 < x2->v2) return -1;
798         
799         return 0;
800 }
801
802
803 void make_edges(Mesh *me)
804 {
805         MFace *mface;
806         MEdge *medge;
807         struct edgesort *edsort, *ed;
808         int a, totedge=0, final=0;
809         
810         /* we put all edges in array, sort them, and detect doubles that way */
811         
812         for(a= me->totface, mface= me->mface; a>0; a--, mface++) {
813                 if(mface->v4) totedge+=4;
814                 else if(mface->v3) totedge+=3;
815                 else totedge+=1;
816         }
817         
818         if(totedge==0) return;
819         
820         ed= edsort= MEM_mallocN(totedge*sizeof(struct edgesort), "edgesort");
821         
822         for(a= me->totface, mface= me->mface; a>0; a--, mface++) {
823                 
824                 to_edgesort(ed, mface->v1, mface->v2, mface->edcode & ME_V1V2);
825                 ed++;
826                 if(mface->v4) {
827                         to_edgesort(ed, mface->v2, mface->v3, mface->edcode & ME_V2V3);
828                         ed++;
829                         to_edgesort(ed, mface->v3, mface->v4, mface->edcode & ME_V3V4);
830                         ed++;
831                         to_edgesort(ed, mface->v4, mface->v1, mface->edcode & ME_V4V1);
832                         ed++;
833                 }
834                 else if(mface->v3) {
835                         to_edgesort(ed, mface->v2, mface->v3, mface->edcode & ME_V2V3);
836                         ed++;
837                         to_edgesort(ed, mface->v3, mface->v1, mface->edcode & ME_V3V1);
838                         ed++;
839                 }
840         }
841         
842         qsort(edsort, totedge, sizeof(struct edgesort), vergedgesort);
843         
844         /* count final amount */
845         for(a=totedge, ed=edsort; a>1; a--, ed++) {
846                 /* edge is unique when it differs from next edge, or is last */
847                 if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) final++;
848                 else {
849                         /* this makes sure identical edges both get draw flag */
850                         if(ed->flag) (ed+1)->flag= 1;
851                         else if((ed+1)->flag) ed->flag= 1;
852                 }
853         }
854         final++;
855         
856         medge= me->medge= MEM_callocN(final*sizeof(MEdge), "make mesh edges");
857         me->totedge= final;
858         
859         for(a=totedge, ed=edsort; a>1; a--, ed++) {
860                 /* edge is unique when it differs from next edge, or is last */
861                 if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) {
862                         medge->v1= ed->v1;
863                         medge->v2= ed->v2;
864                         if(ed->flag) medge->flag= ME_EDGEDRAW;
865                         medge++;
866                 }
867         }
868         /* last edge */
869         medge->v1= ed->v1;
870         medge->v2= ed->v2;
871         if(ed->flag) medge->flag= ME_EDGEDRAW;
872
873         MEM_freeN(edsort);
874 }
875
876
877
878 void mball_to_mesh(ListBase *lb, Mesh *me)
879 {
880         DispList *dl;
881         MVert *mvert;
882         MFace *mface;
883         float *nors, *verts;
884         int a, *index;
885         
886         dl= lb->first;
887         if(dl==0) return;
888
889         if(dl->type==DL_INDEX4) {
890                 me->flag= ME_NOPUNOFLIP;
891                 me->totvert= dl->nr;
892                 me->totface= dl->parts;
893                 
894                 me->mvert=mvert= MEM_callocN(dl->nr*sizeof(MVert), "mverts");
895                 a= dl->nr;
896                 nors= dl->nors;
897                 verts= dl->verts;
898                 while(a--) {
899                         VECCOPY(mvert->co, verts);
900                         mvert->no[0]= (short int)(nors[0]*32767.0);
901                         mvert->no[1]= (short int)(nors[1]*32767.0);
902                         mvert->no[2]= (short int)(nors[2]*32767.0);
903                         mvert++;
904                         nors+= 3;
905                         verts+= 3;
906                 }
907                 
908                 me->mface=mface= MEM_callocN(dl->parts*sizeof(MFace), "mface");
909                 a= dl->parts;
910                 index= dl->index;
911                 while(a--) {
912                         mface->v1= index[0];
913                         mface->v2= index[1];
914                         mface->v3= index[2];
915                         mface->v4= index[3];
916
917                         mface->puno= 0;
918                         mface->edcode= ME_V1V2+ME_V2V3;
919                         mface->flag = ME_SMOOTH;
920                         
921                         mface++;
922                         index+= 4;
923                 }
924         }       
925 }
926
927 void nurbs_to_mesh(Object *ob)
928 {
929         Object *ob1;
930         DispList *dl;
931         Mesh *me;
932         Curve *cu;
933         MVert *mvert;
934         MFace *mface;
935         float *data;
936         int a, b, ofs, vertcount, startvert, totvert=0, totvlak=0;
937         int p1, p2, p3, p4, *index;
938
939         cu= ob->data;
940
941         if(ob->type==OB_CURVE) {
942                 /* rule: dl->type INDEX3 always as first in list */
943                 dl= cu->disp.first;
944                 if(dl->type!=DL_INDEX3) {
945                         curve_to_filledpoly(ob->data, &cu->nurb, &cu->disp);
946                 }
947         }
948
949         /* count */
950         dl= cu->disp.first;
951         while(dl) {
952                 if(dl->type==DL_SEGM) {
953                         totvert+= dl->parts*dl->nr;
954                         totvlak+= dl->parts*(dl->nr-1);
955                 }
956                 else if(dl->type==DL_POLY) {
957                         /* cyclic polys are filled. except when 3D */
958                         if(cu->flag & CU_3D) {
959                                 totvert+= dl->parts*dl->nr;
960                                 totvlak+= dl->parts*dl->nr;
961                         }
962                 }
963                 else if(dl->type==DL_SURF) {
964                         totvert+= dl->parts*dl->nr;
965                         totvlak+= (dl->parts-1+((dl->flag & 2)==2))*(dl->nr-1+(dl->flag & 1));
966                 }
967                 else if(dl->type==DL_INDEX3) {
968                         totvert+= dl->nr;
969                         totvlak+= dl->parts;
970                 }
971                 dl= dl->next;
972         }
973         if(totvert==0) {
974                 error("can't convert");
975                 return;
976         }
977
978         /* make mesh */
979         me= add_mesh();
980         me->totvert= totvert;
981         me->totface= totvlak;
982
983         me->totcol= cu->totcol;
984         me->mat= cu->mat;
985         cu->mat= 0;
986         cu->totcol= 0;
987
988         mvert=me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "cumesh1");
989         mface=me->mface= MEM_callocN(me->totface*sizeof(MFace), "cumesh2");
990
991         /* verts and faces */
992         vertcount= 0;
993
994         dl= cu->disp.first;
995         while(dl) {
996                 if(dl->type==DL_SEGM) {
997                         startvert= vertcount;
998                         a= dl->parts*dl->nr;
999                         data= dl->verts;
1000                         while(a--) {
1001                                 VECCOPY(mvert->co, data);
1002                                 data+=3;
1003                                 vertcount++;
1004                                 mvert++;
1005                         }
1006
1007                         for(a=0; a<dl->parts; a++) {
1008                                 ofs= a*dl->nr;
1009                                 for(b=1; b<dl->nr; b++) {
1010                                         mface->v1= startvert+ofs+b-1;
1011                                         mface->v2= startvert+ofs+b;
1012                                         mface->edcode= ME_V1V2;
1013                                         test_index_mface(mface, 2);
1014                                         mface++;
1015                                 }
1016                         }
1017
1018                 }
1019                 else if(dl->type==DL_POLY) {
1020                         /* 3d polys are not filled */
1021                         if(cu->flag & CU_3D) {
1022                                 startvert= vertcount;
1023                                 a= dl->parts*dl->nr;
1024                                 data= dl->verts;
1025                                 while(a--) {
1026                                         VECCOPY(mvert->co, data);
1027                                         data+=3;
1028                                         vertcount++;
1029                                         mvert++;
1030                                 }
1031         
1032                                 for(a=0; a<dl->parts; a++) {
1033                                         ofs= a*dl->nr;
1034                                         for(b=0; b<dl->nr; b++) {
1035                                                 mface->v1= startvert+ofs+b;
1036                                                 if(b==dl->nr-1) mface->v2= startvert+ofs;
1037                                                 else mface->v2= startvert+ofs+b+1;
1038                                                 mface->edcode= ME_V1V2;
1039                                                 test_index_mface(mface, 2);
1040                                                 mface++;
1041                                         }
1042                                 }
1043                         }
1044                 }
1045                 else if(dl->type==DL_INDEX3) {
1046                         startvert= vertcount;
1047                         a= dl->nr;
1048                         data= dl->verts;
1049                         while(a--) {
1050                                 VECCOPY(mvert->co, data);
1051                                 data+=3;
1052                                 vertcount++;
1053                                 mvert++;
1054                         }
1055
1056                         a= dl->parts;
1057                         index= dl->index;
1058                         while(a--) {
1059                                 mface->v1= startvert+index[0];
1060                                 mface->v2= startvert+index[1];
1061                                 mface->v3= startvert+index[2];
1062                                 mface->v4= 0;
1063         
1064                                 mface->puno= 7;
1065                                 mface->edcode= ME_V1V2+ME_V2V3;
1066                                 test_index_mface(mface, 3);
1067                                 
1068                                 mface++;
1069                                 index+= 3;
1070                         }
1071         
1072         
1073                 }
1074                 else if(dl->type==DL_SURF) {
1075                         startvert= vertcount;
1076                         a= dl->parts*dl->nr;
1077                         data= dl->verts;
1078                         while(a--) {
1079                                 VECCOPY(mvert->co, data);
1080                                 data+=3;
1081                                 vertcount++;
1082                                 mvert++;
1083                         }
1084
1085                         for(a=0; a<dl->parts; a++) {
1086
1087                                 if( (dl->flag & 2)==0 && a==dl->parts-1) break;
1088
1089                                 if(dl->flag & 1) {                      /* p2 -> p1 -> */
1090                                         p1= startvert+ dl->nr*a;        /* p4 -> p3 -> */
1091                                         p2= p1+ dl->nr-1;               /* -----> next row */
1092                                         p3= p1+ dl->nr;
1093                                         p4= p2+ dl->nr;
1094                                         b= 0;
1095                                 }
1096                                 else {
1097                                         p2= startvert+ dl->nr*a;
1098                                         p1= p2+1;
1099                                         p4= p2+ dl->nr;
1100                                         p3= p1+ dl->nr;
1101                                         b= 1;
1102                                 }
1103                                 if( (dl->flag & 2) && a==dl->parts-1) {
1104                                         p3-= dl->parts*dl->nr;
1105                                         p4-= dl->parts*dl->nr;
1106                                 }
1107
1108                                 for(; b<dl->nr; b++) {
1109                                         mface->v1= p1;
1110                                         mface->v2= p3;
1111                                         mface->v3= p4;
1112                                         mface->v4= p2;
1113                                         mface->mat_nr= (unsigned char)dl->col;
1114                                         mface->edcode= ME_V1V2+ME_V2V3;
1115                                         test_index_mface(mface, 4);
1116                                         mface++;
1117
1118                                         p4= p3; 
1119                                         p3++;
1120                                         p2= p1; 
1121                                         p1++;
1122                                 }
1123                         }
1124
1125                 }
1126
1127                 dl= dl->next;
1128         }
1129
1130         if(ob->data) {
1131                 free_libblock(&G.main->curve, ob->data);
1132         }
1133         ob->data= me;
1134         ob->type= OB_MESH;
1135         
1136         tex_space_mesh(me);
1137         
1138         /* other users */
1139         ob1= G.main->object.first;
1140         while(ob1) {
1141                 if(ob1->data==cu) {
1142                         ob1->type= OB_MESH;
1143                 
1144                         ob1->data= ob->data;
1145                         id_us_plus((ID *)ob->data);
1146                 }
1147                 ob1= ob1->id.next;
1148         }
1149
1150 }
1151
1152 void edge_drawflags_mesh(Mesh *me)
1153 {
1154         MFace *mface;
1155         int a;
1156         
1157         mface= me->mface;
1158         for(a=0; a<me->totface; a++, mface++) {
1159                 mface->edcode= ME_V1V2|ME_V2V3|ME_V3V4|ME_V4V1;
1160         }
1161 }
1162
1163 void tface_to_mcol(Mesh *me)
1164 {
1165         TFace *tface;
1166         unsigned int *mcol;
1167         int a;
1168         
1169         me->mcol= MEM_mallocN(4*sizeof(int)*me->totface, "nepmcol");
1170         mcol= (unsigned int *)me->mcol;
1171         
1172         a= me->totface;
1173         tface= me->tface;
1174         while(a--) {
1175                 memcpy(mcol, tface->col, 16);
1176                 mcol+= 4;
1177                 tface++;
1178         }
1179 }
1180
1181 void mcol_to_tface(Mesh *me, int freedata)
1182 {
1183         TFace *tface;
1184         unsigned int *mcol;
1185         int a;
1186         
1187         a= me->totface;
1188         tface= me->tface;
1189         mcol= (unsigned int *)me->mcol;
1190         while(a--) {
1191                 memcpy(tface->col, mcol, 16);
1192                 mcol+= 4;
1193                 tface++;
1194         }
1195         
1196         if(freedata) {
1197                 MEM_freeN(me->mcol);
1198                 me->mcol= 0;
1199         }
1200 }
1201
1202 int mesh_uses_displist(Mesh *me) {
1203         return (me->flag&ME_SUBSURF);
1204 }
1205
1206 void mesh_calculate_vertex_normals(Mesh *me)
1207 {
1208         float (*tempNorms)[3]= MEM_callocN(me->totvert*sizeof(*tempNorms), "tempNorms");
1209         int i;
1210
1211         for (i=0; i<me->totface; i++) {
1212                 MFace *mf= &((MFace*) me->mface)[i];
1213                 float f_no[3];
1214
1215                 if (!mf->v3)
1216                         continue;
1217                         
1218                 if (mf->v4) {
1219                         CalcNormFloat4(me->mvert[mf->v1].co, me->mvert[mf->v2].co, me->mvert[mf->v3].co, me->mvert[mf->v4].co, f_no);
1220                 } else {
1221                         CalcNormFloat(me->mvert[mf->v1].co, me->mvert[mf->v2].co, me->mvert[mf->v3].co, f_no);
1222                 }
1223                 
1224                 VecAddf(tempNorms[mf->v1], tempNorms[mf->v1], f_no);
1225                 VecAddf(tempNorms[mf->v2], tempNorms[mf->v2], f_no);
1226                 VecAddf(tempNorms[mf->v3], tempNorms[mf->v3], f_no);
1227                 if (mf->v4)
1228                         VecAddf(tempNorms[mf->v4], tempNorms[mf->v4], f_no);
1229         }
1230         for (i=0; i<me->totvert; i++) {
1231                 MVert *mv= &me->mvert[i];
1232                 float *no= tempNorms[i];
1233                 
1234                 Normalise(no);
1235                 mv->no[0]= (short)(no[0]*32767.0);
1236                 mv->no[1]= (short)(no[1]*32767.0);
1237                 mv->no[2]= (short)(no[2]*32767.0);
1238         }
1239         
1240         MEM_freeN(tempNorms);
1241 }
1242
1243 void mesh_delete_material_index(Mesh *me, int index) {
1244         int i;
1245
1246         for (i=0; i<me->totface; i++) {
1247                 MFace *mf = &((MFace*) me->mface)[i];
1248                 if (mf->mat_nr && mf->mat_nr>=index) 
1249                         mf->mat_nr--;
1250         }
1251 }
1252
1253 void mesh_set_smooth_flag(Mesh *me, int enableSmooth) {
1254         int i;
1255
1256         for (i=0; i<me->totface; i++) {
1257                 MFace *mf = &((MFace*) me->mface)[i];
1258
1259                 if (enableSmooth) {
1260                         mf->flag |= ME_SMOOTH;
1261                 } else {
1262                         mf->flag &= ~ME_SMOOTH;
1263                 }
1264         }
1265 }