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