0465c5d54f7930aedc6b0af6dbdee322692e97cd
[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_string_utils.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_colorband.h"
48 #include "BKE_context.h"
49 #include "BKE_freestyle.h"
50 #include "BKE_global.h"
51 #include "BKE_library.h"
52 #include "BKE_linestyle.h"
53 #include "BKE_node.h"
54 #include "BKE_colortools.h"
55 #include "BKE_animsys.h"
56
57 static const char *modifier_name[LS_MODIFIER_NUM] = {
58         NULL,
59         "Along Stroke",
60         "Distance from Camera",
61         "Distance from Object",
62         "Material",
63         "Sampling",
64         "Bezier Curve",
65         "Sinus Displacement",
66         "Spatial Noise",
67         "Perlin Noise 1D",
68         "Perlin Noise 2D",
69         "Backbone Stretcher",
70         "Tip Remover",
71         "Calligraphy",
72         "Polygonalization",
73         "Guiding Lines",
74         "Blueprint",
75         "2D Offset",
76         "2D Transform",
77         "Tangent",
78         "Noise",
79         "Crease Angle",
80         "Simplification",
81         "3D Curvature",
82 };
83
84 void BKE_linestyle_init(FreestyleLineStyle *linestyle)
85 {
86         BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(linestyle, id));
87
88         linestyle->panel = LS_PANEL_STROKES;
89         linestyle->r = linestyle->g = linestyle->b = 0.0f;
90         linestyle->alpha = 1.0f;
91         linestyle->thickness = 3.0f;
92         linestyle->thickness_position = LS_THICKNESS_CENTER;
93         linestyle->thickness_ratio = 0.5f;
94         linestyle->flag = LS_SAME_OBJECT | LS_NO_SORTING | LS_TEXTURE;
95         linestyle->chaining = LS_CHAINING_PLAIN;
96         linestyle->rounds = 3;
97         linestyle->min_angle = DEG2RADF(0.0f);
98         linestyle->max_angle = DEG2RADF(0.0f);
99         linestyle->min_length = 0.0f;
100         linestyle->max_length = 10000.0f;
101         linestyle->split_length = 100;
102         linestyle->chain_count = 10;
103         linestyle->sort_key = LS_SORT_KEY_DISTANCE_FROM_CAMERA;
104         linestyle->integration_type = LS_INTEGRATION_MEAN;
105         linestyle->texstep = 1.0f;
106         linestyle->pr_texture = TEX_PR_TEXTURE;
107
108         BLI_listbase_clear(&linestyle->color_modifiers);
109         BLI_listbase_clear(&linestyle->alpha_modifiers);
110         BLI_listbase_clear(&linestyle->thickness_modifiers);
111         BLI_listbase_clear(&linestyle->geometry_modifiers);
112
113         BKE_linestyle_geometry_modifier_add(linestyle, NULL, LS_MODIFIER_SAMPLING);
114
115         linestyle->caps = LS_CAPS_BUTT;
116 }
117
118 FreestyleLineStyle *BKE_linestyle_new(struct Main *bmain, const char *name)
119 {
120         FreestyleLineStyle *linestyle;
121
122         linestyle = (FreestyleLineStyle *)BKE_libblock_alloc(bmain, ID_LS, name, 0);
123
124         BKE_linestyle_init(linestyle);
125
126         return linestyle;
127 }
128
129 /** Free (or release) any data used by this linestyle (does not free the linestyle itself). */
130 void BKE_linestyle_free(FreestyleLineStyle *linestyle)
131 {
132         LineStyleModifier *m;
133         int a;
134
135         BKE_animdata_free(&linestyle->id, false);
136
137         for (a = 0; a < MAX_MTEX; a++) {
138                 MEM_SAFE_FREE(linestyle->mtex[a]);
139         }
140
141         /* is no lib link block, but linestyle extension */
142         if (linestyle->nodetree) {
143                 ntreeFreeTree(linestyle->nodetree);
144                 MEM_freeN(linestyle->nodetree);
145                 linestyle->nodetree = NULL;
146         }
147
148         while ((m = (LineStyleModifier *)linestyle->color_modifiers.first))
149                 BKE_linestyle_color_modifier_remove(linestyle, m);
150         while ((m = (LineStyleModifier *)linestyle->alpha_modifiers.first))
151                 BKE_linestyle_alpha_modifier_remove(linestyle, m);
152         while ((m = (LineStyleModifier *)linestyle->thickness_modifiers.first))
153                 BKE_linestyle_thickness_modifier_remove(linestyle, m);
154         while ((m = (LineStyleModifier *)linestyle->geometry_modifiers.first))
155                 BKE_linestyle_geometry_modifier_remove(linestyle, m);
156 }
157
158 /**
159  * Only copy internal data of Linestyle ID from source to already allocated/initialized destination.
160  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
161  *
162  * WARNING! This function will not handle ID user count!
163  *
164  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
165  */
166 void BKE_linestyle_copy_data(
167         struct Main *bmain, FreestyleLineStyle *linestyle_dst, const FreestyleLineStyle *linestyle_src, const int flag)
168 {
169         /* We never handle usercount here for own data. */
170         const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
171
172         for (int a = 0; a < MAX_MTEX; a++) {
173                 if (linestyle_src->mtex[a]) {
174                         linestyle_dst->mtex[a] = MEM_mallocN(sizeof(*linestyle_dst->mtex[a]), __func__);
175                         *linestyle_dst->mtex[a] = *linestyle_src->mtex[a];
176                 }
177         }
178
179         if (linestyle_src->nodetree) {
180                 /* Note: nodetree is *not* in bmain, however this specific case is handled at lower level
181                  *       (see BKE_libblock_copy_ex()). */
182                 BKE_id_copy_ex(bmain, (ID *)linestyle_src->nodetree, (ID **)&linestyle_dst->nodetree, flag, false);
183         }
184
185         LineStyleModifier *m;
186         BLI_listbase_clear(&linestyle_dst->color_modifiers);
187         for (m = (LineStyleModifier *)linestyle_src->color_modifiers.first; m; m = m->next) {
188                 BKE_linestyle_color_modifier_copy(linestyle_dst, m, flag_subdata);
189         }
190         BLI_listbase_clear(&linestyle_dst->alpha_modifiers);
191         for (m = (LineStyleModifier *)linestyle_src->alpha_modifiers.first; m; m = m->next) {
192                 BKE_linestyle_alpha_modifier_copy(linestyle_dst, m, flag_subdata);
193         }
194         BLI_listbase_clear(&linestyle_dst->thickness_modifiers);
195         for (m = (LineStyleModifier *)linestyle_src->thickness_modifiers.first; m; m = m->next) {
196                 BKE_linestyle_thickness_modifier_copy(linestyle_dst, m, flag_subdata);
197         }
198         BLI_listbase_clear(&linestyle_dst->geometry_modifiers);
199         for (m = (LineStyleModifier *)linestyle_src->geometry_modifiers.first; m; m = m->next) {
200                 BKE_linestyle_geometry_modifier_copy(linestyle_dst, m, flag_subdata);
201         }
202 }
203
204 FreestyleLineStyle *BKE_linestyle_copy(struct Main *bmain, const FreestyleLineStyle *linestyle)
205 {
206         FreestyleLineStyle *linestyle_copy;
207         BKE_id_copy_ex(bmain, &linestyle->id, (ID **)&linestyle_copy, 0, false);
208         return linestyle_copy;
209 }
210
211 void BKE_linestyle_make_local(struct Main *bmain, FreestyleLineStyle *linestyle, const bool lib_local)
212 {
213         BKE_id_make_local_generic(bmain, &linestyle->id, true, lib_local);
214 }
215
216 FreestyleLineStyle *BKE_linestyle_active_from_scene(Scene *scene)
217 {
218         SceneRenderLayer *actsrl = BLI_findlink(&scene->r.layers, scene->r.actlay);
219         FreestyleConfig *config = &actsrl->freestyleConfig;
220         FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(config);
221
222         if (lineset) {
223                 return lineset->linestyle;
224         }
225         return NULL;
226 }
227
228 static LineStyleModifier *new_modifier(const char *name, int type, size_t size)
229 {
230         LineStyleModifier *m;
231
232         if (!name) {
233                 name = modifier_name[type];
234         }
235         m = (LineStyleModifier *)MEM_callocN(size, "line style modifier");
236         m->type = type;
237         BLI_strncpy(m->name, name, sizeof(m->name));
238         m->influence = 1.0f;
239         m->flags = LS_MODIFIER_ENABLED | LS_MODIFIER_EXPANDED;
240
241         return m;
242 }
243
244 static void add_to_modifier_list(ListBase *lb, LineStyleModifier *m)
245 {
246         BLI_addtail(lb, (void *)m);
247         BLI_uniquename(lb, m, modifier_name[m->type], '.', offsetof(LineStyleModifier, name), sizeof(m->name));
248 }
249
250 static LineStyleModifier *alloc_color_modifier(const char *name, int type)
251 {
252         size_t size;
253
254         switch (type) {
255                 case LS_MODIFIER_ALONG_STROKE:
256                         size = sizeof(LineStyleColorModifier_AlongStroke);
257                         break;
258                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
259                         size = sizeof(LineStyleColorModifier_DistanceFromCamera);
260                         break;
261                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
262                         size = sizeof(LineStyleColorModifier_DistanceFromObject);
263                         break;
264                 case LS_MODIFIER_MATERIAL:
265                         size = sizeof(LineStyleColorModifier_Material);
266                         break;
267                 case LS_MODIFIER_TANGENT:
268                         size = sizeof(LineStyleColorModifier_Tangent);
269                         break;
270                 case LS_MODIFIER_NOISE:
271                         size = sizeof(LineStyleColorModifier_Noise);
272                         break;
273                 case LS_MODIFIER_CREASE_ANGLE:
274                         size = sizeof(LineStyleColorModifier_CreaseAngle);
275                         break;
276                 case LS_MODIFIER_CURVATURE_3D:
277                         size = sizeof(LineStyleColorModifier_Curvature_3D);
278                         break;
279                 default:
280                         return NULL; /* unknown modifier type */
281         }
282
283         return new_modifier(name, type, size);
284 }
285
286 LineStyleModifier *BKE_linestyle_color_modifier_add(FreestyleLineStyle *linestyle, const char *name, int type)
287 {
288         LineStyleModifier *m;
289
290         m = alloc_color_modifier(name, type);
291         if (UNLIKELY(m == NULL)) {
292                 return NULL;
293         }
294         m->blend = MA_RAMP_BLEND;
295
296         switch (type) {
297                 case LS_MODIFIER_ALONG_STROKE:
298                         ((LineStyleColorModifier_AlongStroke *)m)->color_ramp = BKE_colorband_add(true);
299                         break;
300                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
301                         ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp = BKE_colorband_add(true);
302                         ((LineStyleColorModifier_DistanceFromCamera *)m)->range_min = 0.0f;
303                         ((LineStyleColorModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
304                         break;
305                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
306                         ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
307                         ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp = BKE_colorband_add(true);
308                         ((LineStyleColorModifier_DistanceFromObject *)m)->range_min = 0.0f;
309                         ((LineStyleColorModifier_DistanceFromObject *)m)->range_max = 10000.0f;
310                         break;
311                 case LS_MODIFIER_MATERIAL:
312                         ((LineStyleColorModifier_Material *)m)->color_ramp = BKE_colorband_add(true);
313                         ((LineStyleColorModifier_Material *)m)->mat_attr = LS_MODIFIER_MATERIAL_LINE;
314                         break;
315                 case LS_MODIFIER_TANGENT:
316                         ((LineStyleColorModifier_Tangent *)m)->color_ramp = BKE_colorband_add(true);
317                         break;
318                 case LS_MODIFIER_NOISE:
319                         ((LineStyleColorModifier_Noise *)m)->color_ramp = BKE_colorband_add(true);
320                         ((LineStyleColorModifier_Noise *)m)->amplitude = 10.0f;
321                         ((LineStyleColorModifier_Noise *)m)->period = 10.0f;
322                         ((LineStyleColorModifier_Noise *)m)->seed = 512;
323                         break;
324                 case LS_MODIFIER_CREASE_ANGLE:
325                         ((LineStyleColorModifier_CreaseAngle *)m)->color_ramp = BKE_colorband_add(true);
326                         ((LineStyleColorModifier_CreaseAngle *)m)->min_angle = 0.0f;
327                         ((LineStyleColorModifier_CreaseAngle *)m)->max_angle = DEG2RADF(180.0f);
328                         break;
329                 case LS_MODIFIER_CURVATURE_3D:
330                         ((LineStyleColorModifier_Curvature_3D *)m)->color_ramp = BKE_colorband_add(true);
331                         ((LineStyleColorModifier_Curvature_3D *)m)->min_curvature = 0.0f;
332                         ((LineStyleColorModifier_Curvature_3D *)m)->max_curvature = 0.5f;
333                         break;
334                 default:
335                         return NULL; /* unknown modifier type */
336         }
337         add_to_modifier_list(&linestyle->color_modifiers, m);
338
339         return m;
340 }
341
342 LineStyleModifier *BKE_linestyle_color_modifier_copy(
343         FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int flag)
344 {
345         LineStyleModifier *new_m;
346
347         new_m = alloc_color_modifier(m->name, m->type);
348         if (UNLIKELY(new_m == NULL)) {
349                 return NULL;
350         }
351         new_m->influence = m->influence;
352         new_m->flags = m->flags;
353         new_m->blend = m->blend;
354
355         switch (m->type) {
356                 case LS_MODIFIER_ALONG_STROKE:
357                 {
358                         LineStyleColorModifier_AlongStroke *p = (LineStyleColorModifier_AlongStroke *)m;
359                         LineStyleColorModifier_AlongStroke *q = (LineStyleColorModifier_AlongStroke *)new_m;
360                         q->color_ramp = MEM_dupallocN(p->color_ramp);
361                         break;
362                 }
363                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
364                 {
365                         LineStyleColorModifier_DistanceFromCamera *p = (LineStyleColorModifier_DistanceFromCamera *)m;
366                         LineStyleColorModifier_DistanceFromCamera *q = (LineStyleColorModifier_DistanceFromCamera *)new_m;
367                         q->color_ramp = MEM_dupallocN(p->color_ramp);
368                         q->range_min = p->range_min;
369                         q->range_max = p->range_max;
370                         break;
371                 }
372                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
373                 {
374                         LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)m;
375                         LineStyleColorModifier_DistanceFromObject *q = (LineStyleColorModifier_DistanceFromObject *)new_m;
376                         q->target = p->target;
377                         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
378                                 id_us_plus((ID *)q->target);
379                         }
380                         q->color_ramp = MEM_dupallocN(p->color_ramp);
381                         q->range_min = p->range_min;
382                         q->range_max = p->range_max;
383                         break;
384                 }
385                 case LS_MODIFIER_MATERIAL:
386                 {
387                         LineStyleColorModifier_Material *p = (LineStyleColorModifier_Material *)m;
388                         LineStyleColorModifier_Material *q = (LineStyleColorModifier_Material *)new_m;
389                         q->color_ramp = MEM_dupallocN(p->color_ramp);
390                         q->flags = p->flags;
391                         q->mat_attr = p->mat_attr;
392                         break;
393                 }
394                 case LS_MODIFIER_TANGENT:
395                 {
396                         LineStyleColorModifier_Tangent *p = (LineStyleColorModifier_Tangent *)m;
397                         LineStyleColorModifier_Tangent *q = (LineStyleColorModifier_Tangent *)new_m;
398                         q->color_ramp = MEM_dupallocN(p->color_ramp);
399                         break;
400                 }
401                 case LS_MODIFIER_NOISE:
402                 {
403                         LineStyleColorModifier_Noise *p = (LineStyleColorModifier_Noise *)m;
404                         LineStyleColorModifier_Noise *q = (LineStyleColorModifier_Noise *)new_m;
405                         q->color_ramp = MEM_dupallocN(p->color_ramp);
406                         q->amplitude = p->amplitude;
407                         q->period = p->period;
408                         q->seed = p->seed;
409                         break;
410                 }
411                 case LS_MODIFIER_CREASE_ANGLE:
412                 {
413                         LineStyleColorModifier_CreaseAngle *p = (LineStyleColorModifier_CreaseAngle *)m;
414                         LineStyleColorModifier_CreaseAngle *q = (LineStyleColorModifier_CreaseAngle *)new_m;
415                         q->color_ramp = MEM_dupallocN(p->color_ramp);
416                         q->min_angle = p->min_angle;
417                         q->max_angle = p->max_angle;
418                         break;
419                 }
420                 case LS_MODIFIER_CURVATURE_3D:
421                 {
422                         LineStyleColorModifier_Curvature_3D *p = (LineStyleColorModifier_Curvature_3D *)m;
423                         LineStyleColorModifier_Curvature_3D *q = (LineStyleColorModifier_Curvature_3D *)new_m;
424                         q->color_ramp = MEM_dupallocN(p->color_ramp);
425                         q->min_curvature = p->min_curvature;
426                         q->max_curvature = p->max_curvature;
427                         break;
428                 }
429                 default:
430                         return NULL; /* unknown modifier type */
431         }
432         add_to_modifier_list(&linestyle->color_modifiers, new_m);
433
434         return new_m;
435 }
436
437 int BKE_linestyle_color_modifier_remove(FreestyleLineStyle *linestyle, LineStyleModifier *m)
438 {
439         if (BLI_findindex(&linestyle->color_modifiers, m) == -1)
440                 return -1;
441         switch (m->type) {
442                 case LS_MODIFIER_ALONG_STROKE:
443                         MEM_freeN(((LineStyleColorModifier_AlongStroke *)m)->color_ramp);
444                         break;
445                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
446                         MEM_freeN(((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp);
447                         break;
448                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
449                         MEM_freeN(((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp);
450                         break;
451                 case LS_MODIFIER_MATERIAL:
452                         MEM_freeN(((LineStyleColorModifier_Material *)m)->color_ramp);
453                         break;
454                 case LS_MODIFIER_TANGENT:
455                         MEM_freeN(((LineStyleColorModifier_Tangent *)m)->color_ramp);
456                         break;
457                 case LS_MODIFIER_NOISE:
458                         MEM_freeN(((LineStyleColorModifier_Noise *)m)->color_ramp);
459                         break;
460                 case LS_MODIFIER_CREASE_ANGLE:
461                         MEM_freeN(((LineStyleColorModifier_CreaseAngle *)m)->color_ramp);
462                         break;
463                 case LS_MODIFIER_CURVATURE_3D:
464                         MEM_freeN(((LineStyleColorModifier_Curvature_3D *)m)->color_ramp);
465                         break;
466         }
467         BLI_freelinkN(&linestyle->color_modifiers, m);
468         return 0;
469 }
470
471 static LineStyleModifier *alloc_alpha_modifier(const char *name, int type)
472 {
473         size_t size;
474
475         switch (type) {
476                 case LS_MODIFIER_ALONG_STROKE:
477                         size = sizeof(LineStyleAlphaModifier_AlongStroke);
478                         break;
479                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
480                         size = sizeof(LineStyleAlphaModifier_DistanceFromCamera);
481                         break;
482                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
483                         size = sizeof(LineStyleAlphaModifier_DistanceFromObject);
484                         break;
485                 case LS_MODIFIER_MATERIAL:
486                         size = sizeof(LineStyleAlphaModifier_Material);
487                         break;
488                 case LS_MODIFIER_TANGENT:
489                         size = sizeof(LineStyleAlphaModifier_Tangent);
490                         break;
491                 case LS_MODIFIER_NOISE:
492                         size = sizeof(LineStyleAlphaModifier_Noise);
493                         break;
494                 case LS_MODIFIER_CREASE_ANGLE:
495                         size = sizeof(LineStyleAlphaModifier_CreaseAngle);
496                         break;
497                 case LS_MODIFIER_CURVATURE_3D:
498                         size = sizeof(LineStyleAlphaModifier_Curvature_3D);
499                         break;
500                 default:
501                         return NULL; /* unknown modifier type */
502         }
503         return new_modifier(name, type, size);
504 }
505
506 LineStyleModifier *BKE_linestyle_alpha_modifier_add(FreestyleLineStyle *linestyle, const char *name, int type)
507 {
508         LineStyleModifier *m;
509
510         m = alloc_alpha_modifier(name, type);
511         m->blend = LS_VALUE_BLEND;
512
513         switch (type) {
514                 case LS_MODIFIER_ALONG_STROKE:
515                 {
516                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
517                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
518                         break;
519                 }
520                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
521                 {
522                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_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 = 10000.0f;
526                         break;
527                 }
528                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
529                 {
530                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
531                         p->target = NULL;
532                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
533                         p->range_min = 0.0f;
534                         p->range_max = 10000.0f;
535                         break;
536                 }
537                 case LS_MODIFIER_MATERIAL:
538                 {
539                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
540                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
541                         p->mat_attr = LS_MODIFIER_MATERIAL_LINE_A;
542                         break;
543                 }
544                 case LS_MODIFIER_TANGENT:
545                 {
546                         LineStyleAlphaModifier_Tangent *p = (LineStyleAlphaModifier_Tangent *)m;
547                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
548                         break;
549                 }
550                 case LS_MODIFIER_NOISE:
551                 {
552                         LineStyleAlphaModifier_Noise *p = (LineStyleAlphaModifier_Noise *)m;
553                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
554                         ((LineStyleAlphaModifier_Noise *)m)->amplitude = 10.0f;
555                         ((LineStyleAlphaModifier_Noise *)m)->period = 10.0f;
556                         ((LineStyleAlphaModifier_Noise *)m)->seed = 512;
557                         break;
558                 }
559                 case LS_MODIFIER_CREASE_ANGLE:
560                 {
561                         LineStyleAlphaModifier_CreaseAngle *p = (LineStyleAlphaModifier_CreaseAngle *)m;
562                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
563                         ((LineStyleAlphaModifier_CreaseAngle *)m)->min_angle = 0.0f;
564                         ((LineStyleAlphaModifier_CreaseAngle *)m)->max_angle = DEG2RADF(180.0f);
565                         break;
566                 }
567                 case LS_MODIFIER_CURVATURE_3D:
568                 {
569                         LineStyleAlphaModifier_Curvature_3D *p = (LineStyleAlphaModifier_Curvature_3D *)m;
570                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
571                         ((LineStyleAlphaModifier_Curvature_3D *)m)->min_curvature = 0.0f;
572                         ((LineStyleAlphaModifier_Curvature_3D *)m)->max_curvature = 0.5f;
573                         break;
574                 }
575                 default:
576                         return NULL; /* unknown modifier type */
577         }
578         add_to_modifier_list(&linestyle->alpha_modifiers, m);
579
580         return m;
581 }
582
583 LineStyleModifier *BKE_linestyle_alpha_modifier_copy(
584         FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int UNUSED(flag))
585 {
586         LineStyleModifier *new_m;
587
588         new_m = alloc_alpha_modifier(m->name, m->type);
589         new_m->influence = m->influence;
590         new_m->flags = m->flags;
591         new_m->blend = m->blend;
592
593         switch (m->type) {
594                 case LS_MODIFIER_ALONG_STROKE:
595                 {
596                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
597                         LineStyleAlphaModifier_AlongStroke *q = (LineStyleAlphaModifier_AlongStroke *)new_m;
598                         q->curve = curvemapping_copy(p->curve);
599                         q->flags = p->flags;
600                         break;
601                 }
602                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
603                 {
604                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
605                         LineStyleAlphaModifier_DistanceFromCamera *q = (LineStyleAlphaModifier_DistanceFromCamera *)new_m;
606                         q->curve = curvemapping_copy(p->curve);
607                         q->flags = p->flags;
608                         q->range_min = p->range_min;
609                         q->range_max = p->range_max;
610                         break;
611                 }
612                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
613                 {
614                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
615                         LineStyleAlphaModifier_DistanceFromObject *q = (LineStyleAlphaModifier_DistanceFromObject *)new_m;
616                         if (p->target)
617                                 id_us_plus(&p->target->id);
618                         q->target = p->target;
619                         q->curve = curvemapping_copy(p->curve);
620                         q->flags = p->flags;
621                         q->range_min = p->range_min;
622                         q->range_max = p->range_max;
623                         break;
624                 }
625                 case LS_MODIFIER_MATERIAL:
626                 {
627                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
628                         LineStyleAlphaModifier_Material *q = (LineStyleAlphaModifier_Material *)new_m;
629                         q->curve = curvemapping_copy(p->curve);
630                         q->flags = p->flags;
631                         q->mat_attr = p->mat_attr;
632                         break;
633                 }
634                 case LS_MODIFIER_TANGENT:
635                 {
636                         LineStyleAlphaModifier_Tangent *p = (LineStyleAlphaModifier_Tangent *)m;
637                         LineStyleAlphaModifier_Tangent *q = (LineStyleAlphaModifier_Tangent *)new_m;
638                         q->curve = curvemapping_copy(p->curve);
639                         q->flags = p->flags;
640                         break;
641                 }
642                 case LS_MODIFIER_NOISE:
643                 {
644                         LineStyleAlphaModifier_Noise *p = (LineStyleAlphaModifier_Noise *)m;
645                         LineStyleAlphaModifier_Noise *q = (LineStyleAlphaModifier_Noise *)new_m;
646                         q->curve = curvemapping_copy(p->curve);
647                         q->flags = p->flags;
648                         q->amplitude = p->amplitude;
649                         q->period = p->period;
650                         q->seed = p->seed;
651                         break;
652                 }
653                 case LS_MODIFIER_CREASE_ANGLE:
654                 {
655                         LineStyleAlphaModifier_CreaseAngle *p = (LineStyleAlphaModifier_CreaseAngle *)m;
656                         LineStyleAlphaModifier_CreaseAngle *q = (LineStyleAlphaModifier_CreaseAngle *)new_m;
657                         q->curve = curvemapping_copy(p->curve);
658                         q->flags = p->flags;
659                         q->min_angle = p->min_angle;
660                         q->max_angle = p->max_angle;
661                         break;
662                 }
663                 case LS_MODIFIER_CURVATURE_3D:
664                 {
665                         LineStyleAlphaModifier_Curvature_3D *p = (LineStyleAlphaModifier_Curvature_3D *)m;
666                         LineStyleAlphaModifier_Curvature_3D *q = (LineStyleAlphaModifier_Curvature_3D *)new_m;
667                         q->curve = curvemapping_copy(p->curve);
668                         q->flags = p->flags;
669                         q->min_curvature = p->min_curvature;
670                         q->max_curvature = p->max_curvature;
671                         break;
672                 }
673                 default:
674                         return NULL; /* unknown modifier type */
675         }
676         add_to_modifier_list(&linestyle->alpha_modifiers, new_m);
677
678         return new_m;
679 }
680
681 int BKE_linestyle_alpha_modifier_remove(FreestyleLineStyle *linestyle, LineStyleModifier *m)
682 {
683         if (BLI_findindex(&linestyle->alpha_modifiers, m) == -1)
684                 return -1;
685         switch (m->type) {
686                 case LS_MODIFIER_ALONG_STROKE:
687                         curvemapping_free(((LineStyleAlphaModifier_AlongStroke *)m)->curve);
688                         break;
689                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
690                         curvemapping_free(((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve);
691                         break;
692                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
693                         curvemapping_free(((LineStyleAlphaModifier_DistanceFromObject *)m)->curve);
694                         break;
695                 case LS_MODIFIER_MATERIAL:
696                         curvemapping_free(((LineStyleAlphaModifier_Material *)m)->curve);
697                         break;
698                 case LS_MODIFIER_TANGENT:
699                         curvemapping_free(((LineStyleAlphaModifier_Tangent *)m)->curve);
700                         break;
701                 case LS_MODIFIER_NOISE:
702                         curvemapping_free(((LineStyleAlphaModifier_Noise *)m)->curve);
703                         break;
704                 case LS_MODIFIER_CREASE_ANGLE:
705                         curvemapping_free(((LineStyleAlphaModifier_CreaseAngle *)m)->curve);
706                         break;
707                 case LS_MODIFIER_CURVATURE_3D:
708                         curvemapping_free(((LineStyleAlphaModifier_Curvature_3D *)m)->curve);
709                         break;
710         }
711         BLI_freelinkN(&linestyle->alpha_modifiers, m);
712         return 0;
713 }
714
715 static LineStyleModifier *alloc_thickness_modifier(const char *name, int type)
716 {
717         size_t size;
718
719         switch (type) {
720                 case LS_MODIFIER_ALONG_STROKE:
721                         size = sizeof(LineStyleThicknessModifier_AlongStroke);
722                         break;
723                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
724                         size = sizeof(LineStyleThicknessModifier_DistanceFromCamera);
725                         break;
726                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
727                         size = sizeof(LineStyleThicknessModifier_DistanceFromObject);
728                         break;
729                 case LS_MODIFIER_MATERIAL:
730                         size = sizeof(LineStyleThicknessModifier_Material);
731                         break;
732                 case LS_MODIFIER_CALLIGRAPHY:
733                         size = sizeof(LineStyleThicknessModifier_Calligraphy);
734                         break;
735                 case LS_MODIFIER_TANGENT:
736                         size = sizeof(LineStyleThicknessModifier_Tangent);
737                         break;
738                 case LS_MODIFIER_NOISE:
739                         size = sizeof(LineStyleThicknessModifier_Noise);
740                         break;
741                 case LS_MODIFIER_CREASE_ANGLE:
742                         size = sizeof(LineStyleThicknessModifier_CreaseAngle);
743                         break;
744                 case LS_MODIFIER_CURVATURE_3D:
745                         size = sizeof(LineStyleThicknessModifier_Curvature_3D);
746                         break;
747                 default:
748                         return NULL; /* unknown modifier type */
749         }
750
751         return new_modifier(name, type, size);
752 }
753
754 LineStyleModifier *BKE_linestyle_thickness_modifier_add(FreestyleLineStyle *linestyle, const char *name, int type)
755 {
756         LineStyleModifier *m;
757
758         m = alloc_thickness_modifier(name, type);
759         m->blend = LS_VALUE_BLEND;
760
761         switch (type) {
762                 case LS_MODIFIER_ALONG_STROKE:
763                 {
764                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
765                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
766                         p->value_min = 0.0f;
767                         p->value_max = 1.0f;
768                         break;
769                 }
770                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
771                 {
772                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
773                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
774                         p->range_min = 0.0f;
775                         p->range_max = 1000.0f;
776                         p->value_min = 0.0f;
777                         p->value_max = 1.0f;
778                         break;
779                 }
780                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
781                 {
782                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
783                         p->target = NULL;
784                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
785                         p->range_min = 0.0f;
786                         p->range_max = 1000.0f;
787                         p->value_min = 0.0f;
788                         p->value_max = 1.0f;
789                         break;
790                 }
791                 case LS_MODIFIER_MATERIAL:
792                 {
793                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
794                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
795                         p->mat_attr = LS_MODIFIER_MATERIAL_LINE;
796                         p->value_min = 0.0f;
797                         p->value_max = 1.0f;
798                         break;
799                 }
800                 case LS_MODIFIER_CALLIGRAPHY:
801                 {
802                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
803                         p->min_thickness = 1.0f;
804                         p->max_thickness = 10.0f;
805                         p->orientation = DEG2RADF(60.0f);
806                         break;
807                 }
808                 case LS_MODIFIER_TANGENT:
809                 {
810                         LineStyleThicknessModifier_Tangent *p = (LineStyleThicknessModifier_Tangent *)m;
811                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
812                         p->min_thickness = 1.0f;
813                         p->max_thickness = 10.0f;
814                         break;
815                 }
816                 case LS_MODIFIER_NOISE:
817                 {
818                         LineStyleThicknessModifier_Noise *p = (LineStyleThicknessModifier_Noise *)m;
819                         p->period = 10.0f;
820                         p->amplitude = 10.0f;
821                         p->seed = 512;
822                         p->flags = LS_THICKNESS_ASYMMETRIC;
823                         break;
824                 }
825                 case LS_MODIFIER_CREASE_ANGLE:
826                 {
827                         LineStyleThicknessModifier_CreaseAngle *p = (LineStyleThicknessModifier_CreaseAngle *)m;
828                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
829                         p->min_angle = 0.0f;
830                         p->max_angle = DEG2RADF(180.0f);
831                         p->min_thickness = 1.0f;
832                         p->max_thickness = 10.0f;
833                         break;
834                 }
835                 case LS_MODIFIER_CURVATURE_3D:
836                 {
837                         LineStyleThicknessModifier_Curvature_3D *p = (LineStyleThicknessModifier_Curvature_3D *)m;
838                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
839                         p->min_curvature = 0.0f;
840                         p->max_curvature = 0.5f;
841                         p->min_thickness = 1.0f;
842                         p->max_thickness = 10.0f;
843                         break;
844                 }
845                 default:
846                         return NULL; /* unknown modifier type */
847         }
848         add_to_modifier_list(&linestyle->thickness_modifiers, m);
849
850         return m;
851 }
852
853 LineStyleModifier *BKE_linestyle_thickness_modifier_copy(
854         FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int flag)
855 {
856         LineStyleModifier *new_m;
857
858         new_m = alloc_thickness_modifier(m->name, m->type);
859         if (!new_m)
860                 return NULL;
861         new_m->influence = m->influence;
862         new_m->flags = m->flags;
863         new_m->blend = m->blend;
864
865         switch (m->type) {
866                 case LS_MODIFIER_ALONG_STROKE:
867                 {
868                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
869                         LineStyleThicknessModifier_AlongStroke *q = (LineStyleThicknessModifier_AlongStroke *)new_m;
870                         q->curve = curvemapping_copy(p->curve);
871                         q->flags = p->flags;
872                         q->value_min = p->value_min;
873                         q->value_max = p->value_max;
874                         break;
875                 }
876                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
877                 {
878                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
879                         LineStyleThicknessModifier_DistanceFromCamera *q = (LineStyleThicknessModifier_DistanceFromCamera *)new_m;
880                         q->curve = curvemapping_copy(p->curve);
881                         q->flags = p->flags;
882                         q->range_min = p->range_min;
883                         q->range_max = p->range_max;
884                         q->value_min = p->value_min;
885                         q->value_max = p->value_max;
886                         break;
887                 }
888                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
889                 {
890                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
891                         LineStyleThicknessModifier_DistanceFromObject *q = (LineStyleThicknessModifier_DistanceFromObject *)new_m;
892                         q->target = p->target;
893                         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
894                                 id_us_plus((ID *)q->target);
895                         }
896                         q->curve = curvemapping_copy(p->curve);
897                         q->flags = p->flags;
898                         q->range_min = p->range_min;
899                         q->range_max = p->range_max;
900                         q->value_min = p->value_min;
901                         q->value_max = p->value_max;
902                         break;
903                 }
904                 case LS_MODIFIER_MATERIAL:
905                 {
906                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
907                         LineStyleThicknessModifier_Material *q = (LineStyleThicknessModifier_Material *)new_m;
908                         q->curve = curvemapping_copy(p->curve);
909                         q->flags = p->flags;
910                         q->mat_attr = p->mat_attr;
911                         q->value_min = p->value_min;
912                         q->value_max = p->value_max;
913                         break;
914                 }
915                 case LS_MODIFIER_CALLIGRAPHY:
916                 {
917                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
918                         LineStyleThicknessModifier_Calligraphy *q = (LineStyleThicknessModifier_Calligraphy *)new_m;
919                         q->min_thickness = p->min_thickness;
920                         q->max_thickness = p->max_thickness;
921                         q->orientation = p->orientation;
922                         break;
923                 }
924                 case LS_MODIFIER_TANGENT:
925                 {
926                         LineStyleThicknessModifier_Tangent *p = (LineStyleThicknessModifier_Tangent *)m;
927                         LineStyleThicknessModifier_Tangent *q = (LineStyleThicknessModifier_Tangent *)new_m;
928                         q->curve = curvemapping_copy(p->curve);
929                         q->flags = p->flags;
930                         q->min_thickness = p->min_thickness;
931                         q->max_thickness = p->max_thickness;
932                         break;
933                 }
934                 case LS_MODIFIER_NOISE:
935                 {
936                         LineStyleThicknessModifier_Noise *p = (LineStyleThicknessModifier_Noise *)m;
937                         LineStyleThicknessModifier_Noise *q = (LineStyleThicknessModifier_Noise *)new_m;
938                         q->amplitude = p->amplitude;
939                         q->period = p->period;
940                         q->seed = p->seed;
941                         q->flags = p->flags;
942                         break;
943                 }
944                 case LS_MODIFIER_CURVATURE_3D:
945                 {
946                         LineStyleThicknessModifier_Curvature_3D *p = (LineStyleThicknessModifier_Curvature_3D *)m;
947                         LineStyleThicknessModifier_Curvature_3D *q = (LineStyleThicknessModifier_Curvature_3D *)new_m;
948                         q->curve = curvemapping_copy(p->curve);
949                         q->flags = p->flags;
950                         q->min_curvature = p->min_curvature;
951                         q->max_curvature = p->max_curvature;
952                         q->min_thickness = p->min_thickness;
953                         q->max_thickness = p->max_thickness;
954                         break;
955                 }
956                 case LS_MODIFIER_CREASE_ANGLE:
957                 {
958                         LineStyleThicknessModifier_CreaseAngle *p = (LineStyleThicknessModifier_CreaseAngle *)m;
959                         LineStyleThicknessModifier_CreaseAngle *q = (LineStyleThicknessModifier_CreaseAngle *)new_m;
960                         q->curve = curvemapping_copy(p->curve);
961                         q->flags = p->flags;
962                         q->min_angle = p->min_angle;
963                         q->max_angle = p->max_angle;
964                         q->min_thickness = p->min_thickness;
965                         q->max_thickness = p->max_thickness;
966                         break;
967                 }
968                 default:
969                         return NULL; /* unknown modifier type */
970         }
971         add_to_modifier_list(&linestyle->thickness_modifiers, new_m);
972
973         return new_m;
974 }
975
976 int BKE_linestyle_thickness_modifier_remove(FreestyleLineStyle *linestyle, LineStyleModifier *m)
977 {
978         if (BLI_findindex(&linestyle->thickness_modifiers, m) == -1)
979                 return -1;
980         switch (m->type) {
981                 case LS_MODIFIER_ALONG_STROKE:
982                         curvemapping_free(((LineStyleThicknessModifier_AlongStroke *)m)->curve);
983                         break;
984                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
985                         curvemapping_free(((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve);
986                         break;
987                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
988                         curvemapping_free(((LineStyleThicknessModifier_DistanceFromObject *)m)->curve);
989                         break;
990                 case LS_MODIFIER_MATERIAL:
991                         curvemapping_free(((LineStyleThicknessModifier_Material *)m)->curve);
992                         break;
993                 case LS_MODIFIER_CALLIGRAPHY:
994                         break;
995                 case LS_MODIFIER_TANGENT:
996                         curvemapping_free(((LineStyleThicknessModifier_Tangent *)m)->curve);
997                         break;
998                 case LS_MODIFIER_NOISE:
999                         break;
1000                 case LS_MODIFIER_CREASE_ANGLE:
1001                         break;
1002                 case LS_MODIFIER_CURVATURE_3D:
1003                         break;
1004         }
1005         BLI_freelinkN(&linestyle->thickness_modifiers, m);
1006         return 0;
1007 }
1008
1009 static LineStyleModifier *alloc_geometry_modifier(const char *name, int type)
1010 {
1011         size_t size;
1012
1013         switch (type) {
1014                 case LS_MODIFIER_SAMPLING:
1015                         size = sizeof(LineStyleGeometryModifier_Sampling);
1016                         break;
1017                 case LS_MODIFIER_BEZIER_CURVE:
1018                         size = sizeof(LineStyleGeometryModifier_BezierCurve);
1019                         break;
1020                 case LS_MODIFIER_SINUS_DISPLACEMENT:
1021                         size = sizeof(LineStyleGeometryModifier_SinusDisplacement);
1022                         break;
1023                 case LS_MODIFIER_SPATIAL_NOISE:
1024                         size = sizeof(LineStyleGeometryModifier_SpatialNoise);
1025                         break;
1026                 case LS_MODIFIER_PERLIN_NOISE_1D:
1027                         size = sizeof(LineStyleGeometryModifier_PerlinNoise1D);
1028                         break;
1029                 case LS_MODIFIER_PERLIN_NOISE_2D:
1030                         size = sizeof(LineStyleGeometryModifier_PerlinNoise2D);
1031                         break;
1032                 case LS_MODIFIER_BACKBONE_STRETCHER:
1033                         size = sizeof(LineStyleGeometryModifier_BackboneStretcher);
1034                         break;
1035                 case LS_MODIFIER_TIP_REMOVER:
1036                         size = sizeof(LineStyleGeometryModifier_TipRemover);
1037                         break;
1038                 case LS_MODIFIER_POLYGONIZATION:
1039                         size = sizeof(LineStyleGeometryModifier_Polygonalization);
1040                         break;
1041                 case LS_MODIFIER_GUIDING_LINES:
1042                         size = sizeof(LineStyleGeometryModifier_GuidingLines);
1043                         break;
1044                 case LS_MODIFIER_BLUEPRINT:
1045                         size = sizeof(LineStyleGeometryModifier_Blueprint);
1046                         break;
1047                 case LS_MODIFIER_2D_OFFSET:
1048                         size = sizeof(LineStyleGeometryModifier_2DOffset);
1049                         break;
1050                 case LS_MODIFIER_2D_TRANSFORM:
1051                         size = sizeof(LineStyleGeometryModifier_2DTransform);
1052                         break;
1053                 case LS_MODIFIER_SIMPLIFICATION:
1054                         size = sizeof(LineStyleGeometryModifier_Simplification);
1055                         break;
1056                 default:
1057                         return NULL; /* unknown modifier type */
1058         }
1059
1060         return new_modifier(name, type, size);
1061 }
1062
1063 LineStyleModifier *BKE_linestyle_geometry_modifier_add(FreestyleLineStyle *linestyle, const char *name, int type)
1064 {
1065         LineStyleModifier *m;
1066
1067         m = alloc_geometry_modifier(name, type);
1068
1069         switch (type) {
1070                 case LS_MODIFIER_SAMPLING:
1071                 {
1072                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
1073                         p->sampling = 10.0f;
1074                         break;
1075                 }
1076                 case LS_MODIFIER_BEZIER_CURVE:
1077                 {
1078                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
1079                         p->error = 10.0f;
1080                         break;
1081                 }
1082                 case LS_MODIFIER_SINUS_DISPLACEMENT:
1083                 {
1084                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
1085                         p->wavelength = 20.0f;
1086                         p->amplitude = 5.0f;
1087                         p->phase = 0.0f;
1088                         break;
1089                 }
1090                 case LS_MODIFIER_SPATIAL_NOISE:
1091                 {
1092                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
1093                         p->amplitude = 5.0f;
1094                         p->scale = 20.0f;
1095                         p->octaves = 4;
1096                         p->flags = LS_MODIFIER_SPATIAL_NOISE_SMOOTH | LS_MODIFIER_SPATIAL_NOISE_PURERANDOM;
1097                         break;
1098                 }
1099                 case LS_MODIFIER_PERLIN_NOISE_1D:
1100                 {
1101                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
1102                         p->frequency = 10.0f;
1103                         p->amplitude = 10.0f;
1104                         p->octaves = 4;
1105                         p->angle = DEG2RADF(45.0f);
1106                         break;
1107                 }
1108                 case LS_MODIFIER_PERLIN_NOISE_2D:
1109                 {
1110                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
1111                         p->frequency = 10.0f;
1112                         p->amplitude = 10.0f;
1113                         p->octaves = 4;
1114                         p->angle = DEG2RADF(45.0f);
1115                         break;
1116                 }
1117                 case LS_MODIFIER_BACKBONE_STRETCHER:
1118                 {
1119                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
1120                         p->backbone_length = 10.0f;
1121                         break;
1122                 }
1123                 case LS_MODIFIER_TIP_REMOVER:
1124                 {
1125                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
1126                         p->tip_length = 10.0f;
1127                         break;
1128                 }
1129                 case LS_MODIFIER_POLYGONIZATION:
1130                 {
1131                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
1132                         p->error = 10.0f;
1133                         break;
1134                 }
1135                 case LS_MODIFIER_GUIDING_LINES:
1136                 {
1137                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
1138                         p->offset = 0.0f;
1139                         break;
1140                 }
1141                 case LS_MODIFIER_BLUEPRINT:
1142                 {
1143                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
1144                         p->flags = LS_MODIFIER_BLUEPRINT_CIRCLES;
1145                         p->rounds = 1;
1146                         p->backbone_length = 10.0f;
1147                         p->random_radius = 3;
1148                         p->random_center = 5;
1149                         p->random_backbone = 5;
1150                         break;
1151                 }
1152                 case LS_MODIFIER_2D_OFFSET:
1153                 {
1154                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
1155                         p->start = 0.0f;
1156                         p->end = 0.0f;
1157                         p->x = 0.0f;
1158                         p->y = 0.0f;
1159                         break;
1160                 }
1161                 case LS_MODIFIER_2D_TRANSFORM:
1162                 {
1163                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
1164                         p->pivot = LS_MODIFIER_2D_TRANSFORM_PIVOT_CENTER;
1165                         p->scale_x = 1.0f;
1166                         p->scale_y = 1.0f;
1167                         p->angle = DEG2RADF(0.0f);
1168                         p->pivot_u = 0.5f;
1169                         p->pivot_x = 0.0f;
1170                         p->pivot_y = 0.0f;
1171                         break;
1172                 }
1173                 case LS_MODIFIER_SIMPLIFICATION:
1174                 {
1175                         LineStyleGeometryModifier_Simplification *p = (LineStyleGeometryModifier_Simplification *)m;
1176                         p->tolerance = 0.1f;
1177                         break;
1178                 }
1179                 default:
1180                         return NULL; /* unknown modifier type */
1181         }
1182         add_to_modifier_list(&linestyle->geometry_modifiers, m);
1183
1184         return m;
1185 }
1186
1187 LineStyleModifier *BKE_linestyle_geometry_modifier_copy(
1188         FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int UNUSED(flag))
1189 {
1190         LineStyleModifier *new_m;
1191
1192         new_m = alloc_geometry_modifier(m->name, m->type);
1193         new_m->flags = m->flags;
1194
1195         switch (m->type) {
1196                 case LS_MODIFIER_SAMPLING:
1197                 {
1198                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
1199                         LineStyleGeometryModifier_Sampling *q = (LineStyleGeometryModifier_Sampling *)new_m;
1200                         q->sampling = p->sampling;
1201                         break;
1202                 }
1203                 case LS_MODIFIER_BEZIER_CURVE:
1204                 {
1205                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
1206                         LineStyleGeometryModifier_BezierCurve *q = (LineStyleGeometryModifier_BezierCurve *)new_m;
1207                         q->error = p->error;
1208                         break;
1209                 }
1210                 case LS_MODIFIER_SINUS_DISPLACEMENT:
1211                 {
1212                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
1213                         LineStyleGeometryModifier_SinusDisplacement *q = (LineStyleGeometryModifier_SinusDisplacement *)new_m;
1214                         q->wavelength = p->wavelength;
1215                         q->amplitude = p->amplitude;
1216                         q->phase = p->phase;
1217                         break;
1218                 }
1219                 case LS_MODIFIER_SPATIAL_NOISE:
1220                 {
1221                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
1222                         LineStyleGeometryModifier_SpatialNoise *q = (LineStyleGeometryModifier_SpatialNoise *)new_m;
1223                         q->amplitude = p->amplitude;
1224                         q->scale = p->scale;
1225                         q->octaves = p->octaves;
1226                         q->flags = p->flags;
1227                         break;
1228                 }
1229                 case LS_MODIFIER_PERLIN_NOISE_1D:
1230                 {
1231                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
1232                         LineStyleGeometryModifier_PerlinNoise1D *q = (LineStyleGeometryModifier_PerlinNoise1D *)new_m;
1233                         q->frequency = p->frequency;
1234                         q->amplitude = p->amplitude;
1235                         q->angle = p->angle;
1236                         q->octaves = p->octaves;
1237                         q->seed = p->seed;
1238                         break;
1239                 }
1240                 case LS_MODIFIER_PERLIN_NOISE_2D:
1241                 {
1242                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
1243                         LineStyleGeometryModifier_PerlinNoise2D *q = (LineStyleGeometryModifier_PerlinNoise2D *)new_m;
1244                         q->frequency = p->frequency;
1245                         q->amplitude = p->amplitude;
1246                         q->angle = p->angle;
1247                         q->octaves = p->octaves;
1248                         q->seed = p->seed;
1249                         break;
1250                 }
1251                 case LS_MODIFIER_BACKBONE_STRETCHER:
1252                 {
1253                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
1254                         LineStyleGeometryModifier_BackboneStretcher *q = (LineStyleGeometryModifier_BackboneStretcher *)new_m;
1255                         q->backbone_length = p->backbone_length;
1256                         break;
1257                 }
1258                 case LS_MODIFIER_TIP_REMOVER:
1259                 {
1260                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
1261                         LineStyleGeometryModifier_TipRemover *q = (LineStyleGeometryModifier_TipRemover *)new_m;
1262                         q->tip_length = p->tip_length;
1263                         break;
1264                 }
1265                 case LS_MODIFIER_POLYGONIZATION:
1266                 {
1267                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
1268                         LineStyleGeometryModifier_Polygonalization *q = (LineStyleGeometryModifier_Polygonalization *)new_m;
1269                         q->error = p->error;
1270                         break;
1271                 }
1272                 case LS_MODIFIER_GUIDING_LINES:
1273                 {
1274                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
1275                         LineStyleGeometryModifier_GuidingLines *q = (LineStyleGeometryModifier_GuidingLines *)new_m;
1276                         q->offset = p->offset;
1277                         break;
1278                 }
1279                 case LS_MODIFIER_BLUEPRINT:
1280                 {
1281                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
1282                         LineStyleGeometryModifier_Blueprint *q = (LineStyleGeometryModifier_Blueprint *)new_m;
1283                         q->flags = p->flags;
1284                         q->rounds = p->rounds;
1285                         q->backbone_length = p->backbone_length;
1286                         q->random_radius = p->random_radius;
1287                         q->random_center = p->random_center;
1288                         q->random_backbone = p->random_backbone;
1289                         break;
1290                 }
1291                 case LS_MODIFIER_2D_OFFSET:
1292                 {
1293                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
1294                         LineStyleGeometryModifier_2DOffset *q = (LineStyleGeometryModifier_2DOffset *)new_m;
1295                         q->start = p->start;
1296                         q->end = p->end;
1297                         q->x = p->x;
1298                         q->y = p->y;
1299                         break;
1300                 }
1301                 case LS_MODIFIER_2D_TRANSFORM:
1302                 {
1303                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
1304                         LineStyleGeometryModifier_2DTransform *q = (LineStyleGeometryModifier_2DTransform *)new_m;
1305                         q->pivot = p->pivot;
1306                         q->scale_x = p->scale_x;
1307                         q->scale_y = p->scale_y;
1308                         q->angle = p->angle;
1309                         q->pivot_u = p->pivot_u;
1310                         q->pivot_x = p->pivot_x;
1311                         q->pivot_y = p->pivot_y;
1312                         break;
1313                 }
1314                 case LS_MODIFIER_SIMPLIFICATION:
1315                 {
1316                         LineStyleGeometryModifier_Simplification *p = (LineStyleGeometryModifier_Simplification *)m;
1317                         LineStyleGeometryModifier_Simplification *q = (LineStyleGeometryModifier_Simplification *)new_m;
1318                         q->tolerance = p->tolerance;
1319                         break;
1320                 }
1321                 default:
1322                         return NULL; /* unknown modifier type */
1323         }
1324         add_to_modifier_list(&linestyle->geometry_modifiers, new_m);
1325
1326         return new_m;
1327 }
1328
1329 int BKE_linestyle_geometry_modifier_remove(FreestyleLineStyle *linestyle, LineStyleModifier *m)
1330 {
1331         if (BLI_findindex(&linestyle->geometry_modifiers, m) == -1)
1332                 return -1;
1333         BLI_freelinkN(&linestyle->geometry_modifiers, m);
1334         return 0;
1335 }
1336
1337 /**
1338  * Reinsert \a modifier in modifier list with an offset of \a direction.
1339  * \return if position of \a modifier has changed.
1340  */
1341 bool BKE_linestyle_color_modifier_move(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1342 {
1343         return BLI_listbase_link_move(&linestyle->color_modifiers, modifier, direction);
1344 }
1345 bool BKE_linestyle_alpha_modifier_move(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1346 {
1347         return BLI_listbase_link_move(&linestyle->alpha_modifiers, modifier, direction);
1348 }
1349 bool BKE_linestyle_thickness_modifier_move(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1350 {
1351         return BLI_listbase_link_move(&linestyle->thickness_modifiers, modifier, direction);
1352 }
1353 bool BKE_linestyle_geometry_modifier_move(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1354 {
1355         return BLI_listbase_link_move(&linestyle->geometry_modifiers, modifier, direction);
1356 }
1357
1358 void BKE_linestyle_modifier_list_color_ramps(FreestyleLineStyle *linestyle, ListBase *listbase)
1359 {
1360         LineStyleModifier *m;
1361         ColorBand *color_ramp;
1362         LinkData *link;
1363
1364         BLI_listbase_clear(listbase);
1365
1366         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1367                 switch (m->type) {
1368                         case LS_MODIFIER_ALONG_STROKE:
1369                                 color_ramp = ((LineStyleColorModifier_AlongStroke *)m)->color_ramp;
1370                                 break;
1371                         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
1372                                 color_ramp = ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp;
1373                                 break;
1374                         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1375                                 color_ramp = ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp;
1376                                 break;
1377                         case LS_MODIFIER_MATERIAL:
1378                                 color_ramp = ((LineStyleColorModifier_Material *)m)->color_ramp;
1379                                 break;
1380                         default:
1381                                 continue;
1382                 }
1383                 link = (LinkData *) MEM_callocN(sizeof(LinkData), "link to color ramp");
1384                 link->data = color_ramp;
1385                 BLI_addtail(listbase, link);
1386         }
1387 }
1388
1389 char *BKE_linestyle_path_to_color_ramp(FreestyleLineStyle *linestyle, ColorBand *color_ramp)
1390 {
1391         LineStyleModifier *m;
1392         bool found = false;
1393
1394         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1395                 switch (m->type) {
1396                         case LS_MODIFIER_ALONG_STROKE:
1397                                 if (color_ramp == ((LineStyleColorModifier_AlongStroke *)m)->color_ramp)
1398                                         found = true;
1399                                 break;
1400                         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
1401                                 if (color_ramp == ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp)
1402                                         found = true;
1403                                 break;
1404                         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1405                                 if (color_ramp == ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp)
1406                                         found = true;
1407                                 break;
1408                         case LS_MODIFIER_MATERIAL:
1409                                 if (color_ramp == ((LineStyleColorModifier_Material *)m)->color_ramp)
1410                                         found = true;
1411                                 break;
1412                         case LS_MODIFIER_TANGENT:
1413                                 if (color_ramp == ((LineStyleColorModifier_Tangent *)m)->color_ramp)
1414                                         found = true;
1415                                 break;
1416                         case LS_MODIFIER_NOISE:
1417                                 if (color_ramp == ((LineStyleColorModifier_Noise *)m)->color_ramp)
1418                                         found = true;
1419                                 break;
1420                         case LS_MODIFIER_CREASE_ANGLE:
1421                                 if (color_ramp == ((LineStyleColorModifier_CreaseAngle *)m)->color_ramp)
1422                                         found = true;
1423                                 break;
1424                         case LS_MODIFIER_CURVATURE_3D:
1425                                 if (color_ramp == ((LineStyleColorModifier_Curvature_3D *)m)->color_ramp)
1426                                         found = true;
1427                                 break;
1428                 }
1429
1430                 if (found) {
1431                         char name_esc[sizeof(m->name) * 2];
1432                         BLI_strescape(name_esc, m->name, sizeof(name_esc));
1433                         return BLI_sprintfN("color_modifiers[\"%s\"].color_ramp", name_esc);
1434                 }
1435         }
1436         printf("BKE_linestyle_path_to_color_ramp: No color ramps correspond to the given pointer.\n");
1437         return NULL;
1438 }
1439
1440 bool BKE_linestyle_use_textures(FreestyleLineStyle *linestyle, const bool use_shading_nodes)
1441 {
1442         if (use_shading_nodes) {
1443                 if (linestyle && linestyle->use_nodes && linestyle->nodetree) {
1444                         bNode *node;
1445
1446                         for (node = linestyle->nodetree->nodes.first; node; node = node->next) {
1447                                 if (node->typeinfo->nclass == NODE_CLASS_TEXTURE) {
1448                                         return true;
1449                                 }
1450                         }
1451                 }
1452         }
1453         else {
1454                 if (linestyle && (linestyle->flag & LS_TEXTURE)) {
1455                         return (linestyle->mtex[0] != NULL);
1456                 }
1457         }
1458         return false;
1459 }
1460
1461 void BKE_linestyle_default_shader(const bContext *C, FreestyleLineStyle *linestyle)
1462 {
1463         bNode *uv_along_stroke, *input_texure, *output_linestyle;
1464         bNodeSocket *fromsock, *tosock;
1465         bNodeTree *ntree;
1466
1467         BLI_assert(linestyle->nodetree == NULL);
1468
1469         ntree = ntreeAddTree(NULL, "stroke_shader", "ShaderNodeTree");
1470
1471         linestyle->nodetree = ntree;
1472
1473         uv_along_stroke = nodeAddStaticNode(C, ntree, SH_NODE_UVALONGSTROKE);
1474         uv_along_stroke->locx = 0.0f;
1475         uv_along_stroke->locy = 300.0f;
1476         uv_along_stroke->custom1 = 0; // use_tips
1477
1478         input_texure = nodeAddStaticNode(C, ntree, SH_NODE_TEX_IMAGE);
1479         input_texure->locx = 200.0f;
1480         input_texure->locy = 300.0f;
1481
1482         output_linestyle = nodeAddStaticNode(C, ntree, SH_NODE_OUTPUT_LINESTYLE);
1483         output_linestyle->locx = 400.0f;
1484         output_linestyle->locy = 300.0f;
1485         output_linestyle->custom1 = MA_RAMP_BLEND;
1486         output_linestyle->custom2 = 0; // use_clamp
1487
1488         nodeSetActive(ntree, input_texure);
1489
1490         fromsock = BLI_findlink(&uv_along_stroke->outputs, 0); // UV
1491         tosock = BLI_findlink(&input_texure->inputs, 0); // UV
1492         nodeAddLink(ntree, uv_along_stroke, fromsock, input_texure, tosock);
1493
1494         fromsock = BLI_findlink(&input_texure->outputs, 0); // Color
1495         tosock = BLI_findlink(&output_linestyle->inputs, 0); // Color
1496         nodeAddLink(ntree, input_texure, fromsock, output_linestyle, tosock);
1497
1498         ntreeUpdateTree(CTX_data_main(C), ntree);
1499 }