Skip to content

Commit

Permalink
Base: refactoring of InventorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 29, 2022
1 parent b325ab9 commit c16e467
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 18 deletions.
146 changes: 145 additions & 1 deletion src/Base/Builder3D.cpp
Expand Up @@ -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)
{
Expand All @@ -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";
Expand Down
107 changes: 92 additions & 15 deletions src/Base/Builder3D.h
Expand Up @@ -147,6 +147,97 @@ class BaseExport Triangle
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.
Expand All @@ -169,6 +260,7 @@ class BaseExport InventorBuilder
*/
virtual ~InventorBuilder();

void addNode(const NodeItem&);
/*!
* \brief Sets a separator node.
*/
Expand Down Expand Up @@ -318,21 +410,6 @@ class BaseExport InventorBuilder
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;
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Mesh/App/Mesh.cpp
Expand Up @@ -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());
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/MeshPart/App/MeshAlgos.cpp
Expand Up @@ -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());
}
}
Expand Down

0 comments on commit c16e467

Please sign in to comment.