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