- replaced G.{edve,eded,edvl} with G.editMesh, atm just a structure to
[blender.git] / source / blender / blenkernel / intern / subsurf.c
1
2 /*  subsurf.c
3  * 
4  *  jun 2001
5  *  
6  * 
7  * $Id$
8  *
9  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version. The Blender
15  * Foundation also sells licenses for use in proprietary software under
16  * the Blender License.  See http://www.blender.org/BL/ for information
17  * about this.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  *
28  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
29  * All rights reserved.
30  *
31  * The Original Code is: all of this file.
32  *
33  * Contributor(s): none yet.
34  *
35  * ***** END GPL/BL DUAL LICENSE BLOCK *****
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifdef WIN32
43 #include "BLI_winstuff.h"
44 #endif
45
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <math.h>
49 #include "MEM_guardedalloc.h"
50
51 #include "DNA_mesh_types.h"
52 #include "DNA_meshdata_types.h"
53 #include "DNA_object_types.h"
54
55 #include "BKE_bad_level_calls.h"
56 #include "BKE_global.h"
57 #include "BKE_mesh.h"
58 #include "BKE_subsurf.h"
59 #include "BKE_displist.h"
60
61 #include "BLI_blenlib.h"
62 #include "BLI_editVert.h"
63 #include "BLI_arithb.h"
64 #include "BLI_linklist.h"
65 #include "BLI_memarena.h"
66
67 /*
68  * TODO
69  * 
70  * make uvco's && vcol's properly subdivided
71  *   - requires moving uvco and vcol data to vertices 
72  *        (where it belongs?), or making sharedness explicit
73  * remove/integrate zsl functions
74  * clean up uvco && vcol stuff
75  * add option to update subsurf only after done transverting
76  * decouple display subdivlevel and render subdivlevel
77  * look into waves/particles with subsurfs
78  * groan... make it work with sticky? 
79  * U check if storing tfaces (clut, tpage) in a displist is 
80  *     going to be a mem problem (for example, on duplicate)
81  * U write game blender convert routine
82  * U thorough rendering check + background
83  * 
84  */
85
86 /****/
87
88 static float *Vec2Cpy(float *t, float *a) {
89         t[0]= a[0];
90         t[1]= a[1];
91         return t;
92 }
93 static float *Vec3Cpy(float *t, float *a) {
94         t[0]= a[0];
95         t[1]= a[1];
96         t[2]= a[2];
97         return t;
98 }
99
100 static float *Vec2CpyI(float *t, float x, float y) {
101         t[0]= x;
102         t[1]= y;
103         return t;
104 }
105 static float *Vec3CpyI(float *t, float x, float y, float z) {
106         t[0]= x;
107         t[1]= y;
108         t[2]= z;
109         return t;
110 }
111
112 static float *Vec2AvgT(float *t, float *a, float *b) {
113         t[0]= (a[0]+b[0])*0.5f;
114         t[1]= (a[1]+b[1])*0.5f;
115         return t;
116 }
117 static float *Vec3AvgT(float *t, float *a, float *b) {
118         t[0]= (a[0]+b[0])*0.5f;
119         t[1]= (a[1]+b[1])*0.5f;
120         t[2]= (a[2]+b[2])*0.5f;
121         return t;
122 }
123
124 static float *Vec3AddT(float *t, float *a, float *b) {
125         t[0]= a[0]+b[0];
126         t[1]= a[1]+b[1];
127         t[2]= a[2]+b[2];
128         return t;
129 }
130 static float *Vec2Add(float *ta, float *b) {
131         ta[0]+= b[0];
132         ta[1]+= b[1];
133         return ta;
134 }
135
136
137 static float *Vec3MulNT(float *t, float *a, float n) {
138         t[0]= a[0]*n;
139         t[1]= a[1]*n;
140         t[2]= a[2]*n;
141         return t;
142 }
143
144 static float *Vec3Add(float *ta, float *b) {
145         ta[0]+= b[0];
146         ta[1]+= b[1];
147         ta[2]+= b[2];
148         return ta;
149 }
150
151 static float *Vec2MulN(float *ta, float n) {
152         ta[0]*= n;
153         ta[1]*= n;
154         return ta;
155 }
156 static float *Vec3MulN(float *ta, float n) {
157         ta[0]*= n;
158         ta[1]*= n;
159         ta[2]*= n;
160         return ta;
161 }
162
163 /****/
164
165 typedef struct _HyperVert HyperVert;
166 typedef struct _HyperEdge HyperEdge;
167 typedef struct _HyperFace HyperFace;
168 typedef struct _HyperMesh HyperMesh;
169
170 struct _HyperVert {
171         HyperVert *next;
172         
173         float co[3];
174         float *orig;    // if set, pointer to original vertex, for handles
175         HyperVert *nmv;
176         LinkNode *edges, *faces;
177 };
178
179 struct _HyperEdge {
180         HyperEdge *next;
181
182         HyperVert *v[2];
183         HyperVert *ep;
184         int flag;               // added for drawing optim
185         LinkNode *faces;
186 };
187
188 struct _HyperFace {
189         HyperFace *next;
190
191         int nverts;
192         HyperVert **verts;      
193         HyperEdge **edges;
194         
195         HyperVert *mid;
196
197         unsigned char (*vcol)[4];
198         float (*uvco)[2];
199                 
200                 /* for getting back tface, matnr, etc */
201         union {
202                 int ind;
203                 EditVlak *ef;
204         } orig;
205 };
206
207 struct _HyperMesh {
208         HyperVert *verts;
209         HyperEdge *edges;
210         HyperFace *faces;
211         Mesh *orig_me;
212         short hasuvco, hasvcol;
213         
214         MemArena *arena;
215 };
216
217 /***/
218
219 static HyperEdge *hypervert_find_edge(HyperVert *v, HyperVert *to) {
220         LinkNode *l;
221         
222         for (l= v->edges; l; l= l->next) {
223                 HyperEdge *e= l->link;
224                 
225                 if ((e->v[0]==v&&e->v[1]==to) || (e->v[1]==v&&e->v[0]==to))
226                          return e;
227         }
228
229         return NULL;
230 }
231
232 static int hyperedge_is_boundary(HyperEdge *e) {
233                 /* len(e->faces) <= 1 */
234         return (!e->faces || !e->faces->next);
235 }
236
237 static int hypervert_is_boundary(HyperVert *v) {
238         LinkNode *l;
239         
240         for (l= v->edges; l; l= l->next)
241                 if (hyperedge_is_boundary(l->link))
242                         return 1;
243                         
244         return 0;
245 }
246
247 static HyperVert *hyperedge_other_vert(HyperEdge *e, HyperVert *a) {
248         return (a==e->v[0])?e->v[1]:e->v[0];
249 }
250
251 static HyperVert *hypermesh_add_vert(HyperMesh *hme, float *co, float *orig) {
252         HyperVert *hv= BLI_memarena_alloc(hme->arena, sizeof(*hv));
253         
254         hv->nmv= NULL;
255         hv->edges= NULL;
256         hv->faces= NULL;
257         
258         Vec3Cpy(hv->co, co);
259         hv->orig= orig;
260         
261         hv->next= hme->verts;
262         hme->verts= hv;
263         
264         return hv;
265 }
266
267 static HyperEdge *hypermesh_add_edge(HyperMesh *hme, HyperVert *v1, HyperVert *v2, int flag) {
268         HyperEdge *he= BLI_memarena_alloc(hme->arena, sizeof(*he));
269         
270         BLI_linklist_prepend_arena(&v1->edges, he, hme->arena);
271         BLI_linklist_prepend_arena(&v2->edges, he, hme->arena);
272         
273         he->v[0]= v1;
274         he->v[1]= v2;
275         he->ep= NULL;
276         he->faces= NULL;
277         he->flag= flag;
278         
279         he->next= hme->edges;
280         hme->edges= he;
281         
282         return he;
283 }
284
285 static HyperFace *hypermesh_add_face(HyperMesh *hme, HyperVert **verts, int nverts, int flag) {
286         HyperFace *f= BLI_memarena_alloc(hme->arena, sizeof(*f));
287         HyperVert *last;
288         int j;
289
290         f->mid= NULL;
291         f->vcol= NULL;
292         f->uvco= NULL;
293
294         f->nverts= nverts;
295         f->verts= BLI_memarena_alloc(hme->arena, sizeof(*f->verts)*f->nverts);
296         f->edges= BLI_memarena_alloc(hme->arena, sizeof(*f->edges)*f->nverts);
297         
298         last= verts[nverts-1];
299         for (j=0; j<nverts; j++) {
300                 HyperVert *v= verts[j];
301                 HyperEdge *e= hypervert_find_edge(v, last);
302
303                 if (!e)
304                         e= hypermesh_add_edge(hme, v, last, flag);
305
306                 f->verts[j]= v;
307                 f->edges[j]= e;
308                 
309                 BLI_linklist_prepend_arena(&v->faces, f, hme->arena);
310                 BLI_linklist_prepend_arena(&e->faces, f, hme->arena);
311                 
312                 last= v;
313         }
314         
315         f->next= hme->faces;
316         hme->faces= f;
317         
318         return f;       
319 }
320
321 static HyperMesh *hypermesh_new(void) {
322         HyperMesh *hme= MEM_mallocN(sizeof(*hme), "hme");
323
324         hme->verts= NULL;
325         hme->edges= NULL;
326         hme->faces= NULL;
327         hme->orig_me= NULL;
328         hme->hasuvco= hme->hasvcol= 0;
329         hme->arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
330         
331         return hme;
332 }
333
334 static HyperMesh *hypermesh_from_mesh(Mesh *me, float *extverts) {
335         HyperMesh *hme= hypermesh_new();
336         HyperVert **vert_tbl;
337         MFace *mface= me->mface;
338         int i, j;
339         
340         hme->orig_me= me;
341         if (me->tface)
342                 hme->hasvcol= hme->hasuvco= 1;
343         else if (me->mcol)
344                 hme->hasvcol= 1;
345                 
346         vert_tbl= MEM_mallocN(sizeof(*vert_tbl)*me->totvert, "vert_tbl");
347         
348         for (i= 0; i<me->totvert; i++) {
349                 if (extverts)
350                         vert_tbl[i]= hypermesh_add_vert(hme, &extverts[i*3], NULL);
351                 else
352                         vert_tbl[i]= hypermesh_add_vert(hme, me->mvert[i].co, NULL);
353         }
354
355         for (i=0; i<me->totface; i++) {
356                 MFace *mf= &mface[i];
357                 
358                 if (mf->v3) {
359                         int nverts= mf->v4?4:3;
360                         HyperVert *verts[4];
361                         HyperFace *f;
362                 
363                         verts[0]= vert_tbl[mf->v1];
364                         verts[1]= vert_tbl[mf->v2];
365                         verts[2]= vert_tbl[mf->v3];
366                         if (nverts>3)
367                                 verts[3]= vert_tbl[mf->v4];
368                 
369                         f= hypermesh_add_face(hme, verts, nverts, 1);
370                         f->orig.ind= i;
371
372                         if (hme->hasuvco) {
373                                 TFace *tf= &((TFace*) me->tface)[i];
374                         
375                                 f->uvco= BLI_memarena_alloc(hme->arena, sizeof(*f->uvco)*nverts);
376                                 for (j=0; j<nverts; j++)
377                                         Vec2Cpy(f->uvco[j], tf->uv[j]);
378
379                                 f->vcol= BLI_memarena_alloc(hme->arena, sizeof(*f->vcol)*nverts);
380                                 for (j=0; j<nverts; j++)
381                                         *((unsigned int*) f->vcol[j])= tf->col[j];
382                         } else if (hme->hasvcol) {
383                                 MCol *mcol= &me->mcol[i*4];
384                         
385                                 f->vcol= BLI_memarena_alloc(hme->arena, sizeof(*f->vcol)*nverts);
386                                 for (j=0; j<nverts; j++)
387                                         *((unsigned int*) f->vcol[j])= *((unsigned int*) &mcol[j]);
388                         }
389                 } else {
390                         hypermesh_add_edge(hme, vert_tbl[mf->v1], vert_tbl[mf->v2], 1);
391                 }
392         }
393
394         MEM_freeN(vert_tbl);
395         
396         return hme;
397 }
398
399 static HyperMesh *hypermesh_from_editmesh(EditMesh *em) {
400         HyperMesh *hme= hypermesh_new();
401         EditVert *ev, *prevev;
402         EditEdge *ee;
403         EditVlak *ef;
404
405                 /* we only add vertices with edges, 'f1' is a free flag */
406                 /* added: check for hide flag in vertices */
407         for (ev= em->verts.first; ev; ev= ev->next) ev->f1= 1;  
408
409                 /* hack, tuck the new hypervert pointer into
410                  * the ev->prev link so we can find it easy, 
411                  * then restore real prev links later.
412                  */
413         for (ee= em->edges.first; ee; ee= ee->next) {
414                 if(ee->v1->h==0 && ee->v2->h==0) {
415                         if(ee->v1->f1) {
416                                 ee->v1->prev= (EditVert*) hypermesh_add_vert(hme, ee->v1->co, ee->v1->co);
417                                 ee->v1->f1= 0;
418                         }
419                         if(ee->v2->f1) {
420                                 ee->v2->prev= (EditVert*) hypermesh_add_vert(hme, ee->v2->co, ee->v2->co);
421                                 ee->v2->f1= 0;
422                         }
423                                 
424                         hypermesh_add_edge(hme, (HyperVert*) ee->v1->prev, (HyperVert*) ee->v2->prev, 1);
425                 }
426         }
427         for (ef= em->faces.first; ef; ef= ef->next) {
428                 if(ef->v1->h || ef->v2->h || ef->v3->h);
429                 else if(ef->v4 && ef->v4->h);
430                 else {
431                         int nverts= ef->v4?4:3;
432                         HyperVert *verts[4];
433                         HyperFace *f;
434                         
435                         verts[0]= (HyperVert*) ef->v1->prev;
436                         verts[1]= (HyperVert*) ef->v2->prev;
437                         verts[2]= (HyperVert*) ef->v3->prev;
438                         if (nverts>3)
439                                 verts[3]= (HyperVert*) ef->v4->prev;
440         
441                         f= hypermesh_add_face(hme, verts, nverts, 1);
442                         f->orig.ef= ef;
443                 }
444         }
445
446                 /* see hack above, restore the prev links */
447         for (prevev= NULL, ev= em->verts.first; ev; prevev= ev, ev= ev->next)
448                 ev->prev= prevev;
449         
450         return hme;
451 }
452
453 static void VColAvgT(unsigned char *t, unsigned char *a, unsigned char *b) {
454         t[0]= (a[0]+b[0])>>1;
455         t[1]= (a[1]+b[1])>>1;
456         t[2]= (a[2]+b[2])>>1;
457         t[3]= (a[3]+b[3])>>1;
458 }
459
460 static void hypermesh_subdivide(HyperMesh *me, HyperMesh *nme) {
461         HyperVert *v;
462         HyperEdge *e;
463         HyperFace *f;
464         LinkNode *link;
465         float co[3];
466         int j, k, count;
467
468         for (f= me->faces; f; f= f->next) {
469                 Vec3CpyI(co, 0.0, 0.0, 0.0);
470                 for (j=0; j<f->nverts; j++)
471                         Vec3Add(co, f->verts[j]->co);
472                 Vec3MulN(co, (float)(1.0/f->nverts));
473
474                 f->mid= hypermesh_add_vert(nme, co, NULL);
475         }
476                 
477         for (e= me->edges; e; e= e->next) {
478                 if (hyperedge_is_boundary(e)) {
479                         Vec3AvgT(co, e->v[0]->co, e->v[1]->co);
480                 } else {
481                         Vec3AddT(co, e->v[0]->co, e->v[1]->co);
482                         for (count=2, link= e->faces; link; count++, link= link->next) {
483                                 f= (HyperFace *) link->link;
484                                 Vec3Add(co, f->mid->co);
485                         }
486                         Vec3MulN(co, (float)(1.0/count));
487                 }
488                 
489                 e->ep= hypermesh_add_vert(nme, co, NULL);
490         }
491
492         for (v= me->verts; v; v= v->next) {
493                 float q[3], r[3], s[3];
494
495                 if (hypervert_is_boundary(v)) {
496                         Vec3CpyI(r, 0.0, 0.0, 0.0);
497
498                         for (count= 0, link= v->edges; link; link= link->next) {
499                                 if (hyperedge_is_boundary(link->link)) {
500                                         HyperVert *ov= hyperedge_other_vert(link->link, v);
501
502                                         Vec3Add(r, ov->co);
503                                         count++;
504                                 }
505                         }
506
507                                 /* I believe CC give the factors as
508                                         3/2k and 1/4k, but that doesn't make
509                                         sense (to me) as they don't sum to unity... 
510                                         It's rarely important.
511                                 */
512                         Vec3MulNT(s, v->co, 0.75f);
513                         Vec3Add(s, Vec3MulN(r, (float)(1.0/(4.0*count))));
514                 } else {
515                         Vec3Cpy(q, Vec3Cpy(r, Vec3CpyI(s, 0.0f, 0.0f, 0.0f)));
516                 
517                         for (count=0, link= v->faces; link; count++, link= link->next) {
518                                 f= (HyperFace *) link->link;
519                                 Vec3Add(q, f->mid->co);
520                         }
521                         Vec3MulN(q, (float)(1.0/count));
522
523                         for (count=0, link= v->edges; link; count++, link= link->next) {
524                                 e= (HyperEdge *) link->link;
525                                 Vec3Add(r, hyperedge_other_vert(e, v)->co);
526                         }
527                         Vec3MulN(r, (float)(1.0/count));
528                 
529                         Vec3MulNT(s, v->co, (float)(count-2));
530
531                         Vec3Add(s, q);
532                         Vec3Add(s, r);
533                         Vec3MulN(s, (float)(1.0/count));
534                 }
535
536                 v->nmv= hypermesh_add_vert(nme, s, v->orig);
537         }
538
539         for (e= me->edges; e; e= e->next) {
540                 hypermesh_add_edge(nme, e->v[0]->nmv, e->ep, e->flag);
541                 hypermesh_add_edge(nme, e->v[1]->nmv, e->ep, e->flag);
542         }
543
544         for (f= me->faces; f; f= f->next) {
545                 int last= f->nverts-1;
546                 unsigned char vcol_mid[4];
547                 unsigned char vcol_edge[4][4];
548                 float uvco_mid[2];
549                 float uvco_edge[4][4];
550                 
551                 if (me->hasvcol) {
552                         int t[4]= {0, 0, 0, 0};
553                         for (j=0; j<f->nverts; j++) {
554                                 t[0]+= f->vcol[j][0];
555                                 t[1]+= f->vcol[j][1];
556                                 t[2]+= f->vcol[j][2];
557                                 t[3]+= f->vcol[j][3];
558                         }
559                         vcol_mid[0]= t[0]/f->nverts;
560                         vcol_mid[1]= t[1]/f->nverts;
561                         vcol_mid[2]= t[2]/f->nverts;
562                         vcol_mid[3]= t[3]/f->nverts;
563                         
564                         for (j=0; j<f->nverts; last= j, j++)
565                                 VColAvgT(vcol_edge[j], f->vcol[last], f->vcol[j]);
566                         last= f->nverts-1;
567                 }
568                 if (me->hasuvco) {
569                         Vec2CpyI(uvco_mid, 0.0, 0.0);
570                         for (j=0; j<f->nverts; j++)
571                                 Vec2Add(uvco_mid, f->uvco[j]);
572                         Vec2MulN(uvco_mid, (float)(1.0/f->nverts));
573
574                         for (j=0; j<f->nverts; last= j, j++)
575                                 Vec2AvgT(uvco_edge[j], f->uvco[last], f->uvco[j]);
576                         last= f->nverts-1;
577                 }
578                 
579                 for (j=0; j<f->nverts; last=j, j++) {
580                         HyperVert *nv[4];
581                         HyperFace *nf;
582                         
583                         nv[0]= f->verts[last]->nmv;
584                         nv[1]= f->edges[j]->ep;
585                         nv[2]= f->mid;
586                         nv[3]= f->edges[last]->ep;
587                         
588                         nf= hypermesh_add_face(nme, nv, 4, 0);
589                         nf->orig= f->orig;
590                         
591                         if (me->hasvcol) {
592                                 nf->vcol= BLI_memarena_alloc(nme->arena, sizeof(*nf->vcol)*4);
593                                 
594                                 for (k=0; k<4; k++) {
595                                         nf->vcol[0][k]= f->vcol[last][k];
596                                         nf->vcol[1][k]= vcol_edge[j][k];
597                                         nf->vcol[2][k]= vcol_mid[k];
598                                         nf->vcol[3][k]= vcol_edge[last][k];
599                                 }
600                         }
601                         if (me->hasuvco) {
602                                 nf->uvco= BLI_memarena_alloc(nme->arena, sizeof(*nf->uvco)*4);
603                                 
604                                 Vec2Cpy(nf->uvco[0], f->uvco[last]);
605                                 Vec2Cpy(nf->uvco[1], uvco_edge[j]);
606                                 Vec2Cpy(nf->uvco[2], uvco_mid);
607                                 Vec2Cpy(nf->uvco[3], uvco_edge[last]);
608                         }
609                 }
610         }
611 }
612
613 /* Simple subdivition surface for radio and displacement */
614 static void hypermesh_simple_subdivide(HyperMesh *me, HyperMesh *nme) {
615         HyperVert *v;
616         HyperEdge *e;
617         HyperFace *f;
618         float co[3];
619         int j, k;
620
621         for (f= me->faces; f; f= f->next) { /* Adds vert at center of each existing face */
622                 Vec3CpyI(co, 0.0, 0.0, 0.0);
623                 for (j=0; j<f->nverts; j++)     Vec3Add(co, f->verts[j]->co);
624                 Vec3MulN(co, (float)(1.0/f->nverts));
625
626                 f->mid= hypermesh_add_vert(nme, co, NULL);
627         }
628                 
629         for (e= me->edges; e; e= e->next) {  /* Add vert in middle of each edge */      
630                 Vec3AvgT(co, e->v[0]->co, e->v[1]->co);
631                 e->ep= hypermesh_add_vert(nme, co, NULL);
632         }
633
634         for (v= me->verts; v; v= v->next) {
635                 v->nmv= hypermesh_add_vert(nme, v->co, v->orig);
636         }
637
638         for (e= me->edges; e; e= e->next) { /* Add originam edges */
639                 hypermesh_add_edge(nme, e->v[0]->nmv, e->ep, e->flag);
640                 hypermesh_add_edge(nme, e->v[1]->nmv, e->ep, e->flag);
641         }
642
643         for (f= me->faces; f; f= f->next) {
644                 int last= f->nverts-1;
645                 unsigned char vcol_mid[4];
646                 unsigned char vcol_edge[4][4];
647                 float uvco_mid[2];
648                 float uvco_edge[4][4];
649                 
650                 if (me->hasvcol) {
651                         int t[4]= {0, 0, 0, 0};
652                         for (j=0; j<f->nverts; j++) {
653                                 t[0]+= f->vcol[j][0];
654                                 t[1]+= f->vcol[j][1];
655                                 t[2]+= f->vcol[j][2];
656                                 t[3]+= f->vcol[j][3];
657                         }
658                         vcol_mid[0]= t[0]/f->nverts;
659                         vcol_mid[1]= t[1]/f->nverts;
660                         vcol_mid[2]= t[2]/f->nverts;
661                         vcol_mid[3]= t[3]/f->nverts;
662                         
663                         for (j=0; j<f->nverts; last= j, j++)
664                                 VColAvgT(vcol_edge[j], f->vcol[last], f->vcol[j]);
665                         last= f->nverts-1;
666                 }
667                 if (me->hasuvco) {
668                         Vec2CpyI(uvco_mid, 0.0, 0.0);
669                         for (j=0; j<f->nverts; j++)
670                                 Vec2Add(uvco_mid, f->uvco[j]);
671                         Vec2MulN(uvco_mid, (float)(1.0/f->nverts));
672
673                         for (j=0; j<f->nverts; last= j, j++)
674                                 Vec2AvgT(uvco_edge[j], f->uvco[last], f->uvco[j]);
675                         last= f->nverts-1;
676                 }
677                 
678                 for (j=0; j<f->nverts; last=j, j++) {
679                         HyperVert *nv[4];
680                         HyperFace *nf;
681                         
682                         nv[0]= f->verts[last]->nmv;
683                         nv[1]= f->edges[j]->ep;
684                         nv[2]= f->mid;
685                         nv[3]= f->edges[last]->ep;
686                         
687                         nf= hypermesh_add_face(nme, nv, 4, 0);
688                         nf->orig= f->orig;
689                         
690                         if (me->hasvcol) {
691                                 nf->vcol= BLI_memarena_alloc(nme->arena, sizeof(*nf->vcol)*4);
692                                 
693                                 for (k=0; k<4; k++) {
694                                         nf->vcol[0][k]= f->vcol[last][k];
695                                         nf->vcol[1][k]= vcol_edge[j][k];
696                                         nf->vcol[2][k]= vcol_mid[k];
697                                         nf->vcol[3][k]= vcol_edge[last][k];
698                                 }
699                         }
700                         if (me->hasuvco) {
701                                 nf->uvco= BLI_memarena_alloc(nme->arena, sizeof(*nf->uvco)*4);
702                                 
703                                 Vec2Cpy(nf->uvco[0], f->uvco[last]);
704                                 Vec2Cpy(nf->uvco[1], uvco_edge[j]);
705                                 Vec2Cpy(nf->uvco[2], uvco_mid);
706                                 Vec2Cpy(nf->uvco[3], uvco_edge[last]);
707                         }
708                 }
709         }
710 }
711
712
713 static void hypermesh_free(HyperMesh *me) {
714         BLI_memarena_free(me->arena);
715                         
716         MEM_freeN(me);
717 }
718
719 /*****/
720
721 static int hypermesh_get_nverts(HyperMesh *hme) {
722         HyperVert *v;
723         int count= 0;
724         
725         for (v= hme->verts; v; v= v->next)
726                 count++;
727         
728         return count;
729 }
730
731 static int hypermesh_get_nverts_handles(HyperMesh *hme) {
732         HyperVert *v;
733         int count= 0;
734         
735         for (v= hme->verts; v; v= v->next)
736                 if(v->orig) count++;
737         
738         return count;
739 }
740
741 static int hypermesh_get_nfaces(HyperMesh *hme) {
742         HyperFace *f;
743         int count= 0;
744         
745         for (f= hme->faces; f; f= f->next)
746                 count++;
747         
748         return count;
749 }
750
751 static int hypermesh_get_nlines(HyperMesh *hme) {
752         HyperEdge *e;
753         int n= 0;
754         
755         for (e= hme->edges; e; e= e->next)
756                 if (!e->faces)
757                         n++;
758         
759         return n;
760 }
761
762 /* flag is me->flag, for handles and 'optim' */
763 static DispListMesh *hypermesh_to_displistmesh(HyperMesh *hme, short flag) {
764         int nverts= hypermesh_get_nverts(hme);
765         int nfaces= hypermesh_get_nfaces(hme) + hypermesh_get_nlines(hme);
766         DispListMesh *dlm= MEM_callocN(sizeof(*dlm), "dlmesh");
767         HyperFace *f;
768         HyperVert *v;
769         HyperEdge *e;
770         TFace *tfaces;
771         MFace *mfaces;
772         MFace *mf;
773         int i, j, handles=0;
774
775                 /* hme->orig_me==NULL if we are working on an editmesh */
776         if (hme->orig_me) {
777                 tfaces= hme->orig_me->tface;
778                 mfaces= hme->orig_me->mface;
779         } else {
780                 tfaces= NULL;
781                 mfaces= NULL;
782         }
783
784         /* added: handles for editmode */
785         if (hme->orig_me==NULL && (flag & ME_OPT_EDGES)) {
786                 handles= hypermesh_get_nverts_handles(hme);
787         }
788         
789         dlm->totvert= nverts+handles;
790         dlm->totface= nfaces+handles;
791         /* calloc for clear flag and nor in mvert */
792         dlm->mvert= MEM_callocN(dlm->totvert*sizeof(*dlm->mvert), "dlm->mvert");
793         dlm->mface= MEM_mallocN(dlm->totface*sizeof(*dlm->mface), "dlm->mface");
794         if (hme->orig_me) {
795                 dlm->flag= hme->orig_me->flag;
796         } else {
797                 dlm->flag= flag;
798         }
799         
800         if (hme->hasuvco)
801                 dlm->tface= MEM_callocN(dlm->totface*sizeof(*dlm->tface), "dlm->tface");
802         else if (hme->hasvcol)
803                 dlm->mcol= MEM_mallocN(dlm->totface*4*sizeof(*dlm->mcol), "dlm->mcol");
804                                 
805         for (i=0, v= hme->verts; i<nverts; i++, v= v->next) {
806                 MVert *mv= &dlm->mvert[i];
807                 Vec3Cpy(mv->co, v->co);
808                 v->nmv= (void*) i;
809         }
810         
811         mf= dlm->mface;
812         for (i=0, f= hme->faces; f; i++, f= f->next) {
813                         /* There is a complicated dependancy here:
814                          * After a subdivision the points that were shifted will always be
815                          * first in the hme->verts list (because they are added last, but to
816                          * the head). This means that the HVert with index 0 will always be
817                          * a shifted vertice, and the shifted vertices (corners) are always
818                          * HFace->verts[0]. Therefore it is guaranteed that if any vertice
819                          * index is 0, it will always be in mf->v1, so we do not have to worry
820                          * about tweaking the indices.
821                          */
822                 mf->v1= (int) f->verts[0]->nmv;
823                 mf->v2= (int) f->verts[1]->nmv;
824                 mf->v3= (int) f->verts[2]->nmv;
825                 mf->v4= (int) f->verts[3]->nmv;
826
827                 if (hme->orig_me) {                     
828                         MFace *origmf= &mfaces[f->orig.ind];
829
830                         mf->mat_nr= origmf->mat_nr;
831                         mf->flag= origmf->flag;
832                         mf->puno= 0;
833                 } else {
834                         EditVlak *origef= f->orig.ef;
835                         
836                         mf->mat_nr= origef->mat_nr;
837                         mf->flag= origef->flag;
838                         mf->puno= 0;
839                 }
840                 
841                 mf->edcode= 0;
842                 if (f->edges[0]->flag) mf->edcode|= ME_V4V1;
843                 if (f->edges[1]->flag) mf->edcode|= ME_V1V2;
844                 if (f->edges[2]->flag) mf->edcode|= ME_V2V3;
845                 if (f->edges[3]->flag) mf->edcode|= ME_V3V4;
846
847                 if (hme->hasuvco) {
848                         TFace *origtf, *tf= &dlm->tface[i];
849                         
850                         //if (hme->orig_me)
851                                 origtf= &tfaces[f->orig.ind];
852                         //else          ton: removed, hme->hasuvco doesn't happen in editmode (yet?)
853                         //      origtf= f->orig.ef->tface;
854                         
855                         for (j=0; j<4; j++) {
856                                 Vec2Cpy(tf->uv[j], f->uvco[j]);
857                                 tf->col[j]= *((unsigned int*) f->vcol[j]);
858                         }
859                         
860                         tf->tpage= origtf->tpage;
861                         tf->flag= origtf->flag;
862                         tf->transp= origtf->transp;
863                         tf->mode= origtf->mode;
864                         tf->tile= origtf->tile;
865                 } else if (hme->hasvcol) {
866                         MCol *mcolbase= &dlm->mcol[i*4];
867                         
868                         for (j=0; j<4; j++)
869                                 *((unsigned int*) &mcolbase[j])= *((unsigned int*) f->vcol[j]);
870                 }
871                 
872                 mf++;
873         }
874
875         for (e= hme->edges; e; e= e->next) {
876                 if (!e->faces) {
877                         mf->v1= (int) e->v[0]->nmv;
878                         mf->v2= (int) e->v[1]->nmv;
879                         mf->v3= 0;
880                         mf->v4= 0;
881                         
882                         mf->mat_nr= 0;
883                         mf->flag= 0;
884                         mf->puno= 0;
885                         mf->edcode= ME_V1V2;
886                                 
887                         mf++;
888                 }
889         }
890         
891         /* and we add the handles */
892         if(handles) {
893                 MVert *mv= dlm->mvert+nverts;
894                 mf= dlm->mface+nfaces;
895                 i= nverts;
896                 for (v= hme->verts; v; v= v->next) {
897                         if(v->orig) {
898                                 /* new vertex */
899                                 Vec3Cpy(mv->co, v->orig);
900
901                                 /* new face */
902                                 mf->v1= (int) v->nmv;
903                                 mf->v2= i;
904                                 mf->v3= 0;
905                                 mf->v4= 0;
906                                 
907                                 mf->mat_nr= 0;
908                                 mf->flag= 0;
909                                 mf->puno= 0;
910                                 mf->edcode= ME_V1V2;
911                                         
912                                 mf++; i++; mv++;
913                         }
914                 }
915         }       
916         
917         displistmesh_calc_vert_normals(dlm);
918
919         return dlm;
920 }
921
922 /* flag is me->flag, for handles and 'optim' */
923 static DispListMesh *subsurf_subdivide_to_displistmesh(HyperMesh *hme, short subdiv,
924                                                                                                 short flag, short type) {
925         DispListMesh *dlm;
926         int i;
927
928         for (i= 0; i<subdiv; i++) {
929                 HyperMesh *tmp= hypermesh_new();
930                 tmp->hasvcol= hme->hasvcol;
931                 tmp->hasuvco= hme->hasuvco;
932                 tmp->orig_me= hme->orig_me;
933                 
934                 if (type == ME_SIMPLE_SUBSURF) hypermesh_simple_subdivide(hme, tmp);
935                 else hypermesh_subdivide(hme, tmp);      /* default to CC subdiv. */
936                 
937                 hypermesh_free(hme);
938                 hme= tmp;
939         }
940
941         dlm= hypermesh_to_displistmesh(hme, flag);
942         hypermesh_free(hme);
943         
944         return dlm;
945 }
946
947 DispListMesh *subsurf_make_dispListMesh_from_editmesh(EditMesh *em, int subdivLevels, int flags, short type) {
948         if (subdivLevels<1) {
949                 return displistmesh_from_editmesh(em);
950         } else {
951                 HyperMesh *hme= hypermesh_from_editmesh(em);
952         
953                 return subsurf_subdivide_to_displistmesh(hme, subdivLevels, flags, type);
954         }
955 }
956
957 DispListMesh *subsurf_make_dispListMesh_from_mesh(Mesh *me, float *extverts, int subdivLevels, int flags) {
958         if (subdivLevels<1) {
959                 return displistmesh_from_mesh(me, extverts);
960         } else {
961                 HyperMesh *hme= hypermesh_from_mesh(me, extverts);
962
963                 return subsurf_subdivide_to_displistmesh(hme, subdivLevels, flags, me->subsurftype);
964         }
965 }
966
967         // editarmature.c
968 void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]) 
969 {
970         /* Finds the subsurf limit positions for the verts in a mesh 
971          * and puts them in an array of floats. Please note that the 
972          * calculated vert positions is incorrect for the verts 
973          * on the boundary of the mesh.
974          */
975         HyperMesh *hme= hypermesh_from_mesh(me, NULL);
976         HyperMesh *nme= hypermesh_new();
977         float edge_sum[3], face_sum[3];
978         HyperVert *hv;
979         LinkNode *l;
980         int i;
981
982         hypermesh_subdivide(hme, nme);
983
984         for (i= me->totvert-1,hv=hme->verts; i>=0; i--,hv=hv->next) {
985                 int N= 0;
986                 
987                 edge_sum[0]= edge_sum[1]= edge_sum[2]= 0.0;
988                 face_sum[0]= face_sum[1]= face_sum[2]= 0.0;
989
990                 for (N=0,l=hv->edges; l; N++,l= l->next) {
991                         Vec3Add(edge_sum, ((HyperEdge*) l->link)->ep->co);
992                 }
993                 for (l=hv->faces; l; l= l->next) {
994                         Vec3Add(face_sum, ((HyperFace*) l->link)->mid->co);
995                 }
996
997                 positions_r[i][0] = 
998                         (hv->nmv->co[0]*N*N + edge_sum[0]*4 + face_sum[0])/(N*(N+5));
999                 positions_r[i][1] = 
1000                         (hv->nmv->co[1]*N*N + edge_sum[1]*4 + face_sum[1])/(N*(N+5));
1001                 positions_r[i][2] = 
1002                         (hv->nmv->co[2]*N*N + edge_sum[2]*4 + face_sum[2])/(N*(N+5));
1003         }
1004
1005         hypermesh_free(nme);
1006         hypermesh_free(hme);
1007 }