KiCad PCB EDA Suite
lib_arc.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) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2004-2015 KiCad Developers, see change_log.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 <fctsys.h>
30 #include <gr_basic.h>
31 #include <macros.h>
32 #include <class_drawpanel.h>
33 #include <plot_common.h>
34 #include <trigo.h>
35 #include <wxstruct.h>
36 #include <richio.h>
37 #include <base_units.h>
38 #include <msgpanel.h>
39 #include <bitmaps.h>
40 
41 #include <general.h>
42 #include <lib_arc.h>
43 #include <transform.h>
44 
45 // Helper function
46 static inline wxPoint twoPointVector( const wxPoint &startPoint, const wxPoint &endPoint )
47 {
48  return endPoint - startPoint;
49 }
50 
51 
55 static wxPoint calcCenter( const wxPoint& A, const wxPoint& B, const wxPoint& C )
56 {
57  double circumCenterX, circumCenterY;
58  double Ax = (double) A.x;
59  double Ay = (double) A.y;
60  double Bx = (double) B.x;
61  double By = (double) B.y;
62  double Cx = (double) C.x;
63  double Cy = (double) C.y;
64 
65  wxPoint circumCenter;
66 
67  double D = 2.0 * ( Ax * ( By - Cy ) + Bx * ( Cy - Ay ) + Cx * ( Ay - By ) );
68 
69  // prevent division / 0
70  if( fabs( D ) < 1e-7 )
71  D = 1e-7;
72 
73  circumCenterX = ( (Ay * Ay + Ax * Ax) * (By - Cy) +
74  (By * By + Bx * Bx) * (Cy - Ay) +
75  (Cy * Cy + Cx * Cx) * (Ay - By) ) / D;
76 
77  circumCenterY = ( (Ay * Ay + Ax * Ax) * (Cx - Bx) +
78  (By * By + Bx * Bx) * (Ax - Cx) +
79  (Cy * Cy + Cx * Cx) * (Bx - Ax) ) / D;
80 
81  circumCenter.x = (int) circumCenterX;
82  circumCenter.y = (int) circumCenterY;
83 
84  return circumCenter;
85 }
86 
87 
88 LIB_ARC::LIB_ARC( LIB_PART* aParent ) : LIB_ITEM( LIB_ARC_T, aParent )
89 {
90  m_Radius = 0;
91  m_t1 = 0;
92  m_t2 = 0;
93  m_Width = 0;
94  m_Fill = NO_FILL;
95  m_isFillable = true;
96  m_typeName = _( "Arc" );
97  m_editState = 0;
98  m_lastEditState = 0;
101  m_editDirection = 0;
102 }
103 
104 
105 bool LIB_ARC::Save( OUTPUTFORMATTER& aFormatter )
106 {
107  int x1 = m_t1;
108 
109  if( x1 > 1800 )
110  x1 -= 3600;
111 
112  int x2 = m_t2;
113 
114  if( x2 > 1800 )
115  x2 -= 3600;
116 
117  aFormatter.Print( 0, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n",
118  m_Pos.x, m_Pos.y, m_Radius, x1, x2, m_Unit, m_Convert, m_Width,
120  m_ArcEnd.y );
121 
122  return true;
123 }
124 
125 
126 bool LIB_ARC::Load( LINE_READER& aLineReader, wxString& aErrorMsg )
127 {
128  int startx, starty, endx, endy, cnt;
129  char tmp[256] = "";
130  char* line = (char*) aLineReader;
131 
132  cnt = sscanf( line + 2, "%d %d %d %d %d %d %d %d %255s %d %d %d %d",
133  &m_Pos.x, &m_Pos.y, &m_Radius, &m_t1, &m_t2, &m_Unit,
134  &m_Convert, &m_Width, tmp, &startx, &starty, &endx, &endy );
135  if( cnt < 8 )
136  {
137  aErrorMsg.Printf( _( "Arc only had %d parameters of the required 8" ), cnt );
138  return false;
139  }
140 
141  if( tmp[0] == 'F' )
143 
144  if( tmp[0] == 'f' )
146 
149 
150  // Actual Coordinates of arc ends are read from file
151  if( cnt >= 13 )
152  {
153  m_ArcStart.x = startx;
154  m_ArcStart.y = starty;
155  m_ArcEnd.x = endx;
156  m_ArcEnd.y = endy;
157  }
158  else
159  {
160  // Actual Coordinates of arc ends are not read from file
161  // (old library), calculate them
163  m_ArcStart.y = 0;
164  m_ArcEnd.x = m_Radius;
165  m_ArcEnd.y = 0;
167  m_ArcStart.x += m_Pos.x;
168  m_ArcStart.y += m_Pos.y;
170  m_ArcEnd.x += m_Pos.x;
171  m_ArcEnd.y += m_Pos.y;
172  }
173 
174  return true;
175 }
176 
177 
178 bool LIB_ARC::HitTest( const wxPoint& aRefPoint ) const
179 {
180  int mindist = GetPenSize() / 2;
181 
182  // Have a minimal tolerance for hit test
183  if( mindist < MINIMUM_SELECTION_DISTANCE )
184  mindist = MINIMUM_SELECTION_DISTANCE;
185 
186  return HitTest( aRefPoint, mindist, DefaultTransform );
187 }
188 
189 
190 bool LIB_ARC::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
191 {
192 
193  if( aThreshold < 0 )
194  aThreshold = GetPenSize() / 2;
195 
196  // TODO: use aTransMat to calculates parameters
197  wxPoint relativePosition = aPosition;
198 
199  relativePosition.y = -relativePosition.y; // reverse Y axis
200 
201  int distance = KiROUND( GetLineLength( m_Pos, relativePosition ) );
202 
203  if( abs( distance - m_Radius ) > aThreshold )
204  return false;
205 
206  // We are on the circle, ensure we are only on the arc, i.e. between
207  // m_ArcStart and m_ArcEnd
208 
209  wxPoint startEndVector = twoPointVector( m_ArcStart, m_ArcEnd);
210  wxPoint startRelativePositionVector = twoPointVector( m_ArcStart, relativePosition );
211 
212  wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
213  wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
214  wxPoint centerRelativePositionVector = twoPointVector( m_Pos, relativePosition );
215 
216  // Compute the cross product to check if the point is in the sector
217  double crossProductStart = CrossProduct( centerStartVector, centerRelativePositionVector );
218  double crossProductEnd = CrossProduct( centerEndVector, centerRelativePositionVector );
219 
220  // The cross products need to be exchanged, depending on which side the center point
221  // relative to the start point to end point vector lies
222  if( CrossProduct( startEndVector, startRelativePositionVector ) < 0 )
223  {
224  std::swap( crossProductStart, crossProductEnd );
225  }
226 
227  // When the cross products have a different sign, the point lies in sector
228  // also check, if the reference is near start or end point
229  return HitTestPoints( m_ArcStart, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
230  HitTestPoints( m_ArcEnd, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
231  ( crossProductStart <= 0 && crossProductEnd >= 0 );
232 }
233 
234 
236 {
237  return new LIB_ARC( *this );
238 }
239 
240 
241 int LIB_ARC::compare( const LIB_ITEM& aOther ) const
242 {
243  wxASSERT( aOther.Type() == LIB_ARC_T );
244 
245  const LIB_ARC* tmp = ( LIB_ARC* ) &aOther;
246 
247  if( m_Pos.x != tmp->m_Pos.x )
248  return m_Pos.x - tmp->m_Pos.x;
249 
250  if( m_Pos.y != tmp->m_Pos.y )
251  return m_Pos.y - tmp->m_Pos.y;
252 
253  if( m_t1 != tmp->m_t1 )
254  return m_t1 - tmp->m_t1;
255 
256  if( m_t2 != tmp->m_t2 )
257  return m_t2 - tmp->m_t2;
258 
259  return 0;
260 }
261 
262 
263 void LIB_ARC::SetOffset( const wxPoint& aOffset )
264 {
265  m_Pos += aOffset;
266  m_ArcStart += aOffset;
267  m_ArcEnd += aOffset;
268 }
269 
270 
271 bool LIB_ARC::Inside( EDA_RECT& aRect ) const
272 {
273  return aRect.Contains( m_ArcStart.x, -m_ArcStart.y )
274  || aRect.Contains( m_ArcEnd.x, -m_ArcEnd.y );
275 }
276 
277 
278 void LIB_ARC::Move( const wxPoint& aPosition )
279 {
280  wxPoint offset = aPosition - m_Pos;
281  m_Pos = aPosition;
282  m_ArcStart += offset;
283  m_ArcEnd += offset;
284 }
285 
286 
287 void LIB_ARC::MirrorHorizontal( const wxPoint& aCenter )
288 {
289  m_Pos.x -= aCenter.x;
290  m_Pos.x *= -1;
291  m_Pos.x += aCenter.x;
292  m_ArcStart.x -= aCenter.x;
293  m_ArcStart.x *= -1;
294  m_ArcStart.x += aCenter.x;
295  m_ArcEnd.x -= aCenter.x;
296  m_ArcEnd.x *= -1;
297  m_ArcEnd.x += aCenter.x;
298  std::swap( m_ArcStart, m_ArcEnd );
299  std::swap( m_t1, m_t2 );
300  m_t1 = 1800 - m_t1;
301  m_t2 = 1800 - m_t2;
302  if( m_t1 > 3600 || m_t2 > 3600 )
303  {
304  m_t1 -= 3600;
305  m_t2 -= 3600;
306  }
307  else if( m_t1 < -3600 || m_t2 < -3600 )
308  {
309  m_t1 += 3600;
310  m_t2 += 3600;
311  }
312 }
313 
314 void LIB_ARC::MirrorVertical( const wxPoint& aCenter )
315 {
316  m_Pos.y -= aCenter.y;
317  m_Pos.y *= -1;
318  m_Pos.y += aCenter.y;
319  m_ArcStart.y -= aCenter.y;
320  m_ArcStart.y *= -1;
321  m_ArcStart.y += aCenter.y;
322  m_ArcEnd.y -= aCenter.y;
323  m_ArcEnd.y *= -1;
324  m_ArcEnd.y += aCenter.y;
325  std::swap( m_ArcStart, m_ArcEnd );
326  std::swap( m_t1, m_t2 );
327  m_t1 = - m_t1;
328  m_t2 = - m_t2;
329  if( m_t1 > 3600 || m_t2 > 3600 )
330  {
331  m_t1 -= 3600;
332  m_t2 -= 3600;
333  }
334  else if( m_t1 < -3600 || m_t2 < -3600 )
335  {
336  m_t1 += 3600;
337  m_t2 += 3600;
338  }
339 }
340 
341 void LIB_ARC::Rotate( const wxPoint& aCenter, bool aRotateCCW )
342 {
343  int rot_angle = aRotateCCW ? -900 : 900;
344  RotatePoint( &m_Pos, aCenter, rot_angle );
345  RotatePoint( &m_ArcStart, aCenter, rot_angle );
346  RotatePoint( &m_ArcEnd, aCenter, rot_angle );
347  m_t1 -= rot_angle;
348  m_t2 -= rot_angle;
349  if( m_t1 > 3600 || m_t2 > 3600 )
350  {
351  m_t1 -= 3600;
352  m_t2 -= 3600;
353  }
354  else if( m_t1 < -3600 || m_t2 < -3600 )
355  {
356  m_t1 += 3600;
357  m_t2 += 3600;
358  }
359 }
360 
361 
362 
363 void LIB_ARC::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
364  const TRANSFORM& aTransform )
365 {
366  wxASSERT( aPlotter != NULL );
367 
368  int t1 = m_t1;
369  int t2 = m_t2;
370  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
371 
372  aTransform.MapAngles( &t1, &t2 );
373 
374  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
375  {
377  aPlotter->Arc( pos, -t2, -t1, m_Radius, FILLED_SHAPE, 0 );
378  }
379 
380  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
381  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
382  aPlotter->Arc( pos, -t2, -t1, m_Radius, already_filled ? NO_FILL : m_Fill, GetPenSize() );
383 }
384 
385 
387 {
388  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
389 }
390 
391 
392 void LIB_ARC::drawEditGraphics( EDA_RECT* aClipBox, wxDC* aDC, COLOR4D aColor )
393 {
394  // The edit indicators only get drawn when a new arc is being drawn.
395  if( !IsNew() )
396  return;
397 
398  // Use the last edit state so when the drawing switches from the end mode to the center
399  // point mode, the last line between the center points gets erased.
400  if( m_lastEditState == 1 )
401  {
402  GRLine( aClipBox, aDC, m_ArcStart.x, -m_ArcStart.y, m_ArcEnd.x, -m_ArcEnd.y, 0, aColor );
403  }
404  else
405  {
406  GRDashedLine( aClipBox, aDC, m_ArcStart.x, -m_ArcStart.y, m_Pos.x, -m_Pos.y, 0, aColor );
407  GRDashedLine( aClipBox, aDC, m_ArcEnd.x, -m_ArcEnd.y, m_Pos.x, -m_Pos.y, 0, aColor );
408  }
409 }
410 
411 
412 void LIB_ARC::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
413  COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
414  const TRANSFORM& aTransform )
415 {
416  // Don't draw the arc until the end point is selected. Only the edit indicators
417  // get drawn at this time.
418  if( IsNew() && m_lastEditState == 1 )
419  return;
420 
421  wxPoint pos1, pos2, posc;
423 
424  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
425  {
426  if( IsSelected() )
427  color = GetItemSelectedColor();
428  }
429  else
430  {
431  color = aColor;
432  }
433 
434  pos1 = aTransform.TransformCoordinate( m_ArcEnd ) + aOffset;
435  pos2 = aTransform.TransformCoordinate( m_ArcStart ) + aOffset;
436  posc = aTransform.TransformCoordinate( m_Pos ) + aOffset;
437  int pt1 = m_t1;
438  int pt2 = m_t2;
439  bool swap = aTransform.MapAngles( &pt1, &pt2 );
440 
441  if( swap )
442  {
443  std::swap( pos1.x, pos2.x );
444  std::swap( pos1.y, pos2.y );
445  }
446 
447  GRSetDrawMode( aDC, aDrawMode );
448 
449  FILL_T fill = aData ? NO_FILL : m_Fill;
450 
451  if( aColor != COLOR4D::UNSPECIFIED )
452  fill = NO_FILL;
453 
454  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
455 
456  if( fill == FILLED_WITH_BG_BODYCOLOR )
457  {
458  GRFilledArc( clipbox, aDC, posc.x, posc.y, pt1, pt2,
459  m_Radius, GetPenSize( ),
462  }
463  else if( fill == FILLED_SHAPE && !aData )
464  {
465  GRFilledArc( clipbox, aDC, posc.x, posc.y, pt1, pt2, m_Radius,
466  color, color );
467  }
468  else
469  {
470 
471 #ifdef DRAW_ARC_WITH_ANGLE
472 
473  GRArc( clipbox, aDC, posc.x, posc.y, pt1, pt2, m_Radius,
474  GetPenSize(), color );
475 #else
476 
477  GRArc1( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y,
478  posc.x, posc.y, GetPenSize(), color );
479 #endif
480  }
481 
482  /* Set to one (1) to draw bounding box around arc to validate bounding box
483  * calculation. */
484 #if 0
485  EDA_RECT bBox = GetBoundingBox();
486  bBox.RevertYAxis();
487  bBox = aTransform.TransformCoordinate( bBox );
488  bBox.Move( aOffset );
489  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
490 #endif
491 }
492 
493 
495 {
496  int minX, minY, maxX, maxY, angleStart, angleEnd;
497  EDA_RECT rect;
498  wxPoint nullPoint, startPos, endPos, centerPos;
499  wxPoint normStart = m_ArcStart - m_Pos;
500  wxPoint normEnd = m_ArcEnd - m_Pos;
501 
502  if( ( normStart == nullPoint ) || ( normEnd == nullPoint ) || ( m_Radius == 0 ) )
503  {
504  wxLogDebug( wxT("Invalid arc drawing definition, center(%d, %d) \
505 start(%d, %d), end(%d, %d), radius %d" ),
506  m_Pos.x, m_Pos.y, m_ArcStart.x, m_ArcStart.y, m_ArcEnd.x,
507  m_ArcEnd.y, m_Radius );
508  return rect;
509  }
510 
513  centerPos = DefaultTransform.TransformCoordinate( m_Pos );
514  angleStart = m_t1;
515  angleEnd = m_t2;
516 
517  if( DefaultTransform.MapAngles( &angleStart, &angleEnd ) )
518  {
519  std::swap( endPos.x, startPos.x );
520  std::swap( endPos.y, startPos.y );
521  }
522 
523  /* Start with the start and end point of the arc. */
524  minX = std::min( startPos.x, endPos.x );
525  minY = std::min( startPos.y, endPos.y );
526  maxX = std::max( startPos.x, endPos.x );
527  maxY = std::max( startPos.y, endPos.y );
528 
529  /* Zero degrees is a special case. */
530  if( angleStart == 0 )
531  maxX = centerPos.x + m_Radius;
532 
533  /* Arc end angle wrapped passed 360. */
534  if( angleStart > angleEnd )
535  angleEnd += 3600;
536 
537  if( angleStart <= 900 && angleEnd >= 900 ) /* 90 deg */
538  maxY = centerPos.y + m_Radius;
539 
540  if( angleStart <= 1800 && angleEnd >= 1800 ) /* 180 deg */
541  minX = centerPos.x - m_Radius;
542 
543  if( angleStart <= 2700 && angleEnd >= 2700 ) /* 270 deg */
544  minY = centerPos.y - m_Radius;
545 
546  if( angleStart <= 3600 && angleEnd >= 3600 ) /* 0 deg */
547  maxX = centerPos.x + m_Radius;
548 
549  rect.SetOrigin( minX, minY );
550  rect.SetEnd( maxX, maxY );
551  rect.Inflate( ( GetPenSize()+1 ) / 2 );
552 
553  return rect;
554 }
555 
556 
557 void LIB_ARC::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList )
558 {
559  wxString msg;
560  EDA_RECT bBox = GetBoundingBox();
561 
562  LIB_ITEM::GetMsgPanelInfo( aList );
563 
564  msg = StringFromValue( g_UserUnit, m_Width, true );
565 
566  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
567 
568  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
569  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
570 
571  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
572 }
573 
574 
576 {
577  return wxString::Format( _( "Arc center (%s, %s), radius %s" ),
581 }
582 
583 
585 {
586  return add_arc_xpm;
587 }
588 
589 
590 void LIB_ARC::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
591 {
592  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
593  wxT( "Invalid edit mode for LIB_ARC object." ) );
594 
595  if( aEditMode == IS_NEW )
596  {
597  m_ArcStart = m_ArcEnd = aPosition;
599  }
600  else if( aEditMode == IS_MOVED )
601  {
603  m_initialCursorPos = aPosition;
605  }
606  else
607  {
608  // The arc center point has to be rotated with while adjusting the
609  // start or end point, determine the side of this point and the distance
610  // from the start / end point
611  wxPoint middlePoint = wxPoint( (m_ArcStart.x + m_ArcEnd.x) / 2,
612  (m_ArcStart.y + m_ArcEnd.y) / 2 );
613  wxPoint centerVector = m_Pos - middlePoint;
614  wxPoint startEndVector = twoPointVector( m_ArcStart, m_ArcEnd );
615  m_editCenterDistance = EuclideanNorm( centerVector );
616 
617  // Determine on which side is the center point
618  m_editDirection = CrossProduct( startEndVector, centerVector ) ? 1 : -1;
619 
620  // Drag either the start, end point or the outline
622  {
624  }
625  else if( HitTestPoints( m_ArcEnd, aPosition, MINIMUM_SELECTION_DISTANCE ) )
626  {
628  }
629  else
630  {
632  }
633 
634  m_editState = 0;
636  }
637 
638  m_Flags = aEditMode;
639 }
640 
641 
642 bool LIB_ARC::ContinueEdit( const wxPoint aPosition )
643 {
644  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
645  wxT( "Bad call to ContinueEdit(). LIB_ARC is not being edited." ) );
646 
647  if( m_Flags == IS_NEW )
648  {
649  if( m_editState == 1 ) // Second position yields the arc segment length.
650  {
651  m_ArcEnd = aPosition;
652  m_editState = 2;
653  SetEraseLastDrawItem( false );
654  return true; // Need third position to calculate center point.
655  }
656  }
657 
658  return false;
659 }
660 
661 
662 void LIB_ARC::EndEdit( const wxPoint& aPosition, bool aAbort )
663 {
664  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
665  wxT( "Bad call to EndEdit(). LIB_ARC is not being edited." ) );
666 
667  SetEraseLastDrawItem( false );
668  m_lastEditState = 0;
669  m_editState = 0;
670  m_Flags = 0;
671 }
672 
673 
674 void LIB_ARC::calcEdit( const wxPoint& aPosition )
675 {
676  if( m_Flags == IS_RESIZED )
677  {
678  wxPoint newCenterPoint, startPos, endPos;
679 
680  // Choose the point of the arc to be adjusted
682  {
683  startPos = aPosition;
684  endPos = m_ArcEnd;
685  }
686  else if( m_editSelectPoint == ARC_STATUS_END )
687  {
688  endPos = aPosition;
689  startPos = m_ArcStart;
690  }
691  else
692  {
693  // Use the cursor for adjusting the arc curvature
694  startPos = m_ArcStart;
695  endPos = m_ArcEnd;
696 
697  // If the distance is too small, use the old center point
698  // else the new center point is calculated over the three points start/end/cursor
699  if( DistanceLinePoint( startPos, endPos, aPosition ) > MINIMUM_SELECTION_DISTANCE )
700  {
701  newCenterPoint = calcCenter( startPos, aPosition, endPos );
702  }
703  else
704  {
705  newCenterPoint = m_Pos;
706  }
707 
708  // Determine if the arc angle is larger than 180 degrees -> this happens if both
709  // points (cursor position, center point) lie on the same side of the vector
710  // start-end
711  double crossA = CrossProduct( twoPointVector( startPos, endPos ),
712  twoPointVector( endPos, aPosition ) );
713  double crossB = CrossProduct( twoPointVector( startPos, endPos ),
714  twoPointVector( endPos, newCenterPoint ) );
715 
716  if( ( crossA < 0 && crossB < 0 ) || ( crossA >= 0 && crossB >= 0 ) )
717  newCenterPoint = m_Pos;
718  }
719 
721  {
722  // Compute the new center point when the start/end points are modified
723  wxPoint middlePoint = wxPoint( (startPos.x + endPos.x) / 2,
724  (startPos.y + endPos.y) / 2 );
725 
726  wxPoint startEndVector = twoPointVector( startPos, endPos );
727  wxPoint perpendicularVector = wxPoint( -startEndVector.y, startEndVector.x );
728  double lengthPerpendicularVector = EuclideanNorm( perpendicularVector );
729 
730  // prevent too large values, division / 0
731  if( lengthPerpendicularVector < 1e-1 )
732  lengthPerpendicularVector = 1e-1;
733 
734  perpendicularVector.x = (int) ( (double) perpendicularVector.x *
736  lengthPerpendicularVector ) * m_editDirection;
737  perpendicularVector.y = (int) ( (double) perpendicularVector.y *
739  lengthPerpendicularVector ) * m_editDirection;
740 
741  newCenterPoint = middlePoint + perpendicularVector;
742 
743  m_ArcStart = startPos;
744  m_ArcEnd = endPos;
745  }
746 
747  m_Pos = newCenterPoint;
749  }
750  else if( m_Flags == IS_NEW )
751  {
752  if( m_editState == 1 )
753  {
754  m_ArcEnd = aPosition;
755  }
756 
759 
760  // Keep the arc center point up to date. Otherwise, there will be edit graphic
761  // artifacts left behind from the initial draw.
762  int dx, dy;
763  int cX, cY;
764  double angle;
765 
766  cX = aPosition.x;
767  cY = aPosition.y;
768 
769  dx = m_ArcEnd.x - m_ArcStart.x;
770  dy = m_ArcEnd.y - m_ArcStart.y;
771  cX -= m_ArcStart.x;
772  cY -= m_ArcStart.y;
773  angle = ArcTangente( dy, dx );
774  RotatePoint( &dx, &dy, angle ); /* The segment dx, dy is horizontal
775  * -> Length = dx, dy = 0 */
776  RotatePoint( &cX, &cY, angle );
777  cX = dx / 2; /* cX, cY is on the median segment 0.0 a dx, 0 */
778 
779  RotatePoint( &cX, &cY, -angle );
780  cX += m_ArcStart.x;
781  cY += m_ArcStart.y;
782  m_Pos.x = cX;
783  m_Pos.y = cY;
785 
787  }
788  else if( m_Flags == IS_MOVED )
789  {
790  Move( m_initialPos + aPosition - m_initialCursorPos );
791  }
792 }
793 
794 
796 {
797  wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
798  wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
799 
800  m_Radius = KiROUND( EuclideanNorm( centerStartVector ) );
801 
802  // Angles in eeschema are still integers
803  m_t1 = KiROUND( ArcTangente( centerStartVector.y, centerStartVector.x ) );
804  m_t2 = KiROUND( ArcTangente( centerEndVector.y, centerEndVector.x ) );
805 
807  NORMALIZE_ANGLE_POS( m_t2 ); // angles = 0 .. 3600
808 
809  // Restrict angle to less than 180 to avoid PBS display mirror Trace because it is
810  // assumed that the arc is less than 180 deg to find orientation after rotate or mirror.
811  if( (m_t2 - m_t1) > 1800 )
812  m_t2 -= 3600;
813  else if( (m_t2 - m_t1) <= -1800 )
814  m_t2 += 3600;
815 
816  while( (m_t2 - m_t1) >= 1800 )
817  {
818  m_t2--;
819  m_t1++;
820  }
821 
822  while( (m_t1 - m_t2) >= 1800 )
823  {
824  m_t2++;
825  m_t1--;
826  }
827 
829 
830  if( !IsMoving() )
832 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:104
Definition: colors.h:57
int m_Radius
Definition: lib_arc.h:47
wxString CoordinateToString(int aValue, bool aConvertToMils)
Function CoordinateToString is a helper to convert the integer coordinate aValue to a string in inche...
Definition: base_units.cpp:117
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
static wxPoint twoPointVector(const wxPoint &startPoint, const wxPoint &endPoint)
Definition: lib_arc.cpp:46
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
bool IsMoving() const
Definition: base_struct.h:218
int m_t1
Definition: lib_arc.h:48
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) override
Plot the draw item using the plot object.
Definition: lib_arc.cpp:363
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Function GetLineLength returns the length of a line segment defined by aPointA and aPointB...
Definition: trigo.h:183
PNG memory record (file in memory).
Definition: bitmap_types.h:38
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:290
wxString m_typeName
Name of object displayed in the message panel.
double m_editCenterDistance
Definition: lib_arc.h:54
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
void SetEraseLastDrawItem(bool aErase=true)
Implementation of conversion functions that require both schematic and board internal units...
bool IsNew() const
Definition: base_struct.h:216
virtual void SetColor(COLOR4D color)=0
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_draw_item.h:54
bool Contains(const wxPoint &aPoint) const
Function Contains.
void GRLine(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color)
Definition: gr_basic.cpp:352
int m_t2
Definition: lib_arc.h:49
int m_lastEditState
Definition: lib_arc.h:58
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:203
const EDA_RECT GetBoundingBox() const override
Definition: lib_arc.cpp:494
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
bool IsSelected() const
Definition: base_struct.h:221
void SetOrigin(const wxPoint &pos)
bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
Definition: lib_arc.cpp:178
int m_Unit
Unit identification for multiple parts per package.
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
bool Load(LINE_READER &aLineReader, wxString &aErrorMsg) override
Definition: lib_arc.cpp:126
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_arc.cpp:584
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: lib_arc.cpp:575
wxPoint m_ArcEnd
Definition: lib_arc.h:51
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:222
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:166
void calcRadiusAngles()
Calculate the radius and angle of an arc using the start, end, and center points. ...
Definition: lib_arc.cpp:795
void SetOffset(const wxPoint &aOffset) override
Function Offset sets the drawing object by aOffset from the current position.
Definition: lib_arc.cpp:263
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
#define abs(a)
Definition: auxiliary.h:84
Class LIB_ITEM is the base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:77
void MirrorVertical(const wxPoint &aCenter) override
Function MirrorVertical mirrors the draw object along the MirrorVertical (Y) axis about aCenter point...
Definition: lib_arc.cpp:314
bool Save(OUTPUTFORMATTER &aFormatter) override
Function Save writes draw item object to aFormatter in component library "*.lib" format.
Definition: lib_arc.cpp:105
This file contains miscellaneous commonly used macros and functions.
void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
Definition: lib_arc.cpp:557
#define IS_NEW
New item, just created.
Definition: base_struct.h:113
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
const wxPoint & GetOrigin() const
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1077
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
bool Inside(EDA_RECT &aRect) const override
Function Inside tests if any part of the draw object is inside rectangle bounds of aRect...
Definition: lib_arc.cpp:271
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:271
bool MapAngles(int *aAngle1, int *aAngle2) const
Calculate new angles according to the transform.
Definition: transform.cpp:80
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
Base window classes and related definitions.
wxPoint m_Pos
Definition: lib_arc.h:52
void SetEnd(int x, int y)
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_arc.cpp:235
void GRArc(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, double StAngle, double EndAngle, int r, COLOR4D Color)
Definition: gr_basic.cpp:982
void Move(const wxPoint &aPosition) override
Function Move moves a draw object to aPosition.
Definition: lib_arc.cpp:278
bool m_isFillable
Flag to indicate if draw item is fillable.
Class LIB_PART defines a library part object.
unsigned STATUS_FLAGS
Definition: base_struct.h:144
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
Definition: lib_arc.cpp:662
int m_editState
Definition: lib_arc.h:56
EDA_RECT * GetClipBox()
double CrossProduct(const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product.
Definition: trigo.h:158
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
int compare(const LIB_ITEM &aOther) const override
Function compare provides the draw object specific comparison called by the == and < operators...
Definition: lib_arc.cpp:241
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
Definition: lib_arc.cpp:642
int GetPenSize() const override
Function GetPenSize.
Definition: lib_arc.cpp:386
virtual void Rotate()
Rotate the draw item.
void GRFilledArc(EDA_RECT *ClipBox, wxDC *DC, int x, int y, double StAngle, double EndAngle, int r, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:922
const wxPoint GetEnd() const
void MirrorHorizontal(const wxPoint &aCenter) override
Function MirrorHorizontal mirrors the draw object along the horizontal (X) axis about aCenter point...
Definition: lib_arc.cpp:287
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform) override
Draws the arc.
Definition: lib_arc.cpp:412
Base plotter engine class.
Definition: plot_common.h:86
#define D(x)
Definition: ptree.cpp:41
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
void GRDashedLine(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color)
Definition: gr_basic.cpp:374
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:92
TRANSFORM DefaultTransform
Definition: eeschema.cpp:57
const int fill_tab[3]
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:205
#define max(a, b)
Definition: auxiliary.h:86
wxPoint m_initialPos
Temporary position when moving an existing item.
void drawEditGraphics(EDA_RECT *aClipBox, wxDC *aDC, COLOR4D aColor) override
Draw the graphics when the arc is being edited.
Definition: lib_arc.cpp:392
FILL_T m_Fill
The body fill type.
LIB_ARC(LIB_PART *aParent)
Definition: lib_arc.cpp:88
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
bool HitTestPoints(const wxPoint &pointA, const wxPoint &pointB, double threshold)
Test, if two points are near each other.
Definition: trigo.h:142
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:175
Class EDA_RECT handles the component boundary box.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:114
int m_editDirection
Definition: lib_arc.h:57
int m_Convert
Shape identification for alternate body styles.
virtual void Arc(const wxPoint &centre, double StAngle, double EndAngle, int rayon, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH)
Generic fallback: arc rendered as a polyline.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:56
COLOR4D GetItemSelectedColor()
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:408
SELECT_T m_editSelectPoint
Definition: lib_arc.h:55
wxPoint m_ArcStart
Definition: lib_arc.h:50
static wxPoint calcCenter(const wxPoint &A, const wxPoint &B, const wxPoint &C)
Given three points A B C, compute the circumcenter of the resulting triangle reference: http://en...
Definition: lib_arc.cpp:55
Message panel definition file.
double DistanceLinePoint(const wxPoint &linePointA, const wxPoint &linePointB, const wxPoint &referencePoint)
Compute the distance between a line and a reference point Reference: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html.
Definition: trigo.h:121
void calcEdit(const wxPoint &aPosition) override
Calculates the center, radius, and angles at aPosition when the arc is being edited.
Definition: lib_arc.cpp:674
void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aStartPoint=wxPoint(0, 0)) override
Begin an editing a component library draw item in aEditMode at aPosition.
Definition: lib_arc.cpp:590
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
wxPoint m_initialCursorPos
Initial cursor position at the beginning of a move.
int m_Width
Definition: lib_arc.h:53
#define min(a, b)
Definition: auxiliary.h:85
void GRArc1(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int xc, int yc, COLOR4D Color)
Definition: gr_basic.cpp:873
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define IS_MOVED
Item being moved.
Definition: base_struct.h:112
Definition: colors.h:62