Fixed seg fault

This commit is contained in:
Andrey Tkachenko 2014-07-28 20:25:39 +04:00
parent 9316eff244
commit b6b4bf3eb6
15 changed files with 103 additions and 76 deletions

View File

@ -24,7 +24,8 @@ SOURCES += main.cpp \
svobject.cpp \ svobject.cpp \
svpointcloud.cpp \ svpointcloud.cpp \
svkernel.cpp \ svkernel.cpp \
svpointcloudviewer.cpp svpointcloudviewer.cpp \
svapplicationcontext.cpp
HEADERS += \ HEADERS += \
svimage.h \ svimage.h \
@ -39,7 +40,8 @@ HEADERS += \
svpointcloud.h \ svpointcloud.h \
svdefs.h \ svdefs.h \
svkernel.h \ svkernel.h \
svpointcloudviewer.h svpointcloudviewer.h \
svapplicationcontext.h
RESOURCES += \ RESOURCES += \
resource.qrc resource.qrc

View File

@ -152,7 +152,7 @@ ApplicationWindow {
Item { Item {
anchors.fill: parent anchors.fill: parent
SvPointCloudViewer { SvPointCloudViewer {
//pointCloud: pointCloud pointCloud: app.getPointCloud()
anchors.fill: parent anchors.fill: parent
} }
} }

View File

@ -2,18 +2,20 @@
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlComponent> #include <QQmlComponent>
#include <QQmlContext> #include <QQmlContext>
#include <QPixmap>
#include "svimage.h" #include "svimage.h"
#include "svprocessor.h" #include "svprocessor.h"
#include "svimageprovider.h" #include "svimageprovider.h"
#include "svpointcloudviewer.h" #include "svpointcloudviewer.h"
#include "svapplicationcontext.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QGuiApplication a(argc, argv); QGuiApplication a(argc, argv);
qmlRegisterType<SvPointCloudViewer>("SvPCV", 1, 0, "SvPointCloudViewer"); qmlRegisterType<SvPointCloudViewer>("SvPCV", 1, 0, "SvPointCloudViewer");
qmlRegisterType<SvApplicationContext>("SvPCV", 1, 0, "SvApplicationContext");
qRegisterMetaType<SvPointCloud*>();
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
SvImageProvider imageProvider; SvImageProvider imageProvider;
@ -25,6 +27,9 @@ int main(int argc, char *argv[])
SvImage right(imgRight); SvImage right(imgRight);
SvPointCloud pointCloud(imgLeft.width(), imgLeft.height()); SvPointCloud pointCloud(imgLeft.width(), imgLeft.height());
SvApplicationContext m;
m.setPointCloud(&pointCloud);
SvProcessor proc(4); SvProcessor proc(4);
@ -35,7 +40,7 @@ int main(int argc, char *argv[])
engine.addImageProvider("images", &imageProvider); engine.addImageProvider("images", &imageProvider);
engine.rootContext()->setContextProperty("processor", &proc); engine.rootContext()->setContextProperty("processor", &proc);
engine.rootContext()->setContextProperty("pointCloud", &pointCloud); engine.rootContext()->setContextProperty("app", &m);
engine.load(QUrl(QStringLiteral("qrc:///Main.qml"))); engine.load(QUrl(QStringLiteral("qrc:///Main.qml")));
proc.start(); proc.start();

6
svapplicationcontext.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "svapplicationcontext.h"
SvApplicationContext::SvApplicationContext(QObject *parent) :
QObject(parent)
{
}

22
svapplicationcontext.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef SVPOINTCLOUDVIEWMODEL_H
#define SVPOINTCLOUDVIEWMODEL_H
#include <QObject>
#include "svpointcloud.h"
class SvApplicationContext : public QObject
{
Q_OBJECT
public:
explicit SvApplicationContext(QObject *parent = 0);
void setPointCloud(SvPointCloud *pointCloud) { m_pointCloud = pointCloud; }
Q_INVOKABLE SvPointCloud* getPointCloud() { return m_pointCloud; }
private:
SvPointCloud *m_pointCloud;
};
#endif // SVPOINTCLOUDVIEWMODEL_H

View File

@ -2,6 +2,7 @@
#define SVDEFS_H #define SVDEFS_H
#include <QDebug> #include <QDebug>
#include <QRgb>
typedef unsigned int SvPointId; typedef unsigned int SvPointId;
typedef unsigned int uint; typedef unsigned int uint;

View File

@ -89,10 +89,13 @@ void SvKernel::exec(SvPointCloud *pc, SvImage *image, int line)
} }
if (value > 0) { if (value > 0) {
p.setX(x - 1); if (x > 0 && line > 0){
p.setY(line - 1); p.setX(x - 1);
p.setY(line - 1);
p.setColor(qRgb(value, value, value));
pc->addPoint(p); pc->addPoint(p);
}
} }
__dX = _dX; __dX = _dX;

View File

