summaryrefslogtreecommitdiff
path: root/gr-qtgui/src/lib/Waterfall3DDisplayPlot.cc
blob: f676cb4af2ac529de9295b7bf428ebfd9b8972e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#ifndef WATERFALL_3D_DISPLAY_PLOT_C
#define WATERFALL_3D_DISPLAY_PLOT_C

#include <Waterfall3DDisplayPlot.h>

#include <qwt3d_helper.h>
#include <qapplication.h>

Waterfall3DColorMap::Waterfall3DColorMap(): Qwt3D::Color(), QwtLinearColorMap(){
  _interval.setInterval(0, 1.0);

}

Waterfall3DColorMap::~Waterfall3DColorMap(){

}

Qwt3D::RGBA
Waterfall3DColorMap::operator()(double, double, double z) const
{
  return Qwt3D::RGBA(Qwt3D::Qt2GL(color(_interval, z)));
}

void
Waterfall3DColorMap::SetInterval(const double minValue, const double maxValue)
{
  _interval.setInterval(minValue, maxValue);
}

Qwt3D::ColorVector&
Waterfall3DColorMap::createVector(Qwt3D::ColorVector& vec)
{
  // Generate 100 interval values and then return those
  Qwt3D::ColorVector colorVec;
  for(unsigned int number = 0; number < 100; number++){
    double value = (_interval.width() * (static_cast<double>(number) / 100.0)) + _interval.minValue();
    colorVec.push_back(operator()(0,0,value));
  }
  vec = colorVec;
  return vec; 
}


const int Waterfall3DDisplayPlot::INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
const int Waterfall3DDisplayPlot::INTENSITY_COLOR_MAP_TYPE_WHITE_HOT;
const int Waterfall3DDisplayPlot::INTENSITY_COLOR_MAP_TYPE_BLACK_HOT;
const int Waterfall3DDisplayPlot::INTENSITY_COLOR_MAP_TYPE_INCANDESCENT;
const int Waterfall3DDisplayPlot::INTENSITY_COLOR_MAP_TYPE_USER_DEFINED;

Waterfall3DDisplayPlot::Waterfall3DDisplayPlot(QWidget* parent):Qwt3D::SurfacePlot(parent)
{
  _startFrequency = 0;
  _stopFrequency = 4000;

  _createCoordinateSystemFlag = true;

  _initialized = false;

  _numPoints = 1024;

  _displayIntervalTime = (1.0/5.0); // 1/5 of a second between updates

  timespec_reset(&_lastReplot);

  _useCenterFrequencyFlag = false;
  _centerFrequency = 0.0;

  _timePerFFT = 1.0;
  timespec_reset(&_dataTimestamp);

  coordinates()->setAutoScale(false);

  _waterfallData = new Waterfall3DData(_startFrequency, _stopFrequency, _numPoints, 200);
  _waterfallData->assign(this);
  _waterfallData->create();

  _intensityColorMapType = -1;
  SetIntensityColorMapType(INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR, Qt::white, Qt::black, true, true);

  legend()->setScale(Qwt3D::LINEARSCALE);
  legend()->setTitleString("Intensity");

  enableMouse(true);  
}

Waterfall3DDisplayPlot::~Waterfall3DDisplayPlot()
{
  delete _waterfallData;
}

void
Waterfall3DDisplayPlot::Init()
{
  if(!_initialized && initializedGL()){
    resize(parentWidget()->width(), parentWidget()->height());

    // Attempting to prevent valgrind uninitialized variable errors in QwtPlot3d::Drawable class
    glDisable(GL_POLYGON_OFFSET_FILL);
    
    setPlotStyle(Qwt3D::FILLED);
    
    setCoordinateStyle(Qwt3D::FRAME);
    setFloorStyle(Qwt3D::FLOORDATA);
    setOrtho(true);  

    _initialized = true;
  }
}

void
Waterfall3DDisplayPlot::Reset()
{
  _waterfallData->ResizeData(_startFrequency, _stopFrequency, _numPoints);
  _waterfallData->Reset();

  if(initializedGL()){
    setScale(1, static_cast<int>(((_stopFrequency - _startFrequency) / 200) ), 10);
  }

  _createCoordinateSystemFlag = true;

  timespec_reset(&_dataTimestamp);
  _timePerFFT = 1.0;
}

void
Waterfall3DDisplayPlot::SetFrequencyRange(const double constStartFreq,
					  const double constStopFreq,
					  const double constCenterFreq,
					  const bool useCenterFrequencyFlag,
					  const double units,
					  const std::string &strunits)
{
  double startFreq = constStartFreq / units;
  double stopFreq = constStopFreq / units;
  double centerFreq = constCenterFreq / units;

  if(stopFreq > startFreq) {
    _startFrequency = startFreq;
    _stopFrequency = stopFreq;

    _useCenterFrequencyFlag = useCenterFrequencyFlag;
    _centerFrequency = centerFreq;

    Reset();

    // Only replot if screen is visible
    if(isVisible()){
      replot();
    }
  }
}

