Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[qmlSfmData] Upscale the currently selected camera #52

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 72 additions & 45 deletions src/qmlSfmData/CameraLocatorEntity.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "CameraLocatorEntity.hpp"

#include <Qt3DRender/QGeometryRenderer>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QObjectPicker>
#include <Qt3DCore/QTransform>

#include <aliceVision/numeric/numeric.hpp>

Expand Down Expand Up @@ -150,52 +148,21 @@ CameraLocatorEntity::CameraLocatorEntity(const aliceVision::IndexT& viewId, cons
customGeometry->addAttribute(positionAttribute);

// colors buffer
QVector<float> colors(points.size(), 1.0f);
float * color;
_colors = initializeColors(points.size(), 1.0f);

color = &(colors[0 * 3]);
color[0] = 1.0f;
color[1] = 0.0f;
color[2] = 0.0f;

color = &(colors[1 * 3]);
color[0] = 1.0f;
color[1] = 0.0f;
color[2] = 0.0f;

color = &(colors[2 * 3]);
color[0] = 0.0f;
color[1] = 1.0f;
color[2] = 0.0f;

color = &(colors[3 * 3]);
color[0] = 0.0f;
color[1] = 1.0f;
color[2] = 0.0f;

color = &(colors[4 * 3]);
color[0] = 0.0f;
color[1] = 0.0f;
color[2] = 1.0f;

color = &(colors[5 * 3]);
color[0] = 0.0f;
color[1] = 0.0f;
color[2] = 1.0f;

QByteArray colorData(reinterpret_cast<const char*>(colors.data()), colors.size() * static_cast<int>(sizeof(float)));
QByteArray colorData(reinterpret_cast<const char*>(_colors.data()), _colors.size() * static_cast<int>(sizeof(float)));
auto colorDataBuffer = new QBuffer;
colorDataBuffer->setData(colorData);
auto colorAttribute = new QAttribute;
colorAttribute->setAttributeType(QAttribute::VertexAttribute);
colorAttribute->setBuffer(colorDataBuffer);
colorAttribute->setVertexBaseType(QAttribute::Float);
colorAttribute->setVertexSize(3);
colorAttribute->setByteOffset(0);
colorAttribute->setByteStride(3 * sizeof(float));
colorAttribute->setCount(static_cast<uint>(colors.size() / 3));
colorAttribute->setName(QAttribute::defaultColorAttributeName());
customGeometry->addAttribute(colorAttribute);
_colorAttribute = new QAttribute;
_colorAttribute->setAttributeType(QAttribute::VertexAttribute);
_colorAttribute->setBuffer(colorDataBuffer);
_colorAttribute->setVertexBaseType(QAttribute::Float);
_colorAttribute->setVertexSize(3);
_colorAttribute->setByteOffset(0);
_colorAttribute->setByteStride(3 * sizeof(float));
_colorAttribute->setCount(static_cast<uint>(_colors.size() / 3));
_colorAttribute->setName(QAttribute::defaultColorAttributeName());
customGeometry->addAttribute(_colorAttribute);

// geometry renderer settings
customMeshRenderer->setInstanceCount(1);
Expand Down Expand Up @@ -241,4 +208,64 @@ void CameraLocatorEntity::setTransform(const Eigen::Matrix4d& T)
_transform->setMatrix(qmat);
}

QVector<float> CameraLocatorEntity::initializeColors(int size, float defaultValue)
{
QVector<float> colors(size, defaultValue);
float* color;

// R
color = &(colors[0 * 3]);
color[0] = 1.0f;
color[1] = 0.0f;
color[2] = 0.0f;

// R
color = &(colors[1 * 3]);
color[0] = 1.0f;
color[1] = 0.0f;
color[2] = 0.0f;

// G
color = &(colors[2 * 3]);
color[0] = 0.0f;
color[1] = 1.0f;
color[2] = 0.0f;

// G
color = &(colors[3 * 3]);
color[0] = 0.0f;
color[1] = 1.0f;
color[2] = 0.0f;

// B
color = &(colors[4 * 3]);
color[0] = 0.0f;
color[1] = 0.0f;
color[2] = 1.0f;

// B
color = &(colors[5 * 3]);
color[0] = 0.0f;
color[1] = 0.0f;
color[2] = 1.0f;

return colors;
}

void CameraLocatorEntity::updateColors(float red, float green, float blue)
{
const int pyramidIndex = 6 * 3; // Only modify the colors of the pyramid, image plane and camera up direction
for (int i = pyramidIndex; i < _colors.size(); i += 3)
{
_colors[i] = red;
_colors[i + 1] = green;
_colors[i + 2] = blue;
}

QByteArray colorData(reinterpret_cast<const char*>(_colors.data()), _colors.size() * static_cast<int>(sizeof(float)));
auto colorDataBuffer = new Qt3DRender::QBuffer;
colorDataBuffer->setData(colorData);
_colorAttribute->setBuffer(colorDataBuffer);
}

} // namespace sfmdataentity
8 changes: 8 additions & 0 deletions src/qmlSfmData/CameraLocatorEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include <QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QAttribute>

#include <Eigen/Dense>

#include <aliceVision/types.hpp>

