From cd29dcf6e070b9572ef2833035a20c9cd1bb17be Mon Sep 17 00:00:00 2001 From: Tomasz Wlostowski Date: Mon, 24 Oct 2022 00:20:02 +0200 Subject: [PATCH] libs/kimath: SHAPE::Format() can now produce C++ object construction and simple test dump of shapes --- libs/kimath/include/geometry/shape.h | 2 +- libs/kimath/include/geometry/shape_circle.h | 2 + libs/kimath/include/geometry/shape_compound.h | 2 +- .../include/geometry/shape_line_chain.h | 2 +- libs/kimath/include/geometry/shape_poly_set.h | 2 +- libs/kimath/include/geometry/shape_rect.h | 2 +- libs/kimath/include/geometry/shape_segment.h | 2 +- libs/kimath/src/geometry/shape.cpp | 7 +-- libs/kimath/src/geometry/shape_compound.cpp | 2 +- libs/kimath/src/geometry/shape_line_chain.cpp | 2 +- libs/kimath/src/geometry/shape_poly_set.cpp | 2 +- libs/kimath/src/geometry/shape_rect.cpp | 2 +- libs/kimath/src/geometry/shape_segment.cpp | 45 ++++++++++++++++++- 13 files changed, 60 insertions(+), 14 deletions(-) diff --git a/libs/kimath/include/geometry/shape.h b/libs/kimath/include/geometry/shape.h index e08751bd7a..b6767b101d 100644 --- a/libs/kimath/include/geometry/shape.h +++ b/libs/kimath/include/geometry/shape.h @@ -243,7 +243,7 @@ public: virtual bool Parse( std::stringstream& aStream ); - virtual const std::string Format( ) const; + virtual const std::string Format( bool aCplusPlus = true ) const; protected: typedef VECTOR2I::extended_type ecoord; diff --git a/libs/kimath/include/geometry/shape_circle.h b/libs/kimath/include/geometry/shape_circle.h index 3ea482b13d..d865ffcf3a 100644 --- a/libs/kimath/include/geometry/shape_circle.h +++ b/libs/kimath/include/geometry/shape_circle.h @@ -135,6 +135,8 @@ public: return true; } + virtual const std::string Format( bool aCplusPlus = true ) const override; + private: CIRCLE m_circle; }; diff --git a/libs/kimath/include/geometry/shape_compound.h b/libs/kimath/include/geometry/shape_compound.h index 6723060680..c4c333cb03 100644 --- a/libs/kimath/include/geometry/shape_compound.h +++ b/libs/kimath/include/geometry/shape_compound.h @@ -51,7 +51,7 @@ public: ~SHAPE_COMPOUND(); SHAPE_COMPOUND* Clone() const override; - const std::string Format() const override; + const std::string Format( bool aCplusPlus = true ) const override; bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr, VECTOR2I* aLocation = nullptr ) const override; diff --git a/libs/kimath/include/geometry/shape_line_chain.h b/libs/kimath/include/geometry/shape_line_chain.h index c316cf2383..1b18b3a0d1 100644 --- a/libs/kimath/include/geometry/shape_line_chain.h +++ b/libs/kimath/include/geometry/shape_line_chain.h @@ -714,7 +714,7 @@ public: const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const; /// @copydoc SHAPE::Format() - const std::string Format() const override; + const std::string Format( bool aCplusPlus = true ) const override; /// @copydoc SHAPE::Parse() bool Parse( std::stringstream& aStream ) override; diff --git a/libs/kimath/include/geometry/shape_poly_set.h b/libs/kimath/include/geometry/shape_poly_set.h index 37567b7c7d..4eed219fa0 100644 --- a/libs/kimath/include/geometry/shape_poly_set.h +++ b/libs/kimath/include/geometry/shape_poly_set.h @@ -1054,7 +1054,7 @@ public: int NormalizeAreaOutlines(); /// @copydoc SHAPE::Format() - const std::string Format() const override; + const std::string Format( bool aCplusPlus = true ) const override; /// @copydoc SHAPE::Parse() bool Parse( std::stringstream& aStream ) override; diff --git a/libs/kimath/include/geometry/shape_rect.h b/libs/kimath/include/geometry/shape_rect.h index 0c02260706..58d970eabd 100644 --- a/libs/kimath/include/geometry/shape_rect.h +++ b/libs/kimath/include/geometry/shape_rect.h @@ -188,7 +188,7 @@ public: return rv; } - virtual const std::string Format( ) const override; + virtual const std::string Format( bool aCplusPlus = true ) const override; private: VECTOR2I m_p0; ///< Top-left corner diff --git a/libs/kimath/include/geometry/shape_segment.h b/libs/kimath/include/geometry/shape_segment.h index 4e5df58c79..02e61ef2fd 100644 --- a/libs/kimath/include/geometry/shape_segment.h +++ b/libs/kimath/include/geometry/shape_segment.h @@ -157,7 +157,7 @@ public: m_seg.B += aVector; } - virtual const std::string Format( ) const override; + virtual const std::string Format( bool aCplusPlus = true ) const override; private: SEG m_seg; diff --git a/libs/kimath/src/geometry/shape.cpp b/libs/kimath/src/geometry/shape.cpp index a52cbe81c3..95226b8e2f 100644 --- a/libs/kimath/src/geometry/shape.cpp +++ b/libs/kimath/src/geometry/shape.cpp @@ -38,10 +38,11 @@ bool SHAPE::Parse( std::stringstream& aStream ) } -const std::string SHAPE::Format() const +const std::string SHAPE::Format( bool aCplusPlus ) const { - assert( false ); - return std::string( "" ); + std::stringstream ss; + ss << "shape " << m_type; + return ss.str(); } diff --git a/libs/kimath/src/geometry/shape_compound.cpp b/libs/kimath/src/geometry/shape_compound.cpp index 9b7df5642b..8510a47f06 100644 --- a/libs/kimath/src/geometry/shape_compound.cpp +++ b/libs/kimath/src/geometry/shape_compound.cpp @@ -27,7 +27,7 @@ #include -const std::string SHAPE_COMPOUND::Format() const +const std::string SHAPE_COMPOUND::Format( bool aCplusPlus ) const { std::stringstream ss; diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index 41e1d32ed7..082ff367ae 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -1974,7 +1974,7 @@ int SHAPE_LINE_CHAIN::NearestSegment( const VECTOR2I& aP ) const } -const std::string SHAPE_LINE_CHAIN::Format() const +const std::string SHAPE_LINE_CHAIN::Format( bool aCplusPlus ) const { std::stringstream ss; diff --git a/libs/kimath/src/geometry/shape_poly_set.cpp b/libs/kimath/src/geometry/shape_poly_set.cpp index 1fe86f8245..129613541f 100644 --- a/libs/kimath/src/geometry/shape_poly_set.cpp +++ b/libs/kimath/src/geometry/shape_poly_set.cpp @@ -1624,7 +1624,7 @@ int SHAPE_POLY_SET::NormalizeAreaOutlines() } -const std::string SHAPE_POLY_SET::Format() const +const std::string SHAPE_POLY_SET::Format( bool aCplusPlus ) const { std::stringstream ss; diff --git a/libs/kimath/src/geometry/shape_rect.cpp b/libs/kimath/src/geometry/shape_rect.cpp index d2f9664316..55ad247874 100644 --- a/libs/kimath/src/geometry/shape_rect.cpp +++ b/libs/kimath/src/geometry/shape_rect.cpp @@ -91,7 +91,7 @@ bool SHAPE_RECT::Collide( const SEG& aSeg, int aClearance, int* aActual, VECTOR2 return false; } -const std::string SHAPE_RECT::Format( ) const +const std::string SHAPE_RECT::Format( bool aCplusPlus ) const { std::stringstream ss; diff --git a/libs/kimath/src/geometry/shape_segment.cpp b/libs/kimath/src/geometry/shape_segment.cpp index 4c4d093b3e..a43094ea20 100644 --- a/libs/kimath/src/geometry/shape_segment.cpp +++ b/libs/kimath/src/geometry/shape_segment.cpp @@ -25,11 +25,14 @@ #include #include +#include -const std::string SHAPE_SEGMENT::Format() const +const std::string SHAPE_SEGMENT::Format( bool aCplusPlus ) const { std::stringstream ss; + if( aCplusPlus ) + { ss << "SHAPE_SEGMENT( VECTOR2I( "; ss << m_seg.A.x; ss << ", "; @@ -41,6 +44,46 @@ const std::string SHAPE_SEGMENT::Format() const ss << "), "; ss << m_width; ss << "); "; + } + else + { + ss << SHAPE::Format( aCplusPlus ) << " "; + ss << m_seg.A.x; + ss << " "; + ss << m_seg.A.y; + ss << " "; + ss << m_seg.B.x; + ss << " "; + ss << m_seg.B.y; + ss << " "; + ss << m_width; + } return ss.str(); } + +const std::string SHAPE_CIRCLE::Format( bool aCplusPlus ) const +{ + std::stringstream ss; + + if( aCplusPlus ) + { + ss << "SHAPE_CIRCLE( VECTOR2I( "; + ss << m_circle.Center.x; + ss << ", "; + ss << m_circle.Center.y; + ss << "), "; + ss << m_circle.Radius; + ss << "); "; + } else + { + ss << SHAPE::Format( aCplusPlus ) << " "; + ss << m_circle.Center.x; + ss << " "; + ss << m_circle.Center.y; + ss << " "; + ss << m_circle.Radius; + } + return ss.str(); +} +