Base: refactoring of InventorBuilder

This commit is contained in:
wmayer
2022-11-29 15:20:43 +01:00
parent cf3225b50f
commit 9537b3e84c
4 changed files with 241 additions and 18 deletions

View File

@@ -124,13 +124,151 @@ const char* PolygonOffset::styleAsString() const
// -----------------------------------------------------------------------------
std::ostream& operator<<( std::ostream& os, InventorBuilder::Indentation m)
std::ostream& operator<<( std::ostream& os, Indentation m)
{
for (int i = 0; i < m.count(); i++)
os << " ";
return os;
}
InventorOutput::InventorOutput(std::ostream& result, Indentation& indent)
: result(result)
, indent(indent)
{
}
std::ostream& InventorOutput::write()
{
result << indent;
return result;
}
std::ostream& InventorOutput::write(const char* str)
{
result << indent << str;
return result;
}
std::ostream& InventorOutput::write(const std::string& str)
{
result << indent << str;
return result;
}
std::ostream& InventorOutput::writeLine()
{
result << indent << '\n';
return result;
}
std::ostream& InventorOutput::writeLine(const char* str)
{
result << indent << str << '\n';
return result;
}
std::ostream& InventorOutput::writeLine(const std::string& str)
{
result << indent << str << '\n';
return result;
}
// -----------------------------------------------------------------------------
LabelItem::LabelItem(const std::string& text) : text(text)
{
}
void LabelItem::write(InventorOutput& out) const
{
out.write("Label { \n");
out.write() << " label \"" << text << "\"\n";
out.write("} \n");
}
// -----------------------------------------------------------------------------
InfoItem::InfoItem(const std::string& text) : text(text)
{
}
void InfoItem::write(InventorOutput& out) const
{
out.write("Info { \n");
out.write() << " string \"" << text << "\"\n";
out.write("} \n");
}
// -----------------------------------------------------------------------------
BaseColorItem::BaseColorItem(const ColorRGB& rgb) : rgb(rgb)
{
}
void BaseColorItem::write(InventorOutput& out) const
{
out.write("BaseColor { \n");
out.write() << " rgb " << rgb.red() << " " << rgb.green() << " " << rgb.blue() << '\n';
out.write("} \n");
}
// -----------------------------------------------------------------------------
PointItem::PointItem(const Base::Vector3f& point, DrawStyle drawStyle, const ColorRGB& rgb)
: point(point)
, drawStyle(drawStyle)
, rgb(rgb)
{
}
void PointItem::write(InventorOutput& out) const
{
out.write() << "Separator { \n";
out.write() << " Material { \n";
out.write() << " diffuseColor " << rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << '\n';
out.write() << " }\n";
out.write() << " MaterialBinding { value PER_PART }\n";
out.write() << " DrawStyle { pointSize " << drawStyle.pointSize << "}\n";
out.write() << " Coordinate3 {\n";
out.write() << " point [ " << point.x << " " << point.y << " " << point.z << "]\n";
out.write() << " }\n";
out.write() << " PointSet { }\n";
out.write() <<"}\n";
}
// -----------------------------------------------------------------------------
LineItem::LineItem(const Base::Line3f& line, DrawStyle drawStyle, const ColorRGB& rgb)
: line(line)
, drawStyle(drawStyle)
, rgb(rgb)
{
}
void LineItem::write(InventorOutput& out) const
{
std::string pattern = drawStyle.patternAsString();
out.write(" Separator { \n");
out.write() << " Material { diffuseColor " << rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "} \n";
out.write() << " DrawStyle { lineWidth " << drawStyle.lineWidth << " linePattern " << pattern << " } \n";
out.write() << " Coordinate3 { \n";
out.write() << " point [ ";
out.write() << line.p1.x << " " << line.p1.y << " " << line.p1.z << ",";
out.write() << line.p2.x << " " << line.p2.y << " " << line.p2.z;
out.write() << " ] \n";
out.write() << " } \n";
out.write() << " LineSet { } \n";
out.write() << " } \n";
}
// -----------------------------------------------------------------------------
InventorBuilder::InventorBuilder(std::ostream& output)
: result(output)
{
@@ -151,6 +289,12 @@ void InventorBuilder::decreaseIndent()
indent.decreaseIndent();
}
void InventorBuilder::addNode(const NodeItem& node)
{
InventorOutput out(result, indent);
node.write(out);
}
void InventorBuilder::beginSeparator()
{
result << indent << "Separator { \n";

View File

@@ -147,6 +147,97 @@ private:
Base::Vector3f pt3;
};
class Indentation {
int spaces = 0;
public:
void increaseIndent() {
spaces += 2;
}
void decreaseIndent() {
spaces -= 2;
}
int count() {
return spaces;
}
};
class BaseExport InventorOutput
{
public:
explicit InventorOutput(std::ostream& result, Indentation& indent);
std::ostream& write();
std::ostream& write(const char*);
std::ostream& write(const std::string&);
std::ostream& writeLine();
std::ostream& writeLine(const char*);
std::ostream& writeLine(const std::string&);
private:
std::ostream& result;
Indentation& indent;
};
class BaseExport NodeItem
{
public:
virtual ~NodeItem() = default;
virtual void write(InventorOutput& out) const = 0;
};
class BaseExport LabelItem : public NodeItem
{
public:
explicit LabelItem(const std::string& text);
void write(InventorOutput& out) const override;
private:
std::string text;
};
class BaseExport InfoItem : public NodeItem
{
public:
explicit InfoItem(const std::string& text);
void write(InventorOutput& out) const override;
private:
std::string text;
};
class BaseExport BaseColorItem : public NodeItem
{
public:
explicit BaseColorItem(const ColorRGB& rgb);
void write(InventorOutput& out) const override;
private:
ColorRGB rgb;
};
class BaseExport PointItem : public NodeItem
{
public:
explicit PointItem(const Base::Vector3f& point, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
void write(InventorOutput& out) const override;
private:
Base::Vector3f point;
DrawStyle drawStyle;
ColorRGB rgb;
};
class BaseExport LineItem : public NodeItem
{
public:
explicit LineItem(const Base::Line3f& line, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
void write(InventorOutput& out) const override;
private:
Base::Line3f line;
DrawStyle drawStyle;
ColorRGB rgb;
};
/**
* This class does basically the same as Builder3D except that it writes the data
* directly into a given stream without buffering the output data in a string stream.
@@ -169,6 +260,7 @@ public:
*/
virtual ~InventorBuilder();
void addNode(const NodeItem&);
/*!
* \brief Sets a separator node.
*/
@@ -318,21 +410,6 @@ private:
InventorBuilder (const InventorBuilder&);
void operator = (const InventorBuilder&);
public:
class Indentation {
int spaces = 0;
public:
void increaseIndent() {
spaces += 2;
}
void decreaseIndent() {
spaces -= 2;
}
int count() {
return spaces;
}
};
private:
std::ostream& result;
Indentation indent;

View File

@@ -967,7 +967,8 @@ void MeshObject::offsetSpecial2(float fSize)
if (angle > 1.6) {
Base::DrawStyle drawStyle;
drawStyle.pointSize = 4.0F;
builder.addSinglePoint(it->GetGravityPoint(), drawStyle, Base::ColorRGB{1.0F, 0.0F, 0.0F});
Base::PointItem item{it->GetGravityPoint(), drawStyle, Base::ColorRGB{1.0F, 0.0F, 0.0F}};
builder.addNode(item);
fliped.insert(it.Position());
}
}

View File

@@ -90,7 +90,8 @@ void MeshAlgos::offsetSpecial2(MeshCore::MeshKernel* Mesh, float fSize)
if (angle > 1.6){
Base::DrawStyle drawStyle;
drawStyle.pointSize = 4.0F;
builder.addSinglePoint(it->GetGravityPoint(), drawStyle, Base::ColorRGB{1.0F, 0.0F, 0.0F});
Base::PointItem item{it->GetGravityPoint(), drawStyle, Base::ColorRGB{1.0F, 0.0F, 0.0F}};
builder.addNode(item);
fliped.insert(it.Position());
}
}