Skip to content

Commit

Permalink
+ make API of InventorBuilder more flexible and add new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 26, 2015
1 parent e0f3bb0 commit 898e0fb
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 116 deletions.
119 changes: 83 additions & 36 deletions src/Base/Builder3D.cpp
Expand Up @@ -331,7 +331,8 @@ void Builder3D::saveToFile(const char* FileName)
// -----------------------------------------------------------------------------

InventorBuilder::InventorBuilder(std::ostream& output)
: result(output),bStartEndOpen(false),bClosed(false), indent(0)
: result(output)
, indent(0)
{
result << "#Inventor V2.1 ascii " << std::endl << std::endl;
beginSeparator();
Expand All @@ -344,8 +345,8 @@ InventorBuilder:: ~InventorBuilder()

void InventorBuilder::close()
{
if (!bClosed) {
bClosed = true;
if (indent > 0) {
indent = 0;
endSeparator();
}
}
Expand All @@ -362,6 +363,28 @@ void InventorBuilder::endSeparator()
result << Base::blanks(indent) << "}" << std::endl;
}

void InventorBuilder::addInfo(const char* text)
{
result << Base::blanks(indent) << "Info { " << std::endl;
result << Base::blanks(indent) << " string \"" << text << "\"" << std::endl;
result << Base::blanks(indent) << "} " << std::endl;
}

void InventorBuilder::addLabel(const char* text)
{
result << Base::blanks(indent) << "Label { " << std::endl;
result << Base::blanks(indent) << " label \"" << text << "\"" << std::endl;
result << Base::blanks(indent) << "} " << std::endl;
}

void InventorBuilder::addBaseColor(float color_r,float color_g,float color_b)
{
result << Base::blanks(indent) << "BaseColor { " << std::endl;
result << Base::blanks(indent) << " rgb "
<< color_r << " "<< color_g << " "<< color_b << std::endl;
result << Base::blanks(indent) << "} " << std::endl;
}

void InventorBuilder::addMaterial(float color_r,float color_g,float color_b)
{
result << Base::blanks(indent) << "Material { " << std::endl;
Expand All @@ -386,6 +409,13 @@ void InventorBuilder::addDrawStyle(short pointSize, short lineWidth, unsigned sh
<< Base::blanks(indent) << "}" << std::endl;
}

void InventorBuilder::addShapeHints(float crease)
{
result << Base::blanks(indent) << "ShapeHints {" << std::endl
<< Base::blanks(indent) << " creaseAngle " << crease << std::endl
<< Base::blanks(indent) << "}" << std::endl;
}

//**************************************************************************
// points handling

Expand All @@ -400,7 +430,7 @@ void InventorBuilder::beginPoints()
{
result << Base::blanks(indent) << "Coordinate3 { " << std::endl;
indent += 2;
result << Base::blanks(indent) << "point [ ";
result << Base::blanks(indent) << "point [ " << std::endl;
indent += 2;
}

Expand All @@ -416,11 +446,17 @@ void InventorBuilder::addPoint(const Vector3f &vec)
addPoint(vec.x,vec.y,vec.z);
}

void InventorBuilder::addPoints(const std::vector<Vector3f> &vec)
{
for (std::vector<Vector3f>::const_iterator it = vec.begin(); it != vec.end(); ++it)
addPoint(it->x, it->y, it->z);
}

/**
* Ends the point set operations and write the resulting inventor string.
* @see startPoints()
*/
void InventorBuilder::endPoints(void)
void InventorBuilder::endPoints()
{
indent -= 2;
result << Base::blanks(indent) << "]" << std::endl;
Expand All @@ -435,7 +471,7 @@ void InventorBuilder::endPoints(void)
* @see beginPoints()
* @see endPoints()
*/
void InventorBuilder::addPointSet(void)
void InventorBuilder::addPointSet()
{
result << Base::blanks(indent) << "PointSet { } " << std::endl;
}
Expand All @@ -447,7 +483,7 @@ void InventorBuilder::addPointSet(void)
* @see beginPoints()
* @see endPoints()
*/
void InventorBuilder::addLineSet(void)
void InventorBuilder::addLineSet()
{
result << Base::blanks(indent) << "LineSet { } " << std::endl;
}
Expand All @@ -467,9 +503,6 @@ void InventorBuilder::addLineSet(void)
*/
void InventorBuilder::addText(float pos_x, float pos_y , float pos_z,const char * text, float color_r,float color_g,float color_b)
{
// addSinglePoint() not between startXXX() and endXXX() allowed
assert( bStartEndOpen == false );

result << Base::blanks(indent) << "Separator { " << std::endl
<< Base::blanks(indent) << " Material { diffuseColor "
<< color_r << " "<< color_g << " "<< color_b << "} " << std::endl
Expand Down Expand Up @@ -598,39 +631,53 @@ void InventorBuilder::addLineSet(const std::vector<Vector3f>& points, short line
//**************************************************************************
// triangle handling

void InventorBuilder::addIndexedFaceSet(const std::vector<Vector3f>& points, const std::vector<int>& indices, float crease)
void InventorBuilder::addIndexedFaceSet(const std::vector<int>& indices)
{
if (points.empty() || indices.size() < 4)
if (indices.size() < 4)
return;
result << " Separator { " << std::endl
<< " ShapeHints {" << std::endl
<< " creaseAngle " << crease << std::endl
<< " }" << std::endl
<< " Coordinate3 { " << std::endl
<< " point [ ";
std::vector<Vector3f>::const_iterator it_last_p = points.end()-1;
for (std::vector<Vector3f>::const_iterator it = points.begin(); it != points.end(); ++it) {
if (it != it_last_p)
result << it->x << " " << it->y << " " << it->z << "," << std::endl;
else
result << it->x << " " << it->y << " " << it->z << " ] " << std::endl;
}

result << " } " << std::endl
<< " IndexedFaceSet { " << std::endl
<< " coordIndex [ ";
result << Base::blanks(indent) << "IndexedFaceSet { " << std::endl
<< Base::blanks(indent) << " coordIndex [ " << std::endl;

indent += 4;
std::vector<int>::const_iterator it_last_f = indices.end()-1;
int index=0;
for (std::vector<int>::const_iterator it = indices.begin(); it != indices.end(); ++it) {
if (it != it_last_f)
result << *it << ", ";
else
result << *it << " ] ";
if (++index%8==0)
result << std::endl;
if (index % 8 == 0)
result << Base::blanks(indent);
if (it != it_last_f)
result << *it << ", ";
else
result << *it << " ] " << std::endl;
if (++index % 8 == 0)
result << std::endl;
}
result << " } " << std::endl
<< " } " << std::endl;
indent -= 4;

result << Base::blanks(indent) << "} " << std::endl;
}

void InventorBuilder::beginNormal()
{
result << Base::blanks(indent) << "Normal { " << std::endl;
indent += 2;
result << Base::blanks(indent) << "vector [ " << std::endl;
indent += 2;
}

void InventorBuilder::endNormal()
{
indent -= 2;
result << Base::blanks(indent) << "]" << std::endl;
indent -= 2;
result << Base::blanks(indent) << "}" << std::endl;
}

void InventorBuilder::addNormalBinding(const char* binding)
{
result << Base::blanks(indent) << "NormalBinding {" << std::endl
<< Base::blanks(indent) << " value " << binding << std::endl
<< Base::blanks(indent) << "}" << std::endl;
}

void InventorBuilder::addSingleTriangle(const Vector3f& pt0, const Vector3f& pt1, const Vector3f& pt2,
Expand Down
101 changes: 86 additions & 15 deletions src/Base/Builder3D.h
Expand Up @@ -141,33 +141,104 @@ class BaseExport Builder3D
class BaseExport InventorBuilder
{
public:
/// Construction
InventorBuilder(std::ostream&);
/// Destruction
/*!
* \brief Construction of an InventorBuilder instance.
* This automatically opens a separator node.
* \param str - stream to write the content into
*/
InventorBuilder(std::ostream& str);
/*!
* \brief Destruction of an InventorBuilder instance
*/
virtual ~InventorBuilder();
/*!
* \brief If needed closes the first opened separator node.
* This method must not be used more than one time for an instance.
*/
void close();

/*!
* \brief Sets a separator node.
*/
void beginSeparator();
/*!
* \brief Closes the last added separator node.
*/
void endSeparator();
/*!
* \brief Sets an info node.
* \param str - text set to the info node
*/
void addInfo(const char* str);
/*!
* \brief Sets a label node.
* \param str - text set to the label node
*/
void addLabel(const char* str);
/*!
* \brief Sets a base color node. The colors are in the range [0, 1].
* \param color_r - red color
* \param color_g - green color
* \param color_b - blue color
*/
void addBaseColor(float color_r,float color_g,float color_b);
/*!
* \brief Sets a material node. The colors are in the range [0, 1].
* \param color_r - red color
* \param color_g - green color
* \param color_b - blue color
*/
void addMaterial(float color_r,float color_g,float color_b);
void addMaterialBinding(const char* = "OVERALL");
/*!
* \brief Sets a material binding node.
* \param binding - binding of the material. Allowed values are:
* OVERALL, PER_PART, PER_PART_INDEXED, PER_FACE, PER_FACE_INDEXED, PER_VERTEX,
* PER_VERTEX_INDEXED and DEFAULT.
*/
void addMaterialBinding(const char* binding = "OVERALL");
/*!
* \brief Sets a draw style node.
* \param pointSize - the point size
* \param lineWidth - the line width
* \param linePattern - the line pattern
* \param style - the draw style
*/
void addDrawStyle(short pointSize, short lineWidth,
unsigned short linePattern = 0xffff, const char* style="FILLED");
/*!
* \brief Sets a shape hints node.
* \param crease - the crease angle in radians
*/
void addShapeHints(float crease=0.0f);

/** @name Add coordinates */
//@{
/// add a single point
void addPoint(float x, float y, float z);
/// add a single point
void addPoint(const Vector3f &vec);
/// add a list of points
void addPoints(const std::vector<Vector3f> &vec);
//@}

/** @name Point set handling */
//@{
/// starts a point set
void beginPoints();
/// insert a point in an point set
void addPoint(float x, float y, float z);
/// add a vector to a point set
void addPoint(const Vector3f &vec);
/// ends the points set operation
void endPoints(void);
void endPoints();
/// add an SoPointSet node
void addPointSet(void);
/// add an SoLineSet node
void addLineSet(void);
void addPointSet();
//@}

/** @name Normal handling */
//@{
/// starts a point set
void beginNormal();
/// ends the points set operation
void endNormal();
/// add normal binding node
void addNormalBinding(const char*);
//@}

/** @name Line/Direction handling */
Expand All @@ -181,7 +252,8 @@ class BaseExport InventorBuilder
/// 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, short lineSize=2,
float color_r=1.0,float color_g=1.0,float color_b=1.0, unsigned short linePattern = 0xffff);
void addIndexedFaceSet(const std::vector<Vector3f>& points, const std::vector<int>& indices, float crease);
/// add an SoLineSet node
void addLineSet();
//@}

/** @name Triangle handling */
Expand All @@ -191,6 +263,7 @@ class BaseExport InventorBuilder
float color_r=1.0,float color_g=1.0,float color_b=1.0);
void addSinglePlane(const Vector3f& base, const Vector3f& eX, const Vector3f& eY, float length, float width, bool filled = true,
short lineSize=2, float color_r=1.0,float color_g=1.0,float color_b=1.0);
void addIndexedFaceSet(const std::vector<int>& indices);
//@}

/** @name Surface handling */
Expand Down Expand Up @@ -229,8 +302,6 @@ class BaseExport InventorBuilder

private:
std::ostream& result;
bool bStartEndOpen;
bool bClosed;
int indent;
};

Expand Down

0 comments on commit 898e0fb

Please sign in to comment.