merge with trunk at r31523
[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_cdderivedmesh.h"
41
42 #include "depsgraph_private.h"
43 #include "MOD_modifiertypes.h"
44 #include "MEM_guardedalloc.h"
45
46 /* used for gathering edge connectivity */
47 typedef struct ScrewVertConnect {
48         float dist;  /* distance from the center axis */
49         float co[3]; /* loaction relative to the transformed axis */
50         float no[3]; /* calc normal of the vertex */
51         int v[2]; /* 2  verts on either side of this one */
52         MEdge *e[2]; /* edges on either side, a bit of a waste since each edge ref's 2 edges */
53         char flag;
54 } ScrewVertConnect;
55
56 typedef struct ScrewVertIter {
57         ScrewVertConnect * v_array;
58         ScrewVertConnect * v_poin;
59         int v;
60         int v_other;
61         MEdge *e;
62 } ScrewVertIter;
63
64
65 static void screwvert_iter_init(ScrewVertIter *iter, ScrewVertConnect *array, int v_init, int dir)
66 {
67         iter->v_array = array;
68         iter->v = v_init;
69
70         if (v_init >= 0) {
71                 iter->v_poin = &array[v_init];
72                 iter->v_other = iter->v_poin->v[dir];
73                 iter->e = iter->v_poin->e[!dir];
74         }
75         else {
76                 iter->v_poin= NULL;
77                 iter->e= NULL;
78         }
79 }       
80
81
82 static void screwvert_iter_step(ScrewVertIter *iter)
83 {
84         if (iter->v_poin->v[0] == iter->v_other) {
85                 iter->v_other= iter->v;
86                 iter->v= iter->v_poin->v[1];
87         }
88         else if (iter->v_poin->v[1] == iter->v_other) {
89                 iter->v_other= iter->v;
90                 iter->v= iter->v_poin->v[0];
91         }
92         if (iter->v >= 0)       {
93                 iter->v_poin= &iter->v_array[iter->v];
94                 iter->e= iter->v_poin->e[(iter->v_poin->e[0] == iter->e)];
95         }
96         else {
97                 iter->e= NULL;
98                 iter->v_poin= NULL;
99         }
100 }
101
102
103 static void initData(ModifierData *md)
104 {
105         ScrewModifierData *ltmd= (ScrewModifierData*) md;
106         ltmd->ob_axis= NULL;
107         ltmd->angle= M_PI * 2.0;
108         ltmd->axis= 2;
109         ltmd->flag= 0;
110         ltmd->steps= 16;
111         ltmd->render_steps= 16;
112         ltmd->iter= 1;
113 }
114
115 static void copyData(ModifierData *md, ModifierData *target)
116 {
117         ScrewModifierData *sltmd= (ScrewModifierData*) md;
118         ScrewModifierData *tltmd= (ScrewModifierData*) target;
119         
120         tltmd->ob_axis= sltmd->ob_axis;
121         tltmd->angle= sltmd->angle;
122         tltmd->axis= sltmd->axis;
123         tltmd->flag= sltmd->flag;
124         tltmd->steps= sltmd->steps;
125         tltmd->render_steps= sltmd->render_steps;
126         tltmd->screw_ofs= sltmd->screw_ofs;
127         tltmd->iter= sltmd->iter;
128 }
129
130 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
131                                                                                 DerivedMesh *derivedData,
132                                                                                 int useRenderParams, int isFinalCalc)
133 {
134         DerivedMesh *dm= derivedData;
135         DerivedMesh *result;
136         ScrewModifierData *ltmd= (ScrewModifierData*) md;
137         
138         int *origindex;
139         int mface_index=0;
140         int step;
141         int i, j;
142         int i1,i2;
143         int step_tot= ltmd->steps;
144         const int do_flip = ltmd->flag & MOD_SCREW_NORMAL_FLIP ? 1 : 0;
145         int maxVerts=0, maxEdges=0, maxFaces=0;
146         int totvert= dm->getNumVerts(dm);
147         int totedge= dm->getNumEdges(dm);
148
149         char axis_char= 'X', close;
150         float angle= ltmd->angle;
151         float screw_ofs= ltmd->screw_ofs;
152         float axis_vec[3]= {0.0f, 0.0f, 0.0f};
153         float tmp_vec1[3], tmp_vec2[3]; 
154         float mat3[3][3];
155         float mtx_tx[4][4]; /* transform the coords by an object relative to this objects transformation */
156         float mtx_tx_inv[4][4]; /* inverted */
157         float mtx_tmp_a[4][4];
158         
159         int vc_tot_linked= 0;
160         short other_axis_1, other_axis_2;
161         float *tmpf1, *tmpf2;
162         
163         MFace *mface_new, *mf_new;
164         MEdge *medge_orig, *med_orig, *med_new, *med_new_firstloop, *medge_new;
165         MVert *mvert_new, *mvert_orig, *mv_orig, *mv_new, *mv_new_base;
166
167         ScrewVertConnect *vc, *vc_tmp, *vert_connect= NULL;
168
169         float mat[4][4] =       {{0.0f, 0.0f, 0.0f, 0.0f},
170                                                  {0.0f, 0.0f, 0.0f, 0.0f},
171                                                  {0.0f, 0.0f, 0.0f, 0.0f},
172                                                  {0.0f, 0.0f, 0.0f, 1.0f}};
173
174         /* dont do anything? */
175         if (!totvert)
176                 return CDDM_from_template(dm, 0, 0, 0, 0, 0);
177
178         step_tot= useRenderParams ? ltmd->render_steps : ltmd->steps;
179
180         switch(ltmd->axis) {
181         case 0:
182                 other_axis_1=1;
183                 other_axis_2=2;
184                 break;
185         case 1:
186                 other_axis_1=0;
187                 other_axis_2=2;
188                 break;
189         default: /* 2, use default to quiet warnings */
190                 other_axis_1=0;
191                 other_axis_2=1;
192                 break;
193         }
194
195         axis_vec[ltmd->axis]= 1.0f;
196
197         if (ltmd->ob_axis) {
198                 /* calc the matrix relative to the axis object */
199                 invert_m4_m4(mtx_tmp_a, ob->obmat);
200                 copy_m4_m4(mtx_tx_inv, ltmd->ob_axis->obmat);
201                 mul_m4_m4m4(mtx_tx, mtx_tx_inv, mtx_tmp_a);
202
203                 /* calc the axis vec */
204                 mul_mat3_m4_v3(mtx_tx, axis_vec); /* only rotation component */
205                 normalize_v3(axis_vec);
206
207                 /* screw */
208                 if(ltmd->flag & MOD_SCREW_OBJECT_OFFSET) {
209                         /* find the offset along this axis relative to this objects matrix */
210                         float totlen = len_v3(mtx_tx[3]);
211
212                         if(totlen != 0.0f) {
213                                 float zero[3]={0.0f, 0.0f, 0.0f};
214                                 float cp[3];                            
215                                 screw_ofs= closest_to_line_v3(cp, mtx_tx[3], zero, axis_vec);
216                         }
217                         else {
218                                 screw_ofs= 0.0f;
219                         }
220                 }
221
222                 /* angle */
223
224 #if 0   // cant incluide this, not pradictable enough, though quite fun,.
225                 if(ltmd->flag & MOD_SCREW_OBJECT_ANGLE) {
226                         float mtx3_tx[3][3];
227                         copy_m3_m4(mtx3_tx, mtx_tx);
228
229                         float vec[3] = {0,1,0};
230                         float cross1[3];
231                         float cross2[3];
232                         cross_v3_v3v3(cross1, vec, axis_vec);
233
234                         mul_v3_m3v3(cross2, mtx3_tx, cross1);
235                         {
236                                 float c1[3];
237                                 float c2[3];
238                                 float axis_tmp[3];
239
240                                 cross_v3_v3v3(c1, cross2, axis_vec);
241                                 cross_v3_v3v3(c2, axis_vec, c1);
242
243
244                                 angle= angle_v3v3(cross1, c2);
245
246                                 cross_v3_v3v3(axis_tmp, cross1, c2);
247                                 normalize_v3(axis_tmp);
248
249                                 if(len_v3v3(axis_tmp, axis_vec) > 1.0f)
250                                         angle= -angle;
251
252                         }
253                 }
254 #endif
255         }
256         else {
257                 /* exis char is used by i_rotate*/
258                 axis_char += ltmd->axis; /* 'X' + axis */
259
260                 /* useful to be able to use the axis vec in some cases still */
261                 zero_v3(axis_vec);
262                 axis_vec[ltmd->axis]= 1.0f;
263         }
264
265         /* apply the multiplier */
266         angle *= ltmd->iter;
267         screw_ofs *= ltmd->iter;
268
269         /* multiplying the steps is a bit tricky, this works best */
270         step_tot = ((step_tot + 1) * ltmd->iter) - (ltmd->iter - 1);
271
272         /* will the screw be closed?
273          * Note! smaller then FLT_EPSILON*100 gives problems with float precission so its never closed. */
274         if (fabs(screw_ofs) <= (FLT_EPSILON*100) && fabs(fabs(angle) - (M_PI * 2)) <= (FLT_EPSILON*100)) {
275                 close= 1;
276                 step_tot--;
277                 if(step_tot < 2) step_tot= 2;
278         
279                 maxVerts =      totvert  * step_tot; /* -1 because we're joining back up */
280                 maxEdges =      (totvert * step_tot) + /* these are the edges between new verts */
281                                         (totedge * step_tot); /* -1 because vert edges join */
282                 maxFaces =      totedge * step_tot;
283
284                 screw_ofs= 0.0f;
285         }
286         else {
287                 close= 0;
288                 if(step_tot < 2) step_tot= 2;
289
290                 maxVerts =      totvert  * step_tot; /* -1 because we're joining back up */
291                 maxEdges =      (totvert * (step_tot-1)) + /* these are the edges between new verts */
292                                         (totedge * step_tot); /* -1 because vert edges join */
293                 maxFaces =      totedge * (step_tot-1);
294         }
295         
296         result= CDDM_from_template(dm, maxVerts, maxEdges, maxFaces, 0, 0);
297         
298         /* copy verts from mesh */
299         mvert_orig =    dm->getVertArray(dm);
300         medge_orig =    dm->getEdgeArray(dm);
301         
302         mvert_new =             result->getVertArray(result);
303         mface_new =             result->getTessFaceArray(result);
304         medge_new =             result->getEdgeArray(result);
305         
306         origindex= result->getTessFaceDataArray(result, CD_ORIGINDEX);
307         
308         /* Set the locations of the first set of verts */
309         
310         mv_new= mvert_new;
311         mv_orig= mvert_orig;
312         
313         /* Copy the first set of edges */
314         med_orig= medge_orig;
315         med_new= medge_new;
316         for (i=0; i < totedge; i++, med_orig++, med_new++) {
317                 med_new->v1= med_orig->v1;
318                 med_new->v2= med_orig->v2;
319                 med_new->crease= med_orig->crease;
320                 med_new->flag= med_orig->flag &  ~ME_LOOSEEDGE;
321         }
322         
323         if(ltmd->flag & MOD_SCREW_NORMAL_CALC) {
324                 /*
325                  * Normal Calculation (for face flipping)
326                  * Sort edge verts for correct face flipping
327                  * NOT REALLY NEEDED but face flipping is nice.
328                  *
329                  * */
330
331
332                 /* Notice!
333                  *
334                  * Since we are only ordering the edges here it can avoid mallocing the
335                  * extra space by abusing the vert array berfore its filled with new verts.
336                  * The new array for vert_connect must be at least sizeof(ScrewVertConnect) * totvert
337                  * and the size of our resulting meshes array is sizeof(MVert) * totvert * 3
338                  * so its safe to use the second 2 thrids of MVert the array for vert_connect,
339                  * just make sure ScrewVertConnect struct is no more then twice as big as MVert,
340                  * at the moment there is no chance of that being a problem,
341                  * unless MVert becomes half its current size.
342                  *
343                  * once the edges are ordered, vert_connect is not needed and it can be used for verts
344                  *
345                  * This makes the modifier faster with one less alloc.
346                  */
347
348                 vert_connect= MEM_mallocN(sizeof(ScrewVertConnect) * totvert, "ScrewVertConnect");
349                 //vert_connect= (ScrewVertConnect *) &medge_new[totvert]; /* skip the first slice of verts */
350                 vc= vert_connect;
351
352                 /* Copy Vert Locations */
353                 /* - We can do this in a later loop - only do here if no normal calc */
354                 if (!totedge) {
355                         for (i=0; i < totvert; i++, mv_orig++, mv_new++) {
356                                 copy_v3_v3(mv_new->co, mv_orig->co);
357                                 normalize_v3_v3(vc->no, mv_new->co); /* no edges- this is realy a dummy normal */
358                         }
359                 }
360                 else {
361                         /*printf("\n\n\n\n\nStarting Modifier\n");*/
362                         /* set edge users */
363                         med_new= medge_new;
364                         mv_new= mvert_new;
365
366                         if (ltmd->ob_axis) {
367                                 /*mtx_tx is initialized early on */
368                                 for (i=0; i < totvert; i++, mv_new++, mv_orig++, vc++) {
369                                         vc->co[0]= mv_new->co[0]= mv_orig->co[0];
370                                         vc->co[1]= mv_new->co[1]= mv_orig->co[1];
371                                         vc->co[2]= mv_new->co[2]= mv_orig->co[2];
372
373                                         vc->flag= 0;
374                                         vc->e[0]= vc->e[1]= NULL;
375                                         vc->v[0]= vc->v[1]= -1;
376
377                                         mul_m4_v3(mtx_tx, vc->co);
378                                         /* length in 2d, dont sqrt because this is only for comparison */
379                                         vc->dist =      vc->co[other_axis_1]*vc->co[other_axis_1] +
380                                                                 vc->co[other_axis_2]*vc->co[other_axis_2];
381
382                                         /* printf("location %f %f %f -- %f\n", vc->co[0], vc->co[1], vc->co[2], vc->dist);*/
383                                 }
384                         }
385                         else {
386                                 for (i=0; i < totvert; i++, mv_new++, mv_orig++, vc++) {
387                                         vc->co[0]= mv_new->co[0]= mv_orig->co[0];
388                                         vc->co[1]= mv_new->co[1]= mv_orig->co[1];
389                                         vc->co[2]= mv_new->co[2]= mv_orig->co[2];
390
391                                         vc->flag= 0;
392                                         vc->e[0]= vc->e[1]= NULL;
393                                         vc->v[0]= vc->v[1]= -1;
394
395                                         /* length in 2d, dont sqrt because this is only for comparison */
396                                         vc->dist =      vc->co[other_axis_1]*vc->co[other_axis_1] +
397                                                                 vc->co[other_axis_2]*vc->co[other_axis_2];
398
399                                         /* printf("location %f %f %f -- %f\n", vc->co[0], vc->co[1], vc->co[2], vc->dist);*/
400                                 }
401                         }
402
403                         /* this loop builds connectivity info for verts */
404                         for (i=0; i<totedge; i++, med_new++) {
405                                 vc= &vert_connect[med_new->v1];
406
407                                 if (vc->v[0] == -1) { /* unused */
408                                         vc->v[0]= med_new->v2;
409                                         vc->e[0]= med_new;
410                                 }
411                                 else if (vc->v[1] == -1) {
412                                         vc->v[1]= med_new->v2;
413                                         vc->e[1]= med_new;
414                                 }
415                                 else {
416                                         vc->v[0]= vc->v[1]= -2; /* erro value  - dont use, 3 edges on vert */
417                                 }
418
419                                 vc= &vert_connect[med_new->v2];
420
421                                 /* same as above but swap v1/2 */
422                                 if (vc->v[0] == -1) { /* unused */
423                                         vc->v[0]= med_new->v1;
424                                         vc->e[0]= med_new;
425                                 }
426                                 else if (vc->v[1] == -1) {
427                                         vc->v[1]= med_new->v1;
428                                         vc->e[1]= med_new;
429                                 }
430                                 else {
431                                         vc->v[0]= vc->v[1]= -2; /* erro value  - dont use, 3 edges on vert */
432                                 }
433                         }
434
435                         /* find the first vert */
436                         vc= vert_connect;
437                         for (i=0; i < totvert; i++, vc++) {
438                                 int v_best=-1, ed_loop_closed=0; /* vert and vert new */
439                                 int ed_loop_flip;
440                                 float fl= -1.0f;
441                                 ScrewVertIter lt_iter;
442
443                                 /* Now do search for connected verts, order all edges and flip them
444                                  * so resulting faces are flipped the right way */
445                                 vc_tot_linked= 0; /* count the number of linked verts for this loop */
446                                 if (vc->flag == 0) {
447                                         /*printf("Loop on connected vert: %i\n", i);*/
448
449                                         for(j=0; j<2; j++) {
450                                                 /*printf("\tSide: %i\n", j);*/
451                                                 screwvert_iter_init(&lt_iter, vert_connect, i, j);
452                                                 if (j == 1) {
453                                                         screwvert_iter_step(&lt_iter);
454                                                 }
455                                                 while (lt_iter.v_poin) {
456                                                         /*printf("\t\tVERT: %i\n", lt_iter.v);*/
457                                                         if (lt_iter.v_poin->flag) {
458                                                                 /*printf("\t\t\tBreaking Found end\n");*/
459                                                                 //endpoints[0]= endpoints[1]= -1;
460                                                                 ed_loop_closed= 1; /* circle */
461                                                                 break;
462                                                         }
463                                                         lt_iter.v_poin->flag= 1;
464                                                         vc_tot_linked++;
465                                                         /*printf("Testing 2 floats %f : %f\n", fl, lt_iter.v_poin->dist);*/
466                                                         if (fl <= lt_iter.v_poin->dist) {
467                                                                 fl= lt_iter.v_poin->dist;
468                                                                 v_best= lt_iter.v;
469                                                                 /*printf("\t\t\tVERT BEST: %i\n", v_best);*/
470                                                         }
471                                                         screwvert_iter_step(&lt_iter);
472                                                         if (!lt_iter.v_poin) {
473                                                                 /*printf("\t\t\tFound End Also Num %i\n", j);*/
474                                                                 /*endpoints[j]= lt_iter.v_other;*/ /* other is still valid */
475                                                                 break;
476                                                         }
477                                                 }
478                                         }
479
480                                         /* now we have a collection of used edges. flip their edges the right way*/
481                                         /*if (v_best != -1) - */
482
483                                         /*printf("Done Looking - vc_tot_linked: %i\n", vc_tot_linked);*/
484
485                                         if (vc_tot_linked>1) {
486                                                 float vf_1, vf_2, vf_best;
487
488                                                 vc_tmp= &vert_connect[v_best];
489
490                                                 tmpf1= vert_connect[vc_tmp->v[0]].co;
491                                                 tmpf2= vert_connect[vc_tmp->v[1]].co;
492
493
494                                                 /* edge connects on each side! */
495                                                 if ((vc_tmp->v[0] > -1) && (vc_tmp->v[1] > -1)) {
496                                                         /*printf("Verts on each side (%i %i)\n", vc_tmp->v[0], vc_tmp->v[1]);*/
497                                                         /* find out which is higher */
498
499                                                         vf_1= tmpf1[ltmd->axis];
500                                                         vf_2= tmpf2[ltmd->axis];
501                                                         vf_best= vc_tmp->co[ltmd->axis];
502
503                                                         if (vf_1 < vf_best && vf_best < vf_2) {
504                                                                 ed_loop_flip= 0;
505                                                         }
506                                                         else if (vf_1 > vf_best && vf_best > vf_2) {
507                                                                 ed_loop_flip= 1;
508                                                         }
509                                                         else {
510                                                                 /* not so simple to work out which edge is higher */
511                                                                 sub_v3_v3v3(tmp_vec1, tmpf1, vc_tmp->co);
512                                                                 sub_v3_v3v3(tmp_vec2, tmpf2, vc_tmp->co);
513                                                                 normalize_v3(tmp_vec1);
514                                                                 normalize_v3(tmp_vec2);
515
516                                                                 if (tmp_vec1[ltmd->axis] < tmp_vec2[ltmd->axis]) {
517                                                                         ed_loop_flip= 1;
518                                                                 }
519                                                                 else {
520                                                                         ed_loop_flip= 0;
521                                                                 }
522                                                         }
523                                                 }
524                                                 else if (vc_tmp->v[0] >= 0) { /*vertex only connected on 1 side */
525                                                         /*printf("Verts on ONE side (%i %i)\n", vc_tmp->v[0], vc_tmp->v[1]);*/
526                                                         if (tmpf1[ltmd->axis] < vc_tmp->co[ltmd->axis]) { /* best is above */
527                                                                 ed_loop_flip= 1;
528                                                         }
529                                                         else { /* best is below or even... in even case we cant know whet  to do. */
530                                                                 ed_loop_flip= 0;
531                                                         }
532
533                                                 }/* else {
534                                                         printf("No Connected ___\n");
535                                                 }*/
536
537                                                 /*printf("flip direction %i\n", ed_loop_flip);*/
538
539
540                                                 /* switch the flip option if set
541                                                  * note: flip is now done at face level so copying vgroup slizes is easier */
542                                                 /*                                              
543                                                 if (do_flip)
544                                                         ed_loop_flip= !ed_loop_flip;
545                                                 */
546
547                                                 if (angle < 0.0f)
548                                                         ed_loop_flip= !ed_loop_flip;
549
550                                                 /* if its closed, we only need 1 loop */
551                                                 for(j=ed_loop_closed; j<2; j++) {
552                                                         /*printf("Ordering Side J %i\n", j);*/
553
554                                                         screwvert_iter_init(&lt_iter, vert_connect, v_best, j);
555                                                         /*printf("\n\nStarting - Loop\n");*/
556                                                         lt_iter.v_poin->flag= 1; /* so a non loop will traverse the other side */
557
558
559                                                         /* If this is the vert off the best vert and
560                                                          * the best vert has 2 edges connected too it
561                                                          * then swap the flip direction */
562                                                         if (j == 1 && (vc_tmp->v[0] > -1) && (vc_tmp->v[1] > -1))
563                                                                 ed_loop_flip= !ed_loop_flip;
564
565                                                         while (lt_iter.v_poin && lt_iter.v_poin->flag != 2) {
566                                                                 /*printf("\tOrdering Vert V %i\n", lt_iter.v);*/
567
568                                                                 lt_iter.v_poin->flag= 2;
569                                                                 if (lt_iter.e) {
570                                                                         if (lt_iter.v == lt_iter.e->v1) {
571                                                                                 if (ed_loop_flip == 0) {
572                                                                                         /*printf("\t\t\tFlipping 0\n");*/
573                                                                                         SWAP(int, lt_iter.e->v1, lt_iter.e->v2);
574                                                                                 }/* else {
575                                                                                         printf("\t\t\tFlipping Not 0\n");
576                                                                                 }*/
577                                                                         }
578                                                                         else if (lt_iter.v == lt_iter.e->v2) {
579                                                                                 if (ed_loop_flip == 1) {
580                                                                                         /*printf("\t\t\tFlipping 1\n");*/
581                                                                                         SWAP(int, lt_iter.e->v1, lt_iter.e->v2);
582                                                                                 }/* else {
583                                                                                         printf("\t\t\tFlipping Not 1\n");
584                                                                                 }*/
585                                                                         }/* else {
586                                                                                 printf("\t\tIncorrect edge topology");
587                                                                         }*/
588                                                                 }/* else {
589                                                                         printf("\t\tNo Edge at this point\n");
590                                                                 }*/
591                                                                 screwvert_iter_step(&lt_iter);
592                                                         }
593                                                 }
594                                         }
595                                 }
596
597                                 /* *VERTEX NORMALS*
598                                  * we know the surrounding edges are ordered correctly now
599                                  * so its safe to create vertex normals.
600                                  *
601                                  * calculate vertex normals that can be propodated on lathing
602                                  * use edge connectivity work this out */
603                                 if (vc->v[0] >= 0) {
604                                         if (vc->v[1] >= 0) {
605                                                 /* 2 edges connedted */
606                                                 /* make 2 connecting vert locations relative to the middle vert */
607                                                 sub_v3_v3v3(tmp_vec1, mvert_new[vc->v[0]].co, mvert_new[i].co);
608                                                 sub_v3_v3v3(tmp_vec2, mvert_new[vc->v[1]].co, mvert_new[i].co);
609                                                 /* normalize so both edges have the same influence, no matter their length */
610                                                 normalize_v3(tmp_vec1);
611                                                 normalize_v3(tmp_vec2);
612
613                                                 /* vc_no_tmp1 - this line is the average direction of both connecting edges
614                                                  *
615                                                  * Use the edge order to make the subtraction, flip the normal the right way
616                                                  * edge should be there but check just in case... */
617                                                 if (vc->e && vc->e[0]->v1 == i) {
618                                                         sub_v3_v3(tmp_vec1, tmp_vec2);
619                                                 }
620                                                 else {
621                                                         sub_v3_v3v3(tmp_vec1, tmp_vec2, tmp_vec1);
622                                                 }
623                                         }
624                                         else {
625                                                 /* only 1 edge connected - same as above except
626                                                  * dont need to average edge direction */
627                                                 if (vc->e && vc->e[0]->v2 == i) {
628                                                         sub_v3_v3v3(tmp_vec1, mvert_new[i].co, mvert_new[vc->v[0]].co);
629                                                 }
630                                                 else {
631                                                         sub_v3_v3v3(tmp_vec1, mvert_new[vc->v[0]].co, mvert_new[i].co);
632                                                 }
633                                         }
634
635                                         /* vc_no_tmp2 - is a line 90d from the pivot to the vec
636                                          * This is used so the resulting normal points directly away from the middle */
637                                         cross_v3_v3v3(tmp_vec2, axis_vec, vc->co);
638
639                                         /* edge average vector and right angle to the pivot make the normal */
640                                         cross_v3_v3v3(vc->no, tmp_vec1, tmp_vec2);
641
642                                 }
643                                 else {
644                                         copy_v3_v3(vc->no, vc->co);
645                                 }
646
647                                 /* we wont be looping on this data again so copy normals here */
648                                 if (angle < 0.0f)
649                                         negate_v3(vc->no);
650
651                                 normalize_v3(vc->no);
652                                 normal_float_to_short_v3(mvert_new[i].no, vc->no);
653
654                                 /* Done with normals */
655                         }
656                 }
657         }
658         else {
659                 mv_orig= mvert_orig;
660                 mv_new= mvert_new;
661
662                 for (i=0; i < totvert; i++, mv_new++, mv_orig++) {
663                         copy_v3_v3(mv_new->co, mv_orig->co);
664                 }
665         }
666         /* done with edge connectivity based normal flipping */
667         
668         DM_copy_vert_data(dm, result, 0, 0, totvert);
669         
670         /* Add Faces */
671         for (step=1; step < step_tot; step++) {
672                 const int varray_stride= totvert * step;
673                 float step_angle;
674                 float nor_tx[3];
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         if((ltmd->flag & MOD_SCREW_NORMAL_CALC) == 0) {
828                 CDDM_calc_normals(result);
829         }
830
831         dm = CDDM_copy(result, 1); /*builds ngon faces from tess (mface) faces*/
832         result->needsFree = 1;
833         result->release(result);
834 }
835
836
837 static void updateDepgraph(
838                                                                         ModifierData *md, DagForest *forest,
839                                                                         struct Scene *scene, Object *ob, DagNode *obNode)
840 {
841         ScrewModifierData *ltmd= (ScrewModifierData*) md;
842
843         if(ltmd->ob_axis) {
844                 DagNode *curNode= dag_get_node(forest, ltmd->ob_axis);
845
846                 dag_add_relation(forest, curNode, obNode,
847                                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA,
848                                                  "Screw Modifier");
849         }
850 }
851
852 static void foreachObjectLink(
853                                 ModifierData *md, Object *ob,
854                                 void (*walk)(void *userData, Object *ob, Object **obpoin),
855                                 void *userData)
856 {
857         ScrewModifierData *ltmd= (ScrewModifierData*) md;
858
859         walk(userData, ob, &ltmd->ob_axis);
860 }
861
862 /* This dosnt work with material*/
863 static DerivedMesh *applyModifierEM(
864                                                 ModifierData *md, Object *ob, struct EditMesh *editData,
865                                                 DerivedMesh *derivedData)
866 {
867         return applyModifier(md, ob, derivedData, 0, 1);
868 }
869
870 static int dependsOnTime(ModifierData *md)
871 {
872         return 0;
873 }
874
875
876 ModifierTypeInfo modifierType_Screw = {
877         /* name */              "Screw",
878         /* structName */        "ScrewModifierData",
879         /* structSize */        sizeof(ScrewModifierData),
880         /* type */              eModifierTypeType_Constructive,
881
882         /* flags */             eModifierTypeFlag_AcceptsMesh
883                                                         | eModifierTypeFlag_AcceptsCVs
884                                                         | eModifierTypeFlag_SupportsEditmode
885                                                         | eModifierTypeFlag_EnableInEditmode,
886
887         /* copyData */          copyData,
888         /* deformVerts */       0,
889         /* deformVertsEM */     0,
890         /* deformMatricesEM */  0,
891         /* applyModifier */     applyModifier,
892         /* applyModifierEM */   applyModifierEM,
893         /* initData */          initData,
894         /* requiredDataMask */  0,
895         /* freeData */          0,
896         /* isDisabled */        0,
897         /* updateDepgraph */    updateDepgraph,
898         /* dependsOnTime */     dependsOnTime,
899         /* foreachObjectLink */ foreachObjectLink,
900         /* foreachIDLink */     0,
901 };