Freestyle: fix crash unlinking object
[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 = 1.0f;
80         linestyle->alpha = 1.0f;
81         linestyle->thickness = 1.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->min_angle = linestyle->min_angle;
151         new_linestyle->max_angle = linestyle->max_angle;
152         new_linestyle->min_length = linestyle->min_length;
153         new_linestyle->max_length = linestyle->max_length;
154         new_linestyle->split_length = linestyle->split_length;
155         new_linestyle->dash1 = linestyle->dash1;
156         new_linestyle->gap1 = linestyle->gap1;
157         new_linestyle->dash2 = linestyle->dash2;
158         new_linestyle->gap2 = linestyle->gap2;
159         new_linestyle->dash3 = linestyle->dash3;
160         new_linestyle->gap3 = linestyle->gap3;
161         new_linestyle->panel = linestyle->panel;
162         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next)
163                 BKE_copy_linestyle_color_modifier(new_linestyle, m);
164         for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next)
165                 BKE_copy_linestyle_alpha_modifier(new_linestyle, m);
166         for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next)
167                 BKE_copy_linestyle_thickness_modifier(new_linestyle, m);
168         for (m = (LineStyleModifier *)linestyle->geometry_modifiers.first; m; m = m->next)
169                 BKE_copy_linestyle_geometry_modifier(new_linestyle, m);
170
171         return new_linestyle;
172 }
173
174 static LineStyleModifier *new_modifier(int type, size_t size)
175 {
176         LineStyleModifier *m;
177
178         m = (LineStyleModifier *)MEM_callocN(size, "line style modifier");
179         m->type = type;
180         strcpy(m->name, modifier_name[type]);
181         m->influence = 1.0f;
182         m->flags = LS_MODIFIER_ENABLED | LS_MODIFIER_EXPANDED;
183
184         return m;
185 }
186
187 static void add_to_modifier_list(ListBase *lb, LineStyleModifier *m)
188 {
189         BLI_addtail(lb, (void *)m);
190         BLI_uniquename(lb, m, modifier_name[m->type], '.', offsetof(LineStyleModifier, name), sizeof(m->name));
191 }
192
193 static LineStyleModifier *alloc_color_modifier(int type)
194 {
195         size_t size;
196
197         switch (type) {
198         case LS_MODIFIER_ALONG_STROKE:
199                 size = sizeof(LineStyleColorModifier_AlongStroke);
200                 break;
201         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
202                 size = sizeof(LineStyleColorModifier_DistanceFromCamera);
203                 break;
204         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
205                 size = sizeof(LineStyleColorModifier_DistanceFromObject);
206                 break;
207         case LS_MODIFIER_MATERIAL:
208                 size = sizeof(LineStyleColorModifier_Material);
209                 break;
210         default:
211                 return NULL; /* unknown modifier type */
212         }
213
214         return new_modifier(type, size);
215 }
216
217 LineStyleModifier *BKE_add_linestyle_color_modifier(FreestyleLineStyle *linestyle, int type)
218 {
219         LineStyleModifier *m;
220
221         m = alloc_color_modifier(type);
222         m->blend = MA_RAMP_BLEND;
223
224         switch (type) {
225         case LS_MODIFIER_ALONG_STROKE:
226                 ((LineStyleColorModifier_AlongStroke *)m)->color_ramp = add_colorband(1);
227                 break;
228         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
229                 ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp = add_colorband(1);
230                 ((LineStyleColorModifier_DistanceFromCamera *)m)->range_min = 0.0f;
231                 ((LineStyleColorModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
232                 break;
233         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
234                 ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
235                 ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp = add_colorband(1);
236                 ((LineStyleColorModifier_DistanceFromObject *)m)->range_min = 0.0f;
237                 ((LineStyleColorModifier_DistanceFromObject *)m)->range_max = 10000.0f;
238                 break;
239         case LS_MODIFIER_MATERIAL:
240                 ((LineStyleColorModifier_Material *)m)->color_ramp = add_colorband(1);
241                 ((LineStyleColorModifier_Material *)m)->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
242                 break;
243         default:
244                 return NULL; /* unknown modifier type */
245         }
246         add_to_modifier_list(&linestyle->color_modifiers, m);
247
248         return m;
249 }
250
251 LineStyleModifier *BKE_copy_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
252 {
253         LineStyleModifier *new_m;
254
255         new_m = alloc_color_modifier(m->type);
256         new_m->influence = m->influence;
257         new_m->flags = m->flags;
258         new_m->blend = m->blend;
259
260         switch (m->type) {
261         case LS_MODIFIER_ALONG_STROKE:
262                 {
263                         LineStyleColorModifier_AlongStroke *p = (LineStyleColorModifier_AlongStroke *)m;
264                         LineStyleColorModifier_AlongStroke *q = (LineStyleColorModifier_AlongStroke *)new_m;
265                         q->color_ramp = MEM_dupallocN(p->color_ramp);
266                 }
267                 break;
268         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
269                 {
270                         LineStyleColorModifier_DistanceFromCamera *p = (LineStyleColorModifier_DistanceFromCamera *)m;
271                         LineStyleColorModifier_DistanceFromCamera *q = (LineStyleColorModifier_DistanceFromCamera *)new_m;
272                         q->color_ramp = MEM_dupallocN(p->color_ramp);
273                         q->range_min = p->range_min;
274                         q->range_max = p->range_max;
275                 }
276                 break;
277         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
278                 {
279                         LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)m;
280                         LineStyleColorModifier_DistanceFromObject *q = (LineStyleColorModifier_DistanceFromObject *)new_m;
281                         if (p->target)
282                                 p->target->id.us++;
283                         q->target = p->target;
284                         q->color_ramp = MEM_dupallocN(p->color_ramp);
285                         q->range_min = p->range_min;
286                         q->range_max = p->range_max;
287                 }
288                 break;
289         case LS_MODIFIER_MATERIAL:
290                 {
291                         LineStyleColorModifier_Material *p = (LineStyleColorModifier_Material *)m;
292                         LineStyleColorModifier_Material *q = (LineStyleColorModifier_Material *)new_m;
293                         q->color_ramp = MEM_dupallocN(p->color_ramp);
294                         q->mat_attr = p->mat_attr;
295                 }
296                 break;
297         default:
298                 return NULL; /* unknown modifier type */
299         }
300         add_to_modifier_list(&linestyle->color_modifiers, new_m);
301
302         return new_m;
303 }
304
305 void BKE_remove_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
306 {
307         switch (m->type) {
308         case LS_MODIFIER_ALONG_STROKE:
309                 MEM_freeN(((LineStyleColorModifier_AlongStroke *)m)->color_ramp);
310                 break;
311         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
312                 MEM_freeN(((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp);
313                 break;
314         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
315                 MEM_freeN(((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp);
316                 break;
317         case LS_MODIFIER_MATERIAL:
318                 MEM_freeN(((LineStyleColorModifier_Material *)m)->color_ramp);
319                 break;
320         }
321         BLI_freelinkN(&linestyle->color_modifiers, m);
322 }
323
324 static LineStyleModifier *alloc_alpha_modifier(int type)
325 {
326         size_t size;
327
328         switch (type) {
329         case LS_MODIFIER_ALONG_STROKE:
330                 size = sizeof(LineStyleAlphaModifier_AlongStroke);
331                 break;
332         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
333                 size = sizeof(LineStyleAlphaModifier_DistanceFromCamera);
334                 break;
335         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
336                 size = sizeof(LineStyleAlphaModifier_DistanceFromObject);
337                 break;
338         case LS_MODIFIER_MATERIAL:
339                 size = sizeof(LineStyleAlphaModifier_Material);
340                 break;
341         default:
342                 return NULL; /* unknown modifier type */
343         }
344         return new_modifier(type, size);
345 }
346
347 LineStyleModifier *BKE_add_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, int type)
348 {
349         LineStyleModifier *m;
350
351         m = alloc_alpha_modifier(type);
352         m->blend = LS_VALUE_BLEND;
353
354         switch (type) {
355         case LS_MODIFIER_ALONG_STROKE:
356                 {
357                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
358                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
359                 }
360                 break;
361         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
362                 {
363                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
364                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
365                         p->range_min = 0.0f;
366                         p->range_max = 10000.0f;
367                 }
368                 break;
369         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
370                 {
371                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
372                         p->target = NULL;
373                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
374                         p->range_min = 0.0f;
375                         p->range_max = 10000.0f;
376                 }
377                 break;
378         case LS_MODIFIER_MATERIAL:
379                 {
380                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
381                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
382                         p->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
383                 }
384                 break;
385         default:
386                 return NULL; /* unknown modifier type */
387         }
388         add_to_modifier_list(&linestyle->alpha_modifiers, m);
389
390         return m;
391 }
392
393 LineStyleModifier *BKE_copy_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
394 {
395         LineStyleModifier *new_m;
396
397         new_m = alloc_alpha_modifier(m->type);
398         new_m->influence = m->influence;
399         new_m->flags = m->flags;
400         new_m->blend = m->blend;
401
402         switch (m->type) {
403         case LS_MODIFIER_ALONG_STROKE:
404                 {
405                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
406                         LineStyleAlphaModifier_AlongStroke *q = (LineStyleAlphaModifier_AlongStroke *)new_m;
407                         q->curve = curvemapping_copy(p->curve);
408                 }
409                 break;
410         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
411                 {
412                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
413                         LineStyleAlphaModifier_DistanceFromCamera *q = (LineStyleAlphaModifier_DistanceFromCamera *)new_m;
414                         q->curve = curvemapping_copy(p->curve);
415                         q->range_min = p->range_min;
416                         q->range_max = p->range_max;
417                 }
418                 break;
419         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
420                 {
421                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
422                         LineStyleAlphaModifier_DistanceFromObject *q = (LineStyleAlphaModifier_DistanceFromObject *)new_m;
423                         if (p->target)
424                                 p->target->id.us++;
425                         q->target = p->target;
426                         q->curve = curvemapping_copy(p->curve);
427                         q->range_min = p->range_min;
428                         q->range_max = p->range_max;
429                 }
430                 break;
431         case LS_MODIFIER_MATERIAL:
432                 {
433                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
434                         LineStyleAlphaModifier_Material *q = (LineStyleAlphaModifier_Material *)new_m;
435                         q->curve = curvemapping_copy(p->curve);
436                         q->mat_attr = p->mat_attr;
437                 }
438                 break;
439         default:
440                 return NULL; /* unknown modifier type */
441         }
442         add_to_modifier_list(&linestyle->alpha_modifiers, new_m);
443
444         return new_m;
445 }
446
447 void BKE_remove_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
448 {
449         switch (m->type) {
450         case LS_MODIFIER_ALONG_STROKE:
451                 curvemapping_free(((LineStyleAlphaModifier_AlongStroke *)m)->curve);
452                 break;
453         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
454                 curvemapping_free(((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve);
455                 break;
456         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
457                 curvemapping_free(((LineStyleAlphaModifier_DistanceFromObject *)m)->curve);
458                 break;
459         case LS_MODIFIER_MATERIAL:
460                 curvemapping_free(((LineStyleAlphaModifier_Material *)m)->curve);
461                 break;
462         }
463         BLI_freelinkN(&linestyle->alpha_modifiers, m);
464 }
465
466 static LineStyleModifier *alloc_thickness_modifier(int type)
467 {
468         size_t size;
469
470         switch (type) {
471         case LS_MODIFIER_ALONG_STROKE:
472                 size = sizeof(LineStyleThicknessModifier_AlongStroke);
473                 break;
474         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
475                 size = sizeof(LineStyleThicknessModifier_DistanceFromCamera);
476                 break;
477         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
478                 size = sizeof(LineStyleThicknessModifier_DistanceFromObject);
479                 break;
480         case LS_MODIFIER_MATERIAL:
481                 size = sizeof(LineStyleThicknessModifier_Material);
482                 break;
483         case LS_MODIFIER_CALLIGRAPHY:
484                 size = sizeof(LineStyleThicknessModifier_Calligraphy);
485                 break;
486         default:
487                 return NULL; /* unknown modifier type */
488         }
489
490         return new_modifier(type, size);
491 }
492
493 LineStyleModifier *BKE_add_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, int type)
494 {
495         LineStyleModifier *m;
496
497         m = alloc_thickness_modifier(type);
498         m->blend = LS_VALUE_BLEND;
499
500         switch (type) {
501         case LS_MODIFIER_ALONG_STROKE:
502                 {
503                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
504                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
505                         p->value_min = 0.0f;
506                         p->value_max = 1.0f;
507                 }
508                 break;
509         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
510                 {
511                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
512                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
513                         p->range_min = 0.0f;
514                         p->range_max = 1000.0f;
515                         p->value_min = 0.0f;
516                         p->value_max = 1.0f;
517                 }
518                 break;
519         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
520                 {
521                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
522                         p->target = NULL;
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_MATERIAL:
531                 {
532                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
533                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
534                         p->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
535                         p->value_min = 0.0f;
536                         p->value_max = 1.0f;
537                 }
538                 break;
539         case LS_MODIFIER_CALLIGRAPHY:
540                 {
541                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
542                         p->min_thickness = 1.0f;
543                         p->max_thickness = 10.0f;
544                         p->orientation = DEG2RADF(60.0f);
545                 }
546                 break;
547         default:
548                 return NULL; /* unknown modifier type */
549         }
550         add_to_modifier_list(&linestyle->thickness_modifiers, m);
551
552         return m;
553 }
554
555 LineStyleModifier *BKE_copy_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
556 {
557         LineStyleModifier *new_m;
558
559         new_m = alloc_thickness_modifier(m->type);
560         if (!new_m)
561                 return NULL;
562         new_m->influence = m->influence;
563         new_m->flags = m->flags;
564         new_m->blend = m->blend;
565
566         switch (m->type) {
567         case LS_MODIFIER_ALONG_STROKE:
568                 {
569                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
570                         LineStyleThicknessModifier_AlongStroke *q = (LineStyleThicknessModifier_AlongStroke *)new_m;
571                         q->curve = curvemapping_copy(p->curve);
572                         q->value_min = p->value_min;
573                         q->value_max = p->value_max;
574                 }
575                 break;
576         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
577                 {
578                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
579                         LineStyleThicknessModifier_DistanceFromCamera *q = (LineStyleThicknessModifier_DistanceFromCamera *)new_m;
580                         q->curve = curvemapping_copy(p->curve);
581                         q->range_min = p->range_min;
582                         q->range_max = p->range_max;
583                         q->value_min = p->value_min;
584                         q->value_max = p->value_max;
585                 }
586                 break;
587         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
588                 {
589                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
590                         LineStyleThicknessModifier_DistanceFromObject *q = (LineStyleThicknessModifier_DistanceFromObject *)new_m;
591                         if (p->target)
592                                 p->target->id.us++;
593                         q->target = p->target;
594                         q->curve = curvemapping_copy(p->curve);
595                         q->range_min = p->range_min;
596                         q->range_max = p->range_max;
597                         q->value_min = p->value_min;
598                         q->value_max = p->value_max;
599                 }
600                 break;
601         case LS_MODIFIER_MATERIAL:
602                 {
603                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
604                         LineStyleThicknessModifier_Material *q = (LineStyleThicknessModifier_Material *)new_m;
605                         q->curve = curvemapping_copy(p->curve);
606                         q->mat_attr = p->mat_attr;
607                         q->value_min = p->value_min;
608                         q->value_max = p->value_max;
609                 }
610                 break;
611         case LS_MODIFIER_CALLIGRAPHY:
612                 {
613                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
614                         LineStyleThicknessModifier_Calligraphy *q = (LineStyleThicknessModifier_Calligraphy *)new_m;
615                         q->min_thickness = p->min_thickness;
616                         q->max_thickness = p->max_thickness;
617                         q->orientation = p->orientation;
618                 }
619                 break;
620         default:
621                 return NULL; /* unknown modifier type */
622         }
623         add_to_modifier_list(&linestyle->thickness_modifiers, new_m);
624
625         return new_m;
626 }
627
628 void BKE_remove_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
629 {
630         switch (m->type) {
631         case LS_MODIFIER_ALONG_STROKE:
632                 curvemapping_free(((LineStyleThicknessModifier_AlongStroke *)m)->curve);
633                 break;
634         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
635                 curvemapping_free(((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve);
636                 break;
637         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
638                 curvemapping_free(((LineStyleThicknessModifier_DistanceFromObject *)m)->curve);
639                 break;
640         case LS_MODIFIER_MATERIAL:
641                 curvemapping_free(((LineStyleThicknessModifier_Material *)m)->curve);
642                 break;
643         case LS_MODIFIER_CALLIGRAPHY:
644                 break;
645         }
646         BLI_freelinkN(&linestyle->thickness_modifiers, m);
647 }
648
649 static LineStyleModifier *alloc_geometry_modifier(int type)
650 {
651         size_t size;
652
653         switch (type) {
654         case LS_MODIFIER_SAMPLING:
655                 size = sizeof(LineStyleGeometryModifier_Sampling);
656                 break;
657         case LS_MODIFIER_BEZIER_CURVE:
658                 size = sizeof(LineStyleGeometryModifier_BezierCurve);
659                 break;
660         case LS_MODIFIER_SINUS_DISPLACEMENT:
661                 size = sizeof(LineStyleGeometryModifier_SinusDisplacement);
662                 break;
663         case LS_MODIFIER_SPATIAL_NOISE:
664                 size = sizeof(LineStyleGeometryModifier_SpatialNoise);
665                 break;
666         case LS_MODIFIER_PERLIN_NOISE_1D:
667                 size = sizeof(LineStyleGeometryModifier_PerlinNoise1D);
668                 break;
669         case LS_MODIFIER_PERLIN_NOISE_2D:
670                 size = sizeof(LineStyleGeometryModifier_PerlinNoise2D);
671                 break;
672         case LS_MODIFIER_BACKBONE_STRETCHER:
673                 size = sizeof(LineStyleGeometryModifier_BackboneStretcher);
674                 break;
675         case LS_MODIFIER_TIP_REMOVER:
676                 size = sizeof(LineStyleGeometryModifier_TipRemover);
677                 break;
678         case LS_MODIFIER_POLYGONIZATION:
679                 size = sizeof(LineStyleGeometryModifier_Polygonalization);
680                 break;
681         case LS_MODIFIER_GUIDING_LINES:
682                 size = sizeof(LineStyleGeometryModifier_GuidingLines);
683                 break;
684         case LS_MODIFIER_BLUEPRINT:
685                 size = sizeof(LineStyleGeometryModifier_Blueprint);
686                 break;
687         case LS_MODIFIER_2D_OFFSET:
688                 size = sizeof(LineStyleGeometryModifier_2DOffset);
689                 break;
690         case LS_MODIFIER_2D_TRANSFORM:
691                 size = sizeof(LineStyleGeometryModifier_2DTransform);
692                 break;
693         default:
694                 return NULL; /* unknown modifier type */
695         }
696
697         return new_modifier(type, size);
698 }
699
700 LineStyleModifier *BKE_add_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, int type)
701 {
702         LineStyleModifier *m;
703
704         m = alloc_geometry_modifier(type);
705
706         switch (type) {
707         case LS_MODIFIER_SAMPLING:
708                 {
709                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
710                         p->sampling = 10.0f;
711                 }
712                 break;
713         case LS_MODIFIER_BEZIER_CURVE:
714                 {
715                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
716                         p->error = 10.0f;
717                 }
718                 break;
719         case LS_MODIFIER_SINUS_DISPLACEMENT:
720                 {
721                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
722                         p->wavelength = 20.0f;
723                         p->amplitude = 5.0f;
724                         p->phase = 0.0f;
725                 }
726                 break;
727         case LS_MODIFIER_SPATIAL_NOISE:
728                 {
729                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
730                         p->amplitude = 5.0f;
731                         p->scale = 20.0f;
732                         p->octaves = 4;
733                         p->flags = LS_MODIFIER_SPATIAL_NOISE_SMOOTH | LS_MODIFIER_SPATIAL_NOISE_PURERANDOM;
734                 }
735                 break;
736         case LS_MODIFIER_PERLIN_NOISE_1D:
737                 {
738                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
739                         p->frequency = 10.0f;
740                         p->amplitude = 10.0f;
741                         p->octaves = 4;
742                         p->angle = DEG2RADF(45.0f);
743                 }
744                 break;
745         case LS_MODIFIER_PERLIN_NOISE_2D:
746                 {
747                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
748                         p->frequency = 10.0f;
749                         p->amplitude = 10.0f;
750                         p->octaves = 4;
751                         p->angle = DEG2RADF(45.0f);
752                 }
753                 break;
754         case LS_MODIFIER_BACKBONE_STRETCHER:
755                 {
756                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
757                         p->backbone_length = 10.0f;
758                 }
759                 break;
760         case LS_MODIFIER_TIP_REMOVER:
761                 {
762                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
763                         p->tip_length = 10.0f;
764                 }
765                 break;
766         case LS_MODIFIER_POLYGONIZATION:
767                 {
768                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
769                         p->error = 10.0f;
770                 }
771                 break;
772         case LS_MODIFIER_GUIDING_LINES:
773                 {
774                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
775                         p->offset = 0.0f;
776                 }
777                 break;
778         case LS_MODIFIER_BLUEPRINT:
779                 {
780                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
781                         p->flags = LS_MODIFIER_BLUEPRINT_CIRCLES;
782                         p->rounds = 1;
783                         p->backbone_length = 10.0f;
784                         p->random_radius = 3;
785                         p->random_center = 5;
786                         p->random_backbone = 5;
787                 }
788                 break;
789         case LS_MODIFIER_2D_OFFSET:
790                 {
791                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
792                         p->start = 0.0f;
793                         p->end = 0.0f;
794                         p->x = 0.0f;
795                         p->y = 0.0f;
796                 }
797                 break;
798         case LS_MODIFIER_2D_TRANSFORM:
799                 {
800                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
801                         p->pivot = LS_MODIFIER_2D_TRANSFORM_PIVOT_CENTER;
802                         p->scale_x = 1.0f;
803                         p->scale_y = 1.0f;
804                         p->angle = DEG2RADF(0.0f);
805                         p->pivot_u = 0.5f;
806                         p->pivot_x = 0.0f;
807                         p->pivot_y = 0.0f;
808                 }
809                 break;
810         default:
811                 return NULL; /* unknown modifier type */
812         }
813         add_to_modifier_list(&linestyle->geometry_modifiers, m);
814
815         return m;
816 }
817
818 LineStyleModifier *BKE_copy_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
819 {
820         LineStyleModifier *new_m;
821
822         new_m = alloc_geometry_modifier(m->type);
823         new_m->flags = m->flags;
824
825         switch (m->type) {
826         case LS_MODIFIER_SAMPLING:
827                 {
828                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
829                         LineStyleGeometryModifier_Sampling *q = (LineStyleGeometryModifier_Sampling *)new_m;
830                         q->sampling = p->sampling;
831                 }
832                 break;
833         case LS_MODIFIER_BEZIER_CURVE:
834                 {
835                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
836                         LineStyleGeometryModifier_BezierCurve *q = (LineStyleGeometryModifier_BezierCurve *)new_m;
837                         q->error = p->error;
838                 }
839                 break;
840         case LS_MODIFIER_SINUS_DISPLACEMENT:
841                 {
842                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
843                         LineStyleGeometryModifier_SinusDisplacement *q = (LineStyleGeometryModifier_SinusDisplacement *)new_m;
844                         q->wavelength = p->wavelength;
845                         q->amplitude = p->amplitude;
846                         q->phase = p->phase;
847                 }
848                 break;
849         case LS_MODIFIER_SPATIAL_NOISE:
850                 {
851                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
852                         LineStyleGeometryModifier_SpatialNoise *q = (LineStyleGeometryModifier_SpatialNoise *)new_m;
853                         q->amplitude = p->amplitude;
854                         q->scale = p->scale;
855                         q->octaves = p->octaves;
856                         q->flags = p->flags;
857                 }
858                 break;
859         case LS_MODIFIER_PERLIN_NOISE_1D:
860                 {
861                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
862                         LineStyleGeometryModifier_PerlinNoise1D *q = (LineStyleGeometryModifier_PerlinNoise1D *)new_m;
863                         q->frequency = p->frequency;
864                         q->amplitude = p->amplitude;
865                         q->octaves = p->octaves;
866                         q->angle = p->angle;
867                 }
868                 break;
869         case LS_MODIFIER_PERLIN_NOISE_2D:
870                 {
871                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
872                         LineStyleGeometryModifier_PerlinNoise2D *q = (LineStyleGeometryModifier_PerlinNoise2D *)new_m;
873                         q->frequency = p->frequency;
874                         q->amplitude = p->amplitude;
875                         q->octaves = p->octaves;
876                         q->angle = p->angle;
877                 }
878                 break;
879         case LS_MODIFIER_BACKBONE_STRETCHER:
880                 {
881                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
882                         LineStyleGeometryModifier_BackboneStretcher *q = (LineStyleGeometryModifier_BackboneStretcher *)new_m;
883                         q->backbone_length = p->backbone_length;
884                 }
885                 break;
886         case LS_MODIFIER_TIP_REMOVER:
887                 {
888                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
889                         LineStyleGeometryModifier_TipRemover *q = (LineStyleGeometryModifier_TipRemover *)new_m;
890                         q->tip_length = p->tip_length;
891                 }
892                 break;
893         case LS_MODIFIER_POLYGONIZATION:
894                 {
895                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
896                         LineStyleGeometryModifier_Polygonalization *q = (LineStyleGeometryModifier_Polygonalization *)new_m;
897                         q->error = p->error;
898                 }
899                 break;
900         case LS_MODIFIER_GUIDING_LINES:
901                 {
902                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
903                         LineStyleGeometryModifier_GuidingLines *q = (LineStyleGeometryModifier_GuidingLines *)new_m;
904                         q->offset = p->offset;
905                 }
906                 break;
907         case LS_MODIFIER_BLUEPRINT:
908                 {
909                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
910                         LineStyleGeometryModifier_Blueprint *q = (LineStyleGeometryModifier_Blueprint *)new_m;
911                         q->flags = p->flags;
912                         q->rounds = p->rounds;
913                         q->backbone_length = p->backbone_length;
914                         q->random_radius = p->random_radius;
915                         q->random_center = p->random_center;
916                         q->random_backbone = p->random_backbone;
917                 }
918                 break;
919         case LS_MODIFIER_2D_OFFSET:
920                 {
921                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
922                         LineStyleGeometryModifier_2DOffset *q = (LineStyleGeometryModifier_2DOffset *)new_m;
923                         q->start = p->start;
924                         q->end = p->end;
925                         q->x = p->x;
926                         q->y = p->y;
927                 }
928                 break;
929         case LS_MODIFIER_2D_TRANSFORM:
930                 {
931                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
932                         LineStyleGeometryModifier_2DTransform *q = (LineStyleGeometryModifier_2DTransform *)new_m;
933                         q->pivot = p->pivot;
934                         q->scale_x = p->scale_x;
935                         q->scale_y = p->scale_y;
936                         q->angle = p->angle;
937                         q->pivot_u = p->pivot_u;
938                         q->pivot_x = p->pivot_x;
939                         q->pivot_y = p->pivot_y;
940                 }
941                 break;
942         default:
943                 return NULL; /* unknown modifier type */
944         }
945         add_to_modifier_list(&linestyle->geometry_modifiers, new_m);
946
947         return new_m;
948 }
949
950 void BKE_remove_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
951 {
952         BLI_freelinkN(&linestyle->geometry_modifiers, m);
953 }
954
955 static void move_modifier(ListBase *lb, LineStyleModifier *modifier, int direction)
956 {
957         BLI_remlink(lb, modifier);
958         if (direction > 0)
959                 BLI_insertlinkbefore(lb, modifier->prev, modifier);
960         else
961                 BLI_insertlinkafter(lb, modifier->next, modifier);
962 }
963
964 void BKE_move_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
965 {
966         move_modifier(&linestyle->color_modifiers, modifier, direction);
967 }
968
969 void BKE_move_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
970 {
971         move_modifier(&linestyle->alpha_modifiers, modifier, direction);
972 }
973
974 void BKE_move_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
975 {
976         move_modifier(&linestyle->thickness_modifiers, modifier, direction);
977 }
978
979 void BKE_move_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
980 {
981         move_modifier(&linestyle->geometry_modifiers, modifier, direction);
982 }
983
984 void BKE_list_modifier_color_ramps(FreestyleLineStyle *linestyle, ListBase *listbase)
985 {
986         LineStyleModifier *m;
987         ColorBand *color_ramp;
988         LinkData *link;
989
990         listbase->first = listbase->last = NULL;
991         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
992                 switch (m->type) {
993                 case LS_MODIFIER_ALONG_STROKE:
994                         color_ramp = ((LineStyleColorModifier_AlongStroke *)m)->color_ramp;
995                         break;
996                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
997                         color_ramp = ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp;
998                         break;
999                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1000                         color_ramp = ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp;
1001                         break;
1002                 case LS_MODIFIER_MATERIAL:
1003                         color_ramp = ((LineStyleColorModifier_Material *)m)->color_ramp;
1004                         break;
1005                 default:
1006                         continue;
1007                 }
1008                 link = (LinkData *) MEM_callocN( sizeof(LinkData), "link to color ramp");
1009                 link->data = color_ramp;
1010                 BLI_addtail(listbase, link);
1011         }
1012 }
1013
1014 char *BKE_path_from_ID_to_color_ramp(FreestyleLineStyle *linestyle, ColorBand *color_ramp)
1015 {
1016         LineStyleModifier *m;
1017         bool found = false;
1018
1019         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1020                 switch (m->type) {
1021                 case LS_MODIFIER_ALONG_STROKE:
1022                         if (color_ramp == ((LineStyleColorModifier_AlongStroke *)m)->color_ramp)
1023                                 found = true;
1024                         break;
1025                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
1026                         if (color_ramp == ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp)
1027                                 found = true;
1028                         break;
1029                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1030                         if (color_ramp == ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp)
1031                                 found = true;
1032                         break;
1033                 case LS_MODIFIER_MATERIAL:
1034                         if (color_ramp == ((LineStyleColorModifier_Material *)m)->color_ramp)
1035                                 found = true;
1036                         break;
1037                 }
1038                 if (found)
1039                         return BLI_sprintfN("color_modifiers[\"%s\"].color_ramp", m->name);
1040         }
1041         printf("BKE_path_from_ID_to_color_ramp: No color ramps correspond to the given pointer.\n");
1042         return NULL;
1043 }
1044
1045 void BKE_unlink_linestyle_target_object(FreestyleLineStyle *linestyle, struct Object *ob)
1046 {
1047         LineStyleModifier *m;
1048
1049         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1050                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1051                         if (((LineStyleColorModifier_DistanceFromObject *)m)->target == ob) {
1052                                 ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
1053                         }
1054                 }
1055         }
1056         for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next) {
1057                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1058                         if (((LineStyleAlphaModifier_DistanceFromObject *)m)->target == ob) {
1059                                 ((LineStyleAlphaModifier_DistanceFromObject *)m)->target = NULL;
1060                         }
1061                 }
1062         }
1063         for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next) {
1064                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1065                         if (((LineStyleThicknessModifier_DistanceFromObject *)m)->target == ob) {
1066                                 ((LineStyleThicknessModifier_DistanceFromObject *)m)->target = NULL;
1067                         }
1068                 }
1069         }
1070 }