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