From 38dac0ada5d051d071171c2fcc7f39ecd18aafe8 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 23 Mar 2016 13:52:55 +0200 Subject: [PATCH] Small refactoring. Use M_2PI(2*pi), M_PI_2 (pi/2) and M_PI_4(pi/4) where possible. --HG-- branch : develop --- src/libs/vdxf/dxflib/dl_codes.h | 11 +++- src/libs/vdxf/dxflib/dl_dxf.cpp | 66 ++++++++++----------- src/libs/vdxf/dxflib/dl_dxf.h | 14 +---- src/libs/vgeometry/vabstractcubicbezier.cpp | 8 +-- src/libs/vgeometry/vabstractcurve.h | 4 ++ src/libs/vgeometry/varc.cpp | 2 +- src/libs/vgeometry/vspline.cpp | 4 +- 7 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/libs/vdxf/dxflib/dl_codes.h b/src/libs/vdxf/dxflib/dl_codes.h index bc03416a6..c3b16d6a9 100644 --- a/src/libs/vdxf/dxflib/dl_codes.h +++ b/src/libs/vdxf/dxflib/dl_codes.h @@ -56,7 +56,16 @@ #endif #ifndef M_PI -#define M_PI 3.1415926535897932384626433832795 +#define M_PI 3.14159265358979323846 /* pi */ +#endif + +#ifndef M_2PI +#define M_2PI 6.28318530717958647692 /* 2*pi */ +#endif + +#ifndef M_PI_2 +#define M_PI_2 1.57079632679489661923 /* pi/2 */ + #endif #define DL_DXF_MAXLINE 1024 diff --git a/src/libs/vdxf/dxflib/dl_dxf.cpp b/src/libs/vdxf/dxflib/dl_dxf.cpp index d4cf1d12d..32681dd0d 100644 --- a/src/libs/vdxf/dxflib/dl_dxf.cpp +++ b/src/libs/vdxf/dxflib/dl_dxf.cpp @@ -200,7 +200,7 @@ bool DL_Dxf::readDxfGroups(std::stringstream& stream, // Read one group of the DXF file and chop the lines: if (DL_Dxf::getStrippedLine(groupCodeTmp, DL_DXF_MAXLINE, stream) && - DL_Dxf::getStrippedLine(groupValue, DL_DXF_MAXLINE, stream, false) ) + DL_Dxf::getStrippedLine(groupValue, DL_DXF_MAXLINE, stream, false) ) { groupCode = static_cast(toInt(groupCodeTmp)); @@ -230,7 +230,7 @@ bool DL_Dxf::readDxfGroups(std::stringstream& stream, * @todo Is it a problem if line is blank (i.e., newline only)? * Then, when function returns, (s==NULL). */ -bool DL_Dxf::getStrippedLine(std::string& s, quint32 size, FILE *fp, bool stripSpace) +bool DL_Dxf::getStrippedLine(std::string& s, quint32 size, FILE *fp, bool stripSpace) { if (!feof(fp)) { @@ -270,7 +270,7 @@ bool DL_Dxf::getStrippedLine(std::string& s, quint32 size, FILE *fp, bool stripS * Same as above but for stringstreams. */ bool DL_Dxf::getStrippedLine(std::string &s, quint32 size, - std::stringstream& stream, bool stripSpace) + std::stringstream& stream, bool stripSpace) { if (!stream.eof()) @@ -312,21 +312,21 @@ bool DL_Dxf::stripWhiteSpace(char** s, bool stripSpace) // Is last character CR or LF? while ( (lastChar >= 0) && (((*s)[lastChar] == 10) || ((*s)[lastChar] == 13) || - (stripSpace && ((*s)[lastChar] == ' ' || ((*s)[lastChar] == '\t')))) ) + (stripSpace && ((*s)[lastChar] == ' ' || ((*s)[lastChar] == '\t')))) ) { (*s)[lastChar] = '\0'; lastChar--; } // Skip whitespace, excluding \n, at beginning of line - if (stripSpace) + if (stripSpace) { - while ((*s)[0]==' ' || (*s)[0]=='\t') + while ((*s)[0]==' ' || (*s)[0]=='\t') { ++(*s); } } - + return ((*s) ? true : false); } @@ -1275,7 +1275,7 @@ void DL_Dxf::addEllipse(DL_CreationInterface* creationInterface) getRealValue(31, 0.0), getRealValue(40, 1.0), getRealValue(41, 0.0), - getRealValue(42, 2*M_PI)); + getRealValue(42, M_2PI)); creationInterface->addEllipse(d); } @@ -1395,7 +1395,7 @@ void DL_Dxf::addMText(DL_CreationInterface* creationInterface) } else { - angle = (getRealValue(50, 0.0)*2*M_PI)/360.0; + angle = (getRealValue(50, 0.0)*M_2PI)/360.0; } } else if (hasValue(11) && hasValue(21)) @@ -1407,11 +1407,11 @@ void DL_Dxf::addMText(DL_CreationInterface* creationInterface) { if (y>0.0) { - angle = M_PI/2.0; + angle = M_PI_2; } else { - angle = M_PI/2.0*3.0; + angle = M_PI_2*3.0; } } else @@ -1481,7 +1481,7 @@ bool DL_Dxf::handleXRecordData(DL_CreationInterface* creationInterface) if (groupCode<=9 || groupCode==100 || groupCode==102 || groupCode==105 || (groupCode>=300 && groupCode<=369) || - (groupCode>=1000 && groupCode<=1009)) + (groupCode>=1000 && groupCode<=1009)) { creationInterface->addXRecordString(static_cast(groupCode), groupValue); @@ -1853,7 +1853,7 @@ void DL_Dxf::addText(DL_CreationInterface* creationInterface) // style getStringValue(7, ""), // angle - (getRealValue(50, 0.0)*2*M_PI)/360.0); + (getRealValue(50, 0.0)*M_2PI)/360.0); creationInterface->addText(d); } @@ -1892,7 +1892,7 @@ void DL_Dxf::addAttribute(DL_CreationInterface* creationInterface) // style getStringValue(7, ""), // angle - (getRealValue(50, 0.0)*2*M_PI)/360.0); + (getRealValue(50, 0.0)*M_2PI)/360.0); creationInterface->addAttribute(d); } @@ -2272,7 +2272,7 @@ bool DL_Dxf::handleHatchData(DL_CreationInterface* creationInterface) hatchEdge.defined = true; return true; default: - break; + break; } } @@ -2291,10 +2291,10 @@ bool DL_Dxf::handleHatchData(DL_CreationInterface* creationInterface) hatchEdge.radius = toReal(groupValue); return true; case 50: - hatchEdge.angle1 = toReal(groupValue)/360.0*2*M_PI; + hatchEdge.angle1 = toReal(groupValue)/360.0*M_2PI; return true; case 51: - hatchEdge.angle2 = toReal(groupValue)/360.0*2*M_PI; + hatchEdge.angle2 = toReal(groupValue)/360.0*M_2PI; return true; case 73: hatchEdge.ccw = static_cast(toInt(groupValue)); @@ -2326,10 +2326,10 @@ bool DL_Dxf::handleHatchData(DL_CreationInterface* creationInterface) hatchEdge.ratio = toReal(groupValue); return true; case 50: - hatchEdge.angle1 = toReal(groupValue)/360.0*2*M_PI; + hatchEdge.angle1 = toReal(groupValue)/360.0*M_2PI; return true; case 51: - hatchEdge.angle2 = toReal(groupValue)/360.0*2*M_PI; + hatchEdge.angle2 = toReal(groupValue)/360.0*M_2PI; return true; case 73: hatchEdge.ccw = static_cast(toInt(groupValue)); @@ -2581,7 +2581,7 @@ void DL_Dxf::writeHeader(DL_WriterA& dw) const break; case DL_Codes::AC1015: dw.dxfString(1, "AC1015"); - break; + break; case DL_Codes::AC1009_MIN: // minimalistic DXF version is unidentified in file: break; @@ -3050,11 +3050,11 @@ void DL_Dxf::writeInsert(DL_WriterA& dw, if (version==DL_VERSION_2000) { dw.dxfString(100, "AcDbEntity"); - if (data.cols!=1 || data.rows!=1) + if (data.cols!=1 || data.rows!=1) { dw.dxfString(100, "AcDbMInsertBlock"); } - else + else { dw.dxfString(100, "AcDbBlockReference"); } @@ -3133,7 +3133,7 @@ void DL_Dxf::writeMText(DL_WriterA& dw, dw.dxfString(7, data.style); // since dxflib 2.0.2.1: degrees not rad (error in autodesk dxf doc) - dw.dxfReal(50, data.angle/(2.0*M_PI)*360.0); + dw.dxfReal(50, data.angle/(M_2PI)*360.0); dw.dxfInt(73, data.lineSpacingStyle); dw.dxfReal(44, data.lineSpacingFactor); @@ -3165,7 +3165,7 @@ void DL_Dxf::writeText(DL_WriterA& dw, dw.dxfReal(30, data.ipz); dw.dxfReal(40, data.height); dw.dxfString(1, data.text); - dw.dxfReal(50, data.angle/(2*M_PI)*360.0); + dw.dxfReal(50, data.angle/(M_2PI)*360.0); dw.dxfReal(41, data.xScaleFactor); dw.dxfString(7, data.style); @@ -3201,7 +3201,7 @@ void DL_Dxf::writeAttribute(DL_WriterA& dw, dw.dxfReal(30, data.ipz); dw.dxfReal(40, data.height); dw.dxfString(1, data.text); - dw.dxfReal(50, data.angle/(2*M_PI)*360.0); + dw.dxfReal(50, data.angle/(M_2PI)*360.0); dw.dxfReal(41, data.xScaleFactor); dw.dxfString(7, data.style); @@ -3366,7 +3366,7 @@ void DL_Dxf::writeDimLinear(DL_WriterA& dw, dw.dxfReal(24, edata.dpy2); dw.dxfReal(34, 0.0); - dw.dxfReal(50, edata.angle/(2.0*M_PI)*360.0); + dw.dxfReal(50, edata.angle/(M_2PI)*360.0); if (version==DL_VERSION_2000) { @@ -3917,8 +3917,8 @@ void DL_Dxf::writeHatchEdge(DL_WriterA& dw, dw.dxfReal(10, data.cx); dw.dxfReal(20, data.cy); dw.dxfReal(40, data.radius); - dw.dxfReal(50, data.angle1/(2*M_PI)*360.0); - dw.dxfReal(51, data.angle2/(2*M_PI)*360.0); + dw.dxfReal(50, data.angle1/(M_2PI)*360.0); + dw.dxfReal(51, data.angle2/(M_2PI)*360.0); dw.dxfInt(73, static_cast((data.ccw))); break; @@ -3929,8 +3929,8 @@ void DL_Dxf::writeHatchEdge(DL_WriterA& dw, dw.dxfReal(11, data.mx); dw.dxfReal(21, data.my); dw.dxfReal(40, data.ratio); - dw.dxfReal(50, data.angle1/(2*M_PI)*360.0); - dw.dxfReal(51, data.angle2/(2*M_PI)*360.0); + dw.dxfReal(50, data.angle1/(M_2PI)*360.0); + dw.dxfReal(51, data.angle2/(M_2PI)*360.0); dw.dxfInt(73, static_cast((data.ccw))); break; @@ -3992,7 +3992,7 @@ int DL_Dxf::writeImage(DL_WriterA& dw, const DL_Attributes& attrib) { - /*if (data.file.empty()) + /*if (data.file.empty()) { std::cerr << "DL_Dxf::writeImage: " << "Image file must not be empty\n"; @@ -4055,7 +4055,7 @@ void DL_Dxf::writeImageDef(DL_WriterA& dw, const DL_ImageData& data) const { - /*if (data.file.empty()) + /*if (data.file.empty()) { std::cerr << "DL_Dxf::writeImage: " << "Image file must not be empty\n"; @@ -4065,7 +4065,7 @@ void DL_Dxf::writeImageDef(DL_WriterA& dw, dw.dxfString(0, "IMAGEDEF"); if (version==DL_VERSION_2000) { - dw.dxfHex(5, handle); + dw.dxfHex(5, handle); } if (version==DL_VERSION_2000) //-V581 diff --git a/src/libs/vdxf/dxflib/dl_dxf.h b/src/libs/vdxf/dxflib/dl_dxf.h index f3e9a1bd4..66d941c6d 100644 --- a/src/libs/vdxf/dxflib/dl_dxf.h +++ b/src/libs/vdxf/dxflib/dl_dxf.h @@ -39,18 +39,6 @@ #include "dl_entities.h" #include "dl_writer_ascii.h" -#ifdef _WIN32 -#undef M_PI -#define M_PI 3.14159265358979323846 -#if defined(Q_CC_MSVC) -#pragma warning(disable : 4800) -#endif // Q_CC_MSVC -#endif - -#ifndef M_PI -#define M_PI 3.1415926535897932384626433832795 -#endif - #ifndef DL_NANDOUBLE #define DL_NANDOUBLE std::numeric_limits::quiet_NaN() #endif @@ -132,7 +120,7 @@ public: DL_CreationInterface* creationInterface); static bool getStrippedLine(std::string& s, quint32 size, FILE* stream, bool stripSpace = true); - + bool readDxfGroups(std::stringstream& stream, DL_CreationInterface* creationInterface); bool in(std::stringstream &stream, diff --git a/src/libs/vgeometry/vabstractcubicbezier.cpp b/src/libs/vgeometry/vabstractcubicbezier.cpp index d86fffb7f..cc1172268 100644 --- a/src/libs/vgeometry/vabstractcubicbezier.cpp +++ b/src/libs/vgeometry/vabstractcubicbezier.cpp @@ -346,7 +346,7 @@ void VAbstractCubicBezier::PointBezier_r(qreal x1, qreal y1, qreal x2, qreal y2, double da1 = fabs(atan2(y4 - y3, x4 - x3) - atan2(y3 - y2, x3 - x2)); if (da1 >= M_PI) { - da1 = 2*M_PI - da1; + da1 = M_2PI - da1; } if (da1 < m_angle_tolerance) @@ -389,7 +389,7 @@ void VAbstractCubicBezier::PointBezier_r(qreal x1, qreal y1, qreal x2, qreal y2, double da1 = fabs(atan2(y3 - y2, x3 - x2) - atan2(y2 - y1, x2 - x1)); if (da1 >= M_PI) { - da1 = 2*M_PI - da1; + da1 = M_2PI - da1; } if (da1 < m_angle_tolerance) @@ -437,11 +437,11 @@ void VAbstractCubicBezier::PointBezier_r(qreal x1, qreal y1, qreal x2, qreal y2, double da2 = fabs(atan2(y4 - y3, x4 - x3) - k); if (da1 >= M_PI) { - da1 = 2*M_PI - da1; + da1 = M_2PI - da1; } if (da2 >= M_PI) { - da2 = 2*M_PI - da2; + da2 = M_2PI - da2; } if (da1 + da2 < m_angle_tolerance) diff --git a/src/libs/vgeometry/vabstractcurve.h b/src/libs/vgeometry/vabstractcurve.h index 6390c9c16..77d4be482 100644 --- a/src/libs/vgeometry/vabstractcurve.h +++ b/src/libs/vgeometry/vabstractcurve.h @@ -33,6 +33,10 @@ #include "../ifc/ifcdef.h" #include +#ifndef M_2PI +#define M_2PI 6.28318530717958647692528676655900576 +#endif + enum class PathDirection : char { Hide, Show }; class QPainterPath; diff --git a/src/libs/vgeometry/varc.cpp b/src/libs/vgeometry/varc.cpp index 408b692a4..208545345 100644 --- a/src/libs/vgeometry/varc.cpp +++ b/src/libs/vgeometry/varc.cpp @@ -377,7 +377,7 @@ void VArc::FindF2(qreal length) //--------------------------------------------------------------------------------------------------------------------- qreal VArc::MaxLength() const { - return 2*M_PI*d->radius; + return M_2PI*d->radius; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vgeometry/vspline.cpp b/src/libs/vgeometry/vspline.cpp index 9501fe326..69b2a85c8 100644 --- a/src/libs/vgeometry/vspline.cpp +++ b/src/libs/vgeometry/vspline.cpp @@ -32,8 +32,6 @@ #include #include -#define M_2PI 6.28318530717958647692528676655900576 - //--------------------------------------------------------------------------------------------------------------------- /** * @brief VSpline default constructor @@ -169,7 +167,7 @@ QVector VSpline::SplinePoints(const QPointF &p1, const QPointF &p4, qre p1pX.setAngle( angle1 ); qreal L = 0, radius = 0, angle = 90; radius = QLineF(QPointF(p1.x(), p4.y()), p4).length(); - L = kCurve * radius * 4 / 3 * tan( angle * M_PI / 180.0 / 4 ); + L = kCurve * radius * 4 / 3 * tan( angle * M_PI_4 / 180.0 ); QLineF p1p2(p1.x(), p1.y(), p1.x() + L * kAsm1, p1.y()); p1p2.setAngle(angle1); QLineF p4p3(p4.x(), p4.y(), p4.x() + L * kAsm2, p4.y());