KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_log_viewer_frame.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.
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
20
21// WARNING - this Tom's crappy PNS hack tool code. Please don't complain about its quality
22// (unless you want to improve it).
23#include <string>
24
25#include <confirm.h>
26#include <common.h>
27#include <wx/clipbrd.h>
28#include <pgm_base.h>
29#include <core/profile.h>
30#include <reporter.h>
31#include <trace_helpers.h>
32#include <view/view_overlay.h>
33#include <view/view_controls.h>
35#include <kiplatform/ui.h>
36
37
38#include "label_manager.h"
39
40#include "pns_log_file.h"
41#include "pns_log_player.h"
43
45#include "router/pns_utils.h"
47
49
50class WX_SHAPE_TREE_ITEM_DATA : public wxClientData
51{
52public:
53 WX_SHAPE_TREE_ITEM_DATA( PNS_DEBUG_SHAPE* item, int level = 0 ) : m_item( item ), m_level( level ) {};
54
57};
58
59
64
65
66void PNS_LOG_VIEWER_OVERLAY::AnnotatedPolyset( const SHAPE_POLY_SET& aPolyset, std::string aName,
67 bool aShowVertexNumbers )
68{
69 for( int i = 0; i < aPolyset.OutlineCount(); i++ )
70 {
71 if( i == 0 && !aName.empty() )
72 AnnotatedPolyline( aPolyset.COutline( i ), aName );
73 else
74 AnnotatedPolyline( aPolyset.COutline( i ), "" );
75
76 for( int j = 0; j < aPolyset.HoleCount( i ); j++ )
77 AnnotatedPolyline( aPolyset.CHole( i, j ), "" );
78 }
79}
80
81
83 bool aShowVertexNumbers )
84{
85 Polyline( aL );
86
87 if( name.length() > 0 && aL.PointCount() > 0 )
89
90 if( aShowVertexNumbers )
91 {
92 for( int i = 0; i < aL.PointCount(); i++ )
93 m_labelMgr->Add( aL.CPoint(i), wxString::Format("%d", i ), GetStrokeColor() );
94 }
95}
96
101
102
103void PNS_LOG_VIEWER_OVERLAY::AnnotatedPoint( const VECTOR2I p, int size, std::string name, bool aShowVertexNumbers )
104{
105 Line( p + VECTOR2D( size, size ), p - VECTOR2D( size, size ) );
106 Line( p + VECTOR2D( -size, size ), p - VECTOR2D( -size, size ) );
107
108 if( name.length() > 0 )
109 m_labelMgr->Add( p, name, GetStrokeColor() );
110}
111
112
114{
115 double radius = arc.GetRadius();
116 EDA_ANGLE start_angle = arc.GetStartAngle();
117 EDA_ANGLE angle = arc.GetCentralAngle();
118
120 KIGFX::VIEW_OVERLAY::Arc( arc.GetCenter(), radius, start_angle, start_angle + angle );
121
123 COLOR4D lightStrokeCol = prevStrokeCol.WithAlpha(0.5);
124 KIGFX::VIEW_OVERLAY::SetStrokeColor( lightStrokeCol );
125
127 KIGFX::VIEW_OVERLAY::Arc( arc.GetCenter(), radius, start_angle, start_angle + angle );
128
130}
131
133{
134 m_labelMgr->Redraw( this );
135}
136
137
140{
141 LoadSettings();
143
145 m_galPanel->SetParent( m_mainSplitter );
146 m_mainSplitter->SplitHorizontally( m_galPanel.get(), m_panelProps );
147
148 Layout();
149
150 Show( true );
151 Maximize();
152 Raise();
153
154 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>(
155 m_galPanel->GetView()->GetPainter()->GetSettings() );
156
157
159
161
162 double opacity = 0.5;
163
164 opts.m_TrackOpacity = opacity;
165 opts.m_ViaOpacity = opacity;
166 opts.m_PadOpacity = opacity;
167 opts.m_ZoneOpacity = opacity;
168
169 settings->LoadDisplayOptions( opts );
170
171
172 m_listPopupMenu = new wxMenu( wxT( "" ) );
173 m_listPopupMenu->Append( ID_LIST_COPY, wxT( "Copy selected geometry" ), wxT( "" ),
174 wxITEM_NORMAL );
175 m_listPopupMenu->Append( ID_LIST_SHOW_ALL, wxT( "Show all" ), wxT( "" ), wxITEM_NORMAL );
176 m_listPopupMenu->Append( ID_LIST_SHOW_NONE, wxT( "Show none" ), wxT( "" ), wxITEM_NORMAL );
177 m_listPopupMenu->Append( ID_LIST_DISPLAY_LINE, wxT( "Go to line in IDE" ), wxT( "" ), wxITEM_NORMAL );
178
179 m_itemList->Connect( m_itemList->GetId(), wxEVT_TREELIST_ITEM_CONTEXT_MENU,
180 wxMouseEventHandler( PNS_LOG_VIEWER_FRAME::onListRightClick ), nullptr,
181 this );
182 //m_itemList->Connect(m_itemList->GetId(),wxEVT_LISTBOX,wxCommandEventHandler(PNS_LOG_VIEWER_FRAME::onListSelect),nullptr,this);
183 m_itemList->Connect( m_itemList->GetId(), wxEVT_TREELIST_SELECTION_CHANGED,
184 wxTreeListEventHandler( PNS_LOG_VIEWER_FRAME::onListSelect ),
185 nullptr, this );
186 m_itemList->Connect( m_itemList->GetId(), wxEVT_TREELIST_ITEM_CHECKED,
187 wxTreeListEventHandler( PNS_LOG_VIEWER_FRAME::onListChecked ),
188 nullptr, this );
189
190 m_itemList->AppendColumn( "Type" );
191 m_itemList->AppendColumn( "Value" );
192 m_itemList->AppendColumn( "File" );
193 m_itemList->AppendColumn( "Method" );
194 m_itemList->AppendColumn( "Line" );
195 m_itemList->AppendColumn( "VCount" );
196 m_itemList->AppendColumn( "Non-45" );
197
198 m_overlay.reset( new PNS_LOG_VIEWER_OVERLAY ( m_galPanel->GetGAL() ) );
199 m_galPanel->GetView()->Add( m_overlay.get() );
200 m_galPanel->GetView()->SetVisible( m_overlay.get() );
201 m_galPanel->GetView()->SetLayerVisible( LAYER_GP_OVERLAY );
202 m_galPanel->GetViewControls()->EnableCursorWarping(false);
203
204 for( PCB_LAYER_ID layer : LSET::AllNonCuMask().Seq() )
205 m_galPanel->GetView()->SetLayerVisible( layer, false );
206}
207
208
210{
211 m_board = nullptr;
212 m_logPlayer = nullptr;
213 m_logFile = nullptr;
214 m_overlay = nullptr;
215}
216
217
222
223
225{
226 PNS_TEST_DEBUG_DECORATOR* dbgd = m_logPlayer->GetDebugDecorator();
227 int count = dbgd->GetStageCount();
228
229 int iter = m_rewindIter;
230
231 if( count <= 0 )
232 return nullptr;
233
234 if( iter < 0 )
235 iter = 0;
236
237 if( iter >= count )
238 iter = count - 1;
239
240 return dbgd->GetStage( iter );
241}
242
243
244void PNS_LOG_VIEWER_FRAME::drawSimpleShape( SHAPE* aShape, bool aIsSelected, const std::string& aName )
245{
246 switch( aShape->Type() )
247 {
248 case SH_CIRCLE:
249 {
250 auto cir = static_cast<SHAPE_CIRCLE*>( aShape );
251 m_overlay->Circle( cir->GetCenter(), cir->GetRadius() );
252
253 break;
254 }
255 case SH_SEGMENT:
256 {
257 auto seg = static_cast<SHAPE_SEGMENT*>( aShape );
258 m_overlay->Line( seg->GetSeg().A, seg->GetSeg().B );
259
260 break;
261 }
262 case SH_RECT:
263 {
264 auto rect = static_cast<SHAPE_RECT*>( aShape );
265 m_overlay->Rectangle( rect->GetPosition(), rect->GetPosition() + rect->GetSize() );
266
267 break;
268 }
269 case SH_LINE_CHAIN:
270 {
271 auto lc = static_cast<SHAPE_LINE_CHAIN*>( aShape );
272 m_overlay->AnnotatedPolyline( *lc, aName, m_showVertices || aIsSelected );
273
274 break;
275 }
276 default: break;
277 }
278}
279
280
282{
283 if( !m_logPlayer )
284 return;
285
287
288 if( !st )
289 return;
290
291 m_overlay.reset( new PNS_LOG_VIEWER_OVERLAY ( m_galPanel->GetGAL() ) );
292 m_galPanel->GetView()->Add( m_overlay.get() );
293 //m_galPanel->GetGAL()->EnableDepthTest( false );
294
295 auto drawShapes = [&]( PNS_DEBUG_SHAPE* ent ) -> bool
296 {
297 bool isEnabled = ent->IsVisible();
298 bool isSelected = ent->IsSelected();
299
300 if( m_searchString.Length() > 0 )
301 isEnabled = ent->m_filterMatch;
302
303 if( !isEnabled && !isSelected )
304 return true;
305
306 for( auto& sh : ent->m_shapes )
307 {
308 COLOR4D color = ent->m_color;
309 int lineWidth = ent->m_width;
310
311 m_overlay->SetIsStroke( true );
312 m_overlay->SetIsFill( false );
313
314 if( isSelected )
315 {
316 color.Brighten( 0.5 );
317 }
318
319 color.a = 1.0;
320
321 m_overlay->SetStrokeColor( color );
322 m_overlay->SetLineWidth( m_showThinLines ? 10000 : ent->m_width );
323
324 if( sh->Type() == SH_COMPOUND )
325 {
326 auto cmpnd = static_cast<SHAPE_COMPOUND*>( sh );
327
328 for( auto subshape : cmpnd->Shapes() )
329 {
330 drawSimpleShape( subshape, isSelected, ent->m_name.ToStdString() );
331 }
332 }
333 else
334 {
335 drawSimpleShape( sh, isSelected, ent->m_name.ToStdString() );
336 }
337 }
338
339 return true;
340 };
341
343
344 m_overlay->DrawAnnotations();
345
346 m_galPanel->GetView()->MarkDirty();
347 m_galPanel->GetParent()->Refresh();
348}
349
350
351void PNS_LOG_VIEWER_FRAME::LoadLogFile( const wxString& aFile )
352{
353 std::unique_ptr<PNS_LOG_FILE> logFile( new PNS_LOG_FILE );
354
355 wxFileName logFn( aFile );
356 logFn.MakeAbsolute();
357
358 if( logFile->Load( logFn, m_reporter.get() ) )
359 {
360 SetLogFile( logFile.release() );
361 m_mruPath = logFn.GetPath();
362 }
363}
364
365
367{
368 m_viewerIface = std::make_shared<PNS_VIEWER_IFACE>( m_board );
369}
370
371
373{
374 m_logPlayer.reset( new PNS_LOG_PLAYER );
375 m_board = nullptr;
376 m_logFile.reset( aLog );
377
378 SetBoard( m_logFile->GetBoard() );
380 m_logPlayer->ReplayLog( m_logFile.get(), 0, 0, -1, true );
381
382 auto dbgd = m_logPlayer->GetDebugDecorator();
383 int n_stages = dbgd->GetStageCount();
384 m_rewindSlider->SetMax( n_stages - 1 );
385 m_rewindSlider->SetValue( n_stages - 1 );
386 m_rewindIter = n_stages - 1;
387
388 auto extents = m_board->GetBoundingBox();
389
390
391 BOX2D bbd;
392 bbd.SetOrigin( extents.GetOrigin() );
393 bbd.SetWidth( extents.GetWidth() );
394 bbd.SetHeight( extents.GetHeight() );
395 bbd.Inflate( std::min( bbd.GetWidth(), bbd.GetHeight() ) / 5 );
396
397 m_galPanel->GetView()->SetViewport( bbd );
398
402
403}
404
405
406void PNS_LOG_VIEWER_FRAME::SetBoard2( std::shared_ptr<BOARD> aBoard )
407{
408 SetBoard( aBoard );
410 auto extents = m_board->GetBoundingBox();
411
412 BOX2D bbd;
413 bbd.SetOrigin( extents.GetOrigin() );
414 bbd.SetWidth( extents.GetWidth() );
415 bbd.SetHeight( extents.GetHeight() );
416 bbd.Inflate( std::min( bbd.GetWidth(), bbd.GetHeight() ) / 5 );
417
418 m_galPanel->GetView()->SetViewport( bbd );
419 m_galPanel->GetView()->Add( m_overlay.get() );
420 m_galPanel->GetView()->SetVisible( m_overlay.get() );
421 m_galPanel->GetView()->SetLayerVisible( LAYER_GP_OVERLAY );
422}
423
424
425void PNS_LOG_VIEWER_FRAME::onOpen( wxCommandEvent& event )
426{
427 wxFileDialog dlg( this, "Select Log File", m_mruPath, wxEmptyString,
428 "PNS log files" + AddFileExtListToFilter( { "log" } ),
429 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
430
432
433 if( dlg.ShowModal() != wxID_CANCEL )
434 {
435 wxString logPath = dlg.GetPath();
436 LoadLogFile( logPath );
437 m_mruPath = wxFileName( logPath ).GetPath();
438 }
439}
440
441
442void PNS_LOG_VIEWER_FRAME::onSaveAs( wxCommandEvent& event )
443{
444 if( !m_logFile )
445 {
446 DisplayErrorMessage( this, wxT( "No log file Loaded!" ) );
447 return;
448 }
449
450 wxFileDialog dlg( this, "New log file", m_mruPath, wxEmptyString,
451 "PNS log files" + AddFileExtListToFilter( { "log" } ),
452 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
453
455
456 if( dlg.ShowModal() != wxID_CANCEL )
457 {
458 // Enforce the extension, wxFileDialog is inept.
459 wxFileName create_me = EnsureFileExtension( dlg.GetPath(), "log" );
460
461 wxASSERT_MSG( create_me.IsAbsolute(), wxS( "wxFileDialog returned non-absolute path" ) );
462
463 int option = SelectSingleOption( this, _( "Select test case type" ), _( "Select test case type" ),
464 { _( "Testcase (strict geometry)" ), _( "Testcase (connectivity only)" ),
465 _( "Testcase (expected failure)" ), _( "Known bug" ) } );
466
467 if ( option >= 0 )
468 {
469 m_logFile->SetTestCaseType( static_cast<PNS::LOGGER::TEST_CASE_TYPE>( option ) );
470 }
471
472 m_logFile->SaveLog( create_me, m_reporter.get() );
473 m_mruPath = create_me.GetPath();
474 }
475}
476
477
478void PNS_LOG_VIEWER_FRAME::onExit( wxCommandEvent& event )
479{
480 Close();
481}
482
483
484void PNS_LOG_VIEWER_FRAME::onListChecked( wxTreeListEvent& event )
485{
486 syncModel();
488}
489
491{
492 m_showThinLines = event.GetInt();
495}
496
497void PNS_LOG_VIEWER_FRAME::onShowRPIsChecked( wxCommandEvent& event )
498{
499 m_showRPIs = event.GetInt();
502}
503
505{
506 m_showVertices = event.GetInt();
509}
510
511
512void PNS_LOG_VIEWER_FRAME::onRewindScroll( wxScrollEvent& event )
513{
514 m_rewindIter = event.GetPosition();
518 m_rewindPos->SetValue( std::to_string( m_rewindIter ) );
519 event.Skip();
520}
521
522
523void PNS_LOG_VIEWER_FRAME::onBtnRewindLeft( wxCommandEvent& event )
524{
525 if( m_rewindIter > 0 )
526 {
527 m_rewindIter--;
531 m_rewindPos->SetValue( std::to_string( m_rewindIter ) );
532 m_rewindSlider->SetValue( m_rewindIter );
533 }
534}
535
536
537void PNS_LOG_VIEWER_FRAME::onBtnRewindRight( wxCommandEvent& event )
538{
539 auto dbgd = m_logPlayer->GetDebugDecorator();
540 int count = dbgd->GetStageCount();
541
542 if( m_rewindIter < count )
543 {
544 m_rewindIter++;
548 m_rewindPos->SetValue( std::to_string( m_rewindIter ) );
549 m_rewindSlider->SetValue( m_rewindIter );
550 }
551}
552
553void PNS_LOG_VIEWER_FRAME::onFilterText( wxCommandEvent& event )
554{
555 m_searchString = m_filterString->GetValue();
557}
558
559
560void PNS_LOG_VIEWER_FRAME::onRewindCountText( wxCommandEvent& event )
561{
562 if( !m_logPlayer )
563 return;
564
565 int val = wxAtoi( m_rewindPos->GetValue() );
566
567 auto dbgd = m_logPlayer->GetDebugDecorator();
568 int count = dbgd->GetStageCount();
569
570 if( val < 0 )
571 val = 0;
572
573 if( val >= count )
574 val = count - 1;
575
576 m_rewindIter = val;
577 m_rewindSlider->SetValue( m_rewindIter );
581
582 event.Skip();
583}
584
585
587{
588 for( wxTreeListItem item = m_itemList->GetFirstItem(); item.IsOk();
589 item = m_itemList->GetNextItem( item ) )
590 {
592 static_cast<WX_SHAPE_TREE_ITEM_DATA*>( m_itemList->GetItemData( item ) );
593
594 if( idata )
595 {
596 bool checked = m_itemList->GetCheckedState( item ) == wxCHK_CHECKED;
597 bool selected = m_itemList->IsSelected( item );
598 idata->m_item->m_checked = checked;
599 idata->m_item->m_visible = checked || selected;
600 idata->m_item->m_selected = selected;
601 }
602 }
603}
604
605
606void runCommand( const wxString& aCommand )
607{
608#ifdef __WXMSW__
609 wxShell( aCommand ); // on windows we need to launch a shell in order to run the command
610#else
611 wxExecute( aCommand );
612#endif /* __WXMSW__ */
613}
614
615
616void PNS_LOG_VIEWER_FRAME::onListRightClick( wxMouseEvent& event )
617{
618 auto sel = m_itemList->GetPopupMenuSelectionFromUser( *m_listPopupMenu );
619
620 switch( sel )
621 {
623 m_itemList->CheckItemRecursively( m_itemList->GetRootItem(), wxCHK_UNCHECKED );
624 syncModel();
626 break;
627 case ID_LIST_SHOW_ALL:
628 m_itemList->CheckItemRecursively( m_itemList->GetRootItem(), wxCHK_CHECKED );
629 syncModel();
631 break;
632 case ID_LIST_COPY:
633 {
634 wxString s;
635
637
638 if( !st )
639 return;
640
641 auto formatShapes = [&]( PNS_DEBUG_SHAPE* ent ) -> bool
642 {
643 if( ent->m_selected )
644 {
645 for( auto sh : ent->m_shapes )
646 {
647 s += "// " + ent->m_name + "\n " + sh->Format() + "; \n";
648 }
649 }
650
651 return true;
652 };
653
654 st->m_entries->IterateTree( formatShapes );
655
656 if( wxTheClipboard->Open() )
657 {
658 // This data objects are held by the clipboard,
659 // so do not delete them in the app.
660 wxTheClipboard->SetData( new wxTextDataObject( s ) );
661 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
662 wxTheClipboard->Close();
663 }
664
665 return;
666 }
668 {
669 wxVector<wxTreeListItem> selectedItems;
670
671 if( m_itemList->GetSelections( selectedItems ) == 1 )
672 {
673 wxString filename = m_itemList->GetItemText(selectedItems.back(), 2);
674 wxString line = m_itemList->GetItemText(selectedItems.back(), 4);
675
676
677 if( !filename.empty() && !line.empty() )
678 {
679 wxString filepath = m_filenameToPathMap[filename];
680
681 switch( m_ideChoice->GetCurrentSelection() )
682 {
683 case 0: runCommand( wxString::Format( "code --goto %s:%s", filepath, line ) ); return;
684 case 1: runCommand( wxString::Format( "start devenv /edit %s /command \"Gotoln %s\"", filepath, line ) ); return; // fixme
685 case 2: runCommand( wxString::Format( "clion --line %s %s", line, filepath ) ); return;
686 case 3: runCommand( wxString::Format( "emacsclient +%s %s", line, filepath ) ); return;
687 default: return;
688 }
689 }
690 }
691 break;
692 }
693 }
694 return;
695}
696
697
698void PNS_LOG_VIEWER_FRAME::onListSelect( wxTreeListEvent& event )
699{
701 static_cast<WX_SHAPE_TREE_ITEM_DATA*>( m_itemList->GetItemData( event.GetItem() ) );
702
703 if( idata )
704 {
705 m_lastSelectedItem = idata->m_item;
706 }
707
708 syncModel();
710}
711
712
713
714static bool isLine45Degree( const SHAPE_LINE_CHAIN* lc )
715{
716 for( int i = 0; i < lc->SegmentCount(); i++ )
717 {
718 const SEG& s = lc->CSegment( i );
719
720 if( lc->IsArcSegment( i ) )
721 continue;
722
723 if( s.Length() < 10 )
724 continue;
725
726 double angle = 180.0 / M_PI *
727 atan2( (double) s.B.y - (double) s.A.y,
728 (double) s.B.x - (double) s.A.x );
729
730 if( angle < 0 )
731 angle += 360.0;
732
733 double angle_a = fabs( fmod( angle, 45.0 ) );
734
735 if( angle_a > 1.0 && angle_a < 44.0 )
736 return false;
737 }
738
739 return true;
740}
741
742
744{
745
746 std::set<PNS_DEBUG_SHAPE*> processed;
747 std::deque<PNS_DEBUG_SHAPE*> q;
748
749 q.push_back(ent);
750 int total = 0;
751 while ( q.size() > 0 )
752 {
753 PNS_DEBUG_SHAPE* top = q.front();
754
755 q.pop_front();
756
757 for ( auto chld : top->m_children )
758 {
759 bool match = m_searchString.Length() == 0 ? true : false;
760 //printf("CHK %s\n", (const char *) chld->m_name.c_str() );
761 chld->m_filterMatch = false;
762 if ( chld->m_name.Contains( m_searchString ) )
763 match = true;
764 if ( chld->m_msg.Contains( m_searchString ) )
765 match = true;
766
767 if( match )
768 {
769 for ( PNS_DEBUG_SHAPE *cur = chld; cur; cur = cur->m_parent )
770 cur->m_filterMatch = match;
771 }
772
773 if( processed.find(chld) == processed.end() )
774 {
775 q.push_back( chld );
776 processed.insert( chld );
777 }
778 }
779 total++;
780 }
781
782 printf("total: %d\n", total );
783
784 return false;
785}
786
787
788void PNS_LOG_VIEWER_FRAME::buildListTree( wxTreeListItem item,
789 PNS_DEBUG_SHAPE* ent, int depth )
790{
791#ifdef EXTRA_VERBOSE
792 for( int i = 0; i < depth * 2; i++ )
793 printf( " " );
794
795 if( ent->m_msg.length() )
796 printf( "MSG: %s\n", ent->m_msg.c_str() );
797 else
798 printf( "SHAPES: %s [%d]\n", ent->m_name.c_str(), ent->m_children.size() );
799#endif
800
801 wxTreeListItem ritem;
802
803 if( !ent->m_filterMatch )
804 return;
805
806 if( ent->m_msg.length() )
807 {
808 ritem = m_itemList->AppendItem( item, "Child" );
809 m_itemList->SetItemText( ritem, 0, "Message" );
810 m_itemList->SetItemText( ritem, 1, ent->m_msg );
811 }
812 else
813 {
814 ritem = m_itemList->AppendItem( item, "Child" );
815 m_itemList->CheckItem( ritem, ent->m_selected ? wxCHK_CHECKED: wxCHK_UNCHECKED );
816
817 int n_verts = 0;
818 for(auto sh : ent->m_shapes )
819 {
820 if ( sh->Type() == SH_LINE_CHAIN )
821 {
822 n_verts += static_cast<const SHAPE_LINE_CHAIN*>( sh )->PointCount();
823 }
824 }
825 m_itemList->SetItemText( ritem, 0, wxString::Format( "Shapes [%d verts]", n_verts ) );
826 m_itemList->SetItemText( ritem, 1, ent->m_name );
827 }
828
829 wxString fullfilepath = ent->m_srcLoc.fileName;
830 wxString filename = wxFileNameFromPath( fullfilepath );
831
832 if( !filename.empty() )
833 m_filenameToPathMap.insert( { filename, fullfilepath } );
834
835 m_itemList->SetItemText( ritem, 2, filename );
836 m_itemList->SetItemText( ritem, 3, ent->m_srcLoc.funcName );
837 m_itemList->SetItemText( ritem, 4, wxString::Format("%d", ent->m_srcLoc.line ) );
838
839 int totalVC = 0, totalVCSimplified = 0;
840 bool is45Degree = true;
841
842 for( SHAPE* sh : ent->m_shapes )
843 {
844 if( sh->Type() == SH_LINE_CHAIN )
845 {
846 auto lc = static_cast<SHAPE_LINE_CHAIN*>( sh );
847
848 totalVC += lc->PointCount();
849
850 SHAPE_LINE_CHAIN simp(*lc);
851
852 simp.Simplify();
853
854 totalVCSimplified += simp.PointCount();
855
856 if( !isLine45Degree( lc ) )
857 is45Degree = false;
858 }
859 }
860
861 if( totalVC > 0 )
862 m_itemList->SetItemText( ritem, 5, wxString::Format( "%d [%d]", totalVC, totalVCSimplified ) );
863
864 if( !is45Degree )
865 m_itemList->SetItemText( ritem, 6, wxT("") );
866
867 m_itemList->SetItemData( ritem, new WX_SHAPE_TREE_ITEM_DATA( ent, depth ) );
868
869 if( m_lastSelectedItem.has_value() && m_lastSelectedItem.value() == ent )
870 {
871 m_itemList->Select( ritem );
872 m_itemList->EnsureVisible( ritem );
873 }
874
875 if( !ent->m_children.size() )
876 return;
877
878 for( auto child : ent->m_children )
879 {
880 buildListTree( ritem, child, depth + 1 );
881 }
882}
883
884
885static void expandAllChildren( wxTreeListCtrl* tree, int maxLevel = -1 )
886{
887 wxTreeListItem child = tree->GetFirstItem ();
888
889 while( child.IsOk() )
890 {
892 static_cast<WX_SHAPE_TREE_ITEM_DATA*>( tree->GetItemData( child ) );
893
894 if( maxLevel < 0 || idata->m_level <= maxLevel )
895 tree->Expand ( child );
896 else
897 tree->Collapse ( child );
898 child = tree->GetNextItem( child );
899 }
900}
901
902static void collapseAllChildren( wxTreeListCtrl* tree )
903{
904 wxTreeListItem child = tree->GetFirstItem ();
905
906 while( child.IsOk() )
907 {
908 tree->Collapse ( child );
909 child = tree->GetNextItem( child );
910 }
911}
912
913
914static void forceSelected( wxTreeListCtrl* tree, PNS_DEBUG_SHAPE* aEntry )
915{
916 wxTreeListItem child = tree->GetFirstItem ();
917
918 while( child.IsOk() )
919 {
921 static_cast<WX_SHAPE_TREE_ITEM_DATA*>( tree->GetItemData( child ) );
922
923
924 if( idata && idata->m_item == aEntry )
925 {
926 tree->Select( child );
927 tree->Expand ( child );
928 }
929 child = tree->GetNextItem( child );
930 }
931}
932
934{
935 printf("UpdateDUmp %d\n", iter );
936 if( !m_logPlayer )
937 return;
938
939 auto dbgd = m_logPlayer->GetDebugDecorator();
940 int count = dbgd->GetStageCount();
941
942 wxArrayString dumpStrings;
943
944 if( count <= 0 )
945 return;
946
947 if( iter < 0 )
948 iter = 0;
949
950 if( iter >= count )
951 iter = count - 1;
952
953 auto st = dbgd->GetStage( iter );
954
955 if( st->m_status )
956 {
957 m_algoStatus->SetLabel("OK");
958 m_algoStatus->SetForegroundColour( wxColor(*wxGREEN));
959 }
960 else
961 {
962 m_algoStatus->SetLabel("FAIL");
963 m_algoStatus->SetForegroundColour( wxColor(*wxRED));
964 }
965
966 auto rootItem = m_itemList->GetRootItem();
967
968 m_itemList->DeleteAllItems();
969 filterStringMatches( st->m_entries );
970 m_itemList->CheckItemRecursively( rootItem, wxCHK_UNCHECKED );
971 buildListTree( rootItem, st->m_entries );
972
976
977 m_itemList->Refresh();
978}
979
981{
982 auto viewTracker = m_logPlayer->GetViewTracker();
983 PNS_LOG_VIEW_TRACKER::VIEW_ENTRIES& entries = viewTracker->GetEntriesForStage( iter );
984 auto view = m_galPanel->GetView();
985 printf("DBG updatePnsPreviewItems: %zu items\n", entries.size() );
986
987 m_previewItems.reset( new KIGFX::VIEW_GROUP( m_galPanel->GetView() ) );
988 m_galPanel->GetView()->Add( m_previewItems.get() );
990 m_galPanel->GetView()->SetLayerVisible( LAYER_SELECT_OVERLAY );
991
992 if( !m_showRPIs )
993 return;
994
995 for( auto& ent : entries )
996 {
997 if ( ent.m_isHideOp )
998 {
999
1000 auto parent = ent.m_item->Parent();
1001 if( parent )
1002 {
1003
1004 view->Hide( parent );
1005 }
1006 }
1007 else
1008 {
1009 ROUTER_PREVIEW_ITEM* pitem = new ROUTER_PREVIEW_ITEM( ent.m_item, m_viewerIface.get(), view );
1010 pitem->Update( ent.m_item );
1011 m_previewItems->Add(pitem);
1012 // printf("DBG vadd %p total %d\n", pitem, m_previewItems->GetSize() );
1013 }
1014 }
1015
1016 view->SetVisible( m_previewItems.get(), true );
1017
1018 view->Update( m_previewItems.get() );
1019 printf("DBG vgrp %p total %d\n", m_previewItems.get(), m_previewItems->GetSize() );
1020
1021
1022 //view->UpdateAllItems( KIGFX::ALL );
1023}
1024
1029
1030
1031#if 0
1032
1033static BOARD* loadBoard( const std::string& filename )
1034{
1036 BOARD* brd = nullptr;
1037
1038 try
1039 {
1040 brd = pi->LoadBoard( wxString( filename.c_str() ), nullptr, nullptr );
1041 }
1042 catch( const IO_ERROR& )
1043 {
1044 return nullptr;
1045 }
1046
1047 return brd;
1048}
1049
1050
1051
1052
1053
1054int render_perftest_main_func( int argc, char* argv[] )
1055{
1056 auto frame = new PNS_LOG_VIEWER_FRAME( nullptr );
1057
1058 // drcCreateTestsProviderClearance();
1059 // drcCreateTestsProviderEdgeClearance();
1060
1061 if( argc >= 2 && std::string( argv[1] ) == "-h" )
1062 {
1063 printf( "PCB render performance test. Just renders a board without UI update overhead.\n" );
1064 return 0;
1065 }
1066
1067 if( argc < 2 )
1068 {
1069 printf( "Expected parameters: board_file\n" );
1070 return 0;
1071 }
1072
1073 PROF_TIMER cnt("load-board");
1074 std::shared_ptr<BOARD> brd ( loadBoard( argv[1] ) );
1075 cnt.Stop();
1076
1077 wxLogTrace( traceGalProfile, "%s", cnt.to_string() );
1078
1079 frame->SetBoard2( brd );
1080
1081 return 0;
1082}
1083
1084
1085static bool registered3 = UTILITY_REGISTRY::Register( {
1086 "render_perftest",
1087 "Renderer performance test",
1088 render_perftest_main_func,
1089} );
1090
1091
1092VECTOR2I NearestPointFixpt( SEG seg, const VECTOR2I& aP )
1093{
1094 VECTOR2I d = seg.B - seg.A;
1095 SEG::ecoord l_squared = d.Dot( d );
1096
1097 if( l_squared == 0 )
1098 return seg.A;
1099
1100 SEG::ecoord t = d.Dot( aP - seg.A );
1101
1102 if( t < 0 )
1103 return seg.A;
1104 else if( t > l_squared )
1105 return seg.B;
1106
1107 int xp = rescale( t, (SEG::ecoord) d.x, l_squared );
1108 int yp = rescale( t, (SEG::ecoord) d.y, l_squared );
1109
1110 return seg.A + VECTOR2I( xp, yp );
1111}
1112
1113
1114VECTOR2D NearestPointDbl( SEG seg, const VECTOR2I& aP )
1115{
1116 VECTOR2D d = seg.B - seg.A;
1117 double l_squared = d.Dot(d);
1118
1119 if( l_squared == 0 )
1120 return seg.A;
1121
1122 double t = d.Dot(VECTOR2D( aP - seg.A ) );
1123
1124 if( t < 0 )
1125 return seg.A;
1126 else if( t > l_squared )
1127 return seg.B;
1128
1129 double xp = t * d.x / l_squared;
1130 double yp = t * d.y / l_squared;
1131
1132 return VECTOR2D(seg.A) + VECTOR2D( xp, yp );
1133}
1134
1135int ttt_main_func( int argc, char* argv[] )
1136{
1137 int n = 1000000;
1138 std::vector<VECTOR2I> pts;
1139 std::vector<SEG> segs;
1140 std::vector<VECTOR2D> rv;
1141 std::vector<VECTOR2I> rvi;
1142
1143
1144 rv.resize(n);
1145 rvi.resize(n);
1146
1147 for (int i = 0; i < n ;i++)
1148 {
1149 pts.push_back(VECTOR2I(random()%100000000, random()%100000000));
1150 segs.push_back(SEG( VECTOR2I(random()%100000000, random()%100000000), VECTOR2I(random()%100000000, random()%100000000) ) );
1151 }
1152
1153 PROF_TIMER tmrFix("nearest-fixpt");
1154 for(int i = 0; i < n ; i++)
1155 {
1156 rvi[i] = NearestPointFixpt( segs[i], pts[i]);
1157 }
1158 tmrFix.Show();
1159
1160 PROF_TIMER tmrDbl("nearest-double");
1161 for(int i = 0; i < n ; i++)
1162 {
1163 rv[i] = NearestPointDbl( segs[i], pts[i]);
1164 }
1165 tmrDbl.Show();
1166 return 0;
1167}
1168
1169
1170
1171static bool registered4 = UTILITY_REGISTRY::Register( {
1172 "ttt",
1173 "Renderer performance test",
1174 ttt_main_func,
1175} );
1176
1177#endif
const char * name
BOX2< VECTOR2D > BOX2D
Definition box2.h:928
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:553
constexpr void SetHeight(size_type val)
Definition box2.h:289
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:234
constexpr size_type GetWidth() const
Definition box2.h:211
constexpr size_type GetHeight() const
Definition box2.h:212
constexpr void SetWidth(size_type val)
Definition box2.h:284
@ GAL_TYPE_OPENGL
OpenGL implementation.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
COLOR4D & Brighten(double aFactor)
Makes the color brighter by a given factor.
Definition color4d.h:206
double a
Alpha component.
Definition color4d.h:393
Abstract interface for drawing on a 2D-surface.
PCB specific render settings.
Definition pcb_painter.h:84
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition view_group.h:39
void SetLineWidth(double aLineWidth)
void Polyline(const SHAPE_LINE_CHAIN &aPolyLine)
const COLOR4D & GetStrokeColor() const
void Line(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
void Arc(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle)
void SetStrokeColor(const COLOR4D &aColor)
static LSET AllNonCuMask()
Return a mask holding all layer minus CU layers.
Definition lset.cpp:623
double m_TrackOpacity
Opacity override for all tracks.
double m_ZoneOpacity
Opacity override for filled zone areas.
double m_PadOpacity
Opacity override for SMD pads and PTHs.
double m_ViaOpacity
Opacity override for all types of via.
ZONE_DISPLAY_MODE m_ZoneDisplayMode
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
std::shared_ptr< BOARD > m_board
std::shared_ptr< PCB_DRAW_PANEL_GAL > m_galPanel
void createView(wxWindow *aParent, PCB_DRAW_PANEL_GAL::GAL_TYPE aGalType=PCB_DRAW_PANEL_GAL::GAL_TYPE_OPENGL)
virtual void SetBoard(std::shared_ptr< BOARD > b)
std::vector< SHAPE * > m_shapes
void IterateTree(std::function< bool(PNS_DEBUG_SHAPE *)> visitor, int depth=0)
PNS_DEBUG_SHAPE * m_parent
PNS::DEBUG_DECORATOR::SRC_LOCATION_INFO m_srcLoc
std::vector< PNS_DEBUG_SHAPE * > m_children
PNS_LOG_VIEWER_FRAME_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=wxT("P&S Log Viewer"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(1045, 574), long style=wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
std::shared_ptr< WX_TEXT_CTRL_REPORTER > m_reporter
std::shared_ptr< PNS_LOG_FILE > m_logFile
virtual void onListSelect(wxTreeListEvent &event)
virtual void onListRightClick(wxMouseEvent &event)
std::shared_ptr< PNS_LOG_PLAYER > m_logPlayer
std::map< wxString, wxString > m_filenameToPathMap
void drawSimpleShape(SHAPE *aShape, bool aIsSelected, const std::string &aName)
virtual void onRewindCountText(wxCommandEvent &event) override
std::shared_ptr< KIGFX::VIEW_GROUP > m_previewItems
PNS_DEBUG_STAGE * getCurrentStage()
bool filterStringMatches(PNS_DEBUG_SHAPE *ent)
void SetBoard2(std::shared_ptr< BOARD > aBoard)
virtual void createUserTools() override
void buildListTree(wxTreeListItem item, PNS_DEBUG_SHAPE *ent, int depth=0)
virtual void onShowVerticesChecked(wxCommandEvent &event) override
void SetLogFile(PNS_LOG_FILE *aLog)
virtual void onShowThinLinesChecked(wxCommandEvent &event) override
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > m_overlay
void LoadLogFile(const wxString &aFile)
std::shared_ptr< PNS_VIEWER_IFACE > m_viewerIface
virtual void onSaveAs(wxCommandEvent &event) override
virtual void onRewindScroll(wxScrollEvent &event) override
std::optional< PNS_DEBUG_SHAPE * > m_lastSelectedItem
virtual void onShowRPIsChecked(wxCommandEvent &event) override
virtual void onBtnRewindRight(wxCommandEvent &event) override
virtual void onFilterText(wxCommandEvent &event) override
PNS_LOG_VIEWER_FRAME(wxFrame *frame)
virtual void onOpen(wxCommandEvent &event) override
virtual void onBtnRewindLeft(wxCommandEvent &event) override
virtual void onListChecked(wxTreeListEvent &event)
virtual void onExit(wxCommandEvent &event) override
void AnnotatedPoint(const VECTOR2I p, int size, std::string name="", bool aShowVertexNumbers=false)
void AnnotatedPolyset(const SHAPE_POLY_SET &aL, std::string name="", bool aShowVertexNumbers=false)
void AnnotatedPolyline(const SHAPE_LINE_CHAIN &aL, std::string name, bool aShowVertexNumbers=false)
PNS_LOG_VIEWER_OVERLAY(KIGFX::GAL *aGal)
std::unique_ptr< LABEL_MANAGER > m_labelMgr
void Arc(const SHAPE_ARC &arc)
std::vector< ENTRY > VIEW_ENTRIES
PNS_DEBUG_STAGE * GetStage(int index)
A small class to help profiling.
Definition profile.h:46
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
void Update(const PNS::ITEM *aItem)
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I::extended_type ecoord
Definition seg.h:40
VECTOR2I B
Definition seg.h:46
int Length() const
Return the length (this).
Definition seg.h:339
EDA_ANGLE GetCentralAngle() const
Get the "central angle" of the arc - this is the angle at the point of the "pie slice".
int GetWidth() const override
Definition shape_arc.h:211
double GetRadius() const
EDA_ANGLE GetStartAngle() const
const VECTOR2I & GetCenter() const
SHAPE_TYPE Type() const
Return the type of the shape.
Definition shape.h:96
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
void Simplify(int aTolerance=0)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
int SegmentCount() const
Return the number of segments in this line chain.
const VECTOR2I & CLastPoint() const
Return the last point in the line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
bool IsArcSegment(size_t aSegment) const
Represent a set of closed polygons.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
An abstract shape on 2D plane.
Definition shape.h:124
static bool Register(const KI_TEST::UTILITY_PROGRAM &aProgInfo)
Register a utility program factory function against an ID string.
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:542
WX_SHAPE_TREE_ITEM_DATA(PNS_DEBUG_SHAPE *item, int level=0)
A wrapper for reporting to a wxTextCtrl object.
Definition reporter.h:218
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:848
int SelectSingleOption(wxWindow *aParent, const wxString &aTitle, const wxString &aMessage, const wxArrayString &aOptions)
Display a dialog with radioboxes asking the user to select an option.
Definition confirm.cpp:299
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
@ LAYER_GP_OVERLAY
General purpose overlay.
Definition layer_ids.h:275
@ LAYER_SELECT_OVERLAY
Selected items overlay.
Definition layer_ids.h:276
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:521
see class PGM_BASE
int drawShapes(int argc, char *argv[])
static bool isLine45Degree(const SHAPE_LINE_CHAIN *lc)
static void collapseAllChildren(wxTreeListCtrl *tree)
static void expandAllChildren(wxTreeListCtrl *tree, int maxLevel=-1)
void runCommand(const wxString &aCommand)
static void forceSelected(wxTreeListCtrl *tree, PNS_DEBUG_SHAPE *aEntry)
#define ID_LIST_COPY
#define ID_LIST_DISPLAY_LINE
#define ID_LIST_SHOW_ALL
#define ID_LIST_SHOW_NONE
@ SH_RECT
axis-aligned rectangle
Definition shape.h:43
@ SH_CIRCLE
circle
Definition shape.h:46
@ SH_SEGMENT
line segment
Definition shape.h:44
@ SH_LINE_CHAIN
line chain (polyline)
Definition shape.h:45
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition shape.h:49
PNS_DEBUG_SHAPE * m_entries
KIBIS top(path, &reporter)
int radius
#define M_PI
wxLogTrace helper definitions.
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition util.h:135
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
Definition of file extensions used in Kicad.