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