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