Base: refactor InventorBuilder

This commit is contained in:
wmayer
2022-12-13 17:48:51 +01:00
parent 12091cceab
commit 2fa8fb45d8
5 changed files with 147 additions and 262 deletions

View File

@@ -367,6 +367,35 @@ void LineItem::write(InventorOutput& out) const
// -----------------------------------------------------------------------------
MultiLineItem::MultiLineItem(const std::vector<Vector3f>& points, DrawStyle drawStyle, const ColorRGB& rgb)
: points{points}
, drawStyle{drawStyle}
, rgb{rgb}
{
}
void MultiLineItem::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";
InventorFieldWriter writer;
writer.write<Vector3f>("point", points, out);
out.write() << " }\n";
out.write() << " LineSet {\n";
out.write() << " numVertices [ -1 ]\n";
out.write() << " }\n";
out.write() << "}\n";
}
// -----------------------------------------------------------------------------
ArrowItem::ArrowItem(const Base::Line3f& line, DrawStyle drawStyle, const ColorRGB& rgb)
: line(line)
, drawStyle(drawStyle)
@@ -417,6 +446,54 @@ void ArrowItem::write(InventorOutput& out) const
// -----------------------------------------------------------------------------
BoundingBoxItem::BoundingBoxItem(const Vector3f& pt1, const Vector3f& pt2, DrawStyle drawStyle, const ColorRGB& rgb)
: pt1{pt1}
, pt2{pt2}
, drawStyle{drawStyle}
, rgb{rgb}
{
}
void BoundingBoxItem::write(InventorOutput& out) const
{
std::vector<Base::Vector3f> points(8);
points[0].Set(pt1.x, pt1.y, pt1.z);
points[1].Set(pt1.x, pt1.y, pt2.z);
points[2].Set(pt1.x, pt2.y, pt1.z);
points[3].Set(pt1.x, pt2.y, pt2.z);
points[4].Set(pt2.x, pt1.y, pt1.z);
points[5].Set(pt2.x, pt1.y, pt2.z);
points[6].Set(pt2.x, pt2.y, pt1.z);
points[7].Set(pt2.x, pt2.y, pt2.z);
std::vector<int> lineset = {
0, 2, 6, 4, 0, -1,
1, 5, 7, 3, 1, -1,
7, 6, 2, 3, 7, -1,
3, 2, 0, 1, 3, -1,
5, 1, 0, 4, 5, -1
};
out.write() << "Separator {\n";
out.write() << " Material { diffuseColor " << rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "}\n";
out.write() << " DrawStyle { lineWidth " << drawStyle.lineWidth << "}\n";
Coordinate3Item coords{points};
out.increaseIndent();
coords.write(out);
out.decreaseIndent();
IndexedLineSetItem indexed{lineset};
out.increaseIndent();
indexed.write(out);
out.decreaseIndent();
out.write() << "}\n";
}
// -----------------------------------------------------------------------------
void MaterialItem::setAmbientColor(const std::vector<ColorRGB>& rgb)
{
ambientColor = rgb;
@@ -509,6 +586,11 @@ void MaterialItem::writeTransparency(InventorOutput& out) const
// -----------------------------------------------------------------------------
MaterialBindingItem::MaterialBindingItem(BindingElement::Binding bind)
{
value.value = bind;
}
void MaterialBindingItem::setValue(BindingElement::Binding bind)
{
value.value = bind;
@@ -522,6 +604,11 @@ void MaterialBindingItem::write(InventorOutput& out) const
// -----------------------------------------------------------------------------
DrawStyleItem::DrawStyleItem(DrawStyle value)
: style{value}
{
}
void DrawStyleItem::setValue(DrawStyle value)
{
style = value;
@@ -604,6 +691,13 @@ void PointSetItem::write(InventorOutput& out) const
// -----------------------------------------------------------------------------
void LineSetItem::write(InventorOutput& out) const
{
out.writeLine("LineSet { }");
}
// -----------------------------------------------------------------------------
FaceSetItem::FaceSetItem(const std::vector<int>& indices)
: indices(indices)
{
@@ -617,7 +711,7 @@ void FaceSetItem::write(InventorOutput &out) const
InventorFieldWriter writer;
writer.write<int>("numVertices", indices, out);
out.decreaseIndent();
out.write() << "}";
out.write() << "}\n";
}
// -----------------------------------------------------------------------------
@@ -635,7 +729,7 @@ void IndexedLineSetItem::write(InventorOutput &out) const
InventorFieldWriter writer;
writer.write<int>("coordIndex", indices, out);
out.decreaseIndent();
out.write() << "}";
out.write() << "}\n";
}
// -----------------------------------------------------------------------------
@@ -653,7 +747,7 @@ void IndexedFaceSetItem::write(InventorOutput &out) const
InventorFieldWriter writer;
writer.write<int>("coordIndex", indices, out);
out.decreaseIndent();
out.write() << "}";
out.write() << "}\n";
}
// -----------------------------------------------------------------------------
@@ -784,7 +878,7 @@ Text2Item::Text2Item(const std::string& string)
void Text2Item::write(InventorOutput& out) const
{
out.write() << "Text2 { string \" " << string << "\" " << "}\n";
out.write() << "Text2 { string \"" << string << "\" " << "}\n";
}
// -----------------------------------------------------------------------------
@@ -795,6 +889,11 @@ TransformItem::TransformItem(const Base::Placement& placement)
}
TransformItem::TransformItem(const Matrix4D& transform)
{
placement.fromMatrix(transform);
}
void TransformItem::write(InventorOutput& out) const
{
Base::Vector3d translation = placement.getPosition();
@@ -851,222 +950,6 @@ void InventorBuilder::endSeparator()
result << indent << "}\n";
}
/**
* Add a Text with a given position to the 3D set. The origin is the
* lower leftmost corner.
* @param pos_x,pos_y,pos_z origin of the text
* @param text the text to display.
* @param color text color.
*/
void InventorBuilder::addText(const Vector3f& pnt, const char * text, const ColorRGB& rgb)
{
result << indent << "Separator { \n"
<< indent << " Material { diffuseColor "
<< rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "} \n"
<< indent << " Transform { translation "
<< pnt.x << " "<< pnt.y << " "<< pnt.z << "} \n"
<< indent << " Text2 { string \" " << text << "\" " << "} \n"
<< indent << "}\n";
}
void InventorBuilder::addSingleLine(const Base::Line3f& line, Base::DrawStyle drawStyle, const ColorRGB& rgb)
{
std::string pattern = drawStyle.patternAsString();
result << " Separator { \n"
<< " Material { diffuseColor " << rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "} \n"
<< " DrawStyle { lineWidth " << drawStyle.lineWidth << " linePattern " << pattern << " } \n"
<< " Coordinate3 { \n"
<< " point [ "
<< line.p1.x << " " << line.p1.y << " " << line.p1.z << ","
<< line.p2.x << " " << line.p2.y << " " << line.p2.z
<< " ] \n"
<< " } \n"
<< " LineSet { } \n"
<< " } \n";
}
void InventorBuilder::addSingleArrow(const Base::Line3f& line, Base::DrawStyle drawStyle, const ColorRGB& rgb)
{
float length = line.Length();
float coneLength = length / 10.0F;
float coneRadius = coneLength / 2.0F;
float sf1 = length - coneLength;
float sf2 = length - coneLength/2.0F;
Vector3f dir = line.GetDirection();
dir.Normalize();
dir.Scale(sf1, sf1, sf1);
Vector3f pt2s = line.p1 + dir;
dir.Normalize();
dir.Scale(sf2, sf2, sf2);
Vector3f cpt = line.p1 + dir;
Vector3f rot = Vector3f(0.0f, 1.0f, 0.0f) % dir;
rot.Normalize();
float a = Vector3f(0.0f, 1.0f, 0.0f).GetAngle(dir);
result << indent << "Separator { \n"
<< indent << " Material { diffuseColor "
<< rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "} \n"
<< indent << " DrawStyle { lineWidth "
<< drawStyle.lineWidth << "} \n"
<< indent << " Coordinate3 { \n"
<< indent << " point [ "
<< line.p1.x << " " << line.p1.y << " " << line.p1.z << ","
<< pt2s.x << " " << pt2s.y << " " << pt2s.z
<< " ] \n"
<< indent << " } \n"
<< indent << " LineSet { } \n"
<< indent << " Transform { \n"
<< indent << " translation "
<< cpt.x << " " << cpt.y << " " << cpt.z << " \n"
<< indent << " rotation "
<< rot.x << " " << rot.y << " " << rot.z << " " << a << '\n'
<< indent << " } \n"
<< indent << " Cone { bottomRadius " << coneRadius << " height " << coneLength << "} \n"
<< indent << "} \n";
}
/** Add a line defined by a list of points whereat always a pair (i.e. a point and the following point) builds a line.
* The size of the list must then be even.
*/
void InventorBuilder::addLineSet(const std::vector<Vector3f>& points, DrawStyle drawStyle, const ColorRGB& rgb)
{
std::string pattern = drawStyle.patternAsString();
result << " Separator { \n"
<< " Material { diffuseColor " << rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "} \n"
<< " DrawStyle { lineWidth " << drawStyle.lineWidth << " linePattern " << pattern << " } \n"
<< " Coordinate3 { \n"
<< " point [ ";
std::vector<Vector3f>::const_iterator it = points.begin();
if ( it != points.end() )
{
result << it->x << " " << it->y << " " << it->z;
for ( ++it ; it != points.end(); ++it )
result << ",\n " << it->x << " " << it->y << " " << it->z;
}
result << " ] \n"
<< " } \n"
<< " LineSet { \n"
<< " numVertices [ ";
result << " -1 ";
result << " ] \n"
<< " } \n"
<< " } \n";
}
void InventorBuilder::addSingleTriangle(const Triangle& triangle, DrawStyle drawStyle, const ColorRGB& rgb)
{
std::string fs = "";
if (drawStyle.style == DrawStyle::Style::Filled) {
fs = " FaceSet { } ";
}
result << " Separator { \n"
<< " Material { diffuseColor " << rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "} \n"
<< " DrawStyle { lineWidth " << drawStyle.lineWidth << "} \n"
<< " Coordinate3 { \n"
<< " point [ "
<< triangle.getPoint1().x << " " << triangle.getPoint1().y << " " << triangle.getPoint1().z << ","
<< triangle.getPoint2().x << " " << triangle.getPoint2().y << " " << triangle.getPoint2().z << ","
<< triangle.getPoint3().x << " " << triangle.getPoint3().y << " " << triangle.getPoint3().z
<< "] \n"
<< " } \n"
<< " IndexedLineSet { coordIndex[ 0, 1, 2, 0, -1 ] } \n"
<< fs << '\n'
<< " } \n";
}
void InventorBuilder::addSinglePlane(const Vector3f& base, const Vector3f& eX, const Vector3f& eY,
float length, float width, DrawStyle drawStyle, const ColorRGB& rgb)
{
Vector3f pt0 = base;
Vector3f pt1 = base + length * eX;
Vector3f pt2 = base + length * eX + width * eY;
Vector3f pt3 = base + width * eY;
std::string fs = "";
if (drawStyle.style == DrawStyle::Style::Filled) {
fs = " FaceSet { } ";
}
result << " Separator { \n"
<< " Material { diffuseColor " << rgb.red() << " "<< rgb.green() << " " << rgb.blue() << "} \n"
<< " DrawStyle { lineWidth " << drawStyle.lineWidth << "} \n"
<< " Coordinate3 { \n"
<< " point [ "
<< pt0.x << " " << pt0.y << " " << pt0.z << ","
<< pt1.x << " " << pt1.y << " " << pt1.z << ","
<< pt2.x << " " << pt2.y << " " << pt2.z << ","
<< pt3.x << " " << pt3.y << " " << pt3.z
<< "] \n"
<< " } \n"
<< " IndexedLineSet { coordIndex[ 0, 1, 2, 3, 0, -1 ] } \n"
<< fs << '\n'
<< " } \n";
}
void InventorBuilder::addBoundingBox(const Vector3f& pt1, const Vector3f& pt2, DrawStyle drawStyle, const ColorRGB& rgb)
{
Base::Vector3f pt[8];
pt[0].Set(pt1.x, pt1.y, pt1.z);
pt[1].Set(pt1.x, pt1.y, pt2.z);
pt[2].Set(pt1.x, pt2.y, pt1.z);
pt[3].Set(pt1.x, pt2.y, pt2.z);
pt[4].Set(pt2.x, pt1.y, pt1.z);
pt[5].Set(pt2.x, pt1.y, pt2.z);
pt[6].Set(pt2.x, pt2.y, pt1.z);
pt[7].Set(pt2.x, pt2.y, pt2.z);
result << " Separator { \n"
<< " Material { diffuseColor " << rgb.red() << " "<< rgb.green() << " "<< rgb.blue() << "} \n"
<< " DrawStyle { lineWidth " << drawStyle.lineWidth << "} \n"
<< " Coordinate3 { \n"
<< " point [ "
<< " " << pt[0].x << " " << pt[0].y << " " << pt[0].z << ",\n"
<< " " << pt[1].x << " " << pt[1].y << " " << pt[1].z << ",\n"
<< " " << pt[2].x << " " << pt[2].y << " " << pt[2].z << ",\n"
<< " " << pt[3].x << " " << pt[3].y << " " << pt[3].z << ",\n"
<< " " << pt[4].x << " " << pt[4].y << " " << pt[4].z << ",\n"
<< " " << pt[5].x << " " << pt[5].y << " " << pt[5].z << ",\n"
<< " " << pt[6].x << " " << pt[6].y << " " << pt[6].z << ",\n"
<< " " << pt[7].x << " " << pt[7].y << " " << pt[7].z
<< "] \n"
<< " } \n"
<< " IndexedLineSet { coordIndex[ 0, 2, 6, 4, 0, -1\n"
" 1, 5, 7, 3, 1, -1,\n"
" 5, 4, 6, 7, 5, -1,\n"
" 7, 6, 2, 3, 7, -1,\n"
" 3, 2, 0, 1, 3, -1,\n"
" 5, 1, 0, 4, 5, -1 ] } \n"
<< " } \n";
}
void InventorBuilder::addTransformation(const Matrix4D& transform)
{
Base::Placement placement;
placement.fromMatrix(transform);
addTransformation(placement);
}
void InventorBuilder::addTransformation(const Base::Placement& transform)
{
Base::Vector3d translation = transform.getPosition();
Base::Vector3d rotationaxis;
double angle{};
transform.getRotation().getValue(rotationaxis, angle);
result << indent << "Transform {\n";
result << indent << " translation "
<< translation.x << " " << translation.y << " " << translation.z << '\n';
result << indent << " rotation "
<< rotationaxis.x << " " << rotationaxis.y << " " << rotationaxis.z
<< " " << angle << '\n';
result << indent << "}" << '\n';
}
// -----------------------------------------------------------------------------
/**

View File

@@ -256,6 +256,19 @@ private:
ColorRGB rgb;
};
class BaseExport MultiLineItem : public NodeItem
{
public:
/// add a line defined by a list of points whereat always a pair (i.e. a point and the following point) builds a line.
explicit MultiLineItem(const std::vector<Vector3f>& points, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
void write(InventorOutput& out) const override;
private:
std::vector<Vector3f> points;
DrawStyle drawStyle;
ColorRGB rgb;
};
class BaseExport ArrowItem : public NodeItem
{
public:
@@ -268,6 +281,19 @@ private:
ColorRGB rgb;
};
class BaseExport BoundingBoxItem : public NodeItem
{
public:
explicit BoundingBoxItem(const Vector3f& pt1, const Vector3f& pt2, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
void write(InventorOutput& out) const override;
private:
Vector3f pt1;
Vector3f pt2;
DrawStyle drawStyle;
ColorRGB rgb;
};
/*!
* \brief The MaterialItem class supports the SoMaterial node.
*/
@@ -308,6 +334,7 @@ class BaseExport MaterialBindingItem : public NodeItem
{
public:
MaterialBindingItem() = default;
explicit MaterialBindingItem(BindingElement::Binding);
void setValue(BindingElement::Binding bind);
void write(InventorOutput& out) const override;
@@ -321,6 +348,8 @@ private:
class BaseExport DrawStyleItem : public NodeItem
{
public:
DrawStyleItem() = default;
explicit DrawStyleItem(DrawStyle);
void setValue(DrawStyle value);
void write(InventorOutput& out) const override;
@@ -378,6 +407,15 @@ public:
void write(InventorOutput& out) const override;
};
/*!
* \brief The LineSetItem class supports the SoLineSet node.
*/
class BaseExport LineSetItem : public NodeItem
{
public:
void write(InventorOutput& out) const override;
};
/*!
* \brief The FaceSetItem class supports the SoFaceSet node.
*/
@@ -525,7 +563,8 @@ private:
class BaseExport TransformItem : public NodeItem
{
public:
TransformItem(const Base::Placement&);
explicit TransformItem(const Base::Placement&);
explicit TransformItem(const Base::Matrix4D&);
void write(InventorOutput& out) const override;
private:
@@ -567,43 +606,6 @@ public:
void endSeparator();
/** @name Appearance handling */
/** @name Line/Direction handling */
//@{
/// add a line
void addSingleLine(const Base::Line3f& line, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
/// add a arrow
void addSingleArrow(const Base::Line3f& line, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
/// add a line defined by a list of points whereat always a pair (i.e. a point and the following point) builds a line.
void addLineSet(const std::vector<Vector3f>& points, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
//@}
/** @name Triangle handling */
//@{
/// add a (filled) triangle defined by 3 vectors
void addSingleTriangle(const Triangle& triangle, DrawStyle drawStyle, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
void addSinglePlane(const Vector3f& base, const Vector3f& eX, const Vector3f& eY, float length, float width, DrawStyle drawStyle,
const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
//@}
/** @name Bounding Box handling */
//@{
void addBoundingBox(const Vector3f& pt1, const Vector3f& pt2, DrawStyle drawStyle,
const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
//@}
/** @name Transformation */
//@{
/// adds a transformation
void addTransformation(const Matrix4D&);
void addTransformation(const Base::Placement&);
//@}
/** @name Text handling */
//@{
/// add a text
void addText(const Vector3f &vec,const char * text, const ColorRGB& rgb = ColorRGB{1.0F, 1.0F, 1.0F});
//@}
private:
void increaseIndent();
void decreaseIndent();

View File

@@ -551,7 +551,7 @@ void MeshObject::writeInventor(std::ostream& str, float creaseangle) const
Base::InventorBuilder builder(str);
builder.beginSeparator();
builder.addTransformation(getTransform());
builder.addNode(Base::TransformItem{getTransform()});
Base::ShapeHintsItem shapeHints{creaseangle};
builder.addNode(shapeHints);
builder.addNode(Base::Coordinate3Item{coords});
@@ -949,7 +949,7 @@ void MeshObject::offsetSpecial2(float fSize)
for (std::vector<Base::Vector3f>::iterator It= PointNormals.begin();It != PointNormals.end();++It,i++) {
Base::Line3f line{_kernel.GetPoint(i), _kernel.GetPoint(i) + It->Normalize() * fSize};
Base::DrawStyle drawStyle;
builder.addSingleLine(line, drawStyle);
builder.addNode(Base::LineItem{line, drawStyle});
// and move each mesh point in the normal direction
_kernel.MovePoint(i,It->Normalize() * fSize);
}

View File

@@ -72,7 +72,7 @@ void MeshAlgos::offsetSpecial2(MeshCore::MeshKernel* Mesh, float fSize)
for(std::vector<Base::Vector3f>::iterator It= PointNormals.begin();It != PointNormals.end();++It,i++) {
Base::Line3f line{Mesh->GetPoint(i), Mesh->GetPoint(i) + It->Normalize() * fSize};
Base::DrawStyle drawStyle;
builder.addSingleLine(line, drawStyle);
builder.addNode(Base::LineItem{line, drawStyle});
// and move each mesh point in the normal direction
Mesh->MovePoint(i,It->Normalize() * fSize);
}

View File

@@ -1142,7 +1142,7 @@ void TopoShape::exportLineSet(std::ostream& str) const
Base::DrawStyle drawStyle;
drawStyle.lineWidth = 2.0F;
builder.addLineSet(vertices, drawStyle, Base::ColorRGB{0, 0, 0});
builder.addNode(Base::MultiLineItem{vertices, drawStyle, Base::ColorRGB{0, 0, 0}});
}
builder.endSeparator();