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 \
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

View File

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

View File

@ -2,18 +2,20 @@
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlContext>
#include <QPixmap>
#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<SvPointCloudViewer>("SvPCV", 1, 0, "SvPointCloudViewer");
qmlRegisterType<SvApplicationContext>("SvPCV", 1, 0, "SvApplicationContext");
qRegisterMetaType<SvPointCloud*>();
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();

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
#include <QDebug>
#include <QRgb>
typedef unsigned int SvPointId;
typedef unsigned int uint;

View File

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

View File

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

View File

@ -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]; }
};

View File

@ -1,21 +1,23 @@
#include "svpointcloud.h"
#include <string.h>
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);
}

View File

@ -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<SvCurve*> m_curves;
QList<SvFigure*> 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

View File

@ -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();
m_program->enableAttributeArray(0);
float values[] = {
-1, -1,
1, -1,
-1, 1,
1, 1
};
m_program->setAttributeArray(0, GL_FLOAT, values, 2);
m_program->setUniformValue("t", (float) 1);
painter->beginNativePainting();
glViewport(0, 0, width(), height());
glDisable(GL_DEPTH_TEST);
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);
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();
}

View File

@ -8,13 +8,15 @@
#include <QtGui/QOpenGLContext>
#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);

View File

@ -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;

View File

@ -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();
};