You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

682 lines
17 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 3
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * https://www.gnu.org/licenses/gpl-3.0.html
  21. * or you may search the http://www.gnu.org website for the version 3 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include "sim_plot_panel.h"
  26. #include "sim_plot_frame.h"
  27. #include <algorithm>
  28. #include <limits>
  29. static wxString formatFloat( double x, int nDigits )
  30. {
  31. wxString rv, fmt;
  32. if( nDigits )
  33. {
  34. fmt = wxT( "%.0Nf" );
  35. fmt[3] = '0' + nDigits;
  36. }
  37. else
  38. {
  39. fmt = wxT( "%.0f" );
  40. }
  41. rv.Printf( fmt, x );
  42. return rv;
  43. }
  44. static void getSISuffix( double x, const wxString& unit, int& power, wxString& suffix )
  45. {
  46. const int n_powers = 11;
  47. const struct
  48. {
  49. double exponent;
  50. char suffix;
  51. } powers[] =
  52. {
  53. { -18, 'a' },
  54. { -15, 'f' },
  55. { -12, 'p' },
  56. { -9, 'n' },
  57. { -6, 'u' },
  58. { -3, 'm' },
  59. { 0, 0 },
  60. { 3, 'k' },
  61. { 6, 'M' },
  62. { 9, 'G' },
  63. { 12, 'T' },
  64. { 14, 'P' }
  65. };
  66. power = 0;
  67. suffix = unit;
  68. if( x == 0.0 )
  69. return;
  70. for( int i = 0; i < n_powers - 1; i++ )
  71. {
  72. double r_cur = pow( 10, powers[i].exponent );
  73. if( fabs( x ) >= r_cur && fabs( x ) < r_cur * 1000.0 )
  74. {
  75. power = powers[i].exponent;
  76. if( powers[i].suffix )
  77. suffix = wxString( powers[i].suffix ) + unit;
  78. else
  79. suffix = unit;
  80. return;
  81. }
  82. }
  83. }
  84. static int countDecimalDigits( double x, int maxDigits )
  85. {
  86. int64_t k = (int)( ( x - floor( x ) ) * pow( 10.0, (double) maxDigits ) );
  87. int n = 0;
  88. while( k && ( ( k % 10LL ) == 0LL || ( k % 10LL ) == 9LL ) )
  89. {
  90. k /= 10LL;
  91. }
  92. n = 0;
  93. while( k != 0LL )
  94. {
  95. n++;
  96. k /= 10LL;
  97. }
  98. return n;
  99. }
  100. static void formatSILabels( mpScaleBase* scale, const wxString& aUnit, int nDigits )
  101. {
  102. double maxVis = scale->AbsVisibleMaxValue();
  103. wxString suffix;
  104. int power, digits = 0;
  105. getSISuffix( maxVis, aUnit, power, suffix );
  106. double sf = pow( 10.0, power );
  107. for( auto &l : scale->TickLabels() )
  108. {
  109. int k = countDecimalDigits( l.pos / sf, nDigits );
  110. digits = std::max( digits, k );
  111. }
  112. for( auto &l : scale->TickLabels() )
  113. {
  114. l.label = formatFloat ( l.pos / sf, digits ) + suffix;
  115. l.visible = true;
  116. }
  117. }
  118. class FREQUENCY_LOG_SCALE : public mpScaleXLog
  119. {
  120. public:
  121. FREQUENCY_LOG_SCALE( wxString name, int flags ) :
  122. mpScaleXLog( name, flags ) {};
  123. void formatLabels() override
  124. {
  125. const wxString unit = wxT( "Hz" );
  126. wxString suffix;
  127. int power;
  128. for( auto &l : TickLabels() )
  129. {
  130. getSISuffix( l.pos, unit, power, suffix );
  131. double sf = pow( 10.0, power );
  132. int k = countDecimalDigits( l.pos / sf, 3 );
  133. l.label = formatFloat( l.pos / sf, k ) + suffix;
  134. l.visible = true;
  135. }
  136. }
  137. };
  138. class FREQUENCY_LIN_SCALE : public mpScaleX
  139. {
  140. public:
  141. FREQUENCY_LIN_SCALE( wxString name, int flags ) :
  142. mpScaleX( name, flags, false , 0 ) {};
  143. void formatLabels() override
  144. {
  145. formatSILabels( this, wxT( "Hz" ), 3 );
  146. }
  147. };
  148. class TIME_SCALE : public mpScaleX
  149. {
  150. public:
  151. TIME_SCALE( wxString name, int flags ) :
  152. mpScaleX( name, flags, false, 0 ) {};
  153. void formatLabels() override
  154. {
  155. formatSILabels( this, wxT( "s" ), 3 );
  156. }
  157. };
  158. class VOLTAGE_SCALE_X : public mpScaleX
  159. {
  160. public:
  161. VOLTAGE_SCALE_X( wxString name, int flags ) :
  162. mpScaleX( name, flags, false, 0 ) {};
  163. void formatLabels() override
  164. {
  165. formatSILabels( this, wxT( "V" ), 3 );
  166. }
  167. };
  168. class GAIN_SCALE : public mpScaleY
  169. {
  170. public:
  171. GAIN_SCALE( wxString name, int flags ) :
  172. mpScaleY( name, flags, false ) {};
  173. void formatLabels() override
  174. {
  175. formatSILabels( this, wxT( "dBV" ), 3 );
  176. }
  177. };
  178. class PHASE_SCALE : public mpScaleY
  179. {
  180. public:
  181. PHASE_SCALE( wxString name, int flags ) :
  182. mpScaleY( name, flags, false ) {};
  183. void formatLabels() override
  184. {
  185. formatSILabels( this, wxT( "\u00B0" ), 3 ); // degree sign
  186. }
  187. };
  188. class VOLTAGE_SCALE_Y : public mpScaleY
  189. {
  190. public:
  191. VOLTAGE_SCALE_Y( wxString name, int flags ) :
  192. mpScaleY( name, flags, false ) {};
  193. void formatLabels() override
  194. {
  195. formatSILabels( this, wxT( "V" ), 3 );
  196. }
  197. };
  198. class CURRENT_SCALE : public mpScaleY
  199. {
  200. public:
  201. CURRENT_SCALE( wxString name, int flags ) :
  202. mpScaleY( name, flags, false ) {};
  203. void formatLabels() override
  204. {
  205. formatSILabels( this, wxT( "A" ), 3 );
  206. }
  207. };
  208. void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
  209. {
  210. if( !m_window )
  211. m_window = &aWindow;
  212. if( !m_visible )
  213. return;
  214. const auto& dataX = m_trace->GetDataX();
  215. const auto& dataY = m_trace->GetDataY();
  216. if( dataX.size() <= 1 )
  217. return;
  218. if( m_updateRequired )
  219. {
  220. m_coords.x = m_trace->s2x( aWindow.p2x( m_dim.x ) );
  221. // Find the closest point coordinates
  222. auto maxXIt = std::upper_bound( dataX.begin(), dataX.end(), m_coords.x );
  223. int maxIdx = maxXIt - dataX.begin();
  224. int minIdx = maxIdx - 1;
  225. // Out of bounds checks
  226. if( minIdx < 0 )
  227. {
  228. minIdx = 0;
  229. maxIdx = 1;
  230. m_coords.x = dataX[0];
  231. }
  232. else if( maxIdx >= (int) dataX.size() )
  233. {
  234. maxIdx = dataX.size() - 1;
  235. minIdx = maxIdx - 1;
  236. m_coords.x = dataX[maxIdx];
  237. }
  238. const double leftX = dataX[minIdx];
  239. const double rightX = dataX[maxIdx];
  240. const double leftY = dataY[minIdx];
  241. const double rightY = dataY[maxIdx];
  242. // Linear interpolation
  243. m_coords.y = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( m_coords.x - leftX );
  244. m_updateRequired = false;
  245. // Notify the parent window about the changes
  246. wxQueueEvent( aWindow.GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
  247. }
  248. else
  249. {
  250. m_updateRef = true;
  251. }
  252. if( m_updateRef )
  253. {
  254. UpdateReference();
  255. m_updateRef = false;
  256. }
  257. // Line length in horizontal and vertical dimensions
  258. const wxPoint cursorPos( aWindow.x2p( m_trace->x2s( m_coords.x ) ),
  259. aWindow.y2p( m_trace->y2s( m_coords.y ) ) );
  260. wxCoord leftPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginLeft();
  261. wxCoord rightPx = m_drawOutsideMargins ? aWindow.GetScrX() : aWindow.GetScrX() - aWindow.GetMarginRight();
  262. wxCoord topPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginTop();
  263. wxCoord bottomPx = m_drawOutsideMargins ? aWindow.GetScrY() : aWindow.GetScrY() - aWindow.GetMarginBottom();
  264. aDC.SetPen( wxPen( m_plotPanel->GetPlotColor( SIM_CURSOR_COLOR ), 1,
  265. m_continuous ? wxPENSTYLE_SOLID : wxPENSTYLE_LONG_DASH ) );
  266. if( topPx < cursorPos.y && cursorPos.y < bottomPx )
  267. aDC.DrawLine( leftPx, cursorPos.y, rightPx, cursorPos.y );
  268. if( leftPx < cursorPos.x && cursorPos.x < rightPx )
  269. aDC.DrawLine( cursorPos.x, topPx, cursorPos.x, bottomPx );
  270. }
  271. bool CURSOR::Inside( wxPoint& aPoint )
  272. {
  273. if( !m_window )
  274. return false;
  275. return ( std::abs( (double) aPoint.x - m_window->x2p( m_trace->x2s( m_coords.x ) ) ) <= DRAG_MARGIN )
  276. || ( std::abs( (double) aPoint.y - m_window->y2p( m_trace->y2s( m_coords.y ) ) ) <= DRAG_MARGIN );
  277. }
  278. void CURSOR::UpdateReference()
  279. {
  280. if( !m_window )
  281. return;
  282. m_reference.x = m_window->x2p( m_trace->x2s( m_coords.x ) );
  283. m_reference.y = m_window->y2p( m_trace->y2s( m_coords.y ) );
  284. }
  285. SIM_PLOT_PANEL::SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, SIM_PLOT_FRAME* aMainFrame,
  286. wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name )
  287. : SIM_PANEL_BASE( aType, parent, id, pos, size, style, name ),
  288. m_colorIdx( 0 ),
  289. m_axis_x( nullptr ),
  290. m_axis_y1( nullptr ),
  291. m_axis_y2( nullptr ),
  292. m_dotted_cp( false ),
  293. m_masterFrame( aMainFrame )
  294. {
  295. m_sizer = new wxBoxSizer( wxVERTICAL );
  296. m_plotWin = new mpWindow( this, wxID_ANY, pos, size, style );
  297. m_plotWin->LimitView( true );
  298. m_plotWin->SetMargins( 50, 80, 50, 80 );
  299. m_plotWin->SetColourTheme( GetPlotColor( SIM_BG_COLOR ), GetPlotColor( SIM_FG_COLOR ),
  300. GetPlotColor( SIM_AXIS_COLOR ) );
  301. switch( GetType() )
  302. {
  303. case ST_AC:
  304. m_axis_x = new FREQUENCY_LOG_SCALE( _( "Frequency" ), mpALIGN_BOTTOM );
  305. m_axis_y1 = new GAIN_SCALE( _( "Gain" ), mpALIGN_LEFT );
  306. m_axis_y2 = new PHASE_SCALE( _( "Phase" ), mpALIGN_RIGHT );
  307. m_axis_y2->SetMasterScale( m_axis_y1 );
  308. break;
  309. case ST_DC:
  310. m_axis_x = new VOLTAGE_SCALE_X( _( "Voltage (swept)" ), mpALIGN_BOTTOM );
  311. m_axis_y1 = new VOLTAGE_SCALE_Y( _( "Voltage (measured)" ), mpALIGN_LEFT );
  312. m_axis_y2 = new CURRENT_SCALE( _( "Current" ), mpALIGN_RIGHT );
  313. break;
  314. case ST_NOISE:
  315. m_axis_x = new FREQUENCY_LOG_SCALE( _( "Frequency" ), mpALIGN_BOTTOM );
  316. m_axis_y1 = new mpScaleY( _( "noise [(V or A)^2/Hz]" ), mpALIGN_LEFT );
  317. break;
  318. case ST_TRANSIENT:
  319. m_axis_x = new TIME_SCALE( _( "Time" ), mpALIGN_BOTTOM );
  320. m_axis_y1 = new VOLTAGE_SCALE_Y( _( "Voltage" ), mpALIGN_LEFT );
  321. m_axis_y2 = new CURRENT_SCALE( _( "Current" ), mpALIGN_RIGHT );
  322. m_axis_y2->SetMasterScale( m_axis_y1 );
  323. break;
  324. default:
  325. // suppress warnings
  326. break;
  327. }
  328. if( m_axis_x )
  329. {
  330. m_axis_x->SetTicks( false );
  331. m_axis_x->SetNameAlign ( mpALIGN_BOTTOM );
  332. m_plotWin->AddLayer( m_axis_x );
  333. }
  334. if( m_axis_y1 )
  335. {
  336. m_axis_y1->SetTicks( false );
  337. m_axis_y1->SetNameAlign ( mpALIGN_LEFT );
  338. m_plotWin->AddLayer( m_axis_y1 );
  339. }
  340. if( m_axis_y2 )
  341. {
  342. m_axis_y2->SetTicks( false );
  343. m_axis_y2->SetNameAlign ( mpALIGN_RIGHT );
  344. m_plotWin->AddLayer( m_axis_y2 );
  345. }
  346. // a mpInfoLegend displays le name of traces on the left top panel corner:
  347. m_legend = new mpInfoLegend( wxRect( 0, 40, 200, 40 ), wxTRANSPARENT_BRUSH );
  348. m_legend->SetVisible( false );
  349. m_plotWin->AddLayer( m_legend );
  350. m_plotWin->EnableDoubleBuffer( true );
  351. m_plotWin->UpdateAll();
  352. m_sizer->Add( m_plotWin, 1, wxALL | wxEXPAND, 1 );
  353. SetSizer( m_sizer );
  354. }
  355. SIM_PLOT_PANEL::~SIM_PLOT_PANEL()
  356. {
  357. // ~mpWindow destroys all the added layers, so there is no need to destroy m_traces contents
  358. }
  359. void SIM_PLOT_PANEL::UpdatePlotColors()
  360. {
  361. // Update bg and fg colors:
  362. m_plotWin->SetColourTheme( GetPlotColor( SIM_BG_COLOR ), GetPlotColor( SIM_FG_COLOR ),
  363. GetPlotColor( SIM_AXIS_COLOR ) );
  364. m_plotWin->UpdateAll();
  365. }
  366. wxColour SIM_PLOT_PANEL::GetPlotColor( int aIndex )
  367. {
  368. return m_masterFrame->GetPlotColor( aIndex );
  369. }
  370. void SIM_PLOT_PANEL::UpdateTraceStyle( TRACE* trace )
  371. {
  372. int flags = trace->GetFlags();
  373. wxPenStyle penStyle = ( ( flags & SPT_AC_PHASE || flags & SPT_CURRENT ) && m_dotted_cp ) ?
  374. wxPENSTYLE_DOT :
  375. wxPENSTYLE_SOLID;
  376. trace->SetPen( wxPen( trace->GetTraceColour(), 2, penStyle ) );
  377. }
  378. bool SIM_PLOT_PANEL::AddTrace( const wxString& aName, int aPoints,
  379. const double* aX, const double* aY, SIM_PLOT_TYPE aFlags )
  380. {
  381. TRACE* trace = NULL;
  382. // Find previous entry, if there is one
  383. auto prev = m_traces.find( aName );
  384. bool addedNewEntry = ( prev == m_traces.end() );
  385. if( addedNewEntry )
  386. {
  387. if( GetType() == ST_TRANSIENT )
  388. {
  389. bool hasVoltageTraces = false;
  390. for( const auto& tr : m_traces )
  391. {
  392. if( !( tr.second->GetFlags() & SPT_CURRENT ) )
  393. {
  394. hasVoltageTraces = true;
  395. break;
  396. }
  397. }
  398. if( !hasVoltageTraces )
  399. m_axis_y2->SetMasterScale( nullptr );
  400. else
  401. m_axis_y2->SetMasterScale( m_axis_y1 );
  402. }
  403. // New entry
  404. trace = new TRACE( aName );
  405. trace->SetTraceColour( generateColor() );
  406. UpdateTraceStyle( trace );
  407. m_traces[aName] = trace;
  408. // It is a trick to keep legend & coords always on the top
  409. for( mpLayer* l : m_topLevel )
  410. m_plotWin->DelLayer( l );
  411. m_plotWin->AddLayer( (mpLayer*) trace );
  412. for( mpLayer* l : m_topLevel )
  413. m_plotWin->AddLayer( l );
  414. }
  415. else
  416. {
  417. trace = prev->second;
  418. }
  419. std::vector<double> tmp( aY, aY + aPoints );
  420. if( GetType() == ST_AC )
  421. {
  422. if( aFlags & SPT_AC_PHASE )
  423. {
  424. for( int i = 0; i < aPoints; i++ )
  425. tmp[i] = tmp[i] * 180.0 / M_PI; // convert to degrees
  426. }
  427. else
  428. {
  429. for( int i = 0; i < aPoints; i++ )
  430. tmp[i] = 20 * log( tmp[i] ) / log( 10.0 ); // convert to dB
  431. }
  432. }
  433. trace->SetData( std::vector<double>( aX, aX + aPoints ), tmp );
  434. if( ( aFlags & SPT_AC_PHASE ) || ( aFlags & SPT_CURRENT ) )
  435. trace->SetScale( m_axis_x, m_axis_y2 );
  436. else
  437. trace->SetScale( m_axis_x, m_axis_y1 );
  438. trace->SetFlags( aFlags );
  439. m_plotWin->UpdateAll();
  440. return addedNewEntry;
  441. }
  442. bool SIM_PLOT_PANEL::DeleteTrace( const wxString& aName )
  443. {
  444. auto it = m_traces.find( aName );
  445. if( it != m_traces.end() )
  446. {
  447. TRACE* trace = it->second;
  448. m_traces.erase( it );
  449. if( CURSOR* cursor = trace->GetCursor() )
  450. m_plotWin->DelLayer( cursor, true );
  451. m_plotWin->DelLayer( trace, true, true );
  452. ResetScales();
  453. return true;
  454. }
  455. return false;
  456. }
  457. void SIM_PLOT_PANEL::DeleteAllTraces()
  458. {
  459. for( auto& t : m_traces )
  460. {
  461. DeleteTrace( t.first );
  462. }
  463. m_colorIdx = 0;
  464. m_traces.clear();
  465. }
  466. bool SIM_PLOT_PANEL::HasCursorEnabled( const wxString& aName ) const
  467. {
  468. TRACE* t = GetTrace( aName );
  469. return t ? t->HasCursor() : false;
  470. }
  471. void SIM_PLOT_PANEL::EnableCursor( const wxString& aName, bool aEnable )
  472. {
  473. TRACE* t = GetTrace( aName );
  474. if( t == nullptr || t->HasCursor() == aEnable )
  475. return;
  476. if( aEnable )
  477. {
  478. CURSOR* c = new CURSOR( t, this );
  479. int plotCenter = GetPlotWin()->GetMarginLeft()
  480. + ( GetPlotWin()->GetXScreen() - GetPlotWin()->GetMarginLeft()
  481. - GetPlotWin()->GetMarginRight() )
  482. / 2;
  483. c->SetX( plotCenter );
  484. t->SetCursor( c );
  485. m_plotWin->AddLayer( c );
  486. }
  487. else
  488. {
  489. CURSOR* c = t->GetCursor();
  490. t->SetCursor( NULL );
  491. m_plotWin->DelLayer( c, true );
  492. }
  493. // Notify the parent window about the changes
  494. wxQueueEvent( GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
  495. }
  496. void SIM_PLOT_PANEL::ResetScales()
  497. {
  498. if( m_axis_x )
  499. m_axis_x->ResetDataRange();
  500. if( m_axis_y1 )
  501. m_axis_y1->ResetDataRange();
  502. if( m_axis_y2 )
  503. m_axis_y2->ResetDataRange();
  504. for( auto t : m_traces )
  505. t.second->UpdateScales();
  506. }
  507. wxColour SIM_PLOT_PANEL::generateColor()
  508. {
  509. const unsigned int colorCount = m_masterFrame->GetPlotColorCount() - SIM_TRACE_COLOR;
  510. for( int i = 0; i < (int)colorCount - 1; i++ )
  511. {
  512. const wxColour color = GetPlotColor( i+SIM_TRACE_COLOR );
  513. bool hasColor = false;
  514. for( auto& t : m_traces )
  515. {
  516. TRACE* trace = t.second;
  517. if( trace->GetTraceColour() == color )
  518. {
  519. hasColor = true;
  520. break;
  521. }
  522. }
  523. if( !hasColor )
  524. return color;
  525. }
  526. // If all colors are in use, choose a suitable color in list
  527. int idx = m_traces.size() % colorCount;
  528. return wxColour( GetPlotColor( idx + SIM_TRACE_COLOR ) );
  529. }
  530. wxDEFINE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );