KiCad PCB EDA Suite
base_struct.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 (C) 2015 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #include <deque>
30 
31 #include <fctsys.h>
32 #include <trigo.h>
33 #include <common.h>
34 #include <macros.h>
35 #include <base_screen.h>
36 #include <bitmaps.h>
37 #include <trace_helpers.h>
38 #include <eda_rect.h>
39 
40 #include <algorithm>
41 
42 
43 static const unsigned char dummy_png[] = {
44  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
45  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0xf3, 0xff,
46  0x61, 0x00, 0x00, 0x00, 0x5f, 0x49, 0x44, 0x41, 0x54, 0x38, 0xcb, 0x63, 0xf8, 0xff, 0xff, 0x3f,
47  0x03, 0x25, 0x98, 0x61, 0x68, 0x1a, 0x00, 0x04, 0x46, 0x40, 0xfc, 0x02, 0x88, 0x45, 0x41, 0x1c,
48  0x76, 0x20, 0xfe, 0x01, 0xc4, 0xbe, 0x24, 0x18, 0x60, 0x01, 0xc4, 0x20, 0x86, 0x04, 0x88, 0xc3,
49  0x01, 0xe5, 0x04, 0x0c, 0xb8, 0x01, 0x37, 0x81, 0xf8, 0x04, 0x91, 0xf8, 0x0a, 0x54, 0x8f, 0x06,
50  0xb2, 0x01, 0x9b, 0x81, 0x78, 0x02, 0x91, 0x78, 0x05, 0x54, 0x8f, 0xca, 0xe0, 0x08, 0x03, 0x36,
51  0xa8, 0xbf, 0xec, 0xc8, 0x32, 0x80, 0xcc, 0x84, 0x04, 0x0a, 0xbc, 0x1d, 0x40, 0x2c, 0xc8, 0x30,
52  0xf4, 0x33, 0x13, 0x00, 0x6b, 0x1a, 0x46, 0x7b, 0x68, 0xe7, 0x0f, 0x0b, 0x00, 0x00, 0x00, 0x00,
53  0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
54 };
55 
56 static const BITMAP_OPAQUE dummy_xpm[1] = {{ dummy_png, sizeof( dummy_png ), "dummy_xpm" }};
57 
58 
59 EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType ) :
60  m_StructType( idType ),
61  m_Status( 0 ),
62  m_Parent( parent ),
63  m_forceVisible( false ),
64  m_Flags( 0 )
65 { }
66 
67 
69  m_StructType( idType ),
70  m_Status( 0 ),
71  m_Parent( nullptr ),
72  m_forceVisible( false ),
73  m_Flags( 0 )
74 { }
75 
76 
77 EDA_ITEM::EDA_ITEM( const EDA_ITEM& base ) :
78  m_Uuid( base.m_Uuid ),
79  m_StructType( base.m_StructType ),
80  m_Status( base.m_Status ),
81  m_Parent( base.m_Parent ),
82  m_forceVisible( base.m_forceVisible ),
83  m_Flags( base.m_Flags )
84 { }
85 
86 
88 {
90 
91  // If this a child object, then the parent modification state also needs to be set.
92  if( m_Parent )
94 }
95 
96 
98 {
99  // return a zero-sized box per default. derived classes should override
100  // this
101  return EDA_RECT( wxPoint( 0, 0 ), wxSize( 0, 0 ) );
102 }
103 
104 
106 {
107  wxCHECK_MSG( false, NULL, wxT( "Clone not implemented in derived class " ) + GetClass() +
108  wxT( ". Bad programmer!" ) );
109 }
110 
111 
112 // see base_struct.h
113 // many classes inherit this method, be careful:
114 //TODO (snh): Fix this to use std::set instead of C-style vector
115 SEARCH_RESULT EDA_ITEM::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
116 {
117 #if 0 && defined(DEBUG)
118  std::cout << GetClass().mb_str() << ' ';
119 #endif
120 
121  if( IsType( scanTypes ) )
122  {
123  if( SEARCH_RESULT::QUIT == inspector( this, testData ) )
124  return SEARCH_RESULT::QUIT;
125  }
126 
128 }
129 
130 
131 wxString EDA_ITEM::GetSelectMenuText( EDA_UNITS aUnits ) const
132 {
133  wxFAIL_MSG( wxT( "GetSelectMenuText() was not overridden for schematic item type " ) +
134  GetClass() );
135 
136  return wxString( wxT( "Undefined menu text for " ) + GetClass() );
137 }
138 
139 
140 bool EDA_ITEM::Matches( const wxString& aText, wxFindReplaceData& aSearchData )
141 {
142  wxString text = aText;
143  wxString searchText = aSearchData.GetFindString();
144 
145  // Don't match if searching for replaceable item and the item doesn't support text replace.
146  if( (aSearchData.GetFlags() & FR_SEARCH_REPLACE) && !IsReplaceable() )
147  return false;
148 
149  if( aSearchData.GetFlags() & wxFR_WHOLEWORD )
150  return aText.IsSameAs( searchText, aSearchData.GetFlags() & wxFR_MATCHCASE );
151 
152  if( aSearchData.GetFlags() & FR_MATCH_WILDCARD )
153  {
154  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
155  return text.Matches( searchText );
156 
157  return text.MakeUpper().Matches( searchText.MakeUpper() );
158  }
159 
160  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
161  return aText.Find( searchText ) != wxNOT_FOUND;
162 
163  return text.MakeUpper().Find( searchText.MakeUpper() ) != wxNOT_FOUND;
164 }
165 
166 
167 bool EDA_ITEM::Replace( wxFindReplaceData& aSearchData, wxString& aText )
168 {
169  wxString searchString = (aSearchData.GetFlags() & wxFR_MATCHCASE) ? aText : aText.Upper();
170 
171  int result = searchString.Find( (aSearchData.GetFlags() & wxFR_MATCHCASE) ?
172  aSearchData.GetFindString() :
173  aSearchData.GetFindString().Upper() );
174 
175  if( result == wxNOT_FOUND )
176  return false;
177 
178  wxString prefix = aText.Left( result );
179  wxString suffix;
180 
181  if( aSearchData.GetFindString().length() + result < aText.length() )
182  suffix = aText.Right( aText.length() - ( aSearchData.GetFindString().length() + result ) );
183 
184  wxLogTrace( traceFindReplace, wxT( "Replacing '%s', prefix '%s', replace '%s', suffix '%s'." ),
185  GetChars( aText ), GetChars( prefix ), GetChars( aSearchData.GetReplaceString() ),
186  GetChars( suffix ) );
187 
188  aText = prefix + aSearchData.GetReplaceString() + suffix;
189 
190  return true;
191 }
192 
193 
194 bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
195 {
196  wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
197  GetChars( GetClass() ) ) );
198 
199  return false;
200 }
201 
203 {
204  // do not call initVars()
205 
206  m_StructType = aItem.m_StructType;
207  m_Flags = aItem.m_Flags;
208  m_Status = aItem.m_Status;
209  m_Parent = aItem.m_Parent;
211 
212  return *this;
213 }
214 
216 {
217  // Basic fallback
218  return BOX2I( VECTOR2I( GetBoundingBox().GetOrigin() ),
219  VECTOR2I( GetBoundingBox().GetSize() ) );
220 }
221 
222 
223 void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
224 {
225  // Basic fallback
226  aCount = 1;
227  aLayers[0] = 0;
228 }
229 
231 {
232  return dummy_xpm;
233 }
234 
235 #if defined(DEBUG)
236 
237 void EDA_ITEM::ShowDummy( std::ostream& os ) const
238 {
239  // XML output:
240  wxString s = GetClass();
241 
242  os << '<' << s.Lower().mb_str() << ">"
243  << " Need ::Show() override for this class "
244  << "</" << s.Lower().mb_str() << ">\n";
245 }
246 
247 
248 std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
249 {
250  for( int i = 0; i<nestLevel; ++i )
251  os << " ";
252 
253  // number of spaces here controls indent per nest level
254 
255  return os;
256 }
257 
258 #endif
259 
260 
261 /******************/
262 /* Class EDA_RECT */
263 /******************/
264 
266 {
267  if( m_Size.y < 0 )
268  {
269  m_Size.y = -m_Size.y;
270  m_Pos.y -= m_Size.y;
271  }
272 
273  if( m_Size.x < 0 )
274  {
275  m_Size.x = -m_Size.x;
276  m_Pos.x -= m_Size.x;
277  }
278 }
279 
280 
281 void EDA_RECT::Move( const wxPoint& aMoveVector )
282 {
283  m_Pos += aMoveVector;
284 }
285 
286 
287 bool EDA_RECT::Contains( const wxPoint& aPoint ) const
288 {
289  wxPoint rel_pos = aPoint - m_Pos;
290  wxSize size = m_Size;
291 
292  if( size.x < 0 )
293  {
294  size.x = -size.x;
295  rel_pos.x += size.x;
296  }
297 
298  if( size.y < 0 )
299  {
300  size.y = -size.y;
301  rel_pos.y += size.y;
302  }
303 
304  return (rel_pos.x >= 0) && (rel_pos.y >= 0) && ( rel_pos.y <= size.y) && ( rel_pos.x <= size.x);
305 }
306 
307 
308 bool EDA_RECT::Contains( const EDA_RECT& aRect ) const
309 {
310  return Contains( aRect.GetOrigin() ) && Contains( aRect.GetEnd() );
311 }
312 
313 
314 bool EDA_RECT::Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const
315 {
316  wxPoint point2, point4;
317 
318  if( Contains( aPoint1 ) || Contains( aPoint2 ) )
319  return true;
320 
321  point2.x = GetEnd().x;
322  point2.y = GetOrigin().y;
323  point4.x = GetOrigin().x;
324  point4.y = GetEnd().y;
325 
326  //Only need to test 3 sides since a straight line cant enter and exit on same side
327  if( SegmentIntersectsSegment( aPoint1, aPoint2, GetOrigin() , point2 ) )
328  return true;
329 
330  if( SegmentIntersectsSegment( aPoint1, aPoint2, point2 , GetEnd() ) )
331  return true;
332 
333  if( SegmentIntersectsSegment( aPoint1, aPoint2, GetEnd() , point4 ) )
334  return true;
335 
336  return false;
337 }
338 
339 
340 bool EDA_RECT::Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2,
341  wxPoint* aIntersection1, wxPoint* aIntersection2 ) const
342 {
343  wxPoint point2, point4;
344 
345  point2.x = GetEnd().x;
346  point2.y = GetOrigin().y;
347  point4.x = GetOrigin().x;
348  point4.y = GetEnd().y;
349 
350  bool intersects = false;
351 
352  wxPoint* aPointToFill = aIntersection1;
353 
354  if( SegmentIntersectsSegment( aPoint1, aPoint2, GetOrigin(), point2, aPointToFill ) )
355  intersects = true;
356 
357  if( intersects )
358  aPointToFill = aIntersection2;
359 
360  if( SegmentIntersectsSegment( aPoint1, aPoint2, point2, GetEnd(), aPointToFill ) )
361  intersects = true;
362 
363  if( intersects )
364  aPointToFill = aIntersection2;
365 
366  if( SegmentIntersectsSegment( aPoint1, aPoint2, GetEnd(), point4, aPointToFill ) )
367  intersects = true;
368 
369  if( intersects )
370  aPointToFill = aIntersection2;
371 
372  if( SegmentIntersectsSegment( aPoint1, aPoint2, point4, GetOrigin(), aPointToFill ) )
373  intersects = true;
374 
375  return intersects;
376 }
377 
378 
379 bool EDA_RECT::Intersects( const EDA_RECT& aRect ) const
380 {
381  if( !m_init )
382  return false;
383 
384  // this logic taken from wxWidgets' geometry.cpp file:
385  bool rc;
386  EDA_RECT me(*this);
387  EDA_RECT rect(aRect);
388  me.Normalize(); // ensure size is >= 0
389  rect.Normalize(); // ensure size is >= 0
390 
391  // calculate the left common area coordinate:
392  int left = std::max( me.m_Pos.x, rect.m_Pos.x );
393  // calculate the right common area coordinate:
394  int right = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
395  // calculate the upper common area coordinate:
396  int top = std::max( me.m_Pos.y, aRect.m_Pos.y );
397  // calculate the lower common area coordinate:
398  int bottom = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
399 
400  // if a common area exists, it must have a positive (null accepted) size
401  if( left <= right && top <= bottom )
402  rc = true;
403  else
404  rc = false;
405 
406  return rc;
407 }
408 
409 
410 bool EDA_RECT::Intersects( const EDA_RECT& aRect, double aRot ) const
411 {
412  if( !m_init )
413  return false;
414 
415  /* Most rectangles will be axis aligned.
416  * It is quicker to check for this case and pass the rect
417  * to the simpler intersection test
418  */
419 
420  // Prevent floating point comparison errors
421  static const double ROT_EPS = 0.000000001;
422 
423  static const double ROT_PARALLEL[] = { -3600, -1800, 0, 1800, 3600 };
424  static const double ROT_PERPENDICULAR[] = { -2700, -900, 0, 900, 2700 };
425 
426  NORMALIZE_ANGLE_POS<double>( aRot );
427 
428  // Test for non-rotated rectangle
429  for( int ii = 0; ii < 5; ii++ )
430  {
431  if( std::fabs( aRot - ROT_PARALLEL[ii] ) < ROT_EPS )
432  {
433  return Intersects( aRect );
434  }
435  }
436 
437  // Test for rectangle rotated by multiple of 90 degrees
438  for( int jj = 0; jj < 4; jj++ )
439  {
440  if( std::fabs( aRot - ROT_PERPENDICULAR[jj] ) < ROT_EPS )
441  {
442  EDA_RECT rotRect;
443 
444  // Rotate the supplied rect by 90 degrees
445  rotRect.SetOrigin( aRect.Centre() );
446  rotRect.Inflate( aRect.GetHeight(), aRect.GetWidth() );
447  return Intersects( rotRect );
448  }
449  }
450 
451  /* There is some non-orthogonal rotation.
452  * There are three cases to test:
453  * A) One point of this rect is inside the rotated rect
454  * B) One point of the rotated rect is inside this rect
455  * C) One of the sides of the rotated rect intersect this
456  */
457 
458  wxPoint corners[4];
459 
460  /* Test A : Any corners exist in rotated rect? */
461 
462  corners[0] = m_Pos;
463  corners[1] = m_Pos + wxPoint( m_Size.x, 0 );
464  corners[2] = m_Pos + wxPoint( m_Size.x, m_Size.y );
465  corners[3] = m_Pos + wxPoint( 0, m_Size.y );
466 
467  wxPoint rCentre = aRect.Centre();
468 
469  for( int i = 0; i < 4; i++ )
470  {
471  wxPoint delta = corners[i] - rCentre;
472  RotatePoint( &delta, -aRot );
473  delta += rCentre;
474 
475  if( aRect.Contains( delta ) )
476  {
477  return true;
478  }
479  }
480 
481  /* Test B : Any corners of rotated rect exist in this one? */
482  int w = aRect.GetWidth() / 2;
483  int h = aRect.GetHeight() / 2;
484 
485  // Construct corners around center of shape
486  corners[0] = wxPoint( -w, -h );
487  corners[1] = wxPoint( w, -h );
488  corners[2] = wxPoint( w, h );
489  corners[3] = wxPoint( -w, h );
490 
491  // Rotate and test each corner
492  for( int j=0; j<4; j++ )
493  {
494  RotatePoint( &corners[j], aRot );
495  corners[j] += rCentre;
496 
497  if( Contains( corners[j] ) )
498  {
499  return true;
500  }
501  }
502 
503  /* Test C : Any sides of rotated rect intersect this */
504 
505  if( Intersects( corners[0], corners[1] ) ||
506  Intersects( corners[1], corners[2] ) ||
507  Intersects( corners[2], corners[3] ) ||
508  Intersects( corners[3], corners[0] ) )
509  {
510  return true;
511  }
512 
513 
514  return false;
515 }
516 
517 
518 const wxPoint EDA_RECT::ClosestPointTo( const wxPoint& aPoint ) const
519 {
520  EDA_RECT me( *this );
521 
522  me.Normalize(); // ensure size is >= 0
523 
524  // Determine closest point to the circle centre within this rect
525  int nx = std::max( me.GetLeft(), std::min( aPoint.x, me.GetRight() ) );
526  int ny = std::max( me.GetTop(), std::min( aPoint.y, me.GetBottom() ) );
527 
528  return wxPoint( nx, ny );
529 }
530 
531 
532 const wxPoint EDA_RECT::FarthestPointTo( const wxPoint& aPoint ) const
533 {
534  EDA_RECT me( *this );
535 
536  me.Normalize(); // ensure size is >= 0
537 
538  int fx = std::max( std::abs( aPoint.x - me.GetLeft() ), std::abs( aPoint.x - me.GetRight() ) );
539  int fy = std::max( std::abs( aPoint.y - me.GetTop() ), std::abs( aPoint.y - me.GetBottom() ) );
540 
541  return wxPoint( fx, fy );
542 }
543 
544 
545 bool EDA_RECT::IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const
546 {
547  if( !m_init )
548  return false;
549 
550  wxPoint closest = ClosestPointTo( aCenter );
551 
552  double dx = aCenter.x - closest.x;
553  double dy = aCenter.y - closest.y;
554 
555  double r = (double) aRadius;
556 
557  return ( dx * dx + dy * dy ) <= ( r * r );
558 }
559 
560 
561 bool EDA_RECT::IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const
562 {
563  if( !m_init )
564  return false;
565 
566  EDA_RECT me( *this );
567  me.Normalize(); // ensure size is >= 0
568 
569  // Test if the circle intersects at all
570  if( !IntersectsCircle( aCenter, aRadius + aWidth / 2 ) )
571  {
572  return false;
573  }
574 
575  wxPoint farpt = FarthestPointTo( aCenter );
576  // Farthest point must be further than the inside of the line
577  double fx = (double) farpt.x;
578  double fy = (double) farpt.y;
579 
580  double r = (double) aRadius - (double) aWidth / 2;
581 
582  return ( fx * fx + fy * fy ) > ( r * r );
583 }
584 
585 
587 {
588  Inflate( aDelta, aDelta );
589  return *this;
590 }
591 
592 
593 EDA_RECT& EDA_RECT::Inflate( wxCoord dx, wxCoord dy )
594 {
595  if( m_Size.x >= 0 )
596  {
597  if( m_Size.x < -2 * dx )
598  {
599  // Don't allow deflate to eat more width than we have,
600  m_Pos.x += m_Size.x / 2;
601  m_Size.x = 0;
602  }
603  else
604  {
605  // The inflate is valid.
606  m_Pos.x -= dx;
607  m_Size.x += 2 * dx;
608  }
609  }
610  else // size.x < 0:
611  {
612  if( m_Size.x > -2 * dx )
613  {
614  // Don't allow deflate to eat more width than we have,
615  m_Pos.x -= m_Size.x / 2;
616  m_Size.x = 0;
617  }
618  else
619  {
620  // The inflate is valid.
621  m_Pos.x += dx;
622  m_Size.x -= 2 * dx; // m_Size.x <0: inflate when dx > 0
623  }
624  }
625 
626  if( m_Size.y >= 0 )
627  {
628  if( m_Size.y < -2 * dy )
629  {
630  // Don't allow deflate to eat more height than we have,
631  m_Pos.y += m_Size.y / 2;
632  m_Size.y = 0;
633  }
634  else
635  {
636  // The inflate is valid.
637  m_Pos.y -= dy;
638  m_Size.y += 2 * dy;
639  }
640  }
641  else // size.y < 0:
642  {
643  if( m_Size.y > 2 * dy )
644  {
645  // Don't allow deflate to eat more height than we have,
646  m_Pos.y -= m_Size.y / 2;
647  m_Size.y = 0;
648  }
649  else
650  {
651  // The inflate is valid.
652  m_Pos.y += dy;
653  m_Size.y -= 2 * dy; // m_Size.y <0: inflate when dy > 0
654  }
655  }
656 
657  return *this;
658 }
659 
660 
661 void EDA_RECT::Merge( const EDA_RECT& aRect )
662 {
663  if( !m_init )
664  {
665  if( aRect.IsValid() )
666  {
667  m_Pos = aRect.GetPosition();
668  m_Size = aRect.GetSize();
669  m_init = true;
670  }
671  return;
672  }
673 
674  Normalize(); // ensure width and height >= 0
675  EDA_RECT rect = aRect;
676  rect.Normalize(); // ensure width and height >= 0
677  wxPoint end = GetEnd();
678  wxPoint rect_end = rect.GetEnd();
679 
680  // Change origin and size in order to contain the given rect
681  m_Pos.x = std::min( m_Pos.x, rect.m_Pos.x );
682  m_Pos.y = std::min( m_Pos.y, rect.m_Pos.y );
683  end.x = std::max( end.x, rect_end.x );
684  end.y = std::max( end.y, rect_end.y );
685  SetEnd( end );
686 }
687 
688 
689 void EDA_RECT::Merge( const wxPoint& aPoint )
690 {
691  if( !m_init )
692  {
693  m_Pos = aPoint;
694  m_Size = wxSize( 0, 0 );
695  m_init = true;
696  return;
697  }
698 
699  Normalize(); // ensure width and height >= 0
700 
701  wxPoint end = GetEnd();
702  // Change origin and size in order to contain the given rect
703  m_Pos.x = std::min( m_Pos.x, aPoint.x );
704  m_Pos.y = std::min( m_Pos.y, aPoint.y );
705  end.x = std::max( end.x, aPoint.x );
706  end.y = std::max( end.y, aPoint.y );
707  SetEnd( end );
708 }
709 
710 
711 double EDA_RECT::GetArea() const
712 {
713  return (double) GetWidth() * (double) GetHeight();
714 }
715 
716 
717 EDA_RECT EDA_RECT::Common( const EDA_RECT& aRect ) const
718 {
719  EDA_RECT r;
720 
721  if( Intersects( aRect ) )
722  {
723  wxPoint originA( std::min( GetOrigin().x, GetEnd().x ),
724  std::min( GetOrigin().y, GetEnd().y ) );
725  wxPoint originB( std::min( aRect.GetOrigin().x, aRect.GetEnd().x ),
726  std::min( aRect.GetOrigin().y, aRect.GetEnd().y ) );
727  wxPoint endA( std::max( GetOrigin().x, GetEnd().x ),
728  std::max( GetOrigin().y, GetEnd().y ) );
729  wxPoint endB( std::max( aRect.GetOrigin().x, aRect.GetEnd().x ),
730  std::max( aRect.GetOrigin().y, aRect.GetEnd().y ) );
731 
732  r.SetOrigin( wxPoint( std::max( originA.x, originB.x ), std::max( originA.y, originB.y ) ) );
733  r.SetEnd ( wxPoint( std::min( endA.x, endB.x ), std::min( endA.y, endB.y ) ) );
734  }
735 
736  return r;
737 }
738 
739 
740 const EDA_RECT EDA_RECT::GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle ) const
741 {
742  wxPoint corners[4];
743 
744  // Build the corners list
745  corners[0] = GetOrigin();
746  corners[2] = GetEnd();
747  corners[1].x = corners[0].x;
748  corners[1].y = corners[2].y;
749  corners[3].x = corners[2].x;
750  corners[3].y = corners[0].y;
751 
752  // Rotate all corners, to find the bounding box
753  for( int ii = 0; ii < 4; ii ++ )
754  RotatePoint( &corners[ii], aRotCenter, aAngle );
755 
756  // Find the corners bounding box
757  wxPoint start = corners[0];
758  wxPoint end = corners[0];
759 
760  for( int ii = 1; ii < 4; ii ++ )
761  {
762  start.x = std::min( start.x, corners[ii].x);
763  start.y = std::min( start.y, corners[ii].y);
764  end.x = std::max( end.x, corners[ii].x);
765  end.y = std::max( end.y, corners[ii].y);
766  }
767 
768  EDA_RECT bbox;
769  bbox.SetOrigin( start );
770  bbox.SetEnd( end );
771 
772  return bbox;
773 }
774 
775 
776 static struct EDA_ITEM_DESC
777 {
779  {
781  .Undefined( TYPE_NOT_INIT )
782  .Map( NOT_USED, wxT( "<not used>" ) )
783  .Map( SCREEN_T, _( "Screen" ) )
784 
785  .Map( PCB_MODULE_T, _( "Footprint" ) )
786  .Map( PCB_PAD_T, _( "Pad" ) )
787  .Map( PCB_LINE_T, _( "Line" ) )
788  .Map( PCB_TEXT_T, _( "Board Text" ) )
789  .Map( PCB_MODULE_TEXT_T, _( "Footprint Text" ) )
790  .Map( PCB_MODULE_EDGE_T, _( "Footprint Graphics" ) )
791  .Map( PCB_TRACE_T, _( "Track" ) )
792  .Map( PCB_VIA_T, _( "Via" ) )
793  .Map( PCB_MARKER_T, _( "Board Marker" ) )
794  .Map( PCB_DIM_ALIGNED_T, _( "Aligned Dimension" ) )
795  .Map( PCB_DIM_ORTHOGONAL_T, _( "Orthogonal Dimension" ) )
796  .Map( PCB_DIM_CENTER_T, _( "Center Dimension" ) )
797  .Map( PCB_DIM_LEADER_T, _( "Leader" ) )
798  .Map( PCB_TARGET_T, _( "Target" ) )
799  .Map( PCB_ZONE_AREA_T, _( "Zone" ) )
800  .Map( PCB_ITEM_LIST_T, _( "Item List" ) )
801  .Map( PCB_NETINFO_T, _( "Net Info" ) )
802  .Map( PCB_GROUP_T, _( "Group" ) )
803 
804  .Map( SCH_MARKER_T, _( "Schematic Marker" ) )
805  .Map( SCH_JUNCTION_T, _( "Junction" ) )
806  .Map( SCH_NO_CONNECT_T, _( "No-Connect Flag" ) )
807  .Map( SCH_BUS_WIRE_ENTRY_T, _( "Wire Entry" ) )
808  .Map( SCH_BUS_BUS_ENTRY_T, _( "Bus Entry" ) )
809  .Map( SCH_LINE_T, _( "Graphic Line" ) )
810  .Map( SCH_BITMAP_T, _( "Bitmap" ) )
811  .Map( SCH_TEXT_T, _( "Schematic Text" ) )
812  .Map( SCH_LABEL_T, _( "Net Label" ) )
813  .Map( SCH_GLOBAL_LABEL_T, _( "Global Label" ) )
814  .Map( SCH_HIER_LABEL_T, _( "Hierarchical Label" ) )
815  .Map( SCH_FIELD_T, _( "Schematic Field" ) )
816  .Map( SCH_COMPONENT_T, _( "Component" ) )
817  .Map( SCH_SHEET_PIN_T, _( "Sheet Pin" ) )
818  .Map( SCH_SHEET_T, _( "Sheet" ) )
819 
820  .Map( SCH_FIELD_LOCATE_REFERENCE_T, _( "Field Locate Reference" ) )
821  .Map( SCH_FIELD_LOCATE_VALUE_T, _( "Field Locate Value" ) )
822  .Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _( "Field Locate Footprint" ) )
823 
824  .Map( SCH_SCREEN_T, _( "SCH Screen" ) )
825 
826  .Map( LIB_PART_T, _( "Symbol" ) )
827  .Map( LIB_ALIAS_T, _( "Alias" ) )
828  .Map( LIB_ARC_T, _( "Arc" ) )
829  .Map( LIB_CIRCLE_T, _( "Circle" ) )
830  .Map( LIB_TEXT_T, _( "Symbol Text" ) )
831  .Map( LIB_RECTANGLE_T, _( "Rectangle" ) )
832  .Map( LIB_POLYLINE_T, _( "Polyline" ) )
833  .Map( LIB_BEZIER_T, _( "Bezier" ) )
834  .Map( LIB_PIN_T, _( "Pin" ) )
835  .Map( LIB_FIELD_T, _( "Symbol Field" ) )
836 
837  .Map( GERBER_LAYOUT_T, _( "Gerber Layout" ) )
838  .Map( GERBER_DRAW_ITEM_T, _( "Draw Item" ) )
839  .Map( GERBER_IMAGE_T, _( "Image" ) );
840 
843  propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( "Type",
845  }
847 
EDA_UNITS
Definition: common.h:198
void SetModified()
Definition: base_struct.cpp:87
virtual SEARCH_RESULT Visit(INSPECTOR inspector, void *testData, const KICAD_T scanTypes[])
Function Visit may be re-implemented for each derived class in order to handle all the types given by...
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:61
EDA_ITEM * m_Parent
Linked list: Link (parent struct)
Definition: base_struct.h:174
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
bool operator<(const EDA_ITEM &aItem) const
Test if another item is less than this object.
class ALIGNED_DIMENSION, a linear dimension (graphic item)
Definition: typeinfo.h:101
class LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
const wxChar *const traceFindReplace
Flag to enable find and replace debug tracing.
void Merge(const EDA_RECT &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect.
PNG memory record (file in memory).
Definition: bitmap_def.h:29
static struct EDA_ITEM_DESC _EDA_ITEM_DESC
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle) const
Function GetBoundingBoxRotated.
KICAD_T m_StructType
Run time identification, keep private so it can never be changed after a constructor sets it.
Definition: base_struct.h:170
static bool Replace(wxFindReplaceData &aSearchData, wxString &aText)
Helper function used in search and replace dialog Function Replace performs a text replace on aText u...
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:109
int GetTop() const
Definition: eda_rect.h:123
class CENTER_DIMENSION, a center point marking (graphic item)
Definition: typeinfo.h:103
int GetLeft() const
Definition: eda_rect.h:122
the 3d code uses this value
Definition: typeinfo.h:80
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:106
int GetWidth() const
Definition: eda_rect.h:119
static ENUM_MAP< T > & Instance()
Definition: property.h:517
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Function IntersectsCircle tests for a common area between a circle and this rectangle.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
bool IntersectsCircleEdge(const wxPoint &aCenter, const int aRadius, const int aWidth) const
IntersectsCircleEdge Tests for intersection between this rect and the edge (radius) of a circle.
EDA_RECT Common(const EDA_RECT &aRect) const
Function Common returns the area that is common with another rectangle.
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
#define NO_SETTER(owner, type)
Macro to define read-only fields (no setter method available)
Definition: property.h:612
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names
Definition: property_mgr.h:244
bool Contains(const wxPoint &aPoint) const
Function Contains.
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
not really an item, used to identify a screen
Definition: typeinfo.h:86
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
This file contains miscellaneous commonly used macros and functions.
int GetBottom() const
Definition: eda_rect.h:124
bool SegmentIntersectsSegment(const wxPoint &a_p1_l1, const wxPoint &a_p2_l1, const wxPoint &a_p1_l2, const wxPoint &a_p2_l2, wxPoint *aIntersectionPoint=nullptr)
Test if two lines intersect.
Definition: trigo.cpp:61
const INSPECTOR_FUNC & INSPECTOR
Definition: base_struct.h:105
const wxPoint GetEnd() const
Definition: eda_rect.h:116
class MODULE, a footprint
Definition: typeinfo.h:89
#define IS_CHANGED
std::function passed to nested users by ref, avoids copying std::function
Definition: base_struct.h:113
wxSize m_Size
Definition: eda_rect.h:48
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:232
#define NULL
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers() Returns the all the layers within the VIEW the object is painted on.
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
void SetEnd(int x, int y)
Definition: eda_rect.h:192
STATUS_FLAGS m_Status
Definition: base_struct.h:173
const wxPoint GetPosition() const
Definition: eda_rect.h:115
const wxPoint FarthestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is farthest from the provided point.
int GetRight() const
Definition: eda_rect.h:121
class BOARD_ITEM_LIST, a list of board items
Definition: typeinfo.h:107
wxLogTrace helper definitions.
virtual bool IsType(const KICAD_T aScanTypes[]) const
Function IsType Checks whether the item is one of the listed types.
Definition: base_struct.h:262
double GetArea() const
Function GetArea returns the area of the rectangle.
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Operator assignment is used to assign the members of aItem to another object.
int GetHeight() const
Definition: eda_rect.h:120
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType)
Definition: base_struct.cpp:59
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:105
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void Normalize()
Function Normalize ensures that the height ant width are positive.
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:99
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:153
virtual wxString GetClass() const =0
Function GetClass returns the class name.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
#define _(s)
Definition: 3d_actions.cpp:33
bool m_init
Definition: eda_rect.h:49
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
STATUS_FLAGS m_Flags
Definition: base_struct.h:176
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:108
void AddProperty(PROPERTY_BASE *aProperty)
Registers a property.
virtual bool IsReplaceable() const
Function IsReplaceable.
Definition: base_struct.h:502
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
wxPoint m_Pos
Definition: eda_rect.h:47
static const BITMAP_OPAQUE dummy_xpm[1]
Definition: base_struct.cpp:56
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
The common library.
wxPoint Centre() const
Definition: eda_rect.h:62
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
Provides class metadata.
Definition: property_mgr.h:58
class ORTHOGONAL_DIMENSION, a linear dimension constrained to x/y
Definition: typeinfo.h:104
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
virtual const EDA_RECT GetBoundingBox() const
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: base_struct.cpp:97
bool IsValid() const
Definition: eda_rect.h:126
static const unsigned char dummy_png[]
Implementation of EDA_ITEM base class for KiCad.
Definition: base_struct.cpp:43
SEARCH_RESULT
Definition: base_struct.h:51
BASE_SCREEN class implementation.
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
virtual const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
bool m_forceVisible
Definition: base_struct.h:175
virtual BITMAP_DEF GetMenuImage() const
Function GetMenuImage returns a pointer to an image to be used in menus.
ENUM_TO_WXANY(KICAD_T)
virtual bool Matches(wxFindReplaceData &aSearchData, void *aAuxData)
Function Matches compares the item against the search criteria in aSearchData.
Definition: base_struct.h:458
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxSize GetSize() const
Definition: eda_rect.h:103
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
const wxPoint ClosestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is closest to the provided point.