bool
Waterfall3DDisplayPlot::loadFromData(double** data, unsigned int columns, unsigned int rows
				     ,double minx, double maxx, double miny, double maxy)
{

  Qwt3D::GridData* gridPtr = (Qwt3D::GridData*)actualData_p;
  
  gridPtr->setPeriodic(false,false);
  gridPtr->setSize(columns,rows);
  readIn(*gridPtr,data,columns,rows,minx,maxx,miny,maxy);
  calcNormals(*gridPtr);  
  
  updateData();
  updateNormals();
  
  if( _createCoordinateSystemFlag ){
    createCoordinateSystem();
    
    for (unsigned i=0; i!=coordinates()->axes.size(); ++i)
      {
	coordinates()->axes[i].setMajors(5);
	coordinates()->axes[i].setMinors(3);
      }
    
    coordinates()->axes[Qwt3D::Y1].setLabelString("Time");
    coordinates()->axes[Qwt3D::Y2].setLabelString("Time");
    coordinates()->axes[Qwt3D::Y3].setLabelString("Time");
    coordinates()->axes[Qwt3D::Y4].setLabelString("Time");
    coordinates()->axes[Qwt3D::Z1].setLabelString("Intensity (dB)");
    coordinates()->axes[Qwt3D::Z2].setLabelString("Intensity (dB)");
    coordinates()->axes[Qwt3D::Z3].setLabelString("Intensity (dB)");
    coordinates()->axes[Qwt3D::Z4].setLabelString("Intensity (dB)");

    coordinates()->axes[Qwt3D::X1].setLabelString((!_useCenterFrequencyFlag ? "Frequency (Hz)" : "Frequency (kHz)"));
    coordinates()->axes[Qwt3D::X2].setLabelString((!_useCenterFrequencyFlag ? "Frequency (Hz)" : "Frequency (kHz)"));
    coordinates()->axes[Qwt3D::X3].setLabelString((!_useCenterFrequencyFlag ? "Frequency (Hz)" : "Frequency (kHz)"));
    coordinates()->axes[Qwt3D::X4].setLabelString((!_useCenterFrequencyFlag ? "Frequency (Hz)" : "Frequency (kHz)"));

    // The QwtPlot3D Interface takes ownership of these items, so there is no need to delete them...
    coordinates()->axes[Qwt3D::X1].setScale(new FrequencyScale(_useCenterFrequencyFlag, _centerFrequency));
    coordinates()->axes[Qwt3D::X2].setScale(new FrequencyScale(_useCenterFrequencyFlag, _centerFrequency));
    coordinates()->axes[Qwt3D::X3].setScale(new FrequencyScale(_useCenterFrequencyFlag, _centerFrequency));
    coordinates()->axes[Qwt3D::X4].setScale(new FrequencyScale(_useCenterFrequencyFlag, _centerFrequency));

    coordinates()->axes[Qwt3D::Y1].setScale(new TimeScale(this));
    coordinates()->axes[Qwt3D::Y2].setScale(new TimeScale(this));
    coordinates()->axes[Qwt3D::Y3].setScale(new TimeScale(this));
    coordinates()->axes[Qwt3D::Y4].setScale(new TimeScale(this));

    coordinates()->axes[Qwt3D::Z1].setScale(new IntensityScale(_waterfallData->GetFloorValue()));
    coordinates()->axes[Qwt3D::Z2].setScale(new IntensityScale(_waterfallData->GetFloorValue()));
    coordinates()->axes[Qwt3D::Z3].setScale(new IntensityScale(_waterfallData->GetFloorValue()));
    coordinates()->axes[Qwt3D::Z4].setScale(new IntensityScale(_waterfallData->GetFloorValue()));

    _createCoordinateSystemFlag = false;
  }
  
  return true;
}

double
Waterfall3DDisplayPlot::GetStartFrequency() const
{
  return _startFrequency;
}

double
Waterfall3DDisplayPlot::GetStopFrequency() const
{
  return _stopFrequency;
}

void
Waterfall3DDisplayPlot::PlotNewData(const double* dataPoints,
				    const int64_t numDataPoints,
				    const double timePerFFT,
				    const timespec timestamp,
				    const int droppedFrames)
{
  if(numDataPoints > 0){
    if(numDataPoints != _numPoints){
      _numPoints = numDataPoints;

      Reset();

      if(isVisible()){
	replot();
      }

      _createCoordinateSystemFlag = true;

      _lastReplot = get_highres_clock();
    }

    _dataTimestamp = timestamp;
    _timePerFFT = timePerFFT;

    _waterfallData->addFFTData(dataPoints, numDataPoints, droppedFrames);
    _waterfallData->IncrementNumLinesToUpdate();
  }

  // Allow at least a 50% duty cycle
  if(diff_timespec(get_highres_clock(), _lastReplot) > _displayIntervalTime){
    // Only update when window is visible
    if(isVisible()){
      replot();
    }

    _lastReplot = get_highres_clock();
  }
}

