KiCad PCB EDA Suite
arc_assistant.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) 2017 Kicad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
25 
28 #include <view/view.h>
29 
30 #include <common.h>
31 #include <base_units.h>
32 
33 using namespace KIGFX::PREVIEW;
34 
36  EDA_ITEM( NOT_USED ),
37  m_constructMan( aManager )
38 {
39 }
40 
41 
43 {
44  BOX2I tmp;
45 
46  // no bounding box when no graphic shown
47  if( m_constructMan.IsReset() )
48  return tmp;
49 
50  // just enclose the whle circular area
51  auto origin = m_constructMan.GetOrigin();
52  auto radius = m_constructMan.GetRadius();
53  VECTOR2D rVec( radius, radius );
54 
55  tmp.SetOrigin( origin + rVec );
56  tmp.SetEnd( origin - rVec );
57  tmp.Normalize();
58  return tmp;
59 }
60 
61 
68 double getNormDeciDegFromRad( double aRadians )
69 {
70  double degs = RAD2DECIDEG( aRadians );
71 
72  // normalise to +/- 360
73  while( degs < -3600.0 )
74  degs += 3600.0;
75 
76  while( degs > 3600.0 )
77  degs -= 3600.0;
78 
79  return degs;
80 }
81 
82 
83 static const double ANGLE_EPSILON = 1e-9;
84 
85 double angleIsSpecial( double aRadians )
86 {
87  return std::fabs( std::remainder( aRadians, M_PI_4 ) ) < ANGLE_EPSILON;
88 }
89 
90 
91 static void drawLineWithHilight( KIGFX::GAL& aGal,
92  const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDim )
93 {
94  const auto vec = aEnd - aStart;
95  COLOR4D strokeColor = PreviewOverlayDefaultColor();
96 
97  if( angleIsSpecial( vec.Angle() ) )
98  strokeColor = PreviewOverlaySpecialAngleColor();
99 
100  aGal.SetStrokeColor( strokeColor.WithAlpha( PreviewOverlayDeemphAlpha( aDim ) ) );
101  aGal.DrawLine( aStart, aEnd );
102 }
103 
104 
105 static void drawArcWithHilight( KIGFX::GAL& aGal,
106  const VECTOR2I& aOrigin, double aRad, double aStartAngle,
107  double aEndAngle )
108 {
110 
111  if( angleIsSpecial( aStartAngle - aEndAngle ) )
113 
114  aGal.SetStrokeColor( color );
115  aGal.SetFillColor( color.WithAlpha( 0.2 ) );
116 
117  // draw the angle reference arc
118  aGal.DrawArc( aOrigin, aRad, -aStartAngle, -aEndAngle );
119 }
120 
121 
122 void ARC_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
123 {
124  auto& gal = *aView->GetGAL();
125 
126  // not in a position to draw anything
127  if( m_constructMan.IsReset() )
128  return;
129 
130  gal.SetLineWidth( 1.0 );
131  gal.SetIsStroke( true );
132  gal.SetIsFill( true );
133 
134  gal.ResetTextAttributes();
135 
136  // constant text size on screen
137  SetConstantGlyphHeight( gal, 12.0 );
138 
139  // angle reference arc size
140  const double innerRad = 12.0 / gal.GetWorldScale();
141 
142  const auto origin = m_constructMan.GetOrigin();
143 
144  // draw first radius line
145  bool dimFirstLine = m_constructMan.GetStep() > ARC_GEOM_MANAGER::SET_START;
146 
147  drawLineWithHilight( gal, origin, m_constructMan.GetStartRadiusEnd(), dimFirstLine );
148 
149  std::vector<wxString> cursorStrings;
150 
152  {
153  // haven't started the angle selection phase yet
154 
155  auto initAngle = m_constructMan.GetStartAngle();
156 
157  const auto angleRefLineEnd = m_constructMan.GetOrigin() + VECTOR2D( innerRad * 1.5, 0.0 );
158 
159  gal.SetStrokeColor( PreviewOverlayDefaultColor() );
160  gal.DrawLine( origin, angleRefLineEnd );
161 
162  // draw the angle reference arc
163  drawArcWithHilight( gal, origin, innerRad, initAngle, 0.0 );
164 
165  double degs = getNormDeciDegFromRad( initAngle );
166 
167  cursorStrings.push_back( DimensionLabel( "r", m_constructMan.GetRadius(), g_UserUnit ) );
168  cursorStrings.push_back( DimensionLabel( "θ", degs, DEGREES ) );
169  }
170  else
171  {
172  drawLineWithHilight( gal, origin, m_constructMan.GetEndRadiusEnd(), false );
173 
174  auto start = m_constructMan.GetStartAngle();
175  auto subtended = m_constructMan.GetSubtended();
176 
177  drawArcWithHilight( gal, origin, innerRad, start, start + subtended );
178 
179  double subtendedDeg = getNormDeciDegFromRad( subtended );
180  double endAngleDeg = getNormDeciDegFromRad( start + subtended );
181 
182  // draw dimmed extender line to cursor
183  drawLineWithHilight( gal, origin, m_constructMan.GetLastPoint(), true );
184 
185  cursorStrings.push_back( DimensionLabel( "Δθ", subtendedDeg, DEGREES ) );
186  cursorStrings.push_back( DimensionLabel( "θ", endAngleDeg, DEGREES ) );
187  }
188 
189  // FIXME: spaces choke OpenGL lp:1668455
190  for( auto& str : cursorStrings )
191  {
192  str.erase( std::remove( str.begin(), str.end(), ' ' ),
193  str.end() );
194  }
195 
196  // place the text next to cursor, on opposite side from radius
198  origin - m_constructMan.GetLastPoint(),
199  cursorStrings );
200 }
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
Class ARC_GEOM_MANAGER.
Implementation of conversion functions that require both schematic and board internal units...
ARC_ASSISTANT(const ARC_GEOM_MANAGER &aManager)
COLOR4D PreviewOverlayDefaultColor()
The default fill/stroke color of preview overlay items.
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle)
Draw an arc.
the 3d code uses this value
Definition: typeinfo.h:92
double RAD2DECIDEG(double rad)
Definition: trigo.h:196
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
const ARC_GEOM_MANAGER & m_constructMan
Definition: arc_assistant.h:69
double GetRadius() const
Get the radius of the arc (valid if step >= SET_START)
VECTOR2I GetEndRadiusEnd() const
Get the coordinates of the arc end point
static void drawArcWithHilight(KIGFX::GAL &aGal, const VECTOR2I &aOrigin, double aRad, double aStartAngle, double aEndAngle)
virtual void SetLineWidth(double aLineWidth)
Set the line width.
static void drawLineWithHilight(KIGFX::GAL &aGal, const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDim)
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
Waiting to lock in origin point
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
VECTOR2I GetLastPoint() const
Gets the last point added (locked in or not).
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:117
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:232
static const double ANGLE_EPSILON
double GetSubtended() const
Get the angle of the vector leading to the end point (valid if step >= SET_ANGLE) ...
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
COLOR4D PreviewOverlaySpecialAngleColor()
The colour of "special" angle overlay features.
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:213
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers. ...
double getNormDeciDegFromRad(double aRadians)
Get deci-degrees from radians, normalised to +/- 360.
void SetConstantGlyphHeight(KIGFX::GAL &aGal, double aHeight)
Set the GAL glyph height to a constant scaled value, so that it always looks the same on screen...
void SetOrigin(const Vec &pos)
Definition: box2.h:198
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
The common library.
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS_T aUnits)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
double GetStartAngle() const
Get the angle of the vector leading to the start point (valid if step >= SET_START) ...
VECTOR2I GetOrigin() const
Get the centre point of the arc (valid when state > SET_ORIGIN)
void DrawTextNextToCursor(KIGFX::GAL &aGal, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
Class VIEW.
Definition: view.h:58
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const overridefinal
Draw the assistance (with reference to the contstruction manager.
ARC_STEPS GetStep() const
Get the current step the mananger is on (useful when drawing something depends on the current state) ...
VECTOR2I GetStartRadiusEnd() const
Get the coordinates of the arc start
Class GAL is the abstract interface for drawing on a 2D-surface.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
double angleIsSpecial(double aRadians)