-
Notifications
You must be signed in to change notification settings - Fork 122
/
QwtRasterDataMD.cpp
341 lines (304 loc) · 10.4 KB
/
QwtRasterDataMD.cpp
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
#include "MantidQtAPI/QwtRasterDataMD.h"
#include <math.h>
#include "MantidGeometry/MDGeometry/MDTypes.h"
#include "MantidGeometry/MDGeometry/IMDDimension.h"
#include "MantidAPI/IMDWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
namespace MantidQt
{
namespace API
{
using namespace Mantid;
using namespace Mantid::API;
using Mantid::Geometry::IMDDimension_const_sptr;
//-------------------------------------------------------------------------
/// Constructor
QwtRasterDataMD::QwtRasterDataMD()
: m_ws(), m_overlayWS(),
m_slicePoint(NULL), m_fast(true), m_zerosAsNan(true),
m_normalization(Mantid::API::VolumeNormalization)
{
m_range = QwtDoubleInterval(0.0, 1.0);
m_nd = 0;
m_dimX = 0;
m_dimY = 0;
nan = std::numeric_limits<double>::quiet_NaN();
}
//-------------------------------------------------------------------------
/// Destructor
QwtRasterDataMD::~QwtRasterDataMD()
{
delete [] m_slicePoint;
}
//-------------------------------------------------------------------------
/** Perform a copy of this data object */
QwtRasterDataMD *QwtRasterDataMD::copy() const
{
QwtRasterDataMD* out = new QwtRasterDataMD();
//base bounding box
out->setBoundingRect(this->boundingRect());
out->m_ws = this->m_ws;
out->m_dimX = this->m_dimX;
out->m_dimY = this->m_dimY;
out->m_nd = this->m_nd;
out->m_range = this->m_range;
out->m_slicePoint = new coord_t[m_nd];
for (size_t d=0; d<m_nd; d++)
out->m_slicePoint[d] = this->m_slicePoint[d];
out->m_ws = this->m_ws;
out->m_fast = this->m_fast;
out->m_zerosAsNan = this->m_zerosAsNan;
out->m_normalization = this->m_normalization;
out->m_overlayWS = this->m_overlayWS;
out->m_overlayXMin = this->m_overlayXMin;
out->m_overlayXMax = this->m_overlayXMax;
out->m_overlayYMin = this->m_overlayYMin;
out->m_overlayYMax = this->m_overlayYMax;
out->m_overlayInSlice = this->m_overlayInSlice;
return out;
}
//-------------------------------------------------------------------------
/** Set the data range (min/max) to display */
void QwtRasterDataMD::setRange(const QwtDoubleInterval & range)
{ m_range = range; }
//-------------------------------------------------------------------------
/** Return the data value to plot at the given position
*
* @param x :: position in coordinates of the MDWorkspace
* @param y :: position in coordinates of the MDWorkspace
* @return signal to plot
*/
double QwtRasterDataMD::value(double x, double y) const
{
if (!m_ws) return 0;
// Generate the vector of coordinates, filling in X and Y
coord_t * lookPoint = new coord_t[m_nd];
for (size_t d=0; d<m_nd; d++)
{
if (d==m_dimX)
lookPoint[d] = static_cast<coord_t>(x);
else if (d==m_dimY)
lookPoint[d] = static_cast<coord_t>(y);
else
lookPoint[d] = m_slicePoint[d];
}
// Get the signal at that point
signal_t value = 0;
// Check if the overlay WS is within range of being viewed
if (m_overlayWS && m_overlayInSlice
&& (x >= m_overlayXMin) && (x < m_overlayXMax)
&& (y >= m_overlayYMin) && (y < m_overlayYMax))
{
// Point is in the overlaid workspace
value = m_overlayWS->getSignalAtCoord(lookPoint, m_normalization);
}
else
{
// No overlay, or not within range of that workspace
value = m_ws->getSignalAtCoord(lookPoint, m_normalization);
}
delete [] lookPoint;
// Special case for 0 = show as NAN
if (m_zerosAsNan && value == 0.)
return nan;
return value;
}
//------------------------------------------------------------------------------------------------------
/** Return the data range to show */
QwtDoubleInterval QwtRasterDataMD::range() const
{
// Linear color plot
return m_range;
}
//------------------------------------------------------------------------------------------------------
/** Set to use "fast" rendering mode
* @param fast :: if true, will guess at the number of pixels to render based
* on workspace resolution
*/
void QwtRasterDataMD::setFastMode(bool fast)
{
this->m_fast = fast;
}
//------------------------------------------------------------------------------------------------------
/** Set to convert Zeros to NAN to make them transparent when displaying
*
* @param val :: true to make 0 = nan
*/
void QwtRasterDataMD::setZerosAsNan(bool val)
{
this->m_zerosAsNan = val;
}
//------------------------------------------------------------------------------------------------------
/** Set how the signal is normalized
*
* @param normalization :: option from MDNormalization enum.
*/
void QwtRasterDataMD::setNormalization(Mantid::API::MDNormalization normalization)
{
m_normalization = normalization;
}
/** @return how the signal is normalized */
Mantid::API::MDNormalization QwtRasterDataMD::getNormalization() const
{
return m_normalization;
}
//------------------------------------------------------------------------------------------------------
/** Return how many pixels this area should be rendered as
*
* @param area :: area under view
* @return # of pixels in each direction
*/
QSize QwtRasterDataMD::rasterHint(const QwtDoubleRect &area) const
{
if (!m_ws || !m_X || !m_Y) return QSize();
// Slow mode? Don't give a raster hint. This will be 1 pixel per point
if (!m_fast) return QSize();
// Fast mode: use the bin size to guess at the pixel density
coord_t binX = m_X->getBinWidth();
coord_t binY = m_Y->getBinWidth();
// Use the overlay workspace, if any, and if its bins are smaller
if (m_overlayWS && m_overlayInSlice)
{
coord_t temp;
temp = m_overlayWS->getDimension(m_dimX)->getBinWidth();
if (temp < binX) binX = temp;
temp = m_overlayWS->getDimension(m_dimY)->getBinWidth();
if (temp < binY) binY = temp;
}
int w = 3 * int(area.width() / binX);
int h = 3 * int(area.height() / binY);
if (w<10) w = 10;
if (h<10) h = 10;
return QSize(w,h);
}
//------------------------------------------------------------------------------------------------------
/** Sets the workspace being displayed
*
* @param ws :: IMDWorkspace to show
*/
void QwtRasterDataMD::setWorkspace(IMDWorkspace_const_sptr ws)
{
if (!ws)
throw std::runtime_error("QwtRasterDataMD::setWorkspace(): NULL workspace passed.");
m_ws = ws;
m_nd = m_ws->getNumDims();
m_dimX = 0;
m_dimY = 1;
delete [] m_slicePoint;
m_slicePoint = new coord_t[m_nd];
}
//------------------------------------------------------------------------------------------------------
/** Sets the workspace that will be displayed ON TOP of the original workspace.
* For dynamic rebinning.
*
* @param ws :: IMDWorkspace to show
*/
void QwtRasterDataMD::setOverlayWorkspace(Mantid::API::IMDWorkspace_const_sptr ws)
{
if (!ws)
{
m_overlayWS.reset();
return;
}
if (ws->getNumDims() != m_nd)
throw std::runtime_error("QwtRasterDataMD::setOverlayWorkspace(): workspace does not have the same number of dimensions!");
m_overlayWS = ws;
}
//------------------------------------------------------------------------------------------------------
/** Set the slicing parameters
*
* @param dimX :: index of the X dimension
* @param dimY :: index of the Y dimension
* @param X : X Dimension
* @param Y : Y Dimension
* @param slicePoint :: vector of slice points
*/
void QwtRasterDataMD::setSliceParams(size_t dimX, size_t dimY,
Mantid::Geometry::IMDDimension_const_sptr X, Mantid::Geometry::IMDDimension_const_sptr Y,
std::vector<Mantid::coord_t> & slicePoint)
{
if (slicePoint.size() != m_nd)
throw std::runtime_error("QwtRasterDataMD::setSliceParams(): inconsistent vector/number of dimensions size.");
m_dimX = dimX;
m_dimY = dimY;
m_X = X;
m_Y = Y;
if (!m_X || !m_Y)
throw std::runtime_error("QwtRasterDataMD::setSliceParams(): one of the input dimensions is NULL");
delete [] m_slicePoint;
m_slicePoint = new coord_t[slicePoint.size()];
m_overlayInSlice = true;
for (size_t d=0; d<m_nd; d++)
{
m_slicePoint[d] = slicePoint[d];
// Don't show the overlay WS if it is outside of range in the slice points
if (m_overlayWS && d != m_dimX && d != m_dimY)
{
if (slicePoint[d] < m_overlayWS->getDimension(d)->getMinimum()
|| slicePoint[d] >= m_overlayWS->getDimension(d)->getMaximum())
m_overlayInSlice = false;
}
}
// Cache the edges of the overlaid workspace
if (m_overlayWS)
{
m_overlayXMin = m_overlayWS->getDimension(m_dimX)->getMinimum();
m_overlayXMax = m_overlayWS->getDimension(m_dimX)->getMaximum();
m_overlayYMin = m_overlayWS->getDimension(m_dimY)->getMinimum();
m_overlayYMax = m_overlayWS->getDimension(m_dimY)->getMaximum();
//std::cout << m_overlayXMin << "," << m_overlayXMax << "," << m_overlayYMin << "," << m_overlayYMax << std::endl;
}
}
//===================================================================================
// NoOverlayRaster2D
//===================================================================================
/**
* Create a new object with the same state as this
* @return A pointer to a new object
*/
NoOverlayRaster2D *NoOverlayRaster2D::copy() const
{
auto* out = new NoOverlayRaster2D();
//base bounding box
out->setBoundingRect(this->boundingRect());
out->m_ws = this->m_ws;
out->m_dimX = this->m_dimX;
out->m_dimY = this->m_dimY;
out->m_nd = this->m_nd;
out->m_range = this->m_range;
out->m_slicePoint = new coord_t[m_nd];
for (size_t d=0; d<m_nd; d++)
out->m_slicePoint[d] = this->m_slicePoint[d];
out->m_ws = this->m_ws;
out->m_fast = this->m_fast;
out->m_zerosAsNan = this->m_zerosAsNan;
out->m_normalization = this->m_normalization;
out->m_overlayWS = this->m_overlayWS;
out->m_overlayXMin = this->m_overlayXMin;
out->m_overlayXMax = this->m_overlayXMax;
out->m_overlayYMin = this->m_overlayYMin;
out->m_overlayYMax = this->m_overlayYMax;
out->m_overlayInSlice = this->m_overlayInSlice;
return out;
}
/**
* Return the data value to plot at the given position specialized
* for a matrix workspace. There is no consideration for a
* slicing point. It simply plots the signal from the given
* coordinates
* @param x :: position in coordinates of the MDWorkspace
* @param y :: position in coordinates of the MDWorkspace
* @return signal to plot
*/
double NoOverlayRaster2D::value(double x, double y) const
{
coord_t lookPoint[2] = {static_cast<coord_t>(x), static_cast<coord_t>(y)};
signal_t value = m_ws->getSignalAtCoord(lookPoint, m_normalization);
if(value != value) // out of range
{
value = m_range.maxValue()*1.1;
}
return value;
}
} //namespace
} //namespace