From 34ea056c5d45a503bb7dd94f5e0be7d8991717a4 Mon Sep 17 00:00:00 2001 From: scttgs0 Date: Sun, 14 May 2023 02:46:13 -0500 Subject: [PATCH] [closedpoly] Maintenance --- src/Airports/GenAirports/closedpoly.cxx | 410 ++++++++++-------------- src/Airports/GenAirports/closedpoly.hxx | 3 +- 2 files changed, 179 insertions(+), 234 deletions(-) diff --git a/src/Airports/GenAirports/closedpoly.cxx b/src/Airports/GenAirports/closedpoly.cxx index 90416c67..e8bdc044 100644 --- a/src/Airports/GenAirports/closedpoly.cxx +++ b/src/Airports/GenAirports/closedpoly.cxx @@ -1,34 +1,30 @@ -#include -#include - #include #include #include -#include "global.hxx" #include "beznode.hxx" #include "closedpoly.hxx" +#include "global.hxx" -#define NO_BEZIER (0) +#define NO_BEZIER (0) -static void stringPurifier( std::string& s ) +static void stringPurifier(std::string& s) { - for ( std::string::iterator it = s.begin(), itEnd = s.end(); it!=itEnd; ++it) { - if ( static_cast(*it) < 32 || static_cast(*it) > 127 ) { + for (std::string::iterator it = s.begin(), itEnd = s.end(); it != itEnd; ++it) { + if (static_cast(*it) < 32 || static_cast(*it) > 127) { (*it) = ' '; } } } -ClosedPoly::ClosedPoly( char* desc ) : - is_pavement(false), - is_border(false), - has_feature(false), - surface_type(0), - smoothness(0.0), - texture_heading(0.0), - description(std::string(desc ? desc : "none")) +ClosedPoly::ClosedPoly(char* desc) : is_pavement(false), + is_border(false), + has_feature(false), + surface_type(0), + smoothness(0.0), + texture_heading(0.0), + description(std::string(desc ? desc : "none")) { is_border = true; @@ -38,41 +34,37 @@ ClosedPoly::ClosedPoly( char* desc ) : cur_contour.clear(); } -ClosedPoly::ClosedPoly( int st, float s, float th, char* desc ) : - ClosedPoly( desc ) +ClosedPoly::ClosedPoly(int st, float s, float th, char* desc) : ClosedPoly(desc) { surface_type = st; - smoothness = s; + smoothness = s; texture_heading = th; - is_pavement = (surface_type != 15) ? true : false; // wrong?? + is_pavement = (surface_type != 15) ? true : false; // wrong?? is_border = false; has_feature = true; } ClosedPoly::~ClosedPoly() { - TG_LOG( SG_GENERAL, SG_DEBUG, "Deleting ClosedPoly " << description ); + TG_LOG(SG_GENERAL, SG_DEBUG, "Deleting ClosedPoly " << description); } -void ClosedPoly::AddNode( std::shared_ptr node ) +void ClosedPoly::AddNode(std::shared_ptr node) { - cur_contour.push_back( node ); + using namespace std::string_literals; - TG_LOG(SG_GENERAL, SG_DEBUG, "CLOSEDPOLY::ADDNODE : " << node->GetLoc() ); + cur_contour.push_back(node); + + TG_LOG(SG_GENERAL, SG_DEBUG, "CLOSEDPOLY::ADDNODE : " << node->GetLoc()); // For pavement polys, add a linear feature for each contour - if (has_feature) - { - if (!cur_feature) - { - std::string feature_desc = description + " - "; - if (boundary.size() == 0) - { + if (has_feature) { + if (!cur_feature) { + std::string feature_desc = description + " - "s; + if (boundary.size() == 0) { feature_desc += "hole"; - } - else - { + } else { feature_desc += "boundary"; } @@ -80,7 +72,7 @@ void ClosedPoly::AddNode( std::shared_ptr node ) TG_LOG(SG_GENERAL, SG_DEBUG, " Adding node " << node->GetLoc() << " to current linear feature " << cur_feature); } - cur_feature->AddNode( node ); + cur_feature->AddNode(node); } } @@ -90,10 +82,10 @@ void ClosedPoly::CloseCurContour() // if we are recording a pavement marking - it must be closed - // add the first node of the poly - if (cur_feature) - { + if (cur_feature) { TG_LOG(SG_GENERAL, SG_DEBUG, "We still have an active linear feature - add the first node to close it"); - cur_feature->Finish(true, features.size() ); + + cur_feature->Finish(true, features.size()); features.push_back(cur_feature); cur_feature.reset(); @@ -102,35 +94,32 @@ void ClosedPoly::CloseCurContour() // add the contour to the poly - first one is the outer boundary // subsequent contours are holes - if ( boundary.size() == 0 ) - { + if (boundary.size() == 0) { boundary = cur_contour; // generate the convex hull from the bezcontour node locations // CreateConvexHull(); cur_contour.clear(); - } - else - { - holes.push_back( cur_contour ); + } else { + holes.push_back(cur_contour); cur_contour.clear(); } } -void ClosedPoly::ConvertContour( const BezContour& src, tgContour& dst ) +void ClosedPoly::ConvertContour(const BezContour& src, tgContour& dst) { - std::shared_ptr curNode; - std::shared_ptr nextNode; + std::shared_ptr curNode; + std::shared_ptr nextNode; SGGeod curLoc; SGGeod nextLoc; SGGeod cp1; SGGeod cp2; - int curve_type; - double total_dist; - int num_segs; + int curve_type; + double total_dist; + int num_segs; TG_LOG(SG_GENERAL, SG_DEBUG, "Creating a contour with " << src.size() << " nodes"); @@ -138,90 +127,66 @@ void ClosedPoly::ConvertContour( const BezContour& src, tgContour& dst ) dst.Erase(); // iterate through each bezier node in the contour - for (unsigned int i = 0; i < src.size(); ++i) - { + for (unsigned int i = 0; i < src.size(); ++i) { TG_LOG(SG_GENERAL, SG_DEBUG, "\nHandling Node " << i << "\n\n"); curNode = src.at(i); - if (i < src.size() - 1) - { + if (i < src.size() - 1) { nextNode = src.at(i + 1); - } - else - { + } else { // for the last node, next is the first node, as all contours are closed nextNode = src.at(0); } // now determine how we will iterate from current node to next node - if( curNode->HasNextCp() ) - { + if (curNode->HasNextCp()) { // next curve is cubic or quadratic - if( nextNode->HasPrevCp() ) - { + if (nextNode->HasPrevCp()) { // curve is cubic : need both control points curve_type = CURVE_CUBIC; cp1 = curNode->GetNextCp(); cp2 = nextNode->GetPrevCp(); - total_dist = CubicDistance( curNode->GetLoc(), cp1, cp2, nextNode->GetLoc() ); - } - else - { + total_dist = CubicDistance(curNode->GetLoc(), cp1, cp2, nextNode->GetLoc()); + } else { // curve is quadratic using current nodes cp as the cp curve_type = CURVE_QUADRATIC; cp1 = curNode->GetNextCp(); - total_dist = QuadraticDistance( curNode->GetLoc(), cp1, nextNode->GetLoc() ); + total_dist = QuadraticDistance(curNode->GetLoc(), cp1, nextNode->GetLoc()); } - } - else - { + } else { // next curve is quadratic or linear - if( nextNode->HasPrevCp() ) - { + if (nextNode->HasPrevCp()) { // curve is quadratic using next nodes cp as the cp curve_type = CURVE_QUADRATIC; cp1 = nextNode->GetPrevCp(); - total_dist = QuadraticDistance( curNode->GetLoc(), cp1, nextNode->GetLoc() ); - } - else - { + total_dist = QuadraticDistance(curNode->GetLoc(), cp1, nextNode->GetLoc()); + } else { // curve is linear curve_type = CURVE_LINEAR; - total_dist = LinearDistance( curNode->GetLoc(), nextNode->GetLoc() ); + total_dist = LinearDistance(curNode->GetLoc(), nextNode->GetLoc()); } } - if (total_dist < 8.0f) - { - if (curve_type != CURVE_LINEAR) - { + if (total_dist < 8.0f) { + if (curve_type != CURVE_LINEAR) { // If total distance is < 4 meters, then we need to modify num Segments so that each segment >= 2 meters num_segs = ((int)total_dist + 1); - TG_LOG(SG_GENERAL, SG_DEBUG, "Segment from " << curNode->GetLoc() << " to " << nextNode->GetLoc() ); - TG_LOG(SG_GENERAL, SG_DEBUG, " Distance is " << total_dist << " ( < 16.0) so num_segs is " << num_segs ); - } - else - { + TG_LOG(SG_GENERAL, SG_DEBUG, "Segment from " << curNode->GetLoc() << " to " << nextNode->GetLoc()); + TG_LOG(SG_GENERAL, SG_DEBUG, " Distance is " << total_dist << " ( < 16.0) so num_segs is " << num_segs); + } else { num_segs = 1; } - } - else if (total_dist > 800.0f) - { + } else if (total_dist > 800.0f) { // If total distance is > 800 meters, then we need to modify num Segments so that each segment <= 100 meters num_segs = total_dist / 100.0f + 1; - TG_LOG(SG_GENERAL, SG_DEBUG, "Segment from " << curNode->GetLoc() << " to " << nextNode->GetLoc() ); - TG_LOG(SG_GENERAL, SG_DEBUG, " Distance is " << total_dist << " ( > 100.0) so num_segs is " << num_segs ); - } - else - { - if (curve_type != CURVE_LINEAR) - { + TG_LOG(SG_GENERAL, SG_DEBUG, "Segment from " << curNode->GetLoc() << " to " << nextNode->GetLoc()); + TG_LOG(SG_GENERAL, SG_DEBUG, " Distance is " << total_dist << " ( > 100.0) so num_segs is " << num_segs); + } else { + if (curve_type != CURVE_LINEAR) { num_segs = 8; - TG_LOG(SG_GENERAL, SG_DEBUG, "Segment from " << curNode->GetLoc() << " to " << nextNode->GetLoc() ); - TG_LOG(SG_GENERAL, SG_DEBUG, " Distance is " << total_dist << " (OK) so num_segs is " << num_segs ); - } - else - { + TG_LOG(SG_GENERAL, SG_DEBUG, "Segment from " << curNode->GetLoc() << " to " << nextNode->GetLoc()); + TG_LOG(SG_GENERAL, SG_DEBUG, " Distance is " << total_dist << " (OK) so num_segs is " << num_segs); + } else { // make sure linear segments don't got over 100m num_segs = total_dist / 100.0f + 1; } @@ -232,78 +197,60 @@ void ClosedPoly::ConvertContour( const BezContour& src, tgContour& dst ) #endif // if only one segment, revert to linear - if (num_segs == 1) - { + if (num_segs == 1) { curve_type = CURVE_LINEAR; } // initialize current location curLoc = curNode->GetLoc(); - if (curve_type != CURVE_LINEAR) - { - for (int p=0; pGetLoc(), cp1, nextNode->GetLoc(), (1.0f/num_segs) * (p+1) ); - } - else - { - nextLoc = CalculateCubicLocation( curNode->GetLoc(), cp1, cp2, nextNode->GetLoc(), (1.0f/num_segs) * (p+1) ); + if (curve_type == CURVE_QUADRATIC) { + nextLoc = CalculateQuadraticLocation(curNode->GetLoc(), cp1, nextNode->GetLoc(), (1.0f / num_segs) * (p + 1)); + } else { + nextLoc = CalculateCubicLocation(curNode->GetLoc(), cp1, cp2, nextNode->GetLoc(), (1.0f / num_segs) * (p + 1)); } // add the pavement vertex // convert from lat/lon to geo // (maybe later) - check some simgear objects... - dst.AddNode( curLoc ); + dst.AddNode(curLoc); - if (p==0) - { - TG_LOG(SG_GENERAL, SG_DEBUG, "adding Curve Anchor node (type " << curve_type << ") at " << curLoc ); - } - else - { - TG_LOG(SG_GENERAL, SG_DEBUG, " add bezier node (type " << curve_type << ") at " << curLoc ); + if (p == 0) { + TG_LOG(SG_GENERAL, SG_DEBUG, "adding Curve Anchor node (type " << curve_type << ") at " << curLoc); + } else { + TG_LOG(SG_GENERAL, SG_DEBUG, " add bezier node (type " << curve_type << ") at " << curLoc); } // now set set cur location for the next iteration curLoc = nextLoc; } - } - else - { - if (num_segs > 1) - { - for (int p=0; p 1) { + for (int p = 0; p < num_segs; ++p) { // calculate next location - nextLoc = CalculateLinearLocation( curNode->GetLoc(), nextNode->GetLoc(), (1.0f/num_segs) * (p+1) ); + nextLoc = CalculateLinearLocation(curNode->GetLoc(), nextNode->GetLoc(), (1.0f / num_segs) * (p + 1)); // add the feature vertex - dst.AddNode( curLoc ); + dst.AddNode(curLoc); - if (p==0) - { - TG_LOG(SG_GENERAL, SG_DEBUG, "adding Linear anchor node at " << curLoc ); - } - else - { - TG_LOG(SG_GENERAL, SG_DEBUG, " add linear node at " << curLoc ); + if (p == 0) { + TG_LOG(SG_GENERAL, SG_DEBUG, "adding Linear anchor node at " << curLoc); + } else { + TG_LOG(SG_GENERAL, SG_DEBUG, " add linear node at " << curLoc); } // now set set prev and cur locations for the next iteration curLoc = nextLoc; } - } - else - { + } else { nextLoc = nextNode->GetLoc(); // just add the one vertex - dist is small - dst.AddNode( curLoc ); + dst.AddNode(curLoc); - TG_LOG(SG_GENERAL, SG_DEBUG, "adding Linear Anchor node at " << curLoc ); + TG_LOG(SG_GENERAL, SG_DEBUG, "adding Linear Anchor node at " << curLoc); curLoc = nextLoc; } @@ -314,36 +261,33 @@ void ClosedPoly::ConvertContour( const BezContour& src, tgContour& dst ) // finish the poly - convert to TGPolygon, and tesselate void ClosedPoly::Finish() { - tgContour dst_contour; + tgContour dst_contour; // error handling - if (boundary.size() == 0) - { + if (boundary.size() == 0) { TG_LOG(SG_GENERAL, SG_ALERT, "no boundary"); } TG_LOG(SG_GENERAL, SG_DEBUG, "Converting a poly with " << holes.size() << " holes"); - if (boundary.size() != 0) - { + if (boundary.size() != 0) { // create the boundary - ConvertContour( boundary, dst_contour ); - dst_contour.SetHole( false ); + ConvertContour(boundary, dst_contour); + dst_contour.SetHole(false); // and add it to the geometry - pre_tess.AddContour( dst_contour ); + pre_tess.AddContour(dst_contour); // Then convert the hole contours - for (unsigned int i=0; i