@ -7,7 +7,8 @@ SvPoint::SvPoint()
SvPoint::SvPoint(SvPoint &point) SvPoint::SvPoint(SvPoint &point)
{ {
m_px = point.x();
m_py = point.y();
} }
void SvPoint::addCurve(SvCurve *curve) void SvPoint::addCurve(SvCurve *curve)

View File

@ -29,6 +29,7 @@ protected:
uint m_curveCount; uint m_curveCount;
SvCurve *m_curves[8]; SvCurve *m_curves[8];
FlowType m_type; FlowType m_type;
QRgb m_color;
public: public:
SvPoint(); SvPoint();
@ -36,6 +37,8 @@ public:
void addCurve(SvCurve *curve); void addCurve(SvCurve *curve);
uint curveCount() {return m_curveCount; } uint curveCount() {return m_curveCount; }
QRgb color() { return m_color; }
void setColor(QRgb color) { m_color = color; }
SvCurve *curve(uint index) {return m_curves[index]; } SvCurve *curve(uint index) {return m_curves[index]; }
}; };

View File

@ -1,21 +1,23 @@
#include "svpointcloud.h" #include "svpointcloud.h"
#include <string.h>
SvPointCloud::SvPointCloud(uint width, uint height) SvPointCloud::SvPointCloud(uint width, uint height)
{ {
m_pointFiledHeight = height; m_pointFieldHeight = height;
m_pointFiledWidth = width; m_pointFieldWidth = width;
m_pointField = new SvPoint**[height](); m_pointField = new SvPoint**[height];
for (uint y = 0; y < height; y++) { for (uint y = 0; y < height; y++) {
m_pointField[y] = new SvPoint*[width](); m_pointField[y] = new SvPoint*[width];
memset(m_pointField[y], 0, sizeof(SvPoint*) * width);
} }
} }
SvPointCloud::~SvPointCloud() SvPointCloud::~SvPointCloud()
{ {
for (uint y = 0; y < m_pointFiledHeight; y++) { for (uint y = 0; y < m_pointFieldHeight; y++) {
for (uint x = 0; x < m_pointFiledWidth; x++) { for (uint x = 0; x < m_pointFieldWidth; x++) {
if (m_pointField[y][x]) { if (m_pointField[y][x]) {
delete m_pointField[y][x]; delete m_pointField[y][x];
} }
@ -27,7 +29,7 @@ SvPointCloud::~SvPointCloud()
delete[] m_pointField; delete[] m_pointField;
} }
void SvPointCloud::addPoint(SvPoint point) void SvPointCloud::addPoint(SvPoint &point)
{ {
m_pointField[point.y()][point.x()] = new SvPoint(point); m_pointField[point.y()][point.x()] = new SvPoint(point);
} }

View File

@ -23,8 +23,8 @@ class SvPointCloud: public QObject
friend class SvObject; friend class SvObject;
protected: protected:
uint m_pointFiledHeight; uint m_pointFieldHeight;
uint m_pointFiledWidth; uint m_pointFieldWidth;
SvPoint*** m_pointField; SvPoint*** m_pointField;
QList<SvCurve*> m_curves; QList<SvCurve*> m_curves;
QList<SvFigure*> m_figures; QList<SvFigure*> m_figures;
@ -38,7 +38,10 @@ public:
SvFigure *createFigure(); SvFigure *createFigure();
SvObject *createObject(); SvObject *createObject();
void addPoint(SvPoint point); void addPoint(SvPoint &point);
uint getWidth() { return m_pointFieldWidth; }
uint getHeight() { return m_pointFieldHeight; }
SvPoint *point(uint x, uint y) { return m_pointField[y][x]; }
}; };
#endif // SVPOINTCLOUD_H #endif // SVPOINTCLOUD_H

View File

@ -11,59 +11,35 @@ SvPointCloudViewer::SvPointCloudViewer(QQuickItem *parent) :
void SvPointCloudViewer::paint(QPainter *painter) void SvPointCloudViewer::paint(QPainter *painter)
{ {
painter->beginNativePainting(); if (!m_pointCloud) {
qDebug() << "point cloud not set!";
if (!m_program) { return;
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute highp vec4 vertices;"
"varying highp vec2 coords;"
"void main() {"
" gl_Position = vertices;"
" coords = vertices.xy;"
"}");
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
"uniform lowp float t;"
"varying highp vec2 coords;"
"void main() {"
" lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));"
" i = smoothstep(t - 0.8, t + 0.8, i);"
" i = floor(i * 20.) / 20.;"
" gl_FragColor = vec4(coords * .5 + .5, i, i);"
"}");
m_program->bindAttributeLocation("vertices", 0);
m_program->link();
} }
m_program->bind(); painter->beginNativePainting();
m_program->enableAttributeArray(0); glViewport(0, 0, width(), height());
float values[] = { glClearColor(0, 1, 1, 1);
-1, -1, glClear(GL_COLOR_BUFFER_BIT/* | GL_DEPTH_BUFFER_BIT*/);
1, -1, glMatrixMode(GL_MODELVIEW);
-1, 1, glLoadIdentity();
1, 1 glTranslatef(0.0f, height(), -400.0f);
}; glScalef(0.1f, 0.1f, 0.1f);
m_program->setAttributeArray(0, GL_FLOAT, values, 2); glColor3f(1.0f, 0.0f, 0.0f);
m_program->setUniformValue("t", (float) 1); glPointSize(3.0f);
glViewport(0, 0, width(), height()); glBegin(GL_POINTS);
for (int y = 0; y < m_pointCloud->getHeight(); y++) {
glDisable(GL_DEPTH_TEST); for (int x = 0; x < m_pointCloud->getWidth(); x++) {
SvPoint *p = m_pointCloud->point(x, y);
glClearColor(0, 0, 0, 1); if (p) {
glClear(GL_COLOR_BUFFER_BIT); glColor3ub(qRed(p->color()),qGreen(p->color()), qBlue(p->color()));
glVertex3i(x, y, 0);
glEnable(GL_BLEND); }
glBlendFunc(GL_SRC_ALPHA, GL_ONE); }
}
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glEnd();
m_program->disableAttributeArray(0);
m_program->release();
painter->endNativePainting(); painter->endNativePainting();
} }

