From 255b77f5a89300cb01df8e2341f085fe25dfe7ed Mon Sep 17 00:00:00 2001 From: Dmitri Naumov <dmitri.naumov@ufz.de> Date: Wed, 31 May 2017 17:26:31 +0200 Subject: [PATCH] Simplify boolean expressions. See clang-tidy docu https://clang.llvm.org/extra/clang-tidy/checks/readability-simplify-boolean-expr.html --- Applications/DataExplorer/Base/OGSError.cpp | 4 +--- .../DataExplorer/VtkVis/VtkStationSource.cpp | 5 ++--- Applications/FileIO/TetGenInterface.cpp | 9 +++------ .../Utils/FileConverter/ConvertSHPToGLI.cpp | 16 ++++++++-------- GeoLib/AnalyticalGeometry.cpp | 8 ++------ GeoLib/IO/Legacy/OGSIOVer4.cpp | 10 ++-------- GeoLib/IO/XmlIO/Qt/XmlStnInterface.cpp | 5 ++--- GeoLib/Polygon.cpp | 6 +++--- GeoLib/Polyline.cpp | 5 +---- GeoLib/Raster.cpp | 9 +++++---- GeoLib/Triangle.cpp | 8 ++------ MathLib/GeometricBasics.cpp | 15 +++------------ MeshLib/Properties.cpp | 6 +----- .../ConvergenceCriterionPerComponentResidual.cpp | 2 +- Tests/GeoLib/TestLineSegmentIntersect2d.cpp | 5 +---- 15 files changed, 37 insertions(+), 76 deletions(-) diff --git a/Applications/DataExplorer/Base/OGSError.cpp b/Applications/DataExplorer/Base/OGSError.cpp index 8c2321f0c75..5f7a35e389e 100644 --- a/Applications/DataExplorer/Base/OGSError.cpp +++ b/Applications/DataExplorer/Base/OGSError.cpp @@ -41,8 +41,6 @@ bool OGSError::question(const QString &e, const QString &t) msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Cancel); - if (msgBox.exec() == QMessageBox::Ok) - return true; - return false; + return msgBox.exec() == QMessageBox::Ok; } diff --git a/Applications/DataExplorer/VtkVis/VtkStationSource.cpp b/Applications/DataExplorer/VtkVis/VtkStationSource.cpp index 9619ef66f79..3aaaa8278cc 100644 --- a/Applications/DataExplorer/VtkVis/VtkStationSource.cpp +++ b/Applications/DataExplorer/VtkVis/VtkStationSource.cpp @@ -86,9 +86,8 @@ int VtkStationSource::RequestData( vtkInformation* request, break; } - bool isBorehole = - (static_cast<GeoLib::Station*>((*_stations)[0])->type() == - GeoLib::Station::StationType::BOREHOLE) ? true : false; + bool isBorehole = static_cast<GeoLib::Station*>((*_stations)[0])->type() == + GeoLib::Station::StationType::BOREHOLE; vtkSmartPointer<vtkInformation> outInfo = outputVector->GetInformationObject(0); vtkSmartPointer<vtkPolyData> output = diff --git a/Applications/FileIO/TetGenInterface.cpp b/Applications/FileIO/TetGenInterface.cpp index 7a7da444a98..249dcc75046 100644 --- a/Applications/FileIO/TetGenInterface.cpp +++ b/Applications/FileIO/TetGenInterface.cpp @@ -107,7 +107,7 @@ std::size_t TetGenInterface::getNFacets(std::ifstream &input) auto it = fields.begin(); const auto nFacets(BaseLib::str2number<std::size_t>(*it)); if (fields.size() > 1) - _boundary_markers = (BaseLib::str2number<std::size_t> (*(++it)) == 0) ? false : true; + _boundary_markers = BaseLib::str2number<std::size_t>(*(++it)) != 0; return nFacets; } return 0; @@ -293,7 +293,7 @@ bool TetGenInterface::parseNodesFileHeader(std::string &line, } n_attributes = BaseLib::str2number<std::size_t> (*(++it)); - boundary_markers = (*(++it) == "1") ? true : false; + boundary_markers = *(++it) == "1"; return true; } @@ -416,10 +416,7 @@ bool TetGenInterface::parseElementsFileHeader(std::string &line, pos_end = line.find_first_of(" \t\n", pos_beg); if (pos_end == std::string::npos) pos_end = line.size(); - if ((line.substr(pos_beg, pos_end - pos_beg)) == "1") - region_attribute = true; - else - region_attribute = false; + region_attribute = (line.substr(pos_beg, pos_end - pos_beg)) == "1"; return true; } diff --git a/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp b/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp index 78fdfbfc6e4..2bb2ae9c878 100644 --- a/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp +++ b/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp @@ -214,14 +214,14 @@ int main (int argc, char* argv[]) fname += ".gml"; INFO("Writing to %s.", fname.c_str()); - convertPoints (dbf_handle, - fname, - x_id, - y_id, - z_id, - name_component_ids, - fname_base, - station == 0 ? true : false); + convertPoints(dbf_handle, + fname, + x_id, + y_id, + z_id, + name_component_ids, + fname_base, + station == 0); DBFClose (dbf_handle); INFO("\tok."); } else { diff --git a/GeoLib/AnalyticalGeometry.cpp b/GeoLib/AnalyticalGeometry.cpp index 5c3d96996f1..eea3e423d75 100644 --- a/GeoLib/AnalyticalGeometry.cpp +++ b/GeoLib/AnalyticalGeometry.cpp @@ -135,10 +135,7 @@ bool lineSegmentIntersect(GeoLib::LineSegment const& s0, std::size_t i) { // check if p is located at v=(a,b): (ap = t*v, t in [0,1]) - if (0.0 <= ap[i] / v[i] && ap[i] / v[i] <= 1.0) { - return true; - } - return false; + return 0.0 <= ap[i] / v[i] && ap[i] / v[i] <= 1.0; }; if (parallel(v,w)) { // original line segments (a,b) and (c,d) are parallel @@ -403,8 +400,7 @@ std::vector<MathLib::Point3d> lineSegmentIntersect2d( auto isPointOnSegment = [](double q, double p0, double p1) { double const t((q - p0) / (p1 - p0)); - if (0 <= t && t <= 1) return true; - return false; + return 0 <= t && t <= 1; }; // check if c in (ab) diff --git a/GeoLib/IO/Legacy/OGSIOVer4.cpp b/GeoLib/IO/Legacy/OGSIOVer4.cpp index 4ce311cf2e1..c845d6b1120 100644 --- a/GeoLib/IO/Legacy/OGSIOVer4.cpp +++ b/GeoLib/IO/Legacy/OGSIOVer4.cpp @@ -70,10 +70,7 @@ std::string readPoints(std::istream &in, std::vector<GeoLib::Point*>* pnt_vec, { if (cnt == 0) { - if (id == 0) - zero_based_indexing = true; - else - zero_based_indexing = false; + zero_based_indexing = id == 0; } pnt_vec->push_back(new GeoLib::Point(x, y, z, id)); @@ -519,10 +516,7 @@ bool readGLIFileV4(const std::string& fname, std::move(sfc_vec), unique_name, std::move(sfc_names)); // KR: insert into GEOObjects if not empty - if (errors.empty()) - return true; - - return false; + return errors.empty(); } std::size_t writeTINSurfaces(std::ofstream &os, GeoLib::SurfaceVec const* sfcs_vec, std::size_t sfc_count, std::string const& path) diff --git a/GeoLib/IO/XmlIO/Qt/XmlStnInterface.cpp b/GeoLib/IO/XmlIO/Qt/XmlStnInterface.cpp index 07d82ce8fc1..d6fd8122ec3 100644 --- a/GeoLib/IO/XmlIO/Qt/XmlStnInterface.cpp +++ b/GeoLib/IO/XmlIO/Qt/XmlStnInterface.cpp @@ -211,9 +211,8 @@ bool XmlStnInterface::write() root.setAttribute( "xsi:noNamespaceSchemaLocation", "http://www.opengeosys.org/images/xsd/OpenGeoSysSTN.xsd" ); const std::vector<GeoLib::Point*>* stations (_geo_objs.getStationVec(_exportName)); - bool isBorehole = - (static_cast<GeoLib::Station*>((*stations)[0])->type() == - GeoLib::Station::StationType::BOREHOLE) ? true : false; + bool isBorehole = static_cast<GeoLib::Station*>((*stations)[0])->type() == + GeoLib::Station::StationType::BOREHOLE; doc.appendChild(root); QDomElement stationListTag = doc.createElement("stationlist"); diff --git a/GeoLib/Polygon.cpp b/GeoLib/Polygon.cpp index 52b3d770bcf..44449d42878 100644 --- a/GeoLib/Polygon.cpp +++ b/GeoLib/Polygon.cpp @@ -183,9 +183,9 @@ bool Polygon::containsSegment(GeoLib::LineSegment const& segment) const if (!isPntInPolygon(GeoLib::Point(0.5*(s[k][0]+s[k+1][0]), 0.5*(s[k][1]+s[k+1][1]), 0.5*(s[k][2]+s[k+1][2])))) return false; } - if (!isPntInPolygon(GeoLib::Point(0.5*(s[0][0]+b[0]), 0.5*(s[0][1]+b[1]), 0.5*(s[0][2]+b[2])))) - return false; - return true; + return isPntInPolygon(GeoLib::Point(0.5 * (s[0][0] + b[0]), + 0.5 * (s[0][1] + b[1]), + 0.5 * (s[0][2] + b[2]))); } bool Polygon::isPolylineInPolygon(const Polyline& ply) const diff --git a/GeoLib/Polyline.cpp b/GeoLib/Polyline.cpp index 0b0beb9e476..4f6eb04798c 100644 --- a/GeoLib/Polyline.cpp +++ b/GeoLib/Polyline.cpp @@ -192,10 +192,7 @@ bool Polyline::isClosed() const if (_ply_pnt_ids.size() < 3) return false; - if (_ply_pnt_ids.front() == _ply_pnt_ids.back()) - return true; - - return false; + return _ply_pnt_ids.front() == _ply_pnt_ids.back(); } bool Polyline::isCoplanar() const diff --git a/GeoLib/Raster.cpp b/GeoLib/Raster.cpp index 8c2b5e86246..625d37dd808 100644 --- a/GeoLib/Raster.cpp +++ b/GeoLib/Raster.cpp @@ -175,10 +175,11 @@ double Raster::interpolateValueAtPoint(MathLib::Point3d const& pnt) const bool Raster::isPntOnRaster(MathLib::Point3d const& pnt) const { - if ((pnt[0]<_header.origin[0]) || (pnt[0]>_header.origin[0]+(_header.n_cols*_header.cell_size)) || - (pnt[1]<_header.origin[1]) || (pnt[1]>_header.origin[1]+(_header.n_rows*_header.cell_size))) - return false; - return true; + return !( + (pnt[0] < _header.origin[0]) || + (pnt[0] > _header.origin[0] + (_header.n_cols * _header.cell_size)) || + (pnt[1] < _header.origin[1]) || + (pnt[1] > _header.origin[1] + (_header.n_rows * _header.cell_size))); } } // end namespace GeoLib diff --git a/GeoLib/Triangle.cpp b/GeoLib/Triangle.cpp index 73294479d5b..1b6ec36420e 100644 --- a/GeoLib/Triangle.cpp +++ b/GeoLib/Triangle.cpp @@ -63,12 +63,8 @@ bool Triangle::containsPoint2D (Point const& pnt) const const double upper (1+delta); // check if u0 and u1 fulfills the condition (with some delta) - if (-delta <= y[0] && y[0] <= upper && -delta <= y[1] && y[1] <= upper && - y[0] + y[1] <= upper) - { - return true; - } - return false; + return -delta <= y[0] && y[0] <= upper && -delta <= y[1] && y[1] <= upper && + y[0] + y[1] <= upper; } void getPlaneCoefficients(Triangle const& tri, double c[3]) diff --git a/MathLib/GeometricBasics.cpp b/MathLib/GeometricBasics.cpp index 55c6be70832..a80e22aaccc 100644 --- a/MathLib/GeometricBasics.cpp +++ b/MathLib/GeometricBasics.cpp @@ -73,9 +73,7 @@ bool isPointInTetrahedron(MathLib::Point3d const& p, MathLib::Point3d const& a, return false; // if p is on the same side of abc as d double const d4 (MathLib::orientation3d(p, a, b, c)); - if (!(d0_sign == (d4>=0) || std::abs(d4) < eps)) - return false; - return true; + return d0_sign == (d4 >= 0) || std::abs(d4) < eps; } return false; } @@ -164,9 +162,7 @@ bool barycentricPointInTriangle(MathLib::Point3d const& p, if (beta < -eps_pnt_out_of_tri || beta > 1 + eps_pnt_out_of_tri) return false; double const gamma(1 - alpha - beta); - if (gamma < -eps_pnt_out_of_tri || gamma > 1 + eps_pnt_out_of_tri) - return false; - return true; + return !(gamma < -eps_pnt_out_of_tri || gamma > 1 + eps_pnt_out_of_tri); } bool isPointInTriangleXY(MathLib::Point3d const& p, @@ -186,12 +182,7 @@ bool isPointInTriangleXY(MathLib::Point3d const& p, gauss.solve(mat, y, true); // check if u0 and u1 fulfills the condition - if (0 <= y[0] && y[0] <= 1 && 0 <= y[1] && y[1] <= 1 && y[0] + y[1] <= 1) - { - return true; - } - return false; - + return 0 <= y[0] && y[0] <= 1 && 0 <= y[1] && y[1] <= 1 && y[0] + y[1] <= 1; } bool dividedByPlane(const MathLib::Point3d& a, const MathLib::Point3d& b, diff --git a/MeshLib/Properties.cpp b/MeshLib/Properties.cpp index cf8a1aa3997..a646656dfd5 100644 --- a/MeshLib/Properties.cpp +++ b/MeshLib/Properties.cpp @@ -32,11 +32,7 @@ void Properties::removePropertyVector(std::string const& name) bool Properties::hasPropertyVector(std::string const& name) const { - auto it(_properties.find(name)); - if (it == _properties.end()) { - return false; - } - return true; + return _properties.find(name) != _properties.end(); } std::vector<std::string> Properties::getPropertyVectorNames() const diff --git a/NumLib/ODESolver/ConvergenceCriterionPerComponentResidual.cpp b/NumLib/ODESolver/ConvergenceCriterionPerComponentResidual.cpp index ab6de3302ca..29600a81995 100644 --- a/NumLib/ODESolver/ConvergenceCriterionPerComponentResidual.cpp +++ b/NumLib/ODESolver/ConvergenceCriterionPerComponentResidual.cpp @@ -68,7 +68,7 @@ void ConvergenceCriterionPerComponentResidual::checkResidual( bool satisfied_abs = true; // Make sure that in the first iteration the relative residual tolerance is // not satisfied. - bool satisfied_rel = _is_first_iteration ? false : true; + bool satisfied_rel = !_is_first_iteration; for (unsigned global_component = 0; global_component < _abstols.size(); ++global_component) diff --git a/Tests/GeoLib/TestLineSegmentIntersect2d.cpp b/Tests/GeoLib/TestLineSegmentIntersect2d.cpp index 285a476f394..b04db18c06d 100644 --- a/Tests/GeoLib/TestLineSegmentIntersect2d.cpp +++ b/Tests/GeoLib/TestLineSegmentIntersect2d.cpp @@ -72,10 +72,7 @@ TEST_F(LineSegmentIntersect2dTest, RandomSegmentOrientationIntersecting) {(s0.getBeginPoint()[0] + s0.getEndPoint()[0]) / 2, (s0.getBeginPoint()[1] + s0.getEndPoint()[1]) / 2, 0.0}}}; const double sqr_dist(MathLib::sqrDist(ipnts[0], center)); - if (sqr_dist < std::numeric_limits<double>::epsilon()) - return true; - - return false; + return sqr_dist < std::numeric_limits<double>::epsilon(); } return ipnts.size() == 2; }; -- GitLab