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 =
90 [&]( const SEG& aLine )
91 {
92 if( !haveSnapLine || !aLine.ApproxCollinear( *m_snapLine, 1 ) )
93 {
94 gal.DrawLine( aLine.A, aLine.B );
95 }
96 };
97
98 // Draw all the items
99 for( const DRAWABLE_INFO& drawable : m_drawables )
100 {
101 gal.SetStrokeColor( drawable.IsPersistent ? m_persistentColor : m_color );
102 gal.SetLineWidth( drawable.LineWidth / gal.GetWorldScale() );
103
104 std::visit(
105 [&]( const auto& visited )
106 {
107 using ItemType = std::decay_t<decltype( visited )>;
108
109 if constexpr( std::is_same_v<ItemType, LINE> )
110 {
111 // Extend the segment to the viewport boundary
112 std::optional<SEG> segToBoundary =
113 KIGEOM::ClipLineToBox( visited, viewport );
114
115 if( segToBoundary )
116 {
117 drawLineIfNotAlsoSnapLine( *segToBoundary );
118 }
119 }
120 else if constexpr( std::is_same_v<ItemType, HALF_LINE> )
121 {
122 // Extend the ray to the viewport boundary
123 std::optional<SEG> segToBoundary =
124 KIGEOM::ClipHalfLineToBox( visited, viewport );
125
126 if( segToBoundary )
127 {
128 drawLineIfNotAlsoSnapLine( *segToBoundary );
129 }
130 }
131 else if constexpr( std::is_same_v<ItemType, SEG> )
132 {
133 drawLineIfNotAlsoSnapLine( visited );
134 }
135 else if constexpr( std::is_same_v<ItemType, CIRCLE> )
136 {
137 gal.DrawCircle( visited.Center, visited.Radius );
138 }
139 else if constexpr( std::is_same_v<ItemType, SHAPE_ARC> )
140 {
141 gal.DrawArc( visited.GetCenter(), visited.GetRadius(),
142 visited.GetStartAngle(), visited.GetCentralAngle() );
143 }
144 else if constexpr( std::is_same_v<ItemType, VECTOR2I> )
145 {
146 KIGFX::DrawCross( gal, visited, aView->ToWorld( 16 ) );
147 }
148 },
149 drawable.Item );
150 }
151
152 for( const SNAP_GUIDE& guide : m_snapGuides )
153 {
154 const SEG& segment = guide.Segment;
155
156 if( segment.A == segment.B )
157 continue;
158
159 std::optional<SEG> clipped = KIGEOM::ClipLineToBox( LINE( segment ), viewport );
160
161 if( !clipped )
162 continue;
163
164 gal.SetStrokeColor( guide.Color );
165 gal.SetLineWidth( guide.LineWidth );
166 gal.DrawLine( clipped->A, clipped->B );
167 }
168
169 if( haveSnapLine )
170 {
172 gal.SetLineWidth( 2 );
173
174 const int dashSizeBasis = aView->ToWorld( 12 );
175 const int snapOriginMarkerSize = aView->ToWorld( 16 );
176
177 // Avoid clash with the snap marker if very close
178 const int omitStartMarkerIfWithinLength = aView->ToWorld( 8 );
179
180 // The line itself
181 KIGFX::DrawDashedLine( gal, *m_snapLine, dashSizeBasis );
182
183 // The line origin marker if the line is long enough
184 if( m_snapLine->A.Distance( m_snapLine->B ) > omitStartMarkerIfWithinLength )
185 {
186 KIGFX::DrawCross( gal, m_snapLine->A, snapOriginMarkerSize );
187 gal.DrawCircle( m_snapLine->A, snapOriginMarkerSize / 2 );
188 }
189 }
190}
191
192
193std::vector<int> CONSTRUCTION_GEOM::ViewGetLayers() const
194{
195 // Don't use LAYER_GP_OVERLAY, we need the construction geometry to be visible
196 // on top of the axis cross
197 std::vector<int> layers{ LAYER_UI_START };
198 return layers;
199}
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