Fix for invalid pointers within a copied line set.
[blender.git] / source / blender / freestyle / intern / blender_interface / FRS_freestyle.cpp
1 #include "../application/Controller.h"
2 #include "../application/AppView.h"
3 #include "../application/AppConfig.h"
4 #include "../application/AppCanvas.h"
5
6 #include <iostream>
7 #include <map>
8 #include <set>
9 using namespace std;
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 #include "MEM_guardedalloc.h"
16
17 #include "DNA_camera_types.h"
18 #include "DNA_text_types.h"
19 #include "DNA_group_types.h"
20 #include "DNA_freestyle_types.h"
21
22 #include "BKE_global.h"
23 #include "BKE_library.h"
24 #include "BKE_linestyle.h"
25 #include "BKE_main.h"
26 #include "BKE_text.h"
27 #include "BLI_blenlib.h"
28 #include "BLI_math.h"
29 #include "BPY_extern.h"
30
31 #include "renderpipeline.h"
32 #include "pixelblending.h"
33
34 #include "../../FRS_freestyle.h"
35 #include "../../FRS_freestyle_config.h"
36
37         // Freestyle configuration
38         static short freestyle_is_initialized = 0;
39         static Config::Path *pathconfig = NULL;
40         static Controller *controller = NULL;
41         static AppView *view = NULL;
42
43         // line set buffer for copy & paste
44         static FreestyleLineSet lineset_buffer;
45         static bool lineset_copied = false;
46
47         // camera information
48         float freestyle_viewpoint[3];
49         float freestyle_mv[4][4];
50         float freestyle_proj[4][4];
51         int freestyle_viewport[4];
52         
53         // current scene
54         Scene *freestyle_scene;
55
56         string default_module_path;
57
58         //=======================================================
59         //   Initialization 
60         //=======================================================
61
62         void FRS_initialize() {
63                 
64                 if( freestyle_is_initialized )
65                         return;
66
67                 pathconfig = new Config::Path;
68                 controller = new Controller();
69                 view = new AppView;
70                 controller->setView(view);
71                 controller->Clear();
72                 freestyle_scene = NULL;
73                 lineset_copied = false;
74                         
75                 default_module_path = pathconfig->getProjectDir() + Config::DIR_SEP + "style_modules" + Config::DIR_SEP + "contour.py";
76                         
77                 freestyle_is_initialized = 1;
78         }
79         
80         void FRS_set_context(bContext* C) {
81                 cout << "FRS_set_context: context 0x" << C << " scene 0x" << CTX_data_scene(C) << endl;
82                 controller->setContext(C);
83         }
84
85         void FRS_read_file(bContext* C) {
86                 lineset_copied = false;
87         }
88
89         void FRS_exit() {
90                 delete pathconfig;
91                 delete controller;
92                 delete view;
93         }
94
95         //=======================================================
96         //   Rendering 
97         //=======================================================
98
99         static void init_view(Render* re){
100                 int width = re->winx;
101                 int height = re->winy;
102                 int xmin = re->disprect.xmin;
103                 int ymin = re->disprect.ymin;
104                 int xmax = re->disprect.xmax;
105                 int ymax = re->disprect.ymax;
106                 
107                 freestyle_viewport[0] = freestyle_viewport[1] = 0;
108                 freestyle_viewport[2] = width;
109                 freestyle_viewport[3] = height;
110                 
111                 view->setWidth( width );
112                 view->setHeight( height );
113                 view->setBorder( xmin, ymin, xmax, ymax );
114
115                 cout << "\n===  Dimensions of the 2D image coordinate system  ===" << endl;
116                 cout << "Width  : " << width << endl;
117                 cout << "Height : " << height << endl;
118                 if (re->r.mode & R_BORDER)
119                         cout << "Border : (" << xmin << ", " << ymin << ") - (" << xmax << ", " << ymax << ")" << endl;
120         }
121
122         static void init_camera(Render* re){
123                 // It is assumed that imported meshes are in the camera coordinate system.
124                 // Therefore, the view point (i.e., camera position) is at the origin, and
125                 // the the model-view matrix is simply the identity matrix.
126
127                 freestyle_viewpoint[0] = 0.0;
128                 freestyle_viewpoint[1] = 0.0;
129                 freestyle_viewpoint[2] = 0.0;
130                 
131                 for( int i = 0; i < 4; i++ )
132                    for( int j = 0; j < 4; j++ )
133                         freestyle_mv[i][j] = (i == j) ? 1.0 : 0.0;
134                 
135                 for( int i = 0; i < 4; i++ )
136                    for( int j = 0; j < 4; j++ )
137                         freestyle_proj[i][j] = re->winmat[i][j];
138
139                 //print_m4("mv", freestyle_mv);
140                 //print_m4("proj", freestyle_proj);
141         }
142
143         static char *escape_quotes(char *name)
144         {
145                 char *s= (char *)MEM_mallocN(strlen(name) * 2 + 1, "escape_quotes");
146                 char *p= s;
147                 while (*name) {
148                         if (*name == '\'')
149                                 *p++= '\\';
150                         *p++ = *name++;
151                 }
152                 *p = '\0';
153                 return s;
154         }
155
156         static Text *create_lineset_handler(char *layer_name, char *lineset_name)
157         {
158                 char *s1= escape_quotes(layer_name);
159                 char *s2= escape_quotes(lineset_name);
160                 Text *text= add_empty_text(lineset_name);
161                 write_text(text, "import parameter_editor; parameter_editor.process('");
162                 write_text(text, s1);
163                 write_text(text, "', '");
164                 write_text(text, s2);
165                 write_text(text, "')\n");
166                 MEM_freeN(s1);
167                 MEM_freeN(s2);
168                 return text;
169         }
170
171         struct edge_type_condition {
172                 int edge_type, value;
173         };
174
175         // examines the conditions and returns true if the target edge type needs to be computed
176         static bool test_edge_type_conditions(struct edge_type_condition *conditions,
177                 int num_edge_types, bool logical_and, int target, bool distinct)
178         {
179                 int target_condition = 0;
180                 int num_non_target_positive_conditions = 0;
181                 int num_non_target_negative_conditions = 0;
182
183                 for (int i = 0; i < num_edge_types; i++) {
184                         if (conditions[i].edge_type == target)
185                                 target_condition = conditions[i].value;
186                         else if (conditions[i].value > 0)
187                                 ++num_non_target_positive_conditions;
188                         else if (conditions[i].value < 0)
189                                 ++num_non_target_negative_conditions;
190                 }
191                 if (distinct) {
192                         // In this case, the 'target' edge type is assumed to appear on distinct edge
193                         // of its own and never together with other edge types.
194                         if (logical_and) {
195                                 if (num_non_target_positive_conditions > 0)
196                                         return false;
197                                 if (target_condition > 0)
198                                         return true;
199                                 if (target_condition < 0)
200                                         return false;
201                                 if (num_non_target_negative_conditions > 0)
202                                         return true;
203                         } else {
204                                 if (target_condition > 0)
205                                         return true;
206                                 if (num_non_target_negative_conditions > 0)
207                                         return true;
208                                 if (target_condition < 0)
209                                         return false;
210                                 if (num_non_target_positive_conditions > 0)
211                                         return false;
212                         }
213                 } else {
214                         // In this case, the 'target' edge type may appear together with other edge types.
215                         if (target_condition > 0)
216                                 return true;
217                         if (target_condition < 0)
218                                 return true;
219                         if (logical_and) {
220                                 if (num_non_target_positive_conditions > 0)
221                                         return false;
222                                 if (num_non_target_negative_conditions > 0)
223                                         return true;
224                         } else {
225                                 if (num_non_target_negative_conditions > 0)
226                                         return true;
227                                 if (num_non_target_positive_conditions > 0)
228                                         return false;
229                         }
230                 }
231                 return true;
232         }
233
234         static void prepare(Render* re, SceneRenderLayer* srl ) {
235                                 
236                 // load mesh
237         re->i.infostr= "Freestyle: Mesh loading";
238                 re->stats_draw(re->sdh, &re->i);
239         re->i.infostr= NULL;
240                 if( controller->LoadMesh(re, srl) ) // returns if scene cannot be loaded or if empty
241                         return;
242         if( re->test_break(re->tbh) )
243             return;
244                 
245                 // add style modules
246                 FreestyleConfig* config = &srl->freestyleConfig;
247                 
248                 cout << "\n===  Rendering options  ===" << endl;
249                 int layer_count = 0;
250                 
251                 switch (config->mode) {
252                 case FREESTYLE_CONTROL_SCRIPT_MODE:
253                         cout << "Modules :"<< endl;
254                         for (FreestyleModuleConfig* module_conf = (FreestyleModuleConfig *)config->modules.first; module_conf; module_conf = module_conf->next) {
255                                 if( module_conf->is_displayed ) {
256                                         cout << "  " << layer_count+1 << ": " << module_conf->module_path << endl;
257                                         controller->InsertStyleModule( layer_count, module_conf->module_path );
258                                         controller->toggleLayer(layer_count, true);
259                                         layer_count++;
260                                 }
261                         }
262                         cout << endl;
263                         controller->setComputeRidgesAndValleysFlag( (config->flags & FREESTYLE_RIDGES_AND_VALLEYS_FLAG) ? true : false);
264                         controller->setComputeSuggestiveContoursFlag( (config->flags & FREESTYLE_SUGGESTIVE_CONTOURS_FLAG) ? true : false);
265                         controller->setComputeMaterialBoundariesFlag( (config->flags & FREESTYLE_MATERIAL_BOUNDARIES_FLAG) ? true : false);
266                         break;
267                 case FREESTYLE_CONTROL_EDITOR_MODE:
268                         int use_ridges_and_valleys = 0;
269                         int use_suggestive_contours = 0;
270                         int use_material_boundaries = 0;
271                         struct edge_type_condition conditions[] = {
272                                 {FREESTYLE_FE_SILHOUETTE, 0},
273                                 {FREESTYLE_FE_BORDER, 0},
274                                 {FREESTYLE_FE_CREASE, 0},
275                                 {FREESTYLE_FE_RIDGE, 0},
276                                 {FREESTYLE_FE_VALLEY, 0},
277                                 {FREESTYLE_FE_SUGGESTIVE_CONTOUR, 0},
278                                 {FREESTYLE_FE_MATERIAL_BOUNDARY, 0},
279                                 {FREESTYLE_FE_CONTOUR, 0},
280                                 {FREESTYLE_FE_EXTERNAL_CONTOUR, 0},
281                                 {FREESTYLE_FE_EDGE_MARK, 0}};
282                         int num_edge_types = sizeof(conditions) / sizeof(struct edge_type_condition);
283                         cout << "Linesets:"<< endl;
284                         for (FreestyleLineSet *lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
285                                 if (lineset->flags & FREESTYLE_LINESET_ENABLED) {
286                                         cout << "  " << layer_count+1 << ": " << lineset->name << " - " << lineset->linestyle->id.name+2 << endl;
287                                         Text *text = create_lineset_handler(srl->name, lineset->name);
288                                         controller->InsertStyleModule( layer_count, lineset->name, text );
289                                         controller->toggleLayer(layer_count, true);
290                                         if (!(lineset->selection & FREESTYLE_SEL_EDGE_TYPES) || !lineset->edge_types) {
291                                                 ++use_ridges_and_valleys;
292                                                 ++use_suggestive_contours;
293                                                 ++use_material_boundaries;
294                                         } else {
295                                                 // conditions for feature edge selection by edge types
296                                                 for (int i = 0; i < num_edge_types; i++) {
297                                                         if (!(lineset->edge_types & conditions[i].edge_type))
298                                                                 conditions[i].value = 0; // no condition specified
299                                                         else if (!(lineset->exclude_edge_types & conditions[i].edge_type))
300                                                                 conditions[i].value = 1; // condition: X
301                                                         else
302                                                                 conditions[i].value = -1; // condition: NOT X
303                                                 }
304                                                 // logical operator for the selection conditions
305                                                 bool logical_and = ((lineset->flags & FREESTYLE_LINESET_FE_AND) != 0);
306                                                 // negation operator
307                                                 if (lineset->flags & FREESTYLE_LINESET_FE_NOT) {
308                                                         // convert an Exclusive condition into an Inclusive equivalent using De Morgan's laws:
309                                                         //   NOT (X OR Y) --> (NOT X) AND (NOT Y)
310                                                         //   NOT (X AND Y) --> (NOT X) OR (NOT Y)
311                                                         for (int i = 0; i < num_edge_types; i++)
312                                                                 conditions[i].value *= -1;
313                                                         logical_and = !logical_and;
314                                                 }
315                                                 if (test_edge_type_conditions(conditions, num_edge_types, logical_and, FREESTYLE_FE_RIDGE, true) ||
316                                                         test_edge_type_conditions(conditions, num_edge_types, logical_and, FREESTYLE_FE_VALLEY, true))
317                                                         ++use_ridges_and_valleys;
318                                                 if (test_edge_type_conditions(conditions, num_edge_types, logical_and, FREESTYLE_FE_SUGGESTIVE_CONTOUR, true))
319                                                         ++use_suggestive_contours;
320                                                 if (test_edge_type_conditions(conditions, num_edge_types, logical_and, FREESTYLE_FE_MATERIAL_BOUNDARY, true))
321                                                         ++use_material_boundaries;
322                                         }
323                                         layer_count++;
324                                 }
325                         }
326                         controller->setComputeRidgesAndValleysFlag( use_ridges_and_valleys > 0 );
327                         controller->setComputeSuggestiveContoursFlag( use_suggestive_contours > 0 );
328                         controller->setComputeMaterialBoundariesFlag( use_material_boundaries > 0 );
329                         break;
330                 }
331                 
332                 // set parameters
333                 controller->setFaceSmoothness( (config->flags & FREESTYLE_FACE_SMOOTHNESS_FLAG) ? true : false);
334                 controller->setCreaseAngle( config->crease_angle );
335                 controller->setSphereRadius( config->sphere_radius );
336                 controller->setSuggestiveContourKrDerivativeEpsilon( config->dkr_epsilon ) ;
337                 controller->setVisibilityAlgo( config->raycasting_algorithm );
338
339                 cout << "Crease angle : " << controller->getCreaseAngle() << endl;
340                 cout << "Sphere radius : " << controller->getSphereRadius() << endl;
341                 cout << "Face smoothness : " << (controller->getFaceSmoothness() ? "enabled" : "disabled") << endl;
342                 cout << "Redges and valleys : " << (controller->getComputeRidgesAndValleysFlag() ? "enabled" : "disabled") << endl;
343                 cout << "Suggestive contours : " << (controller->getComputeSuggestiveContoursFlag() ? "enabled" : "disabled") << endl;
344                 cout << "Suggestive contour Kr derivative epsilon : " << controller->getSuggestiveContourKrDerivativeEpsilon() << endl;
345                 cout << "Material boundaries : " << (controller->getComputeMaterialBoundariesFlag() ? "enabled" : "disabled") << endl;
346                 cout << endl;
347
348         // set diffuse and z depth passes
349         RenderLayer *rl = RE_GetRenderLayer(re->result, srl->name);
350                 bool diffuse = false, z = false;
351                 for (RenderPass *rpass = (RenderPass *)rl->passes.first; rpass; rpass = rpass->next) {
352                         switch (rpass->passtype) {
353                         case SCE_PASS_DIFFUSE:
354                                 controller->setPassDiffuse(rpass->rect, rpass->rectx, rpass->recty);
355                                 diffuse = true;
356                                 break;
357                         case SCE_PASS_Z:
358                                 controller->setPassZ(rpass->rect, rpass->rectx, rpass->recty);
359                                 z = true;
360                                 break;
361                         }
362                 }
363         cout << "Passes :" << endl;
364         cout << "  Diffuse = " << (diffuse ? "enabled" : "disabled") << endl;
365         cout << "  Z = " << (z ? "enabled" : "disabled") << endl;
366
367                 // compute view map
368         re->i.infostr= "Freestyle: View map creation";
369                 re->stats_draw(re->sdh, &re->i);
370         re->i.infostr= NULL;
371                 controller->ComputeViewMap();
372         }
373         
374         void FRS_composite_result(Render* re, SceneRenderLayer* srl, Render* freestyle_render)
375         {
376                 RenderLayer *rl;
377             float *src, *dest, *pixSrc, *pixDest;
378                 int x, y, rectx, recty;
379                 
380                 if( freestyle_render == NULL || freestyle_render->result == NULL )
381                         return;
382
383                 rl = render_get_active_layer( freestyle_render, freestyle_render->result );
384             if( !rl || rl->rectf == NULL) { cout << "Cannot find Freestyle result image" << endl; return; }
385                 src  = rl->rectf;
386                 //cout << "src: " << rl->rectx << " x " << rl->recty << endl;
387                 
388                 rl = RE_GetRenderLayer(re->result, srl->name);
389             if( !rl || rl->rectf == NULL) { cout << "No layer to composite to" << endl; return; }
390                 dest  = rl->rectf;
391                 //cout << "dest: " << rl->rectx << " x " << rl->recty << endl;
392
393                 rectx = re->rectx;
394                 recty = re->recty;
395                 for( y = 0; y < recty; y++) {
396                 for( x = 0; x < rectx; x++) {
397                     pixSrc = src + 4 * (rectx * y + x);
398                     if( pixSrc[3] > 0.0) {
399                                         pixDest = dest + 4 * (rectx * y + x);
400                                         addAlphaOverFloat(pixDest, pixSrc);
401                                 }
402                         }
403             }
404         }
405         
406         static int displayed_layer_count( SceneRenderLayer* srl ) {
407                 int count = 0;
408
409                 switch (srl->freestyleConfig.mode) {
410                 case FREESTYLE_CONTROL_SCRIPT_MODE:
411                         for (FreestyleModuleConfig* module = (FreestyleModuleConfig *)srl->freestyleConfig.modules.first; module; module = module->next) {
412                                 if( module->is_displayed )
413                                         count++;
414                         }
415                         break;
416                 case FREESTYLE_CONTROL_EDITOR_MODE:
417                         for (FreestyleLineSet *lineset = (FreestyleLineSet *)srl->freestyleConfig.linesets.first; lineset; lineset = lineset->next) {
418                                 if (lineset->flags & FREESTYLE_LINESET_ENABLED)
419                                         count++;
420                         }
421                         break;
422                 }
423                 return count;
424         }
425
426         int FRS_is_freestyle_enabled(SceneRenderLayer* srl) {
427                 return (!(srl->layflag & SCE_LAY_DISABLE) &&
428                                 srl->layflag & SCE_LAY_FRS &&
429                                 displayed_layer_count(srl) > 0);
430         }
431         
432         void FRS_init_stroke_rendering(Render* re) {
433
434                 cout << "\n#===============================================================" << endl;
435                 cout << "#  Freestyle" << endl;
436                 cout << "#===============================================================" << endl;
437                 
438                 init_view(re);
439                 init_camera(re);
440
441                 controller->ResetRenderCount();
442         }
443         
444         Render* FRS_do_stroke_rendering(Render* re, SceneRenderLayer *srl) {
445                 
446                 Render* freestyle_render = NULL;
447                 
448                 cout << "\n----------------------------------------------------------" << endl;
449                 cout << "|  " << (re->scene->id.name+2) << "|" << srl->name << endl;
450                 cout << "----------------------------------------------------------" << endl;
451                 
452                 // prepare Freestyle:
453                 //   - load mesh
454                 //   - add style modules
455                 //   - set parameters
456                 //   - compute view map
457                 prepare(re, srl);
458
459         if( re->test_break(re->tbh) ) {
460                         controller->CloseFile();
461             return NULL;
462         }
463
464                 // render and composite Freestyle result
465                 if( controller->_ViewMap ) {
466                         
467                         // render strokes                                       
468             re->i.infostr= "Freestyle: Stroke rendering";
469             re->stats_draw(re->sdh, &re->i);
470                 re->i.infostr= NULL;
471                         freestyle_scene = re->scene;
472                         controller->DrawStrokes();
473                         freestyle_render = controller->RenderStrokes(re);
474                         controller->CloseFile();
475                         freestyle_scene = NULL;
476                         
477                         // composite result
478                         FRS_composite_result(re, srl, freestyle_render);
479                         RE_FreeRenderResult(freestyle_render->result);
480                         freestyle_render->result = NULL;
481                 }
482
483                 return freestyle_render;
484         }
485
486         void FRS_finish_stroke_rendering(Render* re) {
487                 // clear canvas
488                 controller->Clear();
489         }
490
491         //=======================================================
492         //   Freestyle Panel Configuration
493         //=======================================================
494
495         void FRS_add_freestyle_config( SceneRenderLayer* srl )
496         {               
497                 FreestyleConfig* config = &srl->freestyleConfig;
498                 
499                 config->mode = FREESTYLE_CONTROL_SCRIPT_MODE;
500
501                 config->modules.first = config->modules.last = NULL;
502                 config->flags = 0;
503                 config->sphere_radius = 1.0f;
504                 config->dkr_epsilon = 0.001f;
505                 config->crease_angle = 134.43f;
506
507                 config->linesets.first = config->linesets.last = NULL;
508
509                 config->raycasting_algorithm = FREESTYLE_ALGO_REGULAR;
510         }
511         
512         void FRS_free_freestyle_config( SceneRenderLayer* srl )
513         {               
514                 FreestyleLineSet *lineset;
515
516                 for(lineset=(FreestyleLineSet *)srl->freestyleConfig.linesets.first; lineset; lineset=lineset->next) {
517                         if (lineset->group) {
518                                 lineset->group->id.us--;
519                                 lineset->group = NULL;
520                         }
521                         lineset->linestyle->id.us--;
522                         lineset->linestyle = NULL;
523                 }
524                 BLI_freelistN( &srl->freestyleConfig.linesets );
525                 BLI_freelistN( &srl->freestyleConfig.modules );
526         }
527
528         void FRS_add_module(FreestyleConfig *config)
529         {
530                 FreestyleModuleConfig* module_conf = (FreestyleModuleConfig*) MEM_callocN( sizeof(FreestyleModuleConfig), "style module configuration");
531                 BLI_addtail(&config->modules, (void*) module_conf);
532                 
533                 strcpy( module_conf->module_path, default_module_path.c_str() );
534                 module_conf->is_displayed = 1;  
535         }
536         
537         void FRS_delete_module(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
538         {
539                 BLI_freelinkN(&config->modules, module_conf);
540         }
541         
542         void FRS_move_module_up(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
543         {
544                 BLI_remlink(&config->modules, module_conf);
545                 BLI_insertlinkbefore(&config->modules, module_conf->prev, module_conf);
546         }
547         
548         void FRS_move_module_down(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
549         {                       
550                 BLI_remlink(&config->modules, module_conf);
551                 BLI_insertlinkafter(&config->modules, module_conf->next, module_conf);
552         }
553
554         static void unique_lineset_name(FreestyleConfig *config, FreestyleLineSet *lineset)
555         {
556                 BLI_uniquename(&config->linesets, lineset, "FreestyleLineSet", '.', offsetof(FreestyleLineSet, name), sizeof(lineset->name));
557         }
558
559         FreestyleLineSet *FRS_add_lineset(FreestyleConfig *config)
560         {
561                 int lineset_index = BLI_countlist(&config->linesets);
562
563                 FreestyleLineSet *lineset = (FreestyleLineSet *) MEM_callocN( sizeof(FreestyleLineSet), "Freestyle line set");
564                 BLI_addtail(&config->linesets, (void *) lineset);
565                 FRS_set_active_lineset_index(config, lineset_index);
566
567                 lineset->linestyle = FRS_new_linestyle("LineStyle", NULL);
568                 lineset->flags |= FREESTYLE_LINESET_ENABLED;
569                 lineset->selection = FREESTYLE_SEL_IMAGE_BORDER;
570                 lineset->qi = FREESTYLE_QI_VISIBLE;
571                 lineset->qi_start = 0;
572                 lineset->qi_end = 100;
573                 lineset->edge_types = FREESTYLE_FE_SILHOUETTE | FREESTYLE_FE_BORDER | FREESTYLE_FE_CREASE;
574                 lineset->exclude_edge_types = 0;
575                 lineset->group = NULL;
576                 if (lineset_index > 0)
577                         sprintf(lineset->name, "LineSet %i", lineset_index+1);
578                 else
579                         strcpy(lineset->name, "LineSet");
580                 unique_lineset_name(config, lineset);
581
582                 return lineset;
583         }
584
585         void FRS_copy_active_lineset(FreestyleConfig *config)
586         {
587                 FreestyleLineSet *lineset = FRS_get_active_lineset(config);
588
589                 if (lineset) {
590                         lineset_buffer.linestyle = lineset->linestyle;
591                         lineset_buffer.flags = lineset->flags;
592                         lineset_buffer.selection = lineset->selection;
593                         lineset_buffer.qi = lineset->qi;
594                         lineset_buffer.qi_start = lineset->qi_start;
595                         lineset_buffer.qi_end = lineset->qi_end;
596                         lineset_buffer.edge_types = lineset->edge_types;
597                         lineset_buffer.exclude_edge_types = lineset->exclude_edge_types;
598                         lineset_buffer.group = lineset->group;
599                         strcpy(lineset_buffer.name, lineset->name);
600                         lineset_copied = true;
601                 }
602         }
603
604         void FRS_paste_active_lineset(FreestyleConfig *config)
605         {
606                 if (!lineset_copied)
607                         return;
608
609                 FreestyleLineSet *lineset = FRS_get_active_lineset(config);
610
611                 if (lineset) {
612                         lineset->linestyle->id.us--;
613                         lineset->linestyle = lineset_buffer.linestyle;
614                         lineset->linestyle->id.us++;
615                         lineset->flags = lineset_buffer.flags;
616                         lineset->selection = lineset_buffer.selection;
617                         lineset->qi = lineset_buffer.qi;
618                         lineset->qi_start = lineset_buffer.qi_start;
619                         lineset->qi_end = lineset_buffer.qi_end;
620                         lineset->edge_types = lineset_buffer.edge_types;
621                         lineset->exclude_edge_types = lineset_buffer.exclude_edge_types;
622                         if (lineset->group) {
623                                 lineset->group->id.us--;
624                                 lineset->group = NULL;
625                         }
626                         if (lineset_buffer.group) {
627                                 lineset->group = lineset_buffer.group;
628                                 lineset->group->id.us++;
629                         }
630                         strcpy(lineset->name, lineset_buffer.name);
631                         unique_lineset_name(config, lineset);
632                         lineset->flags |= FREESTYLE_LINESET_CURRENT;
633                 }
634         }
635
636         void FRS_delete_active_lineset(FreestyleConfig *config)
637         {
638                 FreestyleLineSet *lineset = FRS_get_active_lineset(config);
639
640                 if (lineset) {
641                         if (lineset->group) {
642                                 lineset->group->id.us--;
643                                 lineset->group = NULL;
644                         }
645                         lineset->linestyle->id.us--;
646                         lineset->linestyle = NULL;
647                         BLI_remlink(&config->linesets, lineset);
648                         MEM_freeN(lineset);
649                         FRS_set_active_lineset_index(config, 0);
650                 }
651         }
652
653         void FRS_move_active_lineset_up(FreestyleConfig *config)
654         {
655                 FreestyleLineSet *lineset = FRS_get_active_lineset(config);
656
657                 if (lineset) {
658                         BLI_remlink(&config->linesets, lineset);
659                         BLI_insertlinkbefore(&config->linesets, lineset->prev, lineset);
660                 }
661         }
662
663         void FRS_move_active_lineset_down(FreestyleConfig *config)
664         {
665                 FreestyleLineSet *lineset = FRS_get_active_lineset(config);
666
667                 if (lineset) {
668                         BLI_remlink(&config->linesets, lineset);
669                         BLI_insertlinkafter(&config->linesets, lineset->next, lineset);
670                 }
671         }
672
673         FreestyleLineSet *FRS_get_active_lineset(FreestyleConfig *config)
674         {
675                 FreestyleLineSet *lineset;
676
677                 for(lineset=(FreestyleLineSet *)config->linesets.first; lineset; lineset=lineset->next)
678                         if(lineset->flags & FREESTYLE_LINESET_CURRENT)
679                                 return lineset;
680                 return NULL;
681         }
682
683         short FRS_get_active_lineset_index(FreestyleConfig *config)
684         {
685                 FreestyleLineSet *lineset;
686                 short i;
687
688                 for(lineset=(FreestyleLineSet *)config->linesets.first, i=0; lineset; lineset=lineset->next, i++)
689                         if(lineset->flags & FREESTYLE_LINESET_CURRENT)
690                                 return i;
691                 return 0;
692         }
693
694         void FRS_set_active_lineset_index(FreestyleConfig *config, short index)
695         {
696                 FreestyleLineSet *lineset;
697                 short i;
698
699                 for(lineset=(FreestyleLineSet *)config->linesets.first, i=0; lineset; lineset=lineset->next, i++) {
700                         if(i == index)
701                                 lineset->flags |= FREESTYLE_LINESET_CURRENT;
702                         else
703                                 lineset->flags &= ~FREESTYLE_LINESET_CURRENT;
704                 }
705         }
706
707         void FRS_unlink_target_object(FreestyleConfig *config, Object *ob)
708         {
709                 FreestyleLineSet *lineset;
710
711                 for(lineset=(FreestyleLineSet *)config->linesets.first; lineset; lineset=lineset->next) {
712                         FRS_unlink_linestyle_target_object(lineset->linestyle, ob);
713                 }
714         }
715
716 #ifdef __cplusplus
717 }
718 #endif