KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_graphics_importer_buffer.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <eda_item.h>
28#include <base_units.h>
29#include <limits>
30
31
33{
34public:
39
40 void AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
41 const IMPORTED_STROKE& aStroke ) override
42 {
43 m_lines.push_back( { aStart, aEnd } );
44 }
45
46 void AddCircle( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
47 bool aFilled, const COLOR4D& aFillColor ) override {}
48
49 void AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
50 const IMPORTED_STROKE& aStroke ) override {}
51
52 void AddPolygon( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
53 bool aFilled, const COLOR4D& aFillColor ) override {}
54
55 void AddText( const VECTOR2D& aOrigin, const wxString& aText, double aHeight, double aWidth,
56 double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
57 GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor ) override {}
58
59 void AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
60 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
61 const IMPORTED_STROKE& aStroke ) override {}
62
63 std::vector<std::pair<VECTOR2D, VECTOR2D>> m_lines;
64};
65
66
67BOOST_AUTO_TEST_SUITE( GraphicsImporterBuffer )
68
69
70
77BOOST_AUTO_TEST_CASE( LargeCoordinatesAutoOffset )
78{
81
82 // Add a line with coordinates that would overflow INT32 when converted to IU
83 // 2300mm * 1e6 IU/mm = 2.3e9 > INT32_MAX (2.147e9)
84 VECTOR2D start( 2300.0, 1400.0 );
85 VECTOR2D end( 2350.0, 1450.0 );
86
87 IMPORTED_STROKE stroke( 0.1 );
88 buffer.AddLine( start, end, stroke );
89
90 // Import with no offset set (default is 0,0)
91 BOOST_CHECK( importer.GetImportOffsetMM() == VECTOR2D( 0, 0 ) );
92
93 buffer.ImportTo( importer );
94
95 // After import, the offset should have been automatically adjusted
96 // to move the coordinates into the valid range
97 VECTOR2D offset = importer.GetImportOffsetMM();
98
99 // The offset should be negative of the bounding box origin to bring coordinates near zero
100 BOOST_CHECK_MESSAGE( offset.x < 0, "X offset should be negative to shift large coords down" );
101
102 // Verify the resulting coordinates are within valid IU range
103 if( !importer.m_lines.empty() )
104 {
105 VECTOR2D importedStart = importer.m_lines[0].first;
106 VECTOR2D importedEnd = importer.m_lines[0].second;
107
108 // After offset and scale, coordinates should be manageable
109 double maxCoord = static_cast<double>( std::numeric_limits<int>::max() )
110 / importer.GetMillimeterToIuFactor();
111
112 BOOST_CHECK_MESSAGE( ( importedStart.x + offset.x ) < maxCoord,
113 "Imported X coord should be within valid range" );
114 BOOST_CHECK_MESSAGE( ( importedStart.y + offset.y ) < maxCoord,
115 "Imported Y coord should be within valid range" );
116 }
117}
118
119
123BOOST_AUTO_TEST_CASE( NormalCoordinatesNoOffset )
124{
126 TEST_GRAPHICS_IMPORTER importer;
127
128 // Add a line with normal coordinates that fit within the valid range
129 // 100mm * 1e6 IU/mm = 1e8 << INT32_MAX
130 VECTOR2D start( 100.0, 100.0 );
131 VECTOR2D end( 200.0, 200.0 );
132
133 IMPORTED_STROKE stroke( 0.1 );
134 buffer.AddLine( start, end, stroke );
135
136 buffer.ImportTo( importer );
137
138 // Offset should remain at zero since coordinates are valid
139 VECTOR2D offset = importer.GetImportOffsetMM();
140 BOOST_CHECK_MESSAGE( offset == VECTOR2D( 0, 0 ),
141 "Normal coordinates should not trigger auto-offset" );
142}
143
144
constexpr double PCB_IU_PER_MM
Pcbnew IU is 1 nanometer.
Definition base_units.h:70
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing a line segment.
void ImportTo(GRAPHICS_IMPORTER &aImporter)
double m_millimeterToIu
Factor to convert millimeters to Internal Units.
const VECTOR2D & GetImportOffsetMM() const
A clone of IMPORTED_STROKE, but with floating-point width.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing a line segment.
void AddCircle(const VECTOR2D &aCenter, double aRadius, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor) override
Create an object representing a circle.
void AddText(const VECTOR2D &aOrigin, const wxString &aText, double aHeight, double aWidth, double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify, GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D &aColor) override
Create an object representing a text.
std::vector< std::pair< VECTOR2D, VECTOR2D > > m_lines
void AddPolygon(const std::vector< VECTOR2D > &aVertices, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor) override
Create an object representing a polygon.
void AddSpline(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing an arc.
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, const EDA_ANGLE &aAngle, const IMPORTED_STROKE &aStroke) override
Create an object representing an arc.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(LargeCoordinatesAutoOffset)
Test that large coordinates that would overflow when converted to internal units are automatically of...
BOOST_AUTO_TEST_SUITE_END()
VECTOR2I end
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
VECTOR2< double > VECTOR2D
Definition vector2d.h:694