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.

680 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 <algorithm>
  27. #include <limits>
  28. static wxString formatFloat( double x, int nDigits )
  29. {
  30. wxString rv, fmt;
  31. if( nDigits )
  32. {
  33. fmt = wxT( "%.0Nf" );
  34. fmt[3] = '0' + nDigits;
  35. }
  36. else
  37. {
  38. fmt = wxT( "%.0f" );
  39. }
  40. rv.Printf( fmt, x );
  41. return rv;
  42. }
  43. static void getSISuffix( double x, const wxString& unit, int& power, wxString& suffix )
  44. {
  45. const int n_powers = 11;
  46. const struct
  47. {
  48. double exponent;
  49. char suffix;
  50. } powers[] =
  51. {
  52. { -18, 'a' },
  53. { -15, 'f' },
  54. { -12, 'p' },
  55. { -9, 'n' },
  56. { -6, 'u' },
  57. { -3, 'm' },
  58. { 0, 0 },
  59. { 3, 'k' },
  60. { 6, 'M' },
  61. { 9, 'G' },
  62. { 12, 'T' },
  63. { 14, 'P' }
  64. };
  65. power = 0;
  66. suffix = unit;
  67. if( x == 0.0 )
  68. return;
  69. for( int i = 0; i < n_powers - 1; i++ )
  70. {
  71. double r_cur = pow( 10, powers[i].exponent );
  72. if( fabs( x ) >= r_cur && fabs( x ) < r_cur * 1000.0 )
  73. {
  74. power = powers[i].exponent;
  75. if( powers[i].suffix )
  76. suffix = wxString( powers[i].suffix ) + unit;
  77. else
  78. suffix = unit;
  79. return;
  80. }
  81. }
  82. }
  83. static int countDecimalDigits( double x, int maxDigits )
  84. {
  85. int64_t k = (int)( ( x - floor( x ) ) * pow( 10.0, (double) maxDigits ) );
  86. int n = 0;
  87. while( k && ( ( k % 10LL ) == 0LL || ( k % 10LL ) == 9LL ) )
  88. {
  89. k /= 10LL;
  90. }
  91. n = 0;
  92. while( k != 0LL )
  93. {
  94. n++;
  95. k /= 10LL;
  96. }
  97. return n;
  98. }
  99. static void formatSILabels( mpScaleBase* scale, const wxString& aUnit, int nDigits )
  100. {
  101. double maxVis = scale->AbsVisibleMaxValue();
  102. wxString suffix;
  103. int power, digits = 0;
  104. getSISuffix( maxVis, aUnit, power, suffix );
  105. double sf = pow( 10.0, power );
  106. for( auto &l : scale->TickLabels() )
  107. {
  108. int k = countDecimalDigits( l.pos / sf, nDigits );
  109. digits = std::max( digits, k );
  110. }
  111. for( auto &l : scale->TickLabels() )
  112. {
  113. l.label = formatFloat ( l.pos / sf, digits ) + suffix;
  114. l.visible = true;
  115. }
  116. }
  117. class FREQUENCY_LOG_SCALE : public mpScaleXLog
  118. {
  119. public:
  120. FREQUENCY_LOG_SCALE( wxString name, int flags ) :
  121. mpScaleXLog( name, flags ) {};
  122. void formatLabels() override
  123. {
  124. const wxString unit = wxT( "Hz" );
  125. wxString suffix;
  126. int power;
  127. for( auto &l : TickLabels() )
  128. {
  129. getSISuffix( l.pos, unit, power, suffix );
  130. double sf = pow( 10.0, power );
  131. int k = countDecimalDigits( l.pos / sf, 3 );
  132. l.label = formatFloat( l.pos / sf, k ) + suffix;
  133. l.visible = true;
  134. }
  135. }
  136. };
  137. class FREQUENCY_LIN_SCALE : public mpScaleX
  138. {
  139. public:
  140. FREQUENCY_LIN_SCALE( wxString name, int flags ) :
  141. mpScaleX( name, flags, false , 0 ) {};
  142. void formatLabels() override
  143. {
  144. formatSILabels( this, wxT( "Hz" ), 3 );
  145. }
  146. };
  147. class TIME_SCALE : public mpScaleX
  148. {
  149. public:
  150. TIME_SCALE( wxString name, int flags ) :
  151. mpScaleX( name, flags, false, 0 ) {};
  152. void formatLabels() override
  153. {
  154. formatSILabels( this, wxT( "s" ), 3 );
  155. }
  156. };
  157. class VOLTAGE_SCALE_X : public mpScaleX
  158. {
  159. public:
  160. VOLTAGE_SCALE_X( wxString name, int flags ) :
  161. mpScaleX( name, flags, false, 0 ) {};
  162. void formatLabels() override
  163. {
  164. formatSILabels( this, wxT( "V" ), 3 );
  165. }
  166. };
  167. class GAIN_SCALE : public mpScaleY
  168. {
  169. public:
  170. GAIN_SCALE( wxString name, int flags ) :
  171. mpScaleY( name, flags, false ) {};
  172. void formatLabels() override
  173. {
  174. formatSILabels( this, wxT( "dBV" ), 3 );
  175. }
  176. };
  177. class PHASE_SCALE : public mpScaleY
  178. {
  179. public:
  180. PHASE_SCALE( wxString name, int flags ) :
  181. mpScaleY( name, flags, false ) {};
  182. void formatLabels() override
  183. {
  184. formatSILabels( this, wxT( "\u00B0" ), 3 ); // degree sign
  185. }
  186. };
  187. class VOLTAGE_SCALE_Y : public mpScaleY
  188. {
  189. public:
  190. VOLTAGE_SCALE_Y( wxString name, int flags ) :
  191. mpScaleY( name, flags, false ) {};
  192. void formatLabels() override
  193. {
  194. formatSILabels( this, wxT( "V" ), 3 );
  195. }
  196. };
  197. class CURRENT_SCALE : public mpScaleY
  198. {
  199. public:
  200. CURRENT_SCALE( wxString name, int flags ) :
  201. mpScaleY( name, flags, false ) {};
  202. void formatLabels() override
  203. {
  204. formatSILabels( this, wxT( "A" ), 3 );
  205. }
  206. };
  207. void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
  208. {
  209. if( !m_window )
  210. m_window = &aWindow;
  211. if( !m_visible )
  212. return;
  213. const auto& dataX = m_trace->GetDataX();
  214. const auto& dataY = m_trace->GetDataY();
  215. if( dataX.size() <= 1 )
  216. return;
  217. if( m_updateRequired )
  218. {
  219. m_coords.x = m_trace->s2x( aWindow.p2x( m_dim.x ) );
  220. // Find the closest point coordinates
  221. auto maxXIt = std::upper_bound( dataX.begin(), dataX.end(), m_coords.x );
  222. int maxIdx = maxXIt - dataX.begin();
  223. int minIdx = maxIdx - 1;
  224. // Out of bounds checks
  225. if( minIdx < 0 )
  226. {
  227. minIdx = 0;
  228. maxIdx = 1;
  229. m_coords.x = dataX[0];
  230. }
  231. else if( maxIdx >= (int) dataX.size() )
  232. {
  233. maxIdx = dataX.size() - 1;
  234. minIdx = maxIdx - 1;
  235. m_coords.x = dataX[maxIdx];
  236. }
  237. const double leftX = dataX[minIdx];
  238. const double rightX = dataX[maxIdx];
  239. const double leftY = dataY[minIdx];
  240. const double rightY = dataY[maxIdx];
  241. // Linear interpolation
  242. m_coords.y = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( m_coords.x - leftX );
  243. m_updateRequired = false;
  244. // Notify the parent window about the changes
  245. wxQueueEvent( aWindow.GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
  246. }
  247. else
  248. {
  249. m_updateRef = true;
  250. }
  251. if( m_updateRef )
  252. {
  253. UpdateReference();
  254. m_updateRef = false;
  255. }
  256. // Line length in horizontal and vertical dimensions
  257. const wxPoint cursorPos( aWindow.x2p( m_trace->x2s( m_coords.x ) ),
  258. aWindow.y2p( m_trace->y2s( m_coords.y ) ) );
  259. wxCoord leftPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginLeft();
  260. wxCoord rightPx = m_drawOutsideMargins ? aWindow.GetScrX() : aWindow.GetScrX() - aWindow.GetMarginRight();
  261. wxCoord topPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginTop();
  262. wxCoord bottomPx = m_drawOutsideMargins ? aWindow.GetScrY() : aWindow.GetScrY() - aWindow.GetMarginBottom();
  263. aDC.SetPen( wxPen( *wxWHITE, 1, m_continuous ? wxPENSTYLE_SOLID : wxPENSTYLE_LONG_DASH ) );
  264. if( topPx < cursorPos.y && cursorPos.y < bottomPx )
  265. aDC.DrawLine( leftPx, cursorPos.y, rightPx, cursorPos.y );
  266. if( leftPx < cursorPos.x && cursorPos.x < rightPx )
  267. aDC.DrawLine( cursorPos.x, topPx, cursorPos.x, bottomPx );
  268. }
  269. bool CURSOR::Inside( wxPoint& aPoint )
  270. {
  271. if( !m_window )
  272. return false;
  273. return ( std::abs( (double) aPoint.x - m_window->x2p( m_trace->x2s( m_coords.x ) ) ) <= DRAG_MARGIN )
  274. || ( std::abs( (double) aPoint.y - m_window->y2p( m_trace->y2s( m_coords.y ) ) ) <= DRAG_MARGIN );
  275. }
  276. void CURSOR::UpdateReference()
  277. {
  278. if( !m_window )
  279. return;
  280. m_reference.x = m_window->x2p( m_trace->x2s( m_coords.x ) );
  281. m_reference.y = m_window->y2p( m_trace->y2s( m_coords.y ) );
  282. }
  283. SIM_PLOT_PANEL::SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, wxWindowID id, const wxPoint& pos,
  284. const wxSize& size, long style, const wxString& name )
  285. : mpWindow( parent, id, pos, size, style ), m_colorIdx( 0 ),
  286. m_axis_x( nullptr ), m_axis_y1( nullptr ), m_axis_y2( nullptr ), m_type( aType )
  287. {
  288. LimitView( true );
  289. SetMargins( 50, 80, 50, 80 );
  290. wxColour grey( 130, 130, 130 );
  291. wxColour dark( 10, 10, 10 );
  292. SetColourTheme( dark, *wxWHITE, grey );
  293. EnableDoubleBuffer( true );
  294. UpdateAll();
  295. switch( m_type )
  296. {
  297. case ST_AC:
  298. m_axis_x = new FREQUENCY_LOG_SCALE( _( "Frequency" ), mpALIGN_BOTTOM );
  299. m_axis_y1 = new GAIN_SCALE( _( "Gain" ), mpALIGN_LEFT );
  300. m_axis_y2 = new PHASE_SCALE( _( "Phase" ), mpALIGN_RIGHT );
  301. m_axis_y2->SetMasterScale( m_axis_y1 );
  302. break;
  303. case ST_DC:
  304. m_axis_x = new VOLTAGE_SCALE_X( _( "Voltage (swept)" ), mpALIGN_BOTTOM );
  305. m_axis_y1 = new VOLTAGE_SCALE_Y( _( "Voltage (measured)" ), mpALIGN_LEFT );
  306. m_axis_y2 = new CURRENT_SCALE( _( "Current" ), mpALIGN_RIGHT );
  307. break;
  308. case ST_NOISE:
  309. m_axis_x = new FREQUENCY_LOG_SCALE( _( "Frequency" ), mpALIGN_BOTTOM );
  310. m_axis_y1 = new mpScaleY( _( "noise [(V or A)^2/Hz]" ), mpALIGN_LEFT );
  311. break;
  312. case ST_TRANSIENT:
  313. m_axis_x = new TIME_SCALE( _( "Time" ), mpALIGN_BOTTOM );
  314. m_axis_y1 = new VOLTAGE_SCALE_Y( _( "Voltage" ), mpALIGN_LEFT );
  315. m_axis_y2 = new CURRENT_SCALE( _( "Current" ), mpALIGN_RIGHT );
  316. m_axis_y2->SetMasterScale( m_axis_y1 );
  317. break;
  318. default:
  319. // suppress warnings
  320. break;
  321. }
  322. if( m_axis_x )
  323. {
  324. m_axis_x->SetTicks( false );
  325. m_axis_x->SetNameAlign ( mpALIGN_BOTTOM );
  326. AddLayer( m_axis_x );
  327. }
  328. if( m_axis_y1 )
  329. {
  330. m_axis_y1->SetTicks( false );
  331. m_axis_y1->SetNameAlign ( mpALIGN_LEFT );
  332. AddLayer( m_axis_y1 );
  333. }
  334. if( m_axis_y2 )
  335. {
  336. m_axis_y2->SetTicks( false );
  337. m_axis_y2->SetNameAlign ( mpALIGN_RIGHT );
  338. AddLayer( m_axis_y2 );
  339. }
  340. m_legend = new mpInfoLegend( wxRect( 0, 40, 200, 40 ), wxTRANSPARENT_BRUSH );
  341. m_legend->SetVisible( false );
  342. AddLayer( m_legend );
  343. m_topLevel.push_back( m_legend );
  344. SetColourTheme( dark, *wxWHITE, grey );
  345. EnableDoubleBuffer( true );
  346. UpdateAll();
  347. }
  348. SIM_PLOT_PANEL::~SIM_PLOT_PANEL()
  349. {
  350. // ~mpWindow destroys all the added layers, so there is no need to destroy m_traces contents
  351. }
  352. bool SIM_PLOT_PANEL::IsPlottable( SIM_TYPE aSimType )
  353. {
  354. switch( aSimType )
  355. {
  356. case ST_AC:
  357. case ST_DC:
  358. case ST_TRANSIENT:
  359. return true;
  360. default:
  361. return false;
  362. }
  363. }
  364. void SIM_PLOT_PANEL::UpdateTraceStyle( TRACE* trace )
  365. {
  366. int flags = trace->GetFlags();
  367. wxPenStyle penStyle = ( ( flags & SPT_AC_PHASE || flags & SPT_CURRENT ) && m_dotted_cp ) ?
  368. wxPENSTYLE_DOT :
  369. wxPENSTYLE_SOLID;
  370. trace->SetPen( wxPen( trace->GetTraceColour(), 2, penStyle ) );
  371. }
  372. bool SIM_PLOT_PANEL::AddTrace( const wxString& aName, int aPoints,
  373. const double* aX, const double* aY, SIM_PLOT_TYPE aFlags )
  374. {
  375. TRACE* trace = NULL;
  376. // Find previous entry, if there is one
  377. auto prev = m_traces.find( aName );
  378. bool addedNewEntry = ( prev == m_traces.end() );
  379. if( addedNewEntry )
  380. {
  381. if( m_type == ST_TRANSIENT )
  382. {
  383. bool hasVoltageTraces = false;
  384. for( const auto& tr : m_traces )
  385. {
  386. if( !( tr.second->GetFlags() & SPT_CURRENT ) )
  387. {
  388. hasVoltageTraces = true;
  389. break;
  390. }
  391. }
  392. if( !hasVoltageTraces )
  393. m_axis_y2->SetMasterScale( nullptr );
  394. else
  395. m_axis_y2->SetMasterScale( m_axis_y1 );
  396. }
  397. // New entry
  398. trace = new TRACE( aName );
  399. trace->SetTraceColour( generateColor() );
  400. UpdateTraceStyle( trace );
  401. m_traces[aName] = trace;
  402. // It is a trick to keep legend & coords always on the top
  403. for( mpLayer* l : m_topLevel )
  404. DelLayer( l );
  405. AddLayer( (mpLayer*) trace );
  406. for( mpLayer* l : m_topLevel )
  407. AddLayer( l );
  408. }
  409. else
  410. {
  411. trace = prev->second;
  412. }
  413. std::vector<double> tmp( aY, aY + aPoints );
  414. if( m_type == ST_AC )
  415. {
  416. if( aFlags & SPT_AC_PHASE )
  417. {
  418. for( int i = 0; i < aPoints; i++ )
  419. tmp[i] = tmp[i] * 180.0 / M_PI; // convert to degrees
  420. }
  421. else
  422. {
  423. for( int i = 0; i < aPoints; i++ )
  424. tmp[i] = 20 * log( tmp[i] ) / log( 10.0 ); // convert to dB
  425. }
  426. }
  427. trace->SetData( std::vector<double>( aX, aX + aPoints ), tmp );
  428. if( aFlags & SPT_AC_PHASE || aFlags & SPT_CURRENT )
  429. trace->SetScale( m_axis_x, m_axis_y2 );
  430. else
  431. trace->SetScale( m_axis_x, m_axis_y1 );
  432. trace->SetFlags( aFlags );
  433. UpdateAll();
  434. return addedNewEntry;
  435. }
  436. bool SIM_PLOT_PANEL::DeleteTrace( const wxString& aName )
  437. {
  438. auto it = m_traces.find( aName );
  439. if( it != m_traces.end() )
  440. {
  441. TRACE* trace = it->second;
  442. m_traces.erase( it );
  443. if( CURSOR* cursor = trace->GetCursor() )
  444. DelLayer( cursor, true );
  445. DelLayer( trace, true, true );
  446. ResetScales();
  447. return true;
  448. }
  449. return false;
  450. }
  451. void SIM_PLOT_PANEL::DeleteAllTraces()
  452. {
  453. for( auto& t : m_traces )
  454. {
  455. DeleteTrace( t.first );
  456. }
  457. m_colorIdx = 0;
  458. m_traces.clear();
  459. }
  460. bool SIM_PLOT_PANEL::HasCursorEnabled( const wxString& aName ) const
  461. {
  462. TRACE* t = GetTrace( aName );
  463. return t ? t->HasCursor() : false;
  464. }
  465. void SIM_PLOT_PANEL::EnableCursor( const wxString& aName, bool aEnable )
  466. {
  467. TRACE* t = GetTrace( aName );
  468. if( t == nullptr || t->HasCursor() == aEnable )
  469. return;
  470. if( aEnable )
  471. {
  472. CURSOR* c = new CURSOR( t );
  473. int plotCenter = GetMarginLeft() + ( GetXScreen() - GetMarginLeft() - GetMarginRight() ) / 2;
  474. c->SetX( plotCenter );
  475. t->SetCursor( c );
  476. AddLayer( c );
  477. }
  478. else
  479. {
  480. CURSOR* c = t->GetCursor();
  481. t->SetCursor( NULL );
  482. DelLayer( c, true );
  483. }
  484. // Notify the parent window about the changes
  485. wxQueueEvent( GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
  486. }
  487. void SIM_PLOT_PANEL::ResetScales()
  488. {
  489. if( m_axis_x )
  490. m_axis_x->ResetDataRange();
  491. if( m_axis_y1 )
  492. m_axis_y1->ResetDataRange();
  493. if( m_axis_y2 )
  494. m_axis_y2->ResetDataRange();
  495. for( auto t : m_traces )
  496. t.second->UpdateScales();
  497. }
  498. wxColour SIM_PLOT_PANEL::generateColor()
  499. {
  500. /// @todo have a look at:
  501. /// http://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html
  502. /// https://github.com/Gnuplotting/gnuplot-palettes
  503. // const unsigned long colors[] = { 0x0000ff, 0x00ff00, 0xff0000, 0x00ffff, 0xff00ff, 0xffff000, 0xffffff };
  504. const unsigned long colors[] = { 0xE41A1C, 0x377EB8, 0x4DAF4A, 0x984EA3, 0xFF7F00, 0xFFFF33, 0xA65628, 0xF781BF,
  505. 0x66C2A5, 0xFC8D62, 0x8DA0CB, 0xE78AC3, 0xA6D854, 0xFFD92F, 0xE5C494, 0xB3B3B3 };
  506. //const unsigned long colors[] = { 0xe3cea6, 0xb4781f, 0x8adfb2, 0x2ca033, 0x999afb, 0x1c1ae3, 0x6fbffd, 0x007fff, 0xd6b2ca, 0x9a3d6a };
  507. // hls
  508. //const unsigned long colors[] = { 0x0f1689, 0x0f7289, 0x35890f, 0x0f8945, 0x89260f, 0x890f53, 0x89820f, 0x630f89 };
  509. // pastels, good for dark background
  510. //const unsigned long colors[] = { 0x2fd8fe, 0x628dfa, 0x53d8a6, 0xa5c266, 0xb3b3b3, 0x94c3e4, 0xca9f8d, 0xac680e };
  511. const unsigned int colorCount = sizeof(colors) / sizeof(unsigned long);
  512. for( int i = 0; i < (int)colorCount - 1; i++ )
  513. {
  514. const wxColour color = wxColour( colors[i] );
  515. bool hasColor = false;
  516. for( auto& t : m_traces )
  517. {
  518. TRACE* trace = t.second;
  519. if( trace->GetTraceColour() == color )
  520. {
  521. hasColor = true;
  522. break;
  523. }
  524. }
  525. if( !hasColor )
  526. return color;
  527. }
  528. return wxColour( colors[m_traces.size() % colorCount] );
  529. }
  530. wxDEFINE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );