From b6b4bf3eb6d8db757b1b47e581258a1fcf6cab9d Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Mon, 28 Jul 2014 20:25:39 +0400 Subject: [PATCH] Fixed seg fault --- CSV.pro | 6 ++-- Main.qml | 2 +- main.cpp | 9 +++-- svapplicationcontext.cpp | 6 ++++ svapplicationcontext.h | 22 ++++++++++++ svdefs.h | 1 + svkernel.cpp | 9 +++-- svpoint.cpp | 3 +- svpoint.h | 3 ++ svpointcloud.cpp | 16 +++++---- svpointcloud.h | 9 +++-- svpointcloudviewer.cpp | 72 ++++++++++++++-------------------------- svpointcloudviewer.h | 6 ++-- svprocessor.cpp | 6 +++- svsimplepoint.h | 9 +++-- 15 files changed, 103 insertions(+), 76 deletions(-) create mode 100644 svapplicationcontext.cpp create mode 100644 svapplicationcontext.h diff --git a/CSV.pro b/CSV.pro index d53ea8d..0d5f546 100644 --- a/CSV.pro +++ b/CSV.pro @@ -24,7 +24,8 @@ SOURCES += main.cpp \ svobject.cpp \ svpointcloud.cpp \ svkernel.cpp \ - svpointcloudviewer.cpp + svpointcloudviewer.cpp \ + svapplicationcontext.cpp HEADERS += \ svimage.h \ @@ -39,7 +40,8 @@ HEADERS += \ svpointcloud.h \ svdefs.h \ svkernel.h \ - svpointcloudviewer.h + svpointcloudviewer.h \ + svapplicationcontext.h RESOURCES += \ resource.qrc diff --git a/Main.qml b/Main.qml index c3ab444..406bf54 100644 --- a/Main.qml +++ b/Main.qml @@ -152,7 +152,7 @@ ApplicationWindow { Item { anchors.fill: parent SvPointCloudViewer { - //pointCloud: pointCloud + pointCloud: app.getPointCloud() anchors.fill: parent } } diff --git a/main.cpp b/main.cpp index e09c58d..4fd389a 100644 --- a/main.cpp +++ b/main.cpp @@ -2,18 +2,20 @@ #include #include #include -#include #include "svimage.h" #include "svprocessor.h" #include "svimageprovider.h" #include "svpointcloudviewer.h" +#include "svapplicationcontext.h" int main(int argc, char *argv[]) { QGuiApplication a(argc, argv); qmlRegisterType("SvPCV", 1, 0, "SvPointCloudViewer"); + qmlRegisterType("SvPCV", 1, 0, "SvApplicationContext"); + qRegisterMetaType(); QQmlApplicationEngine engine; SvImageProvider imageProvider; @@ -25,6 +27,9 @@ int main(int argc, char *argv[]) SvImage right(imgRight); SvPointCloud pointCloud(imgLeft.width(), imgLeft.height()); + SvApplicationContext m; + + m.setPointCloud(&pointCloud); SvProcessor proc(4); @@ -35,7 +40,7 @@ int main(int argc, char *argv[]) engine.addImageProvider("images", &imageProvider); engine.rootContext()->setContextProperty("processor", &proc); - engine.rootContext()->setContextProperty("pointCloud", &pointCloud); + engine.rootContext()->setContextProperty("app", &m); engine.load(QUrl(QStringLiteral("qrc:///Main.qml"))); proc.start(); diff --git a/svapplicationcontext.cpp b/svapplicationcontext.cpp new file mode 100644 index 0000000..23c847f --- /dev/null +++ b/svapplicationcontext.cpp @@ -0,0 +1,6 @@ +#include "svapplicationcontext.h" + +SvApplicationContext::SvApplicationContext(QObject *parent) : + QObject(parent) +{ +} diff --git a/svapplicationcontext.h b/svapplicationcontext.h new file mode 100644 index 0000000..a40d5fa --- /dev/null +++ b/svapplicationcontext.h @@ -0,0 +1,22 @@ +#ifndef SVPOINTCLOUDVIEWMODEL_H +#define SVPOINTCLOUDVIEWMODEL_H + +#include + +#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 diff --git a/svdefs.h b/svdefs.h index 5132cbe..35d81a1 100644 --- a/svdefs.h +++ b/svdefs.h @@ -2,6 +2,7 @@ #define SVDEFS_H #include +#include typedef unsigned int SvPointId; typedef unsigned int uint; diff --git a/svkernel.cpp b/svkernel.cpp index 0e69935..1f8c635 100644 --- a/svkernel.cpp +++ b/svkernel.cpp @@ -89,10 +89,13 @@ void SvKernel::exec(SvPointCloud *pc, SvImage *image, int line) } if (value > 0) { - p.setX(x - 1); - p.setY(line - 1); + if (x > 0 && line > 0){ + p.setX(x - 1); + p.setY(line - 1); + p.setColor(qRgb(value, value, value)); - pc->addPoint(p); + pc->addPoint(p); + } } __dX = _dX; diff --git a/svpoint.cpp b/svpoint.cpp index d6522b8..63e4f21 100644 --- a/svpoint.cpp +++ b/svpoint.cpp @@ -7,7 +7,8 @@ SvPoint::SvPoint() SvPoint::SvPoint(SvPoint &point) { - + m_px = point.x(); + m_py = point.y(); } void SvPoint::addCurve(SvCurve *curve) diff --git a/svpoint.h b/svpoint.h index 744e1ef..fa442b3 100644 --- a/svpoint.h +++ b/svpoint.h @@ -29,6 +29,7 @@ protected: uint m_curveCount; SvCurve *m_curves[8]; FlowType m_type; + QRgb m_color; public: SvPoint(); @@ -36,6 +37,8 @@ public: void addCurve(SvCurve *curve); 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]; } }; diff --git a/svpointcloud.cpp b/svpointcloud.cpp index ddcbf16..24bde77 100644 --- a/svpointcloud.cpp +++ b/svpointcloud.cpp @@ -1,21 +1,23 @@ #include "svpointcloud.h" +#include SvPointCloud::SvPointCloud(uint width, uint height) { - m_pointFiledHeight = height; - m_pointFiledWidth = width; + m_pointFieldHeight = height; + m_pointFieldWidth = width; - m_pointField = new SvPoint**[height](); + m_pointField = new SvPoint**[height]; 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() { - for (uint y = 0; y < m_pointFiledHeight; y++) { - for (uint x = 0; x < m_pointFiledWidth; x++) { + for (uint y = 0; y < m_pointFieldHeight; y++) { + for (uint x = 0; x < m_pointFieldWidth; x++) { if (m_pointField[y][x]) { delete m_pointField[y][x]; } @@ -27,7 +29,7 @@ SvPointCloud::~SvPointCloud() delete[] m_pointField; } -void SvPointCloud::addPoint(SvPoint point) +void SvPointCloud::addPoint(SvPoint &point) { m_pointField[point.y()][point.x()] = new SvPoint(point); } diff --git a/svpointcloud.h b/svpointcloud.h index 00da5d8..2ccbe64 100644 --- a/svpointcloud.h +++ b/svpointcloud.h @@ -23,8 +23,8 @@ class SvPointCloud: public QObject friend class SvObject; protected: - uint m_pointFiledHeight; - uint m_pointFiledWidth; + uint m_pointFieldHeight; + uint m_pointFieldWidth; SvPoint*** m_pointField; QList m_curves; QList m_figures; @@ -38,7 +38,10 @@ public: SvFigure *createFigure(); 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 diff --git a/svpointcloudviewer.cpp b/svpointcloudviewer.cpp index e310344..d263815 100644 --- a/svpointcloudviewer.cpp +++ b/svpointcloudviewer.cpp @@ -11,59 +11,35 @@ SvPointCloudViewer::SvPointCloudViewer(QQuickItem *parent) : void SvPointCloudViewer::paint(QPainter *painter) { - painter->beginNativePainting(); - - if (!m_program) { - 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(); - + if (!m_pointCloud) { + qDebug() << "point cloud not set!"; + return; } - m_program->bind(); + painter->beginNativePainting(); - m_program->enableAttributeArray(0); + glViewport(0, 0, width(), height()); - float values[] = { - -1, -1, - 1, -1, - -1, 1, - 1, 1 - }; - m_program->setAttributeArray(0, GL_FLOAT, values, 2); - m_program->setUniformValue("t", (float) 1); + glClearColor(0, 1, 1, 1); + glClear(GL_COLOR_BUFFER_BIT/* | GL_DEPTH_BUFFER_BIT*/); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0f, height(), -400.0f); + glScalef(0.1f, 0.1f, 0.1f); + glColor3f(1.0f, 0.0f, 0.0f); + glPointSize(3.0f); - glViewport(0, 0, width(), height()); - - glDisable(GL_DEPTH_TEST); - - glClearColor(0, 0, 0, 1); - glClear(GL_COLOR_BUFFER_BIT); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE); - - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - - m_program->disableAttributeArray(0); - m_program->release(); + glBegin(GL_POINTS); + for (int y = 0; y < m_pointCloud->getHeight(); y++) { + for (int x = 0; x < m_pointCloud->getWidth(); x++) { + SvPoint *p = m_pointCloud->point(x, y); + if (p) { + glColor3ub(qRed(p->color()),qGreen(p->color()), qBlue(p->color())); + glVertex3i(x, y, 0); + } + } + } + glEnd(); painter->endNativePainting(); } diff --git a/svpointcloudviewer.h b/svpointcloudviewer.h index 4da4556..948c991 100644 --- a/svpointcloudviewer.h +++ b/svpointcloudviewer.h @@ -8,13 +8,15 @@ #include #include "svpointcloud.h" +#include "svapplicationcontext.h" class SvPointCloudViewer : public QQuickPaintedItem { 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 cameraY READ cameraY WRITE setCameraY NOTIFY cameraYChanged) // Q_PROPERTY(qreal cameraZ READ cameraZ WRITE setCameraZ NOTIFY cameraZChanged) @@ -25,8 +27,6 @@ protected: qreal m_cameraY; qreal m_cameraZ; - QOpenGLShaderProgram *m_program; - public: explicit SvPointCloudViewer(QQuickItem *parent = 0); void paint(QPainter *painter); diff --git a/svprocessor.cpp b/svprocessor.cpp index fac0e7e..84f6e06 100644 --- a/svprocessor.cpp +++ b/svprocessor.cpp @@ -49,12 +49,16 @@ void SvProcessor::enqueueImage(SvPointCloud *pointCloud, SvImage *image) SvProcessorTask SvProcessor::nextTask() { SvProcessorTask task, *taskPtr; + + m_nextTaskMutex.lock(); + if (!m_taskQueue.size()) { + m_nextTaskMutex.unlock(); throw SvNoMoreTasks(); } - m_nextTaskMutex.lock(); taskPtr = m_taskQueue.dequeue(); + m_nextTaskMutex.unlock(); task = *taskPtr; diff --git a/svsimplepoint.h b/svsimplepoint.h index aadf710..685664e 100644 --- a/svsimplepoint.h +++ b/svsimplepoint.h @@ -1,6 +1,7 @@ #ifndef SVSIMPLEPOINT_H #define SVSIMPLEPOINT_H + class SvSimplePoint { protected: @@ -11,11 +12,9 @@ public: int y() {return m_py;} int z() {return m_pz;} - int setX(int px) {return m_px = px;} - int setY(int py) {return m_py = py;} - int setZ(int pz) {return m_pz = pz;} - - + int setX(int px) {m_px = px;} + int setY(int py) {m_py = py;} + int setZ(int pz) {m_pz = pz;} SvSimplePoint(); };