View File

@ -8,13 +8,15 @@
#include <QtGui/QOpenGLContext> #include <QtGui/QOpenGLContext>
#include "svpointcloud.h" #include "svpointcloud.h"
#include "svapplicationcontext.h"
class SvPointCloudViewer : public QQuickPaintedItem class SvPointCloudViewer : public QQuickPaintedItem
{ {
Q_OBJECT Q_OBJECT
//Q_PROPERTY(QObject *pointCloud READ pointCloud WRITE setPointCloud NOTIFY pointCloudChanged) Q_PROPERTY(SvPointCloud *pointCloud READ pointCloud WRITE setPointCloud NOTIFY pointCloudChanged)
// Q_PROPERTY(QString test123 READ pointCloud WRITE setPointCloud NOTIFY pointCloudChanged)
// Q_PROPERTY(qreal cameraX READ cameraX WRITE setCameraX NOTIFY cameraXChanged) // Q_PROPERTY(qreal cameraX READ cameraX WRITE setCameraX NOTIFY cameraXChanged)
// Q_PROPERTY(qreal cameraY READ cameraY WRITE setCameraY NOTIFY cameraYChanged) // Q_PROPERTY(qreal cameraY READ cameraY WRITE setCameraY NOTIFY cameraYChanged)
// Q_PROPERTY(qreal cameraZ READ cameraZ WRITE setCameraZ NOTIFY cameraZChanged) // Q_PROPERTY(qreal cameraZ READ cameraZ WRITE setCameraZ NOTIFY cameraZChanged)
@ -25,8 +27,6 @@ protected:
qreal m_cameraY; qreal m_cameraY;
qreal m_cameraZ; qreal m_cameraZ;
QOpenGLShaderProgram *m_program;
public: public:
explicit SvPointCloudViewer(QQuickItem *parent = 0); explicit SvPointCloudViewer(QQuickItem *parent = 0);
void paint(QPainter *painter); void paint(QPainter *painter);

View File

@ -49,12 +49,16 @@ void SvProcessor::enqueueImage(SvPointCloud *pointCloud, SvImage *image)
SvProcessorTask SvProcessor::nextTask() SvProcessorTask SvProcessor::nextTask()
{ {
SvProcessorTask task, *taskPtr; SvProcessorTask task, *taskPtr;
m_nextTaskMutex.lock();
if (!m_taskQueue.size()) { if (!m_taskQueue.size()) {
m_nextTaskMutex.unlock();
throw SvNoMoreTasks(); throw SvNoMoreTasks();
} }
m_nextTaskMutex.lock();
taskPtr = m_taskQueue.dequeue(); taskPtr = m_taskQueue.dequeue();
m_nextTaskMutex.unlock(); m_nextTaskMutex.unlock();
task = *taskPtr; task = *taskPtr;

View File

@ -1,6 +1,7 @@
#ifndef SVSIMPLEPOINT_H #ifndef SVSIMPLEPOINT_H
#define SVSIMPLEPOINT_H #define SVSIMPLEPOINT_H
class SvSimplePoint class SvSimplePoint
{ {
protected: protected:
@ -11,11 +12,9 @@ public:
int y() {return m_py;} int y() {return m_py;}
int z() {return m_pz;} int z() {return m_pz;}
int setX(int px) {return m_px = px;} int setX(int px) {m_px = px;}
int setY(int py) {return m_py = py;} int setY(int py) {m_py = py;}
int setZ(int pz) {return m_pz = pz;} int setZ(int pz) {m_pz = pz;}
SvSimplePoint(); SvSimplePoint();
}; };