KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
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 ),
205 m_filled( aFilled ),
206 m_fillColor( aFillColor )
207 {
208 }
209
210 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
211 {
213 }
214
215 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
216 {
217 return std::make_unique<IMPORTED_POLYGON>( *this );
218 }
219
220 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
221 {
222 for( VECTOR2D& vert : m_vertices )
223 {
224 vert = aTransform * vert + aTranslation;
225 }
226 }
227
228 std::vector<VECTOR2D>& Vertices() { return m_vertices; }
229
230 bool IsFilled() const { return m_filled; }
231
232 const COLOR4D& GetFillColor() const { return m_fillColor; }
233
234 const IMPORTED_STROKE& GetStroke() const { return m_stroke; }
235
236 BOX2D GetBoundingBox() const override
237 {
238 BOX2D box;
239
240 for( const VECTOR2D& vert : m_vertices )
241 box.Merge( vert );
242
243 return box;
244 }
245
246private:
247 std::vector<VECTOR2D> m_vertices;
251};
252
253
255{
256public:
257 IMPORTED_TEXT( const VECTOR2D& aOrigin, const wxString& aText, double aHeight, double aWidth,
258 double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
259 GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor ) :
260 m_origin( aOrigin ),
261 m_text( aText ), m_height( aHeight ), m_width( aWidth ), m_thickness( aThickness ),
262 m_orientation( aOrientation ), m_hJustify( aHJustify ), m_vJustify( aVJustify ),
263 m_color( aColor )
264 {
265 }
266
267 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
268 {
271 }
272
273 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
274 {
275 return std::make_unique<IMPORTED_TEXT>( *this );
276 }
277
278 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
279 {
280 m_origin = aTransform * m_origin + aTranslation;
281
282 VECTOR2D textSize = aTransform * VECTOR2D( m_width, m_height );
283 m_width = textSize.x;
284 m_height = textSize.y;
285 }
286
287 BOX2D GetBoundingBox() const override
288 {
289 BOX2D box;
290 box.Merge( m_origin );
291 box.Merge( m_origin + VECTOR2D( m_width * m_text.length(), m_height ) );
292
293 return box;
294 }
295
296private:
298 const wxString m_text;
299 double m_height;
300 double m_width;
306};
307
308
310{
311public:
312 IMPORTED_SPLINE( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
313 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
314 const IMPORTED_STROKE& aStroke ) :
315 m_start( aStart ),
316 m_bezierControl1( aBezierControl1 ), m_bezierControl2( aBezierControl2 ), m_end( aEnd ),
317 m_stroke( aStroke )
318 {
319 }
320
321 void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
322 {
324 }
325
326 virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
327 {
328 return std::make_unique<IMPORTED_SPLINE>( *this );
329 }
330
331 void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
332 {
333 m_start = aTransform * m_start + aTranslation;
334 m_bezierControl1 = aTransform * m_bezierControl1 + aTranslation;
335 m_bezierControl2 = aTransform * m_bezierControl2 + aTranslation;
336 m_end = aTransform * m_end + aTranslation;
337 }
338
339 BOX2D GetBoundingBox() const override
340 {
341 BOX2D box;
342 box.Merge( m_start );
343 box.Merge( m_end );
344 return box;
345 }
346
347private:
353};
354
355
357{
358public:
359 void AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
360 const IMPORTED_STROKE& aStroke ) override;
361
362 void AddCircle( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
363 bool aFilled, const COLOR4D& aFillColor = COLOR4D::UNSPECIFIED ) override;
364
365 void AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
366 const IMPORTED_STROKE& aStroke ) override;
367
368 void AddPolygon( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
369 bool aFilled, const COLOR4D& aFillColor = COLOR4D::UNSPECIFIED ) override;
370
371 void AddText( const VECTOR2D& aOrigin, const wxString& aText, double aHeight, double aWidth,
372 double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
373 GR_TEXT_V_ALIGN_T aVJustify,
374 const COLOR4D& aColor = COLOR4D::UNSPECIFIED ) override;
375
376 void AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
377 const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
378 const IMPORTED_STROKE& aStroke ) override;
379
380 void ImportTo( GRAPHICS_IMPORTER& aImporter );
381 void AddShape( std::unique_ptr<IMPORTED_SHAPE>& aShape );
382
383 std::list<std::unique_ptr<IMPORTED_SHAPE>>& GetShapes() { return m_shapes; }
384
385 void ClearShapes() { m_shapes.clear(); }
386
388
389protected:
391 std::list<std::unique_ptr<IMPORTED_SHAPE>> m_shapes;
392};
393
394#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
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