#1803 Use abstract GeometrySerializer as cache to eliminate cyclic dependencies

This commit is contained in:
Thomas Krijnen
2021-10-18 11:38:01 +02:00
parent 4577b7bf60
commit bdc6fcb66f
14 changed files with 54 additions and 26 deletions
+3 -3
View File
@@ -43,13 +43,13 @@
#include "../ifcgeom_schema_agnostic/IfcGeomIterator.h"
#include "../serializers/GeometrySerializer.h"
#include "../ifcgeom_schema_agnostic/GeometrySerializer.h"
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
class ColladaSerializer : public GeometrySerializer
class ColladaSerializer : public WriteOnlyGeometrySerializer
{
// TODO The vast amount of implement details of ColladaSerializer could be hidden to the cpp file.
private:
@@ -219,7 +219,7 @@ private:
float unit_magnitude;
public:
ColladaSerializer(const std::string& dae_filename, const SerializerSettings& settings)
: GeometrySerializer(settings)
: WriteOnlyGeometrySerializer(settings)
, exporter("IfcOpenShell", dae_filename, this, settings.precision >= 15)
{
exporter.serializer = this;
-128
View File
@@ -1,128 +0,0 @@
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef GEOMETRYSERIALIZER_H
#define GEOMETRYSERIALIZER_H
#include "../serializers/Serializer.h"
#include "../ifcgeom/IfcGeomElement.h"
class SerializerSettings : public IfcGeom::IteratorSettings
{
public:
enum Setting : uint64_t
{
/// 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),
/// Use element types instead of unique IDs for naming elements.
/// Applicable for DAE output.
USE_ELEMENT_TYPES = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 4),
/// Order the elements using their IfcBuildingStorey parent
/// Applicable for DAE output
USE_ELEMENT_HIERARCHY = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 5),
/// Use step ids for naming elements.
/// Applicable for OBJ, DAE, and SVG output.
USE_ELEMENT_STEPIDS = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 6),
/// Use Y UP .
/// Applicable for OBJ output.
USE_Y_UP = 1 << (IfcGeom::IteratorSettings::NUM_SETTINGS + 7),
/// Number of different setting flags.
NUM_SETTINGS = 7
};
SerializerSettings()
: precision(DEFAULT_PRECISION) { }
/// Sets the precision used to format floating-point values, 15 by default.
/// Use a negative value to use the system's default precision (should be 6 typically).
short precision;
enum { DEFAULT_PRECISION = 15 };
};
class stream_or_filename {
private:
std::shared_ptr<std::ofstream> ofs_;
std::shared_ptr<std::ostringstream> oss_;
boost::optional<std::string> filename_;
public:
std::ostream& stream;
stream_or_filename(const std::string& fn)
: ofs_(new std::ofstream(IfcUtil::path::from_utf8(fn).c_str()))
, stream(*ofs_)
{}
stream_or_filename()
: oss_(new std::ostringstream)
, stream(*oss_)
{}
std::string get_value() const {
return oss_->str();
}
boost::optional<std::string> filename() const {
return filename_;
}
bool is_ready() {
if (ofs_) {
return ofs_->is_open();
} else {
return true;
}
}
};
class GeometrySerializer : public Serializer {
public:
GeometrySerializer(const SerializerSettings& settings) : settings_(settings) {}
virtual ~GeometrySerializer() {}
virtual bool isTesselated() const = 0;
virtual void write(const IfcGeom::TriangulationElement* o) = 0;
virtual void write(const IfcGeom::BRepElement* o) = 0;
virtual void setUnitNameAndMagnitude(const std::string& name, float magnitude) = 0;
const SerializerSettings& settings() const { return settings_; }
SerializerSettings& settings() { return settings_; }
/// Returns ID for the object depending on the used setting.
virtual std::string object_id(const IfcGeom::Element* o)
{
if (settings_.get(SerializerSettings::USE_ELEMENT_GUIDS)) return o->guid();
if (settings_.get(SerializerSettings::USE_ELEMENT_NAMES)) return o->name();
if (settings_.get(SerializerSettings::USE_ELEMENT_STEPIDS)) return "id-" + boost::lexical_cast<std::string>(o->id());
return o->unique_id();
}
protected:
SerializerSettings settings_;
};
#endif
+1 -1
View File
@@ -45,7 +45,7 @@ static const uint32_t PRIM_TRIANGLE_STRIP = 5;
static const uint32_t PRIM_TRIANGLE_FAN = 6;
GltfSerializer::GltfSerializer(const std::string& filename, const SerializerSettings& settings)
: GeometrySerializer(settings)
: WriteOnlyGeometrySerializer(settings)
, filename_(filename)
, tmp_filename1_(filename + ".indices.tmp")
, tmp_filename2_(filename + ".vertices.tmp")
+2 -2
View File
@@ -22,14 +22,14 @@
#ifdef WITH_GLTF
#include "../serializers/GeometrySerializer.h"
#include "../ifcgeom_schema_agnostic/GeometrySerializer.h"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#include <map>
class GltfSerializer : public GeometrySerializer {
class GltfSerializer : public WriteOnlyGeometrySerializer {
private:
std::string filename_, tmp_filename1_, tmp_filename2_;
std::ofstream fstream_, tmp_fstream1_, tmp_fstream2_;
+1 -4
View File
@@ -28,14 +28,11 @@
#include "H5Cpp.h"
#include "../serializers/GeometrySerializer.h"
#include "../ifcgeom_schema_agnostic/GeometrySerializer.h"
#define USE_BINARY
class HdfSerializer : public GeometrySerializer {
public:
enum read_type { READ_BREP, READ_TRIANGULATION };
private:
const std::string hdf_filename;
unsigned int vcount_total;
+3 -3
View File
@@ -22,9 +22,9 @@
#include "../ifcgeom_schema_agnostic/IfcGeomIterator.h"
#include "../serializers/GeometrySerializer.h"
#include "../ifcgeom_schema_agnostic/GeometrySerializer.h"
class OpenCascadeBasedSerializer : public GeometrySerializer {
class OpenCascadeBasedSerializer : public WriteOnlyGeometrySerializer {
OpenCascadeBasedSerializer(const OpenCascadeBasedSerializer&); //N/A
OpenCascadeBasedSerializer& operator =(const OpenCascadeBasedSerializer&); //N/A
protected:
@@ -32,7 +32,7 @@ protected:
const char* getSymbolForUnitMagnitude(float mag);
public:
explicit OpenCascadeBasedSerializer(const std::string& out_filename, const SerializerSettings& settings)
: GeometrySerializer(settings)
: WriteOnlyGeometrySerializer(settings)
, out_filename(out_filename)
{}
virtual ~OpenCascadeBasedSerializer() {}
-35
View File
@@ -1,35 +0,0 @@
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef SERIALIZER_H
#define SERIALIZER_H
#include "../ifcparse/IfcFile.h"
class Serializer {
public:
virtual ~Serializer() {}
virtual bool ready() = 0;
virtual void writeHeader() = 0;
virtual void finalize() = 0;
virtual void setFile(IfcParse::IfcFile*) = 0;
};
#endif
+3 -3
View File
@@ -22,7 +22,7 @@
#ifndef SVGSERIALIZER_H
#define SVGSERIALIZER_H
#include "../serializers/GeometrySerializer.h"
#include "../ifcgeom_schema_agnostic/GeometrySerializer.h"
#include "../serializers/util.h"
#include "../ifcparse/utils.h"
@@ -129,7 +129,7 @@ typedef boost::variant<
Handle(HLRBRep_PolyAlgo)
> hlr_t;
class SvgSerializer : public GeometrySerializer {
class SvgSerializer : public WriteOnlyGeometrySerializer {
public:
typedef std::pair<std::string, std::vector<util::string_buffer> > path_object;
typedef std::vector< boost::shared_ptr<util::string_buffer::float_item> > float_item_list;
@@ -184,7 +184,7 @@ protected:
public:
SvgSerializer(const stream_or_filename& out_filename, const SerializerSettings& settings)
: GeometrySerializer(settings)
: WriteOnlyGeometrySerializer(settings)
, svg_file(out_filename)
, xmin(+std::numeric_limits<double>::infinity())
, ymin(+std::numeric_limits<double>::infinity())
+1 -1
View File
@@ -28,7 +28,7 @@
#include <iomanip>
WaveFrontOBJSerializer::WaveFrontOBJSerializer(const stream_or_filename& obj_filename, const stream_or_filename& mtl_filename, const SerializerSettings& settings)
: GeometrySerializer(settings)
: WriteOnlyGeometrySerializer(settings)
, obj_stream(obj_filename)
, mtl_stream(mtl_filename)
, vcount_total(1)
+2 -2
View File
@@ -24,10 +24,10 @@
#include <string>
#include <fstream>
#include "../serializers/GeometrySerializer.h"
#include "../ifcgeom_schema_agnostic/GeometrySerializer.h"
// http://people.sc.fsu.edu/~jburkardt/txt/obj_format.txt
class WaveFrontOBJSerializer : public GeometrySerializer {
class WaveFrontOBJSerializer : public WriteOnlyGeometrySerializer {
private:
stream_or_filename obj_stream;
stream_or_filename mtl_stream;
+1 -1
View File
@@ -1,6 +1,6 @@
#define SCHEMA_METHOD
#include "../serializers/Serializer.h"
#include "../ifcgeom_schema_agnostic/Serializer.h"
#include "../ifcparse/IfcFile.h"
#include <boost/function.hpp>