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