KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_plot_tab.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016-2023 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#include <wx/tokenzr.h>
25
26#include <bitmaps.h>
28#include "sim_plot_colors.h"
29#include "sim_plot_tab.h"
30#include "simulator_frame.h"
31#include "spice_value.h"
32#include "core/kicad_algo.h"
33
34#include <algorithm>
35#include <cmath>
36#include <functional>
37#include <limits>
38
39#include <wx/dcclient.h>
40
41
42static wxString formatFloat( double x, int nDigits )
43{
44 wxString rv, fmt;
45
46 if( nDigits )
47 fmt.Printf( "%%.0%df", nDigits );
48 else
49 fmt = wxT( "%.0f" );
50
51 rv.Printf( fmt, x );
52
53 return rv;
54}
55
56
57static void getSISuffix( double x, const wxString& unit, int& power, wxString& suffix )
58{
59 const int n_powers = 11;
60
61 const struct
62 {
63 int exponent;
64 char suffix;
65 } powers[] =
66 {
67 { -18, 'a' },
68 { -15, 'f' },
69 { -12, 'p' },
70 { -9, 'n' },
71 { -6, 'u' },
72 { -3, 'm' },
73 { 0, 0 },
74 { 3, 'k' },
75 { 6, 'M' },
76 { 9, 'G' },
77 { 12, 'T' },
78 { 14, 'P' }
79 };
80
81 power = 0;
82 suffix = unit;
83
84 if( x == 0.0 )
85 return;
86
87 for( int i = 0; i < n_powers - 1; i++ )
88 {
89 double r_cur = pow( 10, powers[i].exponent );
90
91 if( fabs( x ) >= r_cur && fabs( x ) < r_cur * 1000.0 )
92 {
93 power = powers[i].exponent;
94
95 if( powers[i].suffix )
96 suffix = wxString( powers[i].suffix ) + unit;
97 else
98 suffix = unit;
99
100 return;
101 }
102 }
103}
104
105
106static int countDecimalDigits( double x, int maxDigits )
107{
108 if( std::isnan( x ) )
109 return 0;
110
111 auto countSignificantDigits =
112 [&]( int64_t k )
113 {
114 while( k && ( k % 10LL ) == 0LL )
115 k /= 10LL;
116
117 int n = 0;
118
119 while( k != 0LL )
120 {
121 n++;
122 k /= 10LL;
123 }
124
125 return n;
126 };
127
128 int64_t k = (int)( ( x - floor( x ) ) * pow( 10.0, (double) maxDigits ) );
129 int n = countSignificantDigits( k );
130
131 // check for trailing 9's
132 n = std::min( n, countSignificantDigits( k + 1 ) );
133
134 return n;
135}
136
137
138template <typename T_PARENT>
139class LIN_SCALE : public T_PARENT
140{
141public:
142 LIN_SCALE( const wxString& name, const wxString& unit, int flags ) :
143 T_PARENT( name, flags, false ),
144 m_unit( unit )
145 {};
146
147 wxString GetUnits() const { return m_unit; }
148
149private:
150 void formatLabels() override
151 {
152 double maxVis = T_PARENT::AbsVisibleMaxValue();
153
154 wxString suffix;
155 int power = 0;
156 int digits = 0;
157 int constexpr MAX_DIGITS = 3;
158 int constexpr MAX_DISAMBIGUATION_DIGITS = 6;
159 bool duplicateLabels = false;
160
161 getSISuffix( maxVis, m_unit, power, suffix );
162
163 double sf = pow( 10.0, power );
164
165 for( mpScaleBase::TICK_LABEL& l : T_PARENT::m_tickLabels )
166 digits = std::max( digits, countDecimalDigits( l.pos / sf, MAX_DIGITS ) );
167
168 do
169 {
170 for( size_t ii = 0; ii < T_PARENT::m_tickLabels.size(); ++ii )
171 {
172 mpScaleBase::TICK_LABEL& l = T_PARENT::m_tickLabels[ii];
173
174 l.label = formatFloat( l.pos / sf, digits );
175 l.visible = true;
176
177 if( ii > 0 && l.label == T_PARENT::m_tickLabels[ii-1].label )
178 duplicateLabels = true;
179 }
180 }
181 while( duplicateLabels && ++digits <= MAX_DISAMBIGUATION_DIGITS );
182
183 if( m_base_axis_label.IsEmpty() )
184 m_base_axis_label = T_PARENT::GetName();
185
186 T_PARENT::SetName( wxString::Format( "%s (%s)", m_base_axis_label, suffix ) );
187 }
188
189private:
190 const wxString m_unit;
192};
193
194
195class TIME_SCALE : public LIN_SCALE<mpScaleX>
196{
197public:
198 TIME_SCALE( const wxString& name, const wxString& unit, int flags ) :
199 LIN_SCALE( name, unit, flags ),
200 m_startTime( 0.0 ),
201 m_endTime( 1.0 )
202 {};
203
204 void ExtendDataRange( double minV, double maxV ) override
205 {
206 LIN_SCALE::ExtendDataRange( minV, maxV );
207
208 // Time is never longer than the simulation itself
209 if( m_minV < m_startTime )
211
212 if( m_maxV > m_endTime )
214 };
215
216 void SetStartAndEnd( double aStartTime, double aEndTime )
217 {
218 m_startTime = aStartTime;
219 m_endTime = aEndTime;
221 }
222
223 void ResetDataRange() override
224 {
227 m_rangeSet = true;
228 }
229
230protected:
232 double m_endTime;
233};
234
235
236template <typename T_PARENT>
237class LOG_SCALE : public T_PARENT
238{
239public:
240 LOG_SCALE( const wxString& name, const wxString& unit, int flags ) :
241 T_PARENT( name, flags, false ),
242 m_unit( unit )
243 {};
244
245 wxString GetUnits() const { return m_unit; }
246
247private:
248 void formatLabels() override
249 {
250 wxString suffix;
251 int power;
252 int constexpr MAX_DIGITS = 3;
253
254 for( mpScaleBase::TICK_LABEL& l : T_PARENT::m_tickLabels )
255 {
256 getSISuffix( l.pos, m_unit, power, suffix );
257 double sf = pow( 10.0, power );
258 int k = countDecimalDigits( l.pos / sf, MAX_DIGITS );
259
260 l.label = formatFloat( l.pos / sf, k ) + suffix;
261 l.visible = true;
262 }
263 }
264
265private:
266 const wxString m_unit;
267};
268
269
270bool SMITH_GRID::GetChartView( mpWindow& aWindow, double aZoom, const wxRealPoint& aPan, SMITH_VIEW& aView )
271{
272 int mL = aWindow.GetMarginLeft(), mT = aWindow.GetMarginTop();
273 int plotW = aWindow.GetScrX() - mL - aWindow.GetMarginRight();
274 int plotH = aWindow.GetScrY() - mT - aWindow.GetMarginBottom();
275 int radius = std::min( plotW, plotH ) / 2 - 15;
276
277 if( radius <= 20 )
278 return false;
279
280 aView.center = wxPoint( mL + plotW / 2, mT + plotH / 2 );
281 aView.radius = radius * aZoom;
282 aView.zoom = aZoom;
283 aView.pan = aPan;
284 aView.plotRect = wxRect( mL, mT, plotW, plotH );
285
286 return true;
287}
288
289
290static bool smithView( mpWindow& aWindow, SMITH_VIEW& aView )
291{
292 // the layers of a Smith chart are drawn on the SIM_VIEW carrying its own pan and zoom
293 SIM_VIEW* view = dynamic_cast<SIM_VIEW*>( &aWindow );
294 double zoom = view ? view->GetSmithZoom() : 1.0;
295 wxRealPoint pan = view ? view->GetSmithPan() : wxRealPoint( 0.0, 0.0 );
296
297 return SMITH_GRID::GetChartView( aWindow, zoom, pan, aView );
298}
299
300
301void SMITH_GRID::Plot( wxDC& aDC, mpWindow& aWindow )
302{
303 if( !m_visible )
304 return;
305
306 SMITH_VIEW view;
307
308 if( !smithView( aWindow, view ) )
309 return;
310
311 const wxRect& plotRect = view.plotRect;
312 int mL = plotRect.x, mT = plotRect.y, plotW = plotRect.width, plotH = plotRect.height;
313
314 aDC.SetClippingRegion( plotRect );
315
316 // enough segments to keep the chord error under a pixel at this circle's screen radius
317 auto segmentsFor =
318 [&]( double aR ) -> int
319 {
320 return std::clamp( KiROUND( M_PI * std::sqrt( aR * view.radius ) ), 64, 4096 );
321 };
322
323 // draw a gamma-plane circle, keeping only the parts inside the unit circle
324 auto drawClippedCircle =
325 [&]( double aCx, double aCy, double aR )
326 {
327 constexpr double LIMIT = 1.0002;
328
329 std::vector<wxPoint> run;
330 int segments = segmentsFor( aR );
331
332 for( int ii = 0; ii <= segments; ii++ )
333 {
334 double angle = 2.0 * M_PI * ii / segments;
335 double re = aCx + aR * cos( angle );
336 double im = aCy + aR * sin( angle );
337
338 if( re * re + im * im <= LIMIT )
339 {
340 run.push_back( view.ToScreen( re, im ) );
341 }
342 else
343 {
344 if( run.size() > 1 )
345 aDC.DrawLines( (int) run.size(), run.data() );
346
347 run.clear();
348 }
349 }
350
351 if( run.size() > 1 )
352 aDC.DrawLines( (int) run.size(), run.data() );
353 };
354
355 auto formatValue =
356 []( double aValue ) -> wxString
357 {
358 return wxString::Format( wxS( "%g" ), aValue );
359 };
360
361 // ohm labels for a single reference impedance, normalized labels without one
362 double labelScale = m_z0 > 0.0 ? m_z0 : 1.0;
363
364 static const std::vector<double> baseVals = { 0.2, 0.5, 1.0, 2.0, 5.0 };
365 std::vector<double> gridVals = baseVals;
366
367 constexpr double FINEST_SCALE = 2.0 / ( 1.0 + 20.0 );
368
369 wxRealPoint lowGamma = view.ToGamma( wxPoint( mL, mT + plotH ) );
370 wxRealPoint highGamma = view.ToGamma( wxPoint( mL + plotW, mT ) );
371
372 auto poleDistance =
373 [&]( double aRe )
374 {
375 return std::hypot( std::max( { lowGamma.x - aRe, aRe - highGamma.x, 0.0 } ),
376 std::max( { lowGamma.y, -highGamma.y, 0.0 } ) );
377 };
378
379 bool zoomedIn = ( plotW / 2.0 ) / view.radius < 1.0;
380 bool converged = zoomedIn && std::min( poleDistance( 1.0 ), poleDistance( -1.0 ) ) < FINEST_SCALE;
381
382 if( view.zoom >= 2.0 && !converged )
383 gridVals.insert( gridVals.end(), { 0.1, 0.3, 0.4, 0.7, 1.5, 3.0, 10.0 } );
384
385 if( view.zoom >= 5.0 && !converged )
386 gridVals.insert( gridVals.end(), { 0.05, 0.15, 0.6, 0.8, 1.2, 1.7, 2.5, 4.0, 7.0, 20.0 } );
387
388 wxPen gridPen = m_pen;
389 gridPen.SetStyle( wxPENSTYLE_DOT );
390
391 aDC.SetBrush( *wxTRANSPARENT_BRUSH );
392 aDC.SetFont( GetPlotFont() );
393 aDC.SetTextForeground( m_pen.GetColour() );
394 aDC.SetPen( gridPen );
395
396 // constant resistance circles, centered on the real axis, tangent at gamma = 1
397 for( double r : gridVals )
398 drawClippedCircle( r / ( 1.0 + r ), 0.0, 1.0 / ( 1.0 + r ) );
399
400 // constant reactance arcs, one per sign, clipped to the unit circle
401 for( double x : gridVals )
402 {
403 drawClippedCircle( 1.0, 1.0 / x, 1.0 / x );
404 drawClippedCircle( 1.0, -1.0 / x, 1.0 / x );
405 }
406
407 aDC.SetPen( m_pen );
408 aDC.DrawLine( view.ToScreen( -1.0, 0.0 ), view.ToScreen( 1.0, 0.0 ) );
409 aDC.DrawCircle( view.ToScreen( 0.0, 0.0 ), KiROUND( view.radius ) );
410
411 // Label each gridline at its axis/rim anchor, or at the plot edge when the anchor is
412 // panned/zoomed out of view.
413 constexpr double LABEL_LIMIT = 1.0002;
414
415 auto anchorPos =
416 [&]( double aRe, double aIm, wxPoint& aOut ) -> bool
417 {
418 if( aRe * aRe + aIm * aIm > LABEL_LIMIT )
419 return false;
420
421 wxPoint p = view.ToScreen( aRe, aIm );
422
423 if( !plotRect.Contains( p ) )
424 return false;
425
426 aOut = p;
427 return true;
428 };
429
430 // anchor off-screen, put the label where the gridline meets the plot edge
431 auto edgePos =
432 [&]( double aCx, double aCy, double aR, wxPoint& aOut ) -> bool
433 {
434 int bestMargin = std::numeric_limits<int>::max();
435 bool found = false;
436 int segments = segmentsFor( aR );
437
438 for( int ii = 0; ii <= segments; ii++ )
439 {
440 double angle = 2.0 * M_PI * ii / segments;
441 double re = aCx + aR * cos( angle );
442 double im = aCy + aR * sin( angle );
443
444 if( re * re + im * im > LABEL_LIMIT )
445 continue;
446
447 wxPoint p = view.ToScreen( re, im );
448
449 if( !plotRect.Contains( p ) )
450 continue;
451
452 int margin = std::min( { p.x - mL, mL + plotW - p.x, p.y - mT, mT + plotH - p.y } );
453
454 if( margin < bestMargin )
455 {
456 bestMargin = margin;
457 aOut = p;
458 found = true;
459 }
460 }
461
462 return found;
463 };
464
465 auto clampToPlot =
466 [&]( const wxPoint& aPos, const wxSize& aExt ) -> wxPoint
467 {
468 return wxPoint( std::clamp( aPos.x, mL + 1, mL + plotW - aExt.x - 1 ),
469 std::clamp( aPos.y, mT + 1, mT + plotH - aExt.y - 1 ) );
470 };
471
472 std::vector<wxRect> placed;
473
474 auto drawLabel =
475 [&]( const wxString& aLabel, const wxPoint& aPos, const wxSize& aExt )
476 {
477 wxPoint pos = clampToPlot( aPos, aExt );
478 wxRect box( pos, aExt );
479
480 box.Inflate( 2, 1 );
481
482 for( const wxRect& seen : placed )
483 {
484 if( seen.Intersects( box ) )
485 return;
486 }
487
488 placed.push_back( box );
489 aDC.DrawText( aLabel, pos );
490 };
491
492 auto drawEdgeLabel =
493 [&]( const wxString& aLabel, const wxPoint& aAt )
494 {
495 wxSize ext = aDC.GetTextExtent( aLabel );
496
497 drawLabel( aLabel, wxPoint( aAt.x - ext.x / 2, aAt.y + 3 ), ext );
498 };
499
500 // push reactance labels just outside the rim, clamped so j50 and -j50 are not clipped
501 auto drawRimLabel = [&]( const wxString& aLabel, const wxPoint& aAt, double aRe, double aIm )
502 {
503 wxSize ext = aDC.GetTextExtent( aLabel );
504 wxPoint pos = aAt;
505
506 pos.x += KiROUND( aRe * 6 );
507 pos.y -= KiROUND( aIm * 6 );
508 pos.x -= KiROUND( ext.x * ( 1.0 - aRe ) / 2.0 );
509 pos.y -= KiROUND( ext.y * ( 1.0 + aIm ) / 2.0 );
510
511 drawLabel( aLabel, pos, ext );
512 };
513
514 wxPoint at;
515
516 // short (r = 0)
517 if( anchorPos( -1.0, 0.0, at ) )
518 drawRimLabel( wxS( "0" ), at, -1.0, 0.0 );
519 else if( edgePos( 0.0, 0.0, 1.0, at ) )
520 drawEdgeLabel( wxS( "0" ), at );
521
522 for( double r : gridVals )
523 {
524 if( anchorPos( ( r - 1.0 ) / ( r + 1.0 ), 0.0, at ) || edgePos( r / ( 1.0 + r ), 0.0, 1.0 / ( 1.0 + r ), at ) )
525 {
526 drawEdgeLabel( formatValue( r * labelScale ), at );
527 }
528 }
529
530 for( double x : gridVals )
531 {
532 double d = x * x + 1.0;
533 double re = ( x * x - 1.0 ) / d;
534 double im = 2.0 * x / d;
535 wxString posLabel = wxS( "j" ) + formatValue( x * labelScale );
536 wxString negLabel = wxS( "-j" ) + formatValue( x * labelScale );
537
538 if( anchorPos( re, im, at ) )
539 drawRimLabel( posLabel, at, re, im );
540 else if( edgePos( 1.0, 1.0 / x, 1.0 / x, at ) )
541 drawEdgeLabel( posLabel, at );
542
543 if( anchorPos( re, -im, at ) )
544 drawRimLabel( negLabel, at, re, -im );
545 else if( edgePos( 1.0, -1.0 / x, 1.0 / x, at ) )
546 drawEdgeLabel( negLabel, at );
547 }
548
549 // the ohm labels mean nothing unless the reference impedance is named
550 wxString note;
551
552 if( m_z0 > 0.0 )
553 note = wxString::Format( wxS( "Z0 = %s Ω" ), formatValue( m_z0 ) );
554 else if( m_mixedReferences )
555 note = _( "Normalized Z/Z0 (ports differ)" );
556 else
557 note = _( "Normalized Z/Z0" );
558
559 wxSize ext = aDC.GetTextExtent( note );
560
561 aDC.DrawText( note, mL + 4, mT + plotH - ext.y - 4 );
562
563 aDC.DestroyClippingRegion();
564}
565
566
567void SMITH_TRACE::Plot( wxDC& aDC, mpWindow& aWindow )
568{
569 if( !m_visible )
570 return;
571
572 SMITH_VIEW view;
573
574 if( !smithView( aWindow, view ) )
575 return;
576
577 const std::vector<double>& xs = GetDataX();
578 const std::vector<double>& ys = GetDataY();
579 size_t count = std::min( xs.size(), ys.size() );
580
581 if( count == 0 )
582 return;
583
584 aDC.SetPen( m_pen );
585 aDC.SetClippingRegion( view.plotRect );
586
587 size_t chunk = GetSweepSize();
588
589 if( GetSweepCount() <= 1 || chunk == std::numeric_limits<size_t>::max() || chunk == 0 )
590 chunk = count;
591
592 std::vector<wxPoint> pts;
593
594 auto flush =
595 [&]()
596 {
597 if( pts.size() > 1 )
598 {
599 aDC.DrawLines( (int) pts.size(), pts.data() );
600 }
601 else if( pts.size() == 1 )
602 {
603 aDC.SetBrush( wxBrush( m_pen.GetColour() ) );
604 aDC.DrawCircle( pts[0], 2 );
605 }
606
607 pts.clear();
608 };
609
610 for( size_t start = 0; start < count; start += chunk )
611 {
612 size_t end = std::min( count, start + chunk );
613
614 for( size_t ii = start; ii < end; ii++ )
615 {
616 // a non-finite sample breaks the locus rather than drawing a bogus segment
617 if( !std::isfinite( xs[ii] ) || !std::isfinite( ys[ii] ) )
618 {
619 flush();
620 continue;
621 }
622
623 pts.emplace_back( view.ToScreen( xs[ii], ys[ii] ) );
624 }
625
626 flush();
627 }
628
629 aDC.DestroyClippingRegion();
630}
631
632
634{
635 const std::vector<double>& re = m_trace->GetDataX();
636 const std::vector<double>& im = m_trace->GetDataY();
637 const std::vector<double>& freqs = static_cast<SMITH_TRACE*>( m_trace )->GetFrequencies();
638
639 size_t count = std::min( re.size(), im.size() );
640
641 if( count == 0 )
642 return;
643
644 m_index = std::clamp( aIndex, 0, (int) count - 1 );
645 m_gamma = wxRealPoint( re[m_index], im[m_index] );
646
647 // no frequency data for this sample, keep the previous x so a saved position stays finite
648 double freq = m_index < (int) freqs.size() ? freqs[m_index] : m_coords.x;
649
650 m_coords = wxRealPoint( freq, std::hypot( m_gamma.x, m_gamma.y ) );
651}
652
653
655{
656 const std::vector<double>& freqs = static_cast<SMITH_TRACE*>( m_trace )->GetFrequencies();
657 const std::vector<double>& re = m_trace->GetDataX();
658 const std::vector<double>& im = m_trace->GetDataY();
659
660 // frequencies repeat identically per run, search only the run the cursor is on
661 // so a frequency-keyed move cannot silently hop to run 0
662 size_t begin = 0;
663 size_t end = freqs.size();
664 size_t chunk = m_trace->GetSweepSize();
665
666 if( m_trace->GetSweepCount() > 1
667 && chunk > 0
668 && chunk != std::numeric_limits<size_t>::max()
669 && m_index >= 0
670 && (size_t) m_index < freqs.size() )
671 {
672 begin = ( (size_t) m_index / chunk ) * chunk;
673 end = std::min( freqs.size(), begin + chunk );
674 }
675
676 int best = -1;
677 double bestDist = std::numeric_limits<double>::max();
678
679 for( size_t ii = begin; ii < end; ii++ )
680 {
681 if( !std::isfinite( freqs[ii] ) )
682 continue;
683
684 if( ii < re.size() && ii < im.size() && ( !std::isfinite( re[ii] ) || !std::isfinite( im[ii] ) ) )
685 continue;
686
687 double dist = std::fabs( freqs[ii] - aFreq );
688
689 if( dist < bestDist )
690 {
691 bestDist = dist;
692 best = (int) ii;
693 }
694 }
695
696 if( best >= 0 )
697 snapToIndex( best );
698}
699
700
701void SMITH_CURSOR::SetCoordX( double aValue )
702{
703 m_requestFreq = aValue;
704
706
707 if( m_window )
708 m_window->Refresh();
709}
710
711
713{
714 if( static_cast<SMITH_TRACE*>( m_trace )->GetFrequencies().empty() )
715 {
716 // no data yet, remember the frequency and resolve it once the sim fills in
718 m_pendingFreq = true;
719 m_updateRequired = false;
720 return;
721 }
722
724 m_pendingFreq = false;
725 m_updateRequired = false;
726 m_updateRef = true;
727}
728
729
730void SMITH_CURSOR::Move( wxPoint aDelta )
731{
732 m_dragging = true;
733 Update();
734 mpInfoLayer::Move( aDelta );
735}
736
737
739{
740 // skip CURSOR's axis reference, the marker follows the locus
742}
743
744
745bool SMITH_CURSOR::Inside( const wxPoint& aPoint ) const
746{
747 if( !m_window || m_index < 0 )
748 return false;
749
750 SMITH_VIEW view;
751
752 if( !smithView( *m_window, view ) )
753 return false;
754
755 wxPoint marker = view.ToScreen( m_gamma.x, m_gamma.y );
756
757 return std::abs( aPoint.x - marker.x ) <= DRAG_MARGIN && std::abs( aPoint.y - marker.y ) <= DRAG_MARGIN;
758}
759
760
761void SMITH_CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
762{
763 if( !m_window )
764 m_window = &aWindow;
765
766 if( !m_visible )
767 return;
768
769 SMITH_VIEW view;
770
771 if( !smithView( aWindow, view ) )
772 return;
773
774 const std::vector<double>& re = m_trace->GetDataX();
775 const std::vector<double>& im = m_trace->GetDataY();
776 size_t count = std::min( re.size(), im.size() );
777
778 if( count == 0 )
779 return;
780
781 if( m_pendingFreq )
782 {
783 // sim data has arrived, restore the frequency saved from the workbook
785 m_pendingFreq = false;
786 m_updateRequired = false;
787 m_updateRef = true;
788 }
789 else if( m_updateRequired )
790 {
791 if( m_dragging )
792 {
793 // snap to the locus sample closest to the drag position
794 int best = -1;
795 double bestDist = std::numeric_limits<double>::max();
796
797 for( size_t ii = 0; ii < count; ii++ )
798 {
799 if( !std::isfinite( re[ii] ) || !std::isfinite( im[ii] ) )
800 continue;
801
802 wxPoint p = view.ToScreen( re[ii], im[ii] );
803 double dx = (double) p.x - m_dim.x;
804 double dy = (double) p.y - m_dim.y;
805 double dist = dx * dx + dy * dy;
806
807 if( dist < bestDist )
808 {
809 bestDist = dist;
810 best = (int) ii;
811 }
812 }
813
814 if( best >= 0 )
815 {
816 snapToIndex( best );
818 }
819
820 m_dragging = false;
821 }
822 else
823 {
824 // the trace data changed under the cursor, follow the frequency rather than
825 // the screen position so a re-run cannot hop to another point of the locus
827 }
828
829 m_updateRequired = false;
830
831 // Notify the parent window about the changes
832 wxQueueEvent( aWindow.GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
833 }
834 else
835 {
836 if( m_index < 0 )
837 {
838 snapToIndex( (int) count / 2 );
840 }
841
842 m_updateRef = true;
843 }
844
845 wxPoint marker = view.ToScreen( m_gamma.x, m_gamma.y );
846
847 m_dim.SetX( marker.x );
848 m_dim.SetY( marker.y );
849
850 if( m_updateRef )
851 {
853 m_updateRef = false;
854 }
855
856 wxPen pen = GetPen();
857 wxColour fg = aWindow.GetForegroundColour();
858 COLOR4D cursorColor = COLOR4D( m_trace->GetTraceColour() ).Mix( fg, 0.6 );
859
860 pen.SetColour( cursorColor.ToColour() );
861 pen.SetStyle( wxPENSTYLE_SOLID );
862 aDC.SetPen( pen );
863 aDC.SetBrush( *wxTRANSPARENT_BRUSH );
864
865 aDC.DrawCircle( marker, 4 );
866 aDC.DrawLine( marker.x - 8, marker.y, marker.x - 4, marker.y );
867 aDC.DrawLine( marker.x + 4, marker.y, marker.x + 8, marker.y );
868 aDC.DrawLine( marker.x, marker.y - 8, marker.x, marker.y - 4 );
869 aDC.DrawLine( marker.x, marker.y + 4, marker.x, marker.y + 8 );
870
871 double gm = std::hypot( m_gamma.x, m_gamma.y );
872 double z0 = static_cast<SMITH_TRACE*>( m_trace )->GetReferenceImpedance();
873 double freq = m_coords.x;
874 double zr, zi;
875
876 auto formatSI = []( double aValue, const wxString& aUnit ) -> wxString
877 {
878 if( std::isnan( aValue ) )
879 return wxS( "--" );
880
881 int power = 0;
882 wxString suffix;
883
884 getSISuffix( aValue, aUnit, power, suffix );
885
886 double sf = pow( 10.0, power );
887
888 return formatFloat( aValue / sf, 3 ) + wxS( " " ) + suffix;
889 };
890
891 std::vector<wxString> lines;
892
893 lines.push_back( getID() + wxS( ": f = " ) + formatSI( freq, wxS( "Hz" ) ) );
894
895 // ohms need the port impedance, without one only the normalized z is known
896 bool absolute = z0 > 0.0;
897
898 if( !SMITH_MATH::GammaToImpedance( m_gamma.x, m_gamma.y, absolute ? z0 : 1.0, zr, zi ) )
899 {
900 lines.push_back( wxS( "Z = inf" ) );
901 }
902 else if( absolute )
903 {
904 lines.push_back( wxString::Format( wxS( "Z = %s %s j%s" ),
905 formatSI( zr, wxS( "Ω" ) ),
906 zi < 0 ? wxS( "-" ) : wxS( "+" ),
907 formatSI( std::fabs( zi ), wxS( "Ω" ) ) ) );
908
909 // series equivalent of the reactance at the marker frequency
910 if( std::isfinite( freq ) && freq > 0.0 && zi != 0.0 )
911 {
912 if( zi > 0.0 )
913 lines.push_back( wxS( "L = " ) + formatSI( SMITH_MATH::SeriesInductance( zi, freq ), wxS( "H" ) ) );
914 else
915 lines.push_back( wxS( "C = " ) + formatSI( SMITH_MATH::SeriesCapacitance( zi, freq ), wxS( "F" ) ) );
916 }
917 }
918 else
919 {
920 lines.push_back( wxString::Format( wxS( "z = %s %s j%s" ),
921 formatFloat( zr, 3 ),
922 zi < 0 ? wxS( "-" ) : wxS( "+" ),
923 formatFloat( std::fabs( zi ), 3 ) ) );
924 }
925
926 double rl = SMITH_MATH::ReturnLoss( gm );
927 double vswr = SMITH_MATH::VSWR( gm );
928
929 if( std::isfinite( rl ) )
930 lines.push_back( wxString::Format( wxS( "RL = %s dB" ), formatFloat( rl, 1 ) ) );
931 else
932 lines.push_back( wxS( "RL = inf" ) );
933
934 if( std::isfinite( vswr ) )
935 lines.push_back( wxString::Format( wxS( "VSWR = %s" ), formatFloat( vswr, 2 ) ) );
936 else
937 lines.push_back( wxS( "VSWR = inf" ) );
938
939 aDC.SetFont( GetPlotFont() );
940
941 int boxW = 0;
942 int boxH = 0;
943 int lineH = aDC.GetTextExtent( wxS( "M" ) ).y;
944
945 for( const wxString& line : lines )
946 boxW = std::max( boxW, aDC.GetTextExtent( line ).x );
947
948 boxW += 8;
949 boxH = (int) lines.size() * lineH + 6;
950
951 wxPoint boxPos( marker.x + ( marker.x < aWindow.GetScrX() / 2 ? 12 : -12 - boxW ),
952 marker.y + ( marker.y < aWindow.GetScrY() / 2 ? 12 : -12 - boxH ) );
953
954 boxPos.x = std::clamp( boxPos.x, 0, std::max( 0, aWindow.GetScrX() - boxW ) );
955 boxPos.y = std::clamp( boxPos.y, 0, std::max( 0, aWindow.GetScrY() - boxH ) );
956
957 wxBrush labelBrush( aWindow.GetBackgroundColour() );
958
959 aDC.SetBrush( labelBrush );
960 aDC.DrawRectangle( wxRect( boxPos, wxSize( boxW, boxH ) ) );
961 aDC.SetTextForeground( cursorColor.ToColour() );
962
963 for( size_t ii = 0; ii < lines.size(); ii++ )
964 aDC.DrawText( lines[ii], boxPos.x + 4, boxPos.y + 3 + (int) ii * lineH );
965}
966
967
968void CURSOR::SetCoordX( double aValue )
969{
970 wxRealPoint oldCoords = m_coords;
971
972 doSetCoordX( aValue );
973 m_updateRequired = false;
974 m_updateRef = true;
975
976 if( m_window )
977 {
978 wxRealPoint delta = m_coords - oldCoords;
979 mpInfoLayer::Move( wxPoint( m_window->x2p( m_trace->x2s( delta.x ) ),
980 m_window->y2p( m_trace->y2s( delta.y ) ) ) );
981
982 m_window->Refresh();
983 }
984}
985
986
987void CURSOR::Move( wxPoint aDelta )
988{
989 Update();
990
991 if( m_trace->IsMultiRun() && m_window
992 && m_trace->GetSweepCount() > 1
993 && m_trace->GetSweepSize() != std::numeric_limits<size_t>::max() )
994 {
995 int newY = m_reference.y + aDelta.y;
996
997 double plotY = m_window->p2y( newY );
998 m_snapTargetY = m_trace->s2y( plotY );
999 m_snapToNearest = true;
1000 }
1001
1002 mpInfoLayer::Move( aDelta );
1003}
1004
1005
1006bool CURSOR::OnDoubleClick( const wxPoint& aPoint, mpWindow& aWindow )
1007{
1008 if( !Inside( aPoint ) )
1009 return false;
1010
1011 if( !m_trace->IsMultiRun() )
1012 return false;
1013
1014 int sweepCount = m_trace->GetSweepCount();
1015 size_t sweepSize = m_trace->GetSweepSize();
1016
1017 if( sweepCount <= 1 )
1018 return false;
1019
1020 if( sweepSize == std::numeric_limits<size_t>::max() || sweepSize == 0 )
1021 return false;
1022
1023 if( m_sweepIndex < 0 || m_sweepIndex >= sweepCount )
1024 m_sweepIndex = 0;
1025
1026 m_sweepIndex = ( m_sweepIndex + 1 ) % sweepCount;
1027
1028 Update();
1029 m_updateRef = true;
1030 m_window = &aWindow;
1031 aWindow.Refresh();
1032
1033 return true;
1034}
1035
1036
1037void CURSOR::doSetCoordX( double aValue )
1038{
1039 m_coords.x = aValue;
1040
1041 const std::vector<double>& dataX = m_trace->GetDataX();
1042 const std::vector<double>& dataY = m_trace->GetDataY();
1043
1044 if( dataX.size() <= 1 )
1045 return;
1046
1047 bool snapToNearest = m_snapToNearest;
1048 double snapTargetY = m_snapTargetY;
1049 m_snapToNearest = false;
1050
1051 size_t startIdx = 0;
1052 size_t endIdx = dataX.size();
1053 int sweepCount = m_trace->GetSweepCount();
1054 size_t sweepSize = m_trace->GetSweepSize();
1055
1056 if( snapToNearest && m_trace->IsMultiRun()
1057 && sweepCount > 1
1058 && sweepSize != std::numeric_limits<size_t>::max()
1059 && sweepSize > 0
1060 && std::isfinite( snapTargetY ) )
1061 {
1062 double bestDistance = std::numeric_limits<double>::infinity();
1063 int bestSweep = m_sweepIndex;
1064 bool found = false;
1065
1066 for( int sweepIdx = 0; sweepIdx < sweepCount; ++sweepIdx )
1067 {
1068 size_t candidateStart = static_cast<size_t>( sweepIdx ) * sweepSize;
1069 size_t candidateEnd = std::min( dataX.size(), candidateStart + sweepSize );
1070
1071 if( candidateStart >= candidateEnd )
1072 continue;
1073
1074 auto candidateBegin = dataX.begin() + candidateStart;
1075 auto candidateEndIt = dataX.begin() + candidateEnd;
1076 auto candidateMaxIt = std::upper_bound( candidateBegin, candidateEndIt, m_coords.x );
1077 int candidateMaxIdx = candidateMaxIt - dataX.begin();
1078 int candidateMinIdx = candidateMaxIdx - 1;
1079
1080 if( candidateMinIdx < (int) candidateStart
1081 || candidateMaxIdx >= (int) candidateEnd
1082 || candidateMaxIdx >= (int) dataX.size() )
1083 {
1084 continue;
1085 }
1086
1087 double leftX = dataX[candidateMinIdx];
1088 double rightX = dataX[candidateMaxIdx];
1089
1090 if( leftX == rightX )
1091 continue;
1092
1093 double leftY = dataY[candidateMinIdx];
1094 double rightY = dataY[candidateMaxIdx];
1095 double value = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( m_coords.x - leftX );
1096 double distance = std::fabs( value - snapTargetY );
1097
1098 if( distance < bestDistance )
1099 {
1100 bestDistance = distance;
1101 bestSweep = sweepIdx;
1102 found = true;
1103 }
1104 }
1105
1106 if( found )
1107 m_sweepIndex = bestSweep;
1108 }
1109
1110 if( m_trace->IsMultiRun()
1111 && sweepCount > 1
1112 && sweepSize != std::numeric_limits<size_t>::max()
1113 && sweepSize > 0 )
1114 {
1115 size_t available = static_cast<size_t>( sweepCount ) * sweepSize;
1116
1117 if( available <= dataX.size() )
1118 {
1119 if( m_sweepIndex < 0 || m_sweepIndex >= sweepCount )
1120 m_sweepIndex = std::max( sweepCount - 1, 0 );
1121
1122 startIdx = static_cast<size_t>( m_sweepIndex ) * sweepSize;
1123 endIdx = std::min( dataX.size(), startIdx + sweepSize );
1124 }
1125 else
1126 {
1127 m_sweepIndex = 0;
1128 }
1129 }
1130 else
1131 {
1132 m_sweepIndex = 0;
1133 }
1134
1135 if( startIdx >= endIdx )
1136 {
1137 m_coords.y = NAN;
1138 return;
1139 }
1140
1141 auto beginIt = dataX.begin() + startIdx;
1142 auto endIt = dataX.begin() + endIdx;
1143
1144 // Find the closest point coordinates
1145 auto maxXIt = std::upper_bound( beginIt, endIt, m_coords.x );
1146 int maxIdx = maxXIt - dataX.begin();
1147 int minIdx = maxIdx - 1;
1148
1149 // Out of bounds checks
1150 if( minIdx < (int) startIdx || maxIdx >= (int) endIdx || maxIdx >= (int) dataX.size() )
1151 {
1152 // Simulation may not be complete yet, or we may have a cursor off the beginning or end
1153 // of the data. Either way, that's where the user put it. Don't second guess them; just
1154 // leave its y value undefined.
1155 m_coords.y = NAN;
1156 return;
1157 }
1158
1159 const double leftX = dataX[minIdx];
1160 const double rightX = dataX[maxIdx];
1161 const double leftY = dataY[minIdx];
1162 const double rightY = dataY[maxIdx];
1163
1164 // Linear interpolation
1165 m_coords.y = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( m_coords.x - leftX );
1166}
1167
1168
1170{
1171 for( const auto& [ id, cursor ] : m_trace->GetCursors() )
1172 {
1173 if( cursor == this )
1174 return wxString::Format( _( "%d" ), id );
1175 }
1176
1177 return wxEmptyString;
1178}
1179
1180
1181bool TRACE::GetValueAtX( double aX, double* aY ) const
1182{
1183 size_t count = std::min( m_xs.size(), m_ys.size() );
1184
1185 if( count <= 1 )
1186 return false;
1187
1188 size_t startIdx = 0;
1189 size_t endIdx = count;
1190 size_t sweepSize = GetSweepSize();
1191
1192 if( m_isMultiRun && GetSweepCount() > 1 && sweepSize != std::numeric_limits<size_t>::max() && sweepSize > 0 )
1193 {
1194 // Runs are concatenated one after another; only show the most recent one. It may still
1195 // be coming in, so start at its first sample rather than counting a whole run back from
1196 // the end, which would straddle the previous run.
1197 startIdx = std::min( static_cast<size_t>( GetSweepCount() - 1 ) * sweepSize,
1198 ( count - 1 ) / sweepSize * sweepSize );
1199 endIdx = std::min( count, startIdx + sweepSize );
1200 }
1201
1202 if( endIdx - startIdx < 2 )
1203 return false;
1204
1205 auto beginIt = m_xs.begin() + startIdx;
1206 auto endIt = m_xs.begin() + endIdx;
1207
1208 // a DC sweep can step downwards, in which case X is sorted the other way round
1209 auto maxXIt = m_xs[endIdx - 1] < m_xs[startIdx] ? std::upper_bound( beginIt, endIt, aX, std::greater<double>() )
1210 : std::upper_bound( beginIt, endIt, aX );
1211
1212 if( maxXIt == beginIt || maxXIt == endIt )
1213 return false;
1214
1215 size_t maxIdx = maxXIt - m_xs.begin();
1216 size_t minIdx = maxIdx - 1;
1217
1218 const double leftX = m_xs[minIdx];
1219 const double rightX = m_xs[maxIdx];
1220 const double leftY = m_ys[minIdx];
1221 const double rightY = m_ys[maxIdx];
1222
1223 if( leftX == rightX )
1224 return false;
1225
1226 *aY = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( aX - leftX );
1227
1228 // a magnitude of zero plots as NaN, there is no value to read out
1229 return std::isfinite( *aY );
1230}
1231
1232
1233void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
1234{
1235 if( !m_window )
1236 m_window = &aWindow;
1237
1238 if( !m_visible || m_trace->GetDataX().size() <= 1 )
1239 return;
1240
1241 if( m_updateRequired )
1242 {
1243 doSetCoordX( m_trace->s2x( aWindow.p2x( m_dim.x ) ) );
1244 m_updateRequired = false;
1245
1246 // Notify the parent window about the changes
1247 wxQueueEvent( aWindow.GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
1248 }
1249 else
1250 {
1251 m_updateRef = true;
1252 }
1253
1254 if( m_updateRef )
1255 {
1257 m_updateRef = false;
1258 }
1259
1260 if( !std::isfinite( m_coords.x ) )
1261 return;
1262
1263 // A silent trace interpolates to no y value at all, and converting that to a pixel is
1264 // undefined behaviour, so carry the x cursor on its own
1265 const bool hasY = std::isfinite( m_coords.y );
1266
1267 // Line length in horizontal and vertical dimensions
1268 const wxPoint cursorPos( aWindow.x2p( m_trace->x2s( m_coords.x ) ),
1269 hasY ? aWindow.y2p( m_trace->y2s( m_coords.y ) ) : 0 );
1270
1271 wxCoord leftPx = aWindow.GetMarginLeft();
1272 wxCoord rightPx = aWindow.GetScrX() - aWindow.GetMarginRight();
1273 wxCoord topPx = aWindow.GetMarginTop();
1274 wxCoord bottomPx = aWindow.GetScrY() - aWindow.GetMarginBottom();
1275
1276 wxPen pen = GetPen();
1277 wxColour fg = aWindow.GetForegroundColour();
1278 COLOR4D cursorColor = COLOR4D( m_trace->GetTraceColour() ).Mix( fg, 0.6 );
1279 COLOR4D textColor = fg;
1280
1281 if( cursorColor.Distance( textColor ) < 0.66 )
1282 textColor.Invert();
1283
1284 pen.SetColour( cursorColor.ToColour() );
1285 pen.SetStyle( m_continuous ? wxPENSTYLE_SOLID : wxPENSTYLE_LONG_DASH );
1286 aDC.SetPen( pen );
1287
1288 if( hasY && topPx < cursorPos.y && cursorPos.y < bottomPx )
1289 aDC.DrawLine( leftPx, cursorPos.y, rightPx, cursorPos.y );
1290
1291 if( leftPx < cursorPos.x && cursorPos.x < rightPx )
1292 {
1293 aDC.DrawLine( cursorPos.x, topPx, cursorPos.x, bottomPx );
1294
1295 wxString id = getID();
1296 wxSize size = aDC.GetTextExtent( wxS( "M" ) );
1297 wxRect textRect( wxPoint( cursorPos.x + 1 - size.x / 2, topPx - 4 - size.y ), size );
1298 wxBrush brush;
1299 wxPoint poly[3];
1300
1301 // Because a "1" looks off-center if it's actually centred.
1302 if( id == "1" )
1303 textRect.x -= 1;
1304
1305 // We want an equalateral triangle, so use size.y for both axes.
1306 size.y += 3;
1307 // Make sure it's an even number so the slopes of the sides will be identical.
1308 size.y = ( size.y / 2 ) * 2;
1309 poly[0] = { cursorPos.x - 1 - size.y / 2, topPx - size.y };
1310 poly[1] = { cursorPos.x + 1 + size.y / 2, topPx - size.y };
1311 poly[2] = { cursorPos.x, topPx };
1312
1313 brush.SetStyle( wxBRUSHSTYLE_SOLID );
1314 brush.SetColour( m_trace->GetTraceColour() );
1315 aDC.SetBrush( brush );
1316 aDC.DrawPolygon( 3, poly );
1317
1318 aDC.SetTextForeground( textColor.ToColour() );
1319 aDC.DrawLabel( id, textRect, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL );
1320
1321 if( m_trace->IsMultiRun() && m_trace->GetSweepCount() > 1
1322 && m_trace->GetSweepSize() != std::numeric_limits<size_t>::max() )
1323 {
1324 wxString runLabel;
1325 const std::vector<wxString>& labels = m_trace->GetMultiRunLabels();
1326
1327 if( m_sweepIndex >= 0 && m_sweepIndex < (int) labels.size() )
1328 {
1329 runLabel = labels[m_sweepIndex];
1330 }
1331 else
1332 {
1333 runLabel = wxString::Format( _( "Run %d" ), m_sweepIndex + 1 );
1334 }
1335
1336 wxSize runSize = aDC.GetTextExtent( runLabel );
1337 int runX = textRect.GetRight() + 6;
1338 wxRect runRect( wxPoint( runX, textRect.y ), runSize );
1339
1340 runRect.Inflate( 3, 1 );
1341
1342 wxBrush labelBrush( aWindow.GetBackgroundColour() );
1343 wxPen labelPen( cursorColor.ToColour() );
1344
1345 aDC.SetPen( labelPen );
1346 aDC.SetBrush( labelBrush );
1347 aDC.DrawRectangle( runRect );
1348 aDC.SetTextForeground( cursorColor.ToColour() );
1349 aDC.DrawLabel( runLabel, runRect, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL );
1350 }
1351 }
1352}
1353
1354
1355bool CURSOR::Inside( const wxPoint& aPoint ) const
1356{
1357 if( !m_window || !m_trace )
1358 return false;
1359
1360 // An undefined coordinate draws no line, so it offers nothing to grab
1361 bool nearX = std::isfinite( m_coords.x )
1362 && std::abs( (double) aPoint.x - m_window->x2p( m_trace->x2s( m_coords.x ) ) ) <= DRAG_MARGIN;
1363 bool nearY = std::isfinite( m_coords.y )
1364 && std::abs( (double) aPoint.y - m_window->y2p( m_trace->y2s( m_coords.y ) ) ) <= DRAG_MARGIN;
1365
1366 return nearX || nearY;
1367}
1368
1369
1371{
1372 if( !m_window )
1373 return;
1374
1375 // An undefined coordinate has no pixel, so keep the last good reference for a drag to
1376 // measure against
1377 if( std::isfinite( m_coords.x ) )
1378 m_reference.x = m_window->x2p( m_trace->x2s( m_coords.x ) );
1379
1380 if( std::isfinite( m_coords.y ) )
1381 m_reference.y = m_window->y2p( m_trace->y2s( m_coords.y ) );
1382}
1383
1384
1385void TRACK_CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
1386{
1387 if( !m_visible )
1388 return;
1389
1390 // a Smith chart is drawn from the chart geometry, its X axis carries no readable value
1391 if( m_view->IsSmithChart() )
1392 return;
1393
1394 wxCoord leftPx = aWindow.GetMarginLeft();
1395 wxCoord rightPx = aWindow.GetScrX() - aWindow.GetMarginRight();
1396 wxCoord topPx = aWindow.GetMarginTop();
1397 wxCoord bottomPx = aWindow.GetScrY() - aWindow.GetMarginBottom();
1398
1399 // each view converts the shared plot-space X with its own margins and scale
1400 wxCoord mouseX = aWindow.x2p( m_plotX );
1401
1402 if( mouseX < leftPx || mouseX > rightPx )
1403 return;
1404
1405 wxColour fg = aWindow.GetForegroundColour();
1406 wxPen linePen( fg, 1, wxPENSTYLE_DOT );
1407
1408 aDC.SetPen( linePen );
1409 aDC.DrawLine( mouseX, topPx, mouseX, bottomPx );
1410
1411 // only the traces plotted on this view are read out here; the sibling views draw their own
1412 std::vector<TRACE*> traces;
1413
1414 for( const auto& [name, trace] : m_plotTab->GetTraces() )
1415 {
1416 if( trace && trace->GetView() == m_view )
1417 traces.push_back( trace );
1418 }
1419
1420 if( traces.empty() )
1421 return;
1422
1423 // set explicitly: layers are plotted in list order and leave their own font on the DC
1424 aDC.SetFont( GetPlotFont() );
1425
1426 // All traces on a tab share the same X axis, so any one of them can convert the window's
1427 // plot-space X (which may be log-transformed) back into the real X data value.
1428 double dataX = traces.front()->s2x( m_plotX );
1429
1430 wxColour bg = aWindow.GetBackgroundColour();
1431
1432 // Pick a box colour scheme that contrasts with the plot's own background, so the box reads
1433 // the same regardless of whether "Dark Mode Plots" is on.
1434 double luminance = 0.299 * bg.Red() + 0.587 * bg.Green() + 0.114 * bg.Blue();
1435 bool plotIsDark = luminance < 128;
1436 wxColour boxBg = plotIsDark ? wxColour( 0, 0, 0, 220 ) : wxColour( 255, 255, 255, 220 );
1437 wxColour boxDefaultText = plotIsDark ? *wxWHITE : *wxBLACK;
1438
1439 struct ROW
1440 {
1441 wxString label;
1442 wxString value;
1443 wxColour colour;
1444 };
1445
1446 std::vector<ROW> rows;
1447
1448 SPICE_VALUE_FORMAT xFmt{ 4, wxS( "~" ) + m_view->GetUnitsX() };
1449 rows.push_back( { m_view->GetLabelX() + wxS( ":" ), SPICE_VALUE( dataX ).ToString( xFmt ), boxDefaultText } );
1450
1451 for( TRACE* trace : traces )
1452 {
1453 double traceX = trace->s2x( m_plotX );
1454 double y;
1455
1456 if( !trace->GetValueAtX( traceX, &y ) )
1457 continue;
1458
1459 wxColour traceColour = trace->GetTraceColour();
1460
1461 // Mark the intersection point of the crosshair with the trace itself.
1462 int markerY = aWindow.y2p( trace->y2s( y ) );
1463
1464 if( markerY >= topPx && markerY <= bottomPx )
1465 {
1466 wxBrush markerBrush( traceColour );
1467 wxPen markerPen( bg, 1 );
1468
1469 aDC.SetBrush( markerBrush );
1470 aDC.SetPen( markerPen );
1471 aDC.DrawCircle( mouseX, markerY, 4 );
1472 }
1473
1474 SPICE_VALUE_FORMAT fmt{ 4, wxS( "~" ) + m_view->GetUnitsForTrace( trace ) };
1475
1476 rows.push_back( { trace->GetDisplayName() + wxS( ":" ), SPICE_VALUE( y ).ToString( fmt ), traceColour } );
1477 }
1478
1479 if( rows.size() <= 1 )
1480 return;
1481
1482 int labelWidth = 0;
1483 int valueWidth = 0;
1484 int rowHeight = 0;
1485
1486 for( const ROW& row : rows )
1487 {
1488 wxSize labelSize = aDC.GetTextExtent( row.label );
1489 wxSize valueSize = aDC.GetTextExtent( row.value );
1490
1491 labelWidth = std::max( labelWidth, labelSize.x );
1492 valueWidth = std::max( valueWidth, valueSize.x );
1493 rowHeight = std::max( { rowHeight, labelSize.y, valueSize.y } );
1494 }
1495
1496 const int columnGap = 8;
1497 int lineHeight = rowHeight + 2;
1498 int boxWidth = labelWidth + columnGap + valueWidth + 10;
1499 int boxHeight = lineHeight * (int) rows.size() + 6;
1500
1501 int boxX = mouseX + 12;
1502
1503 if( boxX + boxWidth > rightPx )
1504 boxX = std::max( leftPx, mouseX - 12 - boxWidth );
1505
1506 int boxY = topPx + 4;
1507
1508 wxBrush brush( boxBg );
1509 wxPen boxPen( fg );
1510
1511 aDC.SetBrush( brush );
1512 aDC.SetPen( boxPen );
1513 aDC.DrawRectangle( boxX, boxY, boxWidth, boxHeight );
1514
1515 int textY = boxY + 3;
1516 int valueRight = boxX + boxWidth - 5;
1517
1518 for( const ROW& row : rows )
1519 {
1520 aDC.SetTextForeground( row.colour );
1521 aDC.DrawText( row.label, boxX + 5, textY );
1522 aDC.DrawText( row.value, valueRight - aDC.GetTextExtent( row.value ).x, textY );
1523 textY += lineHeight;
1524 }
1525}
1526
1527
1528SIM_VIEW::SIM_VIEW( SIM_PLOT_TAB* aPlotTab, wxWindow* aParent ) :
1529 mpWindow( aParent, wxID_ANY ),
1530 m_axis_x( nullptr ),
1531 m_axis_y1( nullptr ),
1532 m_axis_y2( nullptr ),
1533 m_axis_y3( nullptr ),
1534 m_legend( nullptr ),
1535 m_plotTab( aPlotTab ),
1536 m_trackCursor( nullptr ),
1537 m_smithGrid( nullptr ),
1538 m_smithChart( false ),
1539 m_smithZoom( 1.0 ),
1540 m_smithPanning( false ),
1541 m_smithLeftSkipped( false )
1542{
1543 // Smith-mode pan/zoom, these run before mpWindow's handlers and skip when not a Smith chart
1544 Bind( wxEVT_MOUSEWHEEL, &SIM_VIEW::onSmithMouseWheel, this );
1545 Bind( wxEVT_MAGNIFY, &SIM_VIEW::onSmithMagnify, this );
1546 Bind( wxEVT_MIDDLE_DOWN, &SIM_VIEW::onSmithMiddleDown, this );
1547 Bind( wxEVT_LEFT_DOWN, &SIM_VIEW::onSmithLeftDown, this );
1548 Bind( wxEVT_MOTION, &SIM_VIEW::onSmithMotion, this );
1549 Bind( wxEVT_LEFT_UP, &SIM_VIEW::onSmithLeftUp, this );
1550 Bind( wxEVT_LEFT_DCLICK, &SIM_VIEW::onSmithDClick, this );
1551 Bind( wxEVT_RIGHT_DOWN, &SIM_VIEW::onSmithRightDown, this );
1552 Bind( wxEVT_RIGHT_UP, &SIM_VIEW::onSmithRightUp, this );
1553
1554 // route the context-menu zoom commands to the Smith view
1555 for( int id : { mpID_ZOOM_IN, mpID_ZOOM_OUT, mpID_FIT, mpID_CENTER } )
1556 Bind( wxEVT_MENU, &SIM_VIEW::onSmithMenuCommand, this, id );
1557
1558 m_trackCursor = new TRACK_CURSOR( aPlotTab, this );
1560
1561 Bind( wxEVT_MOTION, &SIM_VIEW::onPlotMouseMove, this );
1562 Bind( wxEVT_LEAVE_WINDOW, &SIM_VIEW::onPlotMouseLeave, this );
1563}
1564
1565
1567{
1568 if( !m_trackCursor || ( m_trackCursor->IsVisible() && m_trackCursor->GetPlotX() == aPlotX ) )
1569 return false;
1570
1571 m_trackCursor->SetPlotX( aPlotX );
1572 return true;
1573}
1574
1575
1577{
1578 if( !m_trackCursor || !m_trackCursor->IsVisible() )
1579 return false;
1580
1581 m_trackCursor->SetHidden();
1582 return true;
1583}
1584
1585
1587{
1588 // Layers are drawn in the order they were added, so re-adding the overlays last ensures
1589 // they stay on top of (newly added) trace and cursor layers. Their order here is their
1590 // stacking order, with the track cursor -- the transient one -- on top.
1592 {
1593 if( overlay )
1594 {
1595 DelLayer( overlay, false, false );
1596 AddLayer( overlay, false );
1597 }
1598 }
1599}
1600
1601
1602void SIM_VIEW::onPlotMouseMove( wxMouseEvent& aEvent )
1603{
1604 // the pointer is over one chart, but the crosshair is drawn on all of them
1605 if( m_plotTab && m_plotTab->IsTrackCursorEnabled() )
1606 m_plotTab->SetTrackCursorX( p2x( aEvent.GetX() ) );
1607
1608 aEvent.Skip();
1609}
1610
1611
1612void SIM_VIEW::onPlotMouseLeave( wxMouseEvent& aEvent )
1613{
1614 if( m_plotTab && m_plotTab->IsTrackCursorEnabled() )
1615 m_plotTab->HideTrackCursor();
1616
1617 aEvent.Skip();
1618}
1619
1620
1622{
1623 if( m_plotTab )
1624 m_plotTab->SyncXView( this );
1625}
1626
1627
1628void SIM_VIEW::SetY1Scale( bool aLock, double aMin, double aMax )
1629{
1630 wxCHECK( m_axis_y1, /* void */ );
1631 m_axis_y1->SetAxisMinMax( aLock, aMin, aMax );
1632}
1633
1634
1635void SIM_VIEW::SetY2Scale( bool aLock, double aMin, double aMax )
1636{
1637 wxCHECK( m_axis_y2, /* void */ );
1638 m_axis_y2->SetAxisMinMax( aLock, aMin, aMax );
1639}
1640
1641
1642void SIM_VIEW::SetY3Scale( bool aLock, double aMin, double aMax )
1643{
1644 wxCHECK( m_axis_y3, /* void */ );
1645 m_axis_y3->SetAxisMinMax( aLock, aMin, aMax );
1646}
1647
1648
1649wxString SIM_VIEW::GetUnitsX() const
1650{
1651 LOG_SCALE<mpScaleXLog>* logScale = dynamic_cast<LOG_SCALE<mpScaleXLog>*>( m_axis_x );
1652 LIN_SCALE<mpScaleX>* linScale = dynamic_cast<LIN_SCALE<mpScaleX>*>( m_axis_x );
1653
1654 if( logScale )
1655 return logScale->GetUnits();
1656 else if( linScale )
1657 return linScale->GetUnits();
1658 else
1659 return wxEmptyString;
1660}
1661
1662
1663wxString SIM_VIEW::GetUnitsY1() const
1664{
1665 LIN_SCALE<mpScaleY>* linScale = dynamic_cast<LIN_SCALE<mpScaleY>*>( m_axis_y1 );
1666
1667 if( linScale )
1668 return linScale->GetUnits();
1669 else
1670 return wxEmptyString;
1671}
1672
1673
1674wxString SIM_VIEW::GetUnitsY2() const
1675{
1676 LIN_SCALE<mpScaleY>* linScale = dynamic_cast<LIN_SCALE<mpScaleY>*>( m_axis_y2 );
1677
1678 if( linScale )
1679 return linScale->GetUnits();
1680 else
1681 return wxEmptyString;
1682}
1683
1684
1685wxString SIM_VIEW::GetUnitsY3() const
1686{
1687 LIN_SCALE<mpScaleY>* linScale = dynamic_cast<LIN_SCALE<mpScaleY>*>( m_axis_y3 );
1688
1689 if( linScale )
1690 return linScale->GetUnits();
1691 else
1692 return wxEmptyString;
1693}
1694
1695
1696wxString SIM_VIEW::GetUnitsForTrace( TRACE* aTrace ) const
1697{
1698 if( m_plotTab->GetSimType() == ST_AC )
1699 {
1700 if( aTrace->GetType() & SPT_AC_PHASE )
1701 return GetUnitsY2();
1702 else
1703 return GetUnitsY1();
1704 }
1705 else
1706 {
1707 if( aTrace->GetType() & SPT_POWER )
1708 return GetUnitsY3();
1709 else if( aTrace->GetType() & SPT_CURRENT )
1710 return GetUnitsY2();
1711 else
1712 return GetUnitsY1();
1713 }
1714}
1715
1716
1717int SIM_VIEW::GetAxisSlot( TRACE* aTrace ) const
1718{
1719 if( ( aTrace->GetType() & SPT_AC_PHASE )
1720 || ( ( m_plotTab->GetSimType() != ST_AC ) && ( aTrace->GetType() & SPT_CURRENT ) ) )
1721 {
1722 return 2;
1723 }
1724 else if( aTrace->GetType() & SPT_POWER )
1725 {
1726 return 3;
1727 }
1728 else
1729 {
1730 return 1;
1731 }
1732}
1733
1734
1736{
1737 switch( aSlot )
1738 {
1739 case 2: return m_axis_y2;
1740 case 3: return m_axis_y3;
1741 default: return m_axis_y1;
1742 }
1743}
1744
1745
1746void SIM_VIEW::updateAxes( int aNewTraceType )
1747{
1748 switch( m_plotTab->GetSimType() )
1749 {
1750 case ST_AC:
1751 if( !m_axis_x )
1752 {
1753 m_axis_x = new LOG_SCALE<mpScaleXLog>( wxEmptyString, wxT( "Hz" ), mpALIGN_BOTTOM );
1754 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1755 AddLayer( m_axis_x );
1756
1757 m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "dB" ), mpALIGN_LEFT );
1758 m_axis_y1->SetNameAlign( mpALIGN_LEFT );
1760
1761 m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "°" ), mpALIGN_RIGHT );
1762 m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
1763 m_axis_y2->SetMasterScale( m_axis_y1 );
1765 }
1766
1767 m_axis_x->SetName( _( "Frequency" ) );
1768 m_axis_y1->SetName( _( "Gain" ) );
1769 m_axis_y2->SetName( _( "Phase" ) );
1770 break;
1771
1772 case ST_SP:
1773 if( !m_axis_x )
1774 {
1775 m_axis_x = new LOG_SCALE<mpScaleXLog>( wxEmptyString, wxT( "Hz" ), mpALIGN_BOTTOM );
1776 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1777 AddLayer( m_axis_x );
1778
1779 m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "" ), mpALIGN_LEFT );
1780 m_axis_y1->SetNameAlign( mpALIGN_LEFT );
1782
1783 m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "°" ), mpALIGN_RIGHT );
1784 m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
1785 m_axis_y2->SetMasterScale( m_axis_y1 );
1787 }
1788
1789 m_axis_x->SetName( _( "Frequency" ) );
1790 m_axis_y1->SetName( _( "Amplitude" ) );
1791 m_axis_y2->SetName( _( "Phase" ) );
1792 break;
1793
1794 case ST_DC: prepareDCAxes( aNewTraceType ); break;
1795
1796 case ST_NOISE:
1797 if( !m_axis_x )
1798 {
1799 m_axis_x = new LOG_SCALE<mpScaleXLog>( wxEmptyString, wxT( "Hz" ), mpALIGN_BOTTOM );
1800 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1801 AddLayer( m_axis_x );
1802
1803 if( ( aNewTraceType & SPT_CURRENT ) == 0 )
1804 {
1805 m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "" ), mpALIGN_LEFT );
1806 m_axis_y1->SetNameAlign( mpALIGN_LEFT );
1808 }
1809 else
1810 {
1811 m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "" ), mpALIGN_RIGHT );
1812 m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
1814 }
1815 }
1816
1817 m_axis_x->SetName( _( "Frequency" ) );
1818
1819 if( m_axis_y1 )
1820 m_axis_y1->SetName( _( "Noise (V/√Hz)" ) );
1821
1822 if( m_axis_y2 )
1823 m_axis_y2->SetName( _( "Noise (A/√Hz)" ) );
1824
1825 break;
1826
1827 case ST_FFT:
1828 if( !m_axis_x )
1829 {
1830 m_axis_x = new LOG_SCALE<mpScaleXLog>( wxEmptyString, wxT( "Hz" ), mpALIGN_BOTTOM );
1831 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1832 AddLayer( m_axis_x );
1833
1834 m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "dB" ), mpALIGN_LEFT );
1835 m_axis_y1->SetNameAlign( mpALIGN_LEFT );
1837 }
1838
1839 m_axis_x->SetName( _( "Frequency" ) );
1840 m_axis_y1->SetName( _( "Intensity" ) );
1841 break;
1842
1843 case ST_TRAN:
1844 if( !m_axis_x )
1845 {
1846 m_axis_x = new TIME_SCALE( wxEmptyString, wxT( "s" ), mpALIGN_BOTTOM );
1847 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1848 AddLayer( m_axis_x );
1849
1850 m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "V" ), mpALIGN_LEFT );
1851 m_axis_y1->SetNameAlign( mpALIGN_LEFT );
1853
1854 m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "A" ), mpALIGN_RIGHT );
1855 m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
1856 m_axis_y2->SetMasterScale( m_axis_y1 );
1858 }
1859
1860 m_axis_x->SetName( _( "Time" ) );
1861 m_axis_y1->SetName( _( "Voltage" ) );
1862 m_axis_y2->SetName( _( "Current" ) );
1863
1864 if( aNewTraceType & SPT_POWER )
1866
1867 if( m_axis_y3 )
1868 m_axis_y3->SetName( _( "Power" ) );
1869
1870 break;
1871
1872 default:
1873 // suppress warnings
1874 break;
1875 }
1876
1877 if( m_plotTab->GetSimType() == ST_TRAN || m_plotTab->GetSimType() == ST_DC )
1878 {
1879 if( m_axis_y3 )
1880 {
1881 SetMargins( 30, 160, 45, 70 );
1882
1883 if( m_axis_y2 )
1884 m_axis_y2->SetNameAlign( mpALIGN_BORDER_RIGHT );
1885
1886 m_axis_y3->SetAlign( mpALIGN_BORDER_RIGHT );
1887 m_axis_y3->SetNameAlign( mpALIGN_BORDER_RIGHT );
1888 }
1889 else
1890 {
1891 SetMargins( 30, 70, 45, 70 );
1892
1893 if( m_axis_y2 )
1894 m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
1895 }
1896 }
1897
1898 if( m_axis_x )
1899 m_axis_x->SetFont( KIUI::GetStatusFont( this ) );
1900
1901 if( m_axis_y1 )
1902 m_axis_y1->SetFont( KIUI::GetStatusFont( this ) );
1903
1904 if( m_axis_y2 )
1905 m_axis_y2->SetFont( KIUI::GetStatusFont( this ) );
1906
1907 if( m_axis_y3 )
1908 m_axis_y3->SetFont( KIUI::GetStatusFont( this ) );
1909
1910 if( m_axis_x )
1911 {
1912 wxClientDC dc( this );
1913 dc.SetFont( m_axis_x->GetFont() );
1914
1915 int tx, ty;
1916 dc.GetTextExtent( wxT( "0" ), &tx, &ty );
1917
1918 // Enough room for a row of tick labels, a gap, and the axis name below them -- mirrors
1919 // the layout mpScaleXBase::Plot() uses when it places the name relative to the bottom
1920 // margin. A fixed margin can't keep up with the UI font, which varies by platform and
1921 // DPI/scaling settings.
1922 int marginBottom = 2 * ty + 10;
1923
1924 if( marginBottom > GetMarginBottom() )
1925 SetMargins( GetMarginTop(), GetMarginRight(), marginBottom, GetMarginLeft() );
1926 }
1927
1929}
1930
1931
1932void SIM_VIEW::prepareDCAxes( int aNewTraceType )
1933{
1934 wxString sim_cmd = m_plotTab->GetSimCommand().Lower();
1935 wxString rem;
1936
1937 if( sim_cmd.StartsWith( ".dc", &rem ) )
1938 {
1939 wxChar ch = 0;
1940
1941 rem.Trim( false );
1942
1943 try
1944 {
1945 ch = rem.GetChar( 0 );
1946 }
1947 catch( ... )
1948 {
1949 // Best efforts
1950 }
1951
1952 switch( ch )
1953 {
1954 // Make sure that we have a reliable default (even if incorrectly labeled)
1955 default:
1956 case 'v':
1957 if( !m_axis_x )
1958 {
1959 m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "V" ), mpALIGN_BOTTOM );
1960 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1961 AddLayer( m_axis_x );
1962 }
1963
1964 m_axis_x->SetName( _( "Voltage (swept)" ) );
1965 break;
1966
1967 case 'i':
1968 if( !m_axis_x )
1969 {
1970 m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "A" ), mpALIGN_BOTTOM );
1971 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1972 AddLayer( m_axis_x );
1973 }
1974
1975 m_axis_x->SetName( _( "Current (swept)" ) );
1976 break;
1977
1978 case 'r':
1979 if( !m_axis_x )
1980 {
1981 m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "Ω" ), mpALIGN_BOTTOM );
1982 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1983 AddLayer( m_axis_x );
1984 }
1985
1986 m_axis_x->SetName( _( "Resistance (swept)" ) );
1987 break;
1988
1989 case 't':
1990 if( !m_axis_x )
1991 {
1992 m_axis_x = new LIN_SCALE<mpScaleX>( wxEmptyString, wxT( "°C" ), mpALIGN_BOTTOM );
1993 m_axis_x->SetNameAlign( mpALIGN_BOTTOM );
1994 AddLayer( m_axis_x );
1995 }
1996
1997 m_axis_x->SetName( _( "Temperature (swept)" ) );
1998 break;
1999 }
2000
2001 if( !m_axis_y1 )
2002 {
2003 m_axis_y1 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "V" ), mpALIGN_LEFT );
2004 m_axis_y1->SetNameAlign( mpALIGN_LEFT );
2006 }
2007
2008 if( !m_axis_y2 )
2009 {
2010 m_axis_y2 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "A" ), mpALIGN_RIGHT );
2011 m_axis_y2->SetNameAlign( mpALIGN_RIGHT );
2013 }
2014
2015 m_axis_y1->SetName( _( "Voltage (measured)" ) );
2016 m_axis_y2->SetName( _( "Current" ) );
2017
2018 if( ( aNewTraceType & SPT_POWER ) )
2020
2021 if( m_axis_y3 )
2022 m_axis_y3->SetName( _( "Power" ) );
2023 }
2024}
2025
2026
2028{
2029 if( !m_axis_y3 )
2030 {
2031 SetMargins( 30, 160, 45, 70 );
2032 m_axis_y3 = new LIN_SCALE<mpScaleY>( wxEmptyString, wxT( "W" ), mpALIGN_BORDER_RIGHT );
2033 m_axis_y3->SetNameAlign( mpALIGN_BORDER_RIGHT );
2034 m_axis_y3->SetMasterScale( m_axis_y1 );
2036 }
2037
2038 if( m_axis_y3 )
2039 {
2040 m_axis_y3->SetAlign( mpALIGN_BORDER_RIGHT );
2041 m_axis_y3->SetNameAlign( mpALIGN_BORDER_RIGHT );
2042 }
2043
2044 if( m_axis_y2 )
2045 m_axis_y2->SetNameAlign( mpALIGN_BORDER_RIGHT );
2046}
2047
2048
2050{
2051 bool hasY1Traces = false;
2052 bool hasY2Traces = false;
2053 bool hasY3Traces = false;
2054
2055 if( !m_smithChart )
2056 {
2057 for( const auto& [name, trace] : m_plotTab->GetTraces() )
2058 {
2059 if( !trace || trace->GetView() != this )
2060 continue;
2061
2062 if( trace->GetType() & SPT_POWER )
2063 {
2064 hasY3Traces = true;
2065 }
2066 else if( ( trace->GetType() & SPT_AC_PHASE )
2067 || ( ( m_plotTab->GetSimType() != ST_AC ) && ( trace->GetType() & SPT_CURRENT ) ) )
2068 {
2069 hasY2Traces = true;
2070 }
2071 else
2072 {
2073 hasY1Traces = true;
2074 }
2075 }
2076 }
2077
2078 bool visibilityChanged = false;
2079
2080 if( m_axis_x && m_axis_x->IsVisible() != !m_smithChart )
2081 {
2082 m_axis_x->SetVisible( !m_smithChart );
2083 visibilityChanged = true;
2084 }
2085
2086 if( m_axis_y1 && m_axis_y1->IsVisible() != hasY1Traces )
2087 {
2088 m_axis_y1->SetVisible( hasY1Traces );
2089 visibilityChanged = true;
2090 }
2091
2092 if( m_axis_y2 && m_axis_y2->IsVisible() != hasY2Traces )
2093 {
2094 m_axis_y2->SetVisible( hasY2Traces );
2095 visibilityChanged = true;
2096 }
2097
2098 if( m_axis_y3 && m_axis_y3->IsVisible() != hasY3Traces )
2099 {
2100 m_axis_y3->SetVisible( hasY3Traces );
2101 visibilityChanged = true;
2102 }
2103
2104 if( visibilityChanged )
2105 UpdateAll();
2106}
2107
2108
2109void SIM_VIEW::ResetScales( bool aIncludeX )
2110{
2111 if( m_axis_x && aIncludeX )
2112 {
2113 m_axis_x->ResetDataRange();
2114
2115 if( m_plotTab->GetSimType() == ST_TRAN )
2116 {
2117 wxStringTokenizer tokenizer( m_plotTab->GetSimCommand(), " \t\r\n", wxTOKEN_STRTOK );
2118 wxString cmd = tokenizer.GetNextToken().Lower();
2119
2120 wxASSERT( cmd == wxS( ".tran" ) );
2121
2122 SPICE_VALUE step;
2123 SPICE_VALUE end( 1.0 );
2124 SPICE_VALUE start( 0.0 );
2125
2126 if( tokenizer.HasMoreTokens() )
2127 step = SPICE_VALUE( tokenizer.GetNextToken() );
2128
2129 if( tokenizer.HasMoreTokens() )
2130 end = SPICE_VALUE( tokenizer.GetNextToken() );
2131
2132 if( tokenizer.HasMoreTokens() )
2133 start = SPICE_VALUE( tokenizer.GetNextToken() );
2134
2135 static_cast<TIME_SCALE*>( m_axis_x )->SetStartAndEnd( start.ToDouble(), end.ToDouble() );
2136 }
2137 }
2138
2139 if( m_axis_y1 )
2140 m_axis_y1->ResetDataRange();
2141
2142 if( m_axis_y2 )
2143 m_axis_y2->ResetDataRange();
2144
2145 if( m_axis_y3 )
2146 m_axis_y3->ResetDataRange();
2147
2148 for( auto& [name, trace] : m_plotTab->GetTraces() )
2149 {
2150 if( trace->GetView() == this )
2151 trace->UpdateScales();
2152 }
2153}
2154
2155
2156void SIM_VIEW::SetSmithChart( bool aEnable )
2157{
2158 if( m_smithChart == aEnable )
2159 return;
2160
2161 m_smithChart = aEnable;
2162
2163 // a mode switch mid-gesture must not leave a pan or a skipped click behind
2164 m_smithPanning = false;
2165 m_smithLeftSkipped = false;
2166
2167 if( aEnable && !m_smithGrid )
2168 {
2169 m_smithGrid = new SMITH_GRID();
2170 m_smithGrid->SetFont( KIUI::GetStatusFont( this ) );
2171 m_smithGrid->SetPen( wxPen( m_plotTab->GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::AXIS ), 1 ) );
2173
2174 // the chart is a backdrop; keep the floating overlays above it
2175 RaiseOverlays();
2176 }
2177
2178 if( m_smithGrid )
2179 m_smithGrid->SetVisible( aEnable );
2180
2181 if( aEnable )
2183
2185
2187 UpdateAll();
2188}
2189
2190
2192{
2193 if( !m_smithGrid )
2194 return;
2195
2196 double z0 = 0.0;
2197 bool mixed = false;
2198 bool unresolved = false;
2199
2200 for( const auto& [name, trace] : m_plotTab->GetTraces() )
2201 {
2202 SMITH_TRACE* smithTrace = dynamic_cast<SMITH_TRACE*>( trace );
2203
2204 if( !smithTrace || smithTrace->GetView() != this )
2205 continue;
2206
2207 double traceZ0 = smithTrace->GetReferenceImpedance();
2208
2209 if( traceZ0 <= 0.0 )
2210 unresolved = true;
2211 else if( z0 == 0.0 )
2212 z0 = traceZ0;
2213 else if( traceZ0 != z0 )
2214 mixed = true;
2215 }
2216
2217 // a trace whose port impedance is unknown leaves no single reference to name either
2218 m_smithGrid->SetReferenceImpedance( mixed || unresolved ? 0.0 : z0 );
2219 m_smithGrid->SetMixedReferences( mixed );
2220}
2221
2222
2224{
2225 if( m_smithGrid )
2226 m_smithGrid->SetPen( wxPen( m_plotTab->GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::AXIS ), 1 ) );
2227}
2228
2229
2231{
2232 return SMITH_GRID::GetChartView( const_cast<SIM_VIEW&>( *this ), m_smithZoom, m_smithPan, aView );
2233}
2234
2235
2236void SIM_VIEW::SmithZoomAt( const wxPoint& aPos, double aFactor )
2237{
2238 SMITH_VIEW view;
2239
2240 if( !getSmithView( view ) )
2241 return;
2242
2243 double newZoom = std::clamp( m_smithZoom * aFactor, 1.0, 50.0 );
2244
2245 if( newZoom <= 1.0 )
2246 {
2248 Refresh();
2249 return;
2250 }
2251
2252 // keep the point under the cursor fixed while zooming
2253 m_smithPan = SMITH_MATH::ZoomAboutPoint( view, aPos, newZoom );
2254 m_smithZoom = newZoom;
2255
2256 Refresh();
2257}
2258
2259
2260void SIM_VIEW::SmithPanBy( const wxPoint& aDelta )
2261{
2262 SMITH_VIEW view;
2263
2264 if( !getSmithView( view ) )
2265 return;
2266
2267 m_smithPan.x -= aDelta.x / view.radius;
2268 m_smithPan.y += aDelta.y / view.radius;
2269
2270 Refresh();
2271}
2272
2273
2274void SIM_VIEW::onSmithMouseWheel( wxMouseEvent& aEvent )
2275{
2276 if( !m_smithChart )
2277 {
2278 aEvent.Skip();
2279 return;
2280 }
2281
2282 // swallow horizontal scroll too, mpWindow would pan its hidden axes with it
2283 if( aEvent.GetWheelAxis() != wxMOUSE_WHEEL_VERTICAL || aEvent.GetWheelRotation() == 0 )
2284 return;
2285
2286 // do not skip, or mpWindow would also zoom its hidden axes
2287 SmithZoomAt( aEvent.GetPosition(), aEvent.GetWheelRotation() > 0 ? 1.2 : 1.0 / 1.2 );
2288}
2289
2290
2291void SIM_VIEW::onSmithMagnify( wxMouseEvent& aEvent )
2292{
2293 if( !m_smithChart )
2294 {
2295 aEvent.Skip();
2296 return;
2297 }
2298
2299 double factor = aEvent.GetMagnification() + 1.0;
2300
2301 if( factor > 0.0 )
2302 SmithZoomAt( aEvent.GetPosition(), factor );
2303}
2304
2305
2306void SIM_VIEW::onSmithMiddleDown( wxMouseEvent& aEvent )
2307{
2308 // keep mpWindow's middle-button pan off the hidden axes
2309 if( !m_smithChart )
2310 aEvent.Skip();
2311}
2312
2313
2314void SIM_VIEW::onSmithLeftDown( wxMouseEvent& aEvent )
2315{
2316 // clear stale pan state so it cannot hijack a cursor grab
2317 m_smithPanning = false;
2318 m_smithLeftSkipped = false;
2319
2320 wxPoint pos = aEvent.GetPosition();
2321
2322 if( m_smithChart && !IsInsideInfoLayer( pos ) )
2323 {
2324 m_smithPanning = true;
2325 m_smithPanLast = pos;
2326 return;
2327 }
2328
2329 // on a cursor or the legend, let mpWindow drag it, and remember that it saw the click
2330 // so the matching motions and release reach it too
2331 m_smithLeftSkipped = true;
2332 aEvent.Skip();
2333}
2334
2335
2336void SIM_VIEW::onSmithMotion( wxMouseEvent& aEvent )
2337{
2338 if( m_smithChart && aEvent.Dragging() )
2339 {
2340 if( aEvent.LeftIsDown() && !m_smithLeftSkipped )
2341 {
2342 // a left drag mpWindow did not see the start of, pan if one is active, and
2343 // swallow either way so mpWindow cannot rubber-band from a stale click point
2344 if( m_smithPanning )
2345 {
2346 wxPoint pos = aEvent.GetPosition();
2347
2348 SmithPanBy( pos - m_smithPanLast );
2349 m_smithPanLast = pos;
2350 }
2351
2352 return;
2353 }
2354
2355 // keep mpWindow's middle-button pan off the hidden axes
2356 if( aEvent.MiddleIsDown() )
2357 return;
2358 }
2359
2360 aEvent.Skip();
2361}
2362
2363
2364void SIM_VIEW::onSmithLeftUp( wxMouseEvent& aEvent )
2365{
2366 if( m_smithChart )
2367 {
2368 m_smithPanning = false;
2369
2370 // a release mpWindow saw no click for would zoom the hidden axes to the rect
2371 // between its stale click point and this position
2372 if( !m_smithLeftSkipped )
2373 return;
2374
2375 m_smithLeftSkipped = false;
2376 }
2377
2378 aEvent.Skip();
2379}
2380
2381
2382void SIM_VIEW::onSmithDClick( wxMouseEvent& aEvent )
2383{
2384 wxPoint pos = aEvent.GetPosition();
2385
2386 if( m_smithChart && !IsInsideInfoLayer( pos ) )
2387 {
2389 Refresh();
2390 return;
2391 }
2392
2393 aEvent.Skip();
2394}
2395
2396
2397void SIM_VIEW::onSmithRightDown( wxMouseEvent& aEvent )
2398{
2399 // remember where the menu opened, for its zoom commands
2400 if( m_smithChart )
2401 m_smithMenuPos = aEvent.GetPosition();
2402
2403 aEvent.Skip();
2404}
2405
2406
2407void SIM_VIEW::onSmithRightUp( wxMouseEvent& aEvent )
2408{
2409 if( !m_smithChart )
2410 {
2411 aEvent.Skip();
2412 return;
2413 }
2414
2415 // no zoom history here, so grey out Undo/Redo and show the menu ourselves
2416 wxMenu* menu = GetPopupMenu();
2417
2418 menu->Enable( mpID_ZOOM_UNDO, false );
2419 menu->Enable( mpID_ZOOM_REDO, false );
2420
2421 PopupMenu( menu, aEvent.GetPosition() );
2422}
2423
2424
2425void SIM_VIEW::onSmithMenuCommand( wxCommandEvent& aEvent )
2426{
2427 if( !m_smithChart )
2428 {
2429 aEvent.Skip();
2430 return;
2431 }
2432
2433 switch( aEvent.GetId() )
2434 {
2435 case mpID_ZOOM_IN: SmithZoomAt( m_smithMenuPos, 1.5 ); break;
2436 case mpID_ZOOM_OUT: SmithZoomAt( m_smithMenuPos, 1.0 / 1.5 ); break;
2437
2438 case mpID_FIT:
2440 Refresh();
2441 break;
2442
2443 case mpID_CENTER:
2444 {
2445 SMITH_VIEW view;
2446
2447 if( getSmithView( view ) )
2448 {
2450 Refresh();
2451 }
2452
2453 break;
2454 }
2455
2456 default: aEvent.Skip();
2457 }
2458}
2459
2460
2461SIM_PLOT_TAB::SIM_PLOT_TAB( const wxString& aSimCommand, wxWindow* parent ) :
2462 SIM_TAB( aSimCommand, parent ),
2463 m_dotted_cp( false ),
2464 m_syncingXView( false ),
2465 m_trackCursorEnabled( false ),
2466 m_smithMode( false )
2467{
2469
2470 wxBoxSizer* outerSizer = new wxBoxSizer( wxVERTICAL );
2471
2472 m_viewsSizer = new wxBoxSizer( wxVERTICAL );
2473 outerSizer->Add( m_viewsSizer, 1, wxEXPAND, 0 );
2474
2475 wxBoxSizer* viewButtonsSizer = new wxBoxSizer( wxHORIZONTAL );
2476
2477 STD_BITMAP_BUTTON* addViewButton = new STD_BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap );
2478 addViewButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
2479 addViewButton->SetToolTip( _( "Add a new signal view" ) );
2480
2481 STD_BITMAP_BUTTON* removeViewButton = new STD_BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap );
2482 removeViewButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
2483 removeViewButton->SetToolTip( _( "Remove the last signal view" ) );
2484
2485 viewButtonsSizer->Add( addViewButton, 0, wxLEFT, 5 );
2486 viewButtonsSizer->Add( removeViewButton, 0, wxLEFT, 30 );
2487
2488 addViewButton->Bind( wxEVT_BUTTON,
2489 [this]( wxCommandEvent& aEvent )
2490 {
2491 AddView();
2492 } );
2493
2494 removeViewButton->Bind( wxEVT_BUTTON,
2495 [this]( wxCommandEvent& aEvent )
2496 {
2497 const std::vector<SIM_VIEW*>& views = GetViews();
2498
2499 if( !views.empty() )
2500 RemoveView( views.back() );
2501 } );
2502
2503 outerSizer->Add( viewButtonsSizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 3 );
2504
2505 SetSizer( outerSizer );
2506
2507 AddView();
2508}
2509
2510
2512{
2513 // ~mpWindow destroys all the added layers, so there is no need to destroy m_traces contents
2514}
2515
2516
2518{
2520
2521 for( SIM_VIEW* view : m_views )
2522 view->SetMouseWheelActions( m_mouseWheelActions );
2523}
2524
2525
2527{
2528 SIM_VIEW* view = new SIM_VIEW( this, this );
2529
2530 view->LimitView( true );
2531 view->SetMargins( 30, 70, 45, 70 );
2533
2534 view->updateAxes();
2535
2536 // a mpInfoLegend displays the name of traces on the left top panel corner:
2537 mpInfoLegend* legend = new mpInfoLegend( wxRect( 0, 0, 200, 40 ), wxTRANSPARENT_BRUSH );
2538 legend->SetVisible( false );
2539 view->AddLayer( legend );
2540 view->m_legend = legend;
2541 view->m_lastLegendPosition = legend->GetPosition();
2542
2543 view->EnableDoubleBuffer( true );
2544 view->RaiseOverlays();
2545 view->SetSmithChart( m_smithMode );
2546
2547 // Fixed Y axis ranges apply across all views in the tab, so a newly added view must
2548 // pick up whatever locks are already in effect on the existing ones.
2549 if( SIM_VIEW* firstView = GetDefaultView() )
2550 {
2551 double min, max;
2552
2553 if( firstView->GetY1Scale( &min, &max ) )
2554 view->SetY1Scale( true, min, max );
2555
2556 if( firstView->GetY2Scale( &min, &max ) )
2557 view->SetY2Scale( true, min, max );
2558
2559 if( firstView->GetY3Scale( &min, &max ) )
2560 {
2561 view->EnsureThirdYAxisExists();
2562 view->SetY3Scale( true, min, max );
2563 }
2564
2565 // a chart added later matches the legend the tab is already showing
2566 view->ShowLegend( firstView->IsLegendShown() );
2567 }
2568
2569 m_views.push_back( view );
2570 m_viewsSizer->Add( view, 1, wxALL | wxEXPAND, 1 );
2571
2573
2574 if( m_views.size() > 1 )
2575 SyncXView( m_views[0] );
2576
2577 view->UpdateAll();
2578 Layout();
2579
2580 wxQueueEvent( this, new wxCommandEvent( EVT_SIM_VIEWS_CHANGED ) );
2581
2582 return view;
2583}
2584
2585
2587{
2588 if( m_views.size() <= 1 )
2589 return false;
2590
2591 auto it = std::find( m_views.begin(), m_views.end(), aView );
2592
2593 if( it == m_views.end() )
2594 return false;
2595
2596 // The traces routed to this view lose their axes along with it, so drop them rather than
2597 // leaving them behind holding a destroyed view (and its destroyed axis layers). Their
2598 // signals stay in the Signals grid, simply no longer plotted, and can be assigned to one
2599 // of the remaining views.
2600 std::vector<TRACE*> plottedHere;
2601
2602 for( const auto& [name, trace] : m_traces )
2603 {
2604 trace->ClearYScaleViewIf( aView );
2605
2606 if( trace->GetView() == aView )
2607 plottedHere.push_back( trace );
2608 }
2609
2610 // DeleteTrace() erases from m_traces, so it cannot run while iterating it
2611 for( TRACE* trace : plottedHere )
2612 DeleteTrace( trace );
2613
2614 m_viewsSizer->Detach( aView );
2615 m_views.erase( it );
2616 aView->Destroy();
2617
2618 Layout();
2619
2620 // Any Y-scale ranges that were merged with the removed view are now stale; recompute them
2621 // (and refresh) so the remaining views reflect only the traces that are still present.
2622 ResetScales( false );
2623
2624 for( SIM_VIEW* view : m_views )
2625 view->UpdateAll();
2626
2627 wxQueueEvent( this, new wxCommandEvent( EVT_SIM_VIEWS_CHANGED ) );
2628
2629 return true;
2630}
2631
2632
2634{
2635 for( size_t i = 0; i < m_views.size(); ++i )
2636 {
2637 if( m_views[i] == aView )
2638 return (int) i;
2639 }
2640
2641 return -1;
2642}
2643
2644
2646{
2647 if( m_syncingXView || !aSource )
2648 return;
2649
2650 m_syncingXView = true;
2651
2652 double scaleX = aSource->GetScaleX();
2653 double pos = aSource->GetPosX();
2654 double desiredMin = aSource->GetDesiredXmin();
2655 double desiredMax = aSource->GetDesiredXmax();
2656
2657 for( SIM_VIEW* view : m_views )
2658 {
2659 if( view == aSource )
2660 continue;
2661
2662 view->SetScaleX( scaleX );
2663 view->SetXRange( pos, desiredMax, desiredMin );
2664 view->UpdateAll();
2665 }
2666
2667 m_syncingXView = false;
2668}
2669
2670
2672{
2673 SIM_VIEW* view = GetDefaultView();
2674 return view ? view->GetLabelX() : wxString( wxS( "" ) );
2675}
2676
2677
2679{
2680 SIM_VIEW* view = GetDefaultView();
2681 return view ? view->GetUnitsX() : wxString( wxS( "" ) );
2682}
2683
2684
2685wxString SIM_PLOT_TAB::GetUnitsForTrace( TRACE* aTrace ) const
2686{
2687 SIM_VIEW* view = aTrace->GetView() ? aTrace->GetView() : GetDefaultView();
2688 return view ? view->GetUnitsForTrace( aTrace ) : wxString( wxS( "" ) );
2689}
2690
2691
2692void SIM_PLOT_TAB::ShowGrid( bool aEnable )
2693{
2694 for( SIM_VIEW* view : m_views )
2695 view->ShowGrid( aEnable );
2696}
2697
2698
2700{
2701 SIM_VIEW* view = GetDefaultView();
2702 return view && view->IsGridShown();
2703}
2704
2705
2706void SIM_PLOT_TAB::ShowLegend( bool aEnable )
2707{
2708 for( SIM_VIEW* view : m_views )
2709 view->ShowLegend( aEnable );
2710}
2711
2712
2714{
2715 SIM_VIEW* view = GetDefaultView();
2716 return view && view->IsLegendShown();
2717}
2718
2719
2721{
2722 SIM_VIEW* view = GetDefaultView();
2723 return view ? view->GetLegendPosition() : wxPoint();
2724}
2725
2726
2727void SIM_PLOT_TAB::SetLegendPosition( const wxPoint& aPosition )
2728{
2729 if( SIM_VIEW* view = GetDefaultView() )
2730 view->SetLegendPosition( aPosition );
2731
2732 m_LastLegendPosition = aPosition;
2733}
2734
2735
2737{
2738 SIM_VIEW* view = GetDefaultView();
2739 return view ? view->GetLabelY1() : wxString( wxS( "" ) );
2740}
2741
2742
2744{
2745 SIM_VIEW* view = GetDefaultView();
2746 return view ? view->GetLabelY2() : wxString( wxS( "" ) );
2747}
2748
2749
2751{
2752 SIM_VIEW* view = GetDefaultView();
2753 return view ? view->GetLabelY3() : wxString( wxS( "" ) );
2754}
2755
2756
2758{
2759 SIM_VIEW* view = GetDefaultView();
2760 return view ? view->GetUnitsY1() : wxString( wxS( "" ) );
2761}
2762
2763
2765{
2766 SIM_VIEW* view = GetDefaultView();
2767 return view ? view->GetUnitsY2() : wxString( wxS( "" ) );
2768}
2769
2770
2772{
2773 SIM_VIEW* view = GetDefaultView();
2774 return view ? view->GetUnitsY3() : wxString( wxS( "" ) );
2775}
2776
2777
2778bool SIM_PLOT_TAB::getYScale( int aSlot, double* aMin, double* aMax ) const
2779{
2780 SIM_VIEW* defaultView = GetDefaultView();
2781
2782 if( !defaultView )
2783 return false;
2784
2785 // A fixed range is applied to every view at once, so the default view's locked state (and
2786 // value) speaks for the whole tab.
2787 if( mpScaleY* axis = defaultView->GetAxisBySlot( aSlot ) )
2788 {
2789 if( axis->GetAxisMinMax( aMin, aMax ) )
2790 return true;
2791 }
2792
2793 // Not locked: propose a default range that covers the data in *every* view sharing this
2794 // axis slot, not just the first view (which might not have any traces of this kind at all,
2795 // e.g. no Current trace in view 1 even though later views plot one).
2796 bool haveRange = false;
2797 double min = 0.0, max = 0.0;
2798
2799 for( SIM_VIEW* view : m_views )
2800 {
2801 mpScaleY* axis = view->GetAxisBySlot( aSlot );
2802
2803 if( !axis || !axis->IsRangeSet() )
2804 continue;
2805
2806 double viewMin, viewMax;
2807 axis->GetDataRange( viewMin, viewMax );
2808
2809 if( !haveRange )
2810 {
2811 min = viewMin;
2812 max = viewMax;
2813 haveRange = true;
2814 }
2815 else
2816 {
2817 min = std::min( min, viewMin );
2818 max = std::max( max, viewMax );
2819 }
2820 }
2821
2822 if( haveRange )
2823 {
2824 *aMin = min;
2825 *aMax = max;
2826 }
2827
2828 return false;
2829}
2830
2831
2832bool SIM_PLOT_TAB::GetY1Scale( double* aMin, double* aMax ) const
2833{
2834 return getYScale( 1, aMin, aMax );
2835}
2836
2837
2838bool SIM_PLOT_TAB::GetY2Scale( double* aMin, double* aMax ) const
2839{
2840 return getYScale( 2, aMin, aMax );
2841}
2842
2843
2844bool SIM_PLOT_TAB::GetY3Scale( double* aMin, double* aMax ) const
2845{
2846 return getYScale( 3, aMin, aMax );
2847}
2848
2849
2850void SIM_PLOT_TAB::SetY1Scale( bool aLock, double aMin, double aMax )
2851{
2852 for( SIM_VIEW* view : m_views )
2853 view->SetY1Scale( aLock, aMin, aMax );
2854}
2855
2856
2857void SIM_PLOT_TAB::SetY2Scale( bool aLock, double aMin, double aMax )
2858{
2859 for( SIM_VIEW* view : m_views )
2860 view->SetY2Scale( aLock, aMin, aMax );
2861}
2862
2863
2864void SIM_PLOT_TAB::SetY3Scale( bool aLock, double aMin, double aMax )
2865{
2866 for( SIM_VIEW* view : m_views )
2867 view->SetY3Scale( aLock, aMin, aMax );
2868}
2869
2870
2872{
2873 for( SIM_VIEW* view : m_views )
2874 view->EnsureThirdYAxisExists();
2875}
2876
2877
2879{
2880 for( SIM_VIEW* view : m_views )
2881 {
2882 view->SetColourTheme( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::BACKGROUND ),
2885
2886 view->UpdateSmithGridColor();
2887 view->UpdateAll();
2888 }
2889}
2890
2891
2893{
2894 m_trackCursorEnabled = aEnable;
2895
2896 if( !aEnable )
2898}
2899
2900
2902{
2903 // only redraw when the crosshair actually moved (e.g. not for vertical-only mouse motion)
2904 for( SIM_VIEW* view : m_views )
2905 {
2906 if( view->SetTrackCursorPlotX( aPlotX ) )
2907 view->UpdateAll();
2908 }
2909}
2910
2911
2913{
2914 for( SIM_VIEW* view : m_views )
2915 {
2916 if( view->HideTrackCursor() )
2917 view->UpdateAll();
2918 }
2919}
2920
2921
2922void SIM_PLOT_TAB::SetSmithMode( bool aEnable )
2923{
2924 // only S-parameter tabs have a Smith view, a stale workbook cannot force one elsewhere
2925 if( aEnable && GetSimType() != ST_SP )
2926 return;
2927
2928 if( m_smithMode == aEnable )
2929 return;
2930
2931 m_smithMode = aEnable;
2932
2933 for( SIM_VIEW* view : m_views )
2934 view->SetSmithChart( aEnable );
2935}
2936
2937
2939{
2940 for( SIM_VIEW* view : m_views )
2941 view->UpdateSmithReferenceImpedance();
2942}
2943
2944
2946{
2947 SIM_VIEW* view = GetDefaultView();
2948 return view ? view->GetSmithZoom() : 1.0;
2949}
2950
2951
2952wxRealPoint SIM_PLOT_TAB::GetSmithPan() const
2953{
2954 SIM_VIEW* view = GetDefaultView();
2955 return view ? view->GetSmithPan() : wxRealPoint( 0.0, 0.0 );
2956}
2957
2958
2960{
2961 for( SIM_VIEW* view : m_views )
2962 {
2963 view->ResetSmithView();
2964 view->Refresh();
2965 }
2966}
2967
2968
2969void SIM_PLOT_TAB::SetSmithView( double aZoom, double aPanX, double aPanY )
2970{
2971 for( SIM_VIEW* view : m_views )
2972 view->SetSmithView( aZoom, aPanX, aPanY );
2973}
2974
2975
2976void SIM_PLOT_TAB::SmithZoomAt( const wxPoint& aPos, double aFactor )
2977{
2978 for( SIM_VIEW* view : m_views )
2979 view->SmithZoomAt( aPos, aFactor );
2980}
2981
2982
2983void SIM_PLOT_TAB::SmithPanBy( const wxPoint& aDelta )
2984{
2985 for( SIM_VIEW* view : m_views )
2986 view->SmithPanBy( aDelta );
2987}
2988
2989
2991{
2992 for( SIM_VIEW* view : m_views )
2993 {
2994 view->updateAxes();
2995 view->UpdateAll();
2996 }
2997}
2998
2999
3001{
3002 int type = trace->GetType();
3003 wxPenStyle penStyle;
3004
3005 if( ( type & SPT_AC_GAIN ) > 0 )
3006 penStyle = wxPENSTYLE_SOLID;
3007 else if( ( type & SPT_AC_PHASE ) > 0 )
3008 penStyle = m_dotted_cp ? wxPENSTYLE_DOT : wxPENSTYLE_SOLID;
3009 else if( ( type & SPT_CURRENT ) > 0 )
3010 penStyle = m_dotted_cp ? wxPENSTYLE_DOT : wxPENSTYLE_SOLID;
3011 else
3012 penStyle = wxPENSTYLE_SOLID;
3013
3014 trace->SetPen( wxPen( trace->GetTraceColour(), 2, penStyle ) );
3015 m_sessionTraceColors[trace->GetName()] = trace->GetTraceColour();
3016}
3017
3018
3019TRACE* SIM_PLOT_TAB::GetOrAddTrace( const wxString& aVectorName, int aType, SIM_VIEW* aView )
3020{
3021 TRACE* trace = GetTrace( aVectorName, aType );
3022
3023 if( !trace && aView )
3024 {
3025 aView->updateAxes( aType );
3026
3027 if( GetSimType() == ST_TRAN || GetSimType() == ST_DC )
3028 {
3029 bool hasVoltageTraces = false;
3030
3031 for( const auto& [id, candidate] : m_traces )
3032 {
3033 if( candidate->GetView() == aView && ( candidate->GetType() & SPT_VOLTAGE ) )
3034 {
3035 hasVoltageTraces = true;
3036 break;
3037 }
3038 }
3039
3040 if( !hasVoltageTraces )
3041 {
3042 if( aView->m_axis_y2 )
3043 aView->m_axis_y2->SetMasterScale( nullptr );
3044
3045 if( aView->m_axis_y3 )
3046 aView->m_axis_y3->SetMasterScale( nullptr );
3047 }
3048 }
3049
3050 if( aType & SPT_SP_SMITH )
3051 trace = new SMITH_TRACE( aVectorName, (SIM_TRACE_TYPE) aType );
3052 else
3053 trace = new TRACE( aVectorName, (SIM_TRACE_TYPE) aType );
3054
3055 if( m_sessionTraceColors.count( aVectorName ) )
3056 trace->SetTraceColour( m_sessionTraceColors[aVectorName] );
3057 else
3058 trace->SetTraceColour( m_colors.GenerateColor( m_sessionTraceColors ) );
3059
3060 UpdateTraceStyle( trace );
3061 m_traces[getTraceId( aVectorName, aType )] = trace;
3062
3063 trace->SetView( aView );
3064 aView->AddLayer( (mpLayer*) trace );
3065 aView->RaiseOverlays();
3066 }
3067
3068 return trace;
3069}
3070
3071
3072void SIM_PLOT_TAB::SetTraceData( TRACE* trace, std::vector<double>& aX, std::vector<double>& aY, int aSweepCount,
3073 size_t aSweepSize, bool aIsMultiRun, const std::vector<wxString>& aMultiRunLabels )
3074{
3075 SIM_VIEW* view = trace->GetView();
3076
3077 wxCHECK( view, /* void */ );
3078
3079 // smith traces carry Re/Im of the reflection coefficient, not frequency
3080 bool smithTrace = ( trace->GetType() & SPT_SP_SMITH ) > 0;
3081
3082 if( dynamic_cast<LOG_SCALE<mpScaleXLog>*>( view->m_axis_x ) && !smithTrace )
3083 {
3084 // log( 0 ) is not valid.
3085 if( aX.size() > 0 && aX[0] == 0 )
3086 {
3087 aX.erase( aX.begin() );
3088 aY.erase( aY.begin() );
3089 }
3090 }
3091
3092 if( GetSimType() == ST_AC || GetSimType() == ST_FFT )
3093 {
3094 if( trace->GetType() & SPT_AC_PHASE )
3095 {
3096 for( double& pt : aY )
3097 pt = pt * 180.0 / M_PI; // convert to degrees
3098 }
3099 else
3100 {
3101 for( double& pt : aY )
3102 pt = MagnitudeToDb( pt ); // NaN where there is no signal
3103 }
3104 }
3105
3106 trace->SetData( aX, aY );
3107 trace->SetSweepCount( aSweepCount );
3108 trace->SetSweepSize( aSweepSize );
3109 trace->SetIsMultiRun( aIsMultiRun );
3110 trace->SetMultiRunLabels( aMultiRunLabels );
3111
3112 // Phase and currents on second Y axis, except for AC currents, those use the same axis as voltage
3113 if( smithTrace )
3114 {
3115 // drawn through the chart geometry, not the axis transforms
3116 trace->SetScale( nullptr, nullptr );
3117 }
3118 else if( ( trace->GetType() & SPT_AC_PHASE )
3119 || ( ( GetSimType() != ST_AC ) && ( trace->GetType() & SPT_CURRENT ) ) )
3120 {
3121 trace->SetScale( view->m_axis_x, view->m_axis_y2 );
3122 }
3123 else if( trace->GetType() & SPT_POWER )
3124 {
3125 trace->SetScale( view->m_axis_x, view->m_axis_y3 );
3126 }
3127 else
3128 {
3129 trace->SetScale( view->m_axis_x, view->m_axis_y1 );
3130 }
3131
3132 for( auto& [cursorId, cursor] : trace->GetCursors() )
3133 {
3134 if( cursor )
3135 cursor->UpdateForNewData();
3136 }
3137
3138 view->UpdateAxisVisibility();
3139}
3140
3141
3143{
3144 for( const auto& [name, trace] : m_traces )
3145 {
3146 if( trace == aTrace )
3147 {
3148 m_traces.erase( name );
3149 break;
3150 }
3151 }
3152
3153 if( SIM_VIEW* view = aTrace->GetView() )
3154 {
3155 for( const auto& [id, cursor] : aTrace->GetCursors() )
3156 {
3157 if( cursor )
3158 view->DelLayer( cursor, true );
3159 }
3160
3161 view->DelLayer( aTrace, true, true );
3162 view->UpdateAxisVisibility();
3163 view->UpdateSmithReferenceImpedance();
3164 }
3165
3166 ResetScales( false );
3167}
3168
3169
3170bool SIM_PLOT_TAB::DeleteTrace( const wxString& aVectorName, int aTraceType )
3171{
3172 if( TRACE* trace = GetTrace( aVectorName, aTraceType ) )
3173 {
3174 DeleteTrace( trace );
3175 return true;
3176 }
3177
3178 return false;
3179}
3180
3181
3182void SIM_PLOT_TAB::EnableCursor( TRACE* aTrace, int aCursorId, const wxString& aSignalName )
3183{
3184 SIM_VIEW* view = aTrace->GetView();
3185
3186 wxCHECK( view, /* void */ );
3187
3188 CURSOR* cursor;
3189
3190 if( aTrace->GetType() & SPT_SP_SMITH )
3191 {
3192 SMITH_TRACE* smithTrace = static_cast<SMITH_TRACE*>( aTrace );
3193
3194 cursor = new SMITH_CURSOR( smithTrace, this );
3195
3196 // start somewhere on the locus, biased per cursor id like the rectangular case
3197 const std::vector<double>& freqs = smithTrace->GetFrequencies();
3198
3199 if( !freqs.empty() )
3200 cursor->SetCoordX( freqs[freqs.size() * ( aCursorId == 1 ? 2 : 3 ) / 5] );
3201 }
3202 else
3203 {
3204 int width = view->GetXScreen() - view->GetMarginLeft() - view->GetMarginRight();
3205 int center = view->GetMarginLeft() + KiROUND( width * ( aCursorId == 1 ? 0.4 : 0.6 ) );
3206
3207 cursor = new CURSOR( aTrace, this );
3208
3209 cursor->SetX( center );
3210 }
3211
3212 cursor->SetName( aSignalName );
3213
3214 aTrace->SetCursor( aCursorId, cursor );
3215 view->AddLayer( cursor );
3216 view->RaiseOverlays();
3217
3218 // Notify the parent window about the changes
3219 wxQueueEvent( this, new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
3220}
3221
3222
3223void SIM_PLOT_TAB::DisableCursor( TRACE* aTrace, int aCursorId )
3224{
3225 if( CURSOR* cursor = aTrace->GetCursor( aCursorId ) )
3226 {
3227 aTrace->SetCursor( aCursorId, nullptr );
3228
3229 if( SIM_VIEW* view = aTrace->GetView() )
3230 view->DelLayer( cursor, true );
3231
3232 // Notify the parent window about the changes
3233 wxQueueEvent( this, new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
3234 }
3235}
3236
3237
3238void SIM_PLOT_TAB::ResetScales( bool aIncludeX )
3239{
3240 for( SIM_VIEW* view : m_views )
3241 view->ResetScales( aIncludeX );
3242
3243 // Merge Y-axis auto-fit ranges for traces whose "Y Scale" has been explicitly linked to
3244 // another view, so that the linked axes end up sharing the same numeric range -- as if all
3245 // the involved traces were plotted together in a single view.
3246 std::map<std::pair<SIM_VIEW*, int>, std::vector<TRACE*>> groups;
3247
3248 for( const auto& [name, trace] : m_traces )
3249 {
3250 if( SIM_VIEW* view = trace->GetView() )
3251 groups[{ trace->GetYScaleView(), view->GetAxisSlot( trace ) }].push_back( trace );
3252 }
3253
3254 for( const auto& [key, traces] : groups )
3255 {
3256 std::vector<SIM_VIEW*> actualViews;
3257
3258 for( TRACE* trace : traces )
3259 {
3260 SIM_VIEW* view = trace->GetView();
3261
3262 if( std::find( actualViews.begin(), actualViews.end(), view ) == actualViews.end() )
3263 actualViews.push_back( view );
3264 }
3265
3266 if( actualViews.size() <= 1 )
3267 continue; // All traces already share the same, single axis object.
3268
3269 bool haveRange = false;
3270 double minV = 0.0;
3271 double maxV = 0.0;
3272
3273 for( TRACE* trace : traces )
3274 {
3275 double lo = trace->GetMinY();
3276 double hi = trace->GetMaxY();
3277
3278 if( !std::isfinite( lo ) || !std::isfinite( hi ) )
3279 continue;
3280
3281 if( !haveRange )
3282 {
3283 minV = lo;
3284 maxV = hi;
3285 haveRange = true;
3286 }
3287 else
3288 {
3289 minV = std::min( minV, lo );
3290 maxV = std::max( maxV, hi );
3291 }
3292 }
3293
3294 if( !haveRange )
3295 continue;
3296
3297 for( SIM_VIEW* view : actualViews )
3298 {
3299 if( mpScaleY* axis = view->GetAxisBySlot( key.second ) )
3300 axis->SetDataRange( minV, maxV );
3301 }
3302 }
3303}
3304
3305
3307{
3308 return GetDefaultView();
3309}
3310
3311
3312wxDEFINE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
3313wxDEFINE_EVENT( EVT_SIM_VIEWS_CHANGED, wxCommandEvent );
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
The SIMULATOR_FRAME holds the main user-interface for running simulations.
mpWindow * m_window
void Move(wxPoint aDelta) override
Moves the layer rectangle of given pixel deltas.
wxString getID()
wxRealPoint m_coords
bool m_updateRef
static constexpr int DRAG_MARGIN
bool Inside(const wxPoint &aPoint) const override
Checks whether a point is inside the info box rectangle.
void doSetCoordX(double aValue)
int m_sweepIndex
bool OnDoubleClick(const wxPoint &aPoint, mpWindow &aWindow) override
virtual void SetCoordX(double aValue)
void UpdateReference() override
Updates the rectangle reference point.
bool m_updateRequired
double m_snapTargetY
TRACE * m_trace
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
bool m_snapToNearest
void Update()
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D & Invert()
Makes the color inverted, alpha remains the same.
Definition color4d.h:239
wxColour ToColour() const
Definition color4d.cpp:221
double Distance(const COLOR4D &other) const
Returns the distance (in RGB space) between two colors.
Definition color4d.cpp:551
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition color4d.h:293
void formatLabels() override
wxString GetUnits() const
const wxString m_unit
LIN_SCALE(const wxString &name, const wxString &unit, int flags)
wxString m_base_axis_label
wxString GetUnits() const
const wxString m_unit
LOG_SCALE(const wxString &name, const wxString &unit, int flags)
void formatLabels() override
bool DeleteTrace(const wxString &aVectorName, int aTraceType)
double GetSmithZoom() const
The saved/restored pan and zoom track the default view.
void UpdateSmithReferenceImpedance()
Refresh the grid z0 from the shown Smith traces, on every view.
wxString GetLabelY1() const
mpWindow * GetPlotWin() const
Getter for the default view's math plot window (back-compat for zoom undo/redo, export).
void SetSmithView(double aZoom, double aPanX, double aPanY)
Restore a saved view, values are validated and clamped.
void EnableTrackCursor(bool aEnable)
Turn on/off the crosshair that follows the mouse and reads out every trace's value.
bool getYScale(int aSlot, double *aMin, double *aMax) const
void ShowGrid(bool aEnable)
void SetTraceData(TRACE *aTrace, std::vector< double > &aX, std::vector< double > &aY, int aSweepCount, size_t aSweepSize, bool aIsMultiRun=false, const std::vector< wxString > &aMultiRunLabels={})
void SyncXView(SIM_VIEW *aSource)
Mirror aSource's current X range onto every other view of this tab.
const std::vector< SIM_VIEW * > & GetViews() const
wxString GetUnitsY2() const
void SetY2Scale(bool aLock, double aMin, double aMax)
void SmithZoomAt(const wxPoint &aPos, double aFactor)
TRACE * GetTrace(const wxString &aVecName, int aType) const
void SetSmithMode(bool aEnable)
Smith mode is tab-wide: every view of the tab shows a Smith chart while it is on.
virtual ~SIM_PLOT_TAB()
wxString GetLabelX() const
bool IsGridShown() const
wxRealPoint GetSmithPan() const
wxString GetLabelY3() const
void SetY1Scale(bool aLock, double aMin, double aMax)
wxPoint GetLegendPosition() const
void SetTrackCursorX(double aPlotX)
Show the track cursor of every view at a shared plot-space X.
void SetY3Scale(bool aLock, double aMin, double aMax)
std::map< wxString, TRACE * > m_traces
bool IsLegendShown() const
SIM_VIEW * GetDefaultView() const
The view new traces default to, and legacy (single-view) settings apply to.
SIM_VIEW * AddView()
Add a new (empty) view, stacked below the existing ones.
wxString GetUnitsForTrace(TRACE *aTrace) const
Get the display units (e.g. "V", "A", "dB") for a given trace's own view.
SIM_PLOT_COLORS m_colors
void UpdateTraceStyle(TRACE *trace)
Update trace line style.
void SetLegendPosition(const wxPoint &aPosition)
TRACE * GetOrAddTrace(const wxString &aVectorName, int aType, SIM_VIEW *aView)
Get (or create, on aView) the TRACE for a signal.
void ResetScales(bool aIncludeX)
Reset scale ranges to fit the current traces, on every view.
void UpdatePlotColors()
Update plot colors.
bool m_trackCursorEnabled
void ShowLegend(bool aEnable)
bool GetY3Scale(double *aMin, double *aMax) const
SIM_PLOT_TAB(const wxString &aSimCommand, wxWindow *parent)
wxString GetLabelY2() const
void EnableCursor(TRACE *aTrace, int aCursorId, const wxString &aSignalName)
Turn on/off the cursor for a particular trace.
int GetViewIndex(SIM_VIEW *aView) const
wxString GetUnitsX() const
void OnLanguageChanged() override
void EnsureThirdYAxisExists()
wxString getTraceId(const wxString &aVectorName, int aType) const
Shared implementation of GetY1Scale/GetY2Scale/GetY3Scale for axis slot 1/2/3.
void ApplyPreferences(const SIM_PREFERENCES &aPrefs) override
wxBoxSizer * m_viewsSizer
mpWindow::MouseWheelActionSet m_mouseWheelActions
bool RemoveView(SIM_VIEW *aView)
Remove a view.
void SmithPanBy(const wxPoint &aDelta)
std::vector< SIM_VIEW * > m_views
wxPoint m_LastLegendPosition
wxString GetUnitsY1() const
std::map< wxString, wxColour > m_sessionTraceColors
static mpWindow::MouseWheelActionSet convertMouseWheelActions(const SIM_MOUSE_WHEEL_ACTION_SET &s)
void DisableCursor(TRACE *aTrace, int aCursorId)
bool GetY2Scale(double *aMin, double *aMax) const
bool GetY1Scale(double *aMin, double *aMax) const
wxString GetUnitsY3() const
SIM_TAB()
Definition sim_tab.cpp:29
SIM_TYPE GetSimType() const
Definition sim_tab.cpp:71
A single stacked plot area within a SIM_PLOT_TAB.
wxString GetLabelY2() const
void onPlotMouseLeave(wxMouseEvent &aEvent)
void OnXViewChanged() override
Mirrors this view's X range onto every other view of the same tab.
void updateAxes(int aNewTraceType=SIM_TRACE_TYPE::SPT_UNKNOWN)
Create/Ensure axes are available for plotting.
void SmithZoomAt(const wxPoint &aPos, double aFactor)
void onSmithMenuCommand(wxCommandEvent &aEvent)
mpScaleY * m_axis_y3
wxString GetUnitsForTrace(TRACE *aTrace) const
Get the display units (e.g. "V", "A", "dB") for a given trace plotted on this view.
bool m_smithPanning
void UpdateSmithGridColor()
Re-read the Smith grid pen from the tab's color theme.
void SetY2Scale(bool aLock, double aMin, double aMax)
void EnsureThirdYAxisExists()
wxPoint m_smithMenuPos
void SmithPanBy(const wxPoint &aDelta)
mpInfoLegend * m_legend
void ResetSmithView()
void onSmithDClick(wxMouseEvent &aEvent)
wxString GetLabelX() const
bool m_smithLeftSkipped
wxPoint m_lastLegendPosition
wxString GetLabelY3() const
wxString GetUnitsY3() const
wxPoint GetLegendPosition() const
void onPlotMouseMove(wxMouseEvent &aEvent)
bool HideTrackCursor()
void onSmithMagnify(wxMouseEvent &aEvent)
void SetY1Scale(bool aLock, double aMin, double aMax)
const wxRealPoint & GetSmithPan() const
void SetY3Scale(bool aLock, double aMin, double aMax)
TRACK_CURSOR * m_trackCursor
void onSmithMiddleDown(wxMouseEvent &aEvent)
void onSmithRightDown(wxMouseEvent &aEvent)
SIM_VIEW(SIM_PLOT_TAB *aPlotTab, wxWindow *aParent)
bool IsLegendShown() const
mpScaleY * GetAxisBySlot(int aSlot) const
Get the Y-axis scale object for a given slot (1, 2 or 3), or nullptr if not created yet.
double GetSmithZoom() const
bool getSmithView(SMITH_VIEW &aView) const
void RaiseOverlays()
Move the floating overlays (legend, track cursor) to the end of the layer stack so they draw on top o...
void onSmithLeftUp(wxMouseEvent &aEvent)
wxString GetLabelY1() const
void UpdateSmithReferenceImpedance()
Refresh the grid z0 from the Smith traces shown on this view.
mpScaleY * m_axis_y2
SMITH_GRID * m_smithGrid
void onSmithLeftDown(wxMouseEvent &aEvent)
void ResetScales(bool aIncludeX)
Reset the Y (and, if requested, X) scale ranges to fit this view's own traces.
mpScaleXBase * m_axis_x
int GetAxisSlot(TRACE *aTrace) const
Get the Y-axis slot (1, 2 or 3) a trace of this type is plotted on.
SIM_PLOT_TAB * m_plotTab
mpScaleY * m_axis_y1
void ShowLegend(bool aEnable)
wxString GetUnitsX() const
void onSmithRightUp(wxMouseEvent &aEvent)
void SetSmithChart(bool aEnable)
Show/hide this view's Smith chart overlay.
bool SetTrackCursorPlotX(double aPlotX)
Position this view's track cursor at a shared plot-space X.
bool m_smithChart
void UpdateAxisVisibility()
void onSmithMotion(wxMouseEvent &aEvent)
wxString GetUnitsY2() const
wxPoint m_smithPanLast
wxString GetUnitsY1() const
void onSmithMouseWheel(wxMouseEvent &aEvent)
double m_smithZoom
bool IsGridShown() const
wxRealPoint m_smithPan
void prepareDCAxes(int aNewTraceType)
Construct the plot axes for DC simulation plot.
Cursor that snaps along a Smith chart locus, keyed by frequency.
void UpdateReference() override
Updates the rectangle reference point.
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
bool Inside(const wxPoint &aPoint) const override
Checks whether a point is inside the info box rectangle.
void SetCoordX(double aValue) override
double m_requestFreq
void snapToIndex(int aIndex)
void snapToFrequency(double aFreq)
void UpdateForNewData() override
wxRealPoint m_gamma
void Move(wxPoint aDelta) override
Moves the layer rectangle of given pixel deltas.
Overlay layer drawing the Smith chart grid (constant resistance and reactance circles)
static bool GetChartView(mpWindow &aWindow, double aZoom, const wxRealPoint &aPan, SMITH_VIEW &aView)
Chart placement including pan/zoom.
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot given view of layer to the given device context.
bool m_mixedReferences
Reflection coefficient locus, Re in X and Im in Y, drawn on the Smith chart.
void Plot(wxDC &aDC, mpWindow &aWindow) override
Layer plot handler.
const std::vector< double > & GetFrequencies() const
double GetReferenceImpedance() const
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition spice_value.h:51
wxString ToString() const
Return string value as when converting double to string (e.g.
double ToDouble() const
A bitmap button widget that behaves like a standard dialog button except with an icon.
void SetBitmap(const wxBitmapBundle &aBmp)
void ResetDataRange() override
True once ExtendDataRange()/SetDataRange() has established a real data range.
void ExtendDataRange(double minV, double maxV) override
void SetStartAndEnd(double aStartTime, double aEndTime)
double m_startTime
TIME_SCALE(const wxString &name, const wxString &unit, int flags)
void SetIsMultiRun(bool aIsMultiRun)
void SetTraceColour(const wxColour &aColour)
void SetMultiRunLabels(const std::vector< wxString > &aLabels)
std::map< int, CURSOR * > & GetCursors()
SIM_VIEW * GetView() const
The SIM_VIEW this trace is currently plotted on (nullptr if not plotted anywhere).
void SetView(SIM_VIEW *aView)
SIM_TRACE_TYPE GetType() const
bool m_isMultiRun
void SetData(const std::vector< double > &aX, const std::vector< double > &aY) override
Assigns new data set for the trace.
bool GetValueAtX(double aX, double *aY) const
Linearly interpolate this trace's value at a given X (in the trace's own data space).
void SetCursor(int aCursorId, CURSOR *aCursor)
const std::vector< double > & GetDataY() const
wxColour GetTraceColour() const
const std::vector< double > & GetDataX() const
CURSOR * GetCursor(int aCursorId)
A crosshair that follows the mouse pointer (rather than being manually positioned) and shows every tr...
SIM_PLOT_TAB * m_plotTab
SIM_VIEW * m_view
void Plot(wxDC &aDC, mpWindow &aWindow) override
Plot method.
void SetSweepSize(size_t aSweepSize)
Definition mathplot.h:1493
std::vector< double > m_ys
Definition mathplot.h:1523
std::vector< double > m_xs
The internal copy of the set of data to draw.
Definition mathplot.h:1523
size_t GetSweepSize() const
Definition mathplot.h:1494
void SetSweepCount(int aSweepCount)
Definition mathplot.h:1492
int GetSweepCount() const override
Definition mathplot.h:1518
virtual void SetScale(mpScaleBase *scaleX, mpScaleBase *scaleY)
wxPoint m_reference
Definition mathplot.h:417
wxRect m_dim
Definition mathplot.h:415
wxPoint GetPosition() const
Returns the position of the upper left corner of the box (in pixels)
Definition mathplot.cpp:197
virtual void UpdateReference()
Updates the rectangle reference point.
Definition mathplot.cpp:152
virtual void Move(wxPoint delta)
Moves the layer rectangle of given pixel deltas.
Definition mathplot.cpp:145
Implements the legend to be added to the plot This layer allows you to add a legend to describe the p...
Definition mathplot.h:430
const wxString & GetName() const
Get layer name.
Definition mathplot.h:256
bool m_continuous
Specify if the layer will be plotted as a continuous line or a set of points.
Definition mathplot.h:345
bool m_visible
Toggles layer visibility.
Definition mathplot.h:348
const wxPen & GetPen() const
Get pen set for this layer.
Definition mathplot.h:287
void SetVisible(bool show)
Sets layer visibility.
Definition mathplot.h:328
const wxFont & GetPlotFont() const
Get the font to draw this layer with, which is the default one when none was set.
Definition mathplot.h:276
void SetPen(const wxPen &pen)
Set layer pen.
Definition mathplot.h:316
wxPen m_pen
Layer's pen.
Definition mathplot.h:341
void GetDataRange(double &minV, double &maxV) const
Definition mathplot.h:679
bool m_rangeSet
Definition mathplot.h:801
double m_maxV
Definition mathplot.h:800
bool IsRangeSet() const
Definition mathplot.h:729
double m_minV
Definition mathplot.h:800
Plot layer implementing a y-scale ruler.
Definition mathplot.h:889
void SetMasterScale(mpScaleY *masterScale)
Definition mathplot.h:914
Canvas for plotting mpLayer implementations.
Definition mathplot.h:970
int GetMarginLeft() const
Definition mathplot.h:1285
void SetMargins(int top, int right, int bottom, int left)
Set window margins, creating a blank area where some kinds of layers cannot draw.
int GetScrX() const
Get current view's X dimension in device context units.
Definition mathplot.h:1091
int GetScrY() const
Get current view's Y dimension in device context units.
Definition mathplot.h:1100
void SetMouseWheelActions(const MouseWheelActionSet &s)
Set the pan/zoom actions corresponding to mousewheel/trackpad events.
Definition mathplot.h:1172
double p2x(wxCoord pixelCoordX)
Converts mpWindow (screen) pixel coordinates into graph (floating point) coordinates,...
Definition mathplot.h:1145
void LimitView(bool aEnable)
Enable limiting of zooming & panning to the area used by the plots.
Definition mathplot.h:1324
wxCoord x2p(double x)
Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates,...
Definition mathplot.h:1155
mpInfoLayer * IsInsideInfoLayer(wxPoint &point)
Check if a given point is inside the area of a mpInfoLayer and eventually returns its pointer.
int GetXScreen() const
Definition mathplot.h:1092
int GetMarginTop() const
Definition mathplot.h:1279
bool DelLayer(mpLayer *layer, bool alsoDeleteObject=false, bool refreshDisplay=true)
Remove a plot layer from the canvas.
void UpdateAll()
Refresh display.
wxCoord y2p(double y)
Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates,...
Definition mathplot.h:1160
double GetDesiredXmax() const
Returns the right-border layer coordinate that the user wants the mpWindow to show (it may be not exa...
Definition mathplot.h:1228
wxMenu * GetPopupMenu()
Get reference to context menu of the plot canvas.
Definition mathplot.h:1011
double GetDesiredXmin() const
Returns the left-border layer coordinate that the user wants the mpWindow to show (it may be not exac...
Definition mathplot.h:1222
int GetMarginRight() const
Definition mathplot.h:1281
int GetMarginBottom() const
Definition mathplot.h:1283
void EnableDoubleBuffer(bool enabled)
Enable/disable the double-buffering of the window, eliminating the flicker (default=disabled).
Definition mathplot.h:1165
double GetScaleX() const
Get current view's X scale.
Definition mathplot.h:1065
bool AddLayer(mpLayer *layer, bool refreshDisplay=true)
Add a plot layer to the canvas.
double GetPosX() const
Get current view's X position.
Definition mathplot.h:1077
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
#define mpALIGN_BORDER_RIGHT
Aligns Y axis to right border.
Definition mathplot.h:493
#define mpALIGN_RIGHT
Aligns label to the right.
Definition mathplot.h:464
#define mpALIGN_LEFT
Aligns label to the left.
Definition mathplot.h:468
@ mpID_ZOOM_REDO
Definition mathplot.h:134
@ mpID_FIT
Definition mathplot.h:132
@ mpID_ZOOM_IN
Definition mathplot.h:135
@ mpID_CENTER
Definition mathplot.h:137
@ mpID_ZOOM_UNDO
Definition mathplot.h:133
@ mpID_ZOOM_OUT
Definition mathplot.h:136
#define mpALIGN_BOTTOM
Aligns label to the bottom.
Definition mathplot.h:472
KICOMMON_API wxFont GetStatusFont(wxWindow *aWindow)
double SeriesCapacitance(double aReactance, double aFreq)
Definition smith_math.h:101
double SeriesInductance(double aReactance, double aFreq)
Series equivalent element of a reactance at one frequency, henries for aReactance > 0,...
Definition smith_math.h:96
double VSWR(double aGammaMag)
Definition smith_math.h:78
wxRealPoint ZoomAboutPoint(const SMITH_VIEW &aView, const wxPoint &aPos, double aNewZoom)
Pan that keeps the gamma point under aPos fixed when the view zooms to aNewZoom.
Definition smith_math.h:107
bool GammaToImpedance(double aRe, double aIm, double aZ0, double &aResistance, double &aReactance)
Impedance of a reflection coefficient, z = z0 ( 1 + gamma ) / ( 1 - gamma ), false at the gamma = 1 s...
Definition smith_math.h:65
double ReturnLoss(double aGammaMag)
Definition smith_math.h:86
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
Class is responsible for providing colors for traces on simulation plot.
wxDEFINE_EVENT(EVT_SIM_CURSOR_UPDATE, wxCommandEvent)
static void getSISuffix(double x, const wxString &unit, int &power, wxString &suffix)
static int countDecimalDigits(double x, int maxDigits)
static bool smithView(mpWindow &aWindow, SMITH_VIEW &aView)
static wxString formatFloat(double x, int nDigits)
SIM_TRACE_TYPE
Possible trace types.
Definition sim_types.h:49
@ SPT_AC_PHASE
Definition sim_types.h:53
@ SPT_AC_GAIN
Definition sim_types.h:54
@ SPT_VOLTAGE
Definition sim_types.h:51
@ SPT_POWER
Definition sim_types.h:55
@ SPT_CURRENT
Definition sim_types.h:52
@ SPT_SP_SMITH
Definition sim_types.h:57
double MagnitudeToDb(double aMagnitude)
Convert a linear magnitude to decibels.
Definition sim_types.h:82
@ ST_SP
Definition sim_types.h:42
@ ST_TRAN
Definition sim_types.h:41
@ ST_NOISE
Definition sim_types.h:36
@ ST_AC
Definition sim_types.h:33
@ ST_DC
Definition sim_types.h:34
@ ST_FFT
Definition sim_types.h:43
static SIM_MOUSE_WHEEL_ACTION_SET GetMouseDefaults()
Contains preferences pertaining to the simulator.
SIM_MOUSE_WHEEL_ACTION_SET mouse_wheel_actions
Smith chart placement plus pan/zoom, maps a gamma point to a screen pixel and back.
Definition smith_math.h:32
wxRealPoint pan
Definition smith_math.h:36
double zoom
Definition smith_math.h:35
wxRealPoint ToGamma(const wxPoint &aPt) const
Definition smith_math.h:53
wxPoint center
Definition smith_math.h:33
wxPoint ToScreen(double aRe, double aIm) const
Definition smith_math.h:39
double radius
Definition smith_math.h:34
wxRect plotRect
Definition smith_math.h:37
A SPICE_VALUE_FORMAT holds precision and range info for formatting values.
Definition spice_value.h:38
DATABASE_CONNECTION dc(odbcBackend(cs))
std::vector< DATABASE_CONNECTION::ROW > rows
VECTOR2I center
int radius
VECTOR2I end
int delta
#define M_PI