KiCad PCB EDA Suite
Loading...
Searching...
No Matches
construction_geom.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 The 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
26#include <layer_ids.h>
27#include <utility>
29#include <geometry/line.h>
32#include <view/view.h>
33
34using namespace KIGFX;
35
36
38 EDA_ITEM( nullptr, NOT_USED ), // Never added to a BOARD/SCHEMATIC so it needs no type
40{
41}
42
43
44void CONSTRUCTION_GEOM::AddDrawable( const DRAWABLE& aItem, bool aPersistent, int aLineWidth )
45{
46 m_drawables.push_back( { aItem, aPersistent, aLineWidth } );
47}
48
49
50void CONSTRUCTION_GEOM::SetSnapGuides( std::vector<SNAP_GUIDE> aGuides )
51{
52 m_snapGuides = std::move( aGuides );
53}
54
55
60
61
63{
64 // We could be a bit more circumspect here, but much of the time the
65 // extended lines go across the whole screen anyway
66 BOX2I bbox;
67 bbox.SetMaximum();
68 return bbox;
69}
70
71
72void CONSTRUCTION_GEOM::ViewDraw( int aLayer, VIEW* aView ) const
73{
74 GAL& gal = *aView->GetGAL();
75
76 gal.SetIsFill( false );
77 gal.SetIsStroke( true );
78 gal.SetLineWidth( 1 );
79
80 const BOX2I viewport = BOX2ISafe( aView->GetViewport() );
81
82 // Prevents extremely short snap lines from inhibiting drawing
83 // These can happen due to rounding in intersections, etc.
84 // (usually it is length 1 IU)
85 const int minSnapLineLength = 10;
86 const bool haveSnapLine = m_snapLine && m_snapLine->Length() >= minSnapLineLength;
87
88 // Avoid fighting with the snap line
89 const auto drawLineIfNotAlsoSnapLine = [&]( const SEG& aLine )
90 {
91 if( !haveSnapLine || !aLine.ApproxCollinear( *m_snapLine, 1 ) )
92 {
93 gal.DrawLine( aLine.A, aLine.B );
94 }
95 };
96
97 // Draw all the items
98 for( const DRAWABLE_INFO& drawable : m_drawables )
99 {
100 gal.SetStrokeColor( drawable.IsPersistent ? m_persistentColor : m_color );
101 gal.SetLineWidth( drawable.LineWidth / gal.GetWorldScale() );
102
103 std::visit(
104 [&]( const auto& visited )
105 {
106 using ItemType = std::decay_t<decltype( visited )>;
107
108 if constexpr( std::is_same_v<ItemType, LINE> )
109 {
110 // Extend the segment to the viewport boundary
111 std::optional<SEG> segToBoundary =
112 KIGEOM::ClipLineToBox( visited, viewport );
113
114 if( segToBoundary )
115 {
116 drawLineIfNotAlsoSnapLine( *segToBoundary );
117 }
118 }
119 else if constexpr( std::is_same_v<ItemType, HALF_LINE> )
120 {
121 // Extend the ray to the viewport boundary
122 std::optional<SEG> segToBoundary =
123 KIGEOM::ClipHalfLineToBox( visited, viewport );
124
125 if( segToBoundary )
126 {
127 drawLineIfNotAlsoSnapLine( *segToBoundary );
128 }
129 }
130 else if constexpr( std::is_same_v<ItemType, SEG> )
131 {
132 drawLineIfNotAlsoSnapLine( visited );
133 }
134 else if constexpr( std::is_same_v<ItemType, CIRCLE> )
135 {
136 gal.DrawCircle( visited.Center, visited.Radius );
137 }
138 else if constexpr( std::is_same_v<ItemType, SHAPE_ARC> )
139 {
140 gal.DrawArc( visited.GetCenter(), visited.GetRadius(),
141 visited.GetStartAngle(), visited.GetCentralAngle() );
142 }
143 else if constexpr( std::is_same_v<ItemType, VECTOR2I> )
144 {
145 KIGFX::DrawCross( gal, visited, aView->ToWorld( 16 ) );
146 }
147 },
148 drawable.Item );
149 }
150
151 for( const SNAP_GUIDE& guide : m_snapGuides )
152 {
153 const SEG& segment = guide.Segment;
154
155 if( segment.A == segment.B )
156 continue;
157
158 std::optional<SEG> clipped = KIGEOM::ClipLineToBox( LINE( segment ), viewport );
159
160 if( !clipped )
161 continue;
162
163 gal.SetStrokeColor( guide.Color );
164 gal.SetLineWidth( guide.LineWidth );
165 gal.DrawLine( clipped->A, clipped->B );
166 }
167
168 if( haveSnapLine )
169 {
171 gal.SetLineWidth( 2 );
172
173 const int dashSizeBasis = aView->ToWorld( 12 );
174 const int snapOriginMarkerSize = aView->ToWorld( 16 );
175
176 // Avoid clash with the snap marker if very close
177 const int omitStartMarkerIfWithinLength = aView->ToWorld( 8 );
178
179 // The line itself
180 KIGFX::DrawDashedLine( gal, *m_snapLine, dashSizeBasis );
181
182 // The line origin marker if the line is long enough
183 if( m_snapLine->A.Distance( m_snapLine->B ) > omitStartMarkerIfWithinLength )
184 {
185 KIGFX::DrawCross( gal, m_snapLine->A, snapOriginMarkerSize );
186 gal.DrawCircle( m_snapLine->A, snapOriginMarkerSize / 2 );
187 }
188 }
189}
190
191
192std::vector<int> CONSTRUCTION_GEOM::ViewGetLayers() const
193{
194 // Don't use LAYER_GP_OVERLAY, we need the construction geometry to be visible
195 // on top of the axis cross
196 std::vector<int> layers{ LAYER_UI_START };
197 return layers;
198}
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:929
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr void SetMaximum()
Definition box2.h:80
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:39
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
void AddDrawable(const DRAWABLE &aItem, bool aIsPersistent, int aLineWidth=1)
std::optional< SEG > m_snapLine
std::vector< SNAP_GUIDE > m_snapGuides
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
void SetSnapGuides(std::vector< SNAP_GUIDE > aGuides)
std::vector< DRAWABLE_INFO > m_drawables
std::variant< SEG, LINE, HALF_LINE, CIRCLE, SHAPE_ARC, VECTOR2I > DRAWABLE
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle)
Draw an arc.
double GetWorldScale() const
Get the world scale.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:66
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition view.cpp:530
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:202
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition view.cpp:467
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
VECTOR2I B
Definition seg.h:50
@ WHITE
Definition color4d.h:48
Utility functions for drawing compound items (i.e.
@ LAYER_UI_START
Definition layer_ids.h:357
std::optional< SEG > ClipHalfLineToBox(const HALF_LINE &aRay, const BOX2I &aBox)
Get the segment of a half-line that is inside a box, if any.
std::optional< SEG > ClipLineToBox(const LINE &aLine, const BOX2I &aBox)
Get the segment of a line that is inside a box, if any.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
void DrawCross(GAL &aGal, const VECTOR2I &aPosition, int aSize)
Draw a cross at a given position.
void DrawDashedLine(GAL &aGal, const SEG &aSeg, double aDashSize)
Draw a dashed line.
Utility functions for working with shapes.
@ NOT_USED
the 3d code uses this value
Definition typeinfo.h:79