Separate serialization-specific settings from IteratorSettings into its own class, SerializerSettings.

This commit is contained in:
Stinkfist0
2017-01-11 12:00:51 +02:00
committed by Thomas Krijnen
parent 5667f3f94a
commit 2d58a1bc7c
12 changed files with 63 additions and 49 deletions
+7 -7
View File
@@ -100,7 +100,7 @@ void ColladaSerializer::ColladaExporter::ColladaGeometries::write(
const size_t num_triangles = std::distance(index_range_start, it) / 3; const size_t num_triangles = std::distance(index_range_start, it) / 3;
if ((previous_material_id != current_material_id && num_triangles > 0) || (it == faces.end())) { if ((previous_material_id != current_material_id && num_triangles > 0) || (it == faces.end())) {
COLLADASW::Triangles triangles(mSW); COLLADASW::Triangles triangles(mSW);
std::string material_name = (serializer->settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (serializer->settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? materials[previous_material_id].original_name() : materials[previous_material_id].name()); ? materials[previous_material_id].original_name() : materials[previous_material_id].name());
collada_id(material_name); collada_id(material_name);
triangles.setMaterial(material_name); triangles.setMaterial(material_name);
@@ -157,7 +157,7 @@ void ColladaSerializer::ColladaExporter::ColladaGeometries::write(
for (linelist_t::const_iterator it = linelist.begin(); it != linelist.end(); ++it) { for (linelist_t::const_iterator it = linelist.begin(); it != linelist.end(); ++it) {
COLLADASW::Lines lines(mSW); COLLADASW::Lines lines(mSW);
std::string material_name = (serializer->settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (serializer->settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? materials[it->first].original_name() : materials[it->first].name()); ? materials[it->first].original_name() : materials[it->first].name());
collada_id(material_name); collada_id(material_name);
lines.setMaterial(material_name); lines.setMaterial(material_name);
@@ -230,7 +230,7 @@ void ColladaSerializer::ColladaExporter::ColladaScene::write() {
void ColladaSerializer::ColladaExporter::ColladaMaterials::ColladaEffects::write(const IfcGeom::Material& material) void ColladaSerializer::ColladaExporter::ColladaMaterials::ColladaEffects::write(const IfcGeom::Material& material)
{ {
std::string material_name = (serializer->settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (serializer->settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? material.original_name() : material.name()); ? material.original_name() : material.name());
collada_id(material_name); collada_id(material_name);
openEffect(material_name + "-fx"); openEffect(material_name + "-fx");
@@ -277,7 +277,7 @@ bool ColladaSerializer::ColladaExporter::ColladaMaterials::contains(const IfcGeo
void ColladaSerializer::ColladaExporter::ColladaMaterials::write() { void ColladaSerializer::ColladaExporter::ColladaMaterials::write() {
effects.close(); effects.close();
foreach(const IfcGeom::Material& material, materials) { foreach(const IfcGeom::Material& material, materials) {
std::string material_name = (serializer->settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (serializer->settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? material.original_name() : material.name()); ? material.original_name() : material.name());
std::string material_name_unescaped = material_name; // workaround double-escaping that would occur in addInstanceEffect() std::string material_name_unescaped = material_name; // workaround double-escaping that would occur in addInstanceEffect()
IfcUtil::sanitate_material_name(material_name_unescaped); IfcUtil::sanitate_material_name(material_name_unescaped);
@@ -302,8 +302,8 @@ void ColladaSerializer::ColladaExporter::startDocument(const std::string& unit_n
void ColladaSerializer::ColladaExporter::write(const IfcGeom::TriangulationElement<real_t>* o) void ColladaSerializer::ColladaExporter::write(const IfcGeom::TriangulationElement<real_t>* o)
{ {
const IfcGeom::Representation::Triangulation<real_t>& mesh = o->geometry(); const IfcGeom::Representation::Triangulation<real_t>& mesh = o->geometry();
const std::string name = serializer->settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_GUIDS) ? const std::string name = serializer->settings().get(SerializerSettings::USE_ELEMENT_GUIDS) ?
o->guid() : (serializer->settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_NAMES) ? o->name() : o->unique_id()); o->guid() : (serializer->settings().get(SerializerSettings::USE_ELEMENT_NAMES) ? o->name() : o->unique_id());
const std::string representation_id = "representation-" + boost::lexical_cast<std::string>(o->geometry().id()); const std::string representation_id = "representation-" + boost::lexical_cast<std::string>(o->geometry().id());
std::vector<std::string> material_references; std::vector<std::string> material_references;
@@ -311,7 +311,7 @@ void ColladaSerializer::ColladaExporter::write(const IfcGeom::TriangulationEleme
if (!materials.contains(material)) { if (!materials.contains(material)) {
materials.add(material); materials.add(material);
} }
std::string material_name = (serializer->settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (serializer->settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? material.original_name() : material.name()); ? material.original_name() : material.name());
collada_id(material_name); collada_id(material_name);
material_references.push_back(material_name); material_references.push_back(material_name);
+1 -1
View File
@@ -176,7 +176,7 @@ private:
std::string unit_name; std::string unit_name;
float unit_magnitude; float unit_magnitude;
public: public:
ColladaSerializer(const std::string& dae_filename, const IfcGeom::IteratorSettings &settings) ColladaSerializer(const std::string& dae_filename, const SerializerSettings& settings)
: GeometrySerializer(settings) : GeometrySerializer(settings)
, exporter("IfcOpenShell", dae_filename, this) , exporter("IfcOpenShell", dae_filename, this)
{ {
+34 -4
View File
@@ -29,9 +29,39 @@ typedef float real_t;
#include "../ifcconvert/Serializer.h" #include "../ifcconvert/Serializer.h"
#include "../ifcgeom/IfcGeomIterator.h" #include "../ifcgeom/IfcGeomIterator.h"
class SerializerSettings : public IfcGeom::IteratorSettings
{
public:
enum Setting
{
/// Use entity names instead of unique IDs for naming elements.
/// Applicable for OBJ, DAE, and SVG output.
USE_ELEMENT_NAMES = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 1),
/// Use entity GUIDs instead of unique IDs for naming elements.
/// Applicable for OBJ, DAE, and SVG output.
USE_ELEMENT_GUIDS = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 2),
/// Use material names instead of unique IDs for naming materials.
/// Applicable for OBJ and DAE output.
USE_MATERIAL_NAMES = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 3),
/// Centers the models upon serialization by the applying the center point of
/// the scene bounds as an offset. Applicable for OBJ and DAE output currently.
CENTER_MODEL = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 4),
/// Number of different setting flags.
NUM_SETTINGS = 4
};
SerializerSettings()
{
memset(offset, 0, sizeof(offset));
}
/// Optional offset that is applied to serialized objects, (0,0,0) by default.
double offset[3];
};
class GeometrySerializer : public Serializer { class GeometrySerializer : public Serializer {
public: public:
GeometrySerializer(const IfcGeom::IteratorSettings &settings) : settings_(settings) {} GeometrySerializer(const SerializerSettings& settings) : settings_(settings) {}
virtual ~GeometrySerializer() {} virtual ~GeometrySerializer() {}
virtual bool isTesselated() const = 0; virtual bool isTesselated() const = 0;
@@ -39,11 +69,11 @@ public:
virtual void write(const IfcGeom::BRepElement<real_t>* o) = 0; virtual void write(const IfcGeom::BRepElement<real_t>* o) = 0;
virtual void setUnitNameAndMagnitude(const std::string& name, float magnitude) = 0; virtual void setUnitNameAndMagnitude(const std::string& name, float magnitude) = 0;
const IfcGeom::IteratorSettings& settings() const { return settings_; } const SerializerSettings& settings() const { return settings_; }
IfcGeom::IteratorSettings& settings() { return settings_; } SerializerSettings& settings() { return settings_; }
protected: protected:
IfcGeom::IteratorSettings settings_; SerializerSettings settings_;
}; };
#endif #endif
+5 -5
View File
@@ -363,7 +363,7 @@ int main(int argc, char** argv) {
return exit_code; return exit_code;
} }
IfcGeom::IteratorSettings settings; SerializerSettings settings;
/// @todo Make APPLY_DEFAULT_MATERIALS configurable? Quickly tested setting this to false and using obj exporter caused the program to crash and burn. /// @todo Make APPLY_DEFAULT_MATERIALS configurable? Quickly tested setting this to false and using obj exporter caused the program to crash and burn.
settings.set(IfcGeom::IteratorSettings::APPLY_DEFAULT_MATERIALS, true); settings.set(IfcGeom::IteratorSettings::APPLY_DEFAULT_MATERIALS, true);
settings.set(IfcGeom::IteratorSettings::USE_WORLD_COORDS, use_world_coords); settings.set(IfcGeom::IteratorSettings::USE_WORLD_COORDS, use_world_coords);
@@ -377,13 +377,13 @@ int main(int argc, char** argv) {
settings.set(IfcGeom::IteratorSettings::INCLUDE_CURVES, include_plan); settings.set(IfcGeom::IteratorSettings::INCLUDE_CURVES, include_plan);
settings.set(IfcGeom::IteratorSettings::EXCLUDE_SOLIDS_AND_SURFACES, !include_model); settings.set(IfcGeom::IteratorSettings::EXCLUDE_SOLIDS_AND_SURFACES, !include_model);
settings.set(IfcGeom::IteratorSettings::APPLY_LAYERSETS, enable_layerset_slicing); settings.set(IfcGeom::IteratorSettings::APPLY_LAYERSETS, enable_layerset_slicing);
settings.set(IfcGeom::IteratorSettings::USE_ELEMENT_NAMES, use_element_names);
settings.set(IfcGeom::IteratorSettings::USE_ELEMENT_GUIDS, use_element_guids);
settings.set(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES, use_material_names);
settings.set(IfcGeom::IteratorSettings::NO_NORMALS, no_normals); settings.set(IfcGeom::IteratorSettings::NO_NORMALS, no_normals);
settings.set(IfcGeom::IteratorSettings::CENTER_MODEL, center_model);
settings.set(IfcGeom::IteratorSettings::GENERATE_UVS, generate_uvs); settings.set(IfcGeom::IteratorSettings::GENERATE_UVS, generate_uvs);
settings.set(IfcGeom::IteratorSettings::TRAVERSE, traverse); settings.set(IfcGeom::IteratorSettings::TRAVERSE, traverse);
settings.set(SerializerSettings::USE_ELEMENT_NAMES, use_element_names);
settings.set(SerializerSettings::USE_ELEMENT_GUIDS, use_element_guids);
settings.set(SerializerSettings::USE_MATERIAL_NAMES, use_material_names);
settings.set(SerializerSettings::CENTER_MODEL, center_model);
if (deflection_tolerance_specified) { if (deflection_tolerance_specified) {
settings.set_deflection_tolerance(deflection_tolerance); settings.set_deflection_tolerance(deflection_tolerance);
} }
+1 -1
View File
@@ -32,7 +32,7 @@ private:
public: public:
/// @note IGESControl_Controller::Init() must be called prior to instantiating IgesSerializer. /// @note IGESControl_Controller::Init() must be called prior to instantiating IgesSerializer.
/// See http://tracker.dev.opencascade.org/view.php?id=23679 for more information. /// See http://tracker.dev.opencascade.org/view.php?id=23679 for more information.
IgesSerializer(const std::string& out_filename, const IfcGeom::IteratorSettings &settings) IgesSerializer(const std::string& out_filename, const SerializerSettings& settings)
: OpenCascadeBasedSerializer(out_filename, settings) : OpenCascadeBasedSerializer(out_filename, settings)
{} {}
virtual ~IgesSerializer() {} virtual ~IgesSerializer() {}
+1 -1
View File
@@ -31,7 +31,7 @@ protected:
const std::string out_filename; const std::string out_filename;
const char* getSymbolForUnitMagnitude(float mag); const char* getSymbolForUnitMagnitude(float mag);
public: public:
explicit OpenCascadeBasedSerializer(const std::string& out_filename, const IfcGeom::IteratorSettings &settings) explicit OpenCascadeBasedSerializer(const std::string& out_filename, const SerializerSettings& settings)
: GeometrySerializer(settings) : GeometrySerializer(settings)
, out_filename(out_filename) , out_filename(out_filename)
{} {}
+1 -1
View File
@@ -32,7 +32,7 @@ class StepSerializer : public OpenCascadeBasedSerializer
private: private:
STEPControl_Writer writer; STEPControl_Writer writer;
public: public:
explicit StepSerializer(const std::string& out_filename, const IfcGeom::IteratorSettings &settings) explicit StepSerializer(const std::string& out_filename, const SerializerSettings& settings)
: OpenCascadeBasedSerializer(out_filename, settings) : OpenCascadeBasedSerializer(out_filename, settings)
{} {}
virtual ~StepSerializer() {} virtual ~StepSerializer() {}
+2 -2
View File
@@ -333,8 +333,8 @@ std::string SvgSerializer::nameElement(const IfcGeom::Element<real_t>* elem)
{ {
std::ostringstream oss; std::ostringstream oss;
const std::string type = "product"; const std::string type = "product";
const std::string name = (settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_GUIDS) const std::string name = (settings().get(SerializerSettings::USE_ELEMENT_GUIDS)
? elem->guid() : (settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_NAMES) ? elem->guid() : (settings().get(SerializerSettings::USE_ELEMENT_NAMES)
? elem->name() : elem->unique_id())); ? elem->name() : elem->unique_id()));
oss << "id=\"" << type << "-" << name<< "\""; oss << "id=\"" << type << "-" << name<< "\"";
return oss.str(); return oss.str();
+1 -1
View File
@@ -43,7 +43,7 @@ protected:
std::vector< boost::shared_ptr<util::string_buffer::float_item> > radii; std::vector< boost::shared_ptr<util::string_buffer::float_item> > radii;
IfcParse::IfcFile* file; IfcParse::IfcFile* file;
public: public:
SvgSerializer(const std::string& out_filename, const IfcGeom::IteratorSettings &settings) SvgSerializer(const std::string& out_filename, const SerializerSettings& settings)
: GeometrySerializer(settings) : GeometrySerializer(settings)
, svg_file(out_filename.c_str()) , svg_file(out_filename.c_str())
, xmin(+std::numeric_limits<double>::infinity()) , xmin(+std::numeric_limits<double>::infinity())
+5 -5
View File
@@ -47,7 +47,7 @@ void WaveFrontOBJSerializer::writeHeader() {
void WaveFrontOBJSerializer::writeMaterial(const IfcGeom::Material& style) void WaveFrontOBJSerializer::writeMaterial(const IfcGeom::Material& style)
{ {
std::string material_name = (settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? style.original_name() : style.name()); ? style.original_name() : style.name());
IfcUtil::sanitate_material_name(material_name); IfcUtil::sanitate_material_name(material_name);
mtl_stream << "newmtl " << material_name << "\n"; mtl_stream << "newmtl " << material_name << "\n";
@@ -72,8 +72,8 @@ void WaveFrontOBJSerializer::writeMaterial(const IfcGeom::Material& style)
void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement<real_t>* o) void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement<real_t>* o)
{ {
const std::string name = (settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_GUIDS) const std::string name = (settings().get(SerializerSettings::USE_ELEMENT_GUIDS)
? o->guid() : (settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_NAMES) ? o->guid() : (settings().get(SerializerSettings::USE_ELEMENT_NAMES)
? o->name() : o->unique_id())); ? o->name() : o->unique_id()));
obj_stream << "g " << name << "\n"; obj_stream << "g " << name << "\n";
obj_stream << "s 1" << "\n"; obj_stream << "s 1" << "\n";
@@ -113,7 +113,7 @@ void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement<real_t>*
const int material_id = *(material_it++); const int material_id = *(material_it++);
if (material_id != previous_material_id) { if (material_id != previous_material_id) {
const IfcGeom::Material& material = mesh.materials()[material_id]; const IfcGeom::Material& material = mesh.materials()[material_id];
std::string material_name = (settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? material.original_name() : material.name()); ? material.original_name() : material.name());
IfcUtil::sanitate_material_name(material_name); IfcUtil::sanitate_material_name(material_name);
obj_stream << "usemtl " << material_name << "\n"; obj_stream << "usemtl " << material_name << "\n";
@@ -157,7 +157,7 @@ void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement<real_t>*
if (material_id != previous_material_id) { if (material_id != previous_material_id) {
const IfcGeom::Material& material = mesh.materials()[material_id]; const IfcGeom::Material& material = mesh.materials()[material_id];
std::string material_name = (settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES) std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? material.original_name() : material.name()); ? material.original_name() : material.name());
IfcUtil::sanitate_material_name(material_name); IfcUtil::sanitate_material_name(material_name);
obj_stream << "usemtl " << material_name << "\n"; obj_stream << "usemtl " << material_name << "\n";
+1 -1
View File
@@ -35,7 +35,7 @@ private:
unsigned int vcount_total; unsigned int vcount_total;
std::set<std::string> materials; std::set<std::string> materials;
public: public:
WaveFrontOBJSerializer(const std::string& obj_filename, const std::string& mtl_filename, const IfcGeom::IteratorSettings &settings) WaveFrontOBJSerializer(const std::string& obj_filename, const std::string& mtl_filename, const SerializerSettings& settings)
: GeometrySerializer(settings) : GeometrySerializer(settings)
, mtl_filename(mtl_filename) , mtl_filename(mtl_filename)
, obj_stream(obj_filename.c_str()) , obj_stream(obj_filename.c_str())
+4 -20
View File
@@ -73,29 +73,17 @@ namespace IfcGeom
/// in instances where you're going to recompute normals for the exported /// in instances where you're going to recompute normals for the exported
/// model in other modelling application in any case. /// model in other modelling application in any case.
NO_NORMALS = 1 << 11, NO_NORMALS = 1 << 11,
/// Use entity names instead of unique IDs for naming elements.
/// Applicable for OBJ, DAE, and SVG output.
USE_ELEMENT_NAMES = 1 << 12,
/// Use entity GUIDs instead of unique IDs for naming elements.
/// Applicable for OBJ, DAE, and SVG output.
USE_ELEMENT_GUIDS = 1 << 13,
/// Use material names instead of unique IDs for naming materials.
/// Applicable for OBJ and DAE output.
USE_MATERIAL_NAMES = 1 << 14,
/// Centers the models upon serialization by the applying the center point of
/// the scene bounds as an offset. Applicable for OBJ and DAE output currently.
CENTER_MODEL = 1 << 15,
/// Generates UVs by using simple box projection. Requires normals. /// Generates UVs by using simple box projection. Requires normals.
/// Applicable for OBJ and DAE output. /// Applicable for OBJ and DAE output.
GENERATE_UVS = 1 << 16, GENERATE_UVS = 1 << 12,
/// Specifies whether to slice representations according to associated IfcLayerSets. /// Specifies whether to slice representations according to associated IfcLayerSets.
APPLY_LAYERSETS = 1 << 17, APPLY_LAYERSETS = 1 << 13,
/// Marks that include/exclude filtering should be applied also to the decomposition /// Marks that include/exclude filtering should be applied also to the decomposition
/// and/or containment (IsDecomposedBy, HasOpenings, FillsVoid, ContainedInStructure) /// and/or containment (IsDecomposedBy, HasOpenings, FillsVoid, ContainedInStructure)
/// of the filtered entity. /// of the filtered entity.
TRAVERSE = 1 << 18, TRAVERSE = 1 << 14,
/// Number of different setting flags. /// Number of different setting flags.
NUM_SETTINGS = 18 NUM_SETTINGS = 14
}; };
/// Used to store logical OR combination of setting flags. /// Used to store logical OR combination of setting flags.
typedef unsigned SettingField; typedef unsigned SettingField;
@@ -104,12 +92,8 @@ namespace IfcGeom
: settings_(WELD_VERTICES) // OR options that default to true here : settings_(WELD_VERTICES) // OR options that default to true here
, deflection_tolerance_(1.e-3) , deflection_tolerance_(1.e-3)
{ {
memset(offset, 0, sizeof(offset));
} }
/// Optional offset that is applied to serialized objects, (0,0,0) by default.
double offset[3];
/// Note that this is independent of the IFC length unit, one millimeter by default. /// Note that this is independent of the IFC length unit, one millimeter by default.
double deflection_tolerance() const { return deflection_tolerance_; } double deflection_tolerance() const { return deflection_tolerance_; }