diff --git a/pcbnew/dialogs/dialog_pns_settings.cpp b/pcbnew/dialogs/dialog_pns_settings.cpp new file mode 100644 index 0000000000..0368294ab1 --- /dev/null +++ b/pcbnew/dialogs/dialog_pns_settings.cpp @@ -0,0 +1,75 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2014 CERN + * Author: Maciej Suminski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +/** + * Push and Shove router settings dialog. + */ + +#include "dialog_pns_settings.h" +#include + +DIALOG_PNS_SETTINGS::DIALOG_PNS_SETTINGS( wxWindow* aParent, PNS_ROUTING_SETTINGS& aSettings ) : + DIALOG_PNS_SETTINGS_BASE( aParent ), m_settings( aSettings ) +{ + // "Figure out what's best" is not available yet + m_mode->Enable( RM_Smart, false ); + + // Load widgets' values from settings + m_mode->SetSelection( m_settings.Mode() ); + m_shoveVias->SetValue( m_settings.ShoveVias() ); + m_backPressure->SetValue( m_settings.JumpOverObstacles() ); + m_removeLoops->SetValue( m_settings.RemoveLoops() ); + m_suggestEnding->SetValue( m_settings.SuggestFinish() ); + m_autoNeckdown->SetValue( m_settings.SmartPads() ); + m_effort->SetValue( m_settings.OptimizerEffort() ); + m_smoothDragged->SetValue( m_settings.SmoothDraggedSegments() ); + m_violateDrc->SetValue( m_settings.CanViolateDRC() ); +} + + +void DIALOG_PNS_SETTINGS::OnClose( wxCloseEvent& aEvent ) +{ + // Do nothing, it is result of ESC pressing + EndModal( 0 ); +} + + +void DIALOG_PNS_SETTINGS::OnOkClick( wxCommandEvent& aEvent ) +{ + // Save widgets' values to settings + m_settings.SetMode( (PNS_MODE) m_mode->GetSelection() ); + m_settings.SetShoveVias( m_shoveVias->GetValue() ); + m_settings.SetJumpOverObstacles( m_backPressure->GetValue() ); + m_settings.SetRemoveLoops( m_removeLoops->GetValue() ); + m_settings.SetSuggestFinish ( m_suggestEnding->GetValue() ); + m_settings.SetSmartPads( m_autoNeckdown->GetValue() ); + m_settings.SetOptimizerEffort( (PNS_OPTIMIZATION_EFFORT) m_effort->GetValue() ); + m_settings.SetSmoothDraggedSegments( m_smoothDragged->GetValue() ); + m_settings.SetCanViolateDRC( m_violateDrc->GetValue() ); + + EndModal( 1 ); +} + + +void DIALOG_PNS_SETTINGS::OnCancelClick( wxCommandEvent& aEvent ) +{ + // Do nothing + EndModal( 0 ); +} diff --git a/pcbnew/dialogs/dialog_pns_settings.h b/pcbnew/dialogs/dialog_pns_settings.h new file mode 100644 index 0000000000..2241ed60dc --- /dev/null +++ b/pcbnew/dialogs/dialog_pns_settings.h @@ -0,0 +1,45 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2014 CERN + * Author: Maciej Suminski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +/** + * Push and Shove router settings dialog. + */ + +#ifndef __dialog_pns_settings__ +#define __dialog_pns_settings__ + +#include "dialog_pns_settings_base.h" + +class PNS_ROUTING_SETTINGS; + +class DIALOG_PNS_SETTINGS : public DIALOG_PNS_SETTINGS_BASE +{ + public: + DIALOG_PNS_SETTINGS( wxWindow* aParent, PNS_ROUTING_SETTINGS& aSettings ); + + virtual void OnClose( wxCloseEvent& aEvent ); + virtual void OnOkClick( wxCommandEvent& aEvent ); + virtual void OnCancelClick( wxCommandEvent& aEvent ); + + private: + PNS_ROUTING_SETTINGS& m_settings; +}; + +#endif // __dialog_pns_settings__ diff --git a/pcbnew/dialogs/dialog_pns_settings_base.cpp b/pcbnew/dialogs/dialog_pns_settings_base.cpp new file mode 100644 index 0000000000..b675f8fd9d --- /dev/null +++ b/pcbnew/dialogs/dialog_pns_settings_base.cpp @@ -0,0 +1,127 @@ +/////////////////////////////////////////////////////////////////////////// +// C++ code generated with wxFormBuilder (version Apr 30 2013) +// http://www.wxformbuilder.org/ +// +// PLEASE DO "NOT" EDIT THIS FILE! +/////////////////////////////////////////////////////////////////////////// + +#include "dialog_pns_settings_base.h" + +/////////////////////////////////////////////////////////////////////////// + +DIALOG_PNS_SETTINGS_BASE::DIALOG_PNS_SETTINGS_BASE( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style ) +{ + this->SetSizeHints( wxDefaultSize, wxDefaultSize ); + + wxBoxSizer* bMainSizer; + bMainSizer = new wxBoxSizer( wxVERTICAL ); + + wxString m_modeChoices[] = { _("Highlight collisions"), _("Shove"), _("Walk around"), _("Figure out what's best") }; + int m_modeNChoices = sizeof( m_modeChoices ) / sizeof( wxString ); + m_mode = new wxRadioBox( this, wxID_ANY, _("Mode"), wxDefaultPosition, wxDefaultSize, m_modeNChoices, m_modeChoices, 1, wxRA_SPECIFY_COLS ); + m_mode->SetSelection( 1 ); + bMainSizer->Add( m_mode, 0, wxALL, 5 ); + + wxStaticBoxSizer* bOptions; + bOptions = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("Options") ), wxVERTICAL ); + + m_shoveVias = new wxCheckBox( this, wxID_ANY, _("Shove vias"), wxDefaultPosition, wxDefaultSize, 0 ); + bOptions->Add( m_shoveVias, 0, wxALL, 5 ); + + m_backPressure = new wxCheckBox( this, wxID_ANY, _("Jump over obstacles"), wxDefaultPosition, wxDefaultSize, 0 ); + bOptions->Add( m_backPressure, 0, wxALL, 5 ); + + m_removeLoops = new wxCheckBox( this, wxID_ANY, _("Remove redundant tracks"), wxDefaultPosition, wxDefaultSize, 0 ); + bOptions->Add( m_removeLoops, 0, wxALL, 5 ); + + m_autoNeckdown = new wxCheckBox( this, wxID_ANY, _("Automatic neckdown"), wxDefaultPosition, wxDefaultSize, 0 ); + bOptions->Add( m_autoNeckdown, 0, wxALL, 5 ); + + m_smoothDragged = new wxCheckBox( this, wxID_ANY, _("Smooth dragged segments"), wxDefaultPosition, wxDefaultSize, 0 ); + bOptions->Add( m_smoothDragged, 0, wxALL, 5 ); + + m_violateDrc = new wxCheckBox( this, wxID_ANY, _("Allow DRC violations"), wxDefaultPosition, wxDefaultSize, 0 ); + bOptions->Add( m_violateDrc, 0, wxALL, 5 ); + + m_suggestEnding = new wxCheckBox( this, wxID_ANY, _("Suggest track finish"), wxDefaultPosition, wxDefaultSize, 0 ); + m_suggestEnding->Enable( false ); + + bOptions->Add( m_suggestEnding, 0, wxALL, 5 ); + + wxBoxSizer* bEffort; + bEffort = new wxBoxSizer( wxHORIZONTAL ); + + m_effortLabel = new wxStaticText( this, wxID_ANY, _("Optimizer effort"), wxDefaultPosition, wxDefaultSize, 0 ); + m_effortLabel->Wrap( -1 ); + bEffort->Add( m_effortLabel, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 ); + + + bEffort->Add( 0, 0, 0, wxEXPAND, 5 ); + + wxBoxSizer* bSlider; + bSlider = new wxBoxSizer( wxVERTICAL ); + + m_effort = new wxSlider( this, wxID_ANY, 1, 0, 2, wxDefaultPosition, wxDefaultSize, wxSL_AUTOTICKS|wxSL_BOTTOM|wxSL_HORIZONTAL|wxSL_TOP ); + bSlider->Add( m_effort, 1, wxEXPAND, 5 ); + + wxBoxSizer* bSliderLabels; + bSliderLabels = new wxBoxSizer( wxHORIZONTAL ); + + m_lowLabel = new wxStaticText( this, wxID_ANY, _("low"), wxDefaultPosition, wxDefaultSize, 0 ); + m_lowLabel->Wrap( -1 ); + m_lowLabel->SetFont( wxFont( 8, 70, 90, 90, false, wxEmptyString ) ); + + bSliderLabels->Add( m_lowLabel, 0, 0, 5 ); + + + bSliderLabels->Add( 0, 0, 1, wxEXPAND, 5 ); + + m_highLabel = new wxStaticText( this, wxID_ANY, _("high"), wxDefaultPosition, wxDefaultSize, 0 ); + m_highLabel->Wrap( -1 ); + m_highLabel->SetFont( wxFont( 8, 70, 90, 90, false, wxEmptyString ) ); + + bSliderLabels->Add( m_highLabel, 0, 0, 5 ); + + + bSlider->Add( bSliderLabels, 1, wxEXPAND, 5 ); + + + bEffort->Add( bSlider, 1, wxEXPAND, 5 ); + + + bOptions->Add( bEffort, 1, wxALL|wxEXPAND, 5 ); + + wxBoxSizer* bButtons; + bButtons = new wxBoxSizer( wxHORIZONTAL ); + + m_ok = new wxButton( this, wxID_OK, _("OK"), wxDefaultPosition, wxDefaultSize, 0 ); + m_ok->SetDefault(); + bButtons->Add( m_ok, 1, wxALL, 5 ); + + m_cancel = new wxButton( this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 ); + bButtons->Add( m_cancel, 1, wxALL, 5 ); + + + bOptions->Add( bButtons, 1, wxEXPAND, 5 ); + + + bMainSizer->Add( bOptions, 1, wxEXPAND, 5 ); + + + this->SetSizer( bMainSizer ); + this->Layout(); + + // Connect Events + this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( DIALOG_PNS_SETTINGS_BASE::OnClose ) ); + m_ok->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PNS_SETTINGS_BASE::OnOkClick ), NULL, this ); + m_cancel->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PNS_SETTINGS_BASE::OnCancelClick ), NULL, this ); +} + +DIALOG_PNS_SETTINGS_BASE::~DIALOG_PNS_SETTINGS_BASE() +{ + // Disconnect Events + this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( DIALOG_PNS_SETTINGS_BASE::OnClose ) ); + m_ok->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PNS_SETTINGS_BASE::OnOkClick ), NULL, this ); + m_cancel->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PNS_SETTINGS_BASE::OnCancelClick ), NULL, this ); + +} diff --git a/pcbnew/dialogs/dialog_pns_settings_base.fbp b/pcbnew/dialogs/dialog_pns_settings_base.fbp new file mode 100644 index 0000000000..dd008e9e25 --- /dev/null +++ b/pcbnew/dialogs/dialog_pns_settings_base.fbp @@ -0,0 +1,1414 @@ + + + + + + C++ + 1 + source_name + 0 + 0 + res + UTF-8 + connect + dialog_pns_settings_base + 1000 + none + 1 + DIALOG_PNS_SETTINGS_BASE + + . + + 1 + 1 + 1 + 0 + 0 + + 0 + wxAUI_MGR_DEFAULT + + + + 1 + 1 + impl_virtual + + + + 0 + wxID_ANY + + + DIALOG_PNS_SETTINGS_BASE + + 289,504 + wxDEFAULT_DIALOG_STYLE + + Interactive Router settings + + + + + + + + + + + + + + OnClose + + + + + + + + + + + + + + + + + + + + + + + + + + + + + bMainSizer + wxVERTICAL + none + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + "Highlight collisions" "Shove" "Walk around" "Figure out what's best" + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Mode + 1 + + 0 + + + 0 + + 1 + m_mode + 1 + + + protected + 1 + + Resizable + 1 + 1 + + wxRA_SPECIFY_COLS + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxEXPAND + 1 + + wxID_ANY + Options + + bOptions + wxVERTICAL + none + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Shove vias + + 0 + + + 0 + + 1 + m_shoveVias + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Jump over obstacles + + 0 + + + 0 + + 1 + m_backPressure + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Remove redundant tracks + + 0 + + + 0 + + 1 + m_removeLoops + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Automatic neckdown + + 0 + + + 0 + + 1 + m_autoNeckdown + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Smooth dragged segments + + 0 + + + 0 + + 1 + m_smoothDragged + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Allow DRC violations + + 0 + + + 0 + + 1 + m_violateDrc + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 0 + + 1 + + 0 + 0 + wxID_ANY + Suggest track finish + + 0 + + + 0 + + 1 + m_suggestEnding + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL|wxEXPAND + 1 + + + bEffort + wxHORIZONTAL + none + + 5 + wxALIGN_CENTER_VERTICAL|wxALL + 1 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Optimizer effort + + 0 + + + 0 + + 1 + m_effortLabel + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + + + -1 + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxEXPAND + 0 + + 0 + protected + 0 + + + + 5 + wxEXPAND + 1 + + + bSlider + wxVERTICAL + none + + 5 + wxEXPAND + 1 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + 2 + + 1 + + 0 + + 0 + + 1 + m_effort + 1 + + + protected + 1 + + Resizable + 1 + + wxSL_AUTOTICKS|wxSL_BOTTOM|wxSL_HORIZONTAL|wxSL_TOP + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxEXPAND + 1 + + + bSliderLabels + wxHORIZONTAL + none + + 5 + + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + ,90,90,8,70,0 + 0 + 0 + wxID_ANY + low + + 0 + + + 0 + + 1 + m_lowLabel + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + + + -1 + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxEXPAND + 1 + + 0 + protected + 0 + + + + 5 + + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + ,90,90,8,70,0 + 0 + 0 + wxID_ANY + high + + 0 + + + 0 + + 1 + m_highLabel + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + + + -1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxEXPAND + 1 + + + bButtons + wxHORIZONTAL + none + + 5 + wxALL + 1 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_OK + OK + + 0 + + + 0 + + 1 + m_ok + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + OnOkClick + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 1 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_CANCEL + Cancel + + 0 + + + 0 + + 1 + m_cancel + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + OnCancelClick + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pcbnew/dialogs/dialog_pns_settings_base.h b/pcbnew/dialogs/dialog_pns_settings_base.h new file mode 100644 index 0000000000..ceb135dc58 --- /dev/null +++ b/pcbnew/dialogs/dialog_pns_settings_base.h @@ -0,0 +1,67 @@ +/////////////////////////////////////////////////////////////////////////// +// C++ code generated with wxFormBuilder (version Apr 30 2013) +// http://www.wxformbuilder.org/ +// +// PLEASE DO "NOT" EDIT THIS FILE! +/////////////////////////////////////////////////////////////////////////// + +#ifndef __DIALOG_PNS_SETTINGS_BASE_H__ +#define __DIALOG_PNS_SETTINGS_BASE_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +/// Class DIALOG_PNS_SETTINGS_BASE +/////////////////////////////////////////////////////////////////////////////// +class DIALOG_PNS_SETTINGS_BASE : public wxDialog +{ + private: + + protected: + wxRadioBox* m_mode; + wxCheckBox* m_shoveVias; + wxCheckBox* m_backPressure; + wxCheckBox* m_removeLoops; + wxCheckBox* m_autoNeckdown; + wxCheckBox* m_smoothDragged; + wxCheckBox* m_violateDrc; + wxCheckBox* m_suggestEnding; + wxStaticText* m_effortLabel; + wxSlider* m_effort; + wxStaticText* m_lowLabel; + wxStaticText* m_highLabel; + wxButton* m_ok; + wxButton* m_cancel; + + // Virtual event handlers, overide them in your derived class + virtual void OnClose( wxCloseEvent& event ) { event.Skip(); } + virtual void OnOkClick( wxCommandEvent& event ) { event.Skip(); } + virtual void OnCancelClick( wxCommandEvent& event ) { event.Skip(); } + + + public: + + DIALOG_PNS_SETTINGS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Interactive Router settings"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 289,504 ), long style = wxDEFAULT_DIALOG_STYLE ); + ~DIALOG_PNS_SETTINGS_BASE(); + +}; + +#endif //__DIALOG_PNS_SETTINGS_BASE_H__ diff --git a/pcbnew/dialogs/dialog_track_via_size.cpp b/pcbnew/dialogs/dialog_track_via_size.cpp new file mode 100644 index 0000000000..8a51cafdb4 --- /dev/null +++ b/pcbnew/dialogs/dialog_track_via_size.cpp @@ -0,0 +1,91 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2014 CERN + * Author: Maciej Suminski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +/** + * Push and Shove router track width and via size dialog. + */ + +#include "dialog_track_via_size.h" +#include +#include +#include + +DIALOG_TRACK_VIA_SIZE::DIALOG_TRACK_VIA_SIZE( wxWindow* aParent, PNS_ROUTING_SETTINGS& aSettings ) : + DIALOG_TRACK_VIA_SIZE_BASE( aParent ), + m_settings( aSettings ) +{ + // Load router settings to dialog fields + m_trackWidth->SetValue( To_User_Unit( m_trackWidth->GetUnits(), m_settings.GetTrackWidth() ) ); + m_viaDiameter->SetValue( To_User_Unit( m_viaDiameter->GetUnits(), m_settings.GetViaDiameter() ) ); + m_viaDrill->SetValue( To_User_Unit( m_viaDrill->GetUnits(), m_settings.GetViaDrill() ) ); + + m_trackWidth->SetFocus(); + + // Pressing ENTER when any of the text input fields is active applies changes + #if wxCHECK_VERSION( 3, 0, 0 ) + Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( DIALOG_TRACK_VIA_SIZE::onOkClick ), NULL, this ); + #else + Connect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( DIALOG_TRACK_VIA_SIZE::onOkClick ), NULL, this ); + #endif +} + + +bool DIALOG_TRACK_VIA_SIZE::check() +{ + // Wrong input + if( !m_trackWidth->GetValue() || !m_viaDiameter->GetValue() || !m_viaDrill->GetValue() ) + return false; + + // Via drill should be smaller than via diameter + if( *m_viaDrill->GetValue() >= m_viaDiameter->GetValue() ) + return false; + + return true; +} + + +void DIALOG_TRACK_VIA_SIZE::onClose( wxCloseEvent& aEvent ) +{ + EndModal( 0 ); +} + + +void DIALOG_TRACK_VIA_SIZE::onOkClick( wxCommandEvent& aEvent ) +{ + if( check() ) + { + // Store dialog values to the router settings + m_settings.SetTrackWidth( From_User_Unit( m_trackWidth->GetUnits(), *m_trackWidth->GetValue() ) ); + m_settings.SetViaDiameter( From_User_Unit( m_viaDiameter->GetUnits(), *m_viaDiameter->GetValue() ) ); + m_settings.SetViaDrill( From_User_Unit( m_viaDrill->GetUnits(), *m_viaDrill->GetValue() ) ); + EndModal( 1 ); + } + else + { + DisplayError( GetParent(), _( "Settings are incorrect" ) ); + m_trackWidth->SetFocus(); + } +} + + +void DIALOG_TRACK_VIA_SIZE::onCancelClick( wxCommandEvent& aEvent ) +{ + EndModal( 0 ); +} diff --git a/pcbnew/dialogs/dialog_track_via_size.h b/pcbnew/dialogs/dialog_track_via_size.h new file mode 100644 index 0000000000..92a03e8ec6 --- /dev/null +++ b/pcbnew/dialogs/dialog_track_via_size.h @@ -0,0 +1,52 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2014 CERN + * Author: Maciej Suminski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +/** + * Push and Shove router track width and via size dialog. + */ + +#ifndef __dialog_track_via_size__ +#define __dialog_track_via_size__ + +#include "dialog_track_via_size_base.h" + +class PNS_ROUTING_SETTINGS; + +/** Implementing DIALOG_TRACK_VIA_SIZE_BASE */ +class DIALOG_TRACK_VIA_SIZE : public DIALOG_TRACK_VIA_SIZE_BASE +{ + public: + /** Constructor */ + DIALOG_TRACK_VIA_SIZE( wxWindow* aParent, PNS_ROUTING_SETTINGS& aSettings ); + + protected: + // Routings settings that are modified by the dialog. + PNS_ROUTING_SETTINGS& m_settings; + + ///> Checks if values given in the dialog are sensible. + bool check(); + + // Handlers for DIALOG_TRACK_VIA_SIZE_BASE events. + void onClose( wxCloseEvent& aEvent ); + void onOkClick( wxCommandEvent& aEvent ); + void onCancelClick( wxCommandEvent& aEvent ); +}; + +#endif // __dialog_track_via_size__ diff --git a/pcbnew/dialogs/dialog_track_via_size_base.cpp b/pcbnew/dialogs/dialog_track_via_size_base.cpp new file mode 100644 index 0000000000..a402009da1 --- /dev/null +++ b/pcbnew/dialogs/dialog_track_via_size_base.cpp @@ -0,0 +1,59 @@ +/////////////////////////////////////////////////////////////////////////// +// C++ code generated with wxFormBuilder (version Apr 30 2013) +// http://www.wxformbuilder.org/ +// +// PLEASE DO "NOT" EDIT THIS FILE! +/////////////////////////////////////////////////////////////////////////// + +#include "dialog_track_via_size_base.h" + +/////////////////////////////////////////////////////////////////////////// + +DIALOG_TRACK_VIA_SIZE_BASE::DIALOG_TRACK_VIA_SIZE_BASE( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style ) +{ + this->SetSizeHints( wxDefaultSize, wxDefaultSize ); + + wxBoxSizer* bSizes; + bSizes = new wxBoxSizer( wxVERTICAL ); + + m_trackWidth = new WX_UNIT_TEXT( this, _("Track width:") ); + bSizes->Add( m_trackWidth, 0, wxALL, 5 ); + + m_viaDiameter = new WX_UNIT_TEXT( this, _("Via diameter:") ); + bSizes->Add( m_viaDiameter, 0, wxALL, 5 ); + + m_viaDrill = new WX_UNIT_TEXT( this, _("Via drill:") ); + bSizes->Add( m_viaDrill, 0, wxALL, 5 ); + + wxBoxSizer* bButtons; + bButtons = new wxBoxSizer( wxHORIZONTAL ); + + m_ok = new wxButton( this, wxID_OK, _("OK"), wxDefaultPosition, wxDefaultSize, 0 ); + bButtons->Add( m_ok, 1, wxALL, 5 ); + + m_cancel = new wxButton( this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 ); + bButtons->Add( m_cancel, 1, wxALL, 5 ); + + + bSizes->Add( bButtons, 0, wxEXPAND, 5 ); + + + this->SetSizer( bSizes ); + this->Layout(); + + this->Centre( wxBOTH ); + + // Connect Events + this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( DIALOG_TRACK_VIA_SIZE_BASE::onClose ) ); + m_ok->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_TRACK_VIA_SIZE_BASE::onOkClick ), NULL, this ); + m_cancel->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_TRACK_VIA_SIZE_BASE::onCancelClick ), NULL, this ); +} + +DIALOG_TRACK_VIA_SIZE_BASE::~DIALOG_TRACK_VIA_SIZE_BASE() +{ + // Disconnect Events + this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( DIALOG_TRACK_VIA_SIZE_BASE::onClose ) ); + m_ok->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_TRACK_VIA_SIZE_BASE::onOkClick ), NULL, this ); + m_cancel->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_TRACK_VIA_SIZE_BASE::onCancelClick ), NULL, this ); + +} diff --git a/pcbnew/dialogs/dialog_track_via_size_base.fbp b/pcbnew/dialogs/dialog_track_via_size_base.fbp new file mode 100644 index 0000000000..61462d3330 --- /dev/null +++ b/pcbnew/dialogs/dialog_track_via_size_base.fbp @@ -0,0 +1,539 @@ + + + + + + C++ + 1 + source_name + 0 + 0 + res + UTF-8 + connect + dialog_track_via_size_base + 1000 + none + 1 + DIALOG_TRACK_VIA_SIZE_BASE + + . + + 1 + 1 + 1 + 0 + 0 + + 0 + wxAUI_MGR_DEFAULT + + wxBOTH + + 1 + 1 + impl_virtual + + + + 0 + wxID_ANY + + + DIALOG_TRACK_VIA_SIZE_BASE + + 388,164 + wxDEFAULT_DIALOG_STYLE + + Track width and via size + + + + + + + + + + + + + + onClose + + + + + + + + + + + + + + + + + + + + + + + + + + + + + bSizes + wxVERTICAL + none + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + WX_UNIT_TEXT + 1 + m_trackWidth = new WX_UNIT_TEXT( this, _("Track width:") ); + + 1 + WX_UNIT_TEXT* m_trackWidth; + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + #include <wxunittext.h> + + 0 + + + 0 + + 1 + m_trackWidth + 1 + + + protected + 1 + + Resizable + + 1 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + WX_UNIT_TEXT + 1 + m_viaDiameter = new WX_UNIT_TEXT( this, _("Via diameter:") ); + + 1 + WX_UNIT_TEXT* m_viaDiameter; + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + #include <wxunittext.h> + + 0 + + + 0 + + 1 + m_viaDiameter + 1 + + + protected + 1 + + Resizable + + 1 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + WX_UNIT_TEXT + 1 + m_viaDrill = new WX_UNIT_TEXT( this, _("Via drill:") ); + + 1 + WX_UNIT_TEXT* m_viaDrill; + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + #include <wxunittext.h> + + 0 + + + 0 + + 1 + m_viaDrill + 1 + + + protected + 1 + + Resizable + + 1 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxEXPAND + 0 + + + bButtons + wxHORIZONTAL + none + + 5 + wxALL + 1 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_OK + OK + + 0 + + + 0 + + 1 + m_ok + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + onOkClick + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + wxALL + 1 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_CANCEL + Cancel + + 0 + + + 0 + + 1 + m_cancel + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + onCancelClick + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pcbnew/dialogs/dialog_track_via_size_base.h b/pcbnew/dialogs/dialog_track_via_size_base.h new file mode 100644 index 0000000000..de4f0e43a1 --- /dev/null +++ b/pcbnew/dialogs/dialog_track_via_size_base.h @@ -0,0 +1,54 @@ +/////////////////////////////////////////////////////////////////////////// +// C++ code generated with wxFormBuilder (version Apr 30 2013) +// http://www.wxformbuilder.org/ +// +// PLEASE DO "NOT" EDIT THIS FILE! +/////////////////////////////////////////////////////////////////////////// + +#ifndef __DIALOG_TRACK_VIA_SIZE_BASE_H__ +#define __DIALOG_TRACK_VIA_SIZE_BASE_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +/// Class DIALOG_TRACK_VIA_SIZE_BASE +/////////////////////////////////////////////////////////////////////////////// +class DIALOG_TRACK_VIA_SIZE_BASE : public wxDialog +{ + private: + + protected: + WX_UNIT_TEXT* m_trackWidth; + WX_UNIT_TEXT* m_viaDiameter; + WX_UNIT_TEXT* m_viaDrill; + wxButton* m_ok; + wxButton* m_cancel; + + // Virtual event handlers, overide them in your derived class + virtual void onClose( wxCloseEvent& event ) { event.Skip(); } + virtual void onOkClick( wxCommandEvent& event ) { event.Skip(); } + virtual void onCancelClick( wxCommandEvent& event ) { event.Skip(); } + + + public: + + DIALOG_TRACK_VIA_SIZE_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Track width and via size"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 388,164 ), long style = wxDEFAULT_DIALOG_STYLE ); + ~DIALOG_TRACK_VIA_SIZE_BASE(); + +}; + +#endif //__DIALOG_TRACK_VIA_SIZE_BASE_H__ diff --git a/pcbnew/router/CMakeLists.txt b/pcbnew/router/CMakeLists.txt index 329714658b..94d89b771c 100644 --- a/pcbnew/router/CMakeLists.txt +++ b/pcbnew/router/CMakeLists.txt @@ -11,40 +11,50 @@ include_directories( set( PCBNEW_PNS_SRCS direction.h - pns_via.h - pns_routing_settings.h - pns_shove.cpp - pns_line.cpp - pns_utils.h - pns_layerset.h + time_limit.h + time_limit.cpp trace.h - pns_line.h - pns_walkaround.cpp - pns_node.h - pns_line_placer.cpp - pns_utils.cpp - pns_solid.h - pns_item.cpp - pns_via.cpp - pns_node.cpp - pns_solid.cpp - pns_line_placer.h - pns_optimizer.h - pns_walkaround.h - pns_shove.h - pns_router.h - pns_router.cpp + + pns_algo_base.h + pns_algo_base.cpp + pns_dragger.cpp + pns_dragger.h pns_index.h pns_item.h - pns_optimizer.cpp - pns_joint.h - pns_segment.h + pns_item.cpp pns_itemset.h pns_itemset.cpp - router_tool.cpp - router_tool.h + pns_joint.h + pns_layerset.h + pns_line.h + pns_line.cpp + pns_line_placer.h + pns_line_placer.cpp + pns_logger.h + pns_logger.cpp + pns_node.h + pns_node.cpp + pns_optimizer.h + pns_optimizer.cpp + pns_router.h + pns_router.cpp + pns_routing_settings.h + pns_routing_settings.cpp + pns_segment.h + pns_shove.h + pns_shove.cpp + pns_solid.h + pns_solid.cpp + pns_utils.h + pns_utils.cpp + pns_via.h + pns_via.cpp + pns_walkaround.h + pns_walkaround.cpp router_preview_item.cpp router_preview_item.h + router_tool.cpp + router_tool.h ) add_library( pnsrouter STATIC ${PCBNEW_PNS_SRCS} ) diff --git a/pcbnew/router/direction.h b/pcbnew/router/direction.h index ad38fcbdef..646c681cd2 100644 --- a/pcbnew/router/direction.h +++ b/pcbnew/router/direction.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -180,6 +180,11 @@ public: return ( m_dir % 2 ) == 1; } + bool IsDefined() const + { + return m_dir != UNDEFINED; + } + /** * Function BuildInitialTrace() * @@ -244,32 +249,75 @@ public: return aOther.m_dir != m_dir; } + /** + * Function Right() + * + * Returns the direction on the right side of this (i.e. turns right + * by 45 deg) + */ const DIRECTION_45 Right() const { DIRECTION_45 r; - r.m_dir = (Directions) (m_dir + 1); - - if( r.m_dir == NW ) - r.m_dir = N; + if ( m_dir != UNDEFINED ) + r.m_dir = static_cast( ( m_dir + 1 ) % 8 ); return r; } -private: - - template - int sign( T val ) const + /** + * Function Left() + * + * Returns the direction on the left side of this (i.e. turns left + * by 45 deg) + */ + const DIRECTION_45 Left() const { - return (T( 0 ) < val) - ( val < T( 0 ) ); + DIRECTION_45 l; + + if (m_dir == UNDEFINED) + return l; + + if(m_dir == N) + l.m_dir = NW; + else + l.m_dir = static_cast (m_dir - 1); + + return l; } + + + /** + * Function ToVector() + * + * Returns a unit vector corresponding to our direction. + */ + const VECTOR2I ToVector() const + { + switch(m_dir) + { + case N: return VECTOR2I(0, 1); + case S: return VECTOR2I(0, -1); + case E: return VECTOR2I(1, 0); + case W: return VECTOR2I(-1, 0); + case NE: return VECTOR2I(1, 1); + case NW: return VECTOR2I(-1, 1); + case SE: return VECTOR2I(1, -1); + case SW: return VECTOR2I(-1, -1); + + default: + return VECTOR2I(0, 0); + } + } + +private: + /** * Function construct() * Calculates the direction from a vector. If the vector's angle is not a multiple of 45 * degrees, the direction is rounded to the nearest octant. - * @param aVec our vector - */ + * @param aVec our vector */ void construct( const VECTOR2I& aVec ) { m_dir = UNDEFINED; @@ -321,8 +369,9 @@ private: m_dir = S; } } - - Directions m_dir; ///> our actual direction + + ///> our actual direction + Directions m_dir; }; #endif // __DIRECTION_H diff --git a/pcbnew/router/pns_algo_base.cpp b/pcbnew/router/pns_algo_base.cpp new file mode 100644 index 0000000000..7008665428 --- /dev/null +++ b/pcbnew/router/pns_algo_base.cpp @@ -0,0 +1,32 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "pns_algo_base.h" +#include "pns_router.h" + +PNS_ROUTING_SETTINGS& PNS_ALGO_BASE::Settings() const +{ + return m_router->Settings(); +} + +PNS_LOGGER *PNS_ALGO_BASE::Logger() +{ + return NULL; +} \ No newline at end of file diff --git a/pcbnew/router/pns_algo_base.h b/pcbnew/router/pns_algo_base.h new file mode 100644 index 0000000000..c274c15116 --- /dev/null +++ b/pcbnew/router/pns_algo_base.h @@ -0,0 +1,60 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __PNS_ALGO_BASE_H +#define __PNS_ALGO_BASE_H + +#include "pns_routing_settings.h" + +class PNS_ROUTER; +class PNS_LOGGER; + +/** + * Class PNS_ALGO_BASE + * + * Base class for all P&S algorithms (shoving, walkaround, line placement, dragging, etc.) + * Holds a bunch of objects commonly used by all algorithms (P&S settings, parent router instance, logging) + **/ + +class PNS_ALGO_BASE { + +public: + PNS_ALGO_BASE ( PNS_ROUTER *aRouter ): + m_router ( aRouter ) + {}; + + virtual ~PNS_ALGO_BASE() {} + + ///> Returns the instance of our router + PNS_ROUTER *Router() const { + return m_router; + } + + ///> Returns current router settings + PNS_ROUTING_SETTINGS& Settings() const; + + ///> Returns the logger object, allowing to dump geometry to a file. + virtual PNS_LOGGER *Logger(); + +private: + PNS_ROUTER *m_router; +}; + +#endif diff --git a/pcbnew/router/pns_dragger.cpp b/pcbnew/router/pns_dragger.cpp new file mode 100644 index 0000000000..5361c641ec --- /dev/null +++ b/pcbnew/router/pns_dragger.cpp @@ -0,0 +1,301 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include + +#include "pns_dragger.h" +#include "pns_shove.h" +#include "pns_router.h" + +PNS_DRAGGER::PNS_DRAGGER( PNS_ROUTER* aRouter ) : + PNS_ALGO_BASE ( aRouter ) +{ + m_world = NULL; + m_shove = NULL; +}; + +PNS_DRAGGER::~PNS_DRAGGER() +{ + if( m_shove ) + delete m_shove; +} + +void PNS_DRAGGER::SetWorld ( PNS_NODE *aWorld ) +{ + m_world = aWorld; +} + +bool PNS_DRAGGER::startDragSegment( const VECTOR2D& aP, PNS_SEGMENT *aSeg ) +{ + int w2 = aSeg->Width() / 2; + + m_draggedLine = m_world->AssembleLine ( aSeg, &m_draggedSegmentIndex ); + m_shove->SetInitialLine (m_draggedLine); + m_lastValidDraggedLine = *m_draggedLine; + m_lastValidDraggedLine.ClearSegmentLinks(); + + if( (aP - aSeg->Seg().A).EuclideanNorm() <= w2 ) + m_mode = CORNER; + else if( (aP - aSeg->Seg().B).EuclideanNorm() <= w2 ) + { + m_draggedSegmentIndex ++; + m_mode = CORNER; + } else + m_mode = SEGMENT; + return true; +} + +bool PNS_DRAGGER::startDragVia( const VECTOR2D& aP, PNS_VIA *aVia ) +{ + m_draggedVia = aVia; + m_initialVia = aVia; + m_mode = VIA; + + VECTOR2I p0 ( aVia->Pos() ); + PNS_JOINT *jt = m_world->FindJoint( p0, aVia->Layers().Start(), aVia->Net() ); + + BOOST_FOREACH(PNS_ITEM *item, jt->LinkList() ) + { + if(item->OfKind( PNS_ITEM::SEGMENT )) + { + int segIndex; + PNS_SEGMENT *seg = (PNS_SEGMENT *) item; + std::auto_ptr l ( m_world->AssembleLine(seg, &segIndex) ); + + if(segIndex != 0) + l->Reverse(); + + m_origViaConnections.push_back (*l); + + } + } + + + return true; +} + +bool PNS_DRAGGER::Start ( const VECTOR2I& aP, PNS_ITEM* aStartItem ) +{ + m_shove = new PNS_SHOVE ( m_world, Router() ); + m_lastNode = NULL; + m_draggedItems.Clear(); + m_currentMode = Settings().Mode(); + + TRACE(2, "StartDragging: item %p [kind %d]", aStartItem % aStartItem->Kind()); + + switch( aStartItem->Kind() ) + { + case PNS_ITEM::SEGMENT: + return startDragSegment ( aP, static_cast (aStartItem) ); + case PNS_ITEM::VIA: + return startDragVia ( aP, static_cast (aStartItem) ); + default: + return false; + } +} + +bool PNS_DRAGGER::dragMarkObstacles(const VECTOR2I& aP) +{ + if(m_lastNode) + { + delete m_lastNode; + m_lastNode = NULL; + } + + switch(m_mode) + { + case SEGMENT: + case CORNER: + { + int thresh = Settings().SmoothDraggedSegments() ? m_draggedLine->Width() / 4 : 0; + PNS_LINE tmp (*m_draggedLine); + + if(m_mode == SEGMENT) + tmp.DragSegment ( aP, m_draggedSegmentIndex, thresh ); + else + tmp.DragCorner ( aP, m_draggedSegmentIndex, thresh ); + + m_lastNode = m_shove->CurrentNode()->Branch(); + + m_lastValidDraggedLine = tmp; + m_lastValidDraggedLine.ClearSegmentLinks(); + m_lastValidDraggedLine.Unmark(); + + m_lastNode->Add ( &m_lastValidDraggedLine ); + m_draggedItems = PNS_ITEMSET ( &m_lastValidDraggedLine ); + + break; + } + case VIA: // fixme... + { + m_lastNode = m_shove->CurrentNode()->Branch(); + dumbDragVia ( m_initialVia, m_lastNode, aP ); + + + break; + } + } + + if (Settings().CanViolateDRC()) + m_dragStatus = true; + else + m_dragStatus = !m_world->CheckColliding( m_draggedItems ); + + return true; +} + +void PNS_DRAGGER::dumbDragVia ( PNS_VIA *aVia, PNS_NODE *aNode, const VECTOR2I& aP ) +{ + // fixme: this is awful. + m_draggedVia = aVia->Clone(); + m_draggedVia->SetPos( aP ); + m_draggedItems.Clear(); + m_draggedItems.Add(m_draggedVia); + + m_lastNode->Remove ( aVia ); + m_lastNode->Add ( m_draggedVia ); + + BOOST_FOREACH(PNS_LINE &l, m_origViaConnections) + { + PNS_LINE origLine (l); + PNS_LINE *draggedLine = l.Clone(); + + draggedLine->DragCorner( aP, 0 ); + draggedLine->ClearSegmentLinks(); + + m_draggedItems.AddOwned( draggedLine ); + + m_lastNode->Remove ( &origLine ); + m_lastNode->Add ( draggedLine ); + } +} + +bool PNS_DRAGGER::dragShove(const VECTOR2I& aP) +{ + bool ok = false; + + + if(m_lastNode) + { + delete m_lastNode; + m_lastNode = NULL; + } + + switch(m_mode) + { + case SEGMENT: + case CORNER: + { + int thresh = Settings().SmoothDraggedSegments() ? m_draggedLine->Width() / 4 : 0; + PNS_LINE tmp (*m_draggedLine); + if(m_mode == SEGMENT) + tmp.DragSegment ( aP, m_draggedSegmentIndex, thresh ); + else + tmp.DragCorner ( aP, m_draggedSegmentIndex, thresh ); + + PNS_SHOVE::ShoveStatus st = m_shove->ShoveLines( tmp ); + + if(st == PNS_SHOVE::SH_OK) + ok = true; + else if (st == PNS_SHOVE::SH_HEAD_MODIFIED) + { + tmp = m_shove->NewHead(); + ok = true; + } + + m_lastNode = m_shove->CurrentNode()->Branch(); + + if(ok) + m_lastValidDraggedLine = tmp; + + m_lastValidDraggedLine.ClearSegmentLinks(); + m_lastValidDraggedLine.Unmark(); + m_lastNode->Add ( &m_lastValidDraggedLine ); + m_draggedItems = PNS_ITEMSET ( &m_lastValidDraggedLine ); + + break; + } + case VIA: + { + PNS_VIA *newVia; + PNS_SHOVE::ShoveStatus st = m_shove -> ShoveDraggingVia ( m_draggedVia, aP, &newVia ); + + if(st == PNS_SHOVE::SH_OK || st == PNS_SHOVE::SH_HEAD_MODIFIED) + ok = true; + + m_lastNode = m_shove->CurrentNode()->Branch(); + + if( ok ) + { + m_draggedVia = newVia; + m_draggedItems.Clear(); + } + + break; + } + + } + + m_dragStatus = ok; + return ok; +} + +bool PNS_DRAGGER::FixRoute( ) +{ + if(m_dragStatus) + { + Router()->CommitRouting( CurrentNode() ); + return true; + } + + return false; +} + +bool PNS_DRAGGER::Drag ( const VECTOR2I& aP ) +{ + switch ( m_currentMode ) + { + case RM_MarkObstacles: + return dragMarkObstacles (aP); + case RM_Shove: + case RM_Walkaround: + case RM_Smart: + return dragShove ( aP ); + default: + return false; + } +} + +PNS_NODE *PNS_DRAGGER::CurrentNode() const +{ + return m_lastNode; +} + +const PNS_ITEMSET PNS_DRAGGER::Traces() +{ + return m_draggedItems; +} + +PNS_LOGGER *PNS_DRAGGER::Logger() +{ + if(m_shove) + return m_shove->Logger(); + return NULL; +} diff --git a/pcbnew/router/pns_dragger.h b/pcbnew/router/pns_dragger.h new file mode 100644 index 0000000000..73d206589d --- /dev/null +++ b/pcbnew/router/pns_dragger.h @@ -0,0 +1,135 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __PNS_DRAGGER_H +#define __PNS_DRAGGER_H + +#include + +#include "pns_node.h" +#include "pns_via.h" +#include "pns_line.h" +#include "pns_algo_base.h" +#include "pns_itemset.h" + +class PNS_ROUTER; +class PNS_SHOVE; +class PNS_OPTIMIZER; +class PNS_ROUTER_BASE; + +/** + * Class PNS_DRAGGER + * + * Via, segment and corner dragging algorithm. + */ + +class PNS_DRAGGER : public PNS_ALGO_BASE +{ +public: + + PNS_DRAGGER( PNS_ROUTER *aRouter ); + ~PNS_DRAGGER(); + + /** + * Function SetWorld() + * + * Sets the board to work on. + */ + void SetWorld ( PNS_NODE *aWorld ); + + + /** + * Function Start() + * + * Starts routing a single track at point aP, taking item aStartItem as anchor + * (unless NULL). Returns true if a dragging operation has started. + */ + bool Start ( const VECTOR2I& aP, PNS_ITEM* aStartItem ); + + /** + * Function Drag() + * + * Drags the current segment/corner/via to the point aP. + * @return true, if dragging finished with success. + */ + bool Drag ( const VECTOR2I& aP ); + + /** + * Function FixRoute() + * + * Checks if the result of current dragging operation is correct + * and eventually commits it to the world. + * @return true, if dragging finished with success. + */ + bool FixRoute ( ); + + /** + * Function CurrentNode() + * + * Returns the most recent world state, including all + * items changed due to dragging operation. + */ + + PNS_NODE* CurrentNode() const; + + /** + * Function Traces() + * + * Returns the set of dragged items. + */ + const PNS_ITEMSET Traces(); + + /// @copydoc PNS_ALGO_BASE::Logger() + virtual PNS_LOGGER *Logger(); + +private: + + typedef std::pair LinePair; + typedef std::vector LinePairVec; + + enum DragMode { + CORNER = 0, + SEGMENT, + VIA + }; + + bool dragMarkObstacles(const VECTOR2I& aP); + bool dragShove(const VECTOR2I& aP); + bool startDragSegment( const VECTOR2D& aP, PNS_SEGMENT *aSeg ); + bool startDragVia( const VECTOR2D& aP, PNS_VIA *aVia ); + void dumbDragVia ( PNS_VIA *aVia, PNS_NODE *aNode, const VECTOR2I& aP ); + + PNS_NODE * m_world; + PNS_NODE * m_lastNode; + DragMode m_mode; + PNS_LINE * m_draggedLine; + PNS_VIA * m_draggedVia; + PNS_LINE m_lastValidDraggedLine; + PNS_SHOVE * m_shove; + int m_draggedSegmentIndex; + bool m_dragStatus; + PNS_MODE m_currentMode; + std::vector m_origViaConnections; + std::vector m_draggedViaConnections; + PNS_VIA * m_initialVia; + PNS_ITEMSET m_draggedItems; +}; + +#endif diff --git a/pcbnew/router/pns_index.h b/pcbnew/router/pns_index.h index 05d612e597..841f61bc1a 100644 --- a/pcbnew/router/pns_index.h +++ b/pcbnew/router/pns_index.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,12 +15,15 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_INDEX_H #define __PNS_INDEX_H +#include +#include + #include #include @@ -33,7 +36,7 @@ * Class PNS_INDEX * * Custom spatial index, holding our board items and allowing for very fast searches. Items - * are assigned to separate R-Tree subundices depending on their type and spanned layers, reducing + * are assigned to separate R-Tree subindices depending on their type and spanned layers, reducing * overlap and improving search time. **/ @@ -47,30 +50,93 @@ public: PNS_INDEX(); ~PNS_INDEX(); + /** + * Function Add() + * + * Adds item to the spatial index. + */ void Add( PNS_ITEM* aItem ); + + /** + * Function Remove() + * + * Removes an item from the spatial index. + */ void Remove( PNS_ITEM* aItem ); + + /** + * Function Add() + * + * Replaces one item with another. + */ void Replace( PNS_ITEM* aOldItem, PNS_ITEM* aNewItem ); + /** + * Function Query() + * + * Searches items in the index that are in proximity of aItem. + * For each item, function object aVisitor is called. Only items on + * overlapping layers are considered. + * + * @param aItem item to search against + * @param aMinDistance proximity distance (wrs to the item's shape) + * @param aVisitor function object called on each found item. Return + false from the visitor to stop searching. + * @return number of items found. + */ template - int Query( const PNS_ITEM* aItem, int aMinDistance, Visitor& v ); + int Query( const PNS_ITEM* aItem, int aMinDistance, Visitor& aVisitor ); + /** + * Function Query() + * + * Searches items in the index that are in proximity of aShape. + * For each item, function object aVisitor is called. Treats all + * layers as colliding. + * + * @param aShape shape to search against + * @param aMinDistance proximity distance (wrs to the item's shape) + * @param aVisitor function object called on each found item. Return + false from the visitor to stop searching. + * @return number of items found. + */ template - int Query( const SHAPE* aShape, int aMinDistance, Visitor& v ); + int Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ); + /** + * Function Clear() + * + * Removes all items from the index. + */ void Clear(); + /** + * Function GetItemsForNet() + * + * Returns list of all items in a given net. + */ NetItemsList* GetItemsForNet( int aNet ); - - ItemSet::iterator begin() { return m_allItems.begin(); } - ItemSet::iterator end() { return m_allItems.end(); } - + + /** + * Function Contains() + * + * Returns true if item aItem exists in the index. + */ bool Contains( PNS_ITEM* aItem ) const { return m_allItems.find( aItem ) != m_allItems.end(); } + /** + * Function Size() + * + * Returns number of items stored in the index. + */ int Size() const { return m_allItems.size(); } + ItemSet::iterator begin() { return m_allItems.begin(); } + ItemSet::iterator end() { return m_allItems.end(); } + private: static const int MaxSubIndices = 64; static const int SI_Multilayer = 2; @@ -81,7 +147,7 @@ private: static const int SI_PadsBottom = 1; template - int querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& v ); + int querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ); ItemShapeIndex* getSubindex( const PNS_ITEM* aItem ); @@ -101,9 +167,9 @@ PNS_INDEX::ItemShapeIndex* PNS_INDEX::getSubindex( const PNS_ITEM* aItem ) { int idx_n = -1; - const PNS_LAYERSET l = aItem->GetLayers(); + const PNS_LAYERSET l = aItem->Layers(); - switch( aItem->GetKind() ) + switch( aItem->Kind() ) { case PNS_ITEM::VIA: idx_n = SI_Multilayer; @@ -113,9 +179,9 @@ PNS_INDEX::ItemShapeIndex* PNS_INDEX::getSubindex( const PNS_ITEM* aItem ) { if( l.IsMultilayer() ) idx_n = SI_Multilayer; - else if( l.Start() == 0 ) // fixme: use kicad layer codes + else if( l.Start() == LAYER_N_BACK) // fixme: use kicad layer codes idx_n = SI_PadsTop; - else if( l.Start() == 15 ) + else if( l.Start() == LAYER_N_FRONT ) idx_n = SI_PadsBottom; break; @@ -143,9 +209,11 @@ void PNS_INDEX::Add( PNS_ITEM* aItem ) { ItemShapeIndex* idx = getSubindex( aItem ); + + idx->Add( aItem ); m_allItems.insert( aItem ); - int net = aItem->GetNet(); + int net = aItem->Net(); if( net >= 0 ) { @@ -161,7 +229,7 @@ void PNS_INDEX::Remove( PNS_ITEM* aItem ) idx->Remove( aItem ); m_allItems.erase( aItem ); - int net = aItem->GetNet(); + int net = aItem->Net(); if( net >= 0 && m_netMap.find( net ) != m_netMap.end() ) m_netMap[net].remove( aItem ); @@ -176,43 +244,43 @@ void PNS_INDEX::Replace( PNS_ITEM* aOldItem, PNS_ITEM* aNewItem ) template -int PNS_INDEX::querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& v ) +int PNS_INDEX::querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ) { if( !m_subIndices[index] ) return 0; - return m_subIndices[index]->Query( aShape, aMinDistance, v, false ); + return m_subIndices[index]->Query( aShape, aMinDistance, aVisitor, false ); } template -int PNS_INDEX::Query( const PNS_ITEM* aItem, int aMinDistance, Visitor& v ) +int PNS_INDEX::Query( const PNS_ITEM* aItem, int aMinDistance, Visitor& aVisitor ) { - const SHAPE* shape = aItem->GetShape(); + const SHAPE* shape = aItem->Shape(); int total = 0; - total += querySingle( SI_Multilayer, shape, aMinDistance, v ); + total += querySingle( SI_Multilayer, shape, aMinDistance, aVisitor ); - const PNS_LAYERSET layers = aItem->GetLayers(); + const PNS_LAYERSET layers = aItem->Layers(); if( layers.IsMultilayer() ) { - total += querySingle( SI_PadsTop, shape, aMinDistance, v ); - total += querySingle( SI_PadsBottom, shape, aMinDistance, v ); + total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor ); + total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor ); for( int i = layers.Start(); i <= layers.End(); ++i ) - total += querySingle( SI_Traces + 2 * i + SI_SegStraight, shape, aMinDistance, v ); + total += querySingle( SI_Traces + 2 * i + SI_SegStraight, shape, aMinDistance, aVisitor ); } else { int l = layers.Start(); - if( l == 0 ) - total += querySingle( SI_PadsTop, shape, aMinDistance, v ); - else if( l == 15 ) - total += querySingle( SI_PadsBottom, shape, aMinDistance, v ); + if( l == LAYER_N_BACK ) + total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor ); + else if( l == LAYER_N_FRONT ) + total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor ); - total += querySingle( SI_Traces + 2 * l + SI_SegStraight, shape, aMinDistance, v ); + total += querySingle( SI_Traces + 2 * l + SI_SegStraight, shape, aMinDistance, aVisitor ); } return total; @@ -220,12 +288,12 @@ int PNS_INDEX::Query( const PNS_ITEM* aItem, int aMinDistance, Visitor& v ) template -int PNS_INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& v ) +int PNS_INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ) { int total = 0; for( int i = 0; i < MaxSubIndices; i++ ) - total += querySingle( i, aShape, aMinDistance, v ); + total += querySingle( i, aShape, aMinDistance, aVisitor ); return total; } diff --git a/pcbnew/router/pns_item.cpp b/pcbnew/router/pns_item.cpp index 68e04a41c0..6ec4b77989 100644 --- a/pcbnew/router/pns_item.cpp +++ b/pcbnew/router/pns_item.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include "pns_item.h" @@ -32,7 +32,7 @@ bool PNS_ITEM::collideSimple( const PNS_ITEM* aOther, int aClearance, bool aNeed if( !m_layers.Overlaps( aOther->m_layers ) ) return false; - return GetShape()->Collide( aOther->GetShape(), aClearance ); + return Shape()->Collide( aOther->Shape(), aClearance ); // fixme: MTV } @@ -50,7 +50,7 @@ bool PNS_ITEM::Collide( const PNS_ITEM* aOther, int aClearance, bool aNeedMTV, const PNS_LINE* line = static_cast( aOther ); if( line->EndsWithVia() ) - return collideSimple( &line->GetVia(), aClearance - line->GetWidth() / 2, aNeedMTV, + return collideSimple( &line->Via(), aClearance - line->Width() / 2, aNeedMTV, aMTV ); } @@ -58,7 +58,7 @@ bool PNS_ITEM::Collide( const PNS_ITEM* aOther, int aClearance, bool aNeedMTV, } -const std::string PNS_ITEM::GetKindStr() const +const std::string PNS_ITEM::KindStr() const { switch( m_kind ) { @@ -85,4 +85,5 @@ const std::string PNS_ITEM::GetKindStr() const PNS_ITEM::~PNS_ITEM() { + } diff --git a/pcbnew/router/pns_item.h b/pcbnew/router/pns_item.h index c375a7ada5..0bc03b8ba7 100644 --- a/pcbnew/router/pns_item.h +++ b/pcbnew/router/pns_item.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_ITEM_H @@ -26,18 +26,25 @@ #include #include +#include "trace.h" + #include "pns_layerset.h" class BOARD_CONNECTED_ITEM; class PNS_NODE; +enum LineMarker { + MK_HEAD = ( 1 << 0 ), + MK_VIOLATION = ( 1 << 3 ), + MK_LOCKED = ( 1 << 4 ) +}; + /** * Class PNS_ITEM * * Base class for PNS router board items. Implements the shared properties of all PCB items - * net, spanned layers, geometric shape & refererence to owning model. */ - class PNS_ITEM { public: @@ -60,8 +67,9 @@ public: m_movable = true; m_kind = aKind; m_parent = NULL; - m_world = NULL; m_owner = NULL; + m_marker = 0; + m_rank = -1; } PNS_ITEM( const PNS_ITEM& aOther ) @@ -70,61 +78,203 @@ public: m_net = aOther.m_net; m_movable = aOther.m_movable; m_kind = aOther.m_kind; - m_world = aOther.m_world; m_parent = aOther.m_parent; m_owner = NULL; + m_marker = aOther.m_marker; + m_rank = aOther.m_rank; } virtual ~PNS_ITEM(); - virtual PNS_ITEM* Clone() const = 0; + /** + * Function Clone() + * + * Returns a deep copy of the item + */ + virtual PNS_ITEM* Clone( ) const = 0; - ///> Returns a convex polygon "hull" of a the item, that is used as the walkaround - /// path. - /// aClearance defines how far from the body of the item the hull should be, - /// aWalkaroundThickness is the width of the line that walks around this hull. + /* + * Function Hull() + * + * Returns a convex polygon "hull" of a the item, that is used as the walk-around + * path. + * @param aClearance defines how far from the body of the item the hull should be, + * @param aWalkaroundThickness is the width of the line that walks around this hull. + */ virtual const SHAPE_LINE_CHAIN Hull( int aClearance = 0, int aWalkaroundThickness = 0 ) const { return SHAPE_LINE_CHAIN(); }; - PnsKind GetKind() const { return m_kind; } - bool OfKind( int aKind ) const { return (aKind & m_kind) != 0; } + /** + * Function Kind() + * + * Returns the type (kind) of the item + */ + PnsKind Kind() const + { + return m_kind; + } + + /** + * Function OfKind() + * + * Returns true if the item's type matches the mask aKindMask. + */ + bool OfKind( int aKindMask ) const + { + return (aKindMask & m_kind) != 0; + } - const std::string GetKindStr() const; + /** + * Function KindStr() + * + * Returns the kind of the item, as string + */ + const std::string KindStr() const; - ///> Gets/Sets the corresponding parent object in the host application's model (pcbnew) - void SetParent( BOARD_CONNECTED_ITEM* aParent ) { m_parent = aParent; } - BOARD_CONNECTED_ITEM* GetParent() const { return m_parent; } + /** + * Function SetParent() + * + * Sets the corresponding parent object in the host application's model. + */ + void SetParent( BOARD_CONNECTED_ITEM* aParent ) + { + m_parent = aParent; + } + + /** + * Function Parent() + * + * Returns the corresponding parent object in the host application's model. + */ + BOARD_CONNECTED_ITEM* Parent() const + { + return m_parent; + } - ///> Net accessors - int GetNet() const { return m_net; } - void SetNet( int aNet ) { m_net = aNet; } + /** + * Function SetNet() + * + * Sets the item's net to aNet + */ + void SetNet( int aNet ) + { + m_net = aNet; + } - ///> Layers accessors - const PNS_LAYERSET& GetLayers() const { return m_layers; } - void SetLayers( const PNS_LAYERSET& aLayers ) { m_layers = aLayers; } + /** + * Function Net() + * + * Returns the item's net. + */ + int Net() const + { + return m_net; + } + + /** + * Function SetLayers() + * + * Sets the layers spanned by the item to aLayers. + */ + void SetLayers( const PNS_LAYERSET& aLayers ) + { + m_layers = aLayers; + } + + /** + * Function SetLayer() + * + * Sets the layers spanned by the item to a single layer aLayer. + */ void SetLayer( int aLayer ) { m_layers = PNS_LAYERSET( aLayer, aLayer ); } - ///> Ownership management. An item can belong to a single PNS_NODE or stay unowned. - void SetOwner( PNS_NODE* aOwner ) { m_owner = aOwner; } - bool BelongsTo( PNS_NODE* aNode ) const { return m_owner == aNode; } - PNS_NODE* GetOwner() const { return m_owner; } + /** + * Function Layers() + * + * Returns the contiguous set of layers spanned by the item. + */ + const PNS_LAYERSET& Layers() const + { + return m_layers; + } + + /** + * Function Layer() + * + * Returns the item's layer, for single-layered items only. + */ + virtual int Layer() const + { + return Layers().Start(); + } - ///> Sets the world that is used for collision resolution. - void SetWorld( PNS_NODE* aWorld ) { m_world = aWorld; } - PNS_NODE* GetWorld() const { return m_world; } + /** + * Function LayersOverlap() + * + * Returns true if the set of layers spanned by aOther overlaps our + * layers. + */ + bool LayersOverlap( const PNS_ITEM *aOther ) const + { + return Layers().Overlaps( aOther->Layers() ); + } - ///> Collision function. Checks if the item aOther is closer to us than - /// aClearance and returns true if so. It can also calculate a minimum translation vector that - /// resolves the collision if needed. + /** + * Functon SetOwner() + * + * Sets the node that owns this item. An item can belong to a single + * PNS_NODE or stay unowned. + */ + void SetOwner( PNS_NODE* aOwner ) + { + m_owner = aOwner; + } + + /** + * Function BelongsTo() + * + * Returns true if the item is owned by the node aNode. + */ + bool BelongsTo( PNS_NODE* aNode ) const + { + return m_owner == aNode; + } + + /** + * Function Owner() + * + * Returns the owner of this item, or NULL if there's none. + */ + PNS_NODE* Owner() const { return m_owner; } + + + /** + * Function Collide() + * + * Checks for a collision (clearance violation) with between us and item aOther. + * Collision checking takes all PCB stuff into accound (layers, nets, DRC rules). + * Optionally returns a minimum translation vector for force propagation + * algorithm. + * + * @param aOther item to check collision against + * @param aClearance desired clearance + * @param aNeedMTV when true, the minimum translation vector is calculated + * @param aMTV the minimum translation vector + * @param true, if a collision was found. + */ virtual bool Collide( const PNS_ITEM* aOther, int aClearance, bool aNeedMTV, VECTOR2I& aMTV ) const; - ///> A shortcut without MTV calculation + /** + * Function Collide() + * + * A shortcut for PNS_ITEM::Colllide() without MTV stuff. + */ bool Collide( const PNS_ITEM* aOther, int aClearance ) const { VECTOR2I dummy; @@ -132,26 +282,68 @@ public: return Collide( aOther, aClearance, false, dummy ); } - ///> Returns the geometric shape of the item - virtual const SHAPE* GetShape() const + /** + * Function Shape() + * + * Returns the geometrical shape of the item. Used + * for collision detection & spatial indexing. + */ + virtual const SHAPE* Shape() const { return NULL; } + virtual void Mark(int aMarker) + { + m_marker = aMarker; + } + + virtual void Unmark () + { + m_marker = 0; + } + + virtual int Marker() const + { + return m_marker; + } + + virtual void SetRank ( int aRank ) + { + m_rank = aRank; + } + + virtual int Rank() const + { + return m_rank; + } + + virtual VECTOR2I Anchor(int n) const + { + return VECTOR2I (); + }; + + virtual int AnchorCount() const + { + return 0; + } + private: + bool collideSimple( const PNS_ITEM* aOther, int aClearance, bool aNeedMTV, VECTOR2I& aMTV ) const; protected: - PnsKind m_kind; + PnsKind m_kind; - BOARD_CONNECTED_ITEM* m_parent; - PNS_NODE* m_world; - PNS_NODE* m_owner; - PNS_LAYERSET m_layers; + BOARD_CONNECTED_ITEM *m_parent; + PNS_NODE *m_owner; + PNS_LAYERSET m_layers; - bool m_movable; - int m_net; + bool m_movable; + int m_net; + int m_marker; + int m_rank; }; #endif // __PNS_ITEM_H diff --git a/pcbnew/router/pns_itemset.cpp b/pcbnew/router/pns_itemset.cpp index ebe69dc555..fccc5eccae 100644 --- a/pcbnew/router/pns_itemset.cpp +++ b/pcbnew/router/pns_itemset.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include @@ -23,16 +23,30 @@ #include "pns_itemset.h" -PNS_ITEMSET::PNS_ITEMSET() +PNS_ITEMSET::PNS_ITEMSET( PNS_ITEM *aInitialItem ) { + if(aInitialItem) + m_items.push_back(aInitialItem); } PNS_ITEMSET::~PNS_ITEMSET() { + Clear(); } +void PNS_ITEMSET::Clear() +{ + BOOST_FOREACH(PNS_ITEM *item, m_ownedItems) + { + delete item; + } + + m_items.clear(); + m_ownedItems.clear(); +} + PNS_ITEMSET& PNS_ITEMSET::FilterLayers( int aStart, int aEnd ) { ItemVector newItems; @@ -45,7 +59,7 @@ PNS_ITEMSET& PNS_ITEMSET::FilterLayers( int aStart, int aEnd ) BOOST_FOREACH( PNS_ITEM * item, m_items ) - if( item->GetLayers().Overlaps( l ) ) + if( item->Layers().Overlaps( l ) ) newItems.push_back( item ); m_items = newItems; @@ -59,7 +73,7 @@ PNS_ITEMSET& PNS_ITEMSET::FilterKinds( int aKindMask ) BOOST_FOREACH( PNS_ITEM * item, m_items ) - if( item->GetKind() & aKindMask ) + if( item->OfKind ( aKindMask ) ) newItems.push_back( item ); m_items = newItems; @@ -73,7 +87,7 @@ PNS_ITEMSET& PNS_ITEMSET::FilterNet( int aNet ) BOOST_FOREACH( PNS_ITEM * item, m_items ) - if( item->GetNet() == aNet ) + if( item->Net() == aNet ) newItems.push_back( item ); m_items = newItems; diff --git a/pcbnew/router/pns_itemset.h b/pcbnew/router/pns_itemset.h index da2c4a6634..9a6aaab3a8 100644 --- a/pcbnew/router/pns_itemset.h +++ b/pcbnew/router/pns_itemset.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_ITEMSET_H @@ -37,10 +37,25 @@ class PNS_ITEMSET public: typedef std::vector ItemVector; - PNS_ITEMSET(); + PNS_ITEMSET( PNS_ITEM *aInitialItem = NULL ); + + PNS_ITEMSET (const PNS_ITEMSET &aOther ) + { + m_items = aOther.m_items; + m_ownedItems = ItemVector(); + } + + const PNS_ITEMSET& operator= (const PNS_ITEMSET &aOther) + { + m_items = aOther.m_items; + m_ownedItems = ItemVector(); + return *this; + } + ~PNS_ITEMSET(); ItemVector& Items() { return m_items; } + const ItemVector& CItems() const { return m_items; } PNS_ITEMSET& FilterLayers( int aStart, int aEnd = -1 ); PNS_ITEMSET& FilterKinds( int aKindMask ); @@ -55,8 +70,17 @@ public: PNS_ITEM* Get( int index ) const { return m_items[index]; } + void Clear(); + + void AddOwned ( PNS_ITEM *aItem ) + { + m_items.push_back( aItem ); + m_ownedItems.push_back( aItem ); + } + private: ItemVector m_items; + ItemVector m_ownedItems; }; #endif diff --git a/pcbnew/router/pns_joint.h b/pcbnew/router/pns_joint.h index 4fd63e177a..699481538f 100644 --- a/pcbnew/router/pns_joint.h +++ b/pcbnew/router/pns_joint.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_JOINT_H @@ -72,7 +72,7 @@ public: m_layers = b.m_layers; } - PNS_ITEM* Clone() const + PNS_ITEM* Clone ( ) const { assert( false ); return NULL; @@ -85,15 +85,15 @@ public: if( m_linkedItems.size() != 2 ) return false; - if( m_linkedItems[0]->GetKind() != SEGMENT || - m_linkedItems[1]->GetKind() != SEGMENT ) + if( m_linkedItems[0]->Kind() != SEGMENT || + m_linkedItems[1]->Kind() != SEGMENT ) return false; PNS_SEGMENT* seg1 = static_cast( m_linkedItems[0] ); PNS_SEGMENT* seg2 = static_cast( m_linkedItems[1] ); - // joints between segments of different widths are not trivial. - return seg1->GetWidth() == seg2->GetWidth(); + // joints between segments of different widths are not considered trivial. + return seg1->Width() == seg2->Width(); } ///> Links the joint to a given board item (when it's added to the PNS_NODE) @@ -131,11 +131,35 @@ public: return static_cast( m_linkedItems[m_linkedItems[0] == aCurrent ? 1 : 0] ); } + PNS_VIA *Via() + { + for( LinkedItems::iterator i = m_linkedItems.begin(); + i != m_linkedItems.end(); ++i ) + if( (*i)->Kind() == PNS_ITEM::VIA ) + return (PNS_VIA *)(*i); + return NULL; + } + /// trivial accessors - const HashTag& GetTag() const { return m_tag; } - const VECTOR2I& GetPos() const { return m_tag.pos; } - int GetNet() const { return m_tag.net; } - LinkedItems& GetLinkList() { return m_linkedItems; }; + const HashTag& Tag() const + { + return m_tag; + } + + const VECTOR2I& Pos() const + { + return m_tag.pos; + } + + int Net() const + { + return m_tag.net; + } + + LinkedItems& LinkList() + { + return m_linkedItems; + } ///> Returns the number of linked items of types listed in aMask. int LinkCount( int aMask = -1 ) const @@ -144,7 +168,7 @@ public: for( LinkedItems::const_iterator i = m_linkedItems.begin(); i != m_linkedItems.end(); ++i ) - if( (*i)->GetKind() & aMask ) + if( (*i)->Kind() & aMask ) n++; return n; diff --git a/pcbnew/router/pns_layerset.h b/pcbnew/router/pns_layerset.h index eedc00e31e..d99ef57040 100644 --- a/pcbnew/router/pns_layerset.h +++ b/pcbnew/router/pns_layerset.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_LAYERSET_H diff --git a/pcbnew/router/pns_line.cpp b/pcbnew/router/pns_line.cpp index 63011b6b75..8cc7bcea87 100644 --- a/pcbnew/router/pns_line.cpp +++ b/pcbnew/router/pns_line.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include @@ -29,262 +29,117 @@ #include "pns_utils.h" #include "pns_router.h" +#include + using boost::optional; -PNS_LINE* PNS_LINE::Clone() const +PNS_LINE::PNS_LINE( const PNS_LINE& aOther ) : + PNS_ITEM( aOther ), + m_line( aOther.m_line ), + m_width( aOther.m_width ) { - PNS_LINE* l = new PNS_LINE(); + m_net = aOther.m_net; + m_movable = aOther.m_movable; + m_layers = aOther.m_layers; + m_owner = aOther.m_owner; + m_via = aOther.m_via; + m_hasVia = aOther.m_hasVia; + m_marker = aOther.m_marker; + m_rank = aOther.m_rank; - l->m_line = m_line; - l->m_width = m_width; - l->m_layers = m_layers; - l->m_net = m_net; - l->m_movable = m_movable; - l->m_segmentRefs = NULL; - l->m_hasVia = m_hasVia; - l->m_via = m_via; + copyLinks ( &aOther ); +} +PNS_LINE::~PNS_LINE() +{ + if( m_segmentRefs ) + delete m_segmentRefs; +}; + + +const PNS_LINE& PNS_LINE :: operator= (const PNS_LINE& aOther) +{ + m_line = aOther.m_line; + m_width = aOther.m_width; + m_net = aOther.m_net; + m_movable = aOther.m_movable; + m_owner = aOther.m_owner; + m_layers = aOther.m_layers; + m_via = aOther.m_via; + m_hasVia = aOther.m_hasVia; + m_marker = aOther.m_marker; + m_rank = aOther.m_rank; + + copyLinks ( &aOther ); + + return *this; +} + +PNS_LINE* PNS_LINE::Clone( ) const +{ + PNS_LINE* l = new PNS_LINE( *this ); return l; } - -PNS_LINE* PNS_LINE::CloneProperties() const +void PNS_LINE::Mark(int aMarker) { - PNS_LINE* l = new PNS_LINE(); + m_marker = aMarker; + + if(m_segmentRefs) + { + BOOST_FOREACH( PNS_SEGMENT *s, *m_segmentRefs ) + s->Mark(aMarker); + } +} - l->m_width = m_width; - l->m_layers = m_layers; - l->m_net = m_net; - l->m_movable = m_movable; +void PNS_LINE::Unmark () +{ + if(m_segmentRefs) + { + BOOST_FOREACH( PNS_SEGMENT *s, *m_segmentRefs ) + s->Unmark(); + } + m_marker = 0; +} - return l; +int PNS_LINE::Marker()const +{ + int marker = m_marker; + if(m_segmentRefs) + { + BOOST_FOREACH( PNS_SEGMENT *s, *m_segmentRefs ) + marker |= s->Marker(); + } + return marker; +} + +void PNS_LINE::copyLinks( const PNS_LINE *aParent ) +{ + if(aParent->m_segmentRefs == NULL) + { + m_segmentRefs = NULL; + return; + } + + m_segmentRefs = new SegmentRefs(); + *m_segmentRefs = *aParent->m_segmentRefs; } -PNS_SEGMENT* PNS_SEGMENT::Clone() const +PNS_SEGMENT* PNS_SEGMENT::Clone( ) const { PNS_SEGMENT* s = new PNS_SEGMENT; - s->m_width = m_width; + s->m_seg = m_seg; s->m_net = m_net; - s->m_shape = m_shape; s->m_layers = m_layers; + s->m_marker = m_marker; + s->m_rank = m_rank; + s->m_owner = m_owner; - return s; // assert(false); + return s; } - -#if 1 -bool PNS_LINE::MergeObtuseSegments() -{ - int step = m_line.PointCount() - 3; - int iter = 0; - - int segs_pre = m_line.SegmentCount(); - - if( step < 0 ) - return false; - - SHAPE_LINE_CHAIN current_path( m_line ); - - while( 1 ) - { - iter++; - int n_segs = current_path.SegmentCount(); - int max_step = n_segs - 2; - - if( step > max_step ) - step = max_step; - - if( step < 2 ) - { - m_line = current_path; - return current_path.SegmentCount() < segs_pre; - } - - bool found_anything = false; - int n = 0; - - while( n < n_segs - step ) - { - const SEG s1 = current_path.CSegment( n ); - const SEG s2 = current_path.CSegment( n + step ); - SEG s1opt, s2opt; - - if( DIRECTION_45( s1 ).IsObtuse( DIRECTION_45( s2 ) ) ) - { - VECTOR2I ip = *s1.IntersectLines( s2 ); - - if( s1.Distance( ip ) <= 1 || s2.Distance( ip ) <= 1 ) - { - s1opt = SEG( s1.A, ip ); - s2opt = SEG( ip, s2.B ); - } - else - { - s1opt = SEG( s1.A, ip ); - s2opt = SEG( ip, s2.B ); - } - - - if( DIRECTION_45( s1opt ).IsObtuse( DIRECTION_45( s2opt ) ) ) - { - SHAPE_LINE_CHAIN opt_path; - opt_path.Append( s1opt.A ); - opt_path.Append( s1opt.B ); - opt_path.Append( s2opt.B ); - - PNS_LINE opt_track( *this, opt_path ); - - if( !m_world->CheckColliding( &opt_track, PNS_ITEM::ANY ) ) - { - current_path.Replace( s1.Index() + 1, s2.Index(), ip ); - n_segs = current_path.SegmentCount(); - found_anything = true; - break; - } - } - } - - n++; - } - - if( !found_anything ) - { - if( step <= 2 ) - { - m_line = current_path; - return m_line.SegmentCount() < segs_pre; - } - - step--; - } - } - - return m_line.SegmentCount() < segs_pre; -} - - -bool PNS_LINE::MergeSegments() -{ - int step = m_line.PointCount() - 3; - int iter = 0; - - int segs_pre = m_line.SegmentCount(); - - if( step < 0 ) - return false; - - SHAPE_LINE_CHAIN current_path( m_line ); - - while( 1 ) - { - iter++; - int n_segs = current_path.SegmentCount(); - int max_step = n_segs - 2; - - if( step > max_step ) - step = max_step; - - if( step < 2 ) - { - m_line = current_path; - return current_path.SegmentCount() < segs_pre; - } - - bool found_anything = false; - int n = 0; - - while( n < n_segs - step ) - { - const SEG s1 = current_path.CSegment( n ); - const SEG s2 = current_path.CSegment( n + step ); - SEG s1opt, s2opt; - - if( n > 0 ) - { - SHAPE_LINE_CHAIN path_straight = DIRECTION_45().BuildInitialTrace( s1.A, - s2.A, false ); - SHAPE_LINE_CHAIN path_diagonal = DIRECTION_45().BuildInitialTrace( s1.A, - s2.A, true ); - } - - if( DIRECTION_45( s1 ) == DIRECTION_45( s2 ) ) - { - if( s1.Collinear( s2 ) ) - { - // printf("Colinear: np %d step %d n1 %d n2 %d\n", n_segs, step, n, n+step); - - SHAPE_LINE_CHAIN opt_path; - opt_path.Append( s1.A ); - opt_path.Append( s2.B ); - - PNS_LINE tmp( *this, opt_path ); - - if( !m_world->CheckColliding( &tmp, PNS_ITEM::ANY ) ) - { - current_path.Remove( s1.Index() + 1, s2.Index() ); - n_segs = current_path.SegmentCount(); - found_anything = true; - break; - } - } - } - else if( DIRECTION_45( s1 ).IsObtuse( DIRECTION_45( s2 ) ) ) - { - VECTOR2I ip = *s1.IntersectLines( s2 ); - - if( s1.Distance( ip ) <= 1 || s2.Distance( ip ) <= 1 ) - { - s1opt = SEG( s1.A, ip ); - s2opt = SEG( ip, s2.B ); - } - else - { - s1opt = SEG( s1.A, ip ); - s2opt = SEG( ip, s2.B ); - } - - - if( DIRECTION_45( s1opt ).IsObtuse( DIRECTION_45( s2opt ) ) ) - { - SHAPE_LINE_CHAIN opt_path; - opt_path.Append( s1opt.A ); - opt_path.Append( s1opt.B ); - opt_path.Append( s2opt.B ); - - PNS_LINE opt_track( *this, opt_path ); - - if( !m_world->CheckColliding( &opt_track, PNS_ITEM::ANY ) ) - { - current_path.Replace( s1.Index() + 1, s2.Index(), ip ); - n_segs = current_path.SegmentCount(); - found_anything = true; - break; - } - } - } - - n++; - } - - if( !found_anything ) - { - if( step <= 2 ) - { - m_line = current_path; - return m_line.SegmentCount() < segs_pre; - } - - step--; - } - } - - return m_line.SegmentCount() < segs_pre; -} -#endif - - int PNS_LINE::CountCorners( int aAngles ) { int count = 0; @@ -306,247 +161,12 @@ int PNS_LINE::CountCorners( int aAngles ) return count; } - -// #define DUMP_TEST_CASES - -// fixme: damn f*****g inefficient and incredibly crappily written -void PNS_LINE::NewWalkaround( const SHAPE_LINE_CHAIN& aObstacle, - SHAPE_LINE_CHAIN& aPrePath, - SHAPE_LINE_CHAIN& aWalkaroundPath, - SHAPE_LINE_CHAIN& aPostPath, - bool aCw ) const -{ - typedef SHAPE_LINE_CHAIN::INTERSECTION INTERSECTION; - - SHAPE_LINE_CHAIN l_orig( m_line ); - SHAPE_LINE_CHAIN l_hull; - std::vector outside, on_edge, inside; - SHAPE_LINE_CHAIN path; - - std::vector isects; - - // don't calculate walkaround for empty lines - if( m_line.PointCount() < 2 ) - return; - -#ifdef DUMP_TEST_CASES - printf( "%s\n", m_line.Format().c_str() ); - printf( "%s\n", aObstacle.Format().c_str() ); -#endif - - aObstacle.Intersect( m_line, isects ); - - // printf("NewWalk intersectiosn :%d\n" ,isects.size()); - if( !aCw ) - l_hull = aObstacle.Reverse(); - else - l_hull = aObstacle; - - BOOST_FOREACH( INTERSECTION isect, isects ) { - l_orig.Split( isect.p ); - l_hull.Split( isect.p ); - } - - -#ifdef DUMP_TEST_CASES - printf( "%s\n", m_line.Format().c_str() ); - printf( "%s\n", aObstacle.Format().c_str() ); - printf( "%s\n", l_orig.Format().c_str() ); - printf( "%s\n", l_hull.Format().c_str() ); -#endif - - - // printf("Pts: line %d hull %d\n", l_orig.PointCount(), l_hull.PointCount()); - - int first_post = -1; - int last_pre = -1; - - for( int i = 0; i < l_orig.PointCount(); i++ ) - { - int ei = l_hull.Find( l_orig.CPoint( i ) ); - bool edge = ei >= 0; - bool in = l_hull.PointInside( l_orig.CPoint( i ) ) && !edge; - bool out = !( in || edge); - - outside.push_back( out ); - on_edge.push_back( edge ); - inside.push_back( in ); - } - - for( int i = l_orig.PointCount() - 1; i >= 1; i-- ) - if( inside[i] && outside[i - 1] ) - { - SHAPE_LINE_CHAIN::INTERSECTIONS ips; - l_hull.Intersect( SEG( l_orig.CPoint( i ), l_orig.CPoint( i - 1 ) ), ips ); - l_orig.Remove( i, -1 ); - l_orig.Append( ips[0].p ); - break; - } - else if( inside[i] && on_edge[i - 1] ) - { - l_orig.Remove( i, -1 ); - // n = i; - } - else if( !inside[i] ) - break; - - if( !outside.size() && on_edge.size() < 2 ) - return; - - for( int i = 0; i < l_orig.PointCount(); i++ ) - { - const VECTOR2I p = l_orig.Point( i ); - - if( outside[i] || ( on_edge[i] && i == ( l_orig.PointCount() - 1 ) ) ) - { - if( last_pre < 0 ) - aPrePath.Append( p ); - - path.Append( p ); - } - else if( on_edge[i] ) - { - int li = -1; - - if( last_pre < 0 ) - { - aPrePath.Append( p ); - last_pre = path.PointCount(); - } - - if( i == l_orig.PointCount() - 1 || outside[i + 1] ) - { - path.Append( p ); - } - else - { - int vi2 = l_hull.Find( l_orig.CPoint( i ) ); - - path.Append( l_hull.CPoint( vi2 ) ); - - for( int j = (vi2 + 1) % l_hull.PointCount(); - j != vi2; - j = (j + 1) % l_hull.PointCount() ) - { - path.Append( l_hull.CPoint( j ) ); - li = l_orig.Find( l_hull.CPoint( j ) ); - - if( li >= 0 && ( li == ( l_orig.PointCount() - 1 ) || - outside[li + 1] ) ) - break; - } - - if( li >= 0 ) - { - if( i >= li ) - break; - else - i = li; - } - } - - first_post = path.PointCount() - 1; - } - } - - if( last_pre < 0 && first_post < 0 ) - return; - - aWalkaroundPath = path.Slice( last_pre, first_post ); - - if( first_post >= 0 ) - aPostPath = path.Slice( first_post, -1 ); -} - - -bool PNS_LINE::onEdge( const SHAPE_LINE_CHAIN& obstacle, VECTOR2I p, int& ei, - bool& is_vertex ) const -{ - int vtx = obstacle.Find( p ); - - if( vtx >= 0 ) - { - ei = vtx; - is_vertex = true; - return true; - } - - for( int s = 0; s < obstacle.SegmentCount(); s++ ) - { - if( obstacle.CSegment( s ).Contains( p ) ) - { - ei = s; - is_vertex = false; - return true; - } - } - - return false; -} - - -bool PNS_LINE::walkScan( const SHAPE_LINE_CHAIN& aLine, const SHAPE_LINE_CHAIN& aObstacle, - bool aReverse, VECTOR2I& aIp, int& aIndexO, int& aIndexL, bool& aIsVertex ) const -{ - int sc = aLine.SegmentCount(); - - for( int i = 0; i < aLine.SegmentCount(); i++ ) - { - printf( "check-seg rev %d %d/%d %d\n", aReverse, i, sc, sc - 1 - i ); - SEG tmp = aLine.CSegment( aReverse ? sc - 1 - i : i ); - SEG s( tmp.A, tmp.B ); - - if( aReverse ) - { - s.A = tmp.B; - s.B = tmp.A; - } - - if( onEdge( aObstacle, s.A, aIndexO, aIsVertex ) ) - { - aIndexL = (aReverse ? sc - 1 - i : i); - aIp = s.A; - printf( "vertex %d on-%s %d\n", aIndexL, - aIsVertex ? "vertex" : "edge", aIndexO ); - return true; - } - - if( onEdge( aObstacle, s.B, aIndexO, aIsVertex ) ) - { - aIndexL = (aReverse ? sc - 1 - i - 1 : i + 1); - aIp = s.B; - printf( "vertex %d on-%s %d\n", aIndexL, - aIsVertex ? "vertex" : "edge", aIndexO ); - return true; - } - - SHAPE_LINE_CHAIN::INTERSECTIONS ips; - int n_is = aObstacle.Intersect( s, ips ); - - if( n_is > 0 ) - { - aIndexO = ips[0].our.Index(); - aIndexL = aReverse ? sc - 1 - i : i; - printf( "segment-%d intersects edge-%d\n", aIndexL, aIndexO ); - aIp = ips[0].p; - return true; - } - } - - return false; -} - - bool PNS_LINE::Walkaround( SHAPE_LINE_CHAIN aObstacle, SHAPE_LINE_CHAIN& aPre, SHAPE_LINE_CHAIN& aWalk, SHAPE_LINE_CHAIN& aPost, bool aCw ) const { - const SHAPE_LINE_CHAIN& line = GetCLine(); + const SHAPE_LINE_CHAIN& line ( CLine() ); VECTOR2I ip_start; - int index_o_start, index_l_start; VECTOR2I ip_end; - int index_o_end, index_l_end; - - bool is_vertex_start, is_vertex_end; if( line.SegmentCount() < 1 ) return false; @@ -555,92 +175,83 @@ bool PNS_LINE::Walkaround( SHAPE_LINE_CHAIN aObstacle, SHAPE_LINE_CHAIN& aPre, aObstacle.PointInside( line.CPoint( -1 ) ) ) return false; -// printf("forward:\n"); - bool found = walkScan( line, aObstacle, false, ip_start, index_o_start, - index_l_start, is_vertex_start ); - // printf("reverse:\n"); - found |= walkScan( line, aObstacle, true, ip_end, index_o_end, index_l_end, is_vertex_end ); + SHAPE_LINE_CHAIN::INTERSECTIONS ips, ips2; - if( !found || ip_start == ip_end ) + line.Intersect(aObstacle, ips); + + int nearest_dist = INT_MAX; + int farthest_dist = 0; + + SHAPE_LINE_CHAIN::INTERSECTION nearest, farthest; + + for(int i = 0; i < (int) ips.size(); i++) + { + const VECTOR2I p = ips[i].p; + int dist = line.PathLength(p); + + if(dist <= nearest_dist) + { + nearest_dist = dist; + nearest = ips[i]; + } + + if(dist >= farthest_dist) + { + farthest_dist = dist; + farthest = ips[i]; + } + } + + if(ips.size() <= 1 || nearest.p == farthest.p) { aPre = line; return true; } - aPre = line.Slice( 0, index_l_start ); - aPre.Append( ip_start ); + aPre = line.Slice( 0, nearest.our.Index() ); + aPre.Append( nearest.p ); + aPre.Simplify(); + aWalk.Clear(); - aWalk.Append( ip_start ); + aWalk.SetClosed(false); + aWalk.Append( nearest.p ); - if( aCw ) + int i = nearest.their.Index(); + + assert ( nearest.their.Index() >= 0 ); + assert ( farthest.their.Index() >= 0 ); + + assert( nearest_dist <= farthest_dist ); + + aObstacle.Split( nearest.p ); + aObstacle.Split( farthest.p ); + + int i_first = aObstacle.Find( nearest.p ); + int i_last = aObstacle.Find( farthest.p ); + + i = i_first; + + while (i != i_last) { - int is = ( index_o_start + 1 ) % aObstacle.PointCount(); - int ie = ( is_vertex_end ? index_o_end : index_o_end + 1 ) % aObstacle.PointCount(); - - while( 1 ) - { - printf( "is %d\n", is ); - aWalk.Append( aObstacle.CPoint( is ) ); - - if( is == ie ) - break; - - is++; - - if( is == aObstacle.PointCount() ) - is = 0; - } + aWalk.Append(aObstacle.CPoint(i)); + i += (aCw ? 1 : -1); + + if (i < 0) + i = aObstacle.PointCount() - 1; + else if (i == aObstacle.PointCount()) + i = 0; } - else - { - int is = index_o_start; - int ie = ( is_vertex_end ? index_o_end : index_o_end ) % aObstacle.PointCount(); - - while( 1 ) - { - printf( "is %d\n", is ); - aWalk.Append( aObstacle.CPoint( is ) ); - - if( is == ie ) - break; - - is--; - - if( is < 0 ) - is = aObstacle.PointCount() - 1; - } - } - - aWalk.Append( ip_end ); + + aWalk.Append( farthest.p ); + aWalk.Simplify(); aPost.Clear(); - aPost.Append( ip_end ); - aPost.Append( line.Slice( is_vertex_end ? index_l_end : index_l_end + 1, -1 ) ); - - // for(int i = (index_o_start + 1) % obstacle.PointCount(); - // i != (index_o_end + 1) % obstacle.PointCount(); i=(i+1) % obstacle.PointCount()) - // { - // printf("append %d\n", i); - // walk.Append(obstacle.CPoint(i)); - // } - + aPost.Append( farthest.p ); + aPost.Append( line.Slice( farthest.our.Index() + 1, -1 ) ); + aPost.Simplify(); return true; } - -void PNS_LINE::NewWalkaround( const SHAPE_LINE_CHAIN& aObstacle, - SHAPE_LINE_CHAIN& aPath, - bool aCw ) const -{ - SHAPE_LINE_CHAIN walk, post; - - NewWalkaround( aObstacle, aPath, walk, post, aCw ); - aPath.Append( walk ); - aPath.Append( post ); - aPath.Simplify(); -} - - void PNS_LINE::Walkaround( const SHAPE_LINE_CHAIN& aObstacle, SHAPE_LINE_CHAIN& aPath, bool aCw ) const @@ -656,38 +267,7 @@ void PNS_LINE::Walkaround( const SHAPE_LINE_CHAIN& aObstacle, const SHAPE_LINE_CHAIN PNS_SEGMENT::Hull( int aClearance, int aWalkaroundThickness ) const { - int d = aClearance + 10; - int x = (int)( 2.0 / ( 1.0 + M_SQRT2 ) * d ) + 2; - - const VECTOR2I a = m_shape.CPoint( 0 ); - const VECTOR2I b = m_shape.CPoint( 1 ); - - VECTOR2I dir = b - a; - - VECTOR2I p0 = dir.Perpendicular().Resize( d ); - - VECTOR2I ds = dir.Perpendicular().Resize( x / 2 ); - VECTOR2I pd = dir.Resize( x / 2 ); - VECTOR2I dp = dir.Resize( d ); - - SHAPE_LINE_CHAIN s; - - s.SetClosed( true ); - - s.Append( b + p0 + pd ); - s.Append( b + dp + ds ); - s.Append( b + dp - ds ); - s.Append( b - p0 + pd ); - s.Append( a - p0 - pd ); - s.Append( a - dp - ds ); - s.Append( a - dp + ds ); - s.Append( a + p0 - pd ); - - // make sure the hull outline is always clockwise - if( s.CSegment( 0 ).Side( a ) < 0 ) - return s.Reverse(); - else - return s; + return SegmentHull ( m_seg, aClearance, aWalkaroundThickness ); } @@ -723,8 +303,8 @@ const PNS_LINE PNS_LINE::ClipToNearestObstacle( PNS_NODE* aNode ) const if( obs ) { l.RemoveVia(); - int p = l.GetLine().Split( obs->ip_first ); - l.GetLine().Remove( p + 1, -1 ); + int p = l.Line().Split( obs->ip_first ); + l.Line().Remove( p + 1, -1 ); } return l; @@ -744,3 +324,404 @@ void PNS_LINE::ShowLinks() for( int i = 0; i < (int) m_segmentRefs->size(); i++ ) printf( "seg %d: %p\n", i, (*m_segmentRefs)[i] ); } + +SHAPE_LINE_CHAIN dragCornerInternal ( const SHAPE_LINE_CHAIN& origin, const VECTOR2I& aP ) +{ + optional picked; + int i; + + int d = 2; + + if(origin.CSegment(-1).Length() > 100000 * 30) // fixme: constant/parameter? + d = 1; + + for(i = origin.SegmentCount() - d; i >= 0; i--) + { + + DIRECTION_45 d_start (origin.CSegment(i)); + VECTOR2I p_start = origin.CPoint(i); + SHAPE_LINE_CHAIN paths [2]; + DIRECTION_45 dirs[2]; + DIRECTION_45 d_prev = (i > 0 ? DIRECTION_45(origin.CSegment(i-1)) : DIRECTION_45()); + + for(int j = 0; j < 2; j++) + { + paths [j] = d_start.BuildInitialTrace( p_start, aP, j ); + dirs [j] = DIRECTION_45(paths[j].CSegment(0)); + } + + + for( int j = 0; j < 2; j++) + if(dirs[j] == d_start) + { + picked = paths[j]; + break; + } + + if(picked) + break; + + for(int j = 0; j < 2; j++) + if (dirs[j].IsObtuse(d_prev)) + { + picked = paths[j]; + break; + } + if(picked) + break; + } + + if(picked) + { + SHAPE_LINE_CHAIN path = origin.Slice(0, i); + path.Append(*picked); + return path; + + } + + return DIRECTION_45().BuildInitialTrace(origin.CPoint(0), aP, true); +} + + + +void PNS_LINE::DragCorner ( const VECTOR2I& aP, int aIndex, int aSnappingThreshold ) +{ + SHAPE_LINE_CHAIN path; + + VECTOR2I snapped = snapDraggedCorner( m_line, aP, aIndex, aSnappingThreshold ); + + if( aIndex == 0) + path = dragCornerInternal( m_line.Reverse(), snapped ).Reverse(); + else if ( aIndex == m_line.SegmentCount() ) + path = dragCornerInternal( m_line, snapped ); + else { + // fixme: awkward behaviour for "outwards" drags + path = dragCornerInternal( m_line.Slice (0, aIndex), snapped ); + SHAPE_LINE_CHAIN path_rev = dragCornerInternal( m_line.Slice (aIndex, -1).Reverse(), snapped ).Reverse(); + path.Append(path_rev); + } + path.Simplify(); + m_line = path; +} + +VECTOR2I PNS_LINE::snapDraggedCorner( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I &aP, int aIndex, int aThreshold ) const +{ + int s_start = std::max(aIndex - 2, 0); + int s_end = std::min(aIndex + 2, aPath.SegmentCount() - 1); + + int i, j; + int best_dist = INT_MAX; + VECTOR2I best_snap = aP; + + if(aThreshold <= 0) + return aP; + + for(i = s_start; i <= s_end; i++) + { + const SEG& a = aPath.CSegment(i); + + for(j = s_start; j < i; j++) + { + const SEG& b = aPath.CSegment(j); + + if( ! (DIRECTION_45(a).IsObtuse(DIRECTION_45(b))) ) + continue; + + OPT_VECTOR2I ip = a.IntersectLines(b); + + if(ip) + { + int dist = (*ip - aP).EuclideanNorm(); + if( dist < aThreshold && dist < best_dist ) + { + best_dist = dist; + best_snap = *ip; + } + } + } + } + + return best_snap; +} + +VECTOR2I PNS_LINE::snapToNeighbourSegments( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I &aP, int aIndex, int aThreshold ) const +{ + VECTOR2I snap_p[2]; + DIRECTION_45 dragDir ( aPath.CSegment(aIndex) ); + int snap_d[2] = {-1, -1}; + + if( aThreshold == 0 ) + return aP; + + if(aIndex >= 2) + { + SEG s = aPath.CSegment(aIndex - 2); + if(DIRECTION_45(s) == dragDir) + snap_d[0] = s.LineDistance(aP); + snap_p[0] = s.A; + } + + if(aIndex < aPath.SegmentCount() - 2) + { + SEG s = aPath.CSegment(aIndex + 2); + if(DIRECTION_45(s) == dragDir) + snap_d[1] = s.LineDistance(aP); + snap_p[1] = s.A; + } + + VECTOR2I best = aP; + int minDist = INT_MAX; + + for(int i = 0; i < 2; i++) + if(snap_d[i] >= 0 && snap_d[i] < minDist && snap_d[i] <= aThreshold) + { + minDist = snap_d[i]; + best = snap_p[i]; + } + + return best; +} + + +void PNS_LINE::DragSegment ( const VECTOR2I& aP, int aIndex, int aSnappingThreshold ) +{ + SHAPE_LINE_CHAIN path (m_line); + VECTOR2I target (aP) ; + + SEG guideA[2], guideB[2]; + int index = aIndex; + + target = snapToNeighbourSegments( path, aP, aIndex, aSnappingThreshold ); + + if(index == 0) + { + path.Insert (0, path.CPoint(0)); + index++; + } + + if(index == path.SegmentCount()-1) + { + path.Insert(path.PointCount() - 1, path.CPoint(-1)); + } + + SEG dragged = path.CSegment(index); + DIRECTION_45 drag_dir (dragged); + + SEG s_prev = path.CSegment(index - 1); + SEG s_next = path.CSegment(index + 1); + + DIRECTION_45 dir_prev (s_prev); + DIRECTION_45 dir_next (s_next); + + if(dir_prev == drag_dir) + { + dir_prev = dir_prev.Left(); + path.Insert( index, path.CPoint(index) ); + index++; + } + + if(dir_next == drag_dir) + { + dir_next = dir_next.Right(); + path.Insert( index + 1, path.CPoint(index + 1) ); + } + + + s_prev = path.CSegment(index - 1); + s_next = path.CSegment(index + 1); + dragged = path.CSegment(index); + + bool lockEndpointA = true; + bool lockEndpointB = true; + + if(aIndex == 0) + { + if(!lockEndpointA) + guideA[0] = guideA[1] = SEG( dragged.A, dragged.A + drag_dir.Right().Right().ToVector() ); + else { + guideA[0] = SEG( dragged.A, dragged.A + drag_dir.Right().ToVector() ); + guideA[1] = SEG( dragged.A, dragged.A + drag_dir.Left().ToVector() ); + } + } else { + if(dir_prev.IsObtuse(drag_dir)) + { + guideA[0] = SEG( s_prev.A, s_prev.A + drag_dir.Left().ToVector() ); + guideA[1] = SEG( s_prev.A, s_prev.A + drag_dir.Right().ToVector() ); + } else + guideA[0] = guideA[1] = SEG( dragged.A, dragged.A + dir_prev.ToVector() ); + } + + if(aIndex == m_line.SegmentCount() - 1) + { + if(!lockEndpointB) + guideB[0] = guideB[1] = SEG( dragged.B, dragged.B + drag_dir.Right().Right().ToVector() ); + else { + guideB[0] = SEG( dragged.B, dragged.B + drag_dir.Right().ToVector() ); + guideB[1] = SEG( dragged.B, dragged.B + drag_dir.Left().ToVector() ); + } + } else { + if(dir_next.IsObtuse(drag_dir)) + { + guideB[0] = SEG( s_next.B, s_next.B + drag_dir.Left().ToVector() ); + guideB[1] = SEG( s_next.B, s_next.B + drag_dir.Right().ToVector() ); + } else + guideB[0] = guideB[1] = SEG( dragged.B, dragged.B + dir_next.ToVector() ); + } + + SEG s_current (target, target + drag_dir.ToVector()); + + int best_len = INT_MAX; + SHAPE_LINE_CHAIN best; + + for(int i = 0; i < 2; i++) + { + for(int j = 0; j < 2; j++) + { + OPT_VECTOR2I ip1 = s_current.IntersectLines(guideA[i]); + OPT_VECTOR2I ip2 = s_current.IntersectLines(guideB[j]); + + SHAPE_LINE_CHAIN np; + + if(!ip1 || !ip2) + continue; + + SEG s1 ( s_prev.A, *ip1 ); + SEG s2 ( *ip1, *ip2 ); + SEG s3 ( *ip2, s_next.B ); + + OPT_VECTOR2I ip; + + if(ip = s1.Intersect(s_next)) + { + np.Append ( s1.A ); + np.Append ( *ip ); + np.Append ( s_next.B ); + } else if(ip = s3.Intersect(s_prev)) + { + np.Append ( s_prev.A ); + np.Append ( *ip ); + np.Append ( s3.B ); + } else if(ip = s1.Intersect(s3)) + { + np.Append( s_prev.A ); + np.Append( *ip ); + np.Append( s_next.B ); + } else { + np.Append( s_prev.A ); + np.Append( *ip1 ); + np.Append( *ip2 ); + np.Append( s_next.B ); + } + + if(np.Length() < best_len) + { + best_len = np.Length(); + best = np; + } + + } + } + + if(!lockEndpointA && aIndex == 0) + best.Remove(0, 0); + if(!lockEndpointB && aIndex == m_line.SegmentCount() - 1) + best.Remove(-1, -1); + + + if(m_line.PointCount() == 1) + m_line = best; + else if (aIndex == 0) + m_line.Replace(0, 1, best); + else if (aIndex == m_line.SegmentCount() - 1) + m_line.Replace(-2, -1, best); + else + m_line.Replace(aIndex, aIndex + 1, best); + + m_line.Simplify(); +} + + +bool PNS_LINE::CompareGeometry( const PNS_LINE& aOther ) +{ + return m_line.CompareGeometry(aOther.m_line); +} + +void PNS_LINE::Reverse() +{ + m_line = m_line.Reverse(); + if(m_segmentRefs) + std::reverse(m_segmentRefs->begin(), m_segmentRefs->end() ); +} + +void PNS_LINE::AppendVia(const PNS_VIA& aVia) +{ + if(aVia.Pos() == m_line.CPoint(0)) + { + + Reverse(); + } + + m_hasVia = true; + m_via = aVia; + m_via.SetNet( m_net ); +} + +void PNS_LINE::SetRank(int aRank) +{ + m_rank = aRank; + if(m_segmentRefs) + { + BOOST_FOREACH( PNS_SEGMENT *s, *m_segmentRefs ) + s->SetRank(aRank); + } +} + +int PNS_LINE::Rank() const +{ + int min_rank = INT_MAX; + int rank; + if(m_segmentRefs) + { + BOOST_FOREACH( PNS_SEGMENT *s, *m_segmentRefs ) + min_rank = std::min(min_rank, s->Rank()); + rank = (min_rank == INT_MAX) ? -1 : min_rank; + } else { + rank = m_rank; + } + + return rank; +} + +void PNS_LINE::ClipVertexRange ( int aStart, int aEnd ) +{ + m_line = m_line.Slice (aStart, aEnd); + + if(m_segmentRefs) + { + SegmentRefs *snew = new SegmentRefs( m_segmentRefs->begin() + aStart, m_segmentRefs->begin() + aEnd ); + + delete m_segmentRefs; + m_segmentRefs = snew; + } +} + +bool PNS_LINE::HasLoops() const +{ + + for(int i = 0; i < PointCount(); i++) + for(int j = 0; j < PointCount(); j++) + { + if( (std::abs(i-j) > 1) && CPoint(i) == CPoint(j)) + return true; + } + + return false; +} + +void PNS_LINE::ClearSegmentLinks() +{ + if(m_segmentRefs) + delete m_segmentRefs; + m_segmentRefs = NULL; +} + diff --git a/pcbnew/router/pns_line.h b/pcbnew/router/pns_line.h index ebd07750cf..520409d937 100644 --- a/pcbnew/router/pns_line.h +++ b/pcbnew/router/pns_line.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_LINE_H @@ -42,60 +42,42 @@ class PNS_VIA; * vias, pads, junctions between multiple traces or two traces different widths * and combinations of these). PNS_LINEs are NOT stored in the model (PNS_NODE). * Instead, they are assembled on-the-fly, based on a via/pad/segment that - * belongs/begins them. + * belongs to/starts/ends them. * * PNS_LINEs can be either loose (consisting of segments that do not belong to * any PNS_NODE) or owned (with segments taken from a PNS_NODE) - these are * returned by PNS_NODE::AssembleLine and friends. * - * A PNS_LINE may have a PNS_VIA attached at its and - this is used by via + * A PNS_LINE may have a PNS_VIA attached at its end (i.e. the last point) - this is used by via * dragging/force propagation stuff. */ +#define PNS_HULL_MARGIN 10 + class PNS_LINE : public PNS_ITEM { public: - typedef std::vector LinkedSegments; - - PNS_LINE() : - PNS_ITEM( LINE ) - { - m_segmentRefs = NULL; - m_hasVia = false; - m_affectedRangeStart = -1; - }; - - PNS_LINE( int aLayer, int aWidth, const SHAPE_LINE_CHAIN& aLine ) : - PNS_ITEM( LINE ) - { - m_line = aLine; - m_width = aWidth; - m_segmentRefs = NULL; - m_hasVia = false; - m_affectedRangeStart = -1; - SetLayer( aLayer ); - } - - PNS_LINE( const PNS_LINE& aOther ) : - PNS_ITEM( aOther ), - m_line( aOther.m_line ), - m_width( aOther.m_width ) - { - m_net = aOther.m_net; - m_movable = aOther.m_movable; - m_world = aOther.m_world; - m_layers = aOther.m_layers; - m_segmentRefs = NULL; - m_via = aOther.m_via; - m_hasVia = aOther.m_hasVia; - m_affectedRangeStart = -1; - } + typedef std::vector SegmentRefs; /** * Constructor - * copies properties (net, layers from a base line), and replaces the shape - * by aLine + * Makes an empty line. + */ + PNS_LINE() : PNS_ITEM (LINE) + { + m_segmentRefs = NULL; + m_hasVia = false; + } + + + PNS_LINE( const PNS_LINE& aOther ) ; + + /** + * Constructor + * Copies properties (net, layers, etc.) from a base line and replaces the shape + * by another **/ + PNS_LINE( const PNS_LINE& aBase, const SHAPE_LINE_CHAIN& aLine ) : PNS_ITEM( aBase ), m_line( aLine ), @@ -105,49 +87,101 @@ public: m_layers = aBase.m_layers; m_segmentRefs = NULL; m_hasVia = false; - m_affectedRangeStart = -1; } - ~PNS_LINE() + ~PNS_LINE(); + + /// @copydoc PNS_ITEM::Clone() + virtual PNS_LINE* Clone( ) const; + + const PNS_LINE& operator= (const PNS_LINE& aOther); + + ///> Assigns a shape to the line (a polyline/line chain) + void SetShape ( const SHAPE_LINE_CHAIN& aLine ) + { + m_line = aLine; + } + + ///> Returns the shape of the line + const SHAPE* Shape() const + { + return &m_line; + } + + ///> Modifiable accessor to the underlying shape + SHAPE_LINE_CHAIN& Line() + { + return m_line; + } + + ///> Const accessor to the underlying shape + const SHAPE_LINE_CHAIN& CLine() const + { + return m_line; + } + + ///> Returns the number of segments in the line + int SegmentCount() const { - if( m_segmentRefs ) - delete m_segmentRefs; - }; + return m_line.SegmentCount(); + } - virtual PNS_LINE* Clone() const; + ///> Returns the number of points in the line + int PointCount() const + { + return m_line.PointCount(); + } - ///> clones the line without cloning the shape - ///> (just the properties - net, width, layers, etc.) - PNS_LINE* CloneProperties() const; + ///> Returns the aIdx-th point of the line + const VECTOR2I& CPoint( int aIdx ) const + { + return m_line.CPoint(aIdx); + } - int GetLayer() const { return GetLayers().Start(); } + ///> Returns the aIdx-th segment of the line + const SEG CSegment (int aIdx ) const + { + return m_line.CSegment(aIdx); + } - ///> Geometry accessors - void SetShape( const SHAPE_LINE_CHAIN& aLine ) { m_line = aLine; } - const SHAPE* GetShape() const { return &m_line; } - SHAPE_LINE_CHAIN& GetLine() { return m_line; } - const SHAPE_LINE_CHAIN& GetCLine() const { return m_line; } + ///> Sets line width + void SetWidth( int aWidth ) + { + m_width = aWidth; + } + + ///> Returns line width + int Width() const + { + return m_width; + } - ///> Width accessors - void SetWidth( int aWidth ) { m_width = aWidth; } - int GetWidth() const { return m_width; } + ///> Returns true if the line is geometrically identical as line aOther + bool CompareGeometry( const PNS_LINE& aOther ); - ///> Links a segment from a PNS_NODE to this line, making it owned by the node + ///> Reverses the point/vertex order + void Reverse(); + + + /* Linking functions */ + + ///> Adds a reference to a segment registered in a PNS_NODE that is a part of this line. void LinkSegment( PNS_SEGMENT* aSeg ) { if( !m_segmentRefs ) - m_segmentRefs = new std::vector (); + m_segmentRefs = new SegmentRefs(); m_segmentRefs->push_back( aSeg ); } - ///> Returns a list of segments from the owning node that constitute this - ///> line (or NULL if the line is loose) - LinkedSegments* GetLinkedSegments() + ///> Returns the list of segments from the owning node that constitute this + ///> line (or NULL if the line is not linked) + SegmentRefs* LinkedSegments() { return m_segmentRefs; } + ///> Checks if the segment aSeg is a part of the line. bool ContainsSegment( PNS_SEGMENT* aSeg ) const { if( !m_segmentRefs ) @@ -157,13 +191,23 @@ public: aSeg ) != m_segmentRefs->end(); } - ///> Returns this line, but clipped to the nearest obstacle - ///> along, to avoid collision. + ///> Erases the linking information. Used to detach the line from the owning node. + void ClearSegmentLinks(); + + ///> Returns the number of segments that were assembled together to form this line. + int LinkCount() const { + if(!m_segmentRefs) + return -1; + return m_segmentRefs->size(); + } + + ///> Clips the line to the nearest obstacle, traversing from the line's start vertex (0). + ///> Returns the clipped line. const PNS_LINE ClipToNearestObstacle( PNS_NODE* aNode ) const; - ///> DEPRECATED optimization functions (moved to PNS_OPTIMIZER) - bool MergeObtuseSegments(); - bool MergeSegments(); + ///> Clips the line to a given range of vertices. + void ClipVertexRange ( int aStart, int aEnd ); + ///> Returns the number of corners of angles specified by mask aAngles. int CountCorners( int aAngles ); @@ -173,17 +217,7 @@ public: ///> aPrePath = path from origin to the obstacle ///> aWalkaroundPath = path around the obstacle ///> aPostPath = past from obstacle till the end - ///> aCW = whether to walkaround in clockwise or counter-clockwise direction. - void NewWalkaround( const SHAPE_LINE_CHAIN& aObstacle, - SHAPE_LINE_CHAIN& aPrePath, - SHAPE_LINE_CHAIN& aWalkaroundPath, - SHAPE_LINE_CHAIN& aPostPath, - bool aCw ) const; - - void NewWalkaround( const SHAPE_LINE_CHAIN& aObstacle, - SHAPE_LINE_CHAIN& aPath, - bool aCw ) const; - + ///> aCW = whether to walk around in clockwise or counter-clockwise direction. bool Walkaround( SHAPE_LINE_CHAIN obstacle, SHAPE_LINE_CHAIN& pre, @@ -202,62 +236,46 @@ public: bool EndsWithVia() const { return m_hasVia; } - void AppendVia( const PNS_VIA& aVia ) - { - m_hasVia = true; - m_via = aVia; - m_via.SetNet( m_net ); - } - + void AppendVia( const PNS_VIA& aVia ); void RemoveVia() { m_hasVia = false; } - const PNS_VIA& GetVia() const { return m_via; } - void SetAffectedRange( int aStart, int aEnd ) - { - m_affectedRangeStart = aStart; - m_affectedRangeEnd = aEnd; - } + const PNS_VIA& Via() const { return m_via; } + + virtual void Mark(int aMarker); + virtual void Unmark (); + virtual int Marker() const; + + void DragSegment ( const VECTOR2I& aP, int aIndex, int aSnappingThreshold = 0 ); + void DragCorner ( const VECTOR2I& aP, int aIndex, int aSnappingThreshold = 0 ); - void ClearAffectedRange() - { - m_affectedRangeStart = -1; - } - - bool GetAffectedRange( int& aStart, int& aEnd ) - { - if( m_affectedRangeStart >= 0 ) - { - aStart = m_affectedRangeStart; - aEnd = m_affectedRangeEnd; - return true; - } - else - { - aStart = 0; - aEnd = m_line.PointCount(); - return false; - } - } + void SetRank ( int aRank ); + int Rank() const; + + bool HasLoops() const; private: - bool onEdge( const SHAPE_LINE_CHAIN& obstacle, VECTOR2I p, int& ei, bool& is_vertex ) const; - bool walkScan( const SHAPE_LINE_CHAIN& line, const SHAPE_LINE_CHAIN& obstacle, - bool reverse, VECTOR2I& ip, int& index_o, int& index_l, bool& is_vertex ) const; - ///> List of semgments in a PNS_NODE (PNS_ITEM::m_owner) that constitute this line. - LinkedSegments* m_segmentRefs; + VECTOR2I snapToNeighbourSegments( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I &aP, int aIndex, int aThreshold) const; + VECTOR2I snapDraggedCorner( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I &aP, int aIndex, int aThreshold ) const; - ///> Shape of the line + ///> Copies m_segmentRefs from the line aParent. + void copyLinks( const PNS_LINE *aParent ) ; + + ///> List of segments in the owning PNS_NODE (PNS_ITEM::m_owner) that constitute this line, or NULL + ///> if the line is not a part of any node. + SegmentRefs* m_segmentRefs; + + ///> The actual shape of the line SHAPE_LINE_CHAIN m_line; + ///> our width int m_width; - ///> Via at the end and a flag indicating if it's enabled. - PNS_VIA m_via; + ///> If true, the line ends with a via bool m_hasVia; - int m_affectedRangeStart; - int m_affectedRangeEnd; + ///> Via at the end point, if m_hasVia == true + PNS_VIA m_via; }; #endif // __PNS_LINE_H diff --git a/pcbnew/router/pns_line_placer.cpp b/pcbnew/router/pns_line_placer.cpp index 4b9b616f3e..16f3ea1429 100644 --- a/pcbnew/router/pns_line_placer.cpp +++ b/pcbnew/router/pns_line_placer.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,12 +15,14 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include #include +#include + #include "trace.h" #include "pns_node.h" @@ -28,44 +30,46 @@ #include "pns_walkaround.h" #include "pns_shove.h" #include "pns_utils.h" +#include "pns_router.h" + +#include using boost::optional; -const double PNS_LINE_PLACER::m_shoveLengthThreshold = 1.7; - -PNS_LINE_PLACER::PNS_LINE_PLACER( PNS_NODE* aWorld ) +PNS_LINE_PLACER::PNS_LINE_PLACER( PNS_ROUTER* aRouter ) : + PNS_ALGO_BASE ( aRouter ) { - m_initial_direction = DIRECTION_45( DIRECTION_45::N ); - m_follow_mouse = false; - m_smoothing_step = 100000; - m_smooth_mouse = false; + m_initial_direction = DIRECTION_45::N; m_iteration = 0; - m_world = aWorld; - m_mode = RM_Smart; - m_follow_mouse = true; + m_world = NULL; m_shove = NULL; + m_currentNode = NULL; }; - PNS_LINE_PLACER::~PNS_LINE_PLACER() { if( m_shove ) delete m_shove; } - -void PNS_LINE_PLACER::ApplySettings( const PNS_ROUTING_SETTINGS& aSettings ) +void PNS_LINE_PLACER::setWorld ( PNS_NODE *aWorld ) { - m_follow_mouse = aSettings.m_followMouse; - m_mode = aSettings.m_routingMode; - m_walkaroundIterationLimit = aSettings.m_walkaroundIterationLimit; - m_smartPads = aSettings.m_smartPads; + m_world = aWorld; } +void PNS_LINE_PLACER::AddVia( bool aEnabled, int aDiameter, int aDrill ) +{ + m_viaDiameter = aDiameter; + m_viaDrill = aDrill; + m_placingVia = aEnabled; +} -void PNS_LINE_PLACER::StartPlacement( const VECTOR2I& aStart, int aNet, +void PNS_LINE_PLACER::startPlacement( const VECTOR2I& aStart, int aNet, int aWidth, int aLayer ) { + + assert(m_world != NULL); + m_direction = m_initial_direction; TRACE( 1, "world %p, intitial-direction %s layer %d\n", m_world % m_direction.Format().c_str() % aLayer ); @@ -73,27 +77,37 @@ void PNS_LINE_PLACER::StartPlacement( const VECTOR2I& aStart, int aNet, m_tail.SetNet( aNet ); m_head.SetWidth( aWidth ); m_tail.SetWidth( aWidth ); - m_head.GetLine().Clear(); - m_tail.GetLine().Clear(); + m_head.Line().Clear(); + m_tail.Line().Clear(); m_head.SetLayer( aLayer ); m_tail.SetLayer( aLayer ); m_iteration = 0; m_p_start = aStart; - m_currentNode = m_world->Branch(); - m_head.SetWorld( m_currentNode ); - m_tail.SetWorld( m_currentNode ); - // if(m_shove) - // delete m_shove; - m_shove = new PNS_SHOVE( m_currentNode ); + + m_lastNode = NULL; + m_currentNode = m_world; + + m_currentMode = Settings().Mode(); + + if(m_shove) + delete m_shove; + + m_shove = NULL; + + if(m_currentMode == RM_Shove || m_currentMode == RM_Smart) + { + m_shove = new PNS_SHOVE( m_world -> Branch(), Router() ); + } + m_placingVia = false; } -void PNS_LINE_PLACER::SetInitialDirection( const DIRECTION_45& aDirection ) +void PNS_LINE_PLACER::setInitialDirection( const DIRECTION_45& aDirection ) { m_initial_direction = aDirection; - if( m_tail.GetCLine().SegmentCount() == 0 ) +if( m_tail.SegmentCount() == 0 ) m_direction = aDirection; } @@ -101,8 +115,8 @@ void PNS_LINE_PLACER::SetInitialDirection( const DIRECTION_45& aDirection ) bool PNS_LINE_PLACER::handleSelfIntersections() { SHAPE_LINE_CHAIN::INTERSECTIONS ips; - SHAPE_LINE_CHAIN& head = m_head.GetLine(); - SHAPE_LINE_CHAIN& tail = m_tail.GetLine(); + SHAPE_LINE_CHAIN& head = m_head.Line(); + SHAPE_LINE_CHAIN& tail = m_tail.Line(); // if there is no tail, there is nothing to intersect with if( tail.PointCount() < 2 ) @@ -159,8 +173,11 @@ bool PNS_LINE_PLACER::handleSelfIntersections() bool PNS_LINE_PLACER::handlePullback() { - SHAPE_LINE_CHAIN& head = m_head.GetLine(); - SHAPE_LINE_CHAIN& tail = m_tail.GetLine(); + SHAPE_LINE_CHAIN& head = m_head.Line(); + SHAPE_LINE_CHAIN& tail = m_tail.Line(); + + if(head.PointCount() < 2) + return false; int n = tail.PointCount(); @@ -173,8 +190,8 @@ bool PNS_LINE_PLACER::handlePullback() return true; } - DIRECTION_45 first_head( head.Segment( 0 ) ); - DIRECTION_45 last_tail( tail.Segment( -1 ) ); + DIRECTION_45 first_head( head.CSegment( 0 ) ); + DIRECTION_45 last_tail( tail.CSegment( -1 ) ); DIRECTION_45::AngleType angle = first_head.Angle( last_tail ); // case 1: we have a defined routing direction, and the currently computed @@ -215,11 +232,14 @@ bool PNS_LINE_PLACER::handlePullback() bool PNS_LINE_PLACER::reduceTail( const VECTOR2I& aEnd ) { - SHAPE_LINE_CHAIN& head = m_head.GetLine(); - SHAPE_LINE_CHAIN& tail = m_tail.GetLine(); + SHAPE_LINE_CHAIN& head = m_head.Line(); + SHAPE_LINE_CHAIN& tail = m_tail.Line(); int n = tail.SegmentCount(); + if(head.SegmentCount() < 1) + return false; + // Don't attempt this for too short tails if( n < 2 ) return false; @@ -231,7 +251,7 @@ bool PNS_LINE_PLACER::reduceTail( const VECTOR2I& aEnd ) VECTOR2I new_start; int reduce_index = -1; - DIRECTION_45 head_dir( head.Segment( 0 ) ); + DIRECTION_45 head_dir( head.CSegment( 0 ) ); for( int i = tail.SegmentCount() - 1; i >= 0; i-- ) { @@ -247,7 +267,7 @@ bool PNS_LINE_PLACER::reduceTail( const VECTOR2I& aEnd ) if( m_currentNode->CheckColliding( &tmp, PNS_ITEM::ANY ) ) break; - if( DIRECTION_45( replacement.Segment( 0 ) ) == dir ) + if( DIRECTION_45( replacement.CSegment( 0 ) ) == dir ) { new_start = s.A; new_direction = dir; @@ -285,8 +305,8 @@ bool PNS_LINE_PLACER::checkObtusity( const SEG& a, const SEG& b ) const bool PNS_LINE_PLACER::mergeHead() { - SHAPE_LINE_CHAIN& head = m_head.GetLine(); - SHAPE_LINE_CHAIN& tail = m_tail.GetLine(); + SHAPE_LINE_CHAIN& head = m_head.Line(); + SHAPE_LINE_CHAIN& tail = m_tail.Line(); const int ForbiddenAngles = DIRECTION_45::ANG_ACUTE | DIRECTION_45::ANG_HALF_FULL | @@ -357,146 +377,157 @@ bool PNS_LINE_PLACER::handleViaPlacement( PNS_LINE& aHead ) return true; PNS_LAYERSET allLayers( 0, 15 ); - PNS_VIA v( aHead.GetCLine().CPoint( -1 ), allLayers, m_viaDiameter, aHead.GetNet() ); + PNS_VIA v( aHead.CPoint( -1 ), allLayers, m_viaDiameter, aHead.Net() ); v.SetDrill( m_viaDrill ); VECTOR2I force; - VECTOR2I lead = aHead.GetCLine().CPoint( -1 ) - aHead.GetCLine().CPoint( 0 ); + VECTOR2I lead = aHead.CPoint( -1 ) - aHead.CPoint( 0 ); - if( v.PushoutForce( m_shove->GetCurrentNode(), lead, force, true, 20 ) ) + bool solidsOnly = ( m_currentMode != RM_Walkaround ); + + if( v.PushoutForce( m_currentNode, lead, force, solidsOnly, 40 ) ) { SHAPE_LINE_CHAIN line = m_direction.BuildInitialTrace( - aHead.GetCLine().CPoint( 0 ), - aHead.GetCLine().CPoint( -1 ) + force ); + aHead.CPoint( 0 ), + aHead.CPoint( -1 ) + force ); aHead = PNS_LINE( aHead, line ); - v.SetPos( v.GetPos() + force ); + v.SetPos( v.Pos() + force ); return true; } return false; } - -bool PNS_LINE_PLACER::routeHead( const VECTOR2I& aP, PNS_LINE& aNewHead, - bool aCwWalkaround ) +bool PNS_LINE_PLACER::rhWalkOnly ( const VECTOR2I& aP, PNS_LINE& aNewHead ) { - // STAGE 1: route a simple two-segment trace between m_p_start and aP... SHAPE_LINE_CHAIN line = m_direction.BuildInitialTrace( m_p_start, aP ); + PNS_LINE initTrack( m_head, line ), walkFull; + int effort = 0; + bool viaOk = handleViaPlacement( initTrack ); + bool rv = true; - PNS_LINE initTrack( m_head, line ); - PNS_LINE walkFull, walkSolids; + PNS_WALKAROUND walkaround( m_currentNode, Router() ); - if( m_mode == RM_Ignore ) + walkaround.SetSolidsOnly( false ); + walkaround.SetIterationLimit( Settings().WalkaroundIterationLimit() ); + + PNS_WALKAROUND::WalkaroundStatus wf = walkaround.Route( initTrack, walkFull, false ); + + switch(Settings().OptimizerEffort()) { - aNewHead = initTrack; - return true; + case OE_Low: + effort = 0; + break; + case OE_Medium: + case OE_Full: + effort = PNS_OPTIMIZER::MERGE_SEGMENTS; + break; } + if(Settings().SmartPads()) + effort |= PNS_OPTIMIZER::SMART_PADS; + + if( wf == PNS_WALKAROUND::STUCK ) + { + walkFull = walkFull.ClipToNearestObstacle( m_currentNode ); + rv = true; + + } else if( m_placingVia && viaOk ) { + PNS_LAYERSET allLayers( 0, 15 ); + PNS_VIA v1( walkFull.CPoint( -1 ), allLayers, m_viaDiameter ); + walkFull.AppendVia( v1 ); + } + + PNS_OPTIMIZER::Optimize( &walkFull, effort, m_currentNode ); + + if( m_currentNode->CheckColliding(&walkFull) ) + { + TRACEn(0, "strange, walk line colliding\n"); + } + + m_head = walkFull; + aNewHead = walkFull; + + return rv; +} + +bool PNS_LINE_PLACER::rhMarkObstacles ( const VECTOR2I& aP, PNS_LINE& aNewHead ) +{ + m_head.SetShape ( m_direction.BuildInitialTrace( m_p_start, aP ) ); + + if( m_placingVia ) + { + PNS_LAYERSET allLayers( 0, 15 ); + PNS_VIA v1( m_head.CPoint( -1 ), allLayers, m_viaDiameter ); + m_head.AppendVia( v1 ); + } + + aNewHead = m_head; + + return m_currentNode->CheckColliding( &m_head ); +} + +bool PNS_LINE_PLACER::rhShoveOnly ( const VECTOR2I& aP, PNS_LINE& aNewHead ) +{ + SHAPE_LINE_CHAIN line = m_direction.BuildInitialTrace( m_p_start, aP ); + PNS_LINE initTrack( m_head, line ); + PNS_LINE walkSolids, l2; handleViaPlacement( initTrack ); - m_currentNode = m_shove->GetCurrentNode(); - + m_currentNode = m_shove->CurrentNode(); PNS_OPTIMIZER optimizer( m_currentNode ); - PNS_WALKAROUND walkaround( m_currentNode ); - walkaround.SetSolidsOnly( false ); - walkaround.SetIterationLimit( m_mode == RM_Walkaround ? 8 : 5 ); - // walkaround.SetApproachCursor(true, aP); + PNS_WALKAROUND walkaround( m_currentNode, Router() ); - PNS_WALKAROUND::WalkaroundStatus wf = walkaround.Route( initTrack, walkFull ); - -#if 0 - - if( m_mode == RM_Walkaround ) - { - // walkaround. -// PNSDisplayDebugLine (walkFull.GetCLine(), 4); - - if( wf == PNS_WALKAROUND::STUCK ) - { - aNewHead = m_head; - aNewHead.SetShape( walkFull.GetCLine() ); - aNewHead = aNewHead.ClipToNearestObstacle( m_currentNode ); - return false; - } - - aNewHead = m_head; - aNewHead.SetShape( walkFull.GetCLine() ); - -// printf("nh w %d l %d\n", aNewHead.GetWidth(), aNewHead.GetLayers().Start()); - return true; - } - -#endif - - PNS_COST_ESTIMATOR cost_walk, cost_orig; - - walkaround.SetApproachCursor( false, aP ); walkaround.SetSolidsOnly( true ); walkaround.SetIterationLimit( 10 ); PNS_WALKAROUND::WalkaroundStatus stat_solids = walkaround.Route( initTrack, walkSolids ); optimizer.SetEffortLevel( PNS_OPTIMIZER::MERGE_SEGMENTS ); - optimizer.SetCollisionMask( PNS_ITEM::SOLID ); + optimizer.SetCollisionMask ( PNS_ITEM::SOLID ); optimizer.Optimize( &walkSolids ); - #if 0 - optimizer.SetCollisionMask( -1 ); - optimizer.Optimize( &walkFull ); - #endif - cost_orig.Add( initTrack ); - cost_walk.Add( walkFull ); - if( m_mode == RM_Smart || m_mode == RM_Shove ) - { - PNS_LINE l2; - - bool walk_better = cost_orig.IsBetter( cost_walk, 1.5, 10.0 ); - walk_better = false; - -#if 0 - printf( "RtTrk width %d %d %d", initTrack.GetWidth(), - walkFull.GetWidth(), walkSolids.GetWidth() ); - printf( "init-coll %d\n", m_currentNode->CheckColliding( &initTrack ) ? 1 : 0 ); - printf( "total cost: walk cor %.0f len %.0f orig cor %.0f len %.0f walk-better %d\n", - cost_walk.GetCornerCost(), cost_walk.GetLengthCost(), - cost_orig.GetCornerCost(), cost_orig.GetLengthCost(), - walk_better ); -#endif - - if( m_mode == RM_Smart && wf == PNS_WALKAROUND::DONE && walk_better - && walkFull.GetCLine().CPoint( -1 ) == initTrack.GetCLine().CPoint( -1 ) ) - l2 = walkFull; - else if( stat_solids == PNS_WALKAROUND::DONE ) + if( stat_solids == PNS_WALKAROUND::DONE ) l2 = walkSolids; else - l2 = initTrack.ClipToNearestObstacle( m_shove->GetCurrentNode() ); + l2 = initTrack.ClipToNearestObstacle( m_shove->CurrentNode() ); - PNS_LINE l( m_tail ); - l.GetLine().Append( l2.GetCLine() ); - l.GetLine().Simplify(); + PNS_LINE l( m_tail ); + l.Line().Append( l2.CLine() ); + l.Line().Simplify(); - if( m_placingVia ) - { - PNS_LAYERSET allLayers( 0, 15 ); - PNS_VIA v1( l.GetCLine().CPoint( -1 ), allLayers, m_viaDiameter ); - PNS_VIA v2( l2.GetCLine().CPoint( -1 ), allLayers, m_viaDiameter ); - v1.SetDrill( m_viaDrill ); - v2.SetDrill( m_viaDrill ); + if( m_placingVia ) + { + PNS_LAYERSET allLayers( 0, 15 ); + PNS_VIA v1( l.CPoint( -1 ), allLayers, m_viaDiameter ); + PNS_VIA v2( l2.CPoint( -1 ), allLayers, m_viaDiameter ); + v1.SetDrill( m_viaDrill ); + v2.SetDrill( m_viaDrill ); - l.AppendVia( v1 ); - l2.AppendVia( v2 ); - } + l.AppendVia( v1 ); + l2.AppendVia( v2 ); + } - PNS_SHOVE::ShoveStatus status = m_shove->ShoveLines( &l ); - m_currentNode = m_shove->GetCurrentNode(); + l.Line().Simplify(); - if( status == PNS_SHOVE::SH_OK ) + // in certain, uncommon cases there may be loops in the head+tail, In such case, we don't shove to avoid + // screwing up the database. + if ( l.HasLoops() ) + { + aNewHead = m_head; + return false; + } + + PNS_SHOVE::ShoveStatus status = m_shove->ShoveLines( l ); + + m_currentNode = m_shove->CurrentNode(); + + if( status == PNS_SHOVE::SH_OK ) { optimizer.SetWorld( m_currentNode ); - optimizer.ClearCache(); optimizer.SetEffortLevel( PNS_OPTIMIZER::MERGE_OBTUSE | PNS_OPTIMIZER::SMART_PADS ); - optimizer.SetCollisionMask( -1 ); + optimizer.SetCollisionMask( PNS_ITEM::ANY ); optimizer.Optimize( &l2 ); aNewHead = l2; @@ -510,25 +541,59 @@ bool PNS_LINE_PLACER::routeHead( const VECTOR2I& aP, PNS_LINE& aNewHead, walkaround.SetIterationLimit( 10 ); walkaround.SetApproachCursor( true, aP ); walkaround.Route( initTrack, l2 ); - aNewHead = l2.ClipToNearestObstacle( m_shove->GetCurrentNode() ); - // aNewHead = l2; + aNewHead = l2.ClipToNearestObstacle( m_shove->CurrentNode() ); return false; } - } + + return false; +} + +bool PNS_LINE_PLACER::routeHead( const VECTOR2I& aP, PNS_LINE& aNewHead ) +{ + switch( m_currentMode ) + { + case RM_MarkObstacles: + return rhMarkObstacles( aP, aNewHead ); + case RM_Walkaround: + return rhWalkOnly ( aP, aNewHead ); + case RM_Shove: + return rhShoveOnly ( aP, aNewHead ); + default: + break; + } + return false; } bool PNS_LINE_PLACER::optimizeTailHeadTransition() { - SHAPE_LINE_CHAIN& head = m_head.GetLine(); - SHAPE_LINE_CHAIN& tail = m_tail.GetLine(); + + PNS_LINE tmp = Trace(); - const int TailLookbackSegments = 5; + if(PNS_OPTIMIZER::Optimize(&tmp, PNS_OPTIMIZER::FANOUT_CLEANUP, m_currentNode)) + { + if(tmp.SegmentCount() < 1) + return false; - int threshold = std::min( tail.PointCount(), TailLookbackSegments + 1 ); + m_head = tmp; + m_p_start = tmp.CLine().CPoint( 0 ); + m_direction = DIRECTION_45( tmp.CSegment( 0 ) ); + m_tail.Line().Clear(); + return true; + } + + SHAPE_LINE_CHAIN& head = m_head.Line(); + SHAPE_LINE_CHAIN& tail = m_tail.Line(); + + int tailLookbackSegments = 3; + + //if(m_currentMode() == RM_Walkaround) + // tailLookbackSegments = 10000; + + int threshold = std::min( tail.PointCount(), tailLookbackSegments + 1 ); if( tail.SegmentCount() < 3 ) return false; @@ -536,8 +601,9 @@ bool PNS_LINE_PLACER::optimizeTailHeadTransition() // assemble TailLookbackSegments tail segments with the current head SHAPE_LINE_CHAIN opt_line = tail.Slice( -threshold, -1 ); - opt_line.Append( head ); -// opt_line.Simplify(); + int end = std::min(2, head.PointCount() - 1 ); + + opt_line.Append( head.Slice( 0, end ) ); PNS_LINE new_head( m_tail, opt_line ); @@ -545,20 +611,18 @@ bool PNS_LINE_PLACER::optimizeTailHeadTransition() // If so, replace the (threshold) last tail points and the head with // the optimized line - // if(PNS_OPTIMIZER::Optimize(&new_head, PNS_OPTIMIZER::MERGE_SEGMENTS)) - - if( new_head.MergeSegments() ) + if(PNS_OPTIMIZER::Optimize(&new_head, PNS_OPTIMIZER::MERGE_OBTUSE, m_currentNode)) { PNS_LINE tmp( m_tail, opt_line ); TRACE( 0, "Placer: optimize tail-head [%d]", threshold ); head.Clear(); - tail.Replace( -threshold, -1, new_head.GetCLine() ); + tail.Replace( -threshold, -1, new_head.CLine() ); tail.Simplify(); - m_p_start = new_head.GetCLine().CPoint( -1 ); - m_direction = DIRECTION_45( new_head.GetCLine().CSegment( -1 ) ); + m_p_start = new_head.CLine().CPoint( -1 ); + m_direction = DIRECTION_45( new_head.CSegment( -1 ) ); return true; } @@ -576,26 +640,24 @@ void PNS_LINE_PLACER::routeStep( const VECTOR2I& aP ) PNS_LINE new_head; - m_follow_mouse = true; - TRACE( 2, "INIT-DIR: %s head: %d, tail: %d segs\n", - m_initial_direction.Format().c_str() % m_head.GetCLine().SegmentCount() % - m_tail.GetCLine().SegmentCount() ); + m_initial_direction.Format().c_str() % m_head.SegmentCount() % + m_tail.SegmentCount() ); for( i = 0; i < n_iter; i++ ) { - if( !go_back && m_follow_mouse ) + if( !go_back && Settings().FollowMouse() ) reduceTail( aP ); go_back = false; - if( !routeHead( aP, new_head, true ) ) + if( !routeHead( aP, new_head ) ) fail = true; if( !new_head.Is45Degree() ) fail = true; - if( !m_follow_mouse ) + if( !Settings().FollowMouse() ) return; m_head = new_head; @@ -615,48 +677,36 @@ void PNS_LINE_PLACER::routeStep( const VECTOR2I& aP ) if( !fail ) { - if( optimizeTailHeadTransition() ) - return; + if( optimizeTailHeadTransition() ) + return; mergeHead(); } } -bool PNS_LINE_PLACER::Route( const VECTOR2I& aP ) +bool PNS_LINE_PLACER::route( const VECTOR2I& aP ) { - if( m_smooth_mouse ) - { - VECTOR2I p_cur = m_p_start; - VECTOR2I step = (aP - m_p_start).Resize( m_smoothing_step ); - - do - { - if( (p_cur - aP).EuclideanNorm() <= m_smoothing_step ) - p_cur = aP; - else - p_cur += step; - - routeStep( p_cur ); - } while( p_cur != aP ); - } - else - routeStep( aP ); - + routeStep( aP ); return CurrentEnd() == aP; } -const PNS_LINE PNS_LINE_PLACER::GetTrace() const +const PNS_LINE PNS_LINE_PLACER::Trace() const { PNS_LINE tmp( m_head ); - tmp.SetShape( m_tail.GetCLine() ); - tmp.GetLine().Append( m_head.GetCLine() ); - tmp.GetLine().Simplify(); + tmp.SetShape( m_tail.CLine() ); + tmp.Line().Append( m_head.CLine() ); + tmp.Line().Simplify(); return tmp; } +const PNS_ITEMSET PNS_LINE_PLACER::Traces() +{ + m_currentTrace = Trace(); + return PNS_ITEMSET( &m_currentTrace ); +} void PNS_LINE_PLACER::FlipPosture() { @@ -664,15 +714,301 @@ void PNS_LINE_PLACER::FlipPosture() m_direction = m_direction.Right(); } - -void PNS_LINE_PLACER::GetUpdatedItems( PNS_NODE::ItemVector& aRemoved, - PNS_NODE::ItemVector& aAdded ) +PNS_NODE* PNS_LINE_PLACER::CurrentNode(bool aLoopsRemoved) const { - return m_shove->GetCurrentNode()->GetUpdatedItems( aRemoved, aAdded ); + if(aLoopsRemoved && m_lastNode) + return m_lastNode; + return m_currentNode; } -PNS_NODE* PNS_LINE_PLACER::GetCurrentNode() const +void PNS_LINE_PLACER::splitAdjacentSegments( PNS_NODE* aNode, PNS_ITEM* aSeg, const VECTOR2I& aP ) { - return m_shove->GetCurrentNode(); + if( aSeg && aSeg->OfKind( PNS_ITEM::SEGMENT ) ) + { + PNS_JOINT *jt = aNode->FindJoint( aP, aSeg ); + + if( jt && jt->LinkCount() >= 1 ) + return; + + PNS_SEGMENT* s_old = static_cast( aSeg ); + + PNS_SEGMENT* s_new[2]; + + s_new[0] = s_old->Clone(); + s_new[1] = s_old->Clone(); + + s_new[0]->SetEnds( s_old->Seg().A, aP ); + s_new[1]->SetEnds( aP, s_old->Seg().B ); + + aNode->Remove( s_old ); + aNode->Add( s_new[0], true ); + aNode->Add( s_new[1], true ); + } } + +void PNS_LINE_PLACER::SetLayer(int aLayer) +{ + m_currentLayer = aLayer; +} + +void PNS_LINE_PLACER::SetWidth(int aWidth) +{ + m_currentWidth = aWidth; +} + +void PNS_LINE_PLACER::Start( const VECTOR2I& aP, PNS_ITEM* aStartItem ) +{ + VECTOR2I p( aP ); + + static int unknowNetIdx = 0; // -10000; + int net = -1; + + m_lastNode = NULL; + m_placingVia = false; + m_startsOnVia = false; + + bool splitSeg = false; + + if( Router()->SnappingEnabled() ) + p = Router()->SnapToItem( aStartItem, aP, splitSeg ); + + if( !aStartItem || aStartItem->Net() < 0 ) + net = unknowNetIdx--; + else + net = aStartItem->Net(); + + m_currentStart = p; + m_originalStart = p; + m_currentEnd = p; + m_currentNet = net; + + + PNS_NODE *rootNode = Router()->GetWorld()->Branch(); + + if( splitSeg ) + splitAdjacentSegments( rootNode, aStartItem, p ); + + setWorld ( rootNode ); + setInitialDirection( Settings().InitialDirection() ); + startPlacement( p, m_currentNet, m_currentWidth, m_currentLayer ); +} + + +void PNS_LINE_PLACER::Move( const VECTOR2I& aP, PNS_ITEM* aEndItem ) +{ + PNS_LINE current; + VECTOR2I p = aP; + int eiDepth = -1; + + if(aEndItem && aEndItem->Owner()) + eiDepth = aEndItem->Owner()->Depth(); + + if( m_lastNode ) + { + delete m_lastNode; + m_lastNode = NULL; + } + + route( p ); + + current = Trace(); + + if(!current.PointCount()) + m_currentEnd = m_p_start; + else + m_currentEnd = current.CLine().CPoint(-1); + + + PNS_NODE *latestNode = m_currentNode; + m_lastNode = latestNode->Branch(); + + + if(eiDepth >= 0 && aEndItem && latestNode->Depth() > eiDepth && current.SegmentCount() && current.CPoint(-1) == aP) + { + splitAdjacentSegments( m_lastNode, aEndItem, current.CPoint(-1) ); + if(Settings().RemoveLoops()) + removeLoops( m_lastNode, ¤t ); + } + + updateLeadingRatLine(); +} + +bool PNS_LINE_PLACER::FixRoute( const VECTOR2I& aP, PNS_ITEM* aEndItem ) +{ + bool realEnd = false; + int lastV; + + PNS_LINE pl = Trace(); + + if (m_currentMode == RM_MarkObstacles && + !Settings().CanViolateDRC() && + m_world->CheckColliding( &pl ) ) + return false; + + const SHAPE_LINE_CHAIN& l = pl.CLine(); + + if( !l.SegmentCount() ) + return true; + + VECTOR2I p_pre_last = l.CPoint( -1 ); + const VECTOR2I p_last = l.CPoint( -1 ); + DIRECTION_45 d_last( l.CSegment( -1 ) ); + + if( l.PointCount() > 2 ) + p_pre_last = l.CPoint( -2 ); + + if( aEndItem && m_currentNet >= 0 && m_currentNet == aEndItem->Net() ) + realEnd = true; + + + if(realEnd || m_placingVia) + lastV = l.SegmentCount(); + else + lastV = std::max( 1, l.SegmentCount() - 1 ); + + + PNS_SEGMENT* lastSeg = NULL; + + for( int i = 0; i < lastV; i++ ) + { + const SEG& s = pl.CSegment( i ); + PNS_SEGMENT* seg = new PNS_SEGMENT( s, m_currentNet ); + seg->SetWidth( pl.Width() ); + seg->SetLayer( m_currentLayer ); + m_lastNode->Add( seg ); + lastSeg = seg; + } + + if( pl.EndsWithVia() ) + m_lastNode->Add( pl.Via().Clone() ); + + if(realEnd) + simplifyNewLine ( m_lastNode, lastSeg ); + + Router()->CommitRouting ( m_lastNode ); + + m_lastNode = NULL; + + if(!realEnd) + { + setInitialDirection( d_last ); + VECTOR2I p_start = m_placingVia ? p_last : p_pre_last; + + if( m_placingVia ) + m_currentLayer = Router()->NextCopperLayer( true ); + + setWorld ( Router()->GetWorld()->Branch() ); + startPlacement( p_start, m_head.Net(), m_head.Width(), m_currentLayer ); + + m_startsOnVia = m_placingVia; + m_placingVia = false; + } + + return realEnd; +} + +void PNS_LINE_PLACER::removeLoops( PNS_NODE* aNode, PNS_LINE* aLatest ) +{ + if(!aLatest->SegmentCount()) + return; + + aNode->Add( aLatest, true ); + + for(int s = 0; s < aLatest->SegmentCount(); s++ ) + { + PNS_SEGMENT *seg = (*aLatest->LinkedSegments())[s]; + + PNS_LINE* ourLine = aNode->AssembleLine( seg ) ; + PNS_JOINT a, b; + + std::vector lines; + + aNode->FindLineEnds( ourLine, a, b ); + + if( a == b ) + { + aNode->FindLineEnds( aLatest, a, b); + } + + aNode->FindLinesBetweenJoints( a, b, lines ); + + int removedCount = 0; + int total = 0; + + BOOST_FOREACH( PNS_LINE* line, lines ) + { + total++; + + if( !( line->ContainsSegment( seg ) ) && line->SegmentCount() ) + { + Router()->DisplayDebugLine ( line->CLine(), -1, 10000 ); + + for(int i = 0; i < line->PointCount(); i++ ) + Router()->DisplayDebugPoint ( line->CPoint(i), -1 ); + + aNode->Remove( line ); + removedCount ++; + } + } + + TRACE(0, "total segs removed: %d/%d\n", removedCount % total); + + delete ourLine; + } + + aNode->Remove( aLatest ); +} + +void PNS_LINE_PLACER::simplifyNewLine ( PNS_NODE *aNode, PNS_SEGMENT *aLatest ) +{ + PNS_LINE *l = aNode->AssembleLine( aLatest) ; + SHAPE_LINE_CHAIN simplified ( l->CLine() ); + simplified.Simplify(); + + if(simplified.PointCount() != l->PointCount()) + { + std::auto_ptr lnew ( l->Clone() ); + aNode -> Remove(l); + lnew->SetShape(simplified); + aNode -> Add( lnew.get() ); + } +} + +void PNS_LINE_PLACER::UpdateSizes( const PNS_ROUTING_SETTINGS& aSettings ) +{ + int trackWidth = aSettings.GetTrackWidth(); + + m_head.SetWidth( trackWidth ); + m_tail.SetWidth( trackWidth ); + + m_viaDiameter = aSettings.GetViaDiameter(); + m_viaDrill = aSettings.GetViaDrill(); +} + +void PNS_LINE_PLACER::updateLeadingRatLine() +{ + PNS_LINE current = Trace(); + + if(! current.PointCount() ) + return; + + std::auto_ptr tmpNode ( m_lastNode->Branch() ); + tmpNode->Add( ¤t ); + + PNS_JOINT *jt = tmpNode->FindJoint( current.CPoint(-1), current.Layers().Start(), current.Net() ); + + if(!jt) + return; + + int anchor; + PNS_ITEM *it = tmpNode->NearestUnconnectedItem ( jt, &anchor ); + + if(it) + { + SHAPE_LINE_CHAIN lc; + lc.Append ( current.CPoint(-1) ); + lc.Append ( it->Anchor(anchor) ); + Router()->DisplayDebugLine( lc, 5, 10000 ); + } +} \ No newline at end of file diff --git a/pcbnew/router/pns_line_placer.h b/pcbnew/router/pns_line_placer.h index 625e6451b2..ddbeb23c74 100644 --- a/pcbnew/router/pns_line_placer.h +++ b/pcbnew/router/pns_line_placer.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_LINE_PLACER_H @@ -29,7 +29,7 @@ #include "pns_node.h" #include "pns_via.h" #include "pns_line.h" -#include "pns_routing_settings.h" +#include "pns_algo_base.h" class PNS_ROUTER; class PNS_SHOVE; @@ -39,29 +39,156 @@ class PNS_ROUTER_BASE; /** * Class PNS_LINE_PLACER * - * Interactively routes a single track. Runs shove and walkaround - * algorithms when needed. + * Single track placement algorithm. Interactively routes a track. + * Applies shove and walkaround algorithms when needed. */ -class PNS_LINE_PLACER +class PNS_LINE_PLACER : public PNS_ALGO_BASE { public: - PNS_LINE_PLACER( PNS_NODE* aWorld ); + PNS_LINE_PLACER( PNS_ROUTER *aRouter ); ~PNS_LINE_PLACER(); - ///> Appends a via at the end of currently placed line. - void AddVia( bool aEnabled, int aDiameter, int aDrill ) - { - m_viaDiameter = aDiameter; - m_viaDrill = aDrill; - m_placingVia = aEnabled; - } - - ///> Starts placement of a line at point aStart. - void StartPlacement( const VECTOR2I& aStart, int aNet, int aWidth, int aLayer ); + /** + * Function Start() + * + * Starts routing a single track at point aP, taking item aStartItem as anchor + * (unless NULL). + */ + void Start ( const VECTOR2I& aP, PNS_ITEM* aStartItem ); + + /** + * Function Move() + * + * Moves the end of the currently routed trace to the point aP, taking + * aEndItem as anchor (if not NULL). + * (unless NULL). + */ + void Move( const VECTOR2I& aP, PNS_ITEM* aEndItem ); /** - * Function Route() + * Function FixRoute() + * + * Commits the currently routed track to the parent node, taking + * aP as the final end point and aEndItem as the final anchor (if provided). + * @return true, if route has been commited. May return false if the routing + * result is violating design rules - in such case, the track is only committed + * if Settings.CanViolateDRC() is on. + */ + bool FixRoute( const VECTOR2I& aP, PNS_ITEM* aEndItem ); + + /** + * Function AddVia() + * + * Enables/disables a via at the end of currently routed trace. + * @param aEnabled if true, a via is attached during placement + * @param aDiameter diameter of the via + * @param aDrill drill of the via + */ + void AddVia( bool aEnabled, int aDiameter, int aDrill ); + + /** + * Function SetLayer() + * + * Sets the current routing layer. + */ + void SetLayer ( int aLayer ); + + /** + * Function SetWidth() + * + * Sets the current track width. + */ + void SetWidth ( int aWidth ); + + /** + * Function Head() + * + * Returns the "head" of the line being placed, that is the volatile part + * that has not "settled" yet. + */ + const PNS_LINE& Head() const { return m_head; } + + /** + * Function Tail() + * + * Returns the "tail" of the line being placed, the part which has already wrapped around + * and shoved some obstacles. + */ + const PNS_LINE& Tail() const { return m_tail; } + + /** + * Function Trace() + * + * Returns the complete routed line. + */ + const PNS_LINE Trace() const; + + /** + * Function Traces() + * + * Returns the complete routed line, as a single-member PNS_ITEMSET. + */ + const PNS_ITEMSET Traces(); + + /** + * Function CurrentEnd() + * + * Returns the current end of the line being placed. It may not be equal + * to the cursor position due to collisions. + */ + const VECTOR2I& CurrentEnd() const + { + return m_currentEnd; + } + + /** + * Function CurrentNet() + * + * Returns the net code of currently routed track. + */ + int CurrentNet() const + { + return m_currentNet; + } + + /** + * Function CurrentLayer() + * + * Returns the layer of currently routed track. + */ + int CurrentLayer() const + { + return m_currentLayer; + } + + /** + * Function CurrentNode() + * + * Returns the most recent world state. + */ + PNS_NODE* CurrentNode(bool aLoopsRemoved = false) const; + + /** + * Function FlipPosture() + * + * Toggles the current posture (straight/diagonal) of the trace head. + */ + void FlipPosture(); + + /** + * Function UpdateSizes() + * + * Performs on-the-fly update of the width, via diameter & drill size from + * a settings class. Used to dynamically change these parameters as + * the track is routed. + */ + void UpdateSizes( const PNS_ROUTING_SETTINGS& aSettings ); + + +private: + /** + * Function route() * * Re-routes the current track to point aP. Returns true, when routing has * completed successfully (i.e. the trace end has reached point aP), and false @@ -70,55 +197,76 @@ public: * @param aP ending point of current route. * @return true, if the routing is complete. */ - bool Route( const VECTOR2I& aP ); - ///> Sets initial routing direction/posture - void SetInitialDirection( const DIRECTION_45& aDirection ); + bool route( const VECTOR2I& aP ); - void ApplySettings( const PNS_ROUTING_SETTINGS& aSettings ); + /** + * Function updateLeadingRatLine() + * + * Draws the "leading" ratsnest line, which connects the end of currently + * routed track and the nearest yet unrouted item. If the routing for + * current net is complete, draws nothing. + */ + void updateLeadingRatLine(); + + /** + * Function setWorld() + * + * Sets the board to route. + */ + void setWorld ( PNS_NODE *aWorld ); + + /** + * Function startPlacement() + * + * Initializes placement of a new line with given parameters. + */ + void startPlacement( const VECTOR2I& aStart, int aNet, int aWidth, int aLayer ); - ///> Returns the "head" of the line being placed, that is the volatile part - ///> that has not been settled yet - const PNS_LINE& GetHead() const { return m_head; } - ///> Returns the "tail" of the line being placed the part that has been - ///> fixed already (follow mouse mode only) - const PNS_LINE& GetTail() const { return m_tail; } + /** + * Function setInitialDirection() + * + * Sets preferred direction of the very first track segment to be laid. + * Used by posture switching mechanism. + */ + void setInitialDirection( const DIRECTION_45& aDirection ); - ///> Returns the whole routed line - const PNS_LINE GetTrace() const; + /** + * Function splitAdjacentSegments() + * + * Checks if point aP lies on segment aSeg. If so, splits the segment in two, + * forming a joint at aP and stores updated topology in node aNode. + */ + void splitAdjacentSegments( PNS_NODE* aNode, PNS_ITEM* aSeg, const VECTOR2I& aP ); - ///> Returns the current end of the line being placed. It may not be equal - ///> to the cursor position due to collisions. - const VECTOR2I& CurrentEnd() const - { - if( m_head.GetCLine().PointCount() > 0 ) - return m_head.GetCLine().CPoint( -1 ); - else if( m_tail.GetCLine().PointCount() > 0 ) - return m_tail.GetCLine().CPoint( -1 ); - else - return m_p_start; - } + /** + * Function removeLoops() + * + * Searches aNode for traces concurrent to aLatest and removes them. Updated + * topology is stored in aNode. + */ + void removeLoops( PNS_NODE* aNode, PNS_LINE* aLatest ); - ///> Returns all items in the world that have been affected by the routing - ///> operation. Used to update data structures of the host application - void GetUpdatedItems( PNS_NODE::ItemVector& aRemoved, - PNS_NODE::ItemVector& aAdded ); - - ///> Toggles the current posture (straight/diagonal) of the trace head. - void FlipPosture(); - - ///> Returns the most recent world state - PNS_NODE* GetCurrentNode() const; - -private: - static const double m_shoveLengthThreshold; + /** + * Function simplifyNewLine() + * + * Assembles a line starting from segment aLatest, removes collinear segments + * and redundant vertexes. If a simplification bhas been found, replaces the + * old line with the simplified one in aNode. + */ + void simplifyNewLine ( PNS_NODE *aNode, PNS_SEGMENT *aLatest ); + /** + * Function handleViaPlacement() + * + * Attempts to find a spot to place the via at the end of line aHead. + */ bool handleViaPlacement( PNS_LINE& aHead ); /** * Function checkObtusity() * - * Helper that checks if segments a and b form an obtuse angle + * Helper function, checking if segments a and b form an obtuse angle * (in 45-degree regime). * @return true, if angle (a, b) is obtuse */ @@ -163,8 +311,6 @@ private: */ bool reduceTail( const VECTOR2I& aEnd ); - void fixHeadPosture(); - /** * Function optimizeTailHeadTransition() * @@ -182,8 +328,7 @@ private: * around all colliding solid or non-movable items. Movable segments are * ignored, as they'll be handled later by the shove algorithm. */ - bool routeHead( const VECTOR2I& aP, PNS_LINE& aNewHead, - bool aCwWalkaround = true ); + bool routeHead( const VECTOR2I& aP, PNS_LINE& aNewHead); /** * Function routeStep() @@ -194,18 +339,15 @@ private: */ void routeStep( const VECTOR2I& aP ); - ///> routing mode (walkaround, shove, etc.) - PNS_MODE m_mode; + ///< route step, walkaround mode + bool rhWalkOnly ( const VECTOR2I& aP, PNS_LINE& aNewHead); - ///> follow mouse trail by attaching new segments to the head - ///> as the cursor moves - bool m_follow_mouse; + ///< route step, shove mode + bool rhShoveOnly ( const VECTOR2I& aP, PNS_LINE& aNewHead); - ///> mouse smoothing active - bool m_smooth_mouse; - - ///> mouse smoothing step (in world units) - int m_smoothing_step; + ///< route step, mark obstacles mode + bool rhMarkObstacles ( const VECTOR2I& aP, PNS_LINE& aNewHead ); + ///> current routing direction DIRECTION_45 m_direction; @@ -235,6 +377,9 @@ private: ///> Current world state PNS_NODE* m_currentNode; + ///> Postprocessed world state (including marked collisions & removed loops) + PNS_NODE* m_lastNode; + ///> Are we placing a via? bool m_placingVia; @@ -244,11 +389,17 @@ private: ///> current via drill int m_viaDrill; - ///> walkaround algorithm iteration limit - int m_walkaroundIterationLimit; + int m_currentWidth; + int m_currentNet; + int m_currentLayer; + + bool m_startsOnVia; + + VECTOR2I m_originalStart, m_currentEnd, m_currentStart; + PNS_LINE m_currentTrace; + + PNS_MODE m_currentMode; - ///> smart pads optimizer enabled. - bool m_smartPads; }; #endif // __PNS_LINE_PLACER_H diff --git a/pcbnew/router/pns_logger.cpp b/pcbnew/router/pns_logger.cpp new file mode 100644 index 0000000000..e20577b6b9 --- /dev/null +++ b/pcbnew/router/pns_logger.cpp @@ -0,0 +1,173 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "pns_logger.h" +#include "pns_item.h" +#include "pns_via.h" +#include "pns_line.h" +#include "pns_segment.h" +#include "pns_solid.h" + +#include +#include +#include +#include + +PNS_LOGGER::PNS_LOGGER( ) +{ + m_groupOpened = false; +} + + +PNS_LOGGER::~PNS_LOGGER() +{ + +} + +void PNS_LOGGER::Clear() +{ + m_theLog.str(std::string()); + m_groupOpened = false; +} + +void PNS_LOGGER::NewGroup ( const std::string& name, int iter) +{ + if(m_groupOpened) + m_theLog << "endgroup" << std::endl; + m_theLog << "group " << name << " " << iter << std::endl; + m_groupOpened = true; +} + +void PNS_LOGGER::EndGroup() +{ + if(!m_groupOpened) + return; + + m_groupOpened = false; + m_theLog << "endgroup" << std::endl; +} + +void PNS_LOGGER::Log ( const PNS_ITEM *item, int kind, const std::string name ) +{ + m_theLog << "item " << kind << " " << name << " "; + m_theLog << item->Net() << " " << item->Layers().Start() << " " << item->Layers().End() << " " << item->Marker() << " " << item->Rank(); + + switch(item->Kind()) + { + case PNS_ITEM::LINE: + { + PNS_LINE *l = (PNS_LINE *) item; + m_theLog << " line "; + m_theLog << l->Width() << " " << (l->EndsWithVia() ? 1 : 0) << " "; + dumpShape ( l->Shape() ); + m_theLog << std::endl; + break; + } + case PNS_ITEM::VIA: + { + m_theLog << " via 0 0 "; + dumpShape ( item->Shape() ); + m_theLog << std::endl; + break; + } + + case PNS_ITEM::SEGMENT: + { + PNS_SEGMENT *s =(PNS_SEGMENT *) item; + m_theLog << " line "; + m_theLog << s->Width() << " 0 linechain 2 0 " << s->Seg().A.x << " " <Seg().A.y << " " << s->Seg().B.x << " " <Seg().B.y << std::endl; + break; + } + + case PNS_ITEM::SOLID: + { + PNS_SOLID *s = (PNS_SOLID*) item; + m_theLog << " solid 0 0 "; + dumpShape ( s->Shape() ); + m_theLog << std::endl; + break; + } + + default: + break; + } +} + +void PNS_LOGGER::Log ( const SHAPE_LINE_CHAIN *l, int kind, const std::string name ) +{ + m_theLog << "item " << kind << " " << name << " "; + m_theLog << 0 << " " << 0 << " " << 0 << " " << 0 << " " << 0; + m_theLog << " line "; + m_theLog << 0 << " " << 0 << " "; + dumpShape ( l ); + m_theLog << std::endl; +} + +void PNS_LOGGER::Log ( const VECTOR2I& start, const VECTOR2I& end, int kind , const std::string name) +{ + +} + +void PNS_LOGGER::dumpShape ( const SHAPE * sh ) +{ + switch(sh->Type()) + { + case SH_LINE_CHAIN: + { + const SHAPE_LINE_CHAIN *lc = (const SHAPE_LINE_CHAIN *) sh; + m_theLog << "linechain " << lc->PointCount() << " " << (lc->IsClosed() ? 1 : 0) << " "; + for(int i = 0; i < lc->PointCount(); i++) + m_theLog << lc->CPoint(i).x << " " << lc->CPoint(i).y << " "; + break; + } + case SH_CIRCLE: + { + const SHAPE_CIRCLE *c = (const SHAPE_CIRCLE *) sh; + m_theLog << "circle " << c->GetCenter().x << " " << c->GetCenter().y << " " << c->GetRadius(); + break; + } + case SH_RECT: + { + const SHAPE_RECT *r = (const SHAPE_RECT *) sh; + m_theLog << "rect " << r->GetPosition().x << " " << r->GetPosition().y << " " << r->GetSize().x << " " <GetSize().y; + break; + } + case SH_SEGMENT: + { + const SHAPE_SEGMENT *s = (const SHAPE_SEGMENT *) sh; + m_theLog << "linechain 2 0 " << s->GetSeg().A.x << " " << s->GetSeg().A.y << " " << s->GetSeg().B.x << " " << s->GetSeg().B.y; + break; + } + default: + break; + } +} + +void PNS_LOGGER::Save ( const std::string& filename ) +{ + + EndGroup(); + + FILE *f=fopen(filename.c_str(),"wb"); + printf("Saving to '%s' [%p]\n", filename.c_str(), f); + const std::string s = m_theLog.str(); + fwrite(s.c_str(), 1, s.length(), f); + fclose(f); +} diff --git a/pcbnew/router/pns_logger.h b/pcbnew/router/pns_logger.h new file mode 100644 index 0000000000..cc112908ae --- /dev/null +++ b/pcbnew/router/pns_logger.h @@ -0,0 +1,58 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __PNS_LOGGER_H +#define __PNS_LOGGER_H + +#include +#include +#include +#include + +#include + +class PNS_ITEM; +class SHAPE_LINE_CHAIN; +class SHAPE; + +class PNS_LOGGER { + +public: + PNS_LOGGER(); + ~PNS_LOGGER(); + + void Save ( const std::string& filename ); + + void Clear(); + void NewGroup ( const std::string& name, int iter = 0); + void EndGroup(); + void Log ( const PNS_ITEM *item, int kind = 0, const std::string name = std::string () ); + void Log ( const SHAPE_LINE_CHAIN *l, int kind = 0, const std::string name = std::string () ); + void Log ( const VECTOR2I& start, const VECTOR2I& end, int kind = 0, const std::string name = std::string () ); + +private: + + void dumpShape ( const SHAPE* sh ); + + bool m_groupOpened; + std::stringstream m_theLog; +}; + +#endif diff --git a/pcbnew/router/pns_node.cpp b/pcbnew/router/pns_node.cpp index 825be77e57..4413289dfb 100644 --- a/pcbnew/router/pns_node.cpp +++ b/pcbnew/router/pns_node.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include @@ -36,31 +36,40 @@ #include "pns_solid.h" #include "pns_joint.h" #include "pns_index.h" +#include "pns_router.h" using boost::unordered_set; using boost::unordered_map; +#ifdef DEBUG static boost::unordered_set allocNodes; +#endif PNS_NODE::PNS_NODE() { - // printf("MakeNode [%p, total = %d]\n", this, allocNodes.size()); + TRACE( 0, "PNS_NODE::create %p", this ); + m_depth = 0; m_root = this; m_parent = NULL; m_maxClearance = 800000; // fixme: depends on how thick traces are. m_index = new PNS_INDEX; +#ifdef DEBUG allocNodes.insert( this ); +#endif } - PNS_NODE::~PNS_NODE() { + TRACE( 0, "PNS_NODE::delete %p", this ); + if( !m_children.empty() ) { TRACEn( 0, "attempting to free a node that has kids.\n" ); assert( false ); } +#ifdef DEBUG + if( allocNodes.find( this ) == allocNodes.end() ) { TRACEn( 0, "attempting to free an already-free'd node.\n" ); @@ -69,6 +78,8 @@ PNS_NODE::~PNS_NODE() allocNodes.erase( this ); +#endif + for( PNS_INDEX::ItemSet::iterator i = m_index->begin(); i != m_index->end(); ++i ) if( (*i)->BelongsTo( this ) ) @@ -81,30 +92,19 @@ PNS_NODE::~PNS_NODE() int PNS_NODE::GetClearance( const PNS_ITEM* a, const PNS_ITEM* b ) const { - int clearance = (*m_clearanceFunctor)( a, b ); - - if( a->OfKind( PNS_ITEM::SEGMENT ) ) - clearance += static_cast(a)->GetWidth() / 2; - - if( a->OfKind( PNS_ITEM::LINE ) ) - clearance += static_cast(a)->GetWidth() / 2; - - if( b->OfKind( PNS_ITEM::SEGMENT ) ) - clearance += static_cast(b)->GetWidth() / 2; - - if( b->OfKind( PNS_ITEM::LINE ) ) - clearance += static_cast(b)->GetWidth() / 2; - - return clearance; + return (*m_clearanceFunctor)( a, b ); } - PNS_NODE* PNS_NODE::Branch() { PNS_NODE* child = new PNS_NODE; + TRACE( 0, "PNS_NODE::branch %p (parent %p)", child % this ); + + m_children.push_back( child ); + child->m_depth = m_depth + 1; child->m_parent = this; child->m_clearanceFunctor = m_clearanceFunctor; child->m_root = isRoot() ? this : m_root; @@ -173,14 +173,21 @@ struct PNS_NODE::obstacleVisitor ///> number of items found so far int m_matchCount; + ///> additional clearance + int m_extraClearance; + obstacleVisitor( PNS_NODE::Obstacles& aTab, const PNS_ITEM* aItem, int aKindMask ) : m_tab( aTab ), m_item( aItem ), m_kindMask( aKindMask ), m_limitCount( -1 ), - m_matchCount( 0 ) - {}; + m_matchCount( 0 ), + m_extraClearance( 0 ) + { + if(aItem->Kind() == PNS_ITEM::LINE) + m_extraClearance += static_cast(aItem)->Width() / 2; + }; void SetCountLimit( int aLimit ) { @@ -203,7 +210,10 @@ struct PNS_NODE::obstacleVisitor if( m_override && m_override->overrides( aItem ) ) return true; - int clearance = m_node->GetClearance( aItem, m_item ); + int clearance = m_extraClearance + m_node->GetClearance( aItem, m_item ); + + if(aItem->Kind() == PNS_ITEM::LINE) + clearance += static_cast(aItem)->Width() / 2; if( !aItem->Collide( m_item, clearance ) ) return true; @@ -228,12 +238,14 @@ int PNS_NODE::QueryColliding( const PNS_ITEM* aItem, { obstacleVisitor visitor( aObstacles, aItem, aKindMask ); +#ifdef DEBUG assert( allocNodes.find( this ) != allocNodes.end() ); +#endif visitor.SetCountLimit( aLimitCount ); visitor.SetWorld( this, NULL ); - // first, look for colliding items ourselves + // first, look for colliding items in the local index m_index->Query( aItem, m_maxClearance, visitor ); // if we haven't found enough items, look in the root branch as well. @@ -252,7 +264,7 @@ PNS_NODE::OptObstacle PNS_NODE::NearestObstacle( const PNS_LINE* aItem, int aKin Obstacles obs_list; bool found_isects = false; - const SHAPE_LINE_CHAIN& line = aItem->GetCLine(); + const SHAPE_LINE_CHAIN& line = aItem->CLine(); obs_list.reserve( 100 ); @@ -265,7 +277,7 @@ PNS_NODE::OptObstacle PNS_NODE::NearestObstacle( const PNS_LINE* aItem, int aKin } if( aItem->EndsWithVia() ) - n += QueryColliding( &aItem->GetVia(), obs_list, aKindMask ); + n += QueryColliding( &aItem->Via(), obs_list, aKindMask ); // if(! QueryColliding ( aItem, obs_list, aKindMask )) if( !n ) @@ -286,20 +298,20 @@ PNS_NODE::OptObstacle PNS_NODE::NearestObstacle( const PNS_LINE* aItem, int aKin int clearance = GetClearance( obs.item, &aLine ); - SHAPE_LINE_CHAIN hull = obs.item->Hull( clearance ); + SHAPE_LINE_CHAIN hull = obs.item->Hull( clearance, aItem->Width() ); if( aLine.EndsWithVia() ) { - int clearance = GetClearance( obs.item, &aLine.GetVia() ); + int clearance = GetClearance( obs.item, &aLine.Via() ); - SHAPE_LINE_CHAIN viaHull = aLine.GetVia().Hull( clearance ); + SHAPE_LINE_CHAIN viaHull = aLine.Via().Hull( clearance, aItem->Width() ); viaHull.Intersect( hull, isect_list ); BOOST_FOREACH( SHAPE_LINE_CHAIN::INTERSECTION isect, isect_list ) { - int dist = aLine.GetCLine().Length() + - ( isect.p - aLine.GetVia().GetPos() ).EuclideanNorm(); + int dist = aLine.CLine().Length() + + ( isect.p - aLine.Via().Pos() ).EuclideanNorm(); if( dist < nearest.dist_first ) { @@ -320,11 +332,11 @@ PNS_NODE::OptObstacle PNS_NODE::NearestObstacle( const PNS_LINE* aItem, int aKin isect_list.clear(); - hull.Intersect( aLine.GetCLine(), isect_list ); + hull.Intersect( aLine.CLine(), isect_list ); BOOST_FOREACH( SHAPE_LINE_CHAIN::INTERSECTION isect, isect_list ) { - int dist = aLine.GetCLine().PathLength( isect.p ); + int dist = aLine.CLine().PathLength( isect.p ); if( dist < nearest.dist_first ) { @@ -346,9 +358,22 @@ PNS_NODE::OptObstacle PNS_NODE::NearestObstacle( const PNS_LINE* aItem, int aKin nearest.dist_last = dist_max; } - return found_isects ? nearest : OptObstacle(); + if(!found_isects) + nearest.item = obs_list[0].item; + + return nearest; } +PNS_NODE::OptObstacle PNS_NODE::CheckColliding( const PNS_ITEMSET& aSet, int aKindMask ) +{ + BOOST_FOREACH( const PNS_ITEM *item, aSet.CItems() ) + { + OptObstacle obs = CheckColliding(item, aKindMask); + if(obs) + return obs; + } + return OptObstacle(); +} PNS_NODE::OptObstacle PNS_NODE::CheckColliding( const PNS_ITEM* aItemA, int aKindMask ) { @@ -356,11 +381,11 @@ PNS_NODE::OptObstacle PNS_NODE::CheckColliding( const PNS_ITEM* aItemA, int aKin obs.reserve( 100 ); - if( aItemA->GetKind() == PNS_ITEM::LINE ) + if( aItemA->Kind() == PNS_ITEM::LINE ) { int n = 0; const PNS_LINE* line = static_cast(aItemA); - const SHAPE_LINE_CHAIN& l = line->GetCLine(); + const SHAPE_LINE_CHAIN& l = line->CLine(); for( int i = 0; i < l.SegmentCount(); i++ ) { @@ -373,7 +398,7 @@ PNS_NODE::OptObstacle PNS_NODE::CheckColliding( const PNS_ITEM* aItemA, int aKin if( line->EndsWithVia() ) { - n += QueryColliding( &line->GetVia(), obs, aKindMask, 1 ); + n += QueryColliding( &line->Via(), obs, aKindMask, 1 ); if( n ) return OptObstacle( obs[0] ); @@ -388,12 +413,16 @@ PNS_NODE::OptObstacle PNS_NODE::CheckColliding( const PNS_ITEM* aItemA, int aKin bool PNS_NODE::CheckColliding( const PNS_ITEM* aItemA, const PNS_ITEM* aItemB, int aKindMask ) { - Obstacles dummy; - assert( aItemB ); - // return QueryColliding(aItemA, dummy, aKindMask, 1) > 0; + int clearance = GetClearance( aItemA, aItemB ); - return aItemA->Collide( aItemB, GetClearance( aItemA, aItemB ) ); + // fixme: refactor + if(aItemA->Kind() == PNS_ITEM::LINE) + clearance += static_cast(aItemA)->Width() / 2; + if(aItemB->Kind() == PNS_ITEM::LINE) + clearance += static_cast(aItemB)->Width() / 2; + + return aItemA->Collide( aItemB, clearance ); } @@ -412,10 +441,7 @@ struct hitVisitor int cl = 0; - if( aItem->GetKind() == PNS_ITEM::SEGMENT ) - cl += static_cast(aItem)->GetWidth() / 2; - - if( aItem->GetShape()->Collide( &cp, cl ) ) + if( aItem->Shape()->Collide( &cp, cl ) ) m_items.Add( aItem ); return true; @@ -426,6 +452,7 @@ struct hitVisitor const PNS_ITEMSET PNS_NODE::HitTest( const VECTOR2I& aPoint ) const { PNS_ITEMSET items; + // fixme: we treat a point as an infinitely small circle - this is inefficient. SHAPE_CIRCLE s( aPoint, 0 ); hitVisitor visitor( items, aPoint, this ); @@ -451,21 +478,21 @@ const PNS_ITEMSET PNS_NODE::HitTest( const VECTOR2I& aPoint ) const void PNS_NODE::addSolid( PNS_SOLID* aSolid ) { - linkJoint( aSolid->GetCenter(), aSolid->GetLayers(), aSolid->GetNet(), aSolid ); + linkJoint( aSolid->Pos(), aSolid->Layers(), aSolid->Net(), aSolid ); m_index->Add( aSolid ); } void PNS_NODE::addVia( PNS_VIA* aVia ) { - linkJoint( aVia->GetPos(), aVia->GetLayers(), aVia->GetNet(), aVia ); + linkJoint( aVia->Pos(), aVia->Layers(), aVia->Net(), aVia ); m_index->Add( aVia ); } -void PNS_NODE::addLine( PNS_LINE* aLine ) +void PNS_NODE::addLine( PNS_LINE* aLine, bool aAllowRedundant ) { - const SHAPE_LINE_CHAIN& l = aLine->GetLine(); + SHAPE_LINE_CHAIN& l = aLine->Line(); for( int i = 0; i < l.SegmentCount(); i++ ) { @@ -474,53 +501,65 @@ void PNS_NODE::addLine( PNS_LINE* aLine ) if( s.A != s.B ) { PNS_SEGMENT* pseg = new PNS_SEGMENT( *aLine, s ); + PNS_SEGMENT* psegR = NULL; - pseg->SetOwner( this ); + if ( !aAllowRedundant ) + psegR = findRedundantSegment( pseg ); - linkJoint( s.A, pseg->GetLayers(), aLine->GetNet(), pseg ); - linkJoint( s.B, pseg->GetLayers(), aLine->GetNet(), pseg ); + if(psegR) + aLine->LinkSegment(psegR); + else { + + pseg->SetOwner( this ); - aLine->LinkSegment( pseg ); + linkJoint( s.A, pseg->Layers(), aLine->Net(), pseg ); + linkJoint( s.B, pseg->Layers(), aLine->Net(), pseg ); - m_index->Add( pseg ); - } + aLine->LinkSegment( pseg ); + + m_index->Add( pseg ); + } + } } } -void PNS_NODE::addSegment( PNS_SEGMENT* aSeg ) +void PNS_NODE::addSegment( PNS_SEGMENT* aSeg, bool aAllowRedundant ) { - if( aSeg->GetSeg().A == aSeg->GetSeg().B ) + if( aSeg->Seg().A == aSeg->Seg().B ) { TRACEn( 0, "attempting to add a segment with same end coordinates, ignoring." ) return; } + if ( !aAllowRedundant && findRedundantSegment ( aSeg ) ) + return; + aSeg->SetOwner( this ); - linkJoint( aSeg->GetSeg().A, aSeg->GetLayers(), aSeg->GetNet(), aSeg ); - linkJoint( aSeg->GetSeg().B, aSeg->GetLayers(), aSeg->GetNet(), aSeg ); + linkJoint( aSeg->Seg().A, aSeg->Layers(), aSeg->Net(), aSeg ); + linkJoint( aSeg->Seg().B, aSeg->Layers(), aSeg->Net(), aSeg ); m_index->Add( aSeg ); } -void PNS_NODE::Add( PNS_ITEM* aItem ) +void PNS_NODE::Add( PNS_ITEM* aItem, bool aAllowRedundant ) { aItem->SetOwner( this ); - switch( aItem->GetKind() ) + switch( aItem->Kind() ) { case PNS_ITEM::SOLID: addSolid( static_cast( aItem ) ); break; case PNS_ITEM::SEGMENT: - addSegment( static_cast( aItem ) ); + addSegment( static_cast( aItem ), aAllowRedundant ); break; case PNS_ITEM::LINE: - addLine( static_cast (aItem) ); + addLine( static_cast (aItem), aAllowRedundant ); break; case PNS_ITEM::VIA: @@ -535,6 +574,9 @@ void PNS_NODE::Add( PNS_ITEM* aItem ) void PNS_NODE::doRemove( PNS_ITEM* aItem ) { + + // assert(m_root->m_index->Contains(aItem) || m_index->Contains(aItem)); + // case 1: removing an item that is stored in the root node from any branch: // mark it as overridden, but do not remove if( aItem->BelongsTo( m_root ) && !isRoot() ) @@ -553,8 +595,8 @@ void PNS_NODE::doRemove( PNS_ITEM* aItem ) void PNS_NODE::removeSegment( PNS_SEGMENT* aSeg ) { - unlinkJoint( aSeg->GetSeg().A, aSeg->GetLayers(), aSeg->GetNet(), aSeg ); - unlinkJoint( aSeg->GetSeg().B, aSeg->GetLayers(), aSeg->GetNet(), aSeg ); + unlinkJoint( aSeg->Seg().A, aSeg->Layers(), aSeg->Net(), aSeg ); + unlinkJoint( aSeg->Seg().B, aSeg->Layers(), aSeg->Net(), aSeg ); doRemove( aSeg ); } @@ -562,12 +604,18 @@ void PNS_NODE::removeSegment( PNS_SEGMENT* aSeg ) void PNS_NODE::removeLine( PNS_LINE* aLine ) { - std::vector* segRefs = aLine->GetLinkedSegments(); + std::vector* segRefs = aLine->LinkedSegments(); - if( !segRefs ) + if(! aLine->SegmentCount() ) return; - assert( aLine->GetOwner() ); + assert (segRefs != NULL); + assert (aLine->Owner()); + + if ( (int)segRefs->size() != aLine->SegmentCount()) + { + //printf("******weird deletion: segrefs %d segcount %d hasloops %d\n", segRefs->size(), aLine->SegmentCount(), aLine->HasLoops()); + } BOOST_FOREACH( PNS_SEGMENT* seg, *segRefs ) { @@ -575,17 +623,60 @@ void PNS_NODE::removeLine( PNS_LINE* aLine ) } aLine->SetOwner( NULL ); + aLine->ClearSegmentLinks(); } + void PNS_NODE::removeVia( PNS_VIA* aVia ) { - unlinkJoint( aVia->GetPos(), aVia->GetLayers(), aVia->GetNet(), aVia ); + // We have to split a single joint (associated with a via, binding together multiple layers) + // into multiple independent joints. As I'm a lazy bastard, I simply delete the via and all its links and re-insert them. - doRemove( aVia ); + PNS_JOINT::HashTag tag; + + VECTOR2I p ( aVia->Pos() ); + PNS_LAYERSET vLayers ( aVia->Layers() ); + int net = aVia->Net(); + + PNS_JOINT *jt = FindJoint( p, vLayers.Start(), net ); + PNS_JOINT::LinkedItems links ( jt->LinkList() ); + + tag.net = net; + tag.pos = p; + + bool split; + do + { + split = false; + std::pair range = m_joints.equal_range( tag ); + + if( range.first == m_joints.end() ) + break; + + // find and remove all joints containing the via to be removed + + for( JointMap::iterator f = range.first; f != range.second; ++f ) + { + if( aVia->LayersOverlap ( &f->second ) ) + { + m_joints.erase( f ); + split = true; + break; + } + } + } while (split); + + // and re-link them, using the former via's link list + BOOST_FOREACH(PNS_ITEM *item, links) + { + if( item != aVia ) + linkJoint ( p, item->Layers(), net, item ); + } + + doRemove( aVia ); } - void PNS_NODE::Replace( PNS_ITEM* aOldItem, PNS_ITEM* aNewItem ) { Remove( aOldItem ); @@ -595,10 +686,11 @@ void PNS_NODE::Replace( PNS_ITEM* aOldItem, PNS_ITEM* aNewItem ) void PNS_NODE::Remove( PNS_ITEM* aItem ) { - switch( aItem->GetKind() ) + switch( aItem->Kind() ) { case PNS_ITEM::SOLID: - assert( false ); + // fixme: this fucks up the joints, but it's only used for marking colliding obstacles for the moment, so we don't care. + doRemove ( aItem ); break; case PNS_ITEM::SEGMENT: @@ -627,13 +719,13 @@ void PNS_NODE::followLine( PNS_SEGMENT* current, bool scanDirection, int& pos, for( ; ; ) { const VECTOR2I p = - (scanDirection ^ prevReversed) ? current->GetSeg().B : current->GetSeg().A; - const OptJoint jt = FindJoint( p, current->GetLayer(), current->GetNet() ); + (scanDirection ^ prevReversed) ? current->Seg().B : current->Seg().A; + const PNS_JOINT *jt = FindJoint( p, current ); assert( jt ); assert( pos > 0 && pos < limit ); - corners[pos] = jt->GetPos(); + corners[pos] = jt->Pos(); segments[pos] = current; pos += (scanDirection ? 1 : -1); @@ -642,13 +734,14 @@ void PNS_NODE::followLine( PNS_SEGMENT* current, bool scanDirection, int& pos, break; current = jt->NextSegment( current ); + prevReversed = - ( jt->GetPos() == (scanDirection ? current->GetSeg().B : current->GetSeg().A ) ); + ( jt->Pos() == (scanDirection ? current->Seg().B : current->Seg().A ) ); } } -PNS_LINE* PNS_NODE::AssembleLine( PNS_SEGMENT* aSeg, const OptJoint& a, const OptJoint& b ) +PNS_LINE* PNS_NODE::AssembleLine( PNS_SEGMENT* aSeg, int *aOriginSegmentIndex) { const int MaxVerts = 1024; @@ -658,61 +751,151 @@ PNS_LINE* PNS_NODE::AssembleLine( PNS_SEGMENT* aSeg, const OptJoint& a, const Op PNS_LINE* pl = new PNS_LINE; int i_start = MaxVerts / 2, i_end = i_start + 1; - pl->SetWidth( aSeg->GetWidth() ); - pl->SetLayers( aSeg->GetLayers() ); - pl->SetNet( aSeg->GetNet() ); + pl->SetWidth( aSeg->Width() ); + pl->SetLayers( aSeg->Layers() ); + pl->SetNet( aSeg->Net() ); pl->SetOwner( this ); - // pl->LinkSegment(aSeg); - followLine( aSeg, false, i_start, MaxVerts, corners, segs ); followLine( aSeg, true, i_end, MaxVerts, corners, segs ); + int n = 0; - int clip_start = -1, clip_end = -1; + PNS_SEGMENT *prev_seg = NULL; for( int i = i_start + 1; i < i_end; i++ ) { const VECTOR2I& p = corners[i]; - if( a && ( p == a->GetPos() || p == b->GetPos() ) ) + pl->Line().Append( p ); + + if( prev_seg != segs[i] ) { - clip_start = std::min( clip_start, i ); - clip_end = std::max( clip_end, i ); + pl->LinkSegment( segs[i] ); + + if(segs[i] == aSeg && aOriginSegmentIndex) + *aOriginSegmentIndex = n; + n++; } - pl->GetLine().Append( p ); - - if( segs[i - 1] != segs[i] ) - pl->LinkSegment( segs[i] ); + prev_seg = segs[i]; } + + assert (pl->SegmentCount() != 0); + assert (pl->SegmentCount() == (int) pl->LinkedSegments()->size()); + return pl; } void PNS_NODE::FindLineEnds( PNS_LINE* aLine, PNS_JOINT& a, PNS_JOINT& b ) { - a = *FindJoint( aLine->GetCLine().CPoint( 0 ), aLine->GetLayers().Start(), aLine->GetNet() ); - b = *FindJoint( aLine->GetCLine().CPoint( -1 ), aLine->GetLayers().Start(), aLine->GetNet() ); + a = *FindJoint( aLine->CPoint( 0 ), aLine ); + b = *FindJoint( aLine->CPoint( -1 ), aLine ); } +void PNS_NODE::MapConnectivity ( PNS_JOINT* aStart, std::vector & aFoundJoints ) +{ + std::deque searchQueue; + std::set processed; + + searchQueue.push_back(aStart); + processed.insert ( aStart ); + + while(!searchQueue.empty()) + { + PNS_JOINT *current = searchQueue.front(); + searchQueue.pop_front(); + + BOOST_FOREACH ( PNS_ITEM *item, current->LinkList() ) + if ( item->OfKind( PNS_ITEM::SEGMENT ) ) + { + PNS_SEGMENT *seg = static_cast(item); + PNS_JOINT *a = FindJoint( seg->Seg().A, seg ); + PNS_JOINT *b = FindJoint( seg->Seg().B, seg ); + PNS_JOINT *next = (*a == *current) ? b : a; + + if( processed.find( next ) == processed.end() ) + { + processed.insert ( next ); + searchQueue.push_back( next ); + } + } + } + + BOOST_FOREACH(PNS_JOINT *jt, processed) + aFoundJoints.push_back( jt ); +} + +PNS_ITEM *PNS_NODE::NearestUnconnectedItem ( PNS_JOINT *aStart, int *aAnchor, int aKindMask ) +{ + std::set disconnected; + std::vector joints; + + AllItemsInNet( aStart->Net(), disconnected ); + MapConnectivity ( aStart, joints ); + + BOOST_FOREACH(PNS_JOINT *jt, joints) + { + BOOST_FOREACH (PNS_ITEM *link, jt->LinkList() ) + { + if(disconnected.find(link) != disconnected.end() ) + disconnected.erase(link); + } + } + + int best_dist = INT_MAX; + PNS_ITEM *best = NULL; + + BOOST_FOREACH (PNS_ITEM *item, disconnected ) + { + if( item->OfKind ( aKindMask ) ) + { + for(int i = 0; i < item->AnchorCount(); i++) + { + VECTOR2I p = item->Anchor ( i ); + int d = (p - aStart->Pos()).EuclideanNorm(); + + if(d < best_dist) + { + best_dist = d; + best = item; + if(aAnchor) + *aAnchor = i; + } + } + } + } + + return best; +} int PNS_NODE::FindLinesBetweenJoints( PNS_JOINT& a, PNS_JOINT& b, std::vector& aLines ) { - BOOST_FOREACH( PNS_ITEM* item, a.GetLinkList() ) + BOOST_FOREACH( PNS_ITEM* item, a.LinkList() ) { - if( item->GetKind() == PNS_ITEM::SEGMENT ) + if( item->Kind() == PNS_ITEM::SEGMENT ) { PNS_SEGMENT* seg = static_cast(item); PNS_LINE* line = AssembleLine( seg ); PNS_JOINT j_start, j_end; + FindLineEnds( line, j_start, j_end ); - if( (j_start == a && j_end == b )|| (j_end == a && j_start == b) ) + + int id_start = line->CLine().Find (a.Pos()); + int id_end = line->CLine().Find (b.Pos()); + + if(id_end < id_start) + std::swap(id_end, id_start); + + if(id_start >= 0 && id_end >= 0) + { + line->ClipVertexRange ( id_start, id_end ); aLines.push_back( line ); - else + } else delete line; } } @@ -721,7 +904,7 @@ int PNS_NODE::FindLinesBetweenJoints( PNS_JOINT& a, PNS_JOINT& b, std::vectorsecond.GetLayers().Overlaps( aLayer ) ) - return f->second; + if( f->second.Layers().Overlaps( aLayer ) ) + return &f->second; ++f; } - return OptJoint(); + return NULL; } @@ -787,7 +970,7 @@ PNS_JOINT& PNS_NODE::touchJoint( const VECTOR2I& aPos, const PNS_LAYERSET& aLaye for( f = range.first; f != range.second; ++f ) { - if( aLayers.Overlaps( f->second.GetLayers() ) ) + if( aLayers.Overlaps( f->second.Layers() ) ) { jt.Merge( f->second ); m_joints.erase( f ); @@ -949,7 +1132,11 @@ void PNS_NODE::Commit( PNS_NODE* aNode ) for( PNS_INDEX::ItemSet::iterator i = aNode->m_index->begin(); i != aNode->m_index->end(); ++i ) + { + (*i)->SetRank ( -1 ); + (*i)->Unmark (); Add( *i ); + } releaseChildren(); } @@ -957,29 +1144,81 @@ void PNS_NODE::Commit( PNS_NODE* aNode ) void PNS_NODE::KillChildren() { - assert( isRoot() ); - + assert ( isRoot() ); releaseChildren(); } -void PNS_NODE::AllItemsInNet( int aNet, std::list& aItems ) +void PNS_NODE::AllItemsInNet( int aNet, std::set& aItems ) { PNS_INDEX::NetItemsList* l_cur = m_index->GetItemsForNet( aNet ); - if( !l_cur ) - return; - - std::copy( aItems.begin(), l_cur->begin(), l_cur->end() ); + if(l_cur) + { + BOOST_FOREACH (PNS_ITEM *item, *l_cur ) + aItems.insert ( item ); + } + if( !isRoot() ) { PNS_INDEX::NetItemsList* l_root = m_root->m_index->GetItemsForNet( aNet ); - for( PNS_INDEX::NetItemsList::iterator i = l_root->begin(); i!= l_root->end(); ++i ) - if( !overrides( *i ) ) - aItems.push_back( *i ); - - + if(l_root) + for( PNS_INDEX::NetItemsList::iterator i = l_root->begin(); i!= l_root->end(); ++i ) + if( !overrides( *i ) ) + aItems.insert( *i ); } } + +void PNS_NODE::ClearRanks() +{ + for( PNS_INDEX::ItemSet::iterator i = m_index->begin(); i != m_index->end(); ++i ) + { + (*i)->SetRank(-1); + (*i)->Mark(0); + } +} + +int PNS_NODE::FindByMarker ( int aMarker, PNS_ITEMSET& aItems ) +{ + for( PNS_INDEX::ItemSet::iterator i = m_index->begin(); i != m_index->end(); ++i ) + if ( (*i)->Marker() & aMarker ) aItems.Add(*i); + return 0; +} + +int PNS_NODE::RemoveByMarker ( int aMarker ) +{ + for( PNS_INDEX::ItemSet::iterator i = m_index->begin(); i != m_index->end(); ++i ) + if ( (*i)->Marker() & aMarker ) + { + Remove (*i); + } + return 0; +} + +PNS_SEGMENT* PNS_NODE::findRedundantSegment ( PNS_SEGMENT *aSeg ) +{ + PNS_JOINT *jtStart = FindJoint ( aSeg->Seg().A, aSeg ); + + if(!jtStart) + return NULL; + + BOOST_FOREACH( PNS_ITEM *item, jtStart->LinkList() ) + if(item->OfKind(PNS_ITEM::SEGMENT)) + { + PNS_SEGMENT *seg2 = (PNS_SEGMENT *) item; + + const VECTOR2I a1 ( aSeg->Seg().A ); + const VECTOR2I b1 ( aSeg->Seg().B ); + + const VECTOR2I a2 ( seg2->Seg().A ); + const VECTOR2I b2 ( seg2->Seg().B ); + + if( seg2->Layers().Start() == aSeg->Layers().Start() && + ((a1 == a2 && b1 == b2) || (a1 == b2 && a2 == b1))) + return seg2; + } + + return NULL; +} diff --git a/pcbnew/router/pns_node.h b/pcbnew/router/pns_node.h index de3e886dc2..4242f8d614 100644 --- a/pcbnew/router/pns_node.h +++ b/pcbnew/router/pns_node.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,8 +15,9 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ + #ifndef __PNS_NODE_H #define __PNS_NODE_H @@ -26,7 +27,6 @@ #include #include #include -#include #include #include @@ -43,8 +43,12 @@ class PNS_VIA; class PNS_RATSNEST; class PNS_INDEX; -using boost::shared_ptr; +/** + * Class PNS_CLEARANCE_FUNC + * + * An abstract function object, returning a required clearance between two items. + **/ class PNS_CLEARANCE_FUNC { public: @@ -84,7 +88,7 @@ struct PNS_OBSTACLE * hierarchical and indexed way. * Features: * - spatial-indexed container for PCB item shapes - * - collision search (with clearance checking) + * - collision search & clearance checking * - assembly of lines connecting joints, finding loops and unique paths * - lightweight cloning/branching (for recursive optimization and shove * springback) @@ -96,101 +100,236 @@ public: typedef boost::optional OptObstacle; typedef std::vector ItemVector; typedef std::vector Obstacles; - typedef boost::optional OptJoint; - - PNS_NODE(); - ~PNS_NODE(); + + PNS_NODE (); + ~PNS_NODE (); ///> Returns the expected clearance between items a and b. - int GetClearance( const PNS_ITEM* a, const PNS_ITEM* b ) const; + int GetClearance ( const PNS_ITEM* a, const PNS_ITEM* b ) const; ///> Returns the pre-set worst case clearance between any pair of items - int GetMaxClearance() const + int GetMaxClearance () const { return m_maxClearance; } - void SetMaxClearance( int aClearance ) + ///> Sets the worst-case clerance between any pair of items + void SetMaxClearance ( int aClearance ) { m_maxClearance = aClearance; } - void SetClearanceFunctor( PNS_CLEARANCE_FUNC* aFunc ) + ///> Assigns a clerance resolution function object + void SetClearanceFunctor ( PNS_CLEARANCE_FUNC* aFunc ) { m_clearanceFunctor = aFunc; } - ///> Finds items that collide with aItem and stores collision information - ///> in aObstacles. - int QueryColliding( const PNS_ITEM* aItem, - Obstacles& aObstacles, - int aKindMask = PNS_ITEM::ANY, - int aLimitCount = -1 ); - - ///> Finds the nearest item that collides with aItem. - OptObstacle NearestObstacle( const PNS_LINE* aItem, int aKindMask = PNS_ITEM::ANY ); - - ///> Checks if the item collides with anything else in the world, - ///> and returns it if so. - OptObstacle CheckColliding( const PNS_ITEM* aItem, int aKindMask = PNS_ITEM::ANY ); - - ///> Checks if two items collide [deprecated]. - bool CheckColliding( const PNS_ITEM* aItemA, - const PNS_ITEM* aItemB, - int aKindMask = PNS_ITEM::ANY ); - - ///> Hit detection - const PNS_ITEMSET HitTest( const VECTOR2I& aPoint ) const; - - void Add( PNS_ITEM* aItem ); - void Remove( PNS_ITEM* aItem ); - void Replace( PNS_ITEM* aOldItem, PNS_ITEM* aNewItem ); - - ///> Creates a lightweight copy ("branch") of self. Note that if there are - ///> any branches in use, their parents must NOT be deleted. - PNS_NODE* Branch(); - - ///> Assembles a line connecting two non-trivial joints the - ///> segment aSeg belongs to. - PNS_LINE* AssembleLine( PNS_SEGMENT* aSeg, - const OptJoint& a = OptJoint(), const OptJoint& b = OptJoint() ); - - ///> Dumps the contents and joints structure - void Dump( bool aLong = false ); - ///> Returns the number of joints int JointCount() const { return m_joints.size(); } - ///> Returns the lists of items removed and added in this branch, with - ///> respect to the root. + ///> Returns the number of nodes in the inheritance chain (wrs to the root node) + int Depth() const + { + return m_depth; + } + + /** + * Function QueryColliding() + * + * Finds items collliding (closer than clearance) with the item aItem. + * @param aItem item to check collisions against + * @param aObstacles set of colliding objects found + * @param aKindMask mask of obstacle types to take into account + * @param aLimitCount stop looking for collisions after finding this number of colliding items + * @return number of obstacles found + */ + int QueryColliding ( const PNS_ITEM* aItem, + Obstacles& aObstacles, + int aKindMask = PNS_ITEM::ANY, + int aLimitCount = -1 ); + + + /** + * Function NearestObstacle() + * + * Follows the line in search of an obstacle that is nearest to the starting to the line's starting + * point. + * @param aItem the item to find collisions with + * @param aKindMask mask of obstacle types to take into account + * @return the obstacle, if found, otherwise empty. + */ + OptObstacle NearestObstacle ( const PNS_LINE* aItem, + int aKindMask = PNS_ITEM::ANY ); + + /** + * Function CheckColliding() + * + * Checks if the item collides with anything else in the world, + * and if found, returns the obstacle. + * @param aItem the item to find collisions with + * @param aKindMask mask of obstacle types to take into account + * @return the obstacle, if found, otherwise empty. + */ + OptObstacle CheckColliding ( const PNS_ITEM* aItem, + int aKindMask = PNS_ITEM::ANY ); + + + /** + * Function CheckColliding() + * + * Checks if any item in the set collides with anything else in the world, + * and if found, returns the obstacle. + * @param aSet set of items to find collisions with + * @param aKindMask mask of obstacle types to take into account + * @return the obstacle, if found, otherwise empty. + */ + OptObstacle CheckColliding ( const PNS_ITEMSET& aSet, + int aKindMask = PNS_ITEM::ANY ); + + + /** + * Function CheckColliding() + * + * Checks if any item in the set collides with anything else in the world, + * and if found, returns the obstacle. + * @param aSet set of items to find collisions with + * @param aKindMask mask of obstacle types to take into account + * @return the obstacle, if found, otherwise empty. + */ + bool CheckColliding ( const PNS_ITEM* aItemA, + const PNS_ITEM* aItemB, + int aKindMask = PNS_ITEM::ANY ); + + /** + * Function HitTest() + * + * Finds all items that contain the point aPoint. + * @param aPoint the point + * @return the items + */ + const PNS_ITEMSET HitTest ( const VECTOR2I& aPoint ) const; + + /** + * Function Add() + * + * Adds an item to the current node. + * @param aItem item to add + * @param aAllowRedundant if true, duplicate items are allowed (e.g. a segment or via + * at the same coordinates as an existing one) + */ + void Add ( PNS_ITEM* aItem, bool aAllowRedundant = false ); + + /** + * Function Remove() + * + * Just as the name says, removes an item from this branch. + * @param aItem item to remove + */ + void Remove ( PNS_ITEM* aItem ); + + /** + * Function Replace() + * + * Just as the name says, replaces an item with another one. + * @param aOldItem item to be removed + * @param aNewItem item add instead + */ + void Replace ( PNS_ITEM* aOldItem, PNS_ITEM* aNewItem ); + + /** + * Function Branch() + * + * Creates a lightweight copy (called branch) of self that tracks + * the changes (added/removed items) wrs to the root. Note that if there are + * any branches in use, their parents must NOT be deleted. + * @return the new branch + */ + PNS_NODE* Branch ( ); + + /** + * Function AssembleLine() + * + * Follows the joint map to assemble a line connecting two non-trivial + * joints starting from segment aSeg. + * @param aSeg the initial segment + * @param aOriginSegmentIndex index of aSeg in the resulting line + * @return the line + */ + + PNS_LINE* AssembleLine ( PNS_SEGMENT* aSeg, + int *aOriginSegmentIndex = NULL ); + + ///> Prints the contents and joints structure + void Dump ( bool aLong = false ); + + + + /** + * Function GetUpdatedItems() + * + * Returns the lists of items removed and added in this branch, with + * respect to the root branch. + * @param aRemoved removed items + * @param aAdded added items + */ void GetUpdatedItems( ItemVector& aRemoved, ItemVector& aAdded ); - ///> Copies the changes from a given branch (aNode) to the root. Called on - ///> a non-root branch will fail. + + /** + * Function Commit() + * + * Applies the changes from a given branch (aNode) to the root branch. Called on + * a non-root branch will fail. Calling commit also kills all children nodes of the root branch. + * @param aNode node to commit changes from + */ void Commit( PNS_NODE* aNode ); - ///> finds a joint at a given position, layer and nets - const OptJoint FindJoint( const VECTOR2I& aPos, int aLayer, int aNet ); + /** + * Function FindJoint() + * + * Searches for a joint at a given position, layer and belonging to given net. + * @return the joint, if found, otherwise empty + */ + PNS_JOINT* FindJoint( const VECTOR2I& aPos, int aLayer, int aNet ); - ///> finds all linest between a pair of joints. Used by the loop removal engine. - int FindLinesBetweenJoints( PNS_JOINT& a, PNS_JOINT& b, - std::vector& aLines ); + /** + * Function FindJoint() + * + * Searches for a joint at a given position, linked to given item. + * @return the joint, if found, otherwise empty + */ + PNS_JOINT* FindJoint( const VECTOR2I& aPos, PNS_ITEM *aItem ) + { + return FindJoint( aPos, aItem->Layers().Start(), aItem->Net() ); + } + + void MapConnectivity ( PNS_JOINT* aStart, std::vector & aFoundJoints ); + + PNS_ITEM *NearestUnconnectedItem ( PNS_JOINT *aStart, int *aAnchor = NULL, int aKindMask = PNS_ITEM::ANY); + + + ///> finds all lines between a pair of joints. Used by the loop removal procedure. + int FindLinesBetweenJoints( PNS_JOINT& a, + PNS_JOINT& b, + std::vector& aLines ); ///> finds the joints corresponding to the ends of line aLine void FindLineEnds( PNS_LINE* aLine, PNS_JOINT& a, PNS_JOINT& b ); - - ///> finds all joints that have an (in)direct connection(s) - ///> (i.e. segments/vias) with the joint aJoint. - void FindConnectedJoints( const PNS_JOINT& aJoint, - std::vector& aConnectedJoints ); - + ///> Destroys all child nodes. Applicable only to the root node. void KillChildren(); - void AllItemsInNet( int aNet, std::list& aItems ); + void AllItemsInNet( int aNet, std::set& aItems ); + + void ClearRanks(); + + + int FindByMarker ( int aMarker, PNS_ITEMSET& aItems ); + int RemoveByMarker ( int aMarker ); private: struct obstacleVisitor; @@ -198,12 +337,13 @@ private: typedef JointMap::value_type TagJointPair; /// nodes are not copyable - PNS_NODE( const PNS_NODE& b ); - PNS_NODE& operator=( const PNS_NODE& b ); + PNS_NODE ( const PNS_NODE& b ); + PNS_NODE& operator= ( const PNS_NODE& b ); ///> tries to find matching joint and creates a new one if not found - PNS_JOINT& touchJoint( const VECTOR2I& aPos, const PNS_LAYERSET& aLayers, - int aNet ); + PNS_JOINT& touchJoint( const VECTOR2I& aPos, + const PNS_LAYERSET& aLayers, + int aNet ); ///> touches a joint and links it to an item void linkJoint( const VECTOR2I& aPos, const PNS_LAYERSET& aLayers, @@ -215,8 +355,8 @@ private: ///> helpers for adding/removing items void addSolid( PNS_SOLID* aSeg ); - void addSegment( PNS_SEGMENT* aSeg ); - void addLine( PNS_LINE* aLine ); + void addSegment( PNS_SEGMENT* aSeg, bool aAllowRedundant ); + void addLine( PNS_LINE* aLine, bool aAllowRedundant ); void addVia( PNS_VIA* aVia ); void removeSolid( PNS_SOLID* aSeg ); void removeLine( PNS_LINE* aLine ); @@ -239,16 +379,15 @@ private: return m_override.find( aItem ) != m_override.end(); } - ///> scans the joint map, forming a line starting from segment (current). - void followLine( PNS_SEGMENT* current, - bool scanDirection, - int& pos, - int limit, - VECTOR2I* corners, - PNS_SEGMENT** segments ); + PNS_SEGMENT *findRedundantSegment ( PNS_SEGMENT *aSeg ); - ///> spatial index of all items - // SHAPE_INDEX_LIST m_items; + ///> scans the joint map, forming a line starting from segment (current). + void followLine ( PNS_SEGMENT* current, + bool scanDirection, + int& pos, + int limit, + VECTOR2I* corners, + PNS_SEGMENT** segments ); ///> hash table with the joints, linking the items. Joints are hashed by ///> their position, layer set and net. @@ -263,7 +402,7 @@ private: ///> list of nodes branched from this one std::vector m_children; - ///> hash of root's items that are more recent in this node + ///> hash of root's items that have been changed in this node boost::unordered_set m_override; ///> worst case item-item clearance @@ -275,8 +414,8 @@ private: ///> Geometric/Net index of the items PNS_INDEX* m_index; - ///> list of currently processed obstacles. - Obstacles m_obstacleList; + ///> depth of the node (number of parent nodes in the inheritance chain) + int m_depth; }; #endif diff --git a/pcbnew/router/pns_optimizer.cpp b/pcbnew/router/pns_optimizer.cpp index 48e84207e5..e6aab0e9a1 100644 --- a/pcbnew/router/pns_optimizer.cpp +++ b/pcbnew/router/pns_optimizer.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include @@ -27,12 +27,13 @@ #include "pns_node.h" #include "pns_optimizer.h" #include "pns_utils.h" +#include "pns_router.h" /** * * Cost Estimator Methods * - **/ + */ int PNS_COST_ESTIMATOR::CornerCost( const SEG& a, const SEG& b ) { @@ -74,29 +75,29 @@ int PNS_COST_ESTIMATOR::CornerCost( const SHAPE_LINE_CHAIN& aLine ) int PNS_COST_ESTIMATOR::CornerCost( const PNS_LINE& aLine ) { - return CornerCost( aLine.GetCLine() ); + return CornerCost( aLine.CLine() ); } void PNS_COST_ESTIMATOR::Add( PNS_LINE& aLine ) { - m_lengthCost += aLine.GetCLine().Length(); + m_lengthCost += aLine.CLine().Length(); m_cornerCost += CornerCost( aLine ); } void PNS_COST_ESTIMATOR::Remove( PNS_LINE& aLine ) { - m_lengthCost -= aLine.GetCLine().Length(); + m_lengthCost -= aLine.CLine().Length(); m_cornerCost -= CornerCost( aLine ); } void PNS_COST_ESTIMATOR::Replace( PNS_LINE& aOldLine, PNS_LINE& aNewLine ) { - m_lengthCost -= aOldLine.GetCLine().Length(); + m_lengthCost -= aOldLine.CLine().Length(); m_cornerCost -= CornerCost( aOldLine ); - m_lengthCost += aNewLine.GetCLine().Length(); + m_lengthCost += aNewLine.CLine().Length(); m_cornerCost += CornerCost( aNewLine ); } @@ -145,7 +146,7 @@ struct PNS_OPTIMIZER::CacheVisitor bool operator()( PNS_ITEM* aOtherItem ) { - if( !m_mask & aOtherItem->GetKind() ) + if( !m_mask & aOtherItem->Kind() ) return true; int clearance = m_node->GetClearance( aOtherItem, m_ourItem ); @@ -177,13 +178,13 @@ void PNS_OPTIMIZER::cacheAdd( PNS_ITEM* aItem, bool aIsStatic = false ) void PNS_OPTIMIZER::removeCachedSegments( PNS_LINE* aLine, int aStartVertex, int aEndVertex ) { - std::vector* segs = aLine->GetLinkedSegments(); + PNS_LINE::SegmentRefs* segs = aLine->LinkedSegments(); if( !segs ) return; if( aEndVertex < 0 ) - aEndVertex += aLine->GetCLine().PointCount(); + aEndVertex += aLine->PointCount(); for( int i = aStartVertex; i < aEndVertex - 1; i++ ) { @@ -196,7 +197,7 @@ void PNS_OPTIMIZER::removeCachedSegments( PNS_LINE* aLine, int aStartVertex, int void PNS_OPTIMIZER::CacheRemove( PNS_ITEM* aItem ) { - if( aItem->GetKind() == PNS_ITEM::LINE ) + if( aItem->Kind() == PNS_ITEM::LINE ) removeCachedSegments( static_cast (aItem) ); } @@ -234,7 +235,7 @@ bool PNS_OPTIMIZER::checkColliding( PNS_ITEM* aItem, bool aUpdateCache ) return m_world->CheckColliding( aItem ); // something is wrong with the cache, need to investigate. - m_cache.Query( aItem->GetShape(), m_world->GetMaxClearance(), v, false ); + m_cache.Query( aItem->Shape(), m_world->GetMaxClearance(), v, false ); if( !v.m_collidingItem ) { @@ -268,7 +269,7 @@ bool PNS_OPTIMIZER::checkColliding( PNS_LINE* aLine, const SHAPE_LINE_CHAIN& aOp bool PNS_OPTIMIZER::mergeObtuse( PNS_LINE* aLine ) { - SHAPE_LINE_CHAIN& line = aLine->GetLine(); + SHAPE_LINE_CHAIN& line = aLine->Line(); int step = line.PointCount() - 3; int iter = 0; @@ -360,7 +361,7 @@ bool PNS_OPTIMIZER::mergeObtuse( PNS_LINE* aLine ) bool PNS_OPTIMIZER::mergeFull( PNS_LINE* aLine ) { - SHAPE_LINE_CHAIN& line = aLine->GetLine(); + SHAPE_LINE_CHAIN& line = aLine->Line(); int step = line.SegmentCount() - 1; int segs_pre = line.SegmentCount(); @@ -395,7 +396,7 @@ bool PNS_OPTIMIZER::mergeFull( PNS_LINE* aLine ) } -bool PNS_OPTIMIZER::Optimize( PNS_LINE* aLine, PNS_LINE* aResult, int aStartVertex, int aEndVertex ) +bool PNS_OPTIMIZER::Optimize( PNS_LINE* aLine, PNS_LINE* aResult )//, int aStartVertex, int aEndVertex ) { if( !aResult ) aResult = aLine; @@ -415,6 +416,9 @@ bool PNS_OPTIMIZER::Optimize( PNS_LINE* aLine, PNS_LINE* aResult, int aStartVert if( m_effortLevel & SMART_PADS ) rv |= runSmartPads( aResult ); + if( m_effortLevel & FANOUT_CLEANUP ) + rv |= fanoutCleanup( aResult ); + return rv; } @@ -427,11 +431,11 @@ bool PNS_OPTIMIZER::mergeStep( PNS_LINE* aLine, SHAPE_LINE_CHAIN& aCurrentPath, int cost_orig = PNS_COST_ESTIMATOR::CornerCost( aCurrentPath ); - if( aLine->GetCLine().SegmentCount() < 4 ) + if( aLine->SegmentCount() < 4 ) return false; - DIRECTION_45 orig_start( aLine->GetCLine().CSegment( 0 ) ); - DIRECTION_45 orig_end( aLine->GetCLine().CSegment( -1 ) ); + DIRECTION_45 orig_start( aLine->CSegment( 0 ) ); + DIRECTION_45 orig_end( aLine->CSegment( -1 ) ); while( n < n_segs - step ) { @@ -559,23 +563,30 @@ PNS_OPTIMIZER::BreakoutList PNS_OPTIMIZER::rectBreakouts( int aWidth, PNS_OPTIMIZER::BreakoutList PNS_OPTIMIZER::computeBreakouts( int aWidth, const PNS_ITEM* aItem, bool aPermitDiagonal ) const { - switch( aItem->GetKind() ) + switch( aItem->Kind() ) { case PNS_ITEM::VIA: { const PNS_VIA* via = static_cast( aItem ); - return circleBreakouts( aWidth, via->GetShape(), aPermitDiagonal ); + return circleBreakouts( aWidth, via->Shape(), aPermitDiagonal ); } case PNS_ITEM::SOLID: { - const SHAPE* shape = aItem->GetShape(); + const SHAPE* shape = aItem->Shape(); switch( shape->Type() ) { case SH_RECT: return rectBreakouts( aWidth, shape, aPermitDiagonal ); + case SH_SEGMENT: + { + const SHAPE_SEGMENT *seg = static_cast (shape); + const SHAPE_RECT rect = ApproximateSegmentAsRect ( *seg ); + return rectBreakouts( aWidth, &rect, aPermitDiagonal ); + } + case SH_CIRCLE: return circleBreakouts( aWidth, shape, aPermitDiagonal ); @@ -594,14 +605,14 @@ PNS_OPTIMIZER::BreakoutList PNS_OPTIMIZER::computeBreakouts( int aWidth, PNS_ITEM* PNS_OPTIMIZER::findPadOrVia( int aLayer, int aNet, const VECTOR2I& aP ) const { - PNS_NODE::OptJoint jt = m_world->FindJoint( aP, aLayer, aNet ); + PNS_JOINT *jt = m_world->FindJoint( aP, aLayer, aNet ); if( !jt ) return NULL; - BOOST_FOREACH( PNS_ITEM* item, jt->GetLinkList() ) + BOOST_FOREACH( PNS_ITEM* item, jt->LinkList() ) { - if( item->GetKind() == PNS_ITEM::VIA || item->GetKind() == PNS_ITEM::SOLID ) + if( item->OfKind (PNS_ITEM::VIA | PNS_ITEM::SOLID ) ) return item; } @@ -621,19 +632,16 @@ int PNS_OPTIMIZER::smartPadsSingle( PNS_LINE* aLine, PNS_ITEM* aPad, bool aEnd, typedef std::pair RtVariant; std::vector variants; - BreakoutList breakouts = computeBreakouts( aLine->GetWidth(), aPad, true ); + BreakoutList breakouts = computeBreakouts( aLine->Width(), aPad, true ); - SHAPE_LINE_CHAIN line = ( aEnd ? aLine->GetCLine().Reverse() : aLine->GetCLine() ); + SHAPE_LINE_CHAIN line = ( aEnd ? aLine->CLine().Reverse() : aLine->CLine() ); - // bool startDiagonal = DIRECTION_45( line.CSegment(0) ).IsDiagonal(); int p_end = std::min( aEndVertex, std::min( 3, line.PointCount() - 1 ) ); for( int p = 1; p <= p_end; p++ ) { BOOST_FOREACH( SHAPE_LINE_CHAIN & l, breakouts ) { - // PNSDisplayDebugLine (l, 0); - for( int diag = 0; diag < 2; diag++ ) { @@ -642,11 +650,12 @@ int PNS_OPTIMIZER::smartPadsSingle( PNS_LINE* aLine, PNS_ITEM* aPad, bool aEnd, line.CPoint( p ), diag == 0 ); DIRECTION_45 dir_bkout( l.CSegment( -1 ) ); - // DIRECTION_45 dir_head ( line.CSegment(p + 1)); + if(!connect.SegmentCount()) + continue; + int ang1 = dir_bkout.Angle( DIRECTION_45( connect.CSegment( 0 ) ) ); int ang2 = 0; - // int ang2 = dir_head.Angle ( DIRECTION_45(connect.CSegment(-1) )); if( (ang1 | ang2) & ForbiddenAngles ) continue; @@ -662,7 +671,6 @@ int PNS_OPTIMIZER::smartPadsSingle( PNS_LINE* aLine, PNS_ITEM* aPad, bool aEnd, v.Append( line.CPoint( i ) ); PNS_LINE tmp( *aLine, v ); - // tmp.GetLine().Simplify(); int cc = tmp.CountCorners( ForbiddenAngles ); if( cc == 0 ) @@ -689,18 +697,12 @@ int PNS_OPTIMIZER::smartPadsSingle( PNS_LINE* aLine, PNS_ITEM* aPad, bool aEnd, if( !checkColliding( &tmp ) ) { -/* if(aEnd) - * PNSDisplayDebugLine (l_best, 6); - * else - * PNSDisplayDebugLine (l_best, 5);*/ - if( cost < min_cost || ( cost == min_cost && len < min_len ) ) { l_best = vp.second; p_best = vp.first; found = true; - // if(cost == min_cost) if( cost == min_cost ) min_len = std::min( len, min_len ); @@ -711,12 +713,6 @@ int PNS_OPTIMIZER::smartPadsSingle( PNS_LINE* aLine, PNS_ITEM* aPad, bool aEnd, if( found ) { -// printf("end: %d, p-best: %d, p-end: %d, p-total: %d\n", aEnd, p_best, p_end, l_best.PointCount()); - -// if(!aEnd) -// PNSDisplayDebugLine (l_best, 5); -// else - aLine->SetShape( l_best ); return p_best; } @@ -724,18 +720,17 @@ int PNS_OPTIMIZER::smartPadsSingle( PNS_LINE* aLine, PNS_ITEM* aPad, bool aEnd, return -1; } - bool PNS_OPTIMIZER::runSmartPads( PNS_LINE* aLine ) { - SHAPE_LINE_CHAIN& line = aLine->GetLine(); - + SHAPE_LINE_CHAIN& line = aLine->Line(); + if( line.PointCount() < 3 ) return false; VECTOR2I p_start = line.CPoint( 0 ), p_end = line.CPoint( -1 ); - PNS_ITEM* startPad = findPadOrVia( aLine->GetLayer(), aLine->GetNet(), p_start ); - PNS_ITEM* endPad = findPadOrVia( aLine->GetLayer(), aLine->GetNet(), p_end ); + PNS_ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start ); + PNS_ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end ); int vtx = -1; @@ -746,16 +741,68 @@ bool PNS_OPTIMIZER::runSmartPads( PNS_LINE* aLine ) smartPadsSingle( aLine, endPad, true, vtx < 0 ? line.PointCount() - 1 : line.PointCount() - 1 - vtx ); - aLine->GetLine().Simplify(); + aLine->Line().Simplify(); return true; } bool PNS_OPTIMIZER::Optimize( PNS_LINE* aLine, int aEffortLevel, PNS_NODE* aWorld ) { - PNS_OPTIMIZER opt( aWorld ? aWorld : aLine->GetWorld() ); + PNS_OPTIMIZER opt( aWorld ); opt.SetEffortLevel( aEffortLevel ); opt.SetCollisionMask( -1 ); return opt.Optimize( aLine ); } + + +bool PNS_OPTIMIZER::fanoutCleanup( PNS_LINE * aLine ) +{ + if( aLine->PointCount() < 3 ) + return false; + + VECTOR2I p_start = aLine->CPoint( 0 ), p_end = aLine->CPoint( -1 ); + + PNS_ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start ); + PNS_ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end ); + + int thr = aLine->Width() * 10; + int len = aLine->CLine().Length(); + + + if(!startPad) + return false; + + + bool startMatch = startPad->OfKind(PNS_ITEM::VIA | PNS_ITEM::SOLID); + bool endMatch = false; + + if(endPad) + { + endMatch = endPad->OfKind(PNS_ITEM::VIA | PNS_ITEM::SOLID); + } else { + endMatch = aLine->EndsWithVia(); + } + + + if(startMatch && endMatch && len < thr) + { + + for(int i = 0; i < 2; i++ ) + { + SHAPE_LINE_CHAIN l2 = DIRECTION_45().BuildInitialTrace(p_start, p_end, i); + PNS_ROUTER::GetInstance()->DisplayDebugLine (l2, 4, 10000); + PNS_LINE repl; + repl = PNS_LINE (*aLine, l2 ); + + + + if (!m_world->CheckColliding(&repl)) + { + aLine->SetShape(repl.CLine()); + return true; + } + } + } + return false; +} diff --git a/pcbnew/router/pns_optimizer.h b/pcbnew/router/pns_optimizer.h index 916e77bb69..3e1d9b211c 100644 --- a/pcbnew/router/pns_optimizer.h +++ b/pcbnew/router/pns_optimizer.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_OPTIMIZER_H @@ -27,6 +27,8 @@ #include #include +#include "range.h" + class PNS_NODE; class PNS_LINE; class PNS_ROUTER; @@ -90,17 +92,17 @@ public: { MERGE_SEGMENTS = 0x01, SMART_PADS = 0x02, - MERGE_OBTUSE = 0x04 + MERGE_OBTUSE = 0x04, + FANOUT_CLEANUP = 0x08 }; PNS_OPTIMIZER( PNS_NODE* aWorld ); ~PNS_OPTIMIZER(); ///> a quick shortcut to optmize a line without creating and setting up an optimizer - static bool Optimize( PNS_LINE* aLine, int aEffortLevel, PNS_NODE* aWorld = NULL ); + static bool Optimize( PNS_LINE* aLine, int aEffortLevel, PNS_NODE* aWorld); - bool Optimize( PNS_LINE* aLine, PNS_LINE* aResult = NULL, - int aStartVertex = 0, int aEndVertex = -1 ); + bool Optimize( PNS_LINE* aLine, PNS_LINE* aResult = NULL ); void SetWorld( PNS_NODE* aNode ) { m_world = aNode; } void CacheStaticItem( PNS_ITEM* aItem ); @@ -135,6 +137,7 @@ private: bool removeUglyCorners( PNS_LINE* aLine ); bool runSmartPads( PNS_LINE* aLine ); bool mergeStep( PNS_LINE* aLine, SHAPE_LINE_CHAIN& aCurrentLine, int step ); + bool fanoutCleanup( PNS_LINE * aLine ); bool checkColliding( PNS_ITEM* aItem, bool aUpdateCache = true ); bool checkColliding( PNS_LINE* aLine, const SHAPE_LINE_CHAIN& aOptPath ); diff --git a/pcbnew/router/pns_router.cpp b/pcbnew/router/pns_router.cpp index 104aeaa05e..a08b0f0fc7 100644 --- a/pcbnew/router/pns_router.cpp +++ b/pcbnew/router/pns_router.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include @@ -42,11 +42,13 @@ #include "pns_solid.h" #include "pns_utils.h" #include "pns_router.h" +#include "pns_shove.h" +#include "pns_dragger.h" #include #include -#include +#include #include #include #include @@ -80,13 +82,29 @@ public: m_defaultClearance = 254000; // aBoard->m_NetClasses.Find ("Default clearance")->GetClearance(); } + int localPadClearance( const PNS_ITEM * item ) const + { + if(!item->Parent() || item->Parent()->Type() != PCB_PAD_T ) + return 0; + + const D_PAD *pad = static_cast( item->Parent() ); + + return pad->GetLocalClearance(); + } + int operator()( const PNS_ITEM* a, const PNS_ITEM* b ) { - int net_a = a->GetNet(); + int net_a = a->Net(); int cl_a = (net_a >= 0 ? m_clearanceCache[net_a] : m_defaultClearance); - int net_b = b->GetNet(); + int net_b = b->Net(); int cl_b = (net_b >= 0 ? m_clearanceCache[net_b] : m_defaultClearance); + int pad_a = localPadClearance( a ); + int pad_b = localPadClearance( b ); + + cl_a = std::max(cl_a, pad_a); + cl_b = std::max(cl_b, pad_b); + return std::max( cl_a, cl_b ); } @@ -97,11 +115,12 @@ private: PNS_ITEM* PNS_ROUTER::syncPad( D_PAD* aPad ) { - PNS_LAYERSET layers( 0, 15 ); + PNS_LAYERSET layers ( 0, 15 ); switch( aPad->GetAttribute() ) { case PAD_STANDARD: + layers = PNS_LAYERSET( 0, 15 ); break; case PAD_SMD: @@ -130,49 +149,64 @@ PNS_ITEM* PNS_ROUTER::syncPad( D_PAD* aPad ) solid->SetLayers( layers ); solid->SetNet( aPad->GetNetCode() ); + solid->SetParent( aPad ); + wxPoint wx_c = aPad->GetPosition(); wxSize wx_sz = aPad->GetSize(); VECTOR2I c( wx_c.x, wx_c.y ); VECTOR2I sz( wx_sz.x, wx_sz.y ); - solid->SetCenter( c ); + solid->SetPos( c ); double orient = aPad->GetOrientation() / 10.0; + bool nonOrtho = false; + if( orient == 90.0 || orient == 270.0 ) sz = VECTOR2I( sz.y, sz.x ); else if( orient != 0.0 && orient != 180.0 ) { - TRACEn( 0, "non-orthogonal pad rotations not supported yet" ); - delete solid; - return NULL; + // rotated pads are replaced by for the moment by circles due to my laziness ;) + solid->SetShape ( new SHAPE_CIRCLE (c, std::min(sz.x, sz.y) / 2 ) ); + nonOrtho = true; } - switch( aPad->GetShape() ) + if(!nonOrtho) { - case PAD_CIRCLE: - solid->SetShape( new SHAPE_CIRCLE( c, sz.x / 2 ) ); - break; - - case PAD_OVAL: - if( sz.x == sz.y ) + switch( aPad->GetShape() ) + { + case PAD_CIRCLE: solid->SetShape( new SHAPE_CIRCLE( c, sz.x / 2 ) ); - else + break; + + case PAD_OVAL: + if( sz.x == sz.y ) + solid->SetShape( new SHAPE_CIRCLE( c, sz.x / 2 ) ); + else { + VECTOR2I delta; + + if (sz.x > sz.y) + delta = VECTOR2I((sz.x - sz.y) / 2, 0); + else + delta = VECTOR2I(0, (sz.y - sz.x) / 2); + + SHAPE_SEGMENT *shape = new SHAPE_SEGMENT( c - delta, c + delta, std::min(sz.x, sz.y) ); + solid->SetShape( shape ); + } + break; + + case PAD_RECT: solid->SetShape( new SHAPE_RECT( c - sz / 2, sz.x, sz.y ) ); - break; + break; - case PAD_RECT: - solid->SetShape( new SHAPE_RECT( c - sz / 2, sz.x, sz.y ) ); - break; - - default: - TRACEn( 0, "unsupported pad shape" ); - delete solid; - return NULL; + default: + TRACEn( 0, "unsupported pad shape" ); + delete solid; + return NULL; + } } - solid->SetParent( aPad ); return solid; } @@ -197,7 +231,9 @@ PNS_ITEM* PNS_ROUTER::syncVia( VIA* aVia ) aVia->GetWidth(), aVia->GetNetCode() ); + v->SetDrill ( aVia->GetDrill() ); v->SetParent( aVia ); + return v; } @@ -233,7 +269,6 @@ int PNS_ROUTER::NextCopperLayer( bool aUp ) void PNS_ROUTER::SyncWorld() { - std::vector pads; if( !m_board ) { @@ -243,11 +278,12 @@ void PNS_ROUTER::SyncWorld() ClearWorld(); + int worstClearance = m_board->GetDesignSettings().GetBiggestClearanceValue(); + m_clearanceFunc = new PCBNEW_CLEARANCE_FUNC( m_board ); m_world = new PNS_NODE(); m_world->SetClearanceFunctor( m_clearanceFunc ); - m_world->SetMaxClearance( 1000000 ); // m_board->GetBiggestClearanceValue()); - pads = m_board->GetPads(); + m_world->SetMaxClearance( 4 * worstClearance ); for( MODULE* module = m_board->m_Modules; module; module = module->Next() ) { @@ -273,8 +309,6 @@ void PNS_ROUTER::SyncWorld() if( item ) m_world->Add( item ); } - - m_placer = new PNS_LINE_PLACER( m_world ); } @@ -291,10 +325,8 @@ PNS_ROUTER::PNS_ROUTER() m_world = NULL; m_placer = NULL; m_previewItems = NULL; - m_start_diagonal = false; m_board = NULL; - - TRACE( 1, "m_board = %p\n", m_board ); + m_dragger = NULL; } @@ -351,11 +383,11 @@ void PNS_ROUTER::ClearWorld() } -void PNS_ROUTER::SetCurrentWidth( int w ) +/*void PNS_ROUTER::SetCurrentWidth( int w ) { // fixme: change width while routing m_currentWidth = w; -} +}*/ bool PNS_ROUTER::RoutingInProgress() const @@ -369,7 +401,11 @@ const PNS_ITEMSET PNS_ROUTER::QueryHoverItems( const VECTOR2I& aP ) if( m_state == IDLE ) return m_world->HitTest( aP ); else - return m_placer->GetCurrentNode()->HitTest( aP ); + { + //assert ( m_placer->GetCurrentNode()->checkExists() ); + //TRACE(0,"query-hover [%p]", m_placer->GetCurrentNode()); + return m_placer->CurrentNode()->HitTest( aP ); + } } @@ -383,23 +419,23 @@ const VECTOR2I PNS_ROUTER::SnapToItem( PNS_ITEM* item, VECTOR2I aP, bool& aSplit return aP; } - switch( item->GetKind() ) + switch( item->Kind() ) { case PNS_ITEM::SOLID: - anchor = static_cast(item)->GetCenter(); + anchor = static_cast(item)->Pos(); aSplitsSegment = false; break; case PNS_ITEM::VIA: - anchor = static_cast(item)->GetPos(); + anchor = static_cast(item)->Pos(); aSplitsSegment = false; break; case PNS_ITEM::SEGMENT: { PNS_SEGMENT* seg = static_cast( item ); - const SEG& s = seg->GetSeg(); - int w = seg->GetWidth(); + const SEG& s = seg->Seg(); + int w = seg->Width(); aSplitsSegment = false; @@ -423,50 +459,52 @@ const VECTOR2I PNS_ROUTER::SnapToItem( PNS_ITEM* item, VECTOR2I aP, bool& aSplit return anchor; } - -void PNS_ROUTER::StartRouting( const VECTOR2I& aP, PNS_ITEM* aStartItem ) +bool PNS_ROUTER::StartDragging( const VECTOR2I& aP, PNS_ITEM* aStartItem ) { - VECTOR2I p; - - static int unknowNetIdx = 0; // -10000; - - m_placingVia = false; - m_startsOnVia = false; - m_currentNet = -1; - - bool splitSeg = false; - - p = SnapToItem( aStartItem, aP, splitSeg ); - - if( !aStartItem || aStartItem->GetNet() < 0 ) - m_currentNet = unknowNetIdx--; - else - m_currentNet = aStartItem->GetNet(); - - m_currentStart = p; - m_originalStart = p; - m_currentEnd = p; - - m_placer->SetInitialDirection( m_start_diagonal ? DIRECTION_45( - DIRECTION_45::NE ) : DIRECTION_45( DIRECTION_45::N ) ); - m_placer->StartPlacement( m_originalStart, m_currentNet, m_currentWidth, m_currentLayer ); - m_state = ROUTE_TRACK; - - if( splitSeg ) - splitAdjacentSegments( m_placer->GetCurrentNode(), aStartItem, p ); + if(!aStartItem || aStartItem->OfKind(PNS_ITEM::SOLID)) + return false; + + m_dragger = new PNS_DRAGGER ( this ); + m_dragger->SetWorld( m_world ); + if( m_dragger->Start ( aP, aStartItem ) ) + m_state = DRAG_SEGMENT; + else { + delete m_dragger; + m_state = IDLE; + return false; + } + + return true; } -const VECTOR2I PNS_ROUTER::GetCurrentEnd() const +bool PNS_ROUTER::StartRouting( const VECTOR2I& aP, PNS_ITEM* aStartItem ) +{ + + + m_state = ROUTE_TRACK; + + m_placer = new PNS_LINE_PLACER( this ); + m_placer->SetLayer( m_currentLayer ); + m_placer->SetWidth ( m_settings.GetTrackWidth() ); + m_placer->Start( aP, aStartItem ); + m_currentEnd = aP; + m_currentEndItem = NULL; + + return true; +} + + +const VECTOR2I PNS_ROUTER::CurrentEnd() const { return m_currentEnd; } -void PNS_ROUTER::EraseView() +void PNS_ROUTER::eraseView() { BOOST_FOREACH( BOARD_ITEM* item, m_hiddenItems ) - { + { item->ViewSetVisible( true ); } @@ -480,71 +518,131 @@ void PNS_ROUTER::EraseView() } -void PNS_ROUTER::DisplayItem( const PNS_ITEM* aItem, bool aIsHead ) +void PNS_ROUTER::DisplayItem( const PNS_ITEM* aItem, int aColor, int aClearance ) { ROUTER_PREVIEW_ITEM* pitem = new ROUTER_PREVIEW_ITEM( aItem, m_previewItems ); - m_previewItems->Add( pitem ); + if(aColor >= 0) + pitem->SetColor ( KIGFX::COLOR4D ( aColor )); - if( aIsHead ) - pitem->MarkAsHead(); + if(aClearance >= 0) + pitem->SetClearance ( aClearance ); + + m_previewItems->Add( pitem ); pitem->ViewSetVisible( true ); m_previewItems->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY | KIGFX::VIEW_ITEM::APPEARANCE ); } +void PNS_ROUTER::DisplayItems( const PNS_ITEMSET& aItems ) +{ + BOOST_FOREACH(const PNS_ITEM *item, aItems.CItems()) + DisplayItem(item); +} void PNS_ROUTER::DisplayDebugLine( const SHAPE_LINE_CHAIN& aLine, int aType, int aWidth ) { ROUTER_PREVIEW_ITEM* pitem = new ROUTER_PREVIEW_ITEM( NULL, m_previewItems ); - pitem->DebugLine( aLine, aWidth, aType ); + pitem->Line( aLine, aWidth, aType ); m_previewItems->Add( pitem ); pitem->ViewSetVisible( true ); m_previewItems->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY | KIGFX::VIEW_ITEM::APPEARANCE ); } -void PNS_ROUTER::DisplayDebugBox( const BOX2I& aBox, int aType, int aWidth ) -{ -} +void PNS_ROUTER::DisplayDebugPoint( const VECTOR2I pos, int aType ) +{ + ROUTER_PREVIEW_ITEM* pitem = new ROUTER_PREVIEW_ITEM( NULL, m_previewItems ); + + pitem->Point( pos, aType ); + m_previewItems->Add( pitem ); + pitem->ViewSetVisible( true ); + m_previewItems->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY | KIGFX::VIEW_ITEM::APPEARANCE ); + +} void PNS_ROUTER::Move( const VECTOR2I& aP, PNS_ITEM* endItem ) { - PNS_NODE::ItemVector removed, added; - VECTOR2I p = aP; + m_currentEnd = aP; + m_currentEndItem = endItem; - if( m_state == IDLE ) - return; - - // TODO is something missing here? - if( m_state == START_ROUTING ) + switch( m_state ) { + case ROUTE_TRACK: + movePlacing( aP, endItem ); + break; + + case DRAG_SEGMENT: + moveDragging (aP, endItem ); + break; + default: + break; } +} - EraseView(); +void PNS_ROUTER::moveDragging( const VECTOR2I& aP, PNS_ITEM* endItem ) +{ + eraseView(); - m_currentEnd = p; - m_placer->Route( p ); + m_dragger->Drag( aP ); + PNS_ITEMSET dragged = m_dragger->Traces(); - PNS_LINE current = m_placer->GetTrace(); + updateView ( m_dragger->CurrentNode ( ), dragged ); +} - DisplayItem( ¤t, true ); +void PNS_ROUTER::markViolations( PNS_NODE *aNode, PNS_ITEMSET& aCurrent, PNS_NODE::ItemVector& aRemoved ) +{ + + BOOST_FOREACH(PNS_ITEM *item, aCurrent.Items()) + { + PNS_NODE::Obstacles obstacles; - if( current.EndsWithVia() ) - DisplayItem( ¤t.GetVia(), true ); + aNode->QueryColliding( item, obstacles, PNS_ITEM::ANY ); + + if ( item->OfKind(PNS_ITEM::LINE ) ) + { + PNS_LINE *l = static_cast (item); + if (l->EndsWithVia()) + { + PNS_VIA v ( l->Via() ); + aNode->QueryColliding(&v , obstacles, PNS_ITEM::ANY ); + } + } - m_placer->GetCurrentNode()->GetUpdatedItems( removed, added ); + BOOST_FOREACH(PNS_OBSTACLE& obs, obstacles) + { + int clearance = aNode->GetClearance( item, obs.item ); + std::auto_ptr tmp ( obs.item->Clone() ); + tmp->Mark ( MK_VIOLATION ); + DisplayItem( tmp.get(), -1, clearance ); + aRemoved.push_back(obs.item); + } + } +} + +void PNS_ROUTER::updateView( PNS_NODE *aNode, PNS_ITEMSET& aCurrent ) +{ + PNS_NODE::ItemVector removed, added; + PNS_NODE::Obstacles obstacles; + + if(!aNode) + return; + + if( Settings().Mode() == RM_MarkObstacles ) + markViolations(aNode, aCurrent, removed); + + aNode->GetUpdatedItems( removed, added ); BOOST_FOREACH( PNS_ITEM* item, added ) - { + { DisplayItem( item ); } BOOST_FOREACH( PNS_ITEM* item, removed ) { - BOARD_ITEM* parent = item->GetParent(); + BOARD_CONNECTED_ITEM* parent = item->Parent(); if( parent ) { @@ -558,32 +656,36 @@ void PNS_ROUTER::Move( const VECTOR2I& aP, PNS_ITEM* endItem ) } -void PNS_ROUTER::splitAdjacentSegments( PNS_NODE* aNode, PNS_ITEM* aSeg, const VECTOR2I& aP ) +void PNS_ROUTER::ApplySettings() { - if( aSeg && aSeg->OfKind( PNS_ITEM::SEGMENT ) ) + // Change track/via size settings + if( m_state == ROUTE_TRACK) { - PNS_NODE::OptJoint jt = aNode->FindJoint( aP, aSeg->GetLayers().Start(), aSeg->GetNet() ); - - if( jt && jt->LinkCount() >= 1 ) - return; - - PNS_SEGMENT* s_old = static_cast( aSeg ); - PNS_SEGMENT* s_new[2]; - - s_new[0] = s_old->Clone(); - s_new[1] = s_old->Clone(); - - s_new[0]->SetEnds( s_old->GetSeg().A, aP ); - s_new[1]->SetEnds( aP, s_old->GetSeg().B ); - - aNode->Remove( s_old ); - aNode->Add( s_new[0] ); - aNode->Add( s_new[1] ); + m_placer->UpdateSizes( m_settings ); + m_placer->Move( m_currentEnd, m_currentEndItem ); + movePlacing( m_currentEnd, m_currentEndItem ); } + + // TODO handle mode/optimization/other options change } +void PNS_ROUTER::movePlacing( const VECTOR2I& aP, PNS_ITEM* endItem ) +{ + eraseView(); -void PNS_ROUTER::commitRouting( PNS_NODE* aNode ) + m_placer->Move( aP, endItem ); + PNS_LINE current = m_placer->Trace(); + + DisplayItem( ¤t ); + + if( current.EndsWithVia() ) + DisplayItem( ¤t.Via() ); + + PNS_ITEMSET tmp ( ¤t ); + updateView ( m_placer->CurrentNode ( true ), tmp ); +} + +void PNS_ROUTER::CommitRouting( PNS_NODE* aNode ) { PNS_NODE::ItemVector removed, added; @@ -591,13 +693,13 @@ void PNS_ROUTER::commitRouting( PNS_NODE* aNode ) for( unsigned int i = 0; i < removed.size(); i++ ) { - BOARD_CONNECTED_ITEM* parent = removed[i]->GetParent(); + BOARD_CONNECTED_ITEM* parent = removed[i]->Parent(); if( parent ) { - m_undoBuffer.PushItem( ITEM_PICKER( parent, UR_DELETED ) ); - m_board->Remove( parent ); m_view->Remove( parent ); + m_board->Remove( parent ); + m_undoBuffer.PushItem( ITEM_PICKER( parent, UR_DELETED ) ); } } @@ -605,19 +707,19 @@ void PNS_ROUTER::commitRouting( PNS_NODE* aNode ) { BOARD_CONNECTED_ITEM* newBI = NULL; - switch( item->GetKind() ) + switch( item->Kind() ) { case PNS_ITEM::SEGMENT: { PNS_SEGMENT* seg = static_cast( item ); TRACK* track = new TRACK( m_board ); - const SEG& s = seg->GetSeg(); + const SEG& s = seg->Seg(); track->SetStart( wxPoint( s.A.x, s.A.y ) ); track->SetEnd( wxPoint( s.B.x, s.B.y ) ); - track->SetWidth( seg->GetWidth() ); - track->SetLayer( seg->GetLayers().Start() ); - track->SetNetCode( seg->GetNet() ); + track->SetWidth( seg->Width() ); + track->SetLayer( seg->Layers().Start() ); + track->SetNetCode( seg->Net() ); newBI = track; break; } @@ -626,9 +728,10 @@ void PNS_ROUTER::commitRouting( PNS_NODE* aNode ) { VIA* via_board = new VIA( m_board ); PNS_VIA* via = static_cast( item ); - via_board->SetPosition( wxPoint( via->GetPos().x, via->GetPos().y ) ); - via_board->SetWidth( via->GetDiameter() ); - via_board->SetNetCode( via->GetNet() ); + via_board->SetPosition( wxPoint( via->Pos().x, via->Pos().y ) ); + via_board->SetWidth( via->Diameter() ); + via_board->SetDrill( via->Drill() ); + via_board->SetNetCode( via->Net() ); newBI = via_board; break; } @@ -657,12 +760,12 @@ PNS_VIA* PNS_ROUTER::checkLoneVia( PNS_JOINT* aJoint ) const PNS_VIA* theVia = NULL; PNS_LAYERSET l; - BOOST_FOREACH( PNS_ITEM* item, aJoint->GetLinkList() ) + BOOST_FOREACH( PNS_ITEM* item, aJoint->LinkList() ) { - if( item->GetKind() == PNS_ITEM::VIA ) + if( item->Kind() == PNS_ITEM::VIA ) theVia = static_cast( item ); - l.Merge( item->GetLayers() ); + l.Merge( item->Layers() ); } if( l.Start() == l.End() ) @@ -671,103 +774,34 @@ PNS_VIA* PNS_ROUTER::checkLoneVia( PNS_JOINT* aJoint ) const return NULL; } - -PNS_NODE* PNS_ROUTER::removeLoops( PNS_NODE* aNode, PNS_SEGMENT* aLatestSeg ) -{ - PNS_LINE* ourLine = aNode->AssembleLine( aLatestSeg ); - PNS_NODE* cleaned = aNode->Branch(); - PNS_JOINT a, b; - - std::vector lines; - - cleaned->FindLineEnds( ourLine, a, b ); - cleaned->FindLinesBetweenJoints( a, b, lines ); - - BOOST_FOREACH( PNS_LINE* line, lines ) - { - if( !( line->ContainsSegment( aLatestSeg ) ) ) - { - cleaned->Remove( line ); - } - } - - return cleaned; -} - - bool PNS_ROUTER::FixRoute( const VECTOR2I& aP, PNS_ITEM* aEndItem ) { - bool real_end = false; + bool rv = false; - PNS_LINE pl = m_placer->GetTrace(); - const SHAPE_LINE_CHAIN& l = pl.GetCLine(); - - if( !l.SegmentCount() ) - return true; - - VECTOR2I p_pre_last = l.CPoint( -1 ); - const VECTOR2I p_last = l.CPoint( -1 ); - DIRECTION_45 d_last( l.CSegment( -1 ) ); - - if( l.PointCount() > 2 ) - p_pre_last = l.CPoint( -2 ); - - if( aEndItem && m_currentNet >= 0 && m_currentNet == aEndItem->GetNet() ) - real_end = true; - - int last = ( real_end || m_placingVia ) ? l.SegmentCount() : std::max( 1, l.SegmentCount() - 1 ); - - PNS_NODE* latest = m_placer->GetCurrentNode(); - - if( real_end ) - splitAdjacentSegments( latest, aEndItem, aP ); - - PNS_SEGMENT* lastSeg = NULL; - - for( int i = 0; i < last; i++ ) + switch(m_state) { - const SEG& s = pl.GetCLine().CSegment( i ); - PNS_SEGMENT* seg = new PNS_SEGMENT( s, m_currentNet ); - seg->SetWidth( pl.GetWidth() ); - seg->SetLayer( m_currentLayer ); - latest->Add( seg ); - lastSeg = seg; - } + case ROUTE_TRACK: + rv = m_placer->FixRoute (aP, aEndItem); + m_placingVia = false; + + break; + case DRAG_SEGMENT: + rv = m_dragger->FixRoute (); + break; + + default: + break; + } - if( pl.EndsWithVia() ) - latest->Add( pl.GetVia().Clone() ); - - if( real_end ) - latest = removeLoops( latest, lastSeg ); - - commitRouting( latest ); - - EraseView(); - - if( real_end ) - { - m_state = IDLE; - // m_world->KillChildren(); - } - else - { - m_state = ROUTE_TRACK; - m_placer->SetInitialDirection( d_last ); - m_currentStart = m_placingVia ? p_last : p_pre_last; - - if( m_placingVia ) - m_currentLayer = NextCopperLayer( true ); - - m_placer->StartPlacement( m_currentStart, m_currentNet, m_currentWidth, m_currentLayer ); - - m_startsOnVia = m_placingVia; - m_placingVia = false; - } - - return real_end; + + if(rv) + StopRouting(); + + return rv; } + void PNS_ROUTER::StopRouting() { // Update the ratsnest with new changes @@ -776,25 +810,30 @@ void PNS_ROUTER::StopRouting() if( !RoutingInProgress() ) return; - EraseView(); + if(m_placer) + delete m_placer; + + if(m_dragger) + delete m_dragger; + + m_placer = NULL; + m_dragger = NULL; + + eraseView(); m_state = IDLE; m_world->KillChildren(); + m_world->ClearRanks(); } void PNS_ROUTER::FlipPosture() { - if( m_placer->GetTail().GetCLine().SegmentCount() == 0 ) + if(m_state == ROUTE_TRACK) { - m_start_diagonal = !m_start_diagonal; - m_placer->SetInitialDirection( m_start_diagonal ? - DIRECTION_45( DIRECTION_45::NE ) : DIRECTION_45( DIRECTION_45::N ) ); - } - else m_placer->FlipPosture(); - - Move( m_currentEnd, NULL ); + m_placer->Move ( m_currentEnd, m_currentEndItem ); + } } @@ -810,10 +849,10 @@ void PNS_ROUTER::SwitchLayer( int layer ) if( m_startsOnVia ) { m_currentLayer = layer; - m_placer->StartPlacement( m_currentStart, m_currentNet, m_currentWidth, - m_currentLayer ); + //m_placer->StartPlacement( m_currentStart, m_currentNet, m_currentWidth, + // m_currentLayer ); } - + break; default: break; } @@ -825,6 +864,45 @@ void PNS_ROUTER::ToggleViaPlacement() if( m_state == ROUTE_TRACK ) { m_placingVia = !m_placingVia; - m_placer->AddVia( m_placingVia, m_currentViaDiameter, m_currentViaDrill ); + m_placer->AddVia( m_placingVia, m_settings.GetViaDiameter(), m_settings.GetViaDrill() ); } } + +int PNS_ROUTER::GetCurrentNet() const +{ + switch(m_state) + { + case ROUTE_TRACK: + return m_placer->CurrentNet(); + default: + return m_currentNet; + } +} + +int PNS_ROUTER::GetCurrentLayer() const +{ + switch(m_state) + { + case ROUTE_TRACK: + return m_placer->CurrentLayer(); + default: + return m_currentLayer; + } +} + +void PNS_ROUTER::DumpLog() +{ + PNS_LOGGER *logger = NULL; + switch(m_state) + { + case DRAG_SEGMENT: + logger = m_dragger->Logger(); + break; + + default: + break; + } + + if(logger) + logger->Save ( "/tmp/shove.log" ); +} diff --git a/pcbnew/router/pns_router.h b/pcbnew/router/pns_router.h index e5fabbeec2..53c0905487 100644 --- a/pcbnew/router/pns_router.h +++ b/pcbnew/router/pns_router.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_ROUTER_H @@ -32,6 +32,7 @@ #include "pns_routing_settings.h" #include "pns_item.h" #include "pns_itemset.h" +#include "pns_node.h" class BOARD; class BOARD_ITEM; @@ -47,7 +48,8 @@ class PNS_SEGMENT; class PNS_JOINT; class PNS_VIA; class PNS_CLEARANCE_FUNC; -class VIEW_GROUP; +class PNS_SHOVE; +class PNS_DRAGGER; namespace KIGFX { class VIEW; @@ -67,9 +69,8 @@ private: enum RouterState { IDLE, - START_ROUTING, - ROUTE_TRACK, - FINISH_TRACK + DRAG_SEGMENT, + ROUTE_TRACK }; public: @@ -85,13 +86,13 @@ public: void SetView( KIGFX::VIEW* aView ); bool RoutingInProgress() const; - void StartRouting( const VECTOR2I& aP, PNS_ITEM* aItem ); + bool StartRouting( const VECTOR2I& aP, PNS_ITEM* aItem ); void Move( const VECTOR2I& aP, PNS_ITEM* aItem ); bool FixRoute( const VECTOR2I& aP, PNS_ITEM* aItem ); void StopRouting(); - const VECTOR2I GetCurrentEnd() const; + const VECTOR2I CurrentEnd() const; int GetClearance( const PNS_ITEM* a, const PNS_ITEM* b ) const; @@ -102,25 +103,22 @@ public: void FlipPosture(); - void DisplayItem( const PNS_ITEM* aItem, bool aIsHead = false ); + void DisplayItem( const PNS_ITEM* aItem, int aColor = -1, int aClearance = -1 ); + void DisplayItems( const PNS_ITEMSET& aItems ); + void DisplayDebugLine( const SHAPE_LINE_CHAIN& aLine, int aType = 0, int aWidth = 0 ); + void DisplayDebugPoint( const VECTOR2I aPos, int aType = 0); void DisplayDebugBox( const BOX2I& aBox, int aType = 0, int aWidth = 0 ); - void EraseView(); void SwitchLayer( int layer ); - int GetCurrentLayer() const { return m_currentLayer; } void ToggleViaPlacement(); - void SetCurrentWidth( int w ); - - void SetCurrentViaDiameter( int d ) { m_currentViaDiameter = d; } - void SetCurrentViaDrill( int d ) { m_currentViaDrill = d; } - int GetCurrentWidth() const { return m_currentWidth; } - int GetCurrentViaDiameter() const { return m_currentViaDiameter; } - int GetCurrentViaDrill() const { return m_currentViaDrill; } - int GetCurrentNet() const { return m_currentNet; } + int GetCurrentLayer() const;// { return m_currentLayer; } + int GetCurrentNet() const;// { return m_currentNet; } + void DumpLog(); + PNS_CLEARANCE_FUNC* GetClearanceFunc() const { return m_clearanceFunc; @@ -138,11 +136,24 @@ public: const PNS_ITEMSET QueryHoverItems( const VECTOR2I& aP ); const VECTOR2I SnapToItem( PNS_ITEM* item, VECTOR2I aP, bool& aSplitsSegment ); + bool StartDragging( const VECTOR2I& aP, PNS_ITEM* aItem ); + + void SetIterLimit( int aX ) { m_iterLimit = aX; } + int GetIterLimit() const { return m_iterLimit; }; + + void SetShowIntermediateSteps(bool aX, int aSnapshotIter = -1 ) { m_showInterSteps = aX; m_snapshotIter = aSnapshotIter; } + bool GetShowIntermediateSteps() const { return m_showInterSteps; } + int GetShapshotIter() const { return m_snapshotIter; } + + PNS_ROUTING_SETTINGS& Settings() { return m_settings; } + + void CommitRouting( PNS_NODE* aNode ); + /** * Returns the last changes introduced by the router (since the last time ClearLastChanges() * was called or a new track has been started). */ - const PICKED_ITEMS_LIST& GetLastChanges() const + const PICKED_ITEMS_LIST& GetUndoBuffer() const { return m_undoBuffer; } @@ -150,21 +161,41 @@ public: /** * Clears the list of recent changes, saved to be stored in the undo buffer. */ - void ClearLastChanges() + void ClearUndoBuffer() { m_undoBuffer.ClearItemsList(); } + /** + * Applies stored settings. + * \see Settings() + */ + void ApplySettings(); + + void EnableSnapping ( bool aEnable ) + { + m_snappingEnabled = aEnable; + } + + bool SnappingEnabled () const + { + return m_snappingEnabled; + } + + private: + void movePlacing ( const VECTOR2I& aP, PNS_ITEM* aItem ); + void moveDragging ( const VECTOR2I& aP, PNS_ITEM* aItem ); + + void eraseView(); + void updateView( PNS_NODE *aNode, PNS_ITEMSET &aCurrent ); //PNS_LINE *aCurrent = NULL ); + void clearViewFlags(); // optHoverItem queryHoverItemEx(const VECTOR2I& aP); PNS_ITEM* pickSingleItem( PNS_ITEMSET& aItems ) const; // std::vector aItems) const; void splitAdjacentSegments( PNS_NODE* aNode, PNS_ITEM* aSeg, const VECTOR2I& aP ); // optHoverItem& aItem); - void commitRouting( PNS_NODE* aNode ); - PNS_NODE* removeLoops( PNS_NODE* aNode, PNS_SEGMENT* aLatestSeg ); - PNS_NODE* removeLoops( PNS_NODE* aNode, PNS_LINE* aNewLine ); PNS_VIA* checkLoneVia( PNS_JOINT* aJoint ) const; PNS_ITEM* syncPad( D_PAD* aPad ); @@ -177,35 +208,45 @@ private: void highlightCurrent( bool enabled ); + void markViolations( PNS_NODE *aNode, PNS_ITEMSET& aCurrent, PNS_NODE::ItemVector& aRemoved ); + int m_currentLayer; int m_currentNet; - int m_currentWidth; - int m_currentViaDiameter; - int m_currentViaDrill; - - bool m_start_diagonal; RouterState m_state; BOARD* m_board; PNS_NODE* m_world; + PNS_NODE* m_lastNode; PNS_LINE_PLACER* m_placer; + PNS_DRAGGER *m_dragger; + PNS_LINE* m_draggedLine; + PNS_SHOVE* m_shove; + int m_draggedSegmentIndex; + int m_iterLimit; + bool m_showInterSteps; + int m_snapshotIter; KIGFX::VIEW* m_view; KIGFX::VIEW_GROUP* m_previewItems; + + PNS_ITEM *m_currentEndItem; + VECTOR2I m_currentEnd; VECTOR2I m_currentStart; VECTOR2I m_originalStart; bool m_placingVia; bool m_startsOnVia; + bool m_snappingEnabled; + bool m_violation; // optHoverItem m_startItem, m_endItem; PNS_ROUTING_SETTINGS m_settings; PNS_CLEARANCE_FUNC* m_clearanceFunc; - boost::unordered_set m_hiddenItems; + boost::unordered_set m_hiddenItems; ///> Stores list of modified items in the current operation PICKED_ITEMS_LIST m_undoBuffer; diff --git a/pcbnew/router/pns_routing_settings.cpp b/pcbnew/router/pns_routing_settings.cpp new file mode 100644 index 0000000000..cddc110311 --- /dev/null +++ b/pcbnew/router/pns_routing_settings.cpp @@ -0,0 +1,49 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "pns_routing_settings.h" + +PNS_ROUTING_SETTINGS::PNS_ROUTING_SETTINGS() +{ + m_routingMode = RM_Walkaround; + m_optimizerEffort = OE_Full; + m_removeLoops = true; + m_smartPads = true; + m_shoveVias = true; + m_suggestFinish = false; + m_followMouse = true; + m_startDiagonal = false; + m_shoveIterationLimit = 250; + m_shoveTimeLimit = 1000; + m_walkaroundIterationLimit = 40; + m_jumpOverObstacles = false; + m_smoothDraggedSegments = true; + m_canViolateDRC = false; +} + +TIME_LIMIT PNS_ROUTING_SETTINGS::ShoveTimeLimit() const +{ + return TIME_LIMIT ( m_shoveTimeLimit ); +} + +int PNS_ROUTING_SETTINGS::ShoveIterationLimit() const +{ + return m_shoveIterationLimit; +} diff --git a/pcbnew/router/pns_routing_settings.h b/pcbnew/router/pns_routing_settings.h index 72a4babef0..02eab2a352 100644 --- a/pcbnew/router/pns_routing_settings.h +++ b/pcbnew/router/pns_routing_settings.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,39 +15,147 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ -#ifndef __PNS_ROUTER_SETTINGS -#define __PNS_ROUTER_SETTINGS +#ifndef __PNS_ROUTING_SETTINGS +#define __PNS_ROUTING_SETTINGS +#include "direction.h" +#include "time_limit.h" + ///> Routing modes enum PNS_MODE { - RM_Ignore = 0, ///> Ignore collisions - RM_Shove, ///> Only shove - RM_Walkaround, ///> Only walkaround - RM_Smart ///> Guess what's better + RM_MarkObstacles = 0, ///> Ignore collisions, mark obstacles + RM_Shove, ///> Only shove + RM_Walkaround, ///> Only walkaround + RM_Smart ///> Guess what's better, try to make least mess on the PCB }; +///> Optimization effort +enum PNS_OPTIMIZATION_EFFORT +{ + OE_Low = 0, + OE_Medium = 1, + OE_Full = 2 +}; + +/** + * Class PNS_ROUTING_SETTINGS + * + * Contains all persistent settings of the router, such as the mode, optimization effort, etc. + */ + class PNS_ROUTING_SETTINGS { public: - PNS_MODE m_routingMode; + PNS_ROUTING_SETTINGS(); + ///> Returns the routing mode. + PNS_MODE Mode() const { return m_routingMode; } + + ///> Sets the routing mode. + void SetMode( PNS_MODE aMode ) { m_routingMode = aMode; } + + ///> Returns the optimizer effort. Bigger means cleaner traces, but slower routing. + PNS_OPTIMIZATION_EFFORT OptimizerEffort() const { return m_optimizerEffort; } + + ///> Sets the optimizer effort. Bigger means cleaner traces, but slower routing. + void SetOptimizerEffort( PNS_OPTIMIZATION_EFFORT aEffort ) { m_optimizerEffort = aEffort; } + + ///> Returns true if shoving vias is enbled. + bool ShoveVias() const { return m_shoveVias; } + + ///> Enables/disables shoving vias. + void SetShoveVias( bool aShoveVias ) { m_shoveVias = aShoveVias; } + + ///> Returns true if loop (redundant track) removal is on. + bool RemoveLoops() const { return m_removeLoops; } + + ///> Enables/disables loop (redundant track) removal. + void SetRemoveLoops( bool aRemoveLoops ) { m_removeLoops = aRemoveLoops; } + + ///> Returns true if suggesting the finish of currently placed track is on. + bool SuggestFinish() { return m_suggestFinish; } + + ///> Enables displaying suggestions for finishing the currently placed track. + void SetSuggestFinish( bool aSuggestFinish ) { m_suggestFinish = aSuggestFinish; } + + ///> Returns true if Smart Pads (automatic neckdown) is enabled. + bool SmartPads () const { return m_smartPads; } + + ///> Enables/disables Smart Pads (automatic neckdown). + void SetSmartPads( bool aSmartPads ) { m_smartPads = aSmartPads; } + + ///> Returns true if follow mouse mode is active (permanently on for the moment). + bool FollowMouse() const + { + return m_followMouse && !(Mode() == RM_MarkObstacles); + } + + ///> Returns true if smoothing segments durign dragging is enabled. + bool SmoothDraggedSegments() const { return m_smoothDraggedSegments; } + + ///> Enables/disabled smoothing segments during dragging. + void SetSmoothDraggedSegments( bool aSmooth ) { m_smoothDraggedSegments = aSmooth; } + + ///> Returns true if jumping over unmovable obstacles is on. + bool JumpOverObstacles() const { return m_jumpOverObstacles; } + + ///> Enables/disables jumping over unmovable obstacles. + void SetJumpOverObstacles( bool aJumpOverObstacles ) { m_jumpOverObstacles = aJumpOverObstacles; } + + void SetStartDiagonal(bool aStartDiagonal) { m_startDiagonal = aStartDiagonal; } + + bool CanViolateDRC() const { return m_canViolateDRC; } + void SetCanViolateDRC( bool aViolate ) { m_canViolateDRC = aViolate; } + + void SetTrackWidth( int aWidth ) { m_trackWidth = aWidth; } + int GetTrackWidth() const { return m_trackWidth; } + void SetViaDiameter( int aDiameter ) { m_viaDiameter = aDiameter; } + int GetViaDiameter() const { return m_viaDiameter; } + void SetViaDrill( int aDrill ) { m_viaDrill = aDrill; } + int GetViaDrill() const { return m_viaDrill; } + + const DIRECTION_45 InitialDirection() const + { + if(m_startDiagonal) + return DIRECTION_45 (DIRECTION_45::NE); + else + return DIRECTION_45 (DIRECTION_45::N); + } + + int ShoveIterationLimit() const; + TIME_LIMIT ShoveTimeLimit() const; + + int WalkaroundIterationLimit() const { return m_walkaroundIterationLimit; }; + TIME_LIMIT WalkaroundTimeLimit() const; + +private: + + bool m_shoveVias; + bool m_startDiagonal; bool m_removeLoops; bool m_smartPads; - bool m_suggestEnding; - bool m_shoveOnRequest; - bool m_changePostures; + bool m_suggestFinish; bool m_followMouse; + bool m_jumpOverObstacles; + bool m_smoothDraggedSegments; + bool m_canViolateDRC; - int m_lineWidth; + PNS_MODE m_routingMode; + PNS_OPTIMIZATION_EFFORT m_optimizerEffort; + + int m_trackWidth; int m_viaDiameter; int m_viaDrill; + int m_preferredLayer; int m_walkaroundIterationLimit; int m_shoveIterationLimit; + TIME_LIMIT m_shoveTimeLimit; + TIME_LIMIT m_walkaroundTimeLimit; }; #endif diff --git a/pcbnew/router/pns_segment.h b/pcbnew/router/pns_segment.h index 06ab41dfa0..0ebc4bc767 100644 --- a/pcbnew/router/pns_segment.h +++ b/pcbnew/router/pns_segment.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_SEGMENT_H @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include "pns_item.h" @@ -40,31 +40,27 @@ public: {}; PNS_SEGMENT( const SEG& aSeg, int aNet ) : - PNS_ITEM( SEGMENT ) + PNS_ITEM( SEGMENT ), m_seg(aSeg, 0) { m_net = aNet; - m_shape.Clear(); - m_shape.Append( aSeg.A ); - m_shape.Append( aSeg.B ); }; PNS_SEGMENT( const PNS_LINE& aParentLine, const SEG& aSeg ) : - PNS_ITEM( SEGMENT ) + PNS_ITEM( SEGMENT ), + m_seg(aSeg, aParentLine.Width()) { - m_net = aParentLine.GetNet(); - m_layers = aParentLine.GetLayers(); - m_width = aParentLine.GetWidth(); - m_shape.Clear(); - m_shape.Append( aSeg.A ); - m_shape.Append( aSeg.B ); + m_net = aParentLine.Net(); + m_layers = aParentLine.Layers(); + m_marker = aParentLine.Marker(); + m_rank = aParentLine.Rank(); }; - PNS_SEGMENT* Clone() const; + PNS_SEGMENT* Clone( ) const; - const SHAPE* GetShape() const + const SHAPE* Shape() const { - return static_cast( &m_shape ); + return static_cast( &m_seg ); } void SetLayer( int aLayer ) @@ -72,53 +68,59 @@ public: SetLayers( PNS_LAYERSET( aLayer ) ); } - int GetLayer() const + int Layer() const { - return GetLayers().Start(); - } - - const SHAPE_LINE_CHAIN& GetCLine() const - { - return m_shape; + return Layers().Start(); } void SetWidth( int aWidth ) { - m_width = aWidth; + m_seg.SetWidth(aWidth); } - int GetWidth() const + int Width() const { - return m_width; + return m_seg.GetWidth(); } - const SEG GetSeg() const + const SEG& Seg() const { - assert( m_shape.PointCount() >= 1 ); + return m_seg.GetSeg(); + } - if( m_shape.PointCount() == 1 ) - return SEG( m_shape.CPoint( 0 ), m_shape.CPoint( 0 ) ); - - return SEG( m_shape.CPoint( 0 ), m_shape.CPoint( 1 ) ); + const SHAPE_LINE_CHAIN CLine() const + { + return SHAPE_LINE_CHAIN( m_seg.GetSeg().A, m_seg.GetSeg().B ); } void SetEnds( const VECTOR2I& a, const VECTOR2I& b ) { - m_shape.Clear(); - m_shape.Append( a ); - m_shape.Append( b ); - } + m_seg.SetSeg( SEG ( a, b ) ); + } void SwapEnds() { - m_shape = m_shape.Reverse(); + SEG tmp = m_seg.GetSeg(); + m_seg.SetSeg( SEG (tmp.B , tmp.A )); } const SHAPE_LINE_CHAIN Hull( int aClearance, int aWalkaroundThickness ) const; + virtual VECTOR2I Anchor(int n) const + { + if(n == 0) + return m_seg.GetSeg().A; + else + return m_seg.GetSeg().B; + } + + virtual int AnchorCount() const + { + return 2; + } + private: - SHAPE_LINE_CHAIN m_shape; - int m_width; + SHAPE_SEGMENT m_seg; }; #endif diff --git a/pcbnew/router/pns_shove.cpp b/pcbnew/router/pns_shove.cpp index ad58ee1feb..491f3327f3 100644 --- a/pcbnew/router/pns_shove.cpp +++ b/pcbnew/router/pns_shove.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,477 +15,1067 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include #include -#include +#include #include "trace.h" +#include "range.h" #include "pns_line.h" #include "pns_node.h" #include "pns_walkaround.h" #include "pns_shove.h" +#include "pns_solid.h" #include "pns_optimizer.h" #include "pns_via.h" #include "pns_utils.h" +#include "pns_router.h" +#include "pns_shove.h" + +#include "time_limit.h" #include -PNS_SHOVE::PNS_SHOVE( PNS_NODE* aWorld ) +static void sanityCheck (PNS_LINE *l_old, PNS_LINE *l_new) +{ + assert (l_old->CPoint(0) == l_new->CPoint(0) ); + assert (l_old->CPoint(-1) == l_new->CPoint(-1 )); +} + +PNS_SHOVE::PNS_SHOVE( PNS_NODE* aWorld, PNS_ROUTER *aRouter ) : + PNS_ALGO_BASE ( aRouter ) { m_root = aWorld; - m_iterLimit = 100; }; PNS_SHOVE::~PNS_SHOVE() { +// free all the stuff we've created during routing/dragging operation. + BOOST_FOREACH(PNS_ITEM *item, m_gcItems) + delete item; } - -struct range +// garbage-collected line assembling +PNS_LINE* PNS_SHOVE::assembleLine ( const PNS_SEGMENT *aSeg, int *aIndex ) { - range() - { - min_v = max_v = -1; - } + PNS_LINE *l = m_currentNode->AssembleLine( const_cast (aSeg), aIndex); - void add( int x ) - { - if( min_v < 0 ) min_v = x; + m_gcItems.push_back(l); + return l; +} - if( max_v < 0 ) max_v = x; - - if( x < min_v ) - min_v = x; - else if( x > max_v ) - max_v = x; - } - - int start() - { - return min_v; - } - - int end() - { - return max_v; - } - - int min_v, max_v; -}; - -// fixme: this is damn f***ing inefficient. And fails much too often due to broken direction finding algorithm. -bool PNS_SHOVE::tryShove( PNS_NODE* aNode, PNS_LINE* aHead, PNS_LINE* aObstacle, - PNS_SEGMENT& aObstacleSeg, PNS_LINE* aResult, bool aInvertWinding ) +// garbage-collected line cloning +PNS_LINE *PNS_SHOVE::cloneLine ( const PNS_LINE *aLine ) { - const SHAPE_LINE_CHAIN& head = aHead->GetCLine(); - bool cw = false; - int i; + PNS_LINE *l = aLine->Clone(); - if( aHead->EndsWithVia() && !aHead->GetLayers().Overlaps( aObstacle->GetLayers() ) ) + m_gcItems.push_back(l); + return l; +} + +// A dumb function that checks if the shoved line is shoved the right way, e.g. +// visually "outwards" of the line/via applying pressure on it. Unfortunately there's no +// mathematical concept of orientation of an open curve, so we use some primitive heuristics: +// if the shoved line wraps around the start of the "pusher", it's likely shoved in wrong direction. +bool PNS_SHOVE::checkBumpDirection ( PNS_LINE *aCurrent, PNS_LINE *aShoved ) const +{ + const SEG ss = aCurrent->CSegment(0); + + int dist = m_currentNode->GetClearance(aCurrent, aShoved) + PNS_HULL_MARGIN; + + dist += aCurrent->Width() / 2; + dist += aShoved->Width() / 2; + + const VECTOR2I ps = ss.A - (ss.B - ss.A).Resize(dist); + + return !aShoved->CLine().PointOnEdge(ps); +} + +PNS_SHOVE::ShoveStatus PNS_SHOVE::walkaroundLoneVia ( PNS_LINE *aCurrent, PNS_LINE *aObstacle, PNS_LINE *aShoved ) +{ + int clearance = m_currentNode->GetClearance( aCurrent, aObstacle ); + const SHAPE_LINE_CHAIN hull = aCurrent->Via().Hull( clearance, aObstacle->Width() ); + SHAPE_LINE_CHAIN path_cw, path_ccw; + + aObstacle->Walkaround( hull, path_cw, true ); + aObstacle->Walkaround( hull, path_ccw, false ); + + const SHAPE_LINE_CHAIN& shortest = path_ccw.Length() < path_cw.Length() ? path_ccw : path_cw; + + if(shortest.PointCount() < 2) + return SH_INCOMPLETE; + if(aObstacle->CPoint(-1) != shortest.CPoint(-1)) + return SH_INCOMPLETE; + if(aObstacle->CPoint(0) != shortest.CPoint(0)) + return SH_INCOMPLETE; + + aShoved->SetShape( shortest ); + + if( m_currentNode->CheckColliding( aShoved, aCurrent ) ) + return SH_INCOMPLETE; + + return SH_OK; +} + +PNS_SHOVE::ShoveStatus PNS_SHOVE::processHullSet ( PNS_LINE *aCurrent, PNS_LINE *aObstacle, PNS_LINE *aShoved, const HullSet& hulls ) +{ + const SHAPE_LINE_CHAIN& obs = aObstacle->CLine(); + bool failingDirCheck = false; + int attempt; + + for(attempt = 0; attempt < 4; attempt++) { - int clearance = aNode->GetClearance( aHead, aObstacle ); - SHAPE_LINE_CHAIN hull = aHead->GetVia().Hull( clearance - aObstacle->GetWidth() / 2 ); + bool invertTraversal = (attempt >= 2); + bool clockwise = attempt % 2; + int vFirst = -1, vLast = -1; - // SHAPE_LINE_CHAIN path_pre, path_walk_cw, path_walk_ccw, path_post; + SHAPE_LINE_CHAIN path; + PNS_LINE l ( *aObstacle ); - SHAPE_LINE_CHAIN path_cw, path_ccw, * path; - - aObstacle->NewWalkaround( hull, path_cw, true ); - aObstacle->NewWalkaround( hull, path_ccw, false ); - - path = path_ccw.Length() < path_cw.Length() ? &path_ccw : &path_cw; - aResult->SetShape( *path ); - - // PNSDisplayDebugLine (*path, 5); - - if( !aResult->Is45Degree() ) + for(int i = 0; i < (int)hulls.size(); i++ ) { - // printf("polyset non-45\npoly %s\nendpolyset\n", aResult->GetCLine().Format().c_str()); + const SHAPE_LINE_CHAIN& hull = hulls[invertTraversal ? hulls.size() - 1 - i : i]; + + l.Walkaround( hull, path, clockwise ); + path.Simplify(); + l.SetShape( path ); + } + + for(int i = 0; i < std::min ( path.PointCount(), obs.PointCount() ); i++) + { + if(path.CPoint(i) != obs.CPoint(i)) + { + vFirst = i; + break; + } } - /*... special case for vias? */ + int k = obs.PointCount() - 1; + for(int i = path.PointCount() - 1; i >= 0 && k >= 0; i--, k--) + { + if(path.CPoint(i) != obs.CPoint(k)) + { + vLast = i; + break; + } + } - return !aNode->CheckColliding( aResult, aHead ); + if( ( vFirst < 0 || vLast < 0) && !path.CompareGeometry( aObstacle->CLine() )) + { + TRACE( 100, "attempt %d fail vfirst-last", attempt ); + continue; + } + + if(path.CPoint(-1) != obs.CPoint(-1) || path.CPoint(0) != obs.CPoint(0)) + { + TRACE(100, "attempt %d fail vend-start\n", attempt); + continue; + } + + if(!checkBumpDirection(aCurrent, &l)) + { + TRACE( 100, "attempt %d fail direction-check", attempt ); + failingDirCheck = true; + aShoved->SetShape(l.CLine()); + continue; + } + + if(path.SelfIntersecting()) + { + TRACE( 100, "attempt %d fail self-intersect", attempt ); + continue; + } + + bool colliding = m_currentNode->CheckColliding( &l, aCurrent ); + + if( (aCurrent->Marker() & MK_HEAD) && !colliding) + { + PNS_JOINT *jtStart = m_currentNode->FindJoint ( aCurrent->CPoint(0), aCurrent ); + + BOOST_FOREACH( PNS_ITEM *item, jtStart->LinkList() ) + { + if(m_currentNode->CheckColliding(item, &l)) + colliding = true; + } + } + + if( colliding ) + { + TRACE( 100, "attempt %d fail coll-check", attempt ); + continue; + } + + aShoved->SetShape( l.CLine() ); + + return SH_OK; } - int ns = head.SegmentCount(); + return failingDirCheck ? SH_OK : SH_INCOMPLETE; +} - if( aHead->EndsWithVia() ) - ns++; +PNS_SHOVE::ShoveStatus PNS_SHOVE::processSingleLine ( PNS_LINE *aCurrent, PNS_LINE *aObstacle, PNS_LINE *aShoved ) +{ + aShoved->ClearSegmentLinks(); - for( i = 0; i < head.SegmentCount(); i++ ) + bool obstacleIsHead = false; + + if( aObstacle->LinkedSegments() ) { - const PNS_SEGMENT hs( *aHead, head.CSegment( i ) ); - - - if( aNode->CheckColliding( &hs, aObstacle ) ) + BOOST_FOREACH( PNS_SEGMENT *s, *aObstacle->LinkedSegments() ) + if(s->Marker() & MK_HEAD) { - VECTOR2I v1 = hs.GetSeg().B - hs.GetSeg().A; - VECTOR2I v2 = aObstacleSeg.GetSeg().B - aObstacleSeg.GetSeg().A; - - VECTOR2I::extended_type det = v1.Cross( v2 ); - - if( det > 0 ) - cw = true; - else - cw = false; - + obstacleIsHead = true; break; } } - if( aInvertWinding ) + ShoveStatus rv; + + bool viaOnEnd = aCurrent->EndsWithVia(); + + if( viaOnEnd && ( !aCurrent->LayersOverlap( aObstacle ) || aCurrent->SegmentCount() == 0 ) ) { - if( cw ) - cw = false; - else - cw = true; - } + rv = walkaroundLoneVia( aCurrent, aObstacle, aShoved ); + } else { + int w = aObstacle->Width(); + int n_segs = aCurrent->SegmentCount(); + int clearance = m_currentNode->GetClearance( aCurrent, aObstacle ); - PNS_LINE shoved( *aObstacle ); + HullSet hulls; - int clearance = aNode->GetClearance( aHead, aObstacle ); + hulls.reserve( n_segs + 1 ); - range r; - - for( i = 0; i < ns; i++ ) - { - SHAPE_LINE_CHAIN hull; - - if( i < head.SegmentCount() ) + for( int i = 0; i < n_segs; i++ ) { - const PNS_SEGMENT hs( *aHead, head.CSegment( i ) ); - hull = hs.Hull( clearance, 0 ); + PNS_SEGMENT seg ( *aCurrent, aCurrent->CSegment(i) ); + hulls.push_back ( seg.Hull( clearance, w ) ); } - else - hull = aHead->GetVia().Hull( clearance - aObstacle->GetWidth() / 2 ); - SHAPE_LINE_CHAIN path_pre, path_walk, path_post, tmp; - SHAPE_LINE_CHAIN path_pre2, path_walk2, path_post2; + if( viaOnEnd ) + hulls.push_back ( aCurrent->Via().Hull( clearance, w ) ); - // shoved.NewWalkaround(hull, path_pre, path_walk, path_post, cw); - shoved.NewWalkaround( hull, path_pre, path_walk, path_post, cw ); - - /*if(path_pre != path_pre2 || path_post != path_post2 || path_walk != path_walk2 ) - * { - * TRACE(5, "polyset orig\npoly %s\npoly %s\npoly %s\nendpolyset\n", path_pre.Format().c_str() % path_walk.Format().c_str() % path_post.Format().c_str()); - * TRACE(5, "polyset err\npoly %s\npoly %s\npoly %s\nendpolyset\n", path_pre2.Format().c_str() % path_walk2.Format().c_str() % path_post2.Format().c_str()); - * }*/ - - tmp = shoved.GetCLine(); - - if( path_walk.SegmentCount() ) - r.add( i ); - - path_pre.Append( path_walk ); - path_pre.Append( path_post ); - path_pre.Simplify(); - shoved.SetShape( path_pre ); -// shoved.SetAffectedRange ( start, end ); - *aResult = shoved; - - if( !aResult->Is45Degree() ) - { - // TRACE(5, "polyset non-45\npoly %s\npoly %s\npoly %s\nendpolyset\n", tmp.Format().c_str() % hull.Format().c_str() % aResult->GetCLine().Format().c_str()); - } + rv = processHullSet ( aCurrent, aObstacle, aShoved, hulls); } + + if(obstacleIsHead) + aShoved->Mark( aShoved->Marker() | MK_HEAD ); - TRACE( 2, "CW %d affectedRange %d-%d [total %d]", (cw ? 1 : 0) % r.start() % r.end() % ns ); - - return !aNode->CheckColliding( aResult, aHead ); + return rv; } -PNS_SHOVE::ShoveStatus PNS_SHOVE::shoveSingleLine( PNS_NODE* aNode, PNS_LINE* aCurrent, - PNS_LINE* aObstacle, PNS_SEGMENT& aObstacleSeg, PNS_LINE* aResult ) +PNS_SHOVE::ShoveStatus PNS_SHOVE::onCollidingSegment( PNS_LINE *aCurrent, PNS_SEGMENT *aObstacleSeg ) { - bool rv = tryShove( aNode, aCurrent, aObstacle, aObstacleSeg, aResult, false ); + int segIndex; + PNS_LINE *obstacleLine = assembleLine (aObstacleSeg, &segIndex); + PNS_LINE *shovedLine = cloneLine ( obstacleLine ); + + ShoveStatus rv = processSingleLine ( aCurrent, obstacleLine, shovedLine ); - if( !rv ) - rv = tryShove( aNode, aCurrent, aObstacle, aObstacleSeg, aResult, true ); + assert ( obstacleLine->LayersOverlap (shovedLine) ); - if( !rv ) + + if(rv == SH_OK) { - TRACEn( 2, "Shove failed" ); - return SH_INCOMPLETE; + if ( shovedLine->Marker() & MK_HEAD ) + m_newHead = *shovedLine; + + sanityCheck(obstacleLine, shovedLine); + m_currentNode->Replace (obstacleLine, shovedLine); + sanityCheck(obstacleLine, shovedLine); + + int rank = aCurrent->Rank(); + shovedLine->SetRank ( rank - 1 ); + + pushLine(shovedLine); } - aResult->GetLine().Simplify(); +#ifdef DEBUG + m_logger.NewGroup ("on-colliding-segment", m_iter); + m_logger.Log ( aObstacleSeg, 0, "obstacle-segment"); + m_logger.Log ( aCurrent, 1, "current-line"); + m_logger.Log ( obstacleLine, 2, "obstacle-line"); + m_logger.Log ( shovedLine, 3, "shoved-line"); +#endif - const SHAPE_LINE_CHAIN& sh_shoved = aResult->GetCLine(); - const SHAPE_LINE_CHAIN& sh_orig = aObstacle->GetCLine(); + return rv; +} - if( sh_shoved.SegmentCount() > 1 && sh_shoved.CPoint( 0 ) == sh_orig.CPoint( 0 ) - && sh_shoved.CPoint( -1 ) == sh_orig.CPoint( -1 ) ) - return SH_OK; - else if( !sh_shoved.SegmentCount() ) - return SH_NULL; - else +PNS_SHOVE::ShoveStatus PNS_SHOVE::onCollidingLine( PNS_LINE *aCurrent, PNS_LINE *aObstacle ) +{ + PNS_LINE *shovedLine = cloneLine(aObstacle); + + ShoveStatus rv = processSingleLine ( aCurrent, aObstacle, shovedLine ); + + if(rv == SH_OK) + { + if ( shovedLine->Marker() & MK_HEAD ) + m_newHead = *shovedLine; + + sanityCheck(aObstacle,shovedLine); + m_currentNode->Replace( aObstacle, shovedLine ); + sanityCheck(aObstacle,shovedLine); + + int rank = aObstacle->Rank(); + shovedLine->SetRank ( rank ); + + pushLine(shovedLine); + + #ifdef DEBUG + m_logger.NewGroup ("on-colliding-line", m_iter); + m_logger.Log ( aObstacle, 0, "obstacle-line"); + m_logger.Log ( aCurrent, 1, "current-line"); + m_logger.Log ( shovedLine, 3, "shoved-line"); + #endif + + + } + + + return rv; +} + +PNS_SHOVE::ShoveStatus PNS_SHOVE::onCollidingSolid( PNS_LINE *aCurrent, PNS_SOLID *aObstacleSolid ) +{ + PNS_WALKAROUND walkaround( m_currentNode, Router() ); + PNS_LINE* walkaroundLine = cloneLine(aCurrent); + + + if(aCurrent->EndsWithVia()) + { + PNS_VIA vh = aCurrent->Via(); + PNS_VIA *via = NULL; + PNS_JOINT *jtStart = m_currentNode->FindJoint ( vh.Pos(), aCurrent ); + + if(!jtStart) + return SH_INCOMPLETE; + + BOOST_FOREACH( PNS_ITEM *item, jtStart->LinkList() ) + if(item->OfKind(PNS_ITEM::VIA)) + { + via = (PNS_VIA *) item; + break; + } + + if( via && m_currentNode->CheckColliding(via, aObstacleSolid) ) + return onCollidingVia ( aObstacleSolid, via ); + } + + walkaround.SetSolidsOnly( true ); + walkaround.SetIterationLimit ( 8 ); // fixme: make configurable + + int currentRank = aCurrent->Rank(); + int nextRank; + + if (!Settings().JumpOverObstacles() ) + { + nextRank = currentRank + 10000; + walkaround.SetSingleDirection( false ); + } else { + nextRank = currentRank - 1; + walkaround.SetSingleDirection( true ); + } + + + if (walkaround.Route( *aCurrent, *walkaroundLine, false ) != PNS_WALKAROUND::DONE ) return SH_INCOMPLETE; + + walkaroundLine->ClearSegmentLinks(); + walkaroundLine->Unmark(); + walkaroundLine->Line().Simplify(); + + if(walkaroundLine->HasLoops()) + return SH_INCOMPLETE; + + if (aCurrent->Marker() & MK_HEAD) + { + walkaroundLine->Mark(MK_HEAD); + m_newHead = *walkaroundLine; + } + + + m_currentNode->Replace( aCurrent, walkaroundLine ); + walkaroundLine->SetRank ( nextRank ); + +#ifdef DEBUG + m_logger.NewGroup ("on-colliding-solid", m_iter); + m_logger.Log ( aObstacleSolid, 0, "obstacle-solid"); + m_logger.Log ( aCurrent, 1, "current-line"); + m_logger.Log ( walkaroundLine, 3, "walk-line"); +#endif + + popLine(); + pushLine(walkaroundLine); + + return SH_OK; } -bool PNS_SHOVE::reduceSpringback( PNS_LINE* aHead ) +bool PNS_SHOVE::reduceSpringback( const PNS_ITEMSET& aHeadSet ) { bool rv = false; while( !m_nodeStack.empty() ) { - SpringbackTag st_stack = m_nodeStack.back(); - bool tail_ok = true; + SpringbackTag spTag = m_nodeStack.back(); - if( !st_stack.node->CheckColliding( aHead ) && tail_ok ) + if( !spTag.node->CheckColliding( aHeadSet ) ) { rv = true; - delete st_stack.node; + + delete spTag.node; m_nodeStack.pop_back(); } else - break; + break; } return rv; } -bool PNS_SHOVE::pushSpringback( PNS_NODE* aNode, PNS_LINE* aHead, const PNS_COST_ESTIMATOR& aCost ) + +bool PNS_SHOVE::pushSpringback( PNS_NODE* aNode, const PNS_ITEMSET& aHeadItems, const PNS_COST_ESTIMATOR& aCost ) { - BOX2I headBB = aHead->GetCLine().BBox(); SpringbackTag st; st.node = aNode; st.cost = aCost; - st.length = std::max( headBB.GetWidth(), headBB.GetHeight() );; + st.headItems = aHeadItems; m_nodeStack.push_back( st ); return true; } -const PNS_COST_ESTIMATOR PNS_SHOVE::TotalCost() const +PNS_SHOVE::ShoveStatus PNS_SHOVE::pushVia ( PNS_VIA *aVia, const VECTOR2I& aForce, int aCurrentRank ) { - if( m_nodeStack.empty() ) - return PNS_COST_ESTIMATOR(); - else - return m_nodeStack.back().cost; + + LinePairVec draggedLines; + VECTOR2I p0 ( aVia->Pos() ); + PNS_JOINT *jt = m_currentNode->FindJoint( p0, 1, aVia->Net() ); + PNS_VIA *pushedVia = aVia -> Clone(); + + pushedVia->SetPos( p0 + aForce ); + pushedVia->Mark( aVia->Marker() ) ; + + if(aVia->Marker() & MK_HEAD) + { + m_draggedVia = pushedVia; + } + + if(!jt) + { + TRACEn(1, "weird, can't find the center-of-via joint\n"); + return SH_INCOMPLETE; + } + + BOOST_FOREACH(PNS_ITEM *item, jt->LinkList() ) + { + if(item->OfKind( PNS_ITEM::SEGMENT )) + { + PNS_SEGMENT *seg = (PNS_SEGMENT *) item; + LinePair lp; + int segIndex; + + lp.first = assembleLine(seg, &segIndex); + + assert(segIndex == 0 || (segIndex == (lp.first->SegmentCount() - 1) )); + + if(segIndex == 0) + lp.first->Reverse(); + + lp.second = cloneLine( lp.first ); + lp.second->ClearSegmentLinks(); + lp.second->DragCorner( p0 + aForce, lp.second->CLine().Find( p0 )); + lp.second->AppendVia ( *pushedVia ); + draggedLines.push_back(lp); + } + } + + m_currentNode->Remove( aVia ); + m_currentNode->Add ( pushedVia ); + + if(aVia->BelongsTo(m_currentNode)) + delete aVia; + + pushedVia -> SetRank (aCurrentRank - 1); + +#ifdef DEBUG + m_logger.Log ( aVia, 0, "obstacle-via"); + m_logger.Log ( pushedVia, 1, "pushed-via"); +#endif + + BOOST_FOREACH( LinePair lp, draggedLines ) + { + if(lp.first->Marker() & MK_HEAD) + { + lp.second->Mark ( MK_HEAD ); + m_newHead = *lp.second; + } + + unwindStack(lp.first); + + if(lp.second->SegmentCount()) + { + m_currentNode->Replace ( lp.first, lp.second ); + lp.second->SetRank( aCurrentRank - 1); + pushLine(lp.second); + } else + m_currentNode->Remove(lp.first); + +#ifdef DEBUG + m_logger.Log ( lp.first, 2, "fan-pre"); + m_logger.Log ( lp.second, 3, "fan-post"); +#endif + } + + return SH_OK; } -PNS_SHOVE::ShoveStatus PNS_SHOVE::ShoveLines( PNS_LINE* aCurrentHead ) +PNS_SHOVE::ShoveStatus PNS_SHOVE::onCollidingVia (PNS_ITEM *aCurrent, PNS_VIA *aObstacleVia ) { - std::stack lineStack; - PNS_NODE* node, * parent; + int clearance = m_currentNode->GetClearance( aCurrent, aObstacleVia ) ; + LinePairVec draggedLines; + VECTOR2I p0 ( aObstacleVia->Pos() ); + bool colLine = false, colVia = false; + PNS_LINE *currentLine = NULL; + VECTOR2I mtvLine, mtvVia, mtv, mtvSolid; + int rank = -1; + + if( aCurrent->OfKind (PNS_ITEM::LINE)) + { + +#ifdef DEBUG + m_logger.NewGroup ("push-via-by-line", m_iter); + m_logger.Log(aCurrent, 4, "current"); +#endif + + currentLine = (PNS_LINE*) aCurrent; + colLine = CollideShapes( aObstacleVia->Shape(), currentLine->Shape(), clearance + currentLine->Width() / 2 + PNS_HULL_MARGIN, true, mtvLine ); + + if(currentLine->EndsWithVia()) + colVia = CollideShapes (currentLine->Via().Shape(), aObstacleVia->Shape(), clearance + PNS_HULL_MARGIN, true, mtvVia); + + if(!colLine && !colVia) + return SH_OK; + + if(colLine && colVia) + mtv = mtvVia.EuclideanNorm() > mtvLine.EuclideanNorm() ? mtvVia : mtvLine; + else if (colLine) + mtv = mtvLine; + else + mtv = mtvVia; + rank = currentLine->Rank(); + } + else if (aCurrent->OfKind(PNS_ITEM::SOLID)) + { + CollideShapes( aObstacleVia->Shape(), aCurrent->Shape(), clearance + PNS_HULL_MARGIN, true, mtvSolid ); + mtv = mtvSolid; + rank = aCurrent->Rank() + 10000; + } + + return pushVia ( aObstacleVia, mtv, rank ); +} + +PNS_SHOVE::ShoveStatus PNS_SHOVE::onReverseCollidingVia (PNS_LINE *aCurrent, PNS_VIA *aObstacleVia ) +{ + std::vector steps; + int n = 0; + PNS_LINE *cur = cloneLine( aCurrent ); + cur->ClearSegmentLinks(); + + PNS_JOINT *jt = m_currentNode->FindJoint ( aObstacleVia->Pos(), aObstacleVia ); + PNS_LINE *shoved = cloneLine( aCurrent ); + shoved->ClearSegmentLinks(); + + + cur->RemoveVia(); + unwindStack(aCurrent); + + BOOST_FOREACH( PNS_ITEM *item, jt->LinkList() ) + { + + if (item->OfKind(PNS_ITEM::SEGMENT) && item->LayersOverlap (aCurrent) ) + { + PNS_SEGMENT *seg = (PNS_SEGMENT *) item; + PNS_LINE *head = assembleLine( seg ); + + head->AppendVia( *aObstacleVia ); + + ShoveStatus st = processSingleLine ( head, cur, shoved ); + + if( st != SH_OK ) + { +#ifdef DEBUG + m_logger.NewGroup ("on-reverse-via-fail-shove", m_iter); + m_logger.Log ( aObstacleVia, 0, "the-via"); + m_logger.Log ( aCurrent, 1, "current-line"); + m_logger.Log ( shoved, 3, "shoved-line"); +#endif + + return st; + } + cur->SetShape ( shoved->CLine() ); + n++; + } + } + + if(!n) + { +#ifdef DEBUG + m_logger.NewGroup ("on-reverse-via-fail-lonevia", m_iter); + m_logger.Log ( aObstacleVia, 0, "the-via"); + m_logger.Log ( aCurrent, 1, "current-line"); +#endif + + PNS_LINE head(*aCurrent); + head.Line().Clear(); + head.AppendVia( *aObstacleVia ); + head.ClearSegmentLinks(); + + ShoveStatus st = processSingleLine ( &head, aCurrent, shoved ); + + if( st != SH_OK ) + return st; + + cur->SetShape ( shoved->CLine() ); + } + + if(aCurrent->EndsWithVia()) + shoved->AppendVia( aCurrent->Via( )); + +#ifdef DEBUG + m_logger.NewGroup ("on-reverse-via", m_iter); + m_logger.Log ( aObstacleVia, 0, "the-via"); + m_logger.Log ( aCurrent, 1, "current-line"); + m_logger.Log ( shoved, 3, "shoved-line"); +#endif + int currentRank = aCurrent->Rank(); + m_currentNode->Replace ( aCurrent, shoved ); + + pushLine(shoved); + shoved->SetRank( currentRank ); + + return SH_OK; +} + + +void PNS_SHOVE::unwindStack ( PNS_SEGMENT *seg ) +{ + for (std::vector::iterator i = m_lineStack.begin(); i != m_lineStack.end(); ) + { + if( (*i)->ContainsSegment ( seg ) ) + i = m_lineStack.erase( i ); + else + i++; + } + + for (std::vector::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end(); ) + { + if( (*i)->ContainsSegment ( seg ) ) + i = m_optimizerQueue.erase( i ); + else + i++; + } +} + +void PNS_SHOVE::unwindStack ( PNS_ITEM *item ) +{ + if (item->OfKind(PNS_ITEM::SEGMENT)) + unwindStack(static_cast(item)); + else if (item->OfKind(PNS_ITEM::LINE)) { + PNS_LINE *l = static_cast( item ); + + if (!l->LinkedSegments()) + return; + + BOOST_FOREACH(PNS_SEGMENT *seg, *l->LinkedSegments() ) + unwindStack(seg); + } +} + +void PNS_SHOVE::pushLine (PNS_LINE *l) +{ + if(l->LinkCount() >= 0 && (l->LinkCount() != l->SegmentCount())) + assert(false); + + m_lineStack.push_back(l); + m_optimizerQueue.push_back(l); +} + +void PNS_SHOVE::popLine( ) +{ + PNS_LINE *l = m_lineStack.back(); + + for (std::vector::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end(); ) + { + if( (*i) == l ) + { + i = m_optimizerQueue.erase( i ); + } else + i++; + } + + m_lineStack.pop_back(); +} + +PNS_SHOVE::ShoveStatus PNS_SHOVE::shoveIteration(int aIter) +{ + PNS_LINE* currentLine = m_lineStack.back(); + PNS_NODE::OptObstacle nearest; + ShoveStatus st; + + PNS_ITEM::PnsKind search_order[] = { PNS_ITEM::SOLID, PNS_ITEM::VIA, PNS_ITEM::SEGMENT }; + + for(int i = 0; i < 3; i++) + { + nearest = m_currentNode->NearestObstacle( currentLine, search_order[i] ); + if(nearest) + break; + } + + if( !nearest ) + { + m_lineStack.pop_back(); + return SH_OK; + } + + PNS_ITEM *ni = nearest->item; + + unwindStack(ni); + + if( !ni->OfKind(PNS_ITEM::SOLID) && ni->Rank() >= 0 && ni->Rank() > currentLine->Rank() ) + { + switch( ni->Kind() ) + { + case PNS_ITEM::VIA: + { + PNS_VIA *revVia = (PNS_VIA *) ni; + TRACE( 2, "iter %d: reverse-collide-via", aIter ); + + if (currentLine->EndsWithVia() && m_currentNode->CheckColliding(¤tLine->Via(), revVia)) + { + st = SH_INCOMPLETE; + } else { + st = onReverseCollidingVia ( currentLine, revVia ); + } + + break; + } + + case PNS_ITEM::SEGMENT: + { + PNS_SEGMENT *seg = (PNS_SEGMENT* ) ni; + TRACE( 2, "iter %d: reverse-collide-segment ", aIter ); + PNS_LINE *revLine = assembleLine ( seg ); + + popLine(); + st = onCollidingLine( revLine, currentLine ); + pushLine(revLine); + break; + } + + default: + assert(false); + } + } else { // "forward" collisoins + switch( ni->Kind() ) + { + case PNS_ITEM::SEGMENT: + TRACE( 2, "iter %d: collide-segment ", aIter ); + st = onCollidingSegment( currentLine, (PNS_SEGMENT* ) ni ); + break; + + case PNS_ITEM::VIA: + TRACE( 2, "iter %d: shove-via ", aIter ); + st = onCollidingVia ( currentLine, (PNS_VIA *) ni ); + break; + + case PNS_ITEM::SOLID: + TRACE( 2, "iter %d: walk-solid ", aIter ); + st = onCollidingSolid ( currentLine, (PNS_SOLID *) ni ); + break; + + default: + break; + } + } + + return st; +} + +PNS_SHOVE::ShoveStatus PNS_SHOVE::shoveMainLoop() +{ + ShoveStatus st = SH_OK; + + TRACE( 1, "ShoveStart [root: %d jts, current: %d jts]", m_root->JointCount() % + m_currentNode->JointCount() ); + + int iterLimit = Settings().ShoveIterationLimit(); + TIME_LIMIT timeLimit = Settings().ShoveTimeLimit(); + + m_iter = 0; + + timeLimit.Restart(); + + while( !m_lineStack.empty() ) + { + st = shoveIteration(m_iter); + + m_iter++; + + if( st == SH_INCOMPLETE || timeLimit.Expired() || m_iter >= iterLimit ) + { + st = SH_INCOMPLETE; + break; + } + } + + return st; +} + + +PNS_SHOVE::ShoveStatus PNS_SHOVE::ShoveLines( const PNS_LINE& aCurrentHead ) +{ + ShoveStatus st = SH_OK; + + // empty head? nothing to shove... + if( ! aCurrentHead.SegmentCount() ) + return SH_INCOMPLETE; + + PNS_LINE* head = cloneLine ( &aCurrentHead ); + head->ClearSegmentLinks(); + + m_lineStack.clear(); + m_optimizerQueue.clear(); + m_newHead = OptLine(); + m_logger.Clear(); + + PNS_ITEMSET headSet ( cloneLine( &aCurrentHead ) ); + + reduceSpringback( headSet ); + + PNS_NODE *parent = m_nodeStack.empty() ? m_root : m_nodeStack.back().node; + + m_currentNode = parent->Branch(); + m_currentNode->ClearRanks(); + m_currentNode->Add( head ); + + head->Mark ( MK_HEAD ); + head->SetRank ( 100000 ); + + m_logger.NewGroup ("initial", 0); + m_logger.Log ( head, 0, "head"); + PNS_VIA* headVia = NULL; - bool fail = false; - int iter = 0; - - PNS_LINE* head = aCurrentHead->Clone(); - - reduceSpringback( aCurrentHead ); - - parent = m_nodeStack.empty() ? m_root : m_nodeStack.back().node; - node = parent->Branch(); - - lineStack.push( head ); - - // node->Add(tail); - node->Add( head ); if( head->EndsWithVia() ) { - headVia = head->GetVia().Clone(); - node->Add( headVia ); + headVia = head->Via().Clone(); + m_currentNode->Add( headVia ); + headVia->Mark( MK_HEAD ); + headVia->SetRank ( 100000 ); + m_logger.Log ( headVia, 0, "head-via"); + } - PNS_OPTIMIZER optimizer( node ); + pushLine (head); + st = shoveMainLoop(); + runOptimizer ( m_currentNode, head ); - optimizer.SetEffortLevel( PNS_OPTIMIZER::MERGE_SEGMENTS | PNS_OPTIMIZER::SMART_PADS ); - optimizer.SetCollisionMask( -1 ); - PNS_NODE::OptObstacle nearest; - - optimizer.CacheStaticItem( head ); - - if( headVia ) - optimizer.CacheStaticItem( headVia ); - - TRACE( 1, "ShoveStart [root: %d jts, node: %d jts]", m_root->JointCount() % - node->JointCount() ); - - // PNS_ITEM *lastWalkSolid = NULL; - prof_counter totalRealTime; - - wxLongLong t_start = wxGetLocalTimeMillis(); - - while( !lineStack.empty() ) + if( m_newHead && st == SH_OK ) { - wxLongLong t_cur = wxGetLocalTimeMillis(); + st = SH_HEAD_MODIFIED; + Router()->DisplayDebugLine ( m_newHead->CLine(), 3, 20000 ); + } - if( (t_cur - t_start).ToLong() > ShoveTimeLimit ) - { - fail = true; - break; - } + m_currentNode->RemoveByMarker ( MK_HEAD ); - iter++; + TRACE( 1, "Shove status : %s after %d iterations", ((st == SH_OK || st == SH_HEAD_MODIFIED) ? "OK" : "FAILURE") % m_iter ); - if( iter > m_iterLimit ) - { - fail = true; - break; - } - - PNS_LINE* currentLine = lineStack.top(); - - prof_start( &totalRealTime ); - nearest = node->NearestObstacle( currentLine, PNS_ITEM::ANY ); - prof_end( &totalRealTime ); - - TRACE( 2, "t-nearestObstacle %lld us", totalRealTime.usecs() ); - - if( !nearest ) - { - if( lineStack.size() > 1 ) - { - PNS_LINE* original = lineStack.top(); - PNS_LINE optimized; - int r_start, r_end; - - original->GetAffectedRange( r_start, r_end ); - - TRACE( 1, "Iter %d optimize-line [range %d-%d, total %d]", - iter % r_start % r_end % original->GetCLine().PointCount() ); - // lastWalkSolid = NULL; - prof_start( &totalRealTime ); - - if( optimizer.Optimize( original, &optimized ) ) - { - node->Remove( original ); - optimizer.CacheRemove( original ); - node->Add( &optimized ); - - if( original->BelongsTo( node ) ) - delete original; - } - - prof_end( &totalRealTime ); - - TRACE( 2, "t-optimizeObstacle %lld us", totalRealTime.usecs() ); - } - - lineStack.pop(); - } - else - { - switch( nearest->item->GetKind() ) - { - case PNS_ITEM::SEGMENT: - { - TRACE( 1, "Iter %d shove-line", iter ); - - PNS_SEGMENT* pseg = static_cast(nearest->item); - PNS_LINE* collidingLine = node->AssembleLine( pseg ); - PNS_LINE* shovedLine = collidingLine->CloneProperties(); - - prof_start( &totalRealTime ); - ShoveStatus st = shoveSingleLine( node, currentLine, collidingLine, - *pseg, shovedLine ); - prof_end( &totalRealTime ); - - TRACE( 2, "t-shoveSingle %lld us", totalRealTime.usecs() ); - - if( st == SH_OK ) - { - node->Replace( collidingLine, shovedLine ); - - if( collidingLine->BelongsTo( node ) ) - delete collidingLine; - - optimizer.CacheRemove( collidingLine ); - lineStack.push( shovedLine ); - } - else - fail = true; - - // lastWalkSolid = NULL; - - break; - } // case SEGMENT - - case PNS_ITEM::SOLID: - case PNS_ITEM::VIA: - { - TRACE( 1, "Iter %d walkaround-solid [%p]", iter % nearest->item ); - - if( lineStack.size() == 1 ) - { - fail = true; - break; - } - -/* if(lastWalkSolid == nearest->item) - * { - * fail = true; - * break; - * }*/ - - PNS_WALKAROUND walkaround( node ); - PNS_LINE* walkaroundLine = currentLine->CloneProperties(); - - walkaround.SetSolidsOnly( true ); - walkaround.SetSingleDirection( true ); - - prof_start( &totalRealTime ); - walkaround.Route( *currentLine, *walkaroundLine, false ); - prof_end( &totalRealTime ); - - TRACE( 2, "t-walkSolid %lld us", totalRealTime.usecs() ); - - - node->Replace( currentLine, walkaroundLine ); - - if( currentLine->BelongsTo( node ) ) - delete currentLine; - - optimizer.CacheRemove( currentLine ); - lineStack.top() = walkaroundLine; - - // lastWalkSolid = nearest->item; - break; - } - - default: - break; - } // switch - - if( fail ) - break; - } - } - - node->Remove( head ); - delete head; - - if( headVia ) + if( st == SH_OK || st == SH_HEAD_MODIFIED ) { - node->Remove( headVia ); - delete headVia; - } - - TRACE( 1, "Shove status : %s after %d iterations", (fail ? "FAILED" : "OK") % iter ); - - if( !fail ) - { - pushSpringback( node, aCurrentHead, PNS_COST_ESTIMATOR() ); - return SH_OK; + pushSpringback( m_currentNode, headSet, PNS_COST_ESTIMATOR() ); } else { - delete node; - return SH_INCOMPLETE; + delete m_currentNode; + + m_currentNode = parent; + m_newHead = OptLine(); } + + return st; +} + + +PNS_SHOVE::ShoveStatus PNS_SHOVE::ShoveDraggingVia ( PNS_VIA *aVia, const VECTOR2I& aWhere, PNS_VIA **aNewVia ) +{ + ShoveStatus st = SH_OK; + + m_lineStack.clear(); + m_optimizerQueue.clear(); + m_newHead = OptLine(); + m_draggedVia = NULL; + + //reduceSpringback( aCurrentHead ); + + PNS_NODE *parent = m_nodeStack.empty() ? m_root : m_nodeStack.back().node; + m_currentNode = parent->Branch(); + m_currentNode->ClearRanks(); + + aVia->Mark( MK_HEAD ); + + + st = pushVia ( aVia, (aWhere - aVia->Pos()), 0 ); + st = shoveMainLoop(); + runOptimizer ( m_currentNode, NULL ); + + if( st == SH_OK || st == SH_HEAD_MODIFIED ) + { + pushSpringback( m_currentNode, PNS_ITEMSET(), PNS_COST_ESTIMATOR() ); + } + else + { + delete m_currentNode; + m_currentNode = parent; + } + + if(aNewVia) + *aNewVia = m_draggedVia; + + return st; +} + +void PNS_SHOVE::runOptimizer ( PNS_NODE *node, PNS_LINE *head ) +{ + PNS_OPTIMIZER optimizer( node ); + int optFlags = 0, n_passes = 0, extend = 0; + + PNS_OPTIMIZATION_EFFORT effort = Settings().OptimizerEffort(); + + + switch(effort) + { + case OE_Low: + optFlags = PNS_OPTIMIZER::MERGE_OBTUSE; + n_passes = 1; + extend = 0; + break; + case OE_Medium: + optFlags = PNS_OPTIMIZER::MERGE_OBTUSE; + n_passes = 2; + extend = 1; + break; + case OE_Full: + optFlags = PNS_OPTIMIZER::MERGE_SEGMENTS; + n_passes = 2; + break; + default: + break; + } + + if(Settings().SmartPads()) + optFlags |= PNS_OPTIMIZER::SMART_PADS ; + + optimizer.SetEffortLevel( optFlags ); + optimizer.SetCollisionMask( PNS_ITEM::ANY ); + + for(int pass = 0; pass < n_passes; pass ++) + { + std::reverse ( m_optimizerQueue.begin(), m_optimizerQueue.end() ); + for(std::vector::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end(); ++i) + { + PNS_LINE *line = *i; + + if( ! (line -> Marker() & MK_HEAD ) ) + { + if(effort == OE_Medium || effort == OE_Low ) + { + RANGE r = findShovedVertexRange ( line ); + + if (r.Defined()) + { + int start_v = std::max(0, r.MinV() - extend); + int end_v = std::min(line->PointCount() - 1 , r.MaxV() + extend ); + line->ClipVertexRange ( start_v, end_v ); + } + } + + PNS_LINE optimized; + + if( optimizer.Optimize( line, &optimized ) ) + { + node->Remove( line ); + line->SetShape(optimized.CLine()); + node->Add( line ); + } + } + } + } + + +} + +const RANGE PNS_SHOVE::findShovedVertexRange ( PNS_LINE *l ) +{ + RANGE r; + + for(int i = 0; i < l->SegmentCount(); i++) + { + PNS_SEGMENT *s = (*l->LinkedSegments())[i]; + PNS_JOINT *jt = m_root->FindJoint( s->Seg().A, s->Layer(), s->Net() ); + bool found = false; + + if(jt) + { + BOOST_FOREACH( PNS_ITEM *item, jt->LinkList() ) + { + if(item->OfKind(PNS_ITEM::SEGMENT)) + { + PNS_SEGMENT *s_old = (PNS_SEGMENT *) item; + + if( s_old->Net() == s->Net() && + s_old->Layer() == s->Layer() && + s_old->Seg().A == s->Seg().A && + s_old->Seg().B == s->Seg().B ) + { + found = true; + break; + } + } + } + } + + if(!found) + { + r.Grow(i); + r.Grow(i + 1); + } + } + return r; +} + +PNS_NODE* PNS_SHOVE::CurrentNode() +{ + return m_nodeStack.empty() ? m_root : m_nodeStack.back().node; +} + +const PNS_LINE PNS_SHOVE::NewHead() const +{ + assert(m_newHead); + return *m_newHead; +} + +void PNS_SHOVE::SetInitialLine ( PNS_LINE *aInitial ) +{ + m_root = m_root->Branch(); + m_root->Remove ( aInitial ); } diff --git a/pcbnew/router/pns_shove.h b/pcbnew/router/pns_shove.h index 581d2f49f9..d070e89790 100644 --- a/pcbnew/router/pns_shove.h +++ b/pcbnew/router/pns_shove.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_SHOVE_H @@ -25,61 +25,114 @@ #include #include "pns_optimizer.h" +#include "pns_routing_settings.h" +#include "pns_algo_base.h" +#include "pns_logger.h" +#include "range.h" class PNS_LINE; class PNS_NODE; class PNS_ROUTER; -class PNS_SHOVE +/** + * Class PNS_SHOVE + * + * The actual Push and Shove algorithm. + */ + +class PNS_SHOVE : public PNS_ALGO_BASE { public: - PNS_SHOVE( PNS_NODE* aWorld ); - ~PNS_SHOVE(); enum ShoveStatus { SH_OK = 0, SH_NULL, - SH_INCOMPLETE + SH_INCOMPLETE, + SH_HEAD_MODIFIED }; - ShoveStatus ShoveLines( PNS_LINE* aCurrentHead ); + PNS_SHOVE( PNS_NODE* aWorld, PNS_ROUTER *aRouter ); + ~PNS_SHOVE(); - PNS_NODE* GetCurrentNode() + virtual PNS_LOGGER *Logger() { - return m_nodeStack.empty() ? m_root : m_nodeStack.back().node; + return &m_logger; } - const PNS_COST_ESTIMATOR TotalCost() const; + ShoveStatus ShoveLines( const PNS_LINE& aCurrentHead ); + ShoveStatus ShoveDraggingVia ( PNS_VIA *aVia, const VECTOR2I& aWhere, PNS_VIA **aNewVia ); - void Reset(); - void KillChildNodes(); + PNS_NODE* CurrentNode(); + + const PNS_LINE NewHead() const; + + void SetInitialLine ( PNS_LINE *aInitial ); private: - static const int ShoveTimeLimit = 3000; - - bool tryShove( PNS_NODE* aWorld, PNS_LINE* aTrack, PNS_LINE* aObstacle, - PNS_SEGMENT& aObstacleSeg, PNS_LINE* aResult, bool aInvertWinding ); - - ShoveStatus shoveSingleLine( PNS_NODE* aNode, PNS_LINE* aCurrent, PNS_LINE* aObstacle, - PNS_SEGMENT& aObstacleSeg, PNS_LINE* aResult ); - - bool reduceSpringback( PNS_LINE* aHead ); - bool pushSpringback( PNS_NODE* aNode, PNS_LINE* aHead, const PNS_COST_ESTIMATOR& aCost ); + typedef std::vector HullSet; + typedef boost::optional OptLine; + typedef std::pair LinePair; + typedef std::vector LinePairVec; struct SpringbackTag { int64_t length; int segments; VECTOR2I p; - PNS_NODE* node; + PNS_NODE *node; + PNS_ITEMSET headItems; PNS_COST_ESTIMATOR cost; }; - std::vector m_nodeStack; - PNS_NODE* m_root; - PNS_NODE* m_currentNode; - int m_iterLimit; + ShoveStatus processSingleLine(PNS_LINE *aCurrent, PNS_LINE* aObstacle, PNS_LINE *aShoved ); + ShoveStatus processHullSet ( PNS_LINE *aCurrent, PNS_LINE *aObstacle, PNS_LINE *aShoved, const HullSet& hulls ); + + bool reduceSpringback( const PNS_ITEMSET &aHeadItems ); + bool pushSpringback( PNS_NODE* aNode, const PNS_ITEMSET &aHeadItems, const PNS_COST_ESTIMATOR& aCost ); + + ShoveStatus walkaroundLoneVia ( PNS_LINE *aCurrent, PNS_LINE *aObstacle, PNS_LINE *aShoved ); + bool checkBumpDirection ( PNS_LINE *aCurrent, PNS_LINE *aShoved ) const; + + ShoveStatus onCollidingLine( PNS_LINE *aCurrent, PNS_LINE *aObstacle ); + ShoveStatus onCollidingSegment( PNS_LINE *aCurrent, PNS_SEGMENT *aObstacleSeg ); + ShoveStatus onCollidingSolid( PNS_LINE *aCurrent, PNS_SOLID *aObstacleSolid ); + ShoveStatus onCollidingVia( PNS_ITEM *aCurrent, PNS_VIA *aObstacleVia ); + ShoveStatus onReverseCollidingVia( PNS_LINE *aCurrent, PNS_VIA *aObstacleVia ); + ShoveStatus pushVia ( PNS_VIA *aVia, const VECTOR2I& aForce, int aCurrentRank ); + + void unwindStack ( PNS_SEGMENT *seg ); + void unwindStack ( PNS_ITEM *item ); + + void runOptimizer ( PNS_NODE *node, PNS_LINE *head ); + + void pushLine ( PNS_LINE *l ); + void popLine(); + + const RANGE findShovedVertexRange ( PNS_LINE *l ); + + PNS_LINE *assembleLine ( const PNS_SEGMENT *aSeg, int *aIndex = NULL ); + PNS_LINE *cloneLine ( const PNS_LINE *aLine ); + + ShoveStatus shoveIteration(int aIter); + ShoveStatus shoveMainLoop(); + + std::vector m_nodeStack; + std::vector m_lineStack; + std::vector m_optimizerQueue; + std::vector m_gcItems; + + PNS_NODE* m_root; + PNS_NODE* m_currentNode; + PNS_LINE* m_currentHead; + PNS_LINE* m_collidingLine; + + OptLine m_newHead; + + PNS_LOGGER m_logger; + PNS_VIA* m_draggedVia; + + int m_iter; }; -#endif +#endif // __PNS_SHOVE_H diff --git a/pcbnew/router/pns_solid.cpp b/pcbnew/router/pns_solid.cpp index 807f9faa0b..da4fab064d 100644 --- a/pcbnew/router/pns_solid.cpp +++ b/pcbnew/router/pns_solid.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include @@ -30,13 +30,15 @@ const SHAPE_LINE_CHAIN PNS_SOLID::Hull( int aClearance, int aWalkaroundThickness ) const { + int cl = aClearance + aWalkaroundThickness / 2; + switch( m_shape->Type() ) { case SH_RECT: { SHAPE_RECT* rect = static_cast( m_shape ); return OctagonalHull( rect->GetPosition(), rect->GetSize(), - aClearance + 1, 0.2 * aClearance ); + cl + 1, 0.2 * cl ); } case SH_CIRCLE: @@ -44,7 +46,12 @@ const SHAPE_LINE_CHAIN PNS_SOLID::Hull( int aClearance, int aWalkaroundThickness SHAPE_CIRCLE* circle = static_cast( m_shape ); int r = circle->GetRadius(); return OctagonalHull( circle->GetCenter() - VECTOR2I( r, r ), VECTOR2I( 2 * r, 2 * r ), - aClearance + 1, 0.52 * (r + aClearance) ); + cl + 1, 0.52 * (r + cl) ); + } + case SH_SEGMENT: + { + SHAPE_SEGMENT *seg = static_cast ( m_shape ); + return SegmentHull (*seg, aClearance, aWalkaroundThickness ); } default: @@ -55,10 +62,8 @@ const SHAPE_LINE_CHAIN PNS_SOLID::Hull( int aClearance, int aWalkaroundThickness } -PNS_ITEM* PNS_SOLID::Clone() const +PNS_ITEM* PNS_SOLID::Clone ( ) const { - // solids are never cloned as the shove algorithm never moves them - assert( false ); - - return NULL; + PNS_ITEM *solid = new PNS_SOLID ( *this ); + return solid; } diff --git a/pcbnew/router/pns_solid.h b/pcbnew/router/pns_solid.h index b6f38a2851..d1e725a461 100644 --- a/pcbnew/router/pns_solid.h +++ b/pcbnew/router/pns_solid.h @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_SOLID_H @@ -42,9 +42,16 @@ public: delete m_shape; } - PNS_ITEM* Clone() const; + PNS_SOLID( const PNS_SOLID& aSolid ) : + PNS_ITEM ( aSolid ) + { + m_shape = aSolid.m_shape->Clone(); + m_pos = aSolid.m_pos; + } + + PNS_ITEM* Clone( ) const; - const SHAPE* GetShape() const { return m_shape; } + const SHAPE* Shape() const { return m_shape; } const SHAPE_LINE_CHAIN Hull( int aClearance = 0, int aWalkaroundThickness = 0 ) const; @@ -56,18 +63,29 @@ public: m_shape = shape; } - const VECTOR2I& GetCenter() const + const VECTOR2I& Pos() const { - return m_center; + return m_pos; } - void SetCenter( const VECTOR2I& aCenter ) + void SetPos( const VECTOR2I& aCenter ) { - m_center = aCenter; + m_pos = aCenter; } + virtual VECTOR2I Anchor(int n) const + { + return m_pos; + } + + virtual int AnchorCount() const + { + return 1; + } + + private: - VECTOR2I m_center; + VECTOR2I m_pos; SHAPE* m_shape; }; diff --git a/pcbnew/router/pns_utils.cpp b/pcbnew/router/pns_utils.cpp index c787753534..e1042e6437 100644 --- a/pcbnew/router/pns_utils.cpp +++ b/pcbnew/router/pns_utils.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,13 +15,15 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include "pns_utils.h" #include "pns_line.h" #include "pns_router.h" +#include + const SHAPE_LINE_CHAIN OctagonalHull( const VECTOR2I& aP0, const VECTOR2I& aSize, int aClearance, @@ -42,3 +44,51 @@ const SHAPE_LINE_CHAIN OctagonalHull( const VECTOR2I& aP0, return s; } + +const SHAPE_LINE_CHAIN SegmentHull ( const SHAPE_SEGMENT& aSeg, + int aClearance, + int aWalkaroundThickness ) +{ + int d = aSeg.GetWidth() / 2 + aClearance + aWalkaroundThickness / 2 + HULL_MARGIN; + int x = (int)( 2.0 / ( 1.0 + M_SQRT2 ) * d ); + + const VECTOR2I a = aSeg.GetSeg().A; + const VECTOR2I b = aSeg.GetSeg().B; + + VECTOR2I dir = b - a; + VECTOR2I p0 = dir.Perpendicular().Resize( d ); + VECTOR2I ds = dir.Perpendicular().Resize( x / 2 ); + VECTOR2I pd = dir.Resize( x / 2 ); + VECTOR2I dp = dir.Resize( d ); + + SHAPE_LINE_CHAIN s; + + s.SetClosed( true ); + + s.Append( b + p0 + pd ); + s.Append( b + dp + ds ); + s.Append( b + dp - ds ); + s.Append( b - p0 + pd ); + s.Append( a - p0 - pd ); + s.Append( a - dp - ds ); + s.Append( a - dp + ds ); + s.Append( a + p0 - pd ); + + // make sure the hull outline is always clockwise + if( s.CSegment( 0 ).Side( a ) < 0 ) + return s.Reverse(); + else + return s; +} + +SHAPE_RECT ApproximateSegmentAsRect( const SHAPE_SEGMENT& aSeg ) +{ + SHAPE_RECT r; + + VECTOR2I delta ( aSeg.GetWidth() / 2, aSeg.GetWidth() / 2 ); + VECTOR2I p0 ( aSeg.GetSeg().A - delta ); + VECTOR2I p1 ( aSeg.GetSeg().B + delta ); + + return SHAPE_RECT ( std::min(p0.x, p1.x), std::min(p0.y, p1.y), + std::abs(p1.x - p0.x), std::abs(p1.y - p0.y )); +} \ No newline at end of file diff --git a/pcbnew/router/pns_utils.h b/pcbnew/router/pns_utils.h index 655720f1f1..50bfc90179 100644 --- a/pcbnew/router/pns_utils.h +++ b/pcbnew/router/pns_utils.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_UTILS_H @@ -23,10 +23,20 @@ #include #include +#include +#include + +#define HULL_MARGIN 10 /** Various utility functions */ const SHAPE_LINE_CHAIN OctagonalHull( const VECTOR2I& aP0, const VECTOR2I& aSize, int aClearance, int aChamfer ); +const SHAPE_LINE_CHAIN SegmentHull ( const SHAPE_SEGMENT& aSeg, + int aClearance, + int aWalkaroundThickness ); + +SHAPE_RECT ApproximateSegmentAsRect( const SHAPE_SEGMENT& aSeg ); + #endif // __PNS_UTILS_H diff --git a/pcbnew/router/pns_via.cpp b/pcbnew/router/pns_via.cpp index 30666d78ab..037bb16e99 100644 --- a/pcbnew/router/pns_via.cpp +++ b/pcbnew/router/pns_via.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,98 +15,16 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include "pns_via.h" #include "pns_node.h" #include "pns_utils.h" +#include "pns_router.h" #include -static bool Circle2Circle( VECTOR2I p1, VECTOR2I p2, int r1, int r2, VECTOR2I& force ) -{ - int mindist = r1 + r2; - VECTOR2I delta = p2 - p1; - int dist = delta.EuclideanNorm(); - - if( dist >= mindist ) - return false; - - force = delta.Resize( abs( mindist - dist ) + 1 ); - return true; -}; - -static bool Rect2Circle( VECTOR2I rp0, VECTOR2I rsize, VECTOR2I cc, int cr, VECTOR2I& force ) -{ - VECTOR2I vts[] = - { - VECTOR2I( rp0.x, rp0.y ), - VECTOR2I( rp0.x, rp0.y + rsize.y ), - VECTOR2I( rp0.x + rsize.x, rp0.y + rsize.y ), - VECTOR2I( rp0.x + rsize.x, rp0.y ), - VECTOR2I( rp0.x, rp0.y ) - }; - - int dist = INT_MAX; - VECTOR2I nearest; - - for( int i = 0; i < 4; i++ ) - { - SEG s( vts[i], vts[i + 1] ); - - VECTOR2I pn = s.NearestPoint( cc ); - - int d = (pn - cc).EuclideanNorm(); - - if( d < dist ) - { - nearest = pn; - dist = d; - } - } - - bool inside = cc.x >= rp0.x && cc.x <= (rp0.x + rsize.x) - && cc.y >= rp0.y && cc.y <= (rp0.y + rsize.y); - - VECTOR2I delta = cc - nearest; - - if( dist >= cr && !inside ) - return false; - - if( inside ) - force = -delta.Resize( abs( cr + dist ) + 1 ); - else - force = delta.Resize( abs( cr - dist ) + 1 ); - - return true; -}; - - -static bool ShPushoutForce( const SHAPE* shape, VECTOR2I p, int r, VECTOR2I& force, int clearance ) -{ - switch( shape->Type() ) - { - case SH_CIRCLE: - { - const SHAPE_CIRCLE* cir = static_cast(shape); - return Circle2Circle( cir->GetCenter(), p, cir->GetRadius(), r + clearance + 1, force ); - } - - case SH_RECT: - { - const SHAPE_RECT* rect = static_cast(shape); - return Rect2Circle( rect->GetPosition(), rect->GetSize(), p, r + clearance + 1, force ); - } - - default: - return false; - } - - return false; -} - - bool PNS_VIA::PushoutForce( PNS_NODE* aNode, const VECTOR2I& aDirection, VECTOR2I& aForce, @@ -115,30 +33,31 @@ bool PNS_VIA::PushoutForce( PNS_NODE* aNode, { int iter = 0; PNS_VIA mv( *this ); - VECTOR2I force, totalForce; + VECTOR2I force, totalForce, force2; + while( iter < aMaxIterations ) { - PNS_NODE::OptObstacle obs = aNode->CheckColliding( &mv, - aSolidsOnly ? PNS_ITEM::SOLID : PNS_ITEM::ANY ); + + PNS_NODE::OptObstacle obs = aNode->CheckColliding( &mv, aSolidsOnly ? PNS_ITEM::SOLID : PNS_ITEM::ANY ); if( !obs ) break; int clearance = aNode->GetClearance( obs->item, &mv ); - if( iter > 10 ) + if( iter > aMaxIterations / 2 ) { - VECTOR2I l = -aDirection.Resize( m_diameter / 4 ); + VECTOR2I l = aDirection.Resize( m_diameter / 2 ); totalForce += l; - mv.SetPos( mv.GetPos() + l ); + mv.SetPos( mv.Pos() + l ); } - if( ShPushoutForce( obs->item->GetShape(), mv.GetPos(), mv.GetDiameter() / 2, force, - clearance ) ) - { - totalForce += force; - mv.SetPos( mv.GetPos() + force ); + bool col = CollideShapes( obs->item->Shape(), mv.Shape(), clearance, true, force2 ); + + if(col) { + totalForce += force2; + mv.SetPos( mv.Pos() + force2 ); } @@ -155,7 +74,26 @@ bool PNS_VIA::PushoutForce( PNS_NODE* aNode, const SHAPE_LINE_CHAIN PNS_VIA::Hull( int aClearance, int aWalkaroundThickness ) const { + int cl = (aClearance + aWalkaroundThickness / 2); + return OctagonalHull( m_pos - VECTOR2I( m_diameter / 2, m_diameter / 2 ), VECTOR2I( m_diameter, - m_diameter ), aClearance + 1, (2 * aClearance + m_diameter) * 0.26 ); + m_diameter ), cl + 1, (2 * cl + m_diameter) * 0.26 ); +} + +PNS_VIA* PNS_VIA::Clone ( ) const +{ + PNS_VIA* v = new PNS_VIA(); + + v->SetNet( Net() ); + v->SetLayers( Layers() ); + v->m_pos = m_pos; + v->m_diameter = m_diameter; + v->m_drill = m_drill; + v->m_owner = NULL; + v->m_shape = SHAPE_CIRCLE( m_pos, m_diameter / 2 ); + v->m_rank = m_rank; + v->m_marker = m_marker; + + return v; } diff --git a/pcbnew/router/pns_via.h b/pcbnew/router/pns_via.h index 1a0fc2a2bd..684116b6da 100644 --- a/pcbnew/router/pns_via.h +++ b/pcbnew/router/pns_via.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_VIA_H @@ -45,16 +45,21 @@ public: }; - PNS_VIA( const PNS_VIA& b ) : PNS_ITEM( VIA ) + PNS_VIA( const PNS_VIA& b ) : + PNS_ITEM( VIA ) { - SetNet( b.GetNet() ); - SetLayers( b.GetLayers() ); + SetNet( b.Net() ); + SetLayers( b.Layers() ); m_pos = b.m_pos; m_diameter = b.m_diameter; m_shape = SHAPE_CIRCLE( m_pos, m_diameter / 2 ); + m_marker = b.m_marker; + m_rank = b.m_rank; + m_owner = b.m_owner; + m_drill = b.m_drill; } - const VECTOR2I& GetPos() const + const VECTOR2I& Pos() const { return m_pos; } @@ -65,7 +70,7 @@ public: m_shape.SetCenter( aPos ); } - int GetDiameter() const + int Diameter() const { return m_diameter; } @@ -76,7 +81,7 @@ public: m_shape.SetRadius( m_diameter / 2 ); } - int GetDrill() const + int Drill() const { return m_drill; } @@ -92,25 +97,24 @@ public: bool aSolidsOnly = true, int aMaxIterations = 10 ); - const SHAPE* GetShape() const + const SHAPE* Shape() const { return &m_shape; } - PNS_VIA* Clone() const - { - PNS_VIA* v = new PNS_VIA(); - - v->SetNet( GetNet() ); - v->SetLayers( GetLayers() ); - v->m_pos = m_pos; - v->m_diameter = m_diameter; - v->m_shape = SHAPE_CIRCLE( m_pos, m_diameter / 2 ); - - return v; - } + PNS_VIA* Clone ( ) const; const SHAPE_LINE_CHAIN Hull( int aClearance = 0, int aWalkaroundThickness = 0 ) const; + + virtual VECTOR2I Anchor(int n) const + { + return m_pos; + } + + virtual int AnchorCount() const + { + return 1; + } private: diff --git a/pcbnew/router/pns_walkaround.cpp b/pcbnew/router/pns_walkaround.cpp index 185def64d2..5be937e489 100644 --- a/pcbnew/router/pns_walkaround.cpp +++ b/pcbnew/router/pns_walkaround.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include @@ -38,8 +38,8 @@ void PNS_WALKAROUND::start( const PNS_LINE& aInitialPath ) PNS_NODE::OptObstacle PNS_WALKAROUND::nearestObstacle( const PNS_LINE& aPath ) { - return m_world->NearestObstacle( &aPath, - m_solids_only ? (PNS_ITEM::SOLID | PNS_ITEM::VIA) : PNS_ITEM::ANY ); + return m_world->NearestObstacle( &aPath, m_item_mask); + } @@ -55,33 +55,42 @@ PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::singleStep( PNS_LINE& aPath, SHAPE_LINE_CHAIN path_pre[2], path_walk[2], path_post[2]; - VECTOR2I last = aPath.GetCLine().CPoint( -1 ); + VECTOR2I last = aPath.CPoint( -1 );; + if( ( current_obs->hull ).PointInside( last ) ) { m_recursiveBlockageCount++; if( m_recursiveBlockageCount < 3 ) - aPath.GetLine().Append( current_obs->hull.NearestPoint( last ) ); + aPath.Line().Append( current_obs->hull.NearestPoint( last ) ); else { aPath = aPath.ClipToNearestObstacle( m_world ); - return STUCK; + return DONE; } } - aPath.NewWalkaround( current_obs->hull, path_pre[0], path_walk[0], + aPath.Walkaround( current_obs->hull, path_pre[0], path_walk[0], path_post[0], aWindingDirection ); - aPath.NewWalkaround( current_obs->hull, path_pre[1], path_walk[1], + aPath.Walkaround( current_obs->hull, path_pre[1], path_walk[1], path_post[1], !aWindingDirection ); - +#ifdef DEBUG + m_logger.NewGroup (aWindingDirection ? "walk-cw" : "walk-ccw", m_iteration); + m_logger.Log ( &path_walk[0], 0, "path-walk"); + m_logger.Log ( &path_pre[0], 1, "path-pre"); + m_logger.Log ( &path_post[0], 4, "path-post"); + m_logger.Log ( ¤t_obs->hull, 2, "hull"); + m_logger.Log ( current_obs->item, 3, "item"); +#endif + int len_pre = path_walk[0].Length(); int len_alt = path_walk[1].Length(); - + PNS_LINE walk_path( aPath, path_walk[1] ); - bool alt_collides = m_world->CheckColliding( &walk_path, - m_solids_only ? PNS_ITEM::SOLID : PNS_ITEM::ANY ); + bool alt_collides = m_world->CheckColliding( &walk_path, m_item_mask ); + SHAPE_LINE_CHAIN pnew; @@ -126,6 +135,7 @@ PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::Route( const PNS_LINE& aInitial WalkaroundStatus s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS; SHAPE_LINE_CHAIN best_path; + start( aInitialPath ); m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath ); @@ -143,9 +153,10 @@ PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::Route( const PNS_LINE& aInitial if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) ) { - int len_cw = path_cw.GetCLine().Length(); - int len_ccw = path_ccw.GetCLine().Length(); + int len_cw = path_cw.CLine().Length(); + int len_ccw = path_ccw.CLine().Length(); + if( m_forceLongerPath ) aWalkPath = (len_cw > len_ccw ? path_cw : path_ccw); else @@ -166,11 +177,11 @@ PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::Route( const PNS_LINE& aInitial m_iteration++; } - + if( m_iteration == m_iteration_limit ) { - int len_cw = path_cw.GetCLine().Length(); - int len_ccw = path_ccw.GetCLine().Length(); + int len_cw = path_cw.CLine().Length(); + int len_ccw = path_ccw.CLine().Length(); if( m_forceLongerPath ) @@ -185,7 +196,7 @@ PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::Route( const PNS_LINE& aInitial // int len_ccw = path_ccw.GetCLine().Length(); bool found = false; - SHAPE_LINE_CHAIN l = aWalkPath.GetCLine(); + SHAPE_LINE_CHAIN l = aWalkPath.CLine(); for( int i = 0; i < l.SegmentCount(); i++ ) { @@ -198,7 +209,6 @@ PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::Route( const PNS_LINE& aInitial if( dist_n <= dist_a && dist_n < dist_b ) { - // PNSDisplayDebugLine( l, 3 ); l.Remove( i + 1, -1 ); l.Append( nearest ); l.Simplify(); @@ -214,13 +224,21 @@ PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::Route( const PNS_LINE& aInitial } } - aWalkPath.SetWorld( m_world ); - aWalkPath.GetLine().Simplify(); + aWalkPath.Line().Simplify(); + if(aWalkPath.SegmentCount() < 1) + return STUCK; + + if(aWalkPath.CPoint(-1) != aInitialPath.CPoint(-1)) + return STUCK; + + if(aWalkPath.CPoint(0) != aInitialPath.CPoint(0)) + return STUCK; + WalkaroundStatus st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK; if( aOptimize && st == DONE ) - PNS_OPTIMIZER::Optimize( &aWalkPath, PNS_OPTIMIZER::MERGE_OBTUSE, m_world ); + PNS_OPTIMIZER::Optimize( &aWalkPath, PNS_OPTIMIZER::MERGE_OBTUSE, m_world ); return st; } diff --git a/pcbnew/router/pns_walkaround.h b/pcbnew/router/pns_walkaround.h index 609b80d2ce..0a87213a22 100644 --- a/pcbnew/router/pns_walkaround.h +++ b/pcbnew/router/pns_walkaround.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __PNS_WALKAROUND_H @@ -23,19 +23,26 @@ #include "pns_line.h" #include "pns_node.h" +#include "pns_router.h" +#include "pns_logger.h" +#include "pns_algo_base.h" -class PNS_WALKAROUND +class PNS_WALKAROUND : public PNS_ALGO_BASE { static const int DefaultIterationLimit = 50; public: - PNS_WALKAROUND( PNS_NODE* aWorld ) : - m_world( aWorld ), m_iteration_limit( DefaultIterationLimit ) + PNS_WALKAROUND( PNS_NODE* aWorld, PNS_ROUTER *aRouter ) : + PNS_ALGO_BASE ( aRouter ), + m_world( aWorld ), + m_iteration_limit( DefaultIterationLimit ) { m_forceSingleDirection = false; m_forceLongerPath = false; m_cursorApproachMode = false; + m_item_mask = PNS_ITEM::ANY; }; + ~PNS_WALKAROUND() {}; enum WalkaroundStatus @@ -57,13 +64,22 @@ public: void SetSolidsOnly( bool aSolidsOnly ) { - m_solids_only = aSolidsOnly; + if(aSolidsOnly) + m_item_mask = PNS_ITEM::SOLID; + else + m_item_mask = PNS_ITEM::ANY; + } + + void SetItemMask ( int aMask ) + { + m_item_mask = aMask; } void SetSingleDirection( bool aForceSingleDirection ) { m_forceSingleDirection = aForceSingleDirection; - m_forceLongerPath = true; + m_forceLongerPath = aForceSingleDirection; + //printf("FSD %d FPD %d\n", m_forceSingleDirection?1:0, m_forceLongerPath ? 1: 0); } void SetApproachCursor( bool aEnabled, const VECTOR2I& aPos ) @@ -75,6 +91,10 @@ public: WalkaroundStatus Route( const PNS_LINE& aInitialPath, PNS_LINE& aWalkPath, bool aOptimize = true ); + virtual PNS_LOGGER *Logger() { + return &m_logger; + } + private: void start( const PNS_LINE& aInitialPath ); @@ -86,12 +106,13 @@ private: int m_recursiveBlockageCount; int m_iteration; int m_iteration_limit; - bool m_solids_only; + int m_item_mask; bool m_forceSingleDirection, m_forceLongerPath; bool m_cursorApproachMode; VECTOR2I m_cursorPos; PNS_NODE::OptObstacle m_currentObstacle[2]; bool m_recursiveCollision[2]; + PNS_LOGGER m_logger; }; #endif // __PNS_WALKAROUND_H diff --git a/pcbnew/router/range.h b/pcbnew/router/range.h new file mode 100644 index 0000000000..8409731926 --- /dev/null +++ b/pcbnew/router/range.h @@ -0,0 +1,91 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __RANGE_H +#define __RANGE_H + +template class RANGE { + + public: + RANGE (T aMin, T aMax) : + m_min(aMin), + m_max(aMax), + m_defined(true) {} + + RANGE (): + m_defined (false) {}; + + T MinV() const + { + return m_min; + } + + T MaxV() const + { + return m_max; + } + + void Set ( T aMin, T aMax ) const + { + m_max = aMax; + m_min = aMin; + } + + void Grow ( T value ) + { + if(!m_defined) + { + m_min = value; + m_max = value; + m_defined = true; + } else { + m_min = std::min(m_min, value); + m_max = std::max(m_max, value); + } + } + + bool Inside ( const T& value ) const + { + if(!m_defined) + return true; + + return value >= m_min && value <= m_max; + } + + bool Overlaps ( const RANGE &aOther ) const + { + if(!m_defined || !aOther.m_defined) + return true; + + return m_max >= aOther.m_min && m_min <= aOther.m_max; + } + + bool Defined() const + { + return m_defined; + } + + private: + T m_min, m_max; + bool m_defined; + +}; + +#endif \ No newline at end of file diff --git a/pcbnew/router/router_preview_item.cpp b/pcbnew/router/router_preview_item.cpp index 8ce1db4721..3287082c3c 100644 --- a/pcbnew/router/router_preview_item.cpp +++ b/pcbnew/router/router_preview_item.cpp @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,13 +15,16 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #include +#include + #include "class_track.h" #include +#include #include "router_preview_item.h" @@ -34,9 +37,12 @@ using namespace KIGFX; ROUTER_PREVIEW_ITEM::ROUTER_PREVIEW_ITEM( const PNS_ITEM* aItem, VIEW_GROUP* aParent ) : EDA_ITEM( NOT_USED ) { - m_Flags = 0; m_parent = aParent; - m_layer = DRAW_N; + + m_shape = NULL; + m_clearance = -1; + m_originLayer = m_layer = ITEM_GAL_LAYER ( GP_OVERLAY ); + if( aItem ) Update( aItem ); @@ -50,62 +56,73 @@ ROUTER_PREVIEW_ITEM::~ROUTER_PREVIEW_ITEM() void ROUTER_PREVIEW_ITEM::Update( const PNS_ITEM* aItem ) { - m_layer = aItem->GetLayers().Start(); - m_color = getLayerColor( m_layer ); + m_originLayer = aItem->Layers().Start(); + + assert (m_originLayer >= 0); + + m_layer = m_originLayer; + m_color = getLayerColor( m_originLayer ); m_color.a = 0.8; - - switch( aItem->GetKind() ) + m_depth = BaseOverlayDepth - aItem->Layers().Start(); + + m_shape = aItem->Shape()->Clone(); + + switch( aItem->Kind() ) { - case PNS_ITEM::LINE: - m_type = PR_LINE; - m_width = static_cast(aItem)->GetWidth(); - m_line = *static_cast( aItem->GetShape() ); - break; + case PNS_ITEM::LINE: + m_type = PR_SHAPE; + m_width = ((PNS_LINE *) aItem)->Width(); + + break; - case PNS_ITEM::SEGMENT: - m_type = PR_LINE; - m_width = static_cast(aItem)->GetWidth(); - m_line = *static_cast( aItem->GetShape() ); - break; + case PNS_ITEM::SEGMENT: + { + PNS_SEGMENT *seg = (PNS_SEGMENT *)aItem; + m_type = PR_SHAPE; + m_width = seg->Width(); + break; + } - case PNS_ITEM::VIA: - m_type = PR_VIA; - m_color = COLOR4D( 0.7, 0.7, 0.7, 0.8 ); - m_width = static_cast(aItem)->GetDiameter(); - m_viaCenter = static_cast(aItem)->GetPos(); - break; + case PNS_ITEM::VIA: + m_type = PR_SHAPE; + m_width = 0; + m_color = COLOR4D( 0.7, 0.7, 0.7, 0.8 ); + m_depth = ViaOverlayDepth; + break; + + case PNS_ITEM::SOLID: + m_type = PR_SHAPE; + m_width = 0; + break; default: break; } + if(aItem->Marker() & MK_VIOLATION) + m_color = COLOR4D (0, 1, 0, 1); + + if(aItem->Marker() & MK_HEAD) + m_color.Brighten(0.7); + ViewSetVisible( true ); ViewUpdate( GEOMETRY | APPEARANCE ); } - -void ROUTER_PREVIEW_ITEM::MarkAsHead() -{ - if( m_type != PR_VIA ) - m_color.Saturate( 1.0 ); -} - - const BOX2I ROUTER_PREVIEW_ITEM::ViewBBox() const { BOX2I bbox; switch( m_type ) { - case PR_LINE: - bbox = m_line.BBox(); + case PR_SHAPE: + bbox = m_shape->BBox(); bbox.Inflate( m_width / 2 ); return bbox; - case PR_VIA: - bbox = BOX2I( m_viaCenter, VECTOR2I( 0, 0 ) ); - bbox.Inflate( m_width / 2 ); + case PR_POINT: + bbox = BOX2I ( m_pos - VECTOR2I(100000, 100000), VECTOR2I( 200000, 200000 )); return bbox; default: @@ -115,76 +132,124 @@ const BOX2I ROUTER_PREVIEW_ITEM::ViewBBox() const return bbox; } +void ROUTER_PREVIEW_ITEM::drawLineChain( const SHAPE_LINE_CHAIN &l, KIGFX::GAL* aGal ) const +{ + for( int s = 0; s < l.SegmentCount(); s++ ) + aGal->DrawLine( l.CSegment( s ).A, l.CSegment( s ).B ); + if( l.IsClosed() ) + aGal->DrawLine( l.CSegment( -1 ).B, l.CSegment( 0 ).A ); +} void ROUTER_PREVIEW_ITEM::ViewDraw( int aLayer, KIGFX::GAL* aGal ) const { - switch( m_type ) + //col.Brighten(0.7); + aGal->SetLayerDepth( m_depth ); + + if(m_type == PR_SHAPE) { - case PR_LINE: - aGal->SetLayerDepth( -100.0 ); aGal->SetLineWidth( m_width ); aGal->SetStrokeColor( m_color ); - aGal->SetIsStroke( true ); - aGal->SetIsFill( false ); - - for( int s = 0; s < m_line.SegmentCount(); s++ ) - aGal->DrawLine( m_line.CSegment( s ).A, m_line.CSegment( s ).B ); - - if( m_line.IsClosed() ) - aGal->DrawLine( m_line.CSegment( -1 ).B, m_line.CSegment( 0 ).A ); - break; - - case PR_VIA: - aGal->SetLayerDepth( -101.0 ); - aGal->SetIsStroke( false ); - aGal->SetIsFill( true ); aGal->SetFillColor( m_color ); - aGal->DrawCircle( m_viaCenter, m_width / 2 ); - break; + aGal->SetIsStroke( m_width ? true : false ); + aGal->SetIsFill( true ); - default: - break; + + if(!m_shape) + return; + + switch( m_shape->Type() ) + { + case SH_LINE_CHAIN: + { + const SHAPE_LINE_CHAIN *l = (const SHAPE_LINE_CHAIN *) m_shape; + drawLineChain(*l, aGal); + break; + } + + case SH_SEGMENT: + { + const SHAPE_SEGMENT *s = (const SHAPE_SEGMENT *) m_shape; + aGal->DrawLine( s->GetSeg().A, s->GetSeg().B ); + + if(m_clearance > 0) + { + aGal->SetLayerDepth ( ClearanceOverlayDepth ); + aGal->SetStrokeColor ( COLOR4D( DARKDARKGRAY )); + aGal->SetLineWidth( m_width + 2 * m_clearance ); + aGal->DrawLine( s->GetSeg().A, s->GetSeg().B ); + + } + + break; + } + + case SH_CIRCLE: + { + const SHAPE_CIRCLE *c = (const SHAPE_CIRCLE *) m_shape; + aGal->DrawCircle( c->GetCenter(), c->GetRadius() ); + + if(m_clearance > 0) + { + aGal->SetLayerDepth ( ClearanceOverlayDepth ); + aGal->SetFillColor ( COLOR4D( DARKDARKGRAY )); + aGal->SetIsStroke( false ); + aGal->DrawCircle( c->GetCenter(), c->GetRadius() + m_clearance ); + } + + break; + } + + case SH_RECT: + { + const SHAPE_RECT *r = (const SHAPE_RECT *) m_shape; + aGal->DrawRectangle (r->GetPosition(), r->GetPosition() + r->GetSize()); + + if(m_clearance > 0) + { + aGal->SetLayerDepth ( ClearanceOverlayDepth ); + VECTOR2I p0 (r -> GetPosition() ), s ( r->GetSize() ); + aGal->SetStrokeColor ( COLOR4D( DARKDARKGRAY )); + aGal->SetIsStroke( true ); + aGal->SetLineWidth ( 2 * m_clearance ); + aGal->DrawLine( p0, VECTOR2I(p0.x + s.x, p0.y) ); + aGal->DrawLine( p0, VECTOR2I(p0.x, p0.y + s.y) ); + aGal->DrawLine( p0 + s , VECTOR2I(p0.x + s.x, p0.y) ); + aGal->DrawLine( p0 + s, VECTOR2I(p0.x, p0.y + s.y) ); + } + + break; + } + } } } -void ROUTER_PREVIEW_ITEM::DebugLine( const SHAPE_LINE_CHAIN& aLine, int aWidth, int aStyle ) +void ROUTER_PREVIEW_ITEM::Line( const SHAPE_LINE_CHAIN& aLine, int aWidth, int aStyle ) { -#if 0 - m_line = aLine; + + m_originLayer = m_layer = 0; m_width = aWidth; m_color = assignColor( aStyle ); + m_type = PR_SHAPE; + m_depth = -2047; + m_shape = aLine.Clone(); - - m_type = PR_LINE; + ViewSetVisible(true); ViewUpdate( GEOMETRY | APPEARANCE ); -#endif + } - -void ROUTER_PREVIEW_ITEM::DebugBox( const BOX2I& aBox, int aStyle ) +void ROUTER_PREVIEW_ITEM::Point( const VECTOR2I& aPos, int aStyle ) { -#if 0 - assert( false ); +} - m_line.Clear(); - m_line.Append( aBox.GetX(), aBox.GetY() ); - m_line.Append( aBox.GetX() + aBox.GetWidth(), aBox.GetY() + aBox.GetHeight() ); - m_line.Append( aBox.GetX() + aBox.GetWidth(), aBox.GetY() + aBox.GetHeight() ); - m_line.Append( aBox.GetX(), aBox.GetY() + aBox.GetHeight() ); - m_line.SetClosed( true ); - m_width = 20000; - m_color = assignColor( aStyle ); - m_type = PR_LINE; - ViewUpdate( GEOMETRY | APPEARANCE ); -#endif +void ROUTER_PREVIEW_ITEM::Box( const BOX2I& aBox, int aStyle ) +{ } const COLOR4D ROUTER_PREVIEW_ITEM::getLayerColor( int aLayer ) const { - // assert (m_view != NULL); - PCB_RENDER_SETTINGS* settings = static_cast ( m_parent->GetView()->GetPainter()->GetSettings() ); @@ -202,10 +267,10 @@ const COLOR4D ROUTER_PREVIEW_ITEM::assignColor( int aStyle ) const color = COLOR4D( 0, 1, 0, 1 ); break; case 1: - color = COLOR4D( 1, 0, 0, 0.3 ); break; + color = COLOR4D( 1, 0, 0, 1 ); break; case 2: - color = COLOR4D( 1, 0.5, 0.5, 1 ); break; + color = COLOR4D( 1, 1, 0, 1 ); break; case 3: color = COLOR4D( 0, 0, 1, 1 ); break; @@ -220,9 +285,11 @@ const COLOR4D ROUTER_PREVIEW_ITEM::assignColor( int aStyle ) const color = COLOR4D( 0, 1, 1, 1 ); break; case 32: - color = COLOR4D( 0, 0, 1, 0.5 ); break; + color = COLOR4D( 0, 0, 1, 1 ); break; default: + color = COLOR4D( 0.4, 0.4, 0.4, 1 ); break; + break; } diff --git a/pcbnew/router/router_preview_item.h b/pcbnew/router/router_preview_item.h index 058c47f816..514b21dcea 100644 --- a/pcbnew/router/router_preview_item.h +++ b/pcbnew/router/router_preview_item.h @@ -1,7 +1,7 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski * * This program is free software: you can redistribute it and/or modify it @@ -15,7 +15,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __ROUTER_PREVIEW_ITEM_H @@ -46,24 +46,32 @@ class ROUTER_PREVIEW_ITEM : public EDA_ITEM public: enum ItemType { - PR_VIA, - PR_LINE, - PR_STUCK_MARKER - }; - - enum ItemFlags - { - PR_SUGGESTION = 1 + PR_STUCK_MARKER = 0, + PR_POINT, + PR_SHAPE }; + ROUTER_PREVIEW_ITEM( const PNS_ITEM* aItem = NULL, KIGFX::VIEW_GROUP* aParent = NULL ); ~ROUTER_PREVIEW_ITEM(); void Update( const PNS_ITEM* aItem ); void StuckMarker( VECTOR2I& aPosition ); - void DebugLine( const SHAPE_LINE_CHAIN& aLine, int aWidth = 0, int aStyle = 0 ); - void DebugBox( const BOX2I& aBox, int aStyle = 0 ); + + void Line( const SHAPE_LINE_CHAIN& aLine, int aWidth = 0, int aStyle = 0 ); + void Box( const BOX2I& aBox, int aStyle = 0 ); + void Point ( const VECTOR2I& aPos, int aStyle = 0); + + void SetColor( const KIGFX::COLOR4D& aColor ) + { + m_color = aColor; + } + + void SetClearance ( int aClearance ) + { + m_clearance = aClearance; + } void Show( int a, std::ostream& b ) const {}; @@ -77,7 +85,7 @@ public: aCount = 1; } - void MarkAsHead(); + void drawLineChain( const SHAPE_LINE_CHAIN &l, KIGFX::GAL *aGal ) const; private: const KIGFX::COLOR4D assignColor( int aStyle ) const; @@ -86,17 +94,25 @@ private: KIGFX::VIEW_GROUP* m_parent; PNS_ROUTER* m_router; - SHAPE_LINE_CHAIN m_line; + SHAPE *m_shape; ItemType m_type; + int m_style; int m_width; int m_layer; + int m_originLayer; + int m_clearance; + + // fixme: shouldn't this go to VIEW? + static const int ClearanceOverlayDepth = -2000; + static const int BaseOverlayDepth = -2020; + static const int ViaOverlayDepth = -2046; + + double m_depth; KIGFX::COLOR4D m_color; - - VECTOR2I m_stuckPosition; - VECTOR2I m_viaCenter; + VECTOR2I m_pos; }; #endif diff --git a/pcbnew/router/router_tool.cpp b/pcbnew/router/router_tool.cpp index e270a0a5e8..4220aa4035 100644 --- a/pcbnew/router/router_tool.cpp +++ b/pcbnew/router/router_tool.cpp @@ -15,22 +15,28 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ +#include + #include #include +#include #include "class_draw_panel_gal.h" -#include "class_board_item.h" #include "class_board.h" #include #include +#include #include #include #include #include +#include +#include +#include #include #include @@ -45,29 +51,181 @@ using namespace KIGFX; using boost::optional; -//static TOOL_ACTION ACT_AutoEndRoute( "pcbnew.InteractiveRouter.AutoEndRoute", AS_CONTEXT, 'G' ); -//static TOOL_ACTION ACT_PlaceVia( "pcbnew.InteractiveRouter.PlaceVia", AS_CONTEXT, 'V' ); -//static TOOL_ACTION ACT_OpenRouteOptions( "pcbnew.InteractiveRouter.OpenRouterOptions", AS_CONTEXT, 'T' ); -//static TOOL_ACTION ACT_SwitchPosture( "pcbnew.InteractiveRouter.SwitchPosture", AS_CONTEXT, '/' ); -//static TOOL_ACTION ACT_EndTrack( "pcbnew.InteractiveRouter.EndTrack", AS_CONTEXT, WXK_END ); +static TOOL_ACTION ACT_NewTrack( "pcbnew.InteractiveRouter.NewTrack", + AS_CONTEXT, 'X', + "New Track", "Starts laying a new track."); +static TOOL_ACTION ACT_EndTrack( "pcbnew.InteractiveRouter.EndTrack", + AS_CONTEXT, WXK_END, + "End Track", "Stops laying the current track."); +static TOOL_ACTION ACT_AutoEndRoute( "pcbnew.InteractiveRouter.AutoEndRoute", + AS_CONTEXT, 'F', + "Auto-end Track", "Automagically finishes currently routed track." ); +static TOOL_ACTION ACT_Drag( "pcbnew.InteractiveRouter.Drag", + AS_CONTEXT, 'G', + "Drag Track/Via", "Drags a track or a via." ); +static TOOL_ACTION ACT_PlaceThroughVia( "pcbnew.InteractiveRouter.PlaceVia", + AS_CONTEXT, 'V', + "Place Through Via", "Adds a through-hole via at the end of currently routed track." ); +static TOOL_ACTION ACT_CustomTrackWidth( "pcbnew.InteractiveRouter.CustomTrackWidth", + AS_CONTEXT, 'W', + "Custom Track Width", "Shows a dialog for changing the track width and via size."); +static TOOL_ACTION ACT_RouterOptions( "pcbnew.InteractiveRouter.RouterOptions", + AS_CONTEXT, 'E', + "Routing Options...", "Shows a dialog containing router options."); +static TOOL_ACTION ACT_SwitchPosture( "pcbnew.InteractiveRouter.SwitchPosture", + AS_CONTEXT, '/', + "Switch Track Posture", "Switches posture of the currenly routed track."); ROUTER_TOOL::ROUTER_TOOL() : TOOL_INTERACTIVE( "pcbnew.InteractiveRouter" ) { m_router = NULL; - m_menu = new CONTEXT_MENU; - - m_menu->SetTitle( wxT( "Interactive router" ) ); // fixme: not implemented yet. Sorry. - m_menu->Add( wxT( "Cancel" ), 1 ); - m_menu->Add( wxT( "New track" ), 2 ); - m_menu->Add( wxT( "End track" ), 3 ); - m_menu->Add( wxT( "Auto-end track" ), 4 ); - m_menu->Add( wxT( "Place via" ), 5 ); - m_menu->Add( wxT( "Switch posture" ), 6 ); - - m_menu->Add( wxT( "Routing options..." ), 7 ); } +class CONTEXT_TRACK_WIDTH_MENU: public CONTEXT_MENU +{ +public: + CONTEXT_TRACK_WIDTH_MENU() + { + setCustomEventHandler( boost::bind( &CONTEXT_TRACK_WIDTH_MENU::handleCustomEvent, this, _1 ) ); + } + + void SetBoard( BOARD* aBoard ) + { + BOARD_DESIGN_SETTINGS &bds = aBoard->GetDesignSettings(); + + wxString msg; + m_board = aBoard; + + Append( ID_POPUP_PCB_SELECT_CUSTOM_WIDTH, _( "Custom size" ), wxEmptyString, wxITEM_CHECK ); + + Append( ID_POPUP_PCB_SELECT_AUTO_WIDTH, _( "Use the starting track width" ), + _( "Route using the width of the starting track." ), + wxITEM_CHECK ); + + Append( ID_POPUP_PCB_SELECT_USE_NETCLASS_VALUES, + _( "Use netclass values" ), + _( "Use track and via sizes from the net class" ), + wxITEM_CHECK ); + + for( unsigned i = 0; i < bds.m_TrackWidthList.size(); i++ ) + { + msg = _ ("Track "); + msg << StringFromValue( g_UserUnit, bds.m_TrackWidthList[i], true ); + + if( i == 0 ) + msg << _( " (from netclass)" ); + + Append( ID_POPUP_PCB_SELECT_WIDTH1 + i, msg, wxEmptyString, wxITEM_CHECK ); + } + + AppendSeparator(); + + for( unsigned i = 0; i < bds.m_ViasDimensionsList.size(); i++ ) + { + msg = _ ("Via "); + msg << StringFromValue( g_UserUnit, bds.m_ViasDimensionsList[i].m_Diameter, + true ); + wxString drill = StringFromValue( g_UserUnit, + bds.m_ViasDimensionsList[i].m_Drill, + true ); + + if( bds.m_ViasDimensionsList[i].m_Drill <= 0 ) + { + msg << _ (", drill: default"); + } else { + msg << _ (", drill: ") << drill; + } + + if( i == 0 ) + msg << _( " (from netclass)" ); + + Append( ID_POPUP_PCB_SELECT_VIASIZE1 + i, msg, wxEmptyString, wxITEM_CHECK ); + } + } + +protected: + OPT_TOOL_EVENT handleCustomEvent( const wxEvent& aEvent ) + { +#if ID_POPUP_PCB_SELECT_VIASIZE1 < ID_POPUP_PCB_SELECT_WIDTH1 +#error You have changed event ids, it breaks a piece of code. Lookup this line for more details. +// Recognising type of event (track width/via size) is based on comparison if the event id is +// within a specific range. If ranges of event ids changes, then the following is not valid anymore. +#endif + BOARD_DESIGN_SETTINGS &bds = m_board->GetDesignSettings(); + + int id = aEvent.GetId(); + + // General settings, to be modified below + bds.m_UseConnectedTrackWidth = false; + bds.UseCustomTrackViaSize( false ); + + if( id == ID_POPUP_PCB_SELECT_CUSTOM_WIDTH ) + { + bds.UseCustomTrackViaSize( true ); + } + + else if( id == ID_POPUP_PCB_SELECT_AUTO_WIDTH ) + { + bds.m_UseConnectedTrackWidth = true; + } + + else if( id == ID_POPUP_PCB_SELECT_USE_NETCLASS_VALUES ) + { + bds.SetViaSizeIndex( 0 ); + bds.SetTrackWidthIndex( 0 ); + } + + else if( id > ID_POPUP_PCB_SELECT_VIASIZE1 ) // via size has changed + { + assert( id < ID_POPUP_PCB_SELECT_WIDTH_END_RANGE ); + + bds.SetViaSizeIndex( id - ID_POPUP_PCB_SELECT_VIASIZE1 ); + } + + else // track width has changed + { + assert( id >= ID_POPUP_PCB_SELECT_WIDTH1 ); + assert( id < ID_POPUP_PCB_SELECT_VIASIZE ); + + bds.SetTrackWidthIndex( id - ID_POPUP_PCB_SELECT_WIDTH1 ); + } + + return OPT_TOOL_EVENT( COMMON_ACTIONS::trackViaSizeChanged.MakeEvent() ); + } + + BOARD* m_board; +}; + + +class CONTEXT_GRID_MENU: public CONTEXT_MENU { }; + + +class ROUTER_TOOL_MENU: public CONTEXT_MENU { + +public: + ROUTER_TOOL_MENU( BOARD *aBoard ) + { + SetTitle( wxT( "Interactive Router" ) ); + Add( ACT_NewTrack ); + Add( ACT_EndTrack ); +// Add( ACT_AutoEndRoute ); // fixme: not implemented yet. Sorry. + Add( ACT_Drag ); + Add( ACT_PlaceThroughVia ); + Add( ACT_SwitchPosture ); + + AppendSeparator ( ); + + CONTEXT_TRACK_WIDTH_MENU* trackMenu = new CONTEXT_TRACK_WIDTH_MENU; + trackMenu->SetBoard( aBoard ); + AppendSubMenu( trackMenu, wxT( "Select Track Width" ) ); + + Add( ACT_CustomTrackWidth ); + + AppendSeparator ( ); + Add( ACT_RouterOptions ); + } +}; ROUTER_TOOL::~ROUTER_TOOL() { @@ -108,6 +266,7 @@ void ROUTER_TOOL::getNetclassDimensions( int aNetCode, int& aWidth, int& aViaDiameter, int& aViaDrill ) { BOARD* board = getModel( PCB_T ); + BOARD_DESIGN_SETTINGS &bds = board->GetDesignSettings(); NETCLASS* netClass = NULL; NETINFO_ITEM* ni = board->FindNet( aNetCode ); @@ -115,11 +274,11 @@ void ROUTER_TOOL::getNetclassDimensions( int aNetCode, int& aWidth, if( ni ) { wxString netClassName = ni->GetClassName(); - netClass = board->GetDesignSettings().m_NetClasses.Find( netClassName ); + netClass = bds.m_NetClasses.Find( netClassName ); } if( !netClass ) - netClass = board->GetDesignSettings().m_NetClasses.GetDefault(); + netClass = bds.m_NetClasses.GetDefault(); aWidth = netClass->GetTrackWidth(); aViaDiameter = netClass->GetViaDiameter(); @@ -134,74 +293,67 @@ PNS_ITEM* ROUTER_TOOL::pickSingleItem( const VECTOR2I& aWhere, int aNet, int aLa if( aLayer > 0 ) tl = aLayer; - PNS_ITEM* picked_seg = NULL; - PNS_ITEM* picked_via = NULL; + PNS_ITEM* prioritized[4]; + + for(int i = 0; i < 4; i++) + prioritized[i] = 0; + PNS_ITEMSET candidates = m_router->QueryHoverItems( aWhere ); BOOST_FOREACH( PNS_ITEM* item, candidates.Items() ) { - if( !IsCopperLayer( item->GetLayers().Start() ) ) + if( !IsCopperLayer( item->Layers().Start() ) ) continue; - if( item->GetParent() && !item->GetParent()->ViewIsVisible() && - !item->GetParent()->IsSelected() ) - continue; + // fixme: this causes flicker with live loop removal... + //if( item->Parent() && !item->Parent()->ViewIsVisible() ) + // continue; - if( aNet < 0 || item->GetNet() == aNet ) + if( aNet < 0 || item->Net() == aNet ) { if( item->OfKind( PNS_ITEM::VIA | PNS_ITEM::SOLID ) ) { - if( item->GetLayers().Overlaps( tl ) || !picked_via ) - picked_via = item; + if( !prioritized[2] ) + prioritized[2] = item; + if( item->Layers().Overlaps( tl )) + prioritized[0] = item; } else { - if( item->GetLayers().Overlaps( tl ) || !picked_seg ) - picked_seg = item; + if( !prioritized[3] ) + prioritized[3] = item; + if (item->Layers().Overlaps( tl )) + prioritized[1] = item; } } } - if( DisplayOpt.ContrastModeDisplay ) + PNS_ITEM *rv = NULL; + for(int i = 0; i < 4; i++) { - if( picked_seg && !picked_seg->GetLayers().Overlaps( tl ) ) - picked_seg = NULL; + PNS_ITEM *item = prioritized[i]; + + if( DisplayOpt.ContrastModeDisplay ) + if( item && !item->Layers().Overlaps( tl ) ) + item = NULL; + + if(item) + { + rv = item; + break; + } } - PNS_ITEM* rv = picked_via ? picked_via : picked_seg; - - if( rv && aLayer >= 0 && !rv->GetLayers().Overlaps( aLayer ) ) + if( rv && aLayer >= 0 && !rv->Layers().Overlaps( aLayer ) ) rv = NULL; if( rv ) - TRACE( 0, "%s, layer : %d, tl: %d", rv->GetKindStr().c_str() % rv->GetLayers().Start() % + TRACE( 0, "%s, layer : %d, tl: %d", rv->KindStr().c_str() % rv->Layers().Start() % tl ); return rv; } - -void ROUTER_TOOL::setMsgPanel( bool aEnabled, int aEntry, - const wxString& aUpperMessage, const wxString& aLowerMessage ) -{ - PCB_EDIT_FRAME* frame = getEditFrame (); - - if( m_panelItems.size() <= (unsigned int) aEntry ) - m_panelItems.resize( aEntry + 1 ); - - m_panelItems[aEntry] = MSG_PANEL_ITEM( aUpperMessage, aLowerMessage, BLACK ); - frame->SetMsgPanel( m_panelItems ); -} - - -void ROUTER_TOOL::clearMsgPanel() -{ - PCB_EDIT_FRAME* frame = getEditFrame (); - - frame->ClearMsgPanel(); -} - - void ROUTER_TOOL::highlightNet( bool aEnabled, int aNetcode ) { RENDER_SETTINGS* rs = getView()->GetPainter()->GetSettings(); @@ -214,37 +366,104 @@ void ROUTER_TOOL::highlightNet( bool aEnabled, int aNetcode ) getView()->UpdateAllLayersColor(); } +void ROUTER_TOOL::handleCommonEvents( TOOL_EVENT& evt ) +{ +#ifdef DEBUG + if( evt.IsKeyPressed() ) + { + switch( evt.KeyCode() ) + { + case 'S': + TRACEn(2, "saving drag/route log...\n"); + m_router->DumpLog(); + break; + } + } + else +#endif + if( evt.IsAction( &ACT_RouterOptions ) ) + { + DIALOG_PNS_SETTINGS settingsDlg( m_toolMgr->GetEditFrame(), m_router->Settings() ); + + if( settingsDlg.ShowModal() ) + m_router->ApplySettings(); + } + + else if( evt.IsAction( &ACT_CustomTrackWidth ) ) + { + DIALOG_TRACK_VIA_SIZE sizeDlg( m_toolMgr->GetEditFrame(), m_router->Settings() ); + BOARD_DESIGN_SETTINGS& bds = getModel( PCB_T )->GetDesignSettings(); + + sizeDlg.ShowModal(); + + // TODO it should be changed, router settings won't keep track & via sizes in the future + bds.SetCustomTrackWidth( m_router->Settings().GetTrackWidth() ); + bds.SetCustomViaSize( m_router->Settings().GetViaDiameter() ); + bds.SetCustomViaDrill( m_router->Settings().GetViaDrill() ); + bds.UseCustomTrackViaSize( true ); + + // TODO Should be done another way, but RunAction() won't work here. As the ROUTER_TOOL + // did not call Wait(), it does not wait for events and therefore the sent event + // won't arrive here + TOOL_EVENT event = COMMON_ACTIONS::trackViaSizeChanged.MakeEvent(); + handleCommonEvents( event ); + } + + else if( evt.IsAction( &COMMON_ACTIONS::trackViaSizeChanged ) ) + { + BOARD_DESIGN_SETTINGS& bds = getModel( PCB_T )->GetDesignSettings(); + + m_router->Settings().SetTrackWidth( bds.GetCurrentTrackWidth() ); + m_router->Settings().SetViaDiameter( bds.GetCurrentViaSize() ); + m_router->Settings().SetViaDrill( bds.GetCurrentViaDrill() ); + m_router->ApplySettings(); + } +} void ROUTER_TOOL::updateStartItem( TOOL_EVENT& aEvent ) { VIEW_CONTROLS* ctls = getViewControls(); int tl = getView()->GetTopLayer(); + VECTOR2I cp = ctls->GetCursorPosition(); PNS_ITEM* startItem = NULL; if( aEvent.IsMotion() || aEvent.IsClick() ) { VECTOR2I p = aEvent.Position(); + startItem = pickSingleItem( p ); - if( startItem && startItem->GetNet() >= 0 ) + bool snapEnabled = !aEvent.Modifier(MD_SHIFT); + + m_router->EnableSnapping ( snapEnabled ); + + if(!snapEnabled && startItem && !startItem->Layers().Overlaps( tl ) ) + startItem = NULL; + + if( startItem && startItem->Net() >= 0 ) { bool dummy; - VECTOR2I cursorPos = m_router->SnapToItem( startItem, p, dummy ); - ctls->ForceCursorPosition( true, cursorPos ); + VECTOR2I psnap = m_router->SnapToItem( startItem, p, dummy ); + + if (snapEnabled) { + m_startSnapPoint = psnap; + ctls->ForceCursorPosition( true, psnap ); + } else { + m_startSnapPoint = cp; + ctls->ForceCursorPosition( false ); + } - m_startSnapPoint = cursorPos; - - if( startItem->GetLayers().IsMultilayer() ) + if( startItem->Layers().IsMultilayer() ) m_startLayer = tl; else - m_startLayer = startItem->GetLayers().Start(); + m_startLayer = startItem->Layers().Start(); m_startItem = startItem; } else { m_startItem = NULL; - m_startSnapPoint = p; + m_startSnapPoint = cp; m_startLayer = tl; ctls->ForceCursorPosition( false ); } @@ -255,13 +474,18 @@ void ROUTER_TOOL::updateStartItem( TOOL_EVENT& aEvent ) void ROUTER_TOOL::updateEndItem( TOOL_EVENT& aEvent ) { VIEW_CONTROLS* ctls = getViewControls(); - VECTOR2I p = aEvent.Position(); + VECTOR2I p = getView()->ToWorld( ctls->GetMousePosition() ); + VECTOR2I cp = ctls->GetCursorPosition(); int layer; - if( m_router->GetCurrentNet() < 0 || !m_startItem ) + bool snapEnabled = !aEvent.Modifier(MD_SHIFT); + + m_router->EnableSnapping ( snapEnabled ); + + if( !snapEnabled || m_router->GetCurrentNet() < 0 || !m_startItem ) { m_endItem = NULL; - m_endSnapPoint = p; + m_endSnapPoint = cp; return; } @@ -272,7 +496,7 @@ void ROUTER_TOOL::updateEndItem( TOOL_EVENT& aEvent ) else layer = m_router->GetCurrentLayer(); - PNS_ITEM* endItem = pickSingleItem( p, m_startItem->GetNet(), layer ); + PNS_ITEM* endItem = pickSingleItem( p, m_startItem->Net(), layer ); if( endItem ) { @@ -284,57 +508,39 @@ void ROUTER_TOOL::updateEndItem( TOOL_EVENT& aEvent ) else { m_endItem = NULL; - m_endSnapPoint = ctls->GetCursorPosition(); + m_endSnapPoint = cp; ctls->ForceCursorPosition( false ); } - // Draw ratsnest for the currently routed track - RN_DATA* ratsnest = getModel( PCB_T )->GetRatsnest(); - ratsnest->ClearSimple(); - - if( ( m_endItem == NULL || m_endItem == m_startItem ) && m_startItem->GetNet() > 0 ) - { - // The ending node has to be first, so the line for the track is drawn first - ratsnest->AddSimple( m_endSnapPoint, m_startItem->GetNet() ); - - // Those nodes are added just to force ratsnest not to drawn - // lines to already routed parts of the track - const PICKED_ITEMS_LIST& changes = m_router->GetLastChanges(); - for( unsigned int i = 0; i < changes.GetCount(); ++i ) - { - // Block the new tracks, do not handle tracks that were moved - // (moved tracks are saved in the undo buffer with UR_DELETED status instead) - if( changes.GetPickedItemStatus( i ) == UR_NEW ) - ratsnest->AddBlocked( static_cast( changes.GetPickedItem( i ) ) ); - } - - // Also the origin of the new track should be skipped in the ratsnest shown for the routed track - ratsnest->AddBlocked( static_cast( m_startItem->GetParent() ) ); - } - if( m_endItem ) - TRACE( 0, "%s, layer : %d", m_endItem->GetKindStr().c_str() % - m_endItem->GetLayers().Start() ); + TRACE( 0, "%s, layer : %d", m_endItem->KindStr().c_str() % + m_endItem->Layers().Start() ); } -void ROUTER_TOOL::startRouting() +void ROUTER_TOOL::performRouting() { + PCB_EDIT_FRAME* frame = getEditFrame(); bool saveUndoBuffer = true; VIEW_CONTROLS* ctls = getViewControls(); - int width = getDefaultWidth( m_startItem ? m_startItem->GetNet() : -1 ); + if( getModel( PCB_T )->GetDesignSettings().m_UseConnectedTrackWidth ) + { + int width = getDefaultWidth( m_startItem ? m_startItem->Net() : -1 ); - if( m_startItem && m_startItem->OfKind( PNS_ITEM::SEGMENT ) ) - width = static_cast( m_startItem )->GetWidth(); + if( m_startItem && m_startItem->OfKind( PNS_ITEM::SEGMENT ) ) + width = static_cast( m_startItem )->Width(); + + m_router->Settings().SetTrackWidth( width ); + } - m_router->SetCurrentWidth( width ); m_router->SwitchLayer( m_startLayer ); - getEditFrame()->SetTopLayer( m_startLayer ); + frame->SetActiveLayer( m_startLayer ); + frame->GetGalCanvas()->SetFocus(); - if( m_startItem && m_startItem->GetNet() >= 0 ) - highlightNet( true, m_startItem->GetNet() ); + if( m_startItem && m_startItem->Net() >= 0 ) + highlightNet( true, m_startItem->Net() ); ctls->ForceCursorPosition( false ); ctls->SetAutoPan( true ); @@ -366,43 +572,37 @@ void ROUTER_TOOL::startRouting() break; m_router->Move( m_endSnapPoint, m_endItem ); - } - else if( evt->IsKeyPressed() ) + } else if( evt->IsAction( &ACT_PlaceThroughVia ) ) { - switch( std::toupper( evt->KeyCode() ) ) - { - case 'V': - { - int w, diameter, drill; - getNetclassDimensions( m_router->GetCurrentNet(), w, diameter, drill ); - m_router->SetCurrentViaDiameter( diameter ); - m_router->SetCurrentViaDrill( drill ); - m_router->ToggleViaPlacement(); - getEditFrame()->SetTopLayer( m_router->GetCurrentLayer() ); - m_router->Move( m_endSnapPoint, m_endItem ); - break; - } - - case '/': - m_router->FlipPosture(); - break; - - case '+': - case '=': - m_router->SwitchLayer( m_router->NextCopperLayer( true ) ); - updateEndItem( *evt ); - getEditFrame()->SetTopLayer( m_router->GetCurrentLayer() ); - m_router->Move( m_endSnapPoint, m_endItem ); - - break; - - case '-': - m_router->SwitchLayer( m_router->NextCopperLayer( false ) ); - getEditFrame()->SetTopLayer( m_router->GetCurrentLayer() ); - m_router->Move( m_endSnapPoint, m_endItem ); - break; - } + m_router->ToggleViaPlacement(); + frame->SetTopLayer( m_router->GetCurrentLayer() ); + m_router->Move( m_endSnapPoint, m_endItem ); } + else if( evt->IsAction( &ACT_SwitchPosture ) ) + { + m_router->FlipPosture(); + m_router->Move( m_endSnapPoint, m_endItem ); + } + else if( evt->IsAction( &COMMON_ACTIONS::layerNext ) ) + { + m_router->SwitchLayer( m_router->NextCopperLayer( true ) ); + updateEndItem( *evt ); + frame->SetActiveLayer( m_router->GetCurrentLayer() ); + m_router->Move( m_endSnapPoint, m_endItem ); + } + else if( evt->IsAction( &COMMON_ACTIONS::layerPrev ) ) + { + m_router->SwitchLayer( m_router->NextCopperLayer( false ) ); + frame->SetActiveLayer( m_router->GetCurrentLayer() ); + m_router->Move( m_endSnapPoint, m_endItem ); + } + else if( evt->IsAction( &ACT_EndTrack ) ) + { + if( m_router->FixRoute( m_endSnapPoint, m_endItem ) ) + break; + } + + handleCommonEvents(*evt); } m_router->StopRouting(); @@ -410,10 +610,9 @@ void ROUTER_TOOL::startRouting() if( saveUndoBuffer ) { // Save the recent changes in the undo buffer - getEditFrame()->SaveCopyInUndoList( m_router->GetLastChanges(), - UR_UNSPECIFIED ); - m_router->ClearLastChanges(); - getEditFrame()->OnModify(); + frame->SaveCopyInUndoList( m_router->GetUndoBuffer(), UR_UNSPECIFIED ); + m_router->ClearUndoBuffer(); + frame->OnModify(); } else { @@ -430,14 +629,23 @@ void ROUTER_TOOL::startRouting() int ROUTER_TOOL::Main( TOOL_EVENT& aEvent ) { VIEW_CONTROLS* ctls = getViewControls(); + BOARD* board = getModel( PCB_T ); + BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings(); - // SetContextMenu ( m_menu ); - // setMsgPanel(true, 0, wxT("KiRouter"), wxT("Pick an item to start routing")); - getEditFrame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Add tracks" ) ); + getEditFrame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Interactive Router" ) ); ctls->SetSnapping( true ); ctls->ShowCursor( true ); + // Set current track widths & via size + m_router->Settings().SetTrackWidth( bds.GetCurrentTrackWidth() ); + m_router->Settings().SetViaDiameter( bds.GetCurrentViaSize() ); + m_router->Settings().SetViaDrill( bds.GetCurrentViaDrill() ); + + ROUTER_TOOL_MENU *ctxMenu = new ROUTER_TOOL_MENU( board ); + + SetContextMenu ( ctxMenu ); + // Main loop: keep receiving events while( OPT_TOOL_EVENT evt = Wait() ) { @@ -453,21 +661,69 @@ int ROUTER_TOOL::Main( TOOL_EVENT& aEvent ) m_needsSync = true; else if( evt->IsMotion() ) updateStartItem( *evt ); - else if( evt->IsClick( BUT_LEFT ) ) + else if( evt->IsClick( BUT_LEFT ) || evt->IsAction( &ACT_NewTrack ) ) { updateStartItem( *evt ); - startRouting(); - } - } - // clearMsgPanel(); + if( evt->Modifier( MD_CTRL ) ) + performDragging(); + else + performRouting(); + } else if ( evt->IsAction( &ACT_Drag ) ) + performDragging(); + + handleCommonEvents(*evt); + } // Restore the default settings ctls->SetAutoPan( false ); ctls->ShowCursor( false ); - ctls->ForceCursorPosition( false ); - getEditFrame()->SetToolID( ID_NO_TOOL_SELECTED, wxCURSOR_DEFAULT, wxEmptyString ); + delete ctxMenu; + return 0; } + + +void ROUTER_TOOL::performDragging() +{ + VIEW_CONTROLS* ctls = getViewControls(); + + bool dragStarted = m_router->StartDragging( m_startSnapPoint, m_startItem ); + + if(!dragStarted) + return; + + if( m_startItem && m_startItem->Net() >= 0 ) + highlightNet( true, m_startItem->Net() ); + + ctls->ForceCursorPosition( false ); + ctls->SetAutoPan( true ); + + while( OPT_TOOL_EVENT evt = Wait() ) + { + if( evt->IsCancel() ) + break; + else if( evt->IsMotion() ) + { + updateEndItem( *evt ); + m_router->Move( m_endSnapPoint, m_endItem ); + } + else if( evt->IsClick( BUT_LEFT ) ) + { + + if( m_router->FixRoute( m_endSnapPoint, m_endItem ) ) + break; + + } + handleCommonEvents(*evt); + } + + if( m_router->RoutingInProgress() ) + m_router->StopRouting(); + + ctls->SetAutoPan( false ); + ctls->ForceCursorPosition( false ); + highlightNet( false ); +} diff --git a/pcbnew/router/router_tool.h b/pcbnew/router/router_tool.h index 42bbceb19c..db9db3ea6a 100644 --- a/pcbnew/router/router_tool.h +++ b/pcbnew/router/router_tool.h @@ -1,8 +1,9 @@ /* * KiRouter - a push-and-(sometimes-)shove PCB router * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2014 CERN * Author: Tomasz Wlostowski + * Author: Maciej Suminski * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -15,7 +16,7 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __ROUTER_TOOL_H @@ -24,6 +25,8 @@ #include #include +#include + #include #include @@ -35,7 +38,7 @@ class PNS_ROUTER; class PNS_ITEM; -class ROUTER_TOOL : public TOOL_INTERACTIVE +class APIEXPORT ROUTER_TOOL : public TOOL_INTERACTIVE { public: ROUTER_TOOL(); @@ -48,12 +51,11 @@ private: PNS_ITEM* pickSingleItem( const VECTOR2I& aWhere, int aNet = -1, int aLayer = -1 ); - void setMsgPanel( bool enabled, int entry, const wxString& aUpperMessage = wxT(""), - const wxString& aLowerMessage = wxT("") ); - void clearMsgPanel(); - int getDefaultWidth( int aNetCode ); - void startRouting(); + + void performRouting(); + void performDragging(); + void highlightNet( bool enabled, int netcode = -1 ); void updateStartItem( TOOL_EVENT& aEvent ); @@ -61,6 +63,8 @@ private: void getNetclassDimensions( int aNetCode, int& aWidth, int& aViaDiameter, int& aViaDrill ); + void handleCommonEvents( TOOL_EVENT& evt ); + MSG_PANEL_ITEMS m_panelItems; PNS_ROUTER* m_router; @@ -72,11 +76,11 @@ private: PNS_ITEM* m_endItem; VECTOR2I m_endSnapPoint; + + CONTEXT_MENU* m_menu; + ///> Flag marking that the router's world needs syncing. bool m_needsSync; - - /*boost::shared_ptr m_menu;*/ - CONTEXT_MENU* m_menu; }; #endif diff --git a/pcbnew/router/time_limit.cpp b/pcbnew/router/time_limit.cpp new file mode 100644 index 0000000000..0e14a10678 --- /dev/null +++ b/pcbnew/router/time_limit.cpp @@ -0,0 +1,46 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include + +#include "time_limit.h" + +TIME_LIMIT::TIME_LIMIT( int aMilliseconds ) : + m_limitMs( aMilliseconds ) + { + Restart(); + }; + +TIME_LIMIT::~TIME_LIMIT () {} + +bool TIME_LIMIT::Expired() const +{ + return ( wxGetLocalTimeMillis().GetValue() - m_startTics ) >= m_limitMs; +} + +void TIME_LIMIT::Restart() +{ + m_startTics = wxGetLocalTimeMillis().GetValue(); +} + +void TIME_LIMIT::Set ( int aMilliseconds ) +{ + m_limitMs = aMilliseconds; +} diff --git a/pcbnew/router/time_limit.h b/pcbnew/router/time_limit.h new file mode 100644 index 0000000000..fbb58c4156 --- /dev/null +++ b/pcbnew/router/time_limit.h @@ -0,0 +1,44 @@ +/* + * KiRouter - a push-and-(sometimes-)shove PCB router + * + * Copyright (C) 2013-2014 CERN + * Author: Tomasz Wlostowski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __TIME_LIMIT_H +#define __TIME_LIMIT_H + +#include + +class TIME_LIMIT { + +public: + TIME_LIMIT( int aMilliseconds = 0); + ~TIME_LIMIT (); + + bool Expired() const; + void Restart(); + + void Set ( int aMilliseconds ); + +private: + + int m_limitMs; + int64_t m_startTics; +}; + + +#endif \ No newline at end of file diff --git a/pcbnew/router/trace.h b/pcbnew/router/trace.h index 1f8a3e32c9..91f29baa15 100644 --- a/pcbnew/router/trace.h +++ b/pcbnew/router/trace.h @@ -15,22 +15,21 @@ * General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with this program. If not, see . + * with this program. If not, see . */ #ifndef __TRACE_H #define __TRACE_H -// #ifdef DEBUG -#if 0 - #include #include #include -static void _trace_print( const char* aFuncName, int level, const std::string& aMsg ) +static inline void _trace_print( const char* aFuncName, int level, const std::string& aMsg ) { +#ifdef DEBUG std::cerr << "trace[" << level << "]: " << aFuncName << ": " << aMsg << std::endl; +#endif } #define TRACE( level, fmt, ... ) \ @@ -39,11 +38,4 @@ static void _trace_print( const char* aFuncName, int level, const std::string& a #define TRACEn( level, msg ) \ _trace_print( __FUNCTION__, level, std::string( msg ) ); -#else - -#define TRACE( level, fmt, ... ) -#define TRACEn( level, msg ) - -#endif - #endif