Freestyle: fix crash unlinking object
[blender.git] / source / blender / blenkernel / intern / freestyle.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) 2013 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/freestyle.c
29  *  \ingroup bke
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_freestyle_types.h"
35 #include "DNA_group_types.h"
36
37 #include "BKE_freestyle.h"
38 #include "BKE_linestyle.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42
43 // function declarations
44 static FreestyleLineSet *alloc_lineset(void);
45 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset);
46 static FreestyleModuleConfig *alloc_module(void);
47 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module);
48
49 void BKE_freestyle_config_init(FreestyleConfig *config)
50 {
51         config->mode = FREESTYLE_CONTROL_EDITOR_MODE;
52
53         config->modules.first = config->modules.last = NULL;
54         config->flags = 0;
55         config->sphere_radius = 1.0f;
56         config->dkr_epsilon = 0.0f;
57         config->crease_angle = DEG2RADF(134.43f);
58
59         config->linesets.first = config->linesets.last = NULL;
60 }
61
62 void BKE_freestyle_config_free(FreestyleConfig *config)
63 {
64         FreestyleLineSet *lineset;
65
66         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
67                 if (lineset->group) {
68                         lineset->group->id.us--;
69                         lineset->group = NULL;
70                 }
71                 lineset->linestyle->id.us--;
72                 lineset->linestyle = NULL;
73         }
74         BLI_freelistN(&config->linesets);
75         BLI_freelistN(&config->modules);
76 }
77
78 void BKE_freestyle_config_copy(FreestyleConfig *new_config, FreestyleConfig *config)
79 {
80         FreestyleLineSet *lineset, *new_lineset;
81         FreestyleModuleConfig *module, *new_module;
82
83         new_config->mode = config->mode;
84         new_config->raycasting_algorithm = config->raycasting_algorithm; /* deprecated */
85         new_config->flags = config->flags;
86         new_config->sphere_radius = config->sphere_radius;
87         new_config->dkr_epsilon = config->dkr_epsilon;
88         new_config->crease_angle = config->crease_angle;
89
90         new_config->linesets.first = new_config->linesets.last = NULL;
91         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
92                 new_lineset = alloc_lineset();
93                 copy_lineset(new_lineset, lineset);
94                 BLI_addtail(&new_config->linesets, (void *)new_lineset);
95         }
96
97         new_config->modules.first = new_config->modules.last = NULL;
98         for (module = (FreestyleModuleConfig *)config->modules.first; module; module = module->next) {
99                 new_module = alloc_module();
100                 copy_module(new_module, module);
101                 BLI_addtail(&new_config->modules, (void *)new_module);
102         }
103 }
104
105 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset)
106 {
107         new_lineset->linestyle = lineset->linestyle;
108         new_lineset->linestyle->id.us++;
109         new_lineset->flags = lineset->flags;
110         new_lineset->selection = lineset->selection;
111         new_lineset->qi = lineset->qi;
112         new_lineset->qi_start = lineset->qi_start;
113         new_lineset->qi_end = lineset->qi_end;
114         new_lineset->edge_types = lineset->edge_types;
115         new_lineset->exclude_edge_types = lineset->exclude_edge_types;
116         new_lineset->group = lineset->group;
117         if (new_lineset->group) {
118                 new_lineset->group->id.us++;
119         }
120         strcpy(new_lineset->name, lineset->name);
121 }
122
123 static FreestyleModuleConfig *alloc_module(void)
124 {
125         return (FreestyleModuleConfig *)MEM_callocN(sizeof(FreestyleModuleConfig), "style module configuration");
126 }
127
128 void BKE_freestyle_module_add(FreestyleConfig *config)
129 {
130         FreestyleModuleConfig *module_conf = alloc_module();
131         const size_t maxlen = sizeof(module_conf->module_path);
132         BLI_addtail(&config->modules, (void *)module_conf);
133
134         BLI_strncpy(module_conf->module_path, BLI_get_folder(BLENDER_SYSTEM_SCRIPTS, "freestyle"), maxlen);
135         BLI_join_dirfile(module_conf->module_path, maxlen, module_conf->module_path, "style_modules");
136         BLI_join_dirfile(module_conf->module_path, maxlen, module_conf->module_path, "contour.py");
137         module_conf->is_displayed = 1;
138 }
139
140 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module)
141 {
142         strcpy(new_module->module_path, module->module_path);
143         new_module->is_displayed = module->is_displayed;
144 }
145
146 void BKE_freestyle_module_delete(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
147 {
148         BLI_freelinkN(&config->modules, module_conf);
149 }
150
151 void BKE_freestyle_module_move_up(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
152 {
153         BLI_remlink(&config->modules, module_conf);
154         BLI_insertlinkbefore(&config->modules, module_conf->prev, module_conf);
155 }
156
157 void BKE_freestyle_module_move_down(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
158 {
159         BLI_remlink(&config->modules, module_conf);
160         BLI_insertlinkafter(&config->modules, module_conf->next, module_conf);
161 }
162
163 void BKE_freestyle_lineset_unique_name(FreestyleConfig *config, FreestyleLineSet *lineset)
164 {
165         BLI_uniquename(&config->linesets, lineset, "FreestyleLineSet", '.', offsetof(FreestyleLineSet, name),
166                        sizeof(lineset->name));
167 }
168
169 static FreestyleLineSet *alloc_lineset(void)
170 {
171         return (FreestyleLineSet *)MEM_callocN(sizeof(FreestyleLineSet), "Freestyle line set");
172 }
173
174 FreestyleLineSet *BKE_freestyle_lineset_add(FreestyleConfig *config)
175 {
176         int lineset_index = BLI_countlist(&config->linesets);
177
178         FreestyleLineSet *lineset = alloc_lineset();
179         BLI_addtail(&config->linesets, (void *)lineset);
180         BKE_freestyle_lineset_set_active_index(config, lineset_index);
181
182         lineset->linestyle = BKE_new_linestyle("LineStyle", NULL);
183         lineset->flags |= FREESTYLE_LINESET_ENABLED;
184         lineset->selection = FREESTYLE_SEL_VISIBILITY | FREESTYLE_SEL_EDGE_TYPES | FREESTYLE_SEL_IMAGE_BORDER;
185         lineset->qi = FREESTYLE_QI_VISIBLE;
186         lineset->qi_start = 0;
187         lineset->qi_end = 100;
188         lineset->edge_types = FREESTYLE_FE_SILHOUETTE | FREESTYLE_FE_BORDER | FREESTYLE_FE_CREASE;
189         lineset->exclude_edge_types = 0;
190         lineset->group = NULL;
191         if (lineset_index > 0)
192                 sprintf(lineset->name, "LineSet %i", lineset_index + 1);
193         else
194                 strcpy(lineset->name, "LineSet");
195         BKE_freestyle_lineset_unique_name(config, lineset);
196
197         return lineset;
198 }
199
200 FreestyleLineSet *BKE_freestyle_lineset_get_active(FreestyleConfig *config)
201 {
202         FreestyleLineSet *lineset;
203
204         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
205                 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
206                         return lineset;
207         }
208         return NULL;
209 }
210
211 short BKE_freestyle_lineset_get_active_index(FreestyleConfig *config)
212 {
213         FreestyleLineSet *lineset;
214         short i;
215
216         for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
217                 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
218                         return i;
219         }
220         return 0;
221 }
222
223 void BKE_freestyle_lineset_set_active_index(FreestyleConfig *config, short index)
224 {
225         FreestyleLineSet *lineset;
226         short i;
227
228         for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
229                 if (i == index)
230                         lineset->flags |= FREESTYLE_LINESET_CURRENT;
231                 else
232                         lineset->flags &= ~FREESTYLE_LINESET_CURRENT;
233         }
234 }