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