KiCad PCB EDA Suite
Loading...
Searching...
No Matches
graphics_importer_buffer.h
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 (C) 2017 CERN
5 * Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Janito Vaqueiro Ferreira Filho <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef GRAPHICS_IMPORTER_BUFFER_H
28#define GRAPHICS_IMPORTER_BUFFER_H
29
30#include "graphics_importer.h"
31
32#include <math/matrix3x3.h>
33#include <math/box2.h>
34#include <list>
35
36
38{
39public:
40 virtual ~IMPORTED_SHAPE() {}
41 virtual void ImportTo( GRAPHICS_IMPORTER& aImporter ) const = 0;
42
43 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const = 0;
44
45 virtual void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) = 0;
46
47 void SetParentShapeIndex( int aIndex ) { m_parentShapeIndex = aIndex; }
49
50 virtual BOX2D GetBoundingBox() const = 0;
51
52protected:
54};
55
56
58{
59public:
60 IMPORTED_LINE( const VECTOR2D& aStart, const VECTOR2D& aEnd, const IMPORTED_STROKE& aStroke ) :
61 m_start( aStart ), m_end( aEnd ), m_stroke( aStroke )
62 {
63 }
64
65 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
66 {
67 aImporter.AddLine( m_start, m_end, m_stroke );
68 }
69
70 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
71 {
72 return std::make_unique<IMPORTED_LINE>( *this );
73 }
74
75 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
76 {
77 m_start = aTransform * m_start + aTranslation;
78 m_end = aTransform * m_end + aTranslation;
79 }
80
81 BOX2D GetBoundingBox() const override
82 {
83 BOX2D box;
84 box.Merge( m_start );
85 box.Merge( m_end );
86 return box;
87 }
88
89private:
93};
94
95
97{
98public:
99 IMPORTED_CIRCLE( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
100 bool aFilled, const COLOR4D& aFillColor ) :
101 m_center( aCenter ),
102 m_radius( aRadius ), m_stroke( aStroke ), m_filled( aFilled ), m_fillColor( aFillColor )
103 {
104 }
105
106 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
107 {
109 }
110
111 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
112 {
113 return std::make_unique<IMPORTED_CIRCLE>( *this );
114 }
115
116 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
117 {
118 VECTOR2D newCenter = ( aTransform * m_center ) + aTranslation;
119
120 VECTOR2D newRadius = VECTOR2D( m_radius, 0 );
121 newRadius = aTransform * newRadius;
122
123 m_center = newCenter;
124 m_radius = newRadius.EuclideanNorm();
125 }
126
127 BOX2D GetBoundingBox() const override
128 {
129 BOX2D box;
132 return box;
133 }
134
135private:
137 double m_radius;
141};
142
143
145{
146public:
147 IMPORTED_ARC( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
148 const IMPORTED_STROKE& aStroke ) :
149 m_center( aCenter ),
150 m_start( aStart ), m_angle( aAngle ), m_stroke( aStroke )
151 {
152 }
153
154 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
155 {
156 aImporter.AddArc( m_center, m_start, m_angle, m_stroke );
157 }
158
159 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
160 {
161 return std::make_unique<IMPORTED_ARC>( *this );
162 }
163
164 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
165 {
166 m_start = aTransform * m_start + aTranslation;
167 m_center = aTransform * m_center + aTranslation;
168 }
169
170 BOX2D GetBoundingBox() const override
171 {
172 BOX2D box;
173
174 box.Merge( m_start + m_stroke.GetWidth() );
175 box.Merge( m_start - m_stroke.GetWidth() );
176
177 for( double angle = 0; angle < m_angle.AsDegrees(); angle += 5 )
178 {
179 EDA_ANGLE ang = EDA_ANGLE( angle );
180 VECTOR2D start = m_center + m_start;
181 VECTOR2D pt = {start.x * ang.Cos() + start.y * ang.Sin(),
182 start.x * ang.Sin() - start.y * ang.Cos()};
183
184 box.Merge( pt );
185 }
186
187 return box;
188 }
189
190private:
195};
196
197
199{
200public:
201 IMPORTED_POLYGON( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
202 bool aFilled, const COLOR4D& aFillColor ) :
203 m_vertices( aVertices ),
204 m_stroke( aStroke ), m_filled( aFilled ), m_fillColor( aFillColor )
205 {
206 }
207
208 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
209 {
211 }
212
213 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
214 {
215 return std::make_unique<IMPORTED_POLYGON>( *this );
216 }
217
218 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
219 {
220 for( VECTOR2D& vert : m_vertices )
221 {
222 vert = aTransform * vert + aTranslation;
223 }
224 }
225
226 std::vector<VECTOR2D>& Vertices() { return m_vertices; }
227
228 bool IsFilled() const { return m_filled; }
229
230 const COLOR4D& GetFillColor() const { return m_fillColor; }
231
232 const IMPORTED_STROKE& GetStroke() const { return m_stroke; }
233
234 BOX2D GetBoundingBox() const override
235 {
236 BOX2D box;
237
238 for( const VECTOR2D& vert : m_vertices )
239 box.Merge( vert );
240
241 return box;
242 }
243
244private:
245 std::vector<VECTOR2D> m_vertices;
249};
250
251
253{
254public:
255 IMPORTED_TEXT( const VECTOR2D& aOrigin, const wxString& aText, double aHeight, double aWidth,
256 double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
257 GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor ) :
258 m_origin( aOrigin ),
259 m_text( aText ), m_height( aHeight ), m_width( aWidth ), m_thickness( aThickness ),
260 m_orientation( aOrientation ), m_hJustify( aHJustify ), m_vJustify( aVJustify ),
261 m_color( aColor )
262 {
263 }
264
265 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
266 {
269 }
270
271 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
272 {
273 return std::make_unique<IMPORTED_TEXT>( *this );
274 }
275
276 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
277 {
278 m_origin = aTransform * m_origin + aTranslation;
279
280 VECTOR2D textSize = aTransform * VECTOR2D( m_width, m_height );
281 m_width = textSize.x;
282 m_height = textSize.y;
283 }
284
285 BOX2D GetBoundingBox() const override
286 {
287 BOX2D box;
288 box.Merge( m_origin );
289 box.Merge( m_origin + VECTOR2D( m_width * m_text.length(), m_height ) );
290
291 return box;
292 }
293
294private:
296 const wxString m_text;
297 double m_height;
298 double m_width;
304};
305
306
308{
309public:
310 IMPORTED_SPLINE( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
311 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
312 const IMPORTED_STROKE& aStroke ) :
313 m_start( aStart ),
314 m_bezierControl1( aBezierControl1 ), m_bezierControl2( aBezierControl2 ), m_end( aEnd ),
315 m_stroke( aStroke )
316 {
317 }
318
319 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
320 {
322 }
323
324 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
325 {
326 return std::make_unique<IMPORTED_SPLINE>( *this );
327 }
328
329 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
330 {
331 m_start = aTransform * m_start + aTranslation;
332 m_bezierControl1 = aTransform * m_bezierControl1 + aTranslation;
333 m_bezierControl2 = aTransform * m_bezierControl2 + aTranslation;
334 m_end = aTransform * m_end + aTranslation;
335 }
336
337 BOX2D GetBoundingBox() const override
338 {
339 BOX2D box;
340 box.Merge( m_start );
341 box.Merge( m_end );
342 return box;
343 }
344
345private:
351};
352
353
355{
356public:
357 void AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
358 const IMPORTED_STROKE& aStroke ) override;
359
360 void AddCircle( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
361 bool aFilled, const COLOR4D& aFillColor = COLOR4D::UNSPECIFIED ) override;
362
363 void AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
364 const IMPORTED_STROKE& aStroke ) override;
365
366 void AddPolygon( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
367 bool aFilled, const COLOR4D& aFillColor = COLOR4D::UNSPECIFIED ) override;
368
369 void AddText( const VECTOR2D& aOrigin, const wxString& aText, double aHeight, double aWidth,
370 double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
371 GR_TEXT_V_ALIGN_T aVJustify,
372 const COLOR4D& aColor = COLOR4D::UNSPECIFIED ) override;
373
374 void AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
375 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
376 const IMPORTED_STROKE& aStroke ) override;
377
378 void ImportTo( GRAPHICS_IMPORTER& aImporter );
379 void AddShape( std::unique_ptr<IMPORTED_SHAPE>& aShape );
380
381 std::list<std::unique_ptr<IMPORTED_SHAPE>>& GetShapes() { return m_shapes; }
382
383 void ClearShapes() { m_shapes.clear(); }
384
386
387protected:
389 std::list<std::unique_ptr<IMPORTED_SHAPE>> m_shapes;
390};
391
392#endif /* GRAPHICS_IMPORTER_BUFFER */
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:658
double Sin() const
Definition: eda_angle.h:170
double AsDegrees() const
Definition: eda_angle.h:113
double Cos() const
Definition: eda_angle.h:189
std::list< std::unique_ptr< IMPORTED_SHAPE > > & GetShapes()
void AddCircle(const VECTOR2D &aCenter, double aRadius, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor=COLOR4D::UNSPECIFIED) override
Create an object representing a circle.
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 AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke) override
Create an object representing a line segment.
void ImportTo(GRAPHICS_IMPORTER &aImporter)
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, const EDA_ANGLE &aAngle, const IMPORTED_STROKE &aStroke) override
Create an object representing an arc.
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=COLOR4D::UNSPECIFIED) override
Create an object representing a text.
std::list< std::unique_ptr< IMPORTED_SHAPE > > m_shapes
< List of imported shapes
void AddShape(std::unique_ptr< IMPORTED_SHAPE > &aShape)
void AddPolygon(const std::vector< VECTOR2D > &aVertices, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor=COLOR4D::UNSPECIFIED) override
Create an object representing a polygon.
Interface that creates objects representing shapes for a given data model.
virtual void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, const EDA_ANGLE &aAngle, const IMPORTED_STROKE &aStroke)=0
Create an object representing an arc.
virtual void AddPolygon(const std::vector< VECTOR2D > &aVertices, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor)=0
Create an object representing a polygon.
virtual void AddLine(const VECTOR2D &aOrigin, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke)=0
Create an object representing a line segment.
virtual 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)=0
Create an object representing a text.
virtual void AddCircle(const VECTOR2D &aCenter, double aRadius, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor)=0
Create an object representing a circle.
virtual void AddSpline(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke)=0
Create an object representing an arc.
IMPORTED_STROKE m_stroke
BOX2D GetBoundingBox() const override
IMPORTED_ARC(const VECTOR2D &aCenter, const VECTOR2D &aStart, const EDA_ANGLE &aAngle, const IMPORTED_STROKE &aStroke)
void Transform(const MATRIX3x3D &aTransform, const VECTOR2D &aTranslation) override
virtual std::unique_ptr< IMPORTED_SHAPE > clone() const override
void ImportTo(GRAPHICS_IMPORTER &aImporter) const override
void Transform(const MATRIX3x3D &aTransform, const VECTOR2D &aTranslation) override
BOX2D GetBoundingBox() const override
IMPORTED_CIRCLE(const VECTOR2D &aCenter, double aRadius, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor)
void ImportTo(GRAPHICS_IMPORTER &aImporter) const override
virtual std::unique_ptr< IMPORTED_SHAPE > clone() const override
virtual std::unique_ptr< IMPORTED_SHAPE > clone() const override
IMPORTED_LINE(const VECTOR2D &aStart, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke)
void Transform(const MATRIX3x3D &aTransform, const VECTOR2D &aTranslation) override
IMPORTED_STROKE m_stroke
void ImportTo(GRAPHICS_IMPORTER &aImporter) const override
BOX2D GetBoundingBox() const override
void Transform(const MATRIX3x3D &aTransform, const VECTOR2D &aTranslation) override
std::vector< VECTOR2D > & Vertices()
const IMPORTED_STROKE & GetStroke() const
IMPORTED_POLYGON(const std::vector< VECTOR2D > &aVertices, const IMPORTED_STROKE &aStroke, bool aFilled, const COLOR4D &aFillColor)
void ImportTo(GRAPHICS_IMPORTER &aImporter) const override
BOX2D GetBoundingBox() const override
const COLOR4D & GetFillColor() const
std::vector< VECTOR2D > m_vertices
virtual std::unique_ptr< IMPORTED_SHAPE > clone() const override
int GetParentShapeIndex() const
virtual void ImportTo(GRAPHICS_IMPORTER &aImporter) const =0
void SetParentShapeIndex(int aIndex)
virtual void Transform(const MATRIX3x3D &aTransform, const VECTOR2D &aTranslation)=0
virtual BOX2D GetBoundingBox() const =0
virtual std::unique_ptr< IMPORTED_SHAPE > clone() const =0
void ImportTo(GRAPHICS_IMPORTER &aImporter) const override
BOX2D GetBoundingBox() const override
void Transform(const MATRIX3x3D &aTransform, const VECTOR2D &aTranslation) override
virtual std::unique_ptr< IMPORTED_SHAPE > clone() const override
IMPORTED_SPLINE(const VECTOR2D &aStart, const VECTOR2D &aBezierControl1, const VECTOR2D &aBezierControl2, const VECTOR2D &aEnd, const IMPORTED_STROKE &aStroke)
A clone of IMPORTED_STROKE, but with floating-point width.
double GetWidth() const
void Transform(const MATRIX3x3D &aTransform, const VECTOR2D &aTranslation) override
GR_TEXT_H_ALIGN_T m_hJustify
IMPORTED_TEXT(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)
GR_TEXT_V_ALIGN_T m_vJustify
BOX2D GetBoundingBox() const override
virtual std::unique_ptr< IMPORTED_SHAPE > clone() const override
void ImportTo(GRAPHICS_IMPORTER &aImporter) const override
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690