KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_log_player.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) 2020-2021 KiCad Developers.
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#include "pns_log_file.h"
26#include "pns_log_player.h"
27
29
30#define PNSLOGINFO PNS::DEBUG_DECORATOR::SRC_LOCATION_INFO( __FILE__, __FUNCTION__, __LINE__ )
31
32using namespace PNS;
33
35{
37}
38
39
41{
43 delete m_debugDecorator;
44}
45
47{
50 m_router.reset( new ROUTER );
51 m_iface->SetBoard( m_board.get() );
52 m_router->SetInterface( m_iface.get() );
53 m_router->ClearWorld();
54 m_router->SyncWorld();
55 m_router->LoadSettings( new PNS::ROUTING_SETTINGS( nullptr, "" ) );
56 m_router->Settings().SetMode( PNS::RM_Walkaround );
57 m_router->Sizes().SetTrackWidth( 250000 );
58
61 m_iface->SetDebugDecorator( m_debugDecorator );
62}
63
64
66{
68 std::vector<PNS::ITEM*> added, removed, heads;
69 m_router->GetUpdatedItems( removed, added, heads );
70
71 //printf("a %d r %d\n", added.size(), removed.size() );
72 for( auto item : removed )
73 {
74 if( item->Parent() )
75 {
76 state.m_removedIds.insert( item->Parent()->m_Uuid );
77 }
78 }
79
80 for( auto item : added )
81 {
82 state.m_addedItems.push_back( item );
83 }
84
85 // fixme: update the state with the head trace (not supported in current testsuite)
86
87 return state;
88}
89
90void PNS_LOG_PLAYER::ReplayLog( PNS_LOG_FILE* aLog, int aStartEventIndex, int aFrom, int aTo,
91 bool aUpdateExpectedResult )
92{
93 m_board = aLog->GetBoard();
94
96
97 m_router->LoadSettings( aLog->GetRoutingSettings() );
98
99 int eventIdx = 0;
100 int totalEvents = aLog->Events().size();
101
102 for( auto evt : aLog->Events() )
103 {
104 if( eventIdx < aFrom || ( aTo >= 0 && eventIdx > aTo ) )
105 continue;
106
107 auto item = aLog->ItemById( evt );
108 ITEM* ritem = item ? m_router->GetWorld()->FindItemByParent( item ) : nullptr;
109 int routingLayer = ritem ? ritem->Layers().Start() : F_Cu;
110
111 eventIdx++;
112
113 switch( evt.type )
114 {
116 {
117 PNS::SIZES_SETTINGS sizes( m_router->Sizes() );
118 m_iface->SetStartLayer( routingLayer );
119 m_iface->ImportSizes( sizes, ritem, nullptr );
120 m_router->UpdateSizes( sizes );
121
122 m_debugDecorator->NewStage( "route-start", 0, PNSLOGINFO );
123 m_viewTracker->SetStage( m_debugDecorator->GetStageCount() - 1 );
124
125 auto msg = wxString::Format( "event [%d/%d]: route-start (%d, %d)", eventIdx,
126 totalEvents, evt.p.x, evt.p.y );
127
129 m_reporter->Report( msg );
130
131 m_router->StartRouting( evt.p, ritem, routingLayer );
132 break;
133 }
134
136 {
137 PNS::SIZES_SETTINGS sizes( m_router->Sizes() );
138 m_iface->SetStartLayer( routingLayer );
139 m_iface->ImportSizes( sizes, ritem, nullptr );
140 m_router->UpdateSizes( sizes );
141
142 m_debugDecorator->NewStage( "drag-start", 0, PNSLOGINFO );
143 m_viewTracker->SetStage( m_debugDecorator->GetStageCount() - 1 );
144
145 auto msg = wxString::Format( "event [%d/%d]: drag-start (%d, %d)", eventIdx,
146 totalEvents, evt.p.x, evt.p.y );
147
149 m_reporter->Report( msg );
150
151 bool rv = m_router->StartDragging( evt.p, ritem, 0 );
152 break;
153 }
154
155 case LOGGER::EVT_FIX:
156 {
158 m_viewTracker->SetStage( m_debugDecorator->GetStageCount() - 1 );
159 m_debugDecorator->Message( wxString::Format( "fix (%d, %d)", evt.p.x, evt.p.y ) );
160 bool rv = m_router->FixRoute( evt.p, ritem, false, false );
161 printf( " fix -> (%d, %d) ret %d\n", evt.p.x, evt.p.y, rv ? 1 : 0 );
162 break;
163 }
164
166 {
167 m_debugDecorator->NewStage( "unfix", 0, PNSLOGINFO );
168 m_viewTracker->SetStage( m_debugDecorator->GetStageCount() - 1 );
169 m_debugDecorator->Message( wxString::Format( "unfix (%d, %d)", evt.p.x, evt.p.y ) );
170 printf( " unfix\n" );
171 m_router->UndoLastSegment();
172 break;
173 }
174
175 case LOGGER::EVT_MOVE:
176 {
177 m_debugDecorator->NewStage( "move", 0, PNSLOGINFO );
178 m_viewTracker->SetStage( m_debugDecorator->GetStageCount() - 1 );
179
180 auto msg = wxString::Format( "event [%d/%d]: move (%d, %d)", eventIdx, totalEvents, evt.p.x, evt.p.y );
181
183 m_reporter->Report( msg );
184
185 bool ret = m_router->Move( evt.p, ritem );
187 break;
188 }
189
191 {
192 m_debugDecorator->NewStage( "toggle-via", 0, PNSLOGINFO );
193
194 auto msg = wxString::Format( "event [%d/%d]: toggle-via", eventIdx, totalEvents );
195
197 m_reporter->Report( msg );
198
199 m_viewTracker->SetStage( m_debugDecorator->GetStageCount() - 1 );
200 m_router->ToggleViaPlacement();
201 break;
202 }
203
204 default: break;
205 }
206
207 PNS::NODE* node = nullptr;
208
209#if 0
210 if( m_router->GetState() == PNS::ROUTER::ROUTE_TRACK )
211 {
212 m_debugDecorator->BeginGroup( "current route", 0 );
213
214 auto traces = m_router->Placer()->Traces();
215
216 for( const auto& t : traces.CItems() )
217 {
218 const LINE *l = static_cast<LINE*>(t.item);
219 const auto& sh = l->CLine();
220
221 m_debugDecorator->AddItem( l, YELLOW, 0, wxT( "line seg" ) );
222 }
223
225
226 node = m_router->Placer()->CurrentNode( true );
227 }
228 else if( m_router->GetState() == PNS::ROUTER::DRAG_SEGMENT )
229 {
230 node = m_router->GetDragger()->CurrentNode();
231 }
232 if( !node )
233 return;
234
235 NODE::ITEM_VECTOR removed, added;
236
237 node->GetUpdatedItems( removed, added );
238
239 if( ! added.empty() )
240 {
241 bool first = true;
242 m_debugDecorator->BeginGroup( wxT( "node-added-items" ), 0 );
243
244 for( auto t : added )
245 {
246 m_debugDecorator->AddItem( t, MAGENTA, 0, wxT( "seg" ) );
247 }
248
250 }
251#endif
252 }
253
254 wxASSERT_MSG( m_router->Mode() == aLog->GetMode(), "didn't set the router mode correctly?" );
255
256 if( aUpdateExpectedResult )
257 {
258 std::vector<PNS::ITEM*> added, removed, heads;
259 m_router->GetUpdatedItems( removed, added, heads );
260
261 std::set<KIID> removedKIIDs;
262
263 for( auto item : removed )
264 {
265 wxASSERT_MSG( item->Parent() != nullptr, "removed an item with no parent uuid?" );
266
267 if( item->Parent() )
268 removedKIIDs.insert( item->Parent()->m_Uuid );
269 }
270
271 std::vector<std::unique_ptr<PNS::ITEM>> myOwnedItems;
272 PNS_LOG_FILE::COMMIT_STATE routerCommitState;
273 routerCommitState.m_addedItems = added;
274 routerCommitState.m_removedIds = removedKIIDs;
275 routerCommitState.m_heads = heads;
276
277 for( PNS::ITEM* head : heads )
278 myOwnedItems.emplace_back( head );
279
280 aLog->SetExpectedResult( routerCommitState, std::move( myOwnedItems ) );
281
282 int test = 0;
283 }
284}
285
286
288{
289 auto cstate = GetRouterUpdatedItems();
290
291 printf("Comparing %zu added/%zu removed items\n", cstate.m_addedItems.size(), cstate.m_removedIds.size() );
292 return cstate.Compare( aLog->GetExpectedResult() );
293}
294
295
297 m_viewTracker( aViewTracker )
298{
299}
300
302{
303}
304
306{
307 //printf("DBG hide %p\n", aItem);
308 m_viewTracker->HideItem( aItem );
309}
310
311void PNS_LOG_PLAYER_KICAD_IFACE::DisplayItem( const PNS::ITEM* aItem, int aClearance, bool aEdit,
312 int aFlags )
313{
314 //printf("DBG disp %p\n", aItem);
315 m_viewTracker->DisplayItem( aItem );
316}
317
318
320{
321 if( aNet )
322 return static_cast<NETINFO_ITEM*>( aNet )->GetNetCode();
323 else
324 return -1;
325}
326
327
329{
330 if( aNet )
331 return static_cast<NETINFO_ITEM*>( aNet )->GetNetname();
332 else
333 return wxEmptyString;
334}
335
336
338{
339}
340
342{
343}
344
346{
347 m_currentStage = aStage;
349}
350
352{
353 ENTRY ent;
354 ent.isHideOp = true;
355 ent.item = aItem;
356 m_vitems[m_currentStage].push_back( ent );
357}
358
360{
361 ENTRY ent;
362 ent.isHideOp = false;
363 ent.item = aItem->Clone();
364 m_vitems[m_currentStage].push_back( ent );
365 //printf("DBG disp cur %d cnt %d\n", m_currentStage, m_vitems[m_currentStage].size() );
366}
367
int Start() const
Definition: pns_layerset.h:82
Handle the data for a net.
Definition: netinfo.h:56
static REPORTER & GetInstance()
Definition: reporter.cpp:119
Base class for PNS router board items.
Definition: pns_item.h:97
virtual ITEM * Clone() const =0
Return a deep copy of the item.
const LAYER_RANGE & Layers() const
Definition: pns_item.h:195
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:61
const SHAPE_LINE_CHAIN & CLine() const
Definition: pns_line.h:136
@ EVT_START_ROUTE
Definition: pns_logger.h:47
@ EVT_START_DRAG
Definition: pns_logger.h:48
@ EVT_TOGGLE_VIA
Definition: pns_logger.h:52
Keep the router "world" - i.e.
Definition: pns_node.h:207
std::vector< ITEM * > ITEM_VECTOR
Definition: pns_node.h:218
void GetUpdatedItems(ITEM_VECTOR &aRemoved, ITEM_VECTOR &aAdded)
Return the list of items removed and added in this branch with respect to the root branch.
Definition: pns_node.cpp:1410
Contain all persistent settings of the router, such as the mode, optimization effort,...
BOARD_CONNECTED_ITEM * ItemById(const PNS::LOGGER::EVENT_ENTRY &evt)
void SetExpectedResult(const COMMIT_STATE &aCommitState, std::vector< std::unique_ptr< PNS::ITEM > > aParsedItems)
Definition: pns_log_file.h:89
const COMMIT_STATE & GetExpectedResult() const
Definition: pns_log_file.h:87
PNS::ROUTING_SETTINGS * GetRoutingSettings() const
Definition: pns_log_file.h:85
PNS::ROUTER_MODE GetMode() const
Definition: pns_log_file.h:96
std::vector< PNS::LOGGER::EVENT_ENTRY > & Events()
Definition: pns_log_file.h:80
std::shared_ptr< BOARD > GetBoard() const
Definition: pns_log_file.h:83
int GetNetCode(PNS::NET_HANDLE aNet) const override
PNS_LOG_PLAYER_KICAD_IFACE(PNS_LOG_VIEW_TRACKER *aViewTracker)
PNS_LOG_VIEW_TRACKER * m_viewTracker
void HideItem(PNS::ITEM *aItem) override
void DisplayItem(const PNS::ITEM *aItem, int aClearance, bool aEdit=false, int aFlags=0) override
wxString GetNetName(PNS::NET_HANDLE aNet) const override
PNS_TEST_DEBUG_DECORATOR * m_debugDecorator
std::shared_ptr< BOARD > m_board
const PNS_LOG_FILE::COMMIT_STATE GetRouterUpdatedItems()
REPORTER * m_reporter
bool CompareResults(PNS_LOG_FILE *aLog)
std::unique_ptr< PNS::ROUTER > m_router
void ReplayLog(PNS_LOG_FILE *aLog, int aStartEventIndex=0, int aFrom=0, int aTo=-1, bool aUpdateExpectedResult=false)
std::shared_ptr< PNS_LOG_VIEW_TRACKER > m_viewTracker
std::unique_ptr< PNS_LOG_PLAYER_KICAD_IFACE > m_iface
void SetReporter(REPORTER *aReporter)
std::map< int, VIEW_ENTRIES > m_vitems
void DisplayItem(const PNS::ITEM *aItem)
void HideItem(PNS::ITEM *aItem)
std::vector< ENTRY > VIEW_ENTRIES
void SetStage(int aStage)
virtual void AddItem(const PNS::ITEM *aItem, const KIGFX::COLOR4D &aColor, int aOverrideWidth=0, const wxString &aName=wxT(""), const SRC_LOCATION_INFO &aSrcLoc=SRC_LOCATION_INFO()) override
virtual void Message(const wxString &msg, const SRC_LOCATION_INFO &aSrcLoc=SRC_LOCATION_INFO()) override
virtual void BeginGroup(const wxString &name, int aLevel=0, const SRC_LOCATION_INFO &aSrcLoc=SRC_LOCATION_INFO()) override
virtual void EndGroup(const SRC_LOCATION_INFO &aSrcLoc=SRC_LOCATION_INFO()) override
virtual void NewStage(const wxString &name, int iter, const SRC_LOCATION_INFO &aSrcLoc=SRC_LOCATION_INFO()) override
virtual void Clear() override
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
@ MAGENTA
Definition: color4d.h:60
@ YELLOW
Definition: color4d.h:67
@ F_Cu
Definition: layer_ids.h:65
Push and Shove diff pair dimensions (gap) settings dialog.
@ RM_Walkaround
Only walk around.
void * NET_HANDLE
Definition: pns_item.h:54
#define PNSLOGINFO
std::set< KIID > m_removedIds
Definition: pns_log_file.h:65
std::vector< PNS::ITEM * > m_addedItems
Definition: pns_log_file.h:66
std::vector< PNS::ITEM * > m_heads
Definition: pns_log_file.h:67