Skip to content

Commit

Permalink
Merge pull request #252 from patrickelectric/waterfall_corr3
Browse files Browse the repository at this point in the history
Waterfall corrections
  • Loading branch information
patrickelectric authored Aug 7, 2018
2 parents 1fb3e98 + f1dcafb commit 47d6401
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 39 deletions.
2 changes: 1 addition & 1 deletion qml/DepthAxis.qml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Item {
// A triangle indicator to show the current depth reported by the ping
Canvas {
id: depthPointer
y: (parent.height / length_mm) * depth_mm - height / 2
y: (parent.height / length_mm) * (depth_mm - start_mm) - height / 2
width: 20
height: 12
anchors.right: parent.right
Expand Down
12 changes: 4 additions & 8 deletions qml/MainMenu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Item {
enabled: !autoGainChB.checked

Text {
text: "Start/Stop (mm):"
text: "Start/Length (mm):"
color: Style.textColor
}

Expand All @@ -156,11 +156,7 @@ Item {
Layout.columnSpan: 2
Layout.fillWidth: true
onEditingFinished: {
var length_mm = parseInt(totalLength.text)
var start_mm = Math.min(parseInt(text), length_mm - 500)
if(isNaN(start_mm)) {
start_mm = 0
}
var start_mm = parseInt(text)
text = start_mm
ping.start_mm = start_mm
}
Expand All @@ -182,9 +178,9 @@ Item {
Layout.fillWidth: true
onEditingFinished: {
var start_mm = parseInt(startLength.text)
var length_mm = Math.max(parseInt(text), start_mm + 500)
var length_mm = Math.min(parseInt(text), 50000 - start_mm)
if(isNaN(length_mm)) {
length_mm = 48903
length_mm = 500
}
text = length_mm
ping.length_mm = length_mm
Expand Down
2 changes: 1 addition & 1 deletion qml/MainPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Item {

onPointsUpdate: {
// Move from mm to m
ping1DVisualizer.draw(ping.points, (ping.length_mm - ping.start_mm) * 1e-3, ping.confidence, ping.start_mm*1e-3, ping.distance*1e-3)
ping1DVisualizer.draw(ping.points, ping.confidence, ping.start_mm*1e-3, ping.length_mm * 1e-3, ping.distance*1e-3)
}

onDistanceUpdate: {
Expand Down
6 changes: 3 additions & 3 deletions qml/Ping1DVisualizer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Item {
}
}

function draw(points, depth, confidence, initialPoint, distance) {
waterfall.draw(points, depth, confidence, initialPoint, distance)
chart.draw(points, depth, initialPoint)
function draw(points, confidence, initialPoint, length, distance) {
waterfall.draw(points, confidence, initialPoint, length, distance)
chart.draw(points, length + initialPoint, initialPoint)
}

function setDepth(depth) {
Expand Down
6 changes: 4 additions & 2 deletions src/sensor/ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ void Ping::handleMessage(PingMessage msg)

case Ping1DNamespace::Mode_auto: {
ping_msg_ping1D_mode_auto m(msg);
_mode_auto = m.mode_auto();
emit modeAutoUpdate();
if(_mode_auto != m.mode_auto()) {
_mode_auto = m.mode_auto();
emit modeAutoUpdate();
}
}
break;

Expand Down
84 changes: 63 additions & 21 deletions src/waterfall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void Waterfall::paint(QPainter *painter)
// Code for debug, draw the entire waterfall
//_painter->drawPixmap(_painter->viewport(), pix, QRect(0, 0, _image.width(), _image.height()));
_painter->drawPixmap(QRect(0, 0, width(), height()), pix,
QRect(first, _minDepthToDrawInPixels, displayWidth, _maxDepthToDrawInPixels - _minDepthToDrawInPixels));
QRect(first, _minDepthToDrawInPixels, displayWidth, _maxDepthToDrawInPixels));
}

void Waterfall::setImage(const QImage &image)
Expand All @@ -193,26 +193,73 @@ float Waterfall::RGBToValue(const QColor& color)
return _gradient.getValue(color);
}

void Waterfall::draw(const QList<double>& points, float depth, float confidence, float initPoint, float distance)
void Waterfall::draw(const QList<double>& points, float confidence, float initPoint, float length, float distance)
{
/*
initPoint: The lowest point of the last sample in meters
length: The length of the last sample in meters
_minPixelsPerMeter: waterfall max pixel height divided by max depth
_minPixelsPerMeter = _image.height()/_waterfallDepth;
lastMaxDC: Returns the last DC structure with max depth
lastMinDepth: Returns the minimum point in the chart
_minDepthToDraw: Minimum depth point, populated by lastMinDepth
_maxDepthToDraw: Maximum depth point, calculated from lastMaxDC
dynamicPixelsPerMeterScalar: Calculate the delta between number of pixels per meter
dynamicPixelsPerMeterScalar = 400/((_maxDepthToDraw - _minDepthToDraw)*_minPixelsPerMeter);
old (oldest sample) new (last sample)
| |
+-----------------------+ - _minDepthToDrawInPixels
| |-----| |
| |-----| |
| |-------------+ - virtualFloor
| |-------------+ - virtualHeight
| || |---| |
| || |-| |
| || |-| |
| || |-| |
| +| |-| |
| | ++ |
| | | |
| | | |
| | | |
| | | |
| | |
| | |
| | |
+-----------------------+ - _maxDepthToDrawInPixels = 400px
_minDepthToDrawInPixels: The lowest pixel that will appears for the user
_minDepthToDrawInPixels = (_minDepthToDraw*_minPixelsPerMeter*dynamicPixelsPerMeterScalar);
_maxDepthToDrawInPixels: Is defined to be 400 pixels
virtualFloor: It's the lowest pixel position to start drawing the last sample
virtualFloor = (initPoint*_minPixelsPerMeter*dynamicPixelsPerMeterScalar);
virtualFloor: It's the highest delta pixel position (from virtualFloor) to finish drawing the last sample
virtualHeight = ((length + initPoint - _minDepthToDraw)*_minPixelsPerMeter*dynamicPixelsPerMeterScalar);
*/

// Declare oldImage variable to do image spins
static QImage old = _image;
// Declare oldPoints variable to do some filter
static QList<double> oldPoints = points;

// This ring vector will store variables of the last n samples for user access
_DCRing.append({initPoint, depth, confidence, distance});
_DCRing.append({initPoint, length, confidence, distance});

/**
* @brief Get lastMaxDepth from the last n samples
*/
static auto lastMaxDepth = [this] {
static auto lastMaxDC = [this] {
float maxDepth = 0;
DCPack tempDC{0, 0, 0, 0};
for(const auto& DC : qAsConst(this->_DCRing))
{
maxDepth = maxDepth < DC.depth ? DC.depth : maxDepth;
if(maxDepth < DC.length + DC.initialDepth && DC.initialDepth != static_cast<const float>(_image.height())) {
maxDepth = DC.length + DC.initialDepth;
tempDC = DC;
}
}
return maxDepth;
return tempDC;
};

/**
Expand All @@ -226,9 +273,10 @@ void Waterfall::draw(const QList<double>& points, float depth, float confidence,
}
return minDepth;
};

static DCPack _maxDC;
_maxDC = lastMaxDC();
_minDepthToDraw = lastMinDepth();
_maxDepthToDraw = lastMaxDepth();
_maxDepthToDraw = _maxDC.initialDepth + _maxDC.length;
emit minDepthToDrawChanged();
emit maxDepthToDrawChanged();

Expand Down Expand Up @@ -258,17 +306,11 @@ void Waterfall::draw(const QList<double>& points, float depth, float confidence,

lastDynamicPixelsPerMeterScalar = dynamicPixelsPerMeterScalar;
} else {
_maxDepthToDrawInPixels = floor(_maxDepthToDraw*_minPixelsPerMeter);
}
_minDepthToDrawInPixels = floor(_minDepthToDraw*_minPixelsPerMeter);

int virtualFloor = floor(initPoint*_minPixelsPerMeter);
int virtualHeight = floor(depth*_minPixelsPerMeter*dynamicPixelsPerMeterScalar);

if(virtualHeight <= virtualFloor || _image.height() < virtualHeight || 0 > virtualFloor) {
qCWarning(waterfall) << "Invalid Height and Floor:" << virtualHeight << virtualFloor;
return;
_maxDepthToDrawInPixels = ((_maxDepthToDraw - _minDepthToDraw)*_minPixelsPerMeter);
}
_minDepthToDrawInPixels = (_minDepthToDraw*_minPixelsPerMeter*dynamicPixelsPerMeterScalar);
int virtualFloor = (initPoint*_minPixelsPerMeter*dynamicPixelsPerMeterScalar);
int virtualHeight = ((length + initPoint - _minDepthToDraw)*_minPixelsPerMeter*dynamicPixelsPerMeterScalar);

// Copy tail to head
// TODO can we get even better and allocate just once at initialization? ie circular buffering
Expand All @@ -289,7 +331,7 @@ void Waterfall::draw(const QList<double>& points, float depth, float confidence,
}

// Do up/downsampling
float factor = points.length()/((float)(virtualHeight - virtualFloor));
float factor = points.length()/((float)(virtualHeight - (virtualFloor - _minDepthToDrawInPixels)));

if(smooth()) {
#pragma omp for
Expand All @@ -298,12 +340,12 @@ void Waterfall::draw(const QList<double>& points, float depth, float confidence,
}

#pragma omp for
for(int i = 0; i < virtualHeight - virtualFloor; i++) {
for(int i = 0; i < virtualHeight; i++) {
_image.setPixelColor(currentDrawIndex, i + virtualFloor, valueToRGB(oldPoints[factor*i]));
}
} else {
#pragma omp for
for(int i = 0; i < virtualHeight - virtualFloor; i++) {
for(int i = 0; i < virtualHeight; i++) {
_image.setPixelColor(currentDrawIndex, i + virtualFloor, valueToRGB(points[factor*i]));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/waterfall.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Waterfall : public QQuickPaintedItem
*/
struct DCPack {
float initialDepth;
float depth;
float length;
float confidence;
float distance;
};
Expand Down Expand Up @@ -238,12 +238,12 @@ class Waterfall : public QQuickPaintedItem
* @brief Draw a list of points in the waterfall
*
* @param points
* @param depth
* @param confidence
* @param initPoint
* @param length
* @param distance
*/
Q_INVOKABLE void draw(const QList<double>& points, float depth = 50, float confidence = 0, float initPoint = 0, float distance = 0);
Q_INVOKABLE void draw(const QList<double>& points, float confidence = 0, float initPoint = 0, float length = 50, float distance = 0);

/**
* @brief Function that deals when the mouse is inside the waterfall
Expand Down

0 comments on commit 47d6401

Please sign in to comment.