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