Base: start of refactoring of Builder3D

This commit is contained in:
wmayer
2022-11-27 15:02:44 +01:00
parent 13acca7504
commit 8f136f60c6
4 changed files with 277 additions and 194 deletions

View File

@@ -51,6 +51,25 @@ using namespace Base;
//**************************************************************************
// Construction/Destruction
constexpr float valueMinLegal {-1.0F};
constexpr float valueMaxLegal {1.0F};
ColorRGB::ColorRGB() : Rgb{1.0F, 1.0F, 1.0F}
{
}
ColorRGB::ColorRGB(float red, float green, float blue)
: Rgb{valueInRange(red),
valueInRange(green),
valueInRange(blue)}
{
}
float ColorRGB::valueInRange(float value)
{
return std::clamp(value, valueMinLegal, valueMaxLegal);
}
/**
* A constructor.
* A more elaborate description of the constructor.
@@ -58,8 +77,8 @@ using namespace Base;
Builder3D::Builder3D()
: bStartEndOpen(false)
{
result << "#Inventor V2.1 ascii \n\n";
result << "Separator { ";
result << "#Inventor V2.1 ascii \n\n";
result << "Separator { ";
}
/**
@@ -78,34 +97,25 @@ Builder3D::~Builder3D() = default;
* Try to put all points in one set.
* @see endPoints()
* @param pointSize the point size in pixel that are displayed.
* @param color_r red part of the point color (0.0 - 1.0).
* @param color_g green part of the point color (0.0 - 1.0).
* @param color_b blue part of the point color (0.0 - 1.0).
* @param ColorRGB point color.
*/
void Builder3D::startPoints(short pointSize, float color_r, float color_g, float color_b)
void Builder3D::startPoints(short pointSize, const ColorRGB& color)
{
bStartEndOpen = true;
result << "Separator { ";
result << "Material { ";
result << "diffuseColor " << color_r << " "<< color_g << " " << color_b;
result << "} ";
result << "MaterialBinding { value PER_PART } ";
result << "DrawStyle { pointSize " << pointSize << "} ";
result << "Coordinate3 { ";
result << "point [ ";
bStartEndOpen = true;
result << "Separator { ";
result << "Material { ";
result << "diffuseColor " << color.red() << " "<< color.green() << " " << color.blue();
result << "} ";
result << "MaterialBinding { value PER_PART } ";
result << "DrawStyle { pointSize " << pointSize << "} ";
result << "Coordinate3 { ";
result << "point [ ";
}
/// insert a point in a point set
void Builder3D::addPoint(float x, float y, float z)
/// add a point to a point set
void Builder3D::addPoint(const Vector3f &point)
{
result << x << " " << y << " " << z << ",";
}
/// add a vector to a point set
void Builder3D::addPoint(const Vector3f &vec)
{
addPoint(vec.x, vec.y, vec.z);
result << point.x << " " << point.y << " " << point.z << ",";
}
/**
* Ends the point set operations and write the resulting inventor string.
@@ -113,37 +123,31 @@ void Builder3D::addPoint(const Vector3f &vec)
*/
void Builder3D::endPoints()
{
result << "] ";
result << "} ";
result << "PointSet { } ";
result << "} ";
bStartEndOpen = false;
result << "] ";
result << "} ";
result << "PointSet { } ";
result << "} ";
bStartEndOpen = false;
}
void Builder3D::addSinglePoint(float x, float y, float z, short pointSize, float color_r, float color_g, float color_b)
void Builder3D::addSinglePoint(const Base::Vector3f &point, DrawStyle drawStyle, const ColorRGB& color)
{
// addSinglePoint() not between startXXX() and endXXX() allowed
assert(!bStartEndOpen);
// addSinglePoint() not between startXXX() and endXXX() allowed
assert(!bStartEndOpen);
result << "Separator { ";
result << "Material { ";
result << "diffuseColor " << color_r << " "<< color_g << " "<< color_b;
result << "} ";
result << "MaterialBinding { value PER_PART } ";
result << "DrawStyle { pointSize " << pointSize << "} ";
result << "Coordinate3 { ";
result << "point [ ";
result << x << " " << y << " " << z << ",";
result << "] ";
result << "} ";
result << "PointSet { } ";
result << "} ";
}
void Builder3D::addSinglePoint(const Base::Vector3f &vec, short pointSize, float color_r, float color_g, float color_b)
{
addSinglePoint(vec.x, vec.y , vec.z, pointSize, color_r, color_g, color_b);
result << "Separator { ";
result << "Material { ";
result << "diffuseColor " << color.red() << " "<< color.green() << " "<< color.blue();
result << "} ";
result << "MaterialBinding { value PER_PART } ";
result << "DrawStyle { pointSize " << drawStyle.pointSize << "} ";
result << "Coordinate3 { ";
result << "point [ ";
result << point.x << " " << point.y << " " << point.z << ",";
result << "] ";
result << "} ";
result << "PointSet { } ";
result << "} ";
}
//**************************************************************************
@@ -153,87 +157,77 @@ void Builder3D::addSinglePoint(const Base::Vector3f &vec, short pointSize, float
/**
* 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 point origin of the text
* @param text the text to display.
* @param color_r red part of the text color (0.0 - 1.0).
* @param color_g green part of the text color (0.0 - 1.0).
* @param color_b blue part of the text color (0.0 - 1.0).
* @param color text color.
*/
void Builder3D::addText(float pos_x, float pos_y , float pos_z, const char * text, float color_r, float color_g, float color_b)
void Builder3D::addText(const Base::Vector3f& point, const char * text, const Base::ColorRGB& color)
{
// addSinglePoint() not between startXXX() and endXXX() allowed
assert(!bStartEndOpen);
result << "Separator { "
<< "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "
<< "Transform { translation " << pos_x << " "<< pos_y << " "<< pos_z << "} "
<< "Material { diffuseColor " << color.red() << " "<< color.green() << " "<< color.blue() << "} "
<< "Transform { translation " << point.x << " "<< point.y << " "<< point.z << "} "
<< "Text2 { string \" " << text << "\" " << "} "
<< "} ";
}
void Builder3D::addText(const Base::Vector3f &vec, const char * text, float color_r, float color_g, float color_b)
{
addText(vec.x, vec.y, vec.z, text, color_r, color_g, color_b);
}
void Builder3D::clear ()
{
// under gcc stringstream::str() returns a copy not a reference
// under gcc stringstream::str() returns a copy not a reference
#if defined(_MSC_VER)
result.str().clear();
result.str().clear();
#endif
result.clear();
result.clear();
}
//**************************************************************************
// line/arrow handling
void Builder3D::addSingleLine(const Vector3f& pt1, const Vector3f& pt2, short lineSize, float color_r, float color_g, float color_b, unsigned short linePattern)
void Builder3D::addSingleLine(const Base::Line3f& line, DrawStyle drawStyle, const ColorRGB& color)
{
char lp[20];
sprintf(lp, "0x%x", linePattern);
//char lp[20] = "0x";
//itoa(linePattern, buf, 16);
//strcat(lp, buf);
char pattern[20];
sprintf(pattern, "0x%x", drawStyle.linePattern);
result << "Separator { "
<< "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "
<< "DrawStyle { lineWidth " << lineSize << " linePattern " << lp << " } "
<< "Coordinate3 { "
<< "point [ "
<< pt1.x << " " << pt1.y << " " << pt1.z << ","
<< pt2.x << " " << pt2.y << " " << pt2.z
<< "] "
<< "} "
<< "LineSet { } "
<< "} ";
result << "Separator { "
<< "Material { diffuseColor " << color.red() << " "<< color.green() << " "<< color.blue() << "} "
<< "DrawStyle { lineWidth " << drawStyle.lineWidth << " linePattern " << pattern << " } "
<< "Coordinate3 { "
<< "point [ "
<< line.p1.x << " " << line.p1.y << " " << line.p1.z << ","
<< line.p2.x << " " << line.p2.y << " " << line.p2.z
<< "] "
<< "} "
<< "LineSet { } "
<< "} ";
}
void Builder3D::addSingleArrow(const Vector3f& pt1, const Vector3f& pt2, short lineSize, float color_r, float color_g, float color_b, unsigned short /*linePattern*/)
void Builder3D::addSingleArrow(const Base::Line3f& line, DrawStyle drawStyle, const ColorRGB& color)
{
float l = (pt2 - pt1).Length();
float l = line.Length();
float cl = l / 10.0f;
float cr = cl / 2.0f;
Vector3f dir = pt2 - pt1;
Vector3f dir = line.GetDirection();
dir.Normalize();
dir.Scale(l-cl, l-cl, l-cl);
Vector3f pt2s = pt1 + dir;
Vector3f pt2s = line.p1 + dir;
dir.Normalize();
dir.Scale(l-cl/2.0f, l-cl/2.0f, l-cl/2.0f);
Vector3f cpt = pt1 + dir;
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 << "Separator { "
<< "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "
<< "DrawStyle { lineWidth " << lineSize << "} "
<< "Material { diffuseColor " << color.red() << " "<< color.green() << " "<< color.blue() << "} "
<< "DrawStyle { lineWidth " << drawStyle.lineWidth << "} "
<< "Coordinate3 { "
<< "point [ "
<< pt1.x << " " << pt1.y << " " << pt1.z << ","
<< line.p1.x << " " << line.p1.y << " " << line.p1.z << ","
<< pt2s.x << " " << pt2s.y << " " << pt2s.z
<< "] "
<< "} "
@@ -250,46 +244,45 @@ void Builder3D::addSingleArrow(const Vector3f& pt1, const Vector3f& pt2, short l
//**************************************************************************
// triangle handling
void Builder3D::addSingleTriangle(const Vector3f& pt0, const Vector3f& pt1, const Vector3f& pt2, bool filled, short lineSize, float color_r, float color_g, float color_b)
void Builder3D::addSingleTriangle(const Triangle& triangle, DrawStyle drawStyle, const ColorRGB& color)
{
std::string fs = "";
if (filled)
{
fs = "IndexedFaceSet { coordIndex[ 0, 1, 2, -1 ] } ";
}
std::string fs = "";
if (drawStyle.style == DrawStyle::Style::Filled) {
fs = "IndexedFaceSet { coordIndex[ 0, 1, 2, -1 ] } ";
}
result << "Separator { "
<< "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "
<< "DrawStyle { lineWidth " << lineSize << "} "
<< "Coordinate3 { "
<< "point [ "
<< pt0.x << " " << pt0.y << " " << pt0.z << ","
<< pt1.x << " " << pt1.y << " " << pt1.z << ","
<< pt2.x << " " << pt2.y << " " << pt2.z << ","
<< "] "
<< "} "
<< "LineSet { } "
<< fs
<< "} ";
<< "Material { diffuseColor " << color.red() << " "<< color.green() << " "<< color.blue() << "} "
<< "DrawStyle { lineWidth " << drawStyle.lineWidth << "} "
<< "Coordinate3 { "
<< "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 << ","
<< "] "
<< "} "
<< "LineSet { } "
<< fs
<< "} ";
}
void Builder3D::addTransformation(const Base::Matrix4D& transform)
{
Base::Vector3f cAxis, cBase;
float fAngle = 0.0f, fTranslation = 0.0f;
transform.toAxisAngle(cBase, cAxis,fAngle,fTranslation);
cBase.x = static_cast<float>(transform[0][3]);
cBase.y = static_cast<float>(transform[1][3]);
cBase.z = static_cast<float>(transform[2][3]);
addTransformation(cBase,cAxis,fAngle);
Base::Placement placement;
placement.fromMatrix(transform);
addTransformation(placement);
}
void Builder3D::addTransformation(const Base::Vector3f& translation, const Base::Vector3f& rotationaxis, float fAngle)
void Builder3D::addTransformation(const Base::Placement& transform)
{
result << "Transform {";
result << " translation " << translation.x << " " << translation.y << " " << translation.z;
result << " rotation " << rotationaxis.x << " " << rotationaxis.y << " " << rotationaxis.z << " " << fAngle;
result << "}";
Base::Vector3d translation = transform.getPosition();
Base::Vector3d rotationaxis;
double angle;
transform.getRotation().getValue(rotationaxis, angle);
result << "Transform {";
result << " translation " << translation.x << " " << translation.y << " " << translation.z;
result << " rotation " << rotationaxis.x << " " << rotationaxis.y << " " << rotationaxis.z << " " << angle;
result << "}";
}
//**************************************************************************
@@ -325,14 +318,14 @@ void Builder3D::saveToLog()
*/
void Builder3D::saveToFile(const char* FileName)
{
result << "} ";
Base::FileInfo fi(FileName);
Base::ofstream file(fi);
if(!file)
throw FileException("Builder3D::saveToFile(): Can not open file...");
result << "} ";
Base::FileInfo fi(FileName);
Base::ofstream file(fi);
if (!file)
throw FileException("Builder3D::saveToFile(): Can not open file...");
file << "#Inventor V2.1 ascii \n";
file << result.str();
file << "#Inventor V2.1 ascii \n";
file << result.str();
}
// -----------------------------------------------------------------------------