×
Namespaces

Variants
Actions
(Difference between revisions)

QML horizon component for camera apps

From Nokia Developer Wiki
Jump to: navigation, search
lildeimos (Talk | contribs)
m (Lildeimos -)
lildeimos (Talk | contribs)
(Lildeimos - - Implementation)
Line 201: Line 201:
 
#define ROT_SAMPLES 5
 
#define ROT_SAMPLES 5
 
</code>
 
</code>
this defines the number of rotation readings to be averaged. As mentioned before, this is useful to avoid shaking of the horizon line since the readings values are very wobble.
+
this defines the number of rotation readings to be averaged. As mentioned before, this is useful to avoid shaking of the horizon line since the readings values are very wobble. I found that 5 samples are enough, but it's possible to increase it to 10 or 20 and see which best value fit a specific device. Less this value is, less computation occurs since the everage is calcutated every time a new reading occurs.
  
 
I opted to use a circular list to store the last {{Icode|ROT_SAMPLES}}. This list is composed by this struct and declarations:
 
I opted to use a circular list to store the last {{Icode|ROT_SAMPLES}}. This list is composed by this struct and declarations:
Line 303: Line 303:
  
 
=== Horizon drawing ===
 
=== Horizon drawing ===
 +
 +
To draw the horizon line [http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#paint QGraphicsItem::paint()] is reimplemented and {{Icode|update()}} is called only when the circular list is filled:
 +
<code>
 +
void Horizon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
 +
{
 +
    Q_UNUSED(option)
 +
    Q_UNUSED(widget)
 +
    qreal rotX, rotY, rotZ;
 +
 +
    // If the device is horizontal +- 45° then exit
 +
    if ( qAbs(m_average_rotZ) > (m_calibrateMaxZ-m_calibrateMinZ)/2 ) return;
 +
</code>
 +
When the device has an inclination which point below the real horizon, don't draw the line and exit.
 +
 +
<code>
 +
    painter->setPen(pen);
 +
 +
    if(smooth() == true)
 +
        painter->setRenderHint(QPainter::Antialiasing, true);
 +
 +
    // Normalize rotation values to PI/2
 +
    rotX = (m_average_rotX-m_calibrateMinX)*M_PI_2/(m_calibrateMaxX-m_calibrateMinX);
 +
    rotY = (m_average_rotY-m_calibrateMinY)*M_PI_2/(m_calibrateMaxY-m_calibrateMinY);
 +
    rotZ = (m_average_rotZ-m_calibrateMinZ)*M_PI_2/(m_calibrateMaxZ-m_calibrateMinZ);
 +
</code>
 +
The acceleration readings, have a range of +/- 12~9.8, rotX, rotY and rotZ have the task to trasform these values in radians regardles the calibration values.
 +
 +
<code>
 +
    qreal teta;
 +
    teta = -atan(rotY/rotX);
 +
 +
    // Calculate different average position for portrait and landscape
 +
    qreal w,h;
 +
    if ( qAbs(m_average_rotY) < (m_calibrateMaxY-m_calibrateMinY)/2 ) {
 +
        w = boundingRect().width();
 +
        h = boundingRect().height();
 +
        if ( m_average_rotX<0 ) h *= -1;
 +
    }
 +
    else {
 +
        w = boundingRect().height();
 +
        h = boundingRect().width();
 +
        if ( m_average_rotY<0 ) { w *= -1; h *= -1; }
 +
    }
 +
    // Calculate 2 point of the horizon slope
 +
    // At 45° deg the horizon should be at top of the screen (rotZ*2)
 +
    m_x1 = boundingRect().width()/2  + cos( (rotZ*2+M_PI_2) )*w/2;
 +
    m_y1 = boundingRect().height()/2 + cos( (rotZ*2+M_PI_2) )*h/2;
 +
 +
    m_x2 = m_x1 + cos(teta);
 +
    m_y2 = m_y1 + sin(teta);
 +
</code>
 +
This code calculate the 2 points coordinate of the slope taking care of all the four device orientations.
 +
 +
<code>
 +
    // With the calculated slope ( (y2-y1)/(x2-x1) ) get the intesction with the (2 edges) bounding rectangle
 +
    qreal x,y;
 +
    qreal x1,y1,x2,y2;
 +
    qreal *px, *py;
 +
 +
    px=&x1;
 +
    py=&y1;
 +
 +
    // Find intersection with upper edge. When a pair is found px and py will point to the x2 and y2 values to set them
 +
    if ( findIntersection( m_x1,m_y1, m_x2,m_y2,  0,0, 1,0,  x,y ) &&
 +
        ( x < boundingRect().width() && x>0 ) ) {
 +
        *px=x;
 +
        *py=0;
 +
        px=&x2;
 +
        py=&y2;
 +
    }
 +
 +
    // Find intersection with bottom edge
 +
    if ( findIntersection( m_x1,m_y1, m_x2,m_y2,  0,boundingRect().height(), 1,boundingRect().height(),  x,y ) &&
 +
        ( x < boundingRect().width() && x>0 ) ) {
 +
        *px=x;
 +
        *py=boundingRect().height();
 +
        px=&x2;
 +
        py=&y2;
 +
    }
 +
 +
    // Find intersection with left edge (add a little bit to x2 to avoid nan)
 +
    if ( findIntersection( m_x1,m_y1, m_x2,m_y2,  0,0, 0.00001,10,  x,y ) &&
 +
        ( y < boundingRect().height() && y>0 ) ) {
 +
        *px=0;
 +
        *py=y;
 +
        px=&x2;
 +
        py=&y2;
 +
    }
 +
 +
    // Find intersection with right edge (add a little bit to x2 to avoid nan)
 +
    if ( findIntersection( m_x1,m_y1, m_x2,m_y2,  boundingRect().width(),0, boundingRect().width()+0.00001,10,  x,y ) &&
 +
        ( y < boundingRect().height() && y>0) ) {
 +
        *px=boundingRect().width();
 +
        *py=y;
 +
    }
 +
</code>
 +
Here are calculated the intersection points of the slope to the bounding rectangle of this item.
 +
{{Icode|findIntersection()}} function will return true if there is an intersection and in {{Icode|x}} and {{Icode|y}} are stored these values. Inside the if statement it is also checked if this point is included into the Item edge.
 +
If so, store the point coordinate. Since a rectangle can be intersected at most 2 times by a line, when an intersection is found, *px and *py pointers are switched to the 2nd point variables being sure that will be valuated in the next if statement.
 +
 +
<code>
 +
    painter->drawLine(x1, y1,  x2, y2);
 +
}
 +
</code>
 +
... and finally draw the horizon !

Revision as of 17:06, 8 May 2012

This article explains how to use QML horizon line component

Article Metadata
Tested with
SDK: Nokia Qt SDK 1.2.0
Devices(s): Nokia C7-00, Nokia N950
Compatibility
Platform(s): Symbian^3 and later, Harmattan
Symbian
Device(s): All* (must have internal Accelerometer sensor)
Article
Keywords: Horizon, QDeclarativeItem, QAccelerometer
Created: lildeimos (08 May 2012)
Last edited: lildeimos (08 May 2012)

Note.pngNote: This is an entry in the PureView Imaging Competition 2012Q2

Contents

Introduction

This QML component draws a overlay horizon line on top of the parent Item.

(sorry for bad quality... the screen shoot I used doesn't work with camera :) )

Overview

While developing this component, I chosen to use QAccelerometer sensor instead QRotationSensor. The reson is that some devices doesn't support Z-axis and for example my Nokia C7 has a snap of 15° deg of values. Should not be a big deal to replace the current accelerometer with the rotation sensor. Another problem I have encountered, is that the sensors are not calibrated all the same. To resolve this problem, I added 6 calibration properties (min and max values for each axis). The calibration properties in the example, are taken asking to the user to put the device in 3 different orientations. The drawback is that values are lost the next time the application run. A solution could be store those with QSettings as done in the OMCcam project.

Usage

Start copying horizon.cpp and horizon.h into your project source directory. In the main.cpp file include the .h:

#include "horizon.h"

then register the component:

qmlRegisterType<Horizon>("Horizon", 1, 0, "Horizon");

In you qml file you can now declare Horizon component:

import QtQuick 1.1
import Horizon 1.0
 
Rectangle {
id: mainRect
Horizon {
id: horizon
anchors.fill: parent
clip: true
active: true
penWidth: 3
color: "black"
} // Horizon
}

active property will activate the accelerometer sensor to display a line on top of mainRect with a black color and a penWidth of 3 pixels. Horizon component has also {{Icode|calibrate[Min|Max][X|Y|Z]} (six properties) that are needed to calculate the position of the line. By default, min values are set to zero, max values are set to 9.8. Every devices need a sensor calibration, to do this, the user help is needed. The following code ask to the user to put the device in vertical portrait mode, then in landscape and finally horizontal with display facing upward:

Dialog {
id: calibratePortrait
title: Text {
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 28
color: "white"
text: "Rotation sensor calibration"
}
 
content: Item {
height: 50
width: parent.width
Text {
font.pixelSize: 22
anchors.centerIn: parent
color: "white"
text: "Put your phone in portrait and press Ok"
}
}
 
buttons: ButtonRow {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 30
Button {
width: 100
text: "OK"
onClicked: {
horizonMenu.open=false;
horizon.calibrateMinX=horizon.averageRotX;
horizon.calibrateMaxY=horizon.averageRotY;
horizon.calibrateMinZ=horizon.averageRotZ;
calibratePortrait.accept();
calibrateLandscape.open();
}
}
Button {width: 100; text: "Cancel"; onClicked: calibratePortrait.reject() }
}
} // calibratePortrait
 
 
Dialog {
id: calibrateLandscape
visualParent: mainPage
title: Text {
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 28
color: "white"
text: "Rotation sensor calibration"
}
 
content: Item {
height: 50
width: parent.width
Text {
font.pixelSize: 22
anchors.centerIn: parent
color: "white"
text: "Put your phone in landscape and press Ok"
}
}
 
buttons: ButtonRow {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 30
Button {
width: 100
text: "OK"
onClicked: {
horizon.calibrateMaxX=horizon.averageRotX;
horizon.calibrateMinY=horizon.averageRotY;
calibrateLandscape.accept();
calibratePlane.open();
}
}
Button {width: 100; text: "Cancel"; onClicked: calibrateLandscape.reject()}
}
} // calibrateLandscape
 
Dialog {
id: calibratePlane
visualParent: mainPage
title: Text {
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 28
color: "white"
text: "Rotation sensor calibration"
}
 
content: Item {
height: 50
width: parent.width
Text {
font.pixelSize: 22
anchors.centerIn: parent
color: "white"
wrapMode: Text.WrapAnywhere
text: "Put your phone on a horizontal plane (face up) and press Ok"
}
}
 
buttons: ButtonRow {
spacing: 30
anchors.horizontalCenter: parent.horizontalCenter
Button {
width: 100
text: "OK"
onClicked: {
horizon.calibrateMaxZ=horizon.averageRotZ;
calibratePlane.accept();
}
}
Button {width: 100;text: "Cancel"; onClicked: calibratePlane.reject()}
}
} // calibrateLandscape

You can bind the open event of the first dialog ( calibratePortrait ) to a button, when accepted the second dialog will rise ans so for the third.

As you can see, here are appeared another 3 properties: averageRotX averageRotY averageRotZ. To avoid flickering of the horizon line, the component makes an average calculation of a defined number of samples of the accelerometer sensor. In these properties is stored the actual average of the last n sensor readings.

Implementation

I will explain here how I thought to implement Horizon. I remind you to the download section for the source code.

Readings management

In the header file is defined

#define ROT_SAMPLES 5

this defines the number of rotation readings to be averaged. As mentioned before, this is useful to avoid shaking of the horizon line since the readings values are very wobble. I found that 5 samples are enough, but it's possible to increase it to 10 or 20 and see which best value fit a specific device. Less this value is, less computation occurs since the everage is calcutated every time a new reading occurs.

I opted to use a circular list to store the last ROT_SAMPLES. This list is composed by this struct and declarations:

typedef struct listRot_ {
qreal value;
struct listRot_ *curr,*prev,*next;
} listRot;
 
listRot *rotX_samples[ROT_SAMPLES];
listRot *rotY_samples[ROT_SAMPLES];
listRot *rotZ_samples[ROT_SAMPLES];
listRot *currRotX;
listRot *currRotY;
listRot *currRotZ;

These 3 arrays are initialized in constructor:

m_rotation = new QAccelerometer();
 
for (int i = 0; i < ROT_SAMPLES; i++) {
rotX_samples[i] = new listRot;
rotY_samples[i] = new listRot;
rotZ_samples[i] = new listRot;
}
for (int i = 0; i < ROT_SAMPLES; i++) {
rotX_samples[i]->prev = (i>0 ? rotX_samples[i-1] : rotX_samples[ROT_SAMPLES]);
rotX_samples[i]->next = (i<ROT_SAMPLES-1 ? rotX_samples[i+1] : rotX_samples[0]);
rotX_samples[i]->value = i;
 
rotY_samples[i]->prev = (i>0 ? rotY_samples[i-1] : rotY_samples[ROT_SAMPLES]);
rotY_samples[i]->next = (i<ROT_SAMPLES-1 ? rotY_samples[i+1] : rotY_samples[0]);
rotY_samples[i]->value = i;
 
rotZ_samples[i]->prev = (i>0 ? rotZ_samples[i-1] : rotZ_samples[ROT_SAMPLES]);
rotZ_samples[i]->next = (i<ROT_SAMPLES-1 ? rotZ_samples[i+1] : rotZ_samples[0]);
rotZ_samples[i]->value = i;
}
currRotX = rotX_samples[0];
currRotY = rotY_samples[0];
currRotZ = rotZ_samples[0];
 
connect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));

the slot is connected to onReadingChanged(). This function is connected only for the time needed to fill the list. Then the connection is 'redirected' to onReadingChanged2() where new readings will be stored sequentially (circulary?) in the list, averages are calculated and the reimplemented QGraphicsItem::paint() function called:

// Slot called when the circular list isn't filled yet. When it is filled, change the connected slot to onReadingChanged2()
void Horizon::onReadingChanged()
{
// fill the circular list of samples
if (!m_reading) return;
if (!m_rotation->isActive() || m_rotation->isBusy()) return;
static int count = 0;
 
rotX_samples[count]->value = m_reading->x();
rotY_samples[count]->value = m_reading->y();
rotZ_samples[count]->value = m_reading->z();
 
// first ROT_SAMPLES samples are achieved, so onReadingChanged2() will be used next time
if (count>ROT_SAMPLES) {
disconnect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
connect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged2()));
}
 
