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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <layer_ids.h>
23#include <utility>
25#include <geometry/line.h>
28#include <view/view.h>
29
30using namespace KIGFX;
31
32
34 EDA_ITEM( nullptr, NOT_USED ), // Never added to a BOARD/SCHEMATIC so it needs no type
36{
37}
38
39
40void CONSTRUCTION_GEOM::AddDrawable( const DRAWABLE& aItem, bool aPersistent, int aLineWidth )
41{
42 m_drawables.push_back( { aItem, aPersistent, aLineWidth } );
43}
44
45
46void CONSTRUCTION_GEOM::SetSnapGuides( std::vector<SNAP_GUIDE> aGuides )
47{
48 m_snapGuides = std::move( aGuides );
49}
50
51
56
57
59{
60 // We could be a bit more circumspect here, but much of the time the
61 // extended lines go across the whole screen anyway
62 BOX2I bbox;
63 bbox.SetMaximum();
64 return bbox;
65}
66
67
68void CONSTRUCTION_GEOM::ViewDraw( int aLayer, VIEW* aView ) const
69{
70 GAL& gal = *aView->GetGAL();
71
72 gal.SetIsFill( false );
73 gal.SetIsStroke( true );
74 gal.SetLineWidth( 1 );
75
76 const BOX2I viewport = BOX2ISafe( aView->GetViewport() );
77
78 // Prevents extremely short snap lines from inhibiting drawing
79 // These can happen due to rounding in intersections, etc.
80 // (usually it is length 1 IU)
81 const int minSnapLineLength = 10;
82 const bool haveSnapLine = m_snapLine && m_snapLine->Length() >= minSnapLineLength;
83
84 // Avoid fighting with the snap line
85 const auto drawLineIfNotAlsoSnapLine =
86 [&]( const SEG& aLine )
87 {
88 if( !haveSnapLine || !aLine.ApproxCollinear( *m_snapLine, 1 ) )
89 {
90 gal.DrawLine( aLine.A, aLine.B );
91 }
92 };
93
94 // Draw all the items
95 for( const DRAWABLE_INFO& drawable : m_drawables )
96 {
97 gal.SetStrokeColor( drawable.IsPersistent ? m_persistentColor : m_color );
98 gal.SetLineWidth( drawable.LineWidth / gal.GetWorldScale() );
99
100 std::visit(
101 [&]( const auto& visited )
102 {
103 using ItemType = std::decay_t<decltype( visited )>;
104
105 if constexpr( std::is_same_v<ItemType, LINE> )
106 {
107 // Extend the segment to the viewport boundary
108 std::optional<SEG> segToBoundary =
109 KIGEOM::ClipLineToBox( visited, viewport );
110
111 if( segToBoundary )
112 {
113 drawLineIfNotAlsoSnapLine( *segToBoundary );
114 }
115 }
116 else if constexpr( std::is_same_v<ItemType, HALF_LINE> )
117 {
118 // Extend the ray to the viewport boundary
119 std::optional<SEG> segToBoundary =
120 KIGEOM::ClipHalfLineToBox( visited, viewport );
121
122 if( segToBoundary )
123 {
124 drawLineIfNotAlsoSnapLine( *segToBoundary );
125 }
126 }
127 else if constexpr( std::is_same_v<ItemType, SEG> )
128 {
129 drawLineIfNotAlsoSnapLine( visited );
130 }
131 else if constexpr( std::is_same_v<ItemType, CIRCLE> )
132 {
133 gal.DrawCircle( visited.Center, visited.Radius );
134 }
135 else if constexpr( std::is_same_v<ItemType, SHAPE_ARC> )
136 {
137 gal.DrawArc( visited.GetCenter(), visited.GetRadius(),
138 visited.GetStartAngle(), visited.GetCentralAngle() );
139 }
140 else if constexpr( std::is_same_v<ItemType, VECTOR2I> )
141 {
142 KIGFX::DrawCross( gal, visited, aView->ToWorld( 16 ) );
143 }
144 },
145 drawable.Item );
146 }
147
148 for( const SNAP_GUIDE& guide : m_snapGuides )
149 {
150 const SEG& segment = guide.Segment;
151 const int dashSize = aView->ToWorld( 8 );
152
153 if( segment.A == segment.B )
154 continue;
155
156 std::optional<SEG> clipped = KIGEOM::ClipLineToBox( LINE( segment ), viewport );
157
158 if( !clipped )
159 continue;
160
161 gal.SetStrokeColor( guide.Color );
162 gal.SetLineWidth( guide.LineWidth );
163 KIGFX::DrawDashedLine( gal, *clipped, dashSize );
164 }
165
166 if( haveSnapLine )
167 {
169 gal.SetLineWidth( 2 );
170
171 const int dashSizeBasis = aView->ToWorld( 12 );
172 const int snapOriginMarkerSize = aView->ToWorld( 16 );
173
174 // Avoid clash with the snap marker if very close
175 const int omitStartMarkerIfWithinLength = aView->ToWorld( 8 );
176
177 // The line itself
178 KIGFX::DrawDashedLine( gal, *m_snapLine, dashSizeBasis );
179
180 // The line origin marker if the line is long enough
181 if( m_snapLine->A.Distance( m_snapLine->B ) > omitStartMarkerIfWithinLength )
182 {
183 KIGFX::DrawCross( gal, m_snapLine->A, snapOriginMarkerSize );
184 gal.DrawCircle( m_snapLine->A, snapOriginMarkerSize / 2 );
185 }
186 }
187}
188
189
190std::vector<int> CONSTRUCTION_GEOM::ViewGetLayers() const
191{
192 // Don't use LAYER_GP_OVERLAY, we need the construction geometry to be visible
193 // on top of the axis cross
194 std::vector<int> layers{ LAYER_UI_START };
195 return layers;
196}
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:925
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr void SetMaximum()
Definition box2.h:76
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
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:63
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition view.cpp:597
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
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:534
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
@ WHITE
Definition color4d.h:44
Utility functions for drawing compound items (i.e.
@ LAYER_UI_START
Definition layer_ids.h:355
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:29
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:72