Decoupled a generic Config implementation from the PrintConfig definitions

This commit is contained in:
Alessandro Ranellucci 2013-12-21 16:15:41 +01:00
parent b2b67372ad
commit 56d4da2ac7
9 changed files with 200 additions and 154 deletions

View File

@ -37,6 +37,8 @@ src/Polyline.cpp
src/Polyline.hpp src/Polyline.hpp
src/PolylineCollection.cpp src/PolylineCollection.cpp
src/PolylineCollection.hpp src/PolylineCollection.hpp
src/PrintConfig.cpp
src/PrintConfig.hpp
src/ppport.h src/ppport.h
src/Surface.cpp src/Surface.cpp
src/Surface.hpp src/Surface.hpp

View File

@ -2,16 +2,6 @@
namespace Slic3r { namespace Slic3r {
t_optiondef_map Options = _build_optiondef_map();
FullConfig DefaultConfig = _build_default_config();
ConfigOptionDef*
get_config_option_def(const t_config_option_key opt_key) {
t_optiondef_map::iterator it = Options.find(opt_key);
if (it == Options.end()) return NULL;
return &it->second;
}
void void
ConfigBase::apply(ConfigBase &other, bool ignore_nonexistent) { ConfigBase::apply(ConfigBase &other, bool ignore_nonexistent) {
// get list of option keys to apply // get list of option keys to apply
@ -43,8 +33,8 @@ ConfigBase::set_deserialize(const t_config_option_key opt_key, std::string str)
float float
ConfigBase::get_abs_value(const t_config_option_key opt_key) { ConfigBase::get_abs_value(const t_config_option_key opt_key) {
// get option definition // get option definition
ConfigOptionDef* def = get_config_option_def(opt_key); assert(this->def->count(opt_key) != 0);
assert(def != NULL); ConfigOptionDef* def = &(*this->def)[opt_key];
assert(def->type == coFloatOrPercent); assert(def->type == coFloatOrPercent);
// get stored option value // get stored option value
@ -171,32 +161,34 @@ DynamicConfig::~DynamicConfig () {
ConfigOption* ConfigOption*
DynamicConfig::option(const t_config_option_key opt_key, bool create) { DynamicConfig::option(const t_config_option_key opt_key, bool create) {
t_options_map::iterator it = this->options.find(opt_key); if (this->options.count(opt_key) == 0) {
if (it == this->options.end()) {
if (create) { if (create) {
ConfigOptionDef* optdef = &(*this->def)[opt_key];
ConfigOption* opt; ConfigOption* opt;
if (Options[opt_key].type == coFloat) { if (optdef->type == coFloat) {
opt = new ConfigOptionFloat (); opt = new ConfigOptionFloat ();
} else if (Options[opt_key].type == coFloats) { } else if (optdef->type == coFloats) {
opt = new ConfigOptionFloats (); opt = new ConfigOptionFloats ();
} else if (Options[opt_key].type == coInt) { } else if (optdef->type == coInt) {
opt = new ConfigOptionInt (); opt = new ConfigOptionInt ();
} else if (Options[opt_key].type == coInts) { } else if (optdef->type == coInts) {
opt = new ConfigOptionInts (); opt = new ConfigOptionInts ();
} else if (Options[opt_key].type == coString) { } else if (optdef->type == coString) {
opt = new ConfigOptionString (); opt = new ConfigOptionString ();
} else if (Options[opt_key].type == coFloatOrPercent) { } else if (optdef->type == coFloatOrPercent) {
opt = new ConfigOptionFloatOrPercent (); opt = new ConfigOptionFloatOrPercent ();
} else if (Options[opt_key].type == coPoint) { } else if (optdef->type == coPoint) {
opt = new ConfigOptionPoint (); opt = new ConfigOptionPoint ();
} else if (Options[opt_key].type == coPoints) { } else if (optdef->type == coPoints) {
opt = new ConfigOptionPoints (); opt = new ConfigOptionPoints ();
} else if (Options[opt_key].type == coBool) { } else if (optdef->type == coBool) {
opt = new ConfigOptionBool (); opt = new ConfigOptionBool ();
} else if (Options[opt_key].type == coBools) { } else if (optdef->type == coBools) {
opt = new ConfigOptionBools (); opt = new ConfigOptionBools ();
} else if (Options[opt_key].type == coEnumGCodeFlavor) { } else if (optdef->type == coEnum) {
opt = new ConfigOptionEnumGCodeFlavor (); ConfigOptionEnumGeneric* optv = new ConfigOptionEnumGeneric ();
optv->keys_map = &optdef->enum_keys_map;
opt = static_cast<ConfigOption*>(optv);
} else { } else {
throw "Unknown option type"; throw "Unknown option type";
} }
@ -206,7 +198,7 @@ DynamicConfig::option(const t_config_option_key opt_key, bool create) {
return NULL; return NULL;
} }
} }
return it->second; return this->options[opt_key];
} }
void void
@ -217,13 +209,12 @@ DynamicConfig::keys(t_config_option_keys *keys) {
bool bool
DynamicConfig::has(const t_config_option_key opt_key) const { DynamicConfig::has(const t_config_option_key opt_key) const {
t_options_map::const_iterator it = this->options.find(opt_key); return (this->options.count(opt_key) != 0);
return (it != this->options.end());
} }
void void
StaticConfig::keys(t_config_option_keys *keys) { StaticConfig::keys(t_config_option_keys *keys) {
for (t_optiondef_map::const_iterator it = Options.begin(); it != Options.end(); ++it) { for (t_optiondef_map::const_iterator it = this->def->begin(); it != this->def->end(); ++it) {
ConfigOption* opt = this->option(it->first); ConfigOption* opt = this->option(it->first);
if (opt != NULL) keys->push_back(it->first); if (opt != NULL) keys->push_back(it->first);
} }

View File

@ -258,6 +258,8 @@ class ConfigOptionBools : public ConfigOption
}; };
}; };
typedef std::map<std::string,int> t_config_enum_values;
template <class T> template <class T>
class ConfigOptionEnum : public ConfigOption class ConfigOptionEnum : public ConfigOption
{ {
@ -267,41 +269,45 @@ class ConfigOptionEnum : public ConfigOption
operator T() const { return this->value; }; operator T() const { return this->value; };
std::string serialize() { std::string serialize() {
typename std::map<std::string,T> enum_keys_map = ConfigOptionEnum<T>::get_enum_values(); t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
for (typename std::map<std::string,T>::iterator it = enum_keys_map.begin(); it != enum_keys_map.end(); ++it) { for (t_config_enum_values::iterator it = enum_keys_map.begin(); it != enum_keys_map.end(); ++it) {
if (it->second == static_cast<int>(this->value)) return it->first;
}
return "";
};
void deserialize(std::string str) {
t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
assert(enum_keys_map.count(str) > 0);
this->value = static_cast<T>(enum_keys_map[str]);
};
static t_config_enum_values get_enum_values();
};
/* We use this one in DynamicConfig objects, otherwise it's better to use
the specialized ConfigOptionEnum<T> containers. */
class ConfigOptionEnumGeneric : public ConfigOption
{
public:
int value;
t_config_enum_values* keys_map;
operator int() const { return this->value; };
std::string serialize() {
for (t_config_enum_values::iterator it = this->keys_map->begin(); it != this->keys_map->end(); ++it) {
if (it->second == this->value) return it->first; if (it->second == this->value) return it->first;
} }
return ""; return "";
}; };
void deserialize(std::string str) { void deserialize(std::string str) {
typename std::map<std::string,T> enum_keys_map = ConfigOptionEnum<T>::get_enum_values(); assert(this->keys_map->count(str) != 0);
assert(enum_keys_map.count(str) > 0); this->value = (*this->keys_map)[str];
this->value = enum_keys_map[str];
}; };
static std::map<std::string,T> get_enum_values();
}; };
enum GCodeFlavor {
gcfRepRap, gcfTeacup, gcfMakerWare, gcfSailfish, gcfMach3, gcfNoExtrusion,
};
typedef ConfigOptionEnum<GCodeFlavor> ConfigOptionEnumGCodeFlavor;
// we declare this as inline to keep it in this file along with all other option definitions
template<> inline std::map<std::string,GCodeFlavor> ConfigOptionEnum<GCodeFlavor>::get_enum_values() {
std::map<std::string,GCodeFlavor> keys_map;
keys_map["reprap"] = gcfRepRap;
keys_map["teacup"] = gcfTeacup;
keys_map["makerware"] = gcfMakerWare;
keys_map["sailfish"] = gcfSailfish;
keys_map["mach3"] = gcfMach3;
keys_map["no-extrusion"] = gcfNoExtrusion;
return keys_map;
}
enum ConfigOptionType { enum ConfigOptionType {
coFloat, coFloat,
coFloats, coFloats,
@ -313,7 +319,7 @@ enum ConfigOptionType {
coPoints, coPoints,
coBool, coBool,
coBools, coBools,
coEnumGCodeFlavor, coEnum,
}; };
class ConfigOptionDef class ConfigOptionDef
@ -323,6 +329,7 @@ class ConfigOptionDef
std::string label; std::string label;
std::string tooltip; std::string tooltip;
std::string ratio_over; std::string ratio_over;
t_config_enum_values enum_keys_map;
}; };
typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map; typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map;
@ -332,6 +339,9 @@ ConfigOptionDef* get_config_option_def(const t_config_option_key opt_key);
class ConfigBase class ConfigBase
{ {
public: public:
t_optiondef_map* def;
ConfigBase() : def(NULL) {};
virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0; virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0;
virtual void keys(t_config_option_keys *keys) = 0; virtual void keys(t_config_option_keys *keys) = 0;
void apply(ConfigBase &other, bool ignore_nonexistent = false); void apply(ConfigBase &other, bool ignore_nonexistent = false);
@ -348,7 +358,7 @@ class ConfigBase
class DynamicConfig : public ConfigBase class DynamicConfig : public ConfigBase
{ {
public: public:
DynamicConfig() {}; DynamicConfig(){};
~DynamicConfig(); ~DynamicConfig();
ConfigOption* option(const t_config_option_key opt_key, bool create = false); ConfigOption* option(const t_config_option_key opt_key, bool create = false);
void keys(t_config_option_keys *keys); void keys(t_config_option_keys *keys);
@ -367,95 +377,6 @@ class StaticConfig : public ConfigBase
void keys(t_config_option_keys *keys); void keys(t_config_option_keys *keys);
}; };
class FullConfig : public StaticConfig
{
public:
ConfigOptionFloat layer_height;
ConfigOptionFloatOrPercent first_layer_height;
ConfigOptionInt perimeters;
ConfigOptionString extrusion_axis;
ConfigOptionPoint print_center;
ConfigOptionPoints extruder_offset;
ConfigOptionString notes;
ConfigOptionBool use_relative_e_distances;
ConfigOptionEnumGCodeFlavor gcode_flavor;
ConfigOptionFloats nozzle_diameter;
ConfigOptionInts temperature;
ConfigOptionBools wipe;
ConfigOption* option(const t_config_option_key opt_key, bool create = false) {
assert(!create); // can't create options in StaticConfig
if (opt_key == "layer_height") return &this->layer_height;
if (opt_key == "first_layer_height") return &this->first_layer_height;
if (opt_key == "perimeters") return &this->perimeters;
if (opt_key == "extrusion_axis") return &this->extrusion_axis;
if (opt_key == "print_center") return &this->print_center;
if (opt_key == "extruder_offset") return &this->extruder_offset;
if (opt_key == "notes") return &this->notes;
if (opt_key == "use_relative_e_distances") return &this->use_relative_e_distances;
if (opt_key == "gcode_flavor") return &this->gcode_flavor;
if (opt_key == "nozzle_diameter") return &this->nozzle_diameter;
if (opt_key == "temperature") return &this->temperature;
if (opt_key == "wipe") return &this->wipe;
return NULL;
};
};
static t_optiondef_map _build_optiondef_map () {
t_optiondef_map Options;
Options["layer_height"].type = coFloat;
Options["layer_height"].label = "Layer height";
Options["layer_height"].tooltip = "This setting controls the height (and thus the total number) of the slices/layers. Thinner layers give better accuracy but take more time to print.";
Options["first_layer_height"].type = coFloatOrPercent;
Options["first_layer_height"].ratio_over = "layer_height";
Options["perimeters"].type = coInt;
Options["perimeters"].label = "Perimeters (minimum)";
Options["perimeters"].tooltip = "This option sets the number of perimeters to generate for each layer. Note that Slic3r may increase this number automatically when it detects sloping surfaces which benefit from a higher number of perimeters if the Extra Perimeters option is enabled.";
Options["extrusion_axis"].type = coString;
Options["print_center"].type = coPoint;
Options["extruder_offset"].type = coPoints;
Options["notes"].type = coString;
Options["use_relative_e_distances"].type = coBool;
Options["gcode_flavor"].type = coEnumGCodeFlavor;
Options["nozzle_diameter"].type = coFloats;
Options["temperature"].type = coInts;
Options["wipe"].type = coBools;
return Options;
}
static FullConfig _build_default_config () {
FullConfig defconf;
defconf.layer_height.value = 0.4;
defconf.first_layer_height.value = 0.35;
defconf.first_layer_height.percent = false;
defconf.perimeters.value = 3;
defconf.extrusion_axis.value = "E";
defconf.print_center.point = Pointf(100,100);
defconf.extruder_offset.points.push_back(Pointf(0,0));
defconf.notes.value = "";
defconf.use_relative_e_distances.value = false;
defconf.gcode_flavor.value = gcfRepRap;
defconf.nozzle_diameter.values.push_back(0.5);
defconf.temperature.values.push_back(200);
defconf.wipe.values.push_back(true);
return defconf;
}
} }
#endif #endif

7
xs/src/PrintConfig.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "PrintConfig.hpp"
namespace Slic3r {
t_optiondef_map PrintConfig::PrintConfigDef = PrintConfig::build_def();
}

123
xs/src/PrintConfig.hpp Normal file
View File

@ -0,0 +1,123 @@
#ifndef slic3r_PrintConfig_hpp_
#define slic3r_PrintConfig_hpp_
#include "Config.hpp"
namespace Slic3r {
enum GCodeFlavor {
gcfRepRap, gcfTeacup, gcfMakerWare, gcfSailfish, gcfMach3, gcfNoExtrusion,
};
template<> inline t_config_enum_values ConfigOptionEnum<GCodeFlavor>::get_enum_values() {
t_config_enum_values keys_map;
keys_map["reprap"] = gcfRepRap;
keys_map["teacup"] = gcfTeacup;
keys_map["makerware"] = gcfMakerWare;
keys_map["sailfish"] = gcfSailfish;
keys_map["mach3"] = gcfMach3;
keys_map["no-extrusion"] = gcfNoExtrusion;
return keys_map;
}
class PrintConfig : public StaticConfig
{
public:
static t_optiondef_map PrintConfigDef;
ConfigOptionFloat layer_height;
ConfigOptionFloatOrPercent first_layer_height;
ConfigOptionInt perimeters;
ConfigOptionString extrusion_axis;
ConfigOptionPoint print_center;
ConfigOptionPoints extruder_offset;
ConfigOptionString notes;
ConfigOptionBool use_relative_e_distances;
ConfigOptionEnum<GCodeFlavor> gcode_flavor;
ConfigOptionFloats nozzle_diameter;
ConfigOptionInts temperature;
ConfigOptionBools wipe;
PrintConfig() {
this->def = &PrintConfig::PrintConfigDef;
this->layer_height.value = 0.4;
this->first_layer_height.value = 0.35;
this->first_layer_height.percent = false;
this->perimeters.value = 3;
this->extrusion_axis.value = "E";
this->print_center.point = Pointf(100,100);
this->extruder_offset.points.push_back(Pointf(0,0));
this->notes.value = "";
this->use_relative_e_distances.value = false;
this->gcode_flavor.value = gcfRepRap;
this->nozzle_diameter.values.push_back(0.5);
this->temperature.values.push_back(200);
this->wipe.values.push_back(true);
};
ConfigOption* option(const t_config_option_key opt_key, bool create = false) {
assert(!create); // can't create options in StaticConfig
if (opt_key == "layer_height") return &this->layer_height;
if (opt_key == "first_layer_height") return &this->first_layer_height;
if (opt_key == "perimeters") return &this->perimeters;
if (opt_key == "extrusion_axis") return &this->extrusion_axis;
if (opt_key == "print_center") return &this->print_center;
if (opt_key == "extruder_offset") return &this->extruder_offset;
if (opt_key == "notes") return &this->notes;
if (opt_key == "use_relative_e_distances") return &this->use_relative_e_distances;
if (opt_key == "gcode_flavor") return &this->gcode_flavor;
if (opt_key == "nozzle_diameter") return &this->nozzle_diameter;
if (opt_key == "temperature") return &this->temperature;
if (opt_key == "wipe") return &this->wipe;
return NULL;
};
static t_optiondef_map build_def () {
t_optiondef_map Options;
Options["layer_height"].type = coFloat;
Options["layer_height"].label = "Layer height";
Options["layer_height"].tooltip = "This setting controls the height (and thus the total number) of the slices/layers. Thinner layers give better accuracy but take more time to print.";
Options["first_layer_height"].type = coFloatOrPercent;
Options["first_layer_height"].ratio_over = "layer_height";
Options["perimeters"].type = coInt;
Options["perimeters"].label = "Perimeters (minimum)";
Options["perimeters"].tooltip = "This option sets the number of perimeters to generate for each layer. Note that Slic3r may increase this number automatically when it detects sloping surfaces which benefit from a higher number of perimeters if the Extra Perimeters option is enabled.";
Options["extrusion_axis"].type = coString;
Options["print_center"].type = coPoint;
Options["extruder_offset"].type = coPoints;
Options["notes"].type = coString;
Options["use_relative_e_distances"].type = coBool;
Options["gcode_flavor"].type = coEnum;
Options["gcode_flavor"].enum_keys_map = ConfigOptionEnum<GCodeFlavor>::get_enum_values();
Options["nozzle_diameter"].type = coFloats;
Options["temperature"].type = coInts;
Options["wipe"].type = coBools;
return Options;
};
};
class DynamicPrintConfig : public DynamicConfig
{
public:
DynamicPrintConfig() {
this->def = &PrintConfig::PrintConfigDef;
};
};
}
#endif

View File

@ -4,7 +4,7 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 32; use Test::More tests => 33;
{ {
my $config = Slic3r::Config->new; my $config = Slic3r::Config->new;
@ -48,6 +48,8 @@ use Test::More tests => 32;
$config->set('gcode_flavor', 'teacup'); $config->set('gcode_flavor', 'teacup');
is $config->get('gcode_flavor'), 'teacup', 'set/get enum'; is $config->get('gcode_flavor'), 'teacup', 'set/get enum';
is $config->serialize('gcode_flavor'), 'teacup', 'serialize enum'; is $config->serialize('gcode_flavor'), 'teacup', 'serialize enum';
$config->set_deserialize('gcode_flavor', 'mach3');
is $config->get('gcode_flavor'), 'mach3', 'deserialize enum';
$config->set('extruder_offset', [[10,20],[30,45]]); $config->set('extruder_offset', [[10,20],[30,45]]);
is_deeply $config->get('extruder_offset'), [[10,20],[30,45]], 'set/get points'; is_deeply $config->get('extruder_offset'), [[10,20],[30,45]], 'set/get points';

View File

@ -2,12 +2,12 @@
%{ %{
#include <myinit.h> #include <myinit.h>
#include "Config.hpp" #include "PrintConfig.hpp"
%} %}
%name{Slic3r::Config} class DynamicConfig { %name{Slic3r::Config} class DynamicPrintConfig {
DynamicConfig(); DynamicPrintConfig();
~DynamicConfig(); ~DynamicPrintConfig();
SV* get(t_config_option_key opt_key); SV* get(t_config_option_key opt_key);
void set(t_config_option_key opt_key, SV* value); void set(t_config_option_key opt_key, SV* value);
void set_deserialize(t_config_option_key opt_key, std::string str); void set_deserialize(t_config_option_key opt_key, std::string str);

View File

@ -1,7 +1,7 @@
std::vector<Points::size_type> T_STD_VECTOR_INT std::vector<Points::size_type> T_STD_VECTOR_INT
t_config_option_key T_STD_STRING t_config_option_key T_STD_STRING
DynamicConfig* O_OBJECT DynamicPrintConfig* O_OBJECT
ZTable* O_OBJECT ZTable* O_OBJECT
TriangleMesh* O_OBJECT TriangleMesh* O_OBJECT
Point* O_OBJECT Point* O_OBJECT

View File

@ -7,7 +7,7 @@
%typemap{SV*}; %typemap{SV*};
%typemap{AV*}; %typemap{AV*};
%typemap{Point*}; %typemap{Point*};
%typemap{DynamicConfig*}; %typemap{DynamicPrintConfig*};
%typemap{ExPolygon*}; %typemap{ExPolygon*};
%typemap{ExPolygonCollection*}; %typemap{ExPolygonCollection*};
%typemap{Line*}; %typemap{Line*};