count++;
}
 
// Slot called when the circular list is filled
void Horizon::onReadingChanged2()
{
// calculate an average of the last ROT_SAMPLES samples. This prevent horizon line to shake
if (!m_reading) return;
if (!m_rotation->isActive() || m_rotation->isBusy()) return;
 
currRotX->value = m_reading->x();
currRotY->value = m_reading->y();
currRotZ->value = m_reading->z();
 
for (int i=0; i<ROT_SAMPLES; i++, currRotX=currRotX->next, currRotY=currRotY->next, currRotZ=currRotZ->next) {
m_average_rotX += currRotX->value;
m_average_rotY += currRotY->value;
m_average_rotZ += currRotZ->value;
}
 
m_average_rotX /= ROT_SAMPLES;
m_average_rotY /= ROT_SAMPLES;
m_average_rotZ /= ROT_SAMPLES;
 
currRotX=currRotX->next->next;
currRotY=currRotY->next->next;
currRotZ=currRotZ->next->next;
 
update();
 
emit averageRotXChanged();
emit averageRotYChanged();
emit averageRotZChanged();
}

Horizon drawing

To draw the horizon line QGraphicsItem::paint() is reimplemented and update() is called only when the circular list is filled:

void Horizon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
qreal rotX, rotY, rotZ;
 
