Creating a BGE staging branch.
[blender.git] / source / blender / blenkernel / intern / linestyle.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2010 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/linestyle.c
29  *  \ingroup bke
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_object_types.h"
39 #include "DNA_material_types.h" /* for ramp blend */
40 #include "DNA_texture_types.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_global.h"
47 #include "BKE_library.h"
48 #include "BKE_linestyle.h"
49 #include "BKE_main.h"
50 #include "BKE_texture.h"
51 #include "BKE_colortools.h"
52 #include "BKE_animsys.h"
53
54 static const char *modifier_name[LS_MODIFIER_NUM] = {
55         NULL,
56         "Along Stroke",
57         "Distance from Camera",
58         "Distance from Object",
59         "Material",
60         "Sampling",
61         "Bezier Curve",
62         "Sinus Displacement",
63         "Spatial Noise",
64         "Perlin Noise 1D",
65         "Perlin Noise 2D",
66         "Backbone Stretcher",
67         "Tip Remover",
68         "Calligraphy",
69         "Polygonalization",
70         "Guiding Lines",
71         "Blueprint",
72         "2D Offset",
73         "2D Transform",
74 };
75
76 static void default_linestyle_settings(FreestyleLineStyle *linestyle)
77 {
78         linestyle->panel = LS_PANEL_STROKES;
79         linestyle->r = linestyle->g = linestyle->b = 0.0f;
80         linestyle->alpha = 1.0f;
81         linestyle->thickness = 3.0f;
82         linestyle->thickness_position = LS_THICKNESS_CENTER;
83         linestyle->thickness_ratio = 0.5f;
84         linestyle->chaining = LS_CHAINING_PLAIN;
85         linestyle->rounds = 3;
86         linestyle->min_angle = DEG2RADF(0.0f);
87         linestyle->max_angle = DEG2RADF(0.0f);
88         linestyle->min_length = 0.0f;
89         linestyle->max_length = 10000.0f;
90         linestyle->split_length = 100;
91
92         linestyle->color_modifiers.first = linestyle->color_modifiers.last = NULL;
93         linestyle->alpha_modifiers.first = linestyle->alpha_modifiers.last = NULL;
94         linestyle->thickness_modifiers.first = linestyle->thickness_modifiers.last = NULL;
95         linestyle->geometry_modifiers.first = linestyle->geometry_modifiers.last = NULL;
96
97         BKE_add_linestyle_geometry_modifier(linestyle, LS_MODIFIER_SAMPLING);
98
99         linestyle->caps = LS_CAPS_BUTT;
100 }
101
102 FreestyleLineStyle *BKE_new_linestyle(const char *name, struct Main *main)
103 {
104         FreestyleLineStyle *linestyle;
105
106         if (!main)
107                 main = G.main;
108
109         linestyle = (FreestyleLineStyle *)BKE_libblock_alloc(&main->linestyle, ID_LS, name);
110
111         default_linestyle_settings(linestyle);
112
113         return linestyle;
114 }
115
116 void BKE_free_linestyle(FreestyleLineStyle *linestyle)
117 {
118         LineStyleModifier *m;
119
120         BKE_free_animdata(&linestyle->id);
121         while ((m = (LineStyleModifier *)linestyle->color_modifiers.first))
122                 BKE_remove_linestyle_color_modifier(linestyle, m);
123         while ((m = (LineStyleModifier *)linestyle->alpha_modifiers.first))
124                 BKE_remove_linestyle_alpha_modifier(linestyle, m);
125         while ((m = (LineStyleModifier *)linestyle->thickness_modifiers.first))
126                 BKE_remove_linestyle_thickness_modifier(linestyle, m);
127         while ((m = (LineStyleModifier *)linestyle->geometry_modifiers.first))
128                 BKE_remove_linestyle_geometry_modifier(linestyle, m);
129 }
130
131 FreestyleLineStyle *BKE_copy_linestyle(FreestyleLineStyle *linestyle)
132 {
133         FreestyleLineStyle *new_linestyle;
134         LineStyleModifier *m;
135
136         new_linestyle = BKE_new_linestyle(linestyle->id.name + 2, NULL);
137         BKE_free_linestyle(new_linestyle);
138
139         new_linestyle->r = linestyle->r;
140         new_linestyle->g = linestyle->g;
141         new_linestyle->b = linestyle->b;
142         new_linestyle->alpha = linestyle->alpha;
143         new_linestyle->thickness = linestyle->thickness;
144         new_linestyle->thickness_position = linestyle->thickness_position;
145         new_linestyle->thickness_ratio = linestyle->thickness_ratio;
146         new_linestyle->flag = linestyle->flag;
147         new_linestyle->caps = linestyle->caps;
148         new_linestyle->chaining = linestyle->chaining;
149         new_linestyle->rounds = linestyle->rounds;
150         new_linestyle->split_length = linestyle->split_length;
151         new_linestyle->min_angle = linestyle->min_angle;
152         new_linestyle->max_angle = linestyle->max_angle;
153         new_linestyle->min_length = linestyle->min_length;
154         new_linestyle->max_length = linestyle->max_length;
155         new_linestyle->split_dash1 = linestyle->split_dash1;
156         new_linestyle->split_gap1 = linestyle->split_gap1;
157         new_linestyle->split_dash2 = linestyle->split_dash2;
158         new_linestyle->split_gap2 = linestyle->split_gap2;
159         new_linestyle->split_dash3 = linestyle->split_dash3;
160         new_linestyle->split_gap3 = linestyle->split_gap3;
161         new_linestyle->dash1 = linestyle->dash1;
162         new_linestyle->gap1 = linestyle->gap1;
163         new_linestyle->dash2 = linestyle->dash2;
164         new_linestyle->gap2 = linestyle->gap2;
165         new_linestyle->dash3 = linestyle->dash3;
166         new_linestyle->gap3 = linestyle->gap3;
167         new_linestyle->panel = linestyle->panel;
168         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next)
169                 BKE_copy_linestyle_color_modifier(new_linestyle, m);
170         for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next)
171                 BKE_copy_linestyle_alpha_modifier(new_linestyle, m);
172         for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next)
173                 BKE_copy_linestyle_thickness_modifier(new_linestyle, m);
174         for (m = (LineStyleModifier *)linestyle->geometry_modifiers.first; m; m = m->next)
175                 BKE_copy_linestyle_geometry_modifier(new_linestyle, m);
176
177         return new_linestyle;
178 }
179
180 static LineStyleModifier *new_modifier(int type, size_t size)
181 {
182         LineStyleModifier *m;
183
184         m = (LineStyleModifier *)MEM_callocN(size, "line style modifier");
185         m->type = type;
186         strcpy(m->name, modifier_name[type]);
187         m->influence = 1.0f;
188         m->flags = LS_MODIFIER_ENABLED | LS_MODIFIER_EXPANDED;
189
190         return m;
191 }
192
193 static void add_to_modifier_list(ListBase *lb, LineStyleModifier *m)
194 {
195         BLI_addtail(lb, (void *)m);
196         BLI_uniquename(lb, m, modifier_name[m->type], '.', offsetof(LineStyleModifier, name), sizeof(m->name));
197 }
198
199 static LineStyleModifier *alloc_color_modifier(int type)
200 {
201         size_t size;
202
203         switch (type) {
204         case LS_MODIFIER_ALONG_STROKE:
205                 size = sizeof(LineStyleColorModifier_AlongStroke);
206                 break;
207         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
208                 size = sizeof(LineStyleColorModifier_DistanceFromCamera);
209                 break;
210         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
211                 size = sizeof(LineStyleColorModifier_DistanceFromObject);
212                 break;
213         case LS_MODIFIER_MATERIAL:
214                 size = sizeof(LineStyleColorModifier_Material);
215                 break;
216         default:
217                 return NULL; /* unknown modifier type */
218         }
219
220         return new_modifier(type, size);
221 }
222
223 LineStyleModifier *BKE_add_linestyle_color_modifier(FreestyleLineStyle *linestyle, int type)
224 {
225         LineStyleModifier *m;
226
227         m = alloc_color_modifier(type);
228         m->blend = MA_RAMP_BLEND;
229
230         switch (type) {
231         case LS_MODIFIER_ALONG_STROKE:
232                 ((LineStyleColorModifier_AlongStroke *)m)->color_ramp = add_colorband(1);
233                 break;
234         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
235                 ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp = add_colorband(1);
236                 ((LineStyleColorModifier_DistanceFromCamera *)m)->range_min = 0.0f;
237                 ((LineStyleColorModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
238                 break;
239         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
240                 ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
241                 ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp = add_colorband(1);
242                 ((LineStyleColorModifier_DistanceFromObject *)m)->range_min = 0.0f;
243                 ((LineStyleColorModifier_DistanceFromObject *)m)->range_max = 10000.0f;
244                 break;
245         case LS_MODIFIER_MATERIAL:
246                 ((LineStyleColorModifier_Material *)m)->color_ramp = add_colorband(1);
247                 ((LineStyleColorModifier_Material *)m)->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
248                 break;
249         default:
250                 return NULL; /* unknown modifier type */
251         }
252         add_to_modifier_list(&linestyle->color_modifiers, m);
253
254         return m;
255 }
256
257 LineStyleModifier *BKE_copy_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
258 {
259         LineStyleModifier *new_m;
260
261         new_m = alloc_color_modifier(m->type);
262         new_m->influence = m->influence;
263         new_m->flags = m->flags;
264         new_m->blend = m->blend;
265
266         switch (m->type) {
267         case LS_MODIFIER_ALONG_STROKE:
268                 {
269                         LineStyleColorModifier_AlongStroke *p = (LineStyleColorModifier_AlongStroke *)m;
270                         LineStyleColorModifier_AlongStroke *q = (LineStyleColorModifier_AlongStroke *)new_m;
271                         q->color_ramp = MEM_dupallocN(p->color_ramp);
272                 }
273                 break;
274         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
275                 {
276                         LineStyleColorModifier_DistanceFromCamera *p = (LineStyleColorModifier_DistanceFromCamera *)m;
277                         LineStyleColorModifier_DistanceFromCamera *q = (LineStyleColorModifier_DistanceFromCamera *)new_m;
278                         q->color_ramp = MEM_dupallocN(p->color_ramp);
279                         q->range_min = p->range_min;
280                         q->range_max = p->range_max;
281                 }
282                 break;
283         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
284                 {
285                         LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)m;
286                         LineStyleColorModifier_DistanceFromObject *q = (LineStyleColorModifier_DistanceFromObject *)new_m;
287                         if (p->target)
288                                 p->target->id.us++;
289                         q->target = p->target;
290                         q->color_ramp = MEM_dupallocN(p->color_ramp);
291                         q->range_min = p->range_min;
292                         q->range_max = p->range_max;
293                 }
294                 break;
295         case LS_MODIFIER_MATERIAL:
296                 {
297                         LineStyleColorModifier_Material *p = (LineStyleColorModifier_Material *)m;
298                         LineStyleColorModifier_Material *q = (LineStyleColorModifier_Material *)new_m;
299                         q->color_ramp = MEM_dupallocN(p->color_ramp);
300                         q->flags = p->flags;
301                         q->mat_attr = p->mat_attr;
302                 }
303                 break;
304         default:
305                 return NULL; /* unknown modifier type */
306         }
307         add_to_modifier_list(&linestyle->color_modifiers, new_m);
308
309         return new_m;
310 }
311
312 void BKE_remove_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
313 {
314         switch (m->type) {
315         case LS_MODIFIER_ALONG_STROKE:
316                 MEM_freeN(((LineStyleColorModifier_AlongStroke *)m)->color_ramp);
317                 break;
318         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
319                 MEM_freeN(((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp);
320                 break;
321         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
322                 MEM_freeN(((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp);
323                 break;
324         case LS_MODIFIER_MATERIAL:
325                 MEM_freeN(((LineStyleColorModifier_Material *)m)->color_ramp);
326                 break;
327         }
328         BLI_freelinkN(&linestyle->color_modifiers, m);
329 }
330
331 static LineStyleModifier *alloc_alpha_modifier(int type)
332 {
333         size_t size;
334
335         switch (type) {
336         case LS_MODIFIER_ALONG_STROKE:
337                 size = sizeof(LineStyleAlphaModifier_AlongStroke);
338                 break;
339         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
340                 size = sizeof(LineStyleAlphaModifier_DistanceFromCamera);
341                 break;
342         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
343                 size = sizeof(LineStyleAlphaModifier_DistanceFromObject);
344                 break;
345         case LS_MODIFIER_MATERIAL:
346                 size = sizeof(LineStyleAlphaModifier_Material);
347                 break;
348         default:
349                 return NULL; /* unknown modifier type */
350         }
351         return new_modifier(type, size);
352 }
353
354 LineStyleModifier *BKE_add_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, int type)
355 {
356         LineStyleModifier *m;
357
358         m = alloc_alpha_modifier(type);
359         m->blend = LS_VALUE_BLEND;
360
361         switch (type) {
362         case LS_MODIFIER_ALONG_STROKE:
363                 {
364                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
365                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
366                 }
367                 break;
368         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
369                 {
370                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
371                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
372                         p->range_min = 0.0f;
373                         p->range_max = 10000.0f;
374                 }
375                 break;
376         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
377                 {
378                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
379                         p->target = NULL;
380                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
381                         p->range_min = 0.0f;
382                         p->range_max = 10000.0f;
383                 }
384                 break;
385         case LS_MODIFIER_MATERIAL:
386                 {
387                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
388                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
389                         p->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
390                 }
391                 break;
392         default:
393                 return NULL; /* unknown modifier type */
394         }
395         add_to_modifier_list(&linestyle->alpha_modifiers, m);
396
397         return m;
398 }
399
400 LineStyleModifier *BKE_copy_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
401 {
402         LineStyleModifier *new_m;
403
404         new_m = alloc_alpha_modifier(m->type);
405         new_m->influence = m->influence;
406         new_m->flags = m->flags;
407         new_m->blend = m->blend;
408
409         switch (m->type) {
410         case LS_MODIFIER_ALONG_STROKE:
411                 {
412                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
413                         LineStyleAlphaModifier_AlongStroke *q = (LineStyleAlphaModifier_AlongStroke *)new_m;
414                         q->curve = curvemapping_copy(p->curve);
415                         q->flags = p->flags;
416                 }
417                 break;
418         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
419                 {
420                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
421                         LineStyleAlphaModifier_DistanceFromCamera *q = (LineStyleAlphaModifier_DistanceFromCamera *)new_m;
422                         q->curve = curvemapping_copy(p->curve);
423                         q->flags = p->flags;
424                         q->range_min = p->range_min;
425                         q->range_max = p->range_max;
426                 }
427                 break;
428         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
429                 {
430                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
431                         LineStyleAlphaModifier_DistanceFromObject *q = (LineStyleAlphaModifier_DistanceFromObject *)new_m;
432                         if (p->target)
433                                 p->target->id.us++;
434                         q->target = p->target;
435                         q->curve = curvemapping_copy(p->curve);
436                         q->flags = p->flags;
437                         q->range_min = p->range_min;
438                         q->range_max = p->range_max;
439                 }
440                 break;
441         case LS_MODIFIER_MATERIAL:
442                 {
443                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
444                         LineStyleAlphaModifier_Material *q = (LineStyleAlphaModifier_Material *)new_m;
445                         q->curve = curvemapping_copy(p->curve);
446                         q->flags = p->flags;
447                         q->mat_attr = p->mat_attr;
448                 }
449                 break;
450         default:
451                 return NULL; /* unknown modifier type */
452         }
453         add_to_modifier_list(&linestyle->alpha_modifiers, new_m);
454
455         return new_m;
456 }
457
458 void BKE_remove_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
459 {
460         switch (m->type) {
461         case LS_MODIFIER_ALONG_STROKE:
462                 curvemapping_free(((LineStyleAlphaModifier_AlongStroke *)m)->curve);
463                 break;
464         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
465                 curvemapping_free(((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve);
466                 break;
467         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
468                 curvemapping_free(((LineStyleAlphaModifier_DistanceFromObject *)m)->curve);
469                 break;
470         case LS_MODIFIER_MATERIAL:
471                 curvemapping_free(((LineStyleAlphaModifier_Material *)m)->curve);
472                 break;
473         }
474         BLI_freelinkN(&linestyle->alpha_modifiers, m);
475 }
476
477 static LineStyleModifier *alloc_thickness_modifier(int type)
478 {
479         size_t size;
480
481         switch (type) {
482         case LS_MODIFIER_ALONG_STROKE:
483                 size = sizeof(LineStyleThicknessModifier_AlongStroke);
484                 break;
485         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
486                 size = sizeof(LineStyleThicknessModifier_DistanceFromCamera);
487                 break;
488         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
489                 size = sizeof(LineStyleThicknessModifier_DistanceFromObject);
490                 break;
491         case LS_MODIFIER_MATERIAL:
492                 size = sizeof(LineStyleThicknessModifier_Material);
493                 break;
494         case LS_MODIFIER_CALLIGRAPHY:
495                 size = sizeof(LineStyleThicknessModifier_Calligraphy);
496                 break;
497         default:
498                 return NULL; /* unknown modifier type */
499         }
500
501         return new_modifier(type, size);
502 }
503
504 LineStyleModifier *BKE_add_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, int type)
505 {
506         LineStyleModifier *m;
507
508         m = alloc_thickness_modifier(type);
509         m->blend = LS_VALUE_BLEND;
510
511         switch (type) {
512         case LS_MODIFIER_ALONG_STROKE:
513                 {
514                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
515                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
516                         p->value_min = 0.0f;
517                         p->value_max = 1.0f;
518                 }
519                 break;
520         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
521                 {
522                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
523                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
524                         p->range_min = 0.0f;
525                         p->range_max = 1000.0f;
526                         p->value_min = 0.0f;
527                         p->value_max = 1.0f;
528                 }
529                 break;
530         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
531                 {
532                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
533                         p->target = NULL;
534                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
535                         p->range_min = 0.0f;
536                         p->range_max = 1000.0f;
537                         p->value_min = 0.0f;
538                         p->value_max = 1.0f;
539                 }
540                 break;
541         case LS_MODIFIER_MATERIAL:
542                 {
543                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
544                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
545                         p->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
546                         p->value_min = 0.0f;
547                         p->value_max = 1.0f;
548                 }
549                 break;
550         case LS_MODIFIER_CALLIGRAPHY:
551                 {
552                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
553                         p->min_thickness = 1.0f;
554                         p->max_thickness = 10.0f;
555                         p->orientation = DEG2RADF(60.0f);
556                 }
557                 break;
558         default:
559                 return NULL; /* unknown modifier type */
560         }
561         add_to_modifier_list(&linestyle->thickness_modifiers, m);
562
563         return m;
564 }
565
566 LineStyleModifier *BKE_copy_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
567 {
568         LineStyleModifier *new_m;
569
570         new_m = alloc_thickness_modifier(m->type);
571         if (!new_m)
572                 return NULL;
573         new_m->influence = m->influence;
574         new_m->flags = m->flags;
575         new_m->blend = m->blend;
576
577         switch (m->type) {
578         case LS_MODIFIER_ALONG_STROKE:
579                 {
580                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
581                         LineStyleThicknessModifier_AlongStroke *q = (LineStyleThicknessModifier_AlongStroke *)new_m;
582                         q->curve = curvemapping_copy(p->curve);
583                         q->flags = p->flags;
584                         q->value_min = p->value_min;
585                         q->value_max = p->value_max;
586                 }
587                 break;
588         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
589                 {
590                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
591                         LineStyleThicknessModifier_DistanceFromCamera *q = (LineStyleThicknessModifier_DistanceFromCamera *)new_m;
592                         q->curve = curvemapping_copy(p->curve);
593                         q->flags = p->flags;
594                         q->range_min = p->range_min;
595                         q->range_max = p->range_max;
596                         q->value_min = p->value_min;
597                         q->value_max = p->value_max;
598                 }
599                 break;
600         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
601                 {
602                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
603                         LineStyleThicknessModifier_DistanceFromObject *q = (LineStyleThicknessModifier_DistanceFromObject *)new_m;
604                         if (p->target)
605                                 p->target->id.us++;
606                         q->target = p->target;
607                         q->curve = curvemapping_copy(p->curve);
608                         q->flags = p->flags;
609                         q->range_min = p->range_min;
610                         q->range_max = p->range_max;
611                         q->value_min = p->value_min;
612                         q->value_max = p->value_max;
613                 }
614                 break;
615         case LS_MODIFIER_MATERIAL:
616                 {
617                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
618                         LineStyleThicknessModifier_Material *q = (LineStyleThicknessModifier_Material *)new_m;
619                         q->curve = curvemapping_copy(p->curve);
620                         q->flags = p->flags;
621                         q->mat_attr = p->mat_attr;
622                         q->value_min = p->value_min;
623                         q->value_max = p->value_max;
624                 }
625                 break;
626         case LS_MODIFIER_CALLIGRAPHY:
627                 {
628                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
629                         LineStyleThicknessModifier_Calligraphy *q = (LineStyleThicknessModifier_Calligraphy *)new_m;
630                         q->min_thickness = p->min_thickness;
631                         q->max_thickness = p->max_thickness;
632                         q->orientation = p->orientation;
633                 }
634                 break;
635         default:
636                 return NULL; /* unknown modifier type */
637         }
638         add_to_modifier_list(&linestyle->thickness_modifiers, new_m);
639
640         return new_m;
641 }
642
643 void BKE_remove_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
644 {
645         switch (m->type) {
646         case LS_MODIFIER_ALONG_STROKE:
647                 curvemapping_free(((LineStyleThicknessModifier_AlongStroke *)m)->curve);
648                 break;
649         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
650                 curvemapping_free(((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve);
651                 break;
652         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
653                 curvemapping_free(((LineStyleThicknessModifier_DistanceFromObject *)m)->curve);
654                 break;
655         case LS_MODIFIER_MATERIAL:
656                 curvemapping_free(((LineStyleThicknessModifier_Material *)m)->curve);
657                 break;
658         case LS_MODIFIER_CALLIGRAPHY:
659                 break;
660         }
661         BLI_freelinkN(&linestyle->thickness_modifiers, m);
662 }
663
664 static LineStyleModifier *alloc_geometry_modifier(int type)
665 {
666         size_t size;
667
668         switch (type) {
669         case LS_MODIFIER_SAMPLING:
670                 size = sizeof(LineStyleGeometryModifier_Sampling);
671                 break;
672         case LS_MODIFIER_BEZIER_CURVE:
673                 size = sizeof(LineStyleGeometryModifier_BezierCurve);
674                 break;
675         case LS_MODIFIER_SINUS_DISPLACEMENT:
676                 size = sizeof(LineStyleGeometryModifier_SinusDisplacement);
677                 break;
678         case LS_MODIFIER_SPATIAL_NOISE:
679                 size = sizeof(LineStyleGeometryModifier_SpatialNoise);
680                 break;
681         case LS_MODIFIER_PERLIN_NOISE_1D:
682                 size = sizeof(LineStyleGeometryModifier_PerlinNoise1D);
683                 break;
684         case LS_MODIFIER_PERLIN_NOISE_2D:
685                 size = sizeof(LineStyleGeometryModifier_PerlinNoise2D);
686                 break;
687         case LS_MODIFIER_BACKBONE_STRETCHER:
688                 size = sizeof(LineStyleGeometryModifier_BackboneStretcher);
689                 break;
690         case LS_MODIFIER_TIP_REMOVER:
691                 size = sizeof(LineStyleGeometryModifier_TipRemover);
692                 break;
693         case LS_MODIFIER_POLYGONIZATION:
694                 size = sizeof(LineStyleGeometryModifier_Polygonalization);
695                 break;
696         case LS_MODIFIER_GUIDING_LINES:
697                 size = sizeof(LineStyleGeometryModifier_GuidingLines);
698                 break;
699         case LS_MODIFIER_BLUEPRINT:
700                 size = sizeof(LineStyleGeometryModifier_Blueprint);
701                 break;
702         case LS_MODIFIER_2D_OFFSET:
703                 size = sizeof(LineStyleGeometryModifier_2DOffset);
704                 break;
705         case LS_MODIFIER_2D_TRANSFORM:
706                 size = sizeof(LineStyleGeometryModifier_2DTransform);
707                 break;
708         default:
709                 return NULL; /* unknown modifier type */
710         }
711
712         return new_modifier(type, size);
713 }
714
715 LineStyleModifier *BKE_add_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, int type)
716 {
717         LineStyleModifier *m;
718
719         m = alloc_geometry_modifier(type);
720
721         switch (type) {
722         case LS_MODIFIER_SAMPLING:
723                 {
724                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
725                         p->sampling = 10.0f;
726                 }
727                 break;
728         case LS_MODIFIER_BEZIER_CURVE:
729                 {
730                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
731                         p->error = 10.0f;
732                 }
733                 break;
734         case LS_MODIFIER_SINUS_DISPLACEMENT:
735                 {
736                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
737                         p->wavelength = 20.0f;
738                         p->amplitude = 5.0f;
739                         p->phase = 0.0f;
740                 }
741                 break;
742         case LS_MODIFIER_SPATIAL_NOISE:
743                 {
744                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
745                         p->amplitude = 5.0f;
746                         p->scale = 20.0f;
747                         p->octaves = 4;
748                         p->flags = LS_MODIFIER_SPATIAL_NOISE_SMOOTH | LS_MODIFIER_SPATIAL_NOISE_PURERANDOM;
749                 }
750                 break;
751         case LS_MODIFIER_PERLIN_NOISE_1D:
752                 {
753                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
754                         p->frequency = 10.0f;
755                         p->amplitude = 10.0f;
756                         p->octaves = 4;
757                         p->angle = DEG2RADF(45.0f);
758                 }
759                 break;
760         case LS_MODIFIER_PERLIN_NOISE_2D:
761                 {
762                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
763                         p->frequency = 10.0f;
764                         p->amplitude = 10.0f;
765                         p->octaves = 4;
766                         p->angle = DEG2RADF(45.0f);
767                 }
768                 break;
769         case LS_MODIFIER_BACKBONE_STRETCHER:
770                 {
771                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
772                         p->backbone_length = 10.0f;
773                 }
774                 break;
775         case LS_MODIFIER_TIP_REMOVER:
776                 {
777                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
778                         p->tip_length = 10.0f;
779                 }
780                 break;
781         case LS_MODIFIER_POLYGONIZATION:
782                 {
783                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
784                         p->error = 10.0f;
785                 }
786                 break;
787         case LS_MODIFIER_GUIDING_LINES:
788                 {
789                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
790                         p->offset = 0.0f;
791                 }
792                 break;
793         case LS_MODIFIER_BLUEPRINT:
794                 {
795                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
796                         p->flags = LS_MODIFIER_BLUEPRINT_CIRCLES;
797                         p->rounds = 1;
798                         p->backbone_length = 10.0f;
799                         p->random_radius = 3;
800                         p->random_center = 5;
801                         p->random_backbone = 5;
802                 }
803                 break;
804         case LS_MODIFIER_2D_OFFSET:
805                 {
806                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
807                         p->start = 0.0f;
808                         p->end = 0.0f;
809                         p->x = 0.0f;
810                         p->y = 0.0f;
811                 }
812                 break;
813         case LS_MODIFIER_2D_TRANSFORM:
814                 {
815                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
816                         p->pivot = LS_MODIFIER_2D_TRANSFORM_PIVOT_CENTER;
817                         p->scale_x = 1.0f;
818                         p->scale_y = 1.0f;
819                         p->angle = DEG2RADF(0.0f);
820                         p->pivot_u = 0.5f;
821                         p->pivot_x = 0.0f;
822                         p->pivot_y = 0.0f;
823                 }
824                 break;
825         default:
826                 return NULL; /* unknown modifier type */
827         }
828         add_to_modifier_list(&linestyle->geometry_modifiers, m);
829
830         return m;
831 }
832
833 LineStyleModifier *BKE_copy_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
834 {
835         LineStyleModifier *new_m;
836
837         new_m = alloc_geometry_modifier(m->type);
838         new_m->flags = m->flags;
839
840         switch (m->type) {
841         case LS_MODIFIER_SAMPLING:
842                 {
843                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
844                         LineStyleGeometryModifier_Sampling *q = (LineStyleGeometryModifier_Sampling *)new_m;
845                         q->sampling = p->sampling;
846                 }
847                 break;
848         case LS_MODIFIER_BEZIER_CURVE:
849                 {
850                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
851                         LineStyleGeometryModifier_BezierCurve *q = (LineStyleGeometryModifier_BezierCurve *)new_m;
852                         q->error = p->error;
853                 }
854                 break;
855         case LS_MODIFIER_SINUS_DISPLACEMENT:
856                 {
857                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
858                         LineStyleGeometryModifier_SinusDisplacement *q = (LineStyleGeometryModifier_SinusDisplacement *)new_m;
859                         q->wavelength = p->wavelength;
860                         q->amplitude = p->amplitude;
861                         q->phase = p->phase;
862                 }
863                 break;
864         case LS_MODIFIER_SPATIAL_NOISE:
865                 {
866                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
867                         LineStyleGeometryModifier_SpatialNoise *q = (LineStyleGeometryModifier_SpatialNoise *)new_m;
868                         q->amplitude = p->amplitude;
869                         q->scale = p->scale;
870                         q->octaves = p->octaves;
871                         q->flags = p->flags;
872                 }
873                 break;
874         case LS_MODIFIER_PERLIN_NOISE_1D:
875                 {
876                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
877                         LineStyleGeometryModifier_PerlinNoise1D *q = (LineStyleGeometryModifier_PerlinNoise1D *)new_m;
878                         q->frequency = p->frequency;
879                         q->amplitude = p->amplitude;
880                         q->angle = p->angle;
881                         q->octaves = p->octaves;
882                         q->seed = p->seed;
883                 }
884                 break;
885         case LS_MODIFIER_PERLIN_NOISE_2D:
886                 {
887                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
888                         LineStyleGeometryModifier_PerlinNoise2D *q = (LineStyleGeometryModifier_PerlinNoise2D *)new_m;
889                         q->frequency = p->frequency;
890                         q->amplitude = p->amplitude;
891                         q->angle = p->angle;
892                         q->octaves = p->octaves;
893                         q->seed = p->seed;
894                 }
895                 break;
896         case LS_MODIFIER_BACKBONE_STRETCHER:
897                 {
898                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
899                         LineStyleGeometryModifier_BackboneStretcher *q = (LineStyleGeometryModifier_BackboneStretcher *)new_m;
900                         q->backbone_length = p->backbone_length;
901                 }
902                 break;
903         case LS_MODIFIER_TIP_REMOVER:
904                 {
905                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
906                         LineStyleGeometryModifier_TipRemover *q = (LineStyleGeometryModifier_TipRemover *)new_m;
907                         q->tip_length = p->tip_length;
908                 }
909                 break;
910         case LS_MODIFIER_POLYGONIZATION:
911                 {
912                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
913                         LineStyleGeometryModifier_Polygonalization *q = (LineStyleGeometryModifier_Polygonalization *)new_m;
914                         q->error = p->error;
915                 }
916                 break;
917         case LS_MODIFIER_GUIDING_LINES:
918                 {
919                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
920                         LineStyleGeometryModifier_GuidingLines *q = (LineStyleGeometryModifier_GuidingLines *)new_m;
921                         q->offset = p->offset;
922                 }
923                 break;
924         case LS_MODIFIER_BLUEPRINT:
925                 {
926                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
927                         LineStyleGeometryModifier_Blueprint *q = (LineStyleGeometryModifier_Blueprint *)new_m;
928                         q->flags = p->flags;
929                         q->rounds = p->rounds;
930                         q->backbone_length = p->backbone_length;
931                         q->random_radius = p->random_radius;
932                         q->random_center = p->random_center;
933                         q->random_backbone = p->random_backbone;
934                 }
935                 break;
936         case LS_MODIFIER_2D_OFFSET:
937                 {
938                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
939                         LineStyleGeometryModifier_2DOffset *q = (LineStyleGeometryModifier_2DOffset *)new_m;
940                         q->start = p->start;
941                         q->end = p->end;
942                         q->x = p->x;
943                         q->y = p->y;
944                 }
945                 break;
946         case LS_MODIFIER_2D_TRANSFORM:
947                 {
948                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
949                         LineStyleGeometryModifier_2DTransform *q = (LineStyleGeometryModifier_2DTransform *)new_m;
950                         q->pivot = p->pivot;
951                         q->scale_x = p->scale_x;
952                         q->scale_y = p->scale_y;
953                         q->angle = p->angle;
954                         q->pivot_u = p->pivot_u;
955                         q->pivot_x = p->pivot_x;
956                         q->pivot_y = p->pivot_y;
957                 }
958                 break;
959         default:
960                 return NULL; /* unknown modifier type */
961         }
962         add_to_modifier_list(&linestyle->geometry_modifiers, new_m);
963
964         return new_m;
965 }
966
967 void BKE_remove_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
968 {
969         BLI_freelinkN(&linestyle->geometry_modifiers, m);
970 }
971
972 static void move_modifier(ListBase *lb, LineStyleModifier *modifier, int direction)
973 {
974         BLI_remlink(lb, modifier);
975         if (direction > 0)
976                 BLI_insertlinkbefore(lb, modifier->prev, modifier);
977         else
978                 BLI_insertlinkafter(lb, modifier->next, modifier);
979 }
980
981 void BKE_move_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
982 {
983         move_modifier(&linestyle->color_modifiers, modifier, direction);
984 }
985
986 void BKE_move_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
987 {
988         move_modifier(&linestyle->alpha_modifiers, modifier, direction);
989 }
990
991 void BKE_move_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
992 {
993         move_modifier(&linestyle->thickness_modifiers, modifier, direction);
994 }
995
996 void BKE_move_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
997 {
998         move_modifier(&linestyle->geometry_modifiers, modifier, direction);
999 }
1000
1001 void BKE_list_modifier_color_ramps(FreestyleLineStyle *linestyle, ListBase *listbase)
1002 {
1003         LineStyleModifier *m;
1004         ColorBand *color_ramp;
1005         LinkData *link;
1006
1007         listbase->first = listbase->last = NULL;
1008         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1009                 switch (m->type) {
1010                 case LS_MODIFIER_ALONG_STROKE:
1011                         color_ramp = ((LineStyleColorModifier_AlongStroke *)m)->color_ramp;
1012                         break;
1013                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
1014                         color_ramp = ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp;
1015                         break;
1016                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1017                         color_ramp = ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp;
1018                         break;
1019                 case LS_MODIFIER_MATERIAL:
1020                         color_ramp = ((LineStyleColorModifier_Material *)m)->color_ramp;
1021                         break;
1022                 default:
1023                         continue;
1024                 }
1025                 link = (LinkData *) MEM_callocN( sizeof(LinkData), "link to color ramp");
1026                 link->data = color_ramp;
1027                 BLI_addtail(listbase, link);
1028         }
1029 }
1030
1031 char *BKE_path_from_ID_to_color_ramp(FreestyleLineStyle *linestyle, ColorBand *color_ramp)
1032 {
1033         LineStyleModifier *m;
1034         bool found = false;
1035
1036         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1037                 switch (m->type) {
1038                         case LS_MODIFIER_ALONG_STROKE:
1039                                 if (color_ramp == ((LineStyleColorModifier_AlongStroke *)m)->color_ramp)
1040                                         found = true;
1041                                 break;
1042                         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
1043                                 if (color_ramp == ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp)
1044                                         found = true;
1045                                 break;
1046                         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1047                                 if (color_ramp == ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp)
1048                                         found = true;
1049                                 break;
1050                         case LS_MODIFIER_MATERIAL:
1051                                 if (color_ramp == ((LineStyleColorModifier_Material *)m)->color_ramp)
1052                                         found = true;
1053                                 break;
1054                 }
1055
1056                 if (found) {
1057                         char name_esc[sizeof(m->name) * 2];
1058                         BLI_strescape(name_esc, m->name, sizeof(name_esc));
1059                         return BLI_sprintfN("color_modifiers[\"%s\"].color_ramp", name_esc);
1060                 }
1061         }
1062         printf("BKE_path_from_ID_to_color_ramp: No color ramps correspond to the given pointer.\n");
1063         return NULL;
1064 }
1065
1066 void BKE_unlink_linestyle_target_object(FreestyleLineStyle *linestyle, struct Object *ob)
1067 {
1068         LineStyleModifier *m;
1069
1070         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1071                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1072                         if (((LineStyleColorModifier_DistanceFromObject *)m)->target == ob) {
1073                                 ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
1074                         }
1075                 }
1076         }
1077         for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next) {
1078                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1079                         if (((LineStyleAlphaModifier_DistanceFromObject *)m)->target == ob) {
1080                                 ((LineStyleAlphaModifier_DistanceFromObject *)m)->target = NULL;
1081                         }
1082                 }
1083         }
1084         for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next) {
1085                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1086                         if (((LineStyleThicknessModifier_DistanceFromObject *)m)->target == ob) {
1087                                 ((LineStyleThicknessModifier_DistanceFromObject *)m)->target = NULL;
1088                         }
1089                 }
1090         }
1091 }