namespace sfmdataentity {
Expand All @@ -20,7 +23,10 @@ class CameraLocatorEntity : public Qt3DCore::QEntity
~CameraLocatorEntity() override = default;

void setTransform(const Eigen::Matrix4d&);
QVector<float> initializeColors(int size, float defaultValue = 1.0f);
void updateColors(float red, float green, float blue);

aliceVision::IndexT viewId() const { return _viewId; }
aliceVision::IndexT resectionId() const { return _resectionId; }

Q_SIGNAL void viewIdChanged();
Expand All @@ -30,6 +36,8 @@ class CameraLocatorEntity : public Qt3DCore::QEntity
Qt3DCore::QTransform* _transform;
aliceVision::IndexT _viewId;
aliceVision::IndexT _resectionId;
Qt3DRender::QAttribute* _colorAttribute;
QVector<float> _colors;
};

} // namespace sfmdataentity
51 changes: 50 additions & 1 deletion src/qmlSfmData/SfmDataEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,58 @@ void SfmDataEntity::scaleLocators() const
{
for (auto* transform : entity->findChildren<Qt3DCore::QTransform*>(QString(), Qt::FindDirectChildrenOnly))
{
transform->setScale(_locatorScale);
if (entity->viewId() == _selectedViewId)
{
entity->updateColors(0.f, 0.f, 1.f);
transform->setScale(_locatorScale * 1.5f);
}
else
{
entity->updateColors(1.f, 1.f, 1.f);
transform->setScale(_locatorScale);
}
}
}
}

void SfmDataEntity::setSelectedViewId(const aliceVision::IndexT& viewId)
{
if (_selectedViewId == viewId)
{
return;
}

bool previousReset = _selectedViewId == 0 ? true : false;
bool newUpdated = false;
for (auto* entity : _cameras)
{
if (entity->viewId() == _selectedViewId) // Previously selected camera: the scale must be reset
{
entity->updateColors(1.f, 1.f, 1.f);
for (auto* transform : entity->findChildren<Qt3DCore::QTransform*>(QString(), Qt::FindDirectChildrenOnly))
{
transform->setScale(_locatorScale);
}
previousReset = true;
}
else if (entity->viewId() == viewId) // Newly selected camera: the scale must be enlarged
{
entity->updateColors(0.f, 0.f, 1.f);
for (auto* transform : entity->findChildren<Qt3DCore::QTransform*>(QString(), Qt::FindDirectChildrenOnly))
{
transform->setScale(_locatorScale * 1.5f);
}
newUpdated = true;
}

if (previousReset && newUpdated)
{
break;
}
}
_selectedViewId = viewId;

Q_EMIT selectedViewIdChanged();
}

void SfmDataEntity::setResectionId(const aliceVision::IndexT& value)
Expand Down
5 changes: 5 additions & 0 deletions src/qmlSfmData/SfmDataEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SfmDataEntity : public Qt3DCore::QEntity
Q_PROPERTY(float locatorScale READ locatorScale WRITE setLocatorScale NOTIFY locatorScaleChanged)
Q_PROPERTY(QQmlListProperty<sfmdataentity::CameraLocatorEntity> cameras READ cameras NOTIFY camerasChanged)
Q_PROPERTY(QQmlListProperty<sfmdataentity::PointCloudEntity> pointClouds READ pointClouds NOTIFY pointCloudsChanged)
Q_PROPERTY(quint32 selectedViewId READ selectedViewId WRITE setSelectedViewId NOTIFY selectedViewIdChanged)
Q_PROPERTY(quint32 resectionId READ resectionId WRITE setResectionId NOTIFY resectionIdChanged)

Q_PROPERTY(Status status READ status NOTIFY statusChanged)
Expand All @@ -47,10 +48,12 @@ class SfmDataEntity : public Qt3DCore::QEntity
Q_SLOT const QUrl& source() const { return _source; }
Q_SLOT float pointSize() const { return _pointSize; }
Q_SLOT float locatorScale() const { return _locatorScale; }
Q_SLOT aliceVision::IndexT selectedViewId() const { return _selectedViewId; }
Q_SLOT aliceVision::IndexT resectionId() const { return _resectionId; }
Q_SLOT void setSource(const QUrl& source);
Q_SLOT void setPointSize(const float& value);
Q_SLOT void setLocatorScale(const float& value);
Q_SLOT void setSelectedViewId(const aliceVision::IndexT& viewId);
Q_SLOT void setResectionId(const aliceVision::IndexT& value);

Status status() const { return _status; }
Expand All @@ -71,6 +74,7 @@ class SfmDataEntity : public Qt3DCore::QEntity
Q_SIGNAL void objectPicked(Qt3DCore::QTransform* transform);
Q_SIGNAL void statusChanged(Status status);
Q_SIGNAL void skipHiddenChanged();
Q_SIGNAL void selectedViewIdChanged();
Q_SIGNAL void resectionIdChanged();

protected:
Expand All @@ -94,6 +98,7 @@ class SfmDataEntity : public Qt3DCore::QEntity
bool _skipHidden = false;
float _pointSize = 0.5f;
float _locatorScale = 1.0f;
aliceVision::IndexT _selectedViewId = 0;
aliceVision::IndexT _resectionId = 0;
Qt3DRender::QParameter* _pointSizeParameter;
Qt3DRender::QMaterial* _cloudMaterial;
Expand Down