void
Waterfall3DDisplayPlot::SetIntensityRange(const double minIntensity,
					  const double maxIntensity)
{
  _waterfallData->SetFloorValue(minIntensity);
  _waterfallData->setMinZ(0);
  _waterfallData->setMaxZ(maxIntensity-minIntensity);

  _createCoordinateSystemFlag = true;

  emit UpdatedLowerIntensityLevel(minIntensity);
  emit UpdatedUpperIntensityLevel(maxIntensity);

  SetIntensityColorMapType(_intensityColorMapType,
			   _userDefinedLowIntensityColor,
			   _userDefinedLowIntensityColor, true);
}

void
Waterfall3DDisplayPlot::replot()
{
  if(!_initialized){
    Init();
  }
  if(initializedGL()){
    const timespec startTime = get_highres_clock();
    
    _waterfallData->create();
    
    legend()->setMajors(4);
    legend()->setMinors(5);
    double start, stop;
    coordinates()->axes[Qwt3D::Z1].limits(start,stop);
    legend()->setLimits( _waterfallData->GetFloorValue(), _waterfallData->GetFloorValue() + stop - start );

    coordinates()->axes[Qwt3D::X1].limits(start,stop);

    showColorLegend(true);

    updateGL();

    double differenceTime = (diff_timespec(get_highres_clock(), startTime));
  
    // Require at least a 20% duty cycle
    differenceTime *= 4.0;
    if(differenceTime > (1.0/5.0)){
      _displayIntervalTime = differenceTime;
    }
  }
}

void
Waterfall3DDisplayPlot::resizeSlot( QSize *s )
{
  resize(s->width(), s->height());
}

int
Waterfall3DDisplayPlot::GetIntensityColorMapType() const
{
  return _intensityColorMapType;
}

void
Waterfall3DDisplayPlot::SetIntensityColorMapType(const int newType,
						 const QColor lowColor,
						 const QColor highColor,
						 const bool forceFlag,
						 const bool noReplotFlag)
{
  if(((_intensityColorMapType != newType) || forceFlag) || 
     ((newType == INTENSITY_COLOR_MAP_TYPE_USER_DEFINED) &&
      (lowColor.isValid() && highColor.isValid()))){

    Waterfall3DColorMap* colorMap = new Waterfall3DColorMap();
    colorMap->SetInterval(_waterfallData->minZ(), _waterfallData->maxZ());

    switch(newType){
    case INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR:{
      _intensityColorMapType = newType;
      colorMap->setColorInterval(Qt::darkCyan, Qt::white);
      colorMap->addColorStop(0.25, Qt::cyan);
      colorMap->addColorStop(0.5, Qt::yellow);
      colorMap->addColorStop(0.75, Qt::red);
      setBackgroundColor(Qwt3D::Qt2GL(Qt::gray));
      break;
    }
    case INTENSITY_COLOR_MAP_TYPE_WHITE_HOT:{
      _intensityColorMapType = newType;
      colorMap->setColorInterval(Qt::black, Qt::white);
      setBackgroundColor(Qwt3D::Qt2GL(Qt::blue));
      break;
    }
    case INTENSITY_COLOR_MAP_TYPE_BLACK_HOT:{
      _intensityColorMapType = newType;
      colorMap->setColorInterval(Qt::white, Qt::black);
      setBackgroundColor(Qwt3D::Qt2GL(Qt::blue));
      break;
    }
    case INTENSITY_COLOR_MAP_TYPE_INCANDESCENT:{
      _intensityColorMapType = newType;
      colorMap->setColorInterval(Qt::black, Qt::white);
      colorMap->addColorStop(0.5, Qt::darkRed);
      setBackgroundColor(Qwt3D::Qt2GL(Qt::gray));
      break;
    }
    case INTENSITY_COLOR_MAP_TYPE_USER_DEFINED:{
      _userDefinedLowIntensityColor = lowColor;
      _userDefinedHighIntensityColor = highColor;
      _intensityColorMapType = newType;
      colorMap->setColorInterval(_userDefinedLowIntensityColor, _userDefinedHighIntensityColor);
      setBackgroundColor(Qwt3D::Qt2GL(Qt::white));
      break;
    }
    default:
      colorMap->setColorInterval(Qt::black, Qt::white);
      break;
    }

    // Qwt3D takes over destruction of this object...
    setDataColor(colorMap);

    if(!noReplotFlag){
      // Draw again
      replot();
      
      // Update the last replot timer
      _lastReplot = get_highres_clock();
    }
  }
}

const QColor
Waterfall3DDisplayPlot::GetUserDefinedLowIntensityColor() const
{
  return _userDefinedLowIntensityColor;
}

const QColor
Waterfall3DDisplayPlot::GetUserDefinedHighIntensityColor() const
{
  return _userDefinedHighIntensityColor;
}

#endif /* WATERFALL_3D_DISPLAY_PLOT_C */