From baf230dceef581b150c245eab526e401d074f88e Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 5 Dec 2022 18:35:50 +0100 Subject: [PATCH] Base: refactor NodeItem --- src/Base/Builder3D.cpp | 121 ++++++++++++++++++++++++----------------- src/Base/Builder3D.h | 6 +- 2 files changed, 71 insertions(+), 56 deletions(-) diff --git a/src/Base/Builder3D.cpp b/src/Base/Builder3D.cpp index 56d331ac1c..f2d77f8c8f 100644 --- a/src/Base/Builder3D.cpp +++ b/src/Base/Builder3D.cpp @@ -130,6 +130,11 @@ InventorOutput::InventorOutput(std::ostream& result, Indentation& indent) { } +std::ostream& InventorOutput::stream() +{ + return result; +} + std::ostream& InventorOutput::write() { result << indent; @@ -178,61 +183,68 @@ void InventorOutput::decreaseIndent() // ----------------------------------------------------------------------------- -void NodeItem::writeField(const char* field, const std::vector& vec, InventorOutput& out) const +namespace Base { +template +struct field_traits { }; + +template <> +struct field_traits { + using field_type = float; + static std::ostream& write(std::ostream& out, const field_type& item) { + out << item; + return out; + } +}; + +template <> +struct field_traits { + using field_type = Vector3f; + static std::ostream& write(std::ostream& out, const field_type& item) { + out << item.x << " " << item.y << " " << item.z; + return out; + } +}; + +template <> +struct field_traits { + using field_type = ColorRGB; + static std::ostream& write(std::ostream& out, const field_type& item) { + out << item.red() << " " << item.green() << " " << item.blue(); + return out; + } +}; + +/** + * Writes a field type to a stream. + * @author Werner Mayer + */ +class InventorFieldWriter { +public: + template + void write(const char* fieldName, const std::vector& fieldData, InventorOutput& out) const; +}; + +template +void InventorFieldWriter::write(const char* fieldName, const std::vector& fieldData, InventorOutput& out) const { - if (vec.empty()) + if (fieldData.empty()) return; - if (vec.size() == 1) { - out.write() << field << " " << vec[0].x << " " << vec[0].y << " " << vec[0].z << '\n'; + if (fieldData.size() == 1) { + out.write() << fieldName << " "; + field_traits::write(out.stream(), fieldData[0]) << '\n'; } else { - out.write() << field << " [\n"; + out.write() << fieldName << " [\n"; out.increaseIndent(); - for (auto it : vec) { - out.write() << it.x << " " << it.y << " " << it.z << '\n'; + for (auto it : fieldData) { + out.write(); + field_traits::write(out.stream(), it) << '\n'; } out.decreaseIndent(); out.write() << "]\n"; } } - -void NodeItem::writeField(const char* field, const std::vector& rgb, InventorOutput& out) const -{ - if (rgb.empty()) - return; - - if (rgb.size() == 1) { - out.write() << field << " " << rgb[0].red() << " " << rgb[0].green() << " " << rgb[0].blue() << '\n'; - } - else { - out.write() << field << " [\n"; - out.increaseIndent(); - for (auto it : rgb) { - out.write() << it.red() << " " << it.green() << " " << it.blue() << '\n'; - } - out.decreaseIndent(); - out.write() << "]\n"; - } -} - -void NodeItem::writeField(const char* field, const std::vector& value, InventorOutput& out) const -{ - if (value.empty()) - return; - - if (value.size() == 1) { - out.write() << field << " " << value[0] << '\n'; - } - else { - out.write() << field << " [\n"; - out.increaseIndent(); - for (auto it : value) { - out.write() << it << '\n'; - } - out.decreaseIndent(); - out.write() << "]\n"; - } } // ----------------------------------------------------------------------------- @@ -437,32 +449,38 @@ void MaterialItem::endMaterial(InventorOutput& out) const void MaterialItem::writeAmbientColor(InventorOutput& out) const { - writeField("ambientColor", ambientColor, out); + InventorFieldWriter writer; + writer.write("ambientColor", ambientColor, out); } void MaterialItem::writeDiffuseColor(InventorOutput& out) const { - writeField("diffuseColor", diffuseColor, out); + InventorFieldWriter writer; + writer.write("diffuseColor", diffuseColor, out); } void MaterialItem::writeSpecularColor(InventorOutput& out) const { - writeField("specularColor", specularColor, out); + InventorFieldWriter writer; + writer.write("specularColor", specularColor, out); } void MaterialItem::writeEmissiveColor(InventorOutput& out) const { - writeField("emissiveColor", emissiveColor, out); + InventorFieldWriter writer; + writer.write("emissiveColor", emissiveColor, out); } void MaterialItem::writeShininess(InventorOutput& out) const { - writeField("shininess", shininess, out); + InventorFieldWriter writer; + writer.write("shininess", shininess, out); } void MaterialItem::writeTransparency(InventorOutput& out) const { - writeField("transparency", transparency, out); + InventorFieldWriter writer; + writer.write("transparency", transparency, out); } // ----------------------------------------------------------------------------- @@ -543,7 +561,8 @@ void NormalItem::setVector(const std::vector& vec) void NormalItem::write(InventorOutput& out) const { beginNormal(out); - writeField("vector", vector, out); + InventorFieldWriter writer; + writer.write("vector", vector, out); endNormal(out); } diff --git a/src/Base/Builder3D.h b/src/Base/Builder3D.h index 0124f3fc05..83587a1101 100644 --- a/src/Base/Builder3D.h +++ b/src/Base/Builder3D.h @@ -171,6 +171,7 @@ class BaseExport InventorOutput { public: explicit InventorOutput(std::ostream& result, Indentation& indent); + std::ostream& stream(); std::ostream& write(); std::ostream& write(const char*); std::ostream& write(const std::string&); @@ -190,11 +191,6 @@ class BaseExport NodeItem public: virtual ~NodeItem() = default; virtual void write(InventorOutput& out) const = 0; - -protected: - void writeField(const char* field, const std::vector& vec, InventorOutput& out) const; - void writeField(const char* field, const std::vector& rgb, InventorOutput& out) const; - void writeField(const char* field, const std::vector& value, InventorOutput& out) const; }; /*!