// If the device is horizontal +- 45° then exit
if ( qAbs(m_average_rotZ) > (m_calibrateMaxZ-m_calibrateMinZ)/2 ) return;

When the device has an inclination which point below the real horizon, don't draw the line and exit.

    painter->setPen(pen);
 
if(smooth() == true)
painter->setRenderHint(QPainter::Antialiasing, true);
 
// Normalize rotation values to PI/2
rotX = (m_average_rotX-m_calibrateMinX)*M_PI_2/(m_calibrateMaxX-m_calibrateMinX);
rotY = (m_average_rotY-m_calibrateMinY)*M_PI_2/(m_calibrateMaxY-m_calibrateMinY);
rotZ = (m_average_rotZ-m_calibrateMinZ)*M_PI_2/(m_calibrateMaxZ-m_calibrateMinZ);

The acceleration readings, have a range of +/- 12~9.8, rotX, rotY and rotZ have the task to trasform these values in radians regardles the calibration values.

    qreal teta;
teta = -atan(rotY/rotX);
 
// Calculate different average position for portrait and landscape
qreal w,h;
if ( qAbs(m_average_rotY) < (m_calibrateMaxY-m_calibrateMinY)/2 ) {
w = boundingRect().width();
h = boundingRect().height();
if ( m_average_rotX<0 ) h *= -1;
}
else {
w = boundingRect().height();
h = boundingRect().width();
if ( m_average_rotY<0 ) { w *= -1; h *= -1; }
}
// Calculate 2 point of the horizon slope
// At 45° deg the horizon should be at top of the screen (rotZ*2)
m_x1 = boundingRect().width()/2 + cos( (rotZ*2+M_PI_2) )*w/2;
m_y1 = boundingRect().height()/2 + cos( (rotZ*2+M_PI_2) )*h/2;
 
m_x2 = m_x1 + cos(teta);
m_y2 = m_y1 + sin(teta);

This code calculate the 2 points coordinate of the slope taking care of all the four device orientations.

    // With the calculated slope ( (y2-y1)/(x2-x1) ) get the intesction with the (2 edges) bounding rectangle
qreal x,y;
qreal x1,y1,x2,y2;
qreal *px, *py;
 
px=&x1;
py=&y1;
 
// Find intersection with upper edge. When a pair is found px and py will point to the x2 and y2 values to set them
if ( findIntersection( m_x1,m_y1, m_x2,m_y2, 0,0, 1,0, x,y ) &&
( x < boundingRect().width() && x>0 ) ) {
*px=x;
*py=0;
px=&x2;
py=&y2;
}
 
// Find intersection with bottom edge
if ( findIntersection( m_x1,m_y1, m_x2,m_y2, 0,boundingRect().height(), 1,boundingRect().height(), x,y ) &&
( x < boundingRect().width() && x>0 ) ) {
*px=x;
*py=boundingRect().height();
px=&x2;
py=&y2;
}
 
// Find intersection with left edge (add a little bit to x2 to avoid nan)
if ( findIntersection( m_x1,m_y1, m_x2,m_y2, 0,0, 0.00001,10, x,y ) &&
( y < boundingRect().height() && y>0 ) ) {
*px=0;
*py=y;
px=&x2;
py=&y2;
}
 
// Find intersection with right edge (add a little bit to x2 to avoid nan)
if ( findIntersection( m_x1,m_y1, m_x2,m_y2, boundingRect().width(),0, boundingRect().width()+0.00001,10, x,y ) &&
( y < boundingRect().height() && y>0) ) {
*px=boundingRect().width();
*py=y;
}

Here are calculated the intersection points of the slope to the bounding rectangle of this item. findIntersection() function will return true if there is an intersection and in x and y are stored these values. Inside the if statement it is also checked if this point is included into the Item edge. If so, store the point coordinate. Since a rectangle can be intersected at most 2 times by a line, when an intersection is found, *px and *py pointers are switched to the 2nd point variables being sure that will be valuated in the next if statement.

    painter->drawLine(x1, y1,   x2, y2);
}

... and finally draw the horizon !

111 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×