svn merge ^/trunk/blender -r41226:41227 .
[blender.git] / source / blender / modifiers / intern / MOD_screw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_screw.c
32  *  \ingroup modifiers
33  */
34
35
36 /* Screw modifier: revolves the edges about an axis */
37
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43
44
45 #include "BKE_cdderivedmesh.h"
46
47 #include "depsgraph_private.h"
48 #include "MOD_modifiertypes.h"
49 #include "MEM_guardedalloc.h"
50
51 /* used for gathering edge connectivity */
52 typedef struct ScrewVertConnect {
53         float dist;  /* distance from the center axis */
54         float co[3]; /* loaction relative to the transformed axis */
55         float no[3]; /* calc normal of the vertex */
56         int v[2]; /* 2  verts on either side of this one */
57         MEdge *e[2]; /* edges on either side, a bit of a waste since each edge ref's 2 edges */
58         char flag;
59 } ScrewVertConnect;
60
61 typedef struct ScrewVertIter {
62         ScrewVertConnect * v_array;
63         ScrewVertConnect * v_poin;
64         int v;
65         int v_other;
66         MEdge *e;
67 } ScrewVertIter;
68
69
70 static void screwvert_iter_init(ScrewVertIter *iter, ScrewVertConnect *array, int v_init, int dir)
71 {
72         iter->v_array = array;
73         iter->v = v_init;
74
75         if (v_init >= 0) {
76                 iter->v_poin = &array[v_init];
77                 iter->v_other = iter->v_poin->v[dir];
78                 iter->e = iter->v_poin->e[!dir];
79         }
80         else {
81                 iter->v_poin= NULL;
82                 iter->e= NULL;
83         }
84 }       
85
86
87 static void screwvert_iter_step(ScrewVertIter *iter)
88 {
89         if (iter->v_poin->v[0] == iter->v_other) {
90                 iter->v_other= iter->v;
91                 iter->v= iter->v_poin->v[1];
92         }
93         else if (iter->v_poin->v[1] == iter->v_other) {
94                 iter->v_other= iter->v;
95                 iter->v= iter->v_poin->v[0];
96         }
97         if (iter->v >= 0)       {
98                 iter->v_poin= &iter->v_array[iter->v];
99                 iter->e= iter->v_poin->e[(iter->v_poin->e[0] == iter->e)];
100         }
101         else {
102                 iter->e= NULL;
103                 iter->v_poin= NULL;
104         }
105 }
106
107
108 static void initData(ModifierData *md)
109 {
110         ScrewModifierData *ltmd= (ScrewModifierData*) md;
111         ltmd->ob_axis= NULL;
112         ltmd->angle= M_PI * 2.0;
113         ltmd->axis= 2;
114         ltmd->flag= 0;
115         ltmd->steps= 16;
116         ltmd->render_steps= 16;
117         ltmd->iter= 1;
118 }
119
120 static void copyData(ModifierData *md, ModifierData *target)
121 {
122         ScrewModifierData *sltmd= (ScrewModifierData*) md;
123         ScrewModifierData *tltmd= (ScrewModifierData*) target;
124         
125         tltmd->ob_axis= sltmd->ob_axis;
126         tltmd->angle= sltmd->angle;
127         tltmd->axis= sltmd->axis;
128         tltmd->flag= sltmd->flag;
129         tltmd->steps= sltmd->steps;
130         tltmd->render_steps= sltmd->render_steps;
131         tltmd->screw_ofs= sltmd->screw_ofs;
132         tltmd->iter= sltmd->iter;
133 }
134
135 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
136                                                 DerivedMesh *derivedData,
137                                                 int useRenderParams,
138                                                 int UNUSED(isFinalCalc))
139 {
140         DerivedMesh *dm= derivedData;
141         DerivedMesh *result;
142         ScrewModifierData *ltmd= (ScrewModifierData*) md;
143         
144         int *origindex;
145         int mface_index=0;
146         int step;
147         int i, j;
148         int i1,i2;
149         int step_tot= useRenderParams ? ltmd->render_steps : ltmd->steps;
150         const int do_flip = ltmd->flag & MOD_SCREW_NORMAL_FLIP ? 1 : 0;
151         int maxVerts=0, maxEdges=0, maxFaces=0;
152         int totvert= dm->getNumVerts(dm);
153         int totedge= dm->getNumEdges(dm);
154
155         char axis_char= 'X', close;
156         float angle= ltmd->angle;
157         float screw_ofs= ltmd->screw_ofs;
158         float axis_vec[3]= {0.0f, 0.0f, 0.0f};
159         float tmp_vec1[3], tmp_vec2[3]; 
160         float mat3[3][3];
161         float mtx_tx[4][4]; /* transform the coords by an object relative to this objects transformation */
162         float mtx_tx_inv[4][4]; /* inverted */
163         float mtx_tmp_a[4][4];
164         
165         int vc_tot_linked= 0;
166         short other_axis_1, other_axis_2;
167         float *tmpf1, *tmpf2;
168         
169         MFace *mface_new, *mf_new;
170         MEdge *medge_orig, *med_orig, *med_new, *med_new_firstloop, *medge_new;
171         MVert *mvert_new, *mvert_orig, *mv_orig, *mv_new, *mv_new_base;
172
173         ScrewVertConnect *vc, *vc_tmp, *vert_connect= NULL;
174
175         /* dont do anything? */
176         if (!totvert)
177                 return CDDM_from_template(dm, 0, 0, 0, 0, 0);
178
179         switch(ltmd->axis) {
180         case 0:
181                 other_axis_1=1;
182                 other_axis_2=2;
183                 break;
184         case 1:
185                 other_axis_1=0;
186                 other_axis_2=2;
187                 break;
188         default: /* 2, use default to quiet warnings */
189                 other_axis_1=0;
190                 other_axis_2=1;
191                 break;
192         }
193
194         axis_vec[ltmd->axis]= 1.0f;
195
196         if (ltmd->ob_axis) {
197                 /* calc the matrix relative to the axis object */
198                 invert_m4_m4(mtx_tmp_a, ob->obmat);
199                 copy_m4_m4(mtx_tx_inv, ltmd->ob_axis->obmat);
200                 mul_m4_m4m4(mtx_tx, mtx_tx_inv, mtx_tmp_a);
201
202                 /* calc the axis vec */
203                 mul_mat3_m4_v3(mtx_tx, axis_vec); /* only rotation component */
204                 normalize_v3(axis_vec);
205
206                 /* screw */
207                 if(ltmd->flag & MOD_SCREW_OBJECT_OFFSET) {
208                         /* find the offset along this axis relative to this objects matrix */
209                         float totlen = len_v3(mtx_tx[3]);
210
211                         if(totlen != 0.0f) {
212                                 float zero[3]={0.0f, 0.0f, 0.0f};
213                                 float cp[3];                            
214                                 screw_ofs= closest_to_line_v3(cp, mtx_tx[3], zero, axis_vec);
215                         }
216                         else {
217                                 screw_ofs= 0.0f;
218                         }
219                 }
220
221                 /* angle */
222
223 #if 0   // cant incluide this, not predictable enough, though quite fun,.
224                 if(ltmd->flag & MOD_SCREW_OBJECT_ANGLE) {
225                         float mtx3_tx[3][3];
226                         copy_m3_m4(mtx3_tx, mtx_tx);
227
228                         float vec[3] = {0,1,0};
229                         float cross1[3];
230                         float cross2[3];
231                         cross_v3_v3v3(cross1, vec, axis_vec);
232
233                         mul_v3_m3v3(cross2, mtx3_tx, cross1);
234                         {
235                                 float c1[3];
236                                 float c2[3];
237                                 float axis_tmp[3];
238
239                                 cross_v3_v3v3(c1, cross2, axis_vec);
240                                 cross_v3_v3v3(c2, axis_vec, c1);
241
242
243                                 angle= angle_v3v3(cross1, c2);
244
245                                 cross_v3_v3v3(axis_tmp, cross1, c2);
246                                 normalize_v3(axis_tmp);
247
248                                 if(len_v3v3(axis_tmp, axis_vec) > 1.0f)
249                                         angle= -angle;
250
251                         }
252                 }
253 #endif
254         }
255         else {
256                 /* exis char is used by i_rotate*/
257                 axis_char += ltmd->axis; /* 'X' + axis */
258
259                 /* useful to be able to use the axis vec in some cases still */
260                 zero_v3(axis_vec);
261                 axis_vec[ltmd->axis]= 1.0f;
262         }
263
264         /* apply the multiplier */
265         angle *= ltmd->iter;
266         screw_ofs *= ltmd->iter;
267
268         /* multiplying the steps is a bit tricky, this works best */
269         step_tot = ((step_tot + 1) * ltmd->iter) - (ltmd->iter - 1);
270
271         /* will the screw be closed?
272          * Note! smaller then FLT_EPSILON*100 gives problems with float precision so its never closed. */
273         if (fabsf(screw_ofs) <= (FLT_EPSILON*100.0f) && fabsf(fabsf(angle) - ((float)M_PI * 2.0f)) <= (FLT_EPSILON*100.0f)) {
274                 close= 1;
275                 step_tot--;
276                 if(step_tot < 3) step_tot= 3;
277         
278                 maxVerts =      totvert  * step_tot; /* -1 because we're joining back up */
279                 maxEdges =      (totvert * step_tot) + /* these are the edges between new verts */
280                                         (totedge * step_tot); /* -1 because vert edges join */
281                 maxFaces =      totedge * step_tot;
282
283                 screw_ofs= 0.0f;
284         }
285         else {
286                 close= 0;
287                 if(step_tot < 3) step_tot= 3;
288
289                 maxVerts =      totvert  * step_tot; /* -1 because we're joining back up */
290                 maxEdges =      (totvert * (step_tot-1)) + /* these are the edges between new verts */
291                                         (totedge * step_tot); /* -1 because vert edges join */
292                 maxFaces =      totedge * (step_tot-1);
293         }
294         
295         result= CDDM_from_template(dm, maxVerts, maxEdges, maxFaces, 0, 0);
296         
297         /* copy verts from mesh */
298         mvert_orig =    dm->getVertArray(dm);
299         medge_orig =    dm->getEdgeArray(dm);
300         
301         mvert_new =             result->getVertArray(result);
302         mface_new =             result->getTessFaceArray(result);
303         medge_new =             result->getEdgeArray(result);
304         
305         origindex= result->getTessFaceDataArray(result, CD_ORIGINDEX);
306
307         DM_copy_vert_data(dm, result, 0, 0, totvert); /* copy first otherwise this overwrites our own vertex normals */
308         
309         /* Set the locations of the first set of verts */
310         
311         mv_new= mvert_new;
312         mv_orig= mvert_orig;
313         
314         /* Copy the first set of edges */
315         med_orig= medge_orig;
316         med_new= medge_new;
317         for (i=0; i < totedge; i++, med_orig++, med_new++) {
318                 med_new->v1= med_orig->v1;
319                 med_new->v2= med_orig->v2;
320                 med_new->crease= med_orig->crease;
321                 med_new->flag= med_orig->flag &  ~ME_LOOSEEDGE;
322         }
323         
324         if(ltmd->flag & MOD_SCREW_NORMAL_CALC) {
325                 /*
326                  * Normal Calculation (for face flipping)
327                  * Sort edge verts for correct face flipping
328                  * NOT REALLY NEEDED but face flipping is nice.
329                  *
330                  * */
331
332
333                 /* Notice!
334                  *
335                  * Since we are only ordering the edges here it can avoid mallocing the
336                  * extra space by abusing the vert array berfore its filled with new verts.
337                  * The new array for vert_connect must be at least sizeof(ScrewVertConnect) * totvert
338                  * and the size of our resulting meshes array is sizeof(MVert) * totvert * 3
339                  * so its safe to use the second 2 thrids of MVert the array for vert_connect,
340                  * just make sure ScrewVertConnect struct is no more then twice as big as MVert,
341                  * at the moment there is no chance of that being a problem,
342                  * unless MVert becomes half its current size.
343                  *
344                  * once the edges are ordered, vert_connect is not needed and it can be used for verts
345                  *
346                  * This makes the modifier faster with one less alloc.
347                  */
348
349                 vert_connect= MEM_mallocN(sizeof(ScrewVertConnect) * totvert, "ScrewVertConnect");
350                 //vert_connect= (ScrewVertConnect *) &medge_new[totvert]; /* skip the first slice of verts */
351                 vc= vert_connect;
352
353                 /* Copy Vert Locations */
354                 /* - We can do this in a later loop - only do here if no normal calc */
355                 if (!totedge) {
356                         for (i=0; i < totvert; i++, mv_orig++, mv_new++) {
357                                 copy_v3_v3(mv_new->co, mv_orig->co);
358                                 normalize_v3_v3(vc->no, mv_new->co); /* no edges- this is really a dummy normal */
359                         }
360                 }
361                 else {
362                         /*printf("\n\n\n\n\nStarting Modifier\n");*/
363                         /* set edge users */
364                         med_new= medge_new;
365                         mv_new= mvert_new;
366
367                         if (ltmd->ob_axis) {
368                                 /*mtx_tx is initialized early on */
369                                 for (i=0; i < totvert; i++, mv_new++, mv_orig++, vc++) {
370                                         vc->co[0]= mv_new->co[0]= mv_orig->co[0];
371                                         vc->co[1]= mv_new->co[1]= mv_orig->co[1];
372                                         vc->co[2]= mv_new->co[2]= mv_orig->co[2];
373
374                                         vc->flag= 0;
375                                         vc->e[0]= vc->e[1]= NULL;
376                                         vc->v[0]= vc->v[1]= -1;
377
378                                         mul_m4_v3(mtx_tx, vc->co);
379                                         /* length in 2d, dont sqrt because this is only for comparison */
380                                         vc->dist =      vc->co[other_axis_1]*vc->co[other_axis_1] +
381                                                                 vc->co[other_axis_2]*vc->co[other_axis_2];
382
383                                         /* printf("location %f %f %f -- %f\n", vc->co[0], vc->co[1], vc->co[2], vc->dist);*/
384                                 }
385                         }
386                         else {
387                                 for (i=0; i < totvert; i++, mv_new++, mv_orig++, vc++) {
388                                         vc->co[0]= mv_new->co[0]= mv_orig->co[0];
389                                         vc->co[1]= mv_new->co[1]= mv_orig->co[1];
390                                         vc->co[2]= mv_new->co[2]= mv_orig->co[2];
391
392                                         vc->flag= 0;
393                                         vc->e[0]= vc->e[1]= NULL;
394                                         vc->v[0]= vc->v[1]= -1;
395
396                                         /* length in 2d, dont sqrt because this is only for comparison */
397                                         vc->dist =      vc->co[other_axis_1]*vc->co[other_axis_1] +
398                                                                 vc->co[other_axis_2]*vc->co[other_axis_2];
399
400                                         /* printf("location %f %f %f -- %f\n", vc->co[0], vc->co[1], vc->co[2], vc->dist);*/
401                                 }
402                         }
403
404                         /* this loop builds connectivity info for verts */
405                         for (i=0; i<totedge; i++, med_new++) {
406                                 vc= &vert_connect[med_new->v1];
407
408                                 if (vc->v[0] == -1) { /* unused */
409                                         vc->v[0]= med_new->v2;
410                                         vc->e[0]= med_new;
411                                 }
412                                 else if (vc->v[1] == -1) {
413                                         vc->v[1]= med_new->v2;
414                                         vc->e[1]= med_new;
415                                 }
416                                 else {
417                                         vc->v[0]= vc->v[1]= -2; /* erro value  - dont use, 3 edges on vert */
418                                 }
419
420                                 vc= &vert_connect[med_new->v2];
421
422                                 /* same as above but swap v1/2 */
423                                 if (vc->v[0] == -1) { /* unused */
424                                         vc->v[0]= med_new->v1;
425                                         vc->e[0]= med_new;
426                                 }
427                                 else if (vc->v[1] == -1) {
428                                         vc->v[1]= med_new->v1;
429                                         vc->e[1]= med_new;
430                                 }
431                                 else {
432                                         vc->v[0]= vc->v[1]= -2; /* erro value  - dont use, 3 edges on vert */
433                                 }
434                         }
435
436                         /* find the first vert */
437                         vc= vert_connect;
438                         for (i=0; i < totvert; i++, vc++) {
439                                 /* Now do search for connected verts, order all edges and flip them
440                                  * so resulting faces are flipped the right way */
441                                 vc_tot_linked= 0; /* count the number of linked verts for this loop */
442                                 if (vc->flag == 0) {
443                                         int v_best=-1, ed_loop_closed=0; /* vert and vert new */
444                                         ScrewVertIter lt_iter;
445                                         int ed_loop_flip= 0; /* compiler complains if not initialized, but it should be initialized below */
446                                         float fl= -1.0f;
447
448                                         /*printf("Loop on connected vert: %i\n", i);*/
449
450                                         for(j=0; j<2; j++) {
451                                                 /*printf("\tSide: %i\n", j);*/
452                                                 screwvert_iter_init(&lt_iter, vert_connect, i, j);
453                                                 if (j == 1) {
454                                                         screwvert_iter_step(&lt_iter);
455                                                 }
456                                                 while (lt_iter.v_poin) {
457                                                         /*printf("\t\tVERT: %i\n", lt_iter.v);*/
458                                                         if (lt_iter.v_poin->flag) {
459                                                                 /*printf("\t\t\tBreaking Found end\n");*/
460                                                                 //endpoints[0]= endpoints[1]= -1;
461                                                                 ed_loop_closed= 1; /* circle */
462                                                                 break;
463                                                         }
464                                                         lt_iter.v_poin->flag= 1;
465                                                         vc_tot_linked++;
466                                                         /*printf("Testing 2 floats %f : %f\n", fl, lt_iter.v_poin->dist);*/
467                                                         if (fl <= lt_iter.v_poin->dist) {
468                                                                 fl= lt_iter.v_poin->dist;
469                                                                 v_best= lt_iter.v;
470                                                                 /*printf("\t\t\tVERT BEST: %i\n", v_best);*/
471                                                         }
472                                                         screwvert_iter_step(&lt_iter);
473                                                         if (!lt_iter.v_poin) {
474                                                                 /*printf("\t\t\tFound End Also Num %i\n", j);*/
475                                                                 /*endpoints[j]= lt_iter.v_other;*/ /* other is still valid */
476                                                                 break;
477                                                         }
478                                                 }
479                                         }
480
481                                         /* now we have a collection of used edges. flip their edges the right way*/
482                                         /*if (v_best != -1) - */
483
484                                         /*printf("Done Looking - vc_tot_linked: %i\n", vc_tot_linked);*/
485
486                                         if (vc_tot_linked>1) {
487                                                 float vf_1, vf_2, vf_best;
488
489                                                 vc_tmp= &vert_connect[v_best];
490
491                                                 tmpf1= vert_connect[vc_tmp->v[0]].co;
492                                                 tmpf2= vert_connect[vc_tmp->v[1]].co;
493
494
495                                                 /* edge connects on each side! */
496                                                 if ((vc_tmp->v[0] > -1) && (vc_tmp->v[1] > -1)) {
497                                                         /*printf("Verts on each side (%i %i)\n", vc_tmp->v[0], vc_tmp->v[1]);*/
498                                                         /* find out which is higher */
499
500                                                         vf_1= tmpf1[ltmd->axis];
501                                                         vf_2= tmpf2[ltmd->axis];
502                                                         vf_best= vc_tmp->co[ltmd->axis];
503
504                                                         if (vf_1 < vf_best && vf_best < vf_2) {
505                                                                 ed_loop_flip= 0;
506                                                         }
507                                                         else if (vf_1 > vf_best && vf_best > vf_2) {
508                                                                 ed_loop_flip= 1;
509                                                         }
510                                                         else {
511                                                                 /* not so simple to work out which edge is higher */
512                                                                 sub_v3_v3v3(tmp_vec1, tmpf1, vc_tmp->co);
513                                                                 sub_v3_v3v3(tmp_vec2, tmpf2, vc_tmp->co);
514                                                                 normalize_v3(tmp_vec1);
515                                                                 normalize_v3(tmp_vec2);
516
517                                                                 if (tmp_vec1[ltmd->axis] < tmp_vec2[ltmd->axis]) {
518                                                                         ed_loop_flip= 1;
519                                                                 }
520                                                                 else {
521                                                                         ed_loop_flip= 0;
522                                                                 }
523                                                         }
524                                                 }
525                                                 else if (vc_tmp->v[0] >= 0) { /*vertex only connected on 1 side */
526                                                         /*printf("Verts on ONE side (%i %i)\n", vc_tmp->v[0], vc_tmp->v[1]);*/
527                                                         if (tmpf1[ltmd->axis] < vc_tmp->co[ltmd->axis]) { /* best is above */
528                                                                 ed_loop_flip= 1;
529                                                         }
530                                                         else { /* best is below or even... in even case we cant know whet  to do. */
531                                                                 ed_loop_flip= 0;
532                                                         }
533
534                                                 }/* else {
535                                                         printf("No Connected ___\n");
536                                                 }*/
537
538                                                 /*printf("flip direction %i\n", ed_loop_flip);*/
539
540
541                                                 /* switch the flip option if set
542                                                  * note: flip is now done at face level so copying vgroup slizes is easier */
543                                                 /*                                              
544                                                 if (do_flip)
545                                                         ed_loop_flip= !ed_loop_flip;
546                                                 */
547
548                                                 if (angle < 0.0f)
549                                                         ed_loop_flip= !ed_loop_flip;
550
551                                                 /* if its closed, we only need 1 loop */
552                                                 for(j=ed_loop_closed; j<2; j++) {
553                                                         /*printf("Ordering Side J %i\n", j);*/
554
555                                                         screwvert_iter_init(&lt_iter, vert_connect, v_best, j);
556                                                         /*printf("\n\nStarting - Loop\n");*/
557                                                         lt_iter.v_poin->flag= 1; /* so a non loop will traverse the other side */
558
559
560                                                         /* If this is the vert off the best vert and
561                                                          * the best vert has 2 edges connected too it
562                                                          * then swap the flip direction */
563                                                         if (j == 1 && (vc_tmp->v[0] > -1) && (vc_tmp->v[1] > -1))
564                                                                 ed_loop_flip= !ed_loop_flip;
565
566                                                         while (lt_iter.v_poin && lt_iter.v_poin->flag != 2) {
567                                                                 /*printf("\tOrdering Vert V %i\n", lt_iter.v);*/
568
569                                                                 lt_iter.v_poin->flag= 2;
570                                                                 if (lt_iter.e) {
571                                                                         if (lt_iter.v == lt_iter.e->v1) {
572                                                                                 if (ed_loop_flip == 0) {
573                                                                                         /*printf("\t\t\tFlipping 0\n");*/
574                                                                                         SWAP(int, lt_iter.e->v1, lt_iter.e->v2);
575                                                                                 }/* else {
576                                                                                         printf("\t\t\tFlipping Not 0\n");
577                                                                                 }*/
578                                                                         }
579                                                                         else if (lt_iter.v == lt_iter.e->v2) {
580                                                                                 if (ed_loop_flip == 1) {
581                                                                                         /*printf("\t\t\tFlipping 1\n");*/
582                                                                                         SWAP(int, lt_iter.e->v1, lt_iter.e->v2);
583                                                                                 }/* else {
584                                                                                         printf("\t\t\tFlipping Not 1\n");
585                                                                                 }*/
586                                                                         }/* else {
587                                                                                 printf("\t\tIncorrect edge topology");
588                                                                         }*/
589                                                                 }/* else {
590                                                                         printf("\t\tNo Edge at this point\n");
591                                                                 }*/
592                                                                 screwvert_iter_step(&lt_iter);
593                                                         }
594                                                 }
595                                         }
596                                 }
597
598                                 /* *VERTEX NORMALS*
599                                  * we know the surrounding edges are ordered correctly now
600                                  * so its safe to create vertex normals.
601                                  *
602                                  * calculate vertex normals that can be propodated on lathing
603                                  * use edge connectivity work this out */
604                                 if (vc->v[0] >= 0) {
605                                         if (vc->v[1] >= 0) {
606                                                 /* 2 edges connedted */
607                                                 /* make 2 connecting vert locations relative to the middle vert */
608                                                 sub_v3_v3v3(tmp_vec1, mvert_new[vc->v[0]].co, mvert_new[i].co);
609                                                 sub_v3_v3v3(tmp_vec2, mvert_new[vc->v[1]].co, mvert_new[i].co);
610                                                 /* normalize so both edges have the same influence, no matter their length */
611                                                 normalize_v3(tmp_vec1);
612                                                 normalize_v3(tmp_vec2);
613
614                                                 /* vc_no_tmp1 - this line is the average direction of both connecting edges
615                                                  *
616                                                  * Use the edge order to make the subtraction, flip the normal the right way
617                                                  * edge should be there but check just in case... */
618                                                 if (vc->e && vc->e[0]->v1 == i) {
619                                                         sub_v3_v3(tmp_vec1, tmp_vec2);
620                                                 }
621                                                 else {
622                                                         sub_v3_v3v3(tmp_vec1, tmp_vec2, tmp_vec1);
623                                                 }
624                                         }
625                                         else {
626                                                 /* only 1 edge connected - same as above except
627                                                  * dont need to average edge direction */
628                                                 if (vc->e && vc->e[0]->v2 == i) {
629                                                         sub_v3_v3v3(tmp_vec1, mvert_new[i].co, mvert_new[vc->v[0]].co);
630                                                 }
631                                                 else {
632                                                         sub_v3_v3v3(tmp_vec1, mvert_new[vc->v[0]].co, mvert_new[i].co);
633                                                 }
634                                         }
635
636                                         /* vc_no_tmp2 - is a line 90d from the pivot to the vec
637                                          * This is used so the resulting normal points directly away from the middle */
638                                         cross_v3_v3v3(tmp_vec2, axis_vec, vc->co);
639
640                                         /* edge average vector and right angle to the pivot make the normal */
641                                         cross_v3_v3v3(vc->no, tmp_vec1, tmp_vec2);
642
643                                 }
644                                 else {
645                                         copy_v3_v3(vc->no, vc->co);
646                                 }
647
648                                 /* we wont be looping on this data again so copy normals here */
649                                 if (angle < 0.0f)
650                                         negate_v3(vc->no);
651
652                                 normalize_v3(vc->no);
653                                 normal_float_to_short_v3(mvert_new[i].no, vc->no);
654
655                                 /* Done with normals */
656                         }
657                 }
658         }
659         else {
660                 mv_orig= mvert_orig;
661                 mv_new= mvert_new;
662
663                 for (i=0; i < totvert; i++, mv_new++, mv_orig++) {
664                         copy_v3_v3(mv_new->co, mv_orig->co);
665                 }
666         }
667         /* done with edge connectivity based normal flipping */
668         
669         /* Add Faces */
670         for (step=1; step < step_tot; step++) {
671                 const int varray_stride= totvert * step;
672                 float step_angle;
673                 float nor_tx[3];
674                 float mat[4][4];
675                 /* Rotation Matrix */
676                 step_angle= (angle / (step_tot - (!close))) * step;
677
678                 if (ltmd->ob_axis) {
679                         axis_angle_to_mat3(mat3, axis_vec, step_angle);
680                         copy_m4_m3(mat, mat3);
681                 }
682                 else {
683                         unit_m4(mat);
684                         rotate_m4(mat, axis_char, step_angle);
685                         copy_m3_m4(mat3, mat);
686                 }
687
688                 if(screw_ofs)
689                         madd_v3_v3fl(mat[3], axis_vec, screw_ofs * ((float)step / (float)(step_tot-1)));
690
691                 /* copy a slice */
692                 DM_copy_vert_data(dm, result, 0, varray_stride, totvert);
693                 
694                 mv_new_base= mvert_new;
695                 mv_new= &mvert_new[varray_stride]; /* advance to the next slice */
696                 
697                 for (j=0; j<totvert; j++, mv_new_base++, mv_new++) {
698                         /* set normal */
699                         if(vert_connect) {
700                                 mul_v3_m3v3(nor_tx, mat3, vert_connect[j].no);
701
702                                 /* set the normal now its transformed */
703                                 normal_float_to_short_v3(mv_new->no, nor_tx);
704                         }
705                         
706                         /* set location */
707                         copy_v3_v3(mv_new->co, mv_new_base->co);
708                         
709                         /* only need to set these if using non cleared memory */
710                         /*mv_new->mat_nr= mv_new->flag= 0;*/
711                                 
712                         if (ltmd->ob_axis) {
713                                 sub_v3_v3(mv_new->co, mtx_tx[3]);
714
715                                 mul_m4_v3(mat, mv_new->co);
716
717                                 add_v3_v3(mv_new->co, mtx_tx[3]);
718                         }
719                         else {
720                                 mul_m4_v3(mat, mv_new->co);
721                         }
722                         
723                         /* add the new edge */
724                         med_new->v1= varray_stride + j;
725                         med_new->v2= med_new->v1 - totvert;
726                         med_new->flag= ME_EDGEDRAW|ME_EDGERENDER;
727                         med_new++;
728                 }
729         }
730
731         /* we can avoid if using vert alloc trick */
732         if(vert_connect) {
733                 MEM_freeN(vert_connect);
734                 vert_connect= NULL;
735         }
736
737         if (close) {
738                 /* last loop of edges, previous loop dosnt account for the last set of edges */
739                 const int varray_stride= (step_tot - 1) * totvert;
740
741                 for (i=0; i<totvert; i++) {
742                         med_new->v1= i;
743                         med_new->v2= varray_stride + i;
744                         med_new->flag= ME_EDGEDRAW|ME_EDGERENDER;
745                         med_new++;
746                 }
747         }
748         
749         mf_new= mface_new;
750         med_new_firstloop= medge_new;
751         
752         for (i=0; i < totedge; i++, med_new_firstloop++) {
753                 /* for each edge, make a cylinder of quads */
754                 i1= med_new_firstloop->v1;
755                 i2= med_new_firstloop->v2;
756
757                 for (step=0; step < step_tot-1; step++) {
758                         
759                         /* new face */
760                         if(do_flip) {
761                                 mf_new->v4= i1;
762                                 mf_new->v3= i2;
763                                 mf_new->v2= i2 + totvert;
764                                 mf_new->v1= i1 + totvert;
765                         }
766                         else {
767                                 mf_new->v1= i1;
768                                 mf_new->v2= i2;
769                                 mf_new->v3= i2 + totvert;
770                                 mf_new->v4= i1 + totvert;
771                         }
772                         
773                         if( !mf_new->v3 || !mf_new->v4 ) {
774                                 SWAP(int, mf_new->v1, mf_new->v3);
775                                 SWAP(int, mf_new->v2, mf_new->v4);
776                         }
777                         mf_new->flag= ME_SMOOTH;
778                         origindex[mface_index]= ORIGINDEX_NONE;
779                         mf_new++;
780                         mface_index++;
781                         
782                         /* new vertical edge */
783                         if (step) { /* The first set is already dome */
784                                 med_new->v1= i1;
785                                 med_new->v2= i2;
786                                 med_new->flag= med_new_firstloop->flag;
787                                 med_new->crease= med_new_firstloop->crease;
788                                 med_new++;
789                         }
790                         i1 += totvert;
791                         i2 += totvert;
792                 }
793                 
794                 /* close the loop*/
795                 if (close) { 
796                         if(do_flip) {
797                                 mf_new->v4= i1;
798                                 mf_new->v3= i2;
799                                 mf_new->v2= med_new_firstloop->v2;
800                                 mf_new->v1= med_new_firstloop->v1;
801                         }
802                         else {
803                                 mf_new->v1= i1;
804                                 mf_new->v2= i2;
805                                 mf_new->v3= med_new_firstloop->v2;
806                                 mf_new->v4= med_new_firstloop->v1;
807                         }
808
809                         if( !mf_new->v3 || !mf_new->v4 ) {
810                                 SWAP(int, mf_new->v1, mf_new->v3);
811                                 SWAP(int, mf_new->v2, mf_new->v4);
812                         }
813                         mf_new->flag= ME_SMOOTH;
814                         origindex[mface_index]= ORIGINDEX_NONE;
815                         mf_new++;
816                         mface_index++;
817                 }
818                 
819                 /* new vertical edge */
820                 med_new->v1= i1;
821                 med_new->v2= i2;
822                 med_new->flag= med_new_firstloop->flag & ~ME_LOOSEEDGE;
823                 med_new->crease= med_new_firstloop->crease;
824                 med_new++;
825         }
826
827         /* BMesh implimentation info - need to calculate polys before recalculating
828          * normals, since normal calc overwrites MFaces from Polys */
829         CDDM_tessfaces_to_faces(result);
830
831         if((ltmd->flag & MOD_SCREW_NORMAL_CALC) == 0) {
832                 CDDM_calc_normals(result);
833         }
834
835         return result;
836 }
837
838
839 static void updateDepgraph(ModifierData *md, DagForest *forest,
840                                                 struct Scene *UNUSED(scene),
841                                                 Object *UNUSED(ob),
842                                                 DagNode *obNode)
843 {
844         ScrewModifierData *ltmd= (ScrewModifierData*) md;
845
846         if(ltmd->ob_axis) {
847                 DagNode *curNode= dag_get_node(forest, ltmd->ob_axis);
848
849                 dag_add_relation(forest, curNode, obNode,
850                                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA,
851                                                  "Screw Modifier");
852         }
853 }
854
855 static void foreachObjectLink(
856                                 ModifierData *md, Object *ob,
857                                 void (*walk)(void *userData, Object *ob, Object **obpoin),
858                                 void *userData)
859 {
860         ScrewModifierData *ltmd= (ScrewModifierData*) md;
861
862         walk(userData, ob, &ltmd->ob_axis);
863 }
864
865 /* This dosnt work with material*/
866 static DerivedMesh *applyModifierEM(
867                                                 ModifierData *md,
868                                                 Object *ob,
869                                                 struct BMEditMesh *UNUSED(editData),
870                                                 DerivedMesh *derivedData)
871 {
872         return applyModifier(md, ob, derivedData, 0, 1);
873 }
874
875 static int dependsOnTime(ModifierData *UNUSED(md))
876 {
877         return 0;
878 }
879
880
881 ModifierTypeInfo modifierType_Screw = {
882         /* name */              "Screw",
883         /* structName */        "ScrewModifierData",
884         /* structSize */        sizeof(ScrewModifierData),
885         /* type */              eModifierTypeType_Constructive,
886
887         /* flags */             eModifierTypeFlag_AcceptsMesh
888                                                         | eModifierTypeFlag_AcceptsCVs
889                                                         | eModifierTypeFlag_SupportsEditmode
890                                                         | eModifierTypeFlag_EnableInEditmode,
891
892         /* copyData */          copyData,
893         /* deformVerts */       NULL,
894         /* deformMatrices */    NULL,
895         /* deformVertsEM */     NULL,
896         /* deformMatricesEM */  NULL,
897         /* applyModifier */     applyModifier,
898         /* applyModifierEM */   applyModifierEM,
899         /* initData */          initData,
900         /* requiredDataMask */  NULL,
901         /* freeData */          NULL,
902         /* isDisabled */        NULL,
903         /* updateDepgraph */    updateDepgraph,
904         /* dependsOnTime */     dependsOnTime,
905         /* dependsOnNormals */  NULL,
906         /* foreachObjectLink */ foreachObjectLink,
907         /* foreachIDLink */     NULL,
908         /* foreachTexLink */    NULL,
909 };