diff --git a/gui/qml/res/config/binocularCameraConfig.json b/gui/qml/res/config/binocularCameraConfig.json index 39ddc28..424ff8f 100644 --- a/gui/qml/res/config/binocularCameraConfig.json +++ b/gui/qml/res/config/binocularCameraConfig.json @@ -58,12 +58,12 @@ "type" : "number" }, { - "data" : 32.0, + "data" : 64.0, "title" : "Cycles", "type" : "number" }, { - "data" : 18.0, + "data" : 19.0, "title" : "Total Fringes", "type" : "number" }, @@ -106,7 +106,7 @@ "type" : "bool" }, { - "data" : 1, + "data" : 0, "title" : "Gpu Accelerate", "type" : "bool" }, diff --git a/gui/qml/res/config/monocularCameraConfig.json b/gui/qml/res/config/monocularCameraConfig.json index 815ca1e..5d3fd8b 100644 --- a/gui/qml/res/config/monocularCameraConfig.json +++ b/gui/qml/res/config/monocularCameraConfig.json @@ -81,7 +81,7 @@ "type" : "bool" }, { - "data" : 1, + "data" : 0, "title" : "Gpu Accelerate", "type" : "bool" }, diff --git a/gui/qml/ui/global/Lang.qml b/gui/qml/ui/global/Lang.qml index d490d65..1764a6d 100644 --- a/gui/qml/ui/global/Lang.qml +++ b/gui/qml/ui/global/Lang.qml @@ -258,6 +258,8 @@ QtObject { property string height property string length property string inverse + property string clip_width + property string clip_height function zh() { settings = "设置"; @@ -515,6 +517,8 @@ QtObject { height = "高度"; length = "长度"; inverse = "翻转特征方向"; + clip_width = "裁剪宽度"; + clip_height = "裁剪高度"; } function en() { @@ -566,6 +570,8 @@ QtObject { error_diffusion_method = "Error Diffusion Method"; img_width = "IMG Width"; img_height = "IMG Height"; + clip_width = "Clip Width"; + clip_height = "Clip Height"; cycles = "Cycles"; shift_time = "Shift Time"; stripe_img = "Stripe IMG"; diff --git a/gui/qml/ui/page/Page_Device.qml b/gui/qml/ui/page/Page_Device.qml index f6f7c1a..92e81b6 100644 --- a/gui/qml/ui/page/Page_Device.qml +++ b/gui/qml/ui/page/Page_Device.qml @@ -28,6 +28,8 @@ FluContentPage{ property int defocus_encoding: pixel_depth ? AppType.Disable : AppType.OptimalPlusWithModulation property int img_width: Number(CameraEngine.getStringAttribute("DLP Width")) property int img_height: Number(CameraEngine.getStringAttribute("DLP Height")) + property int clip_width: img_width + property int clip_height: img_height property int cycles: CameraEngine.getNumberAttribute("Cycles") property int shiftTime: CameraEngine.getNumberAttribute("Phase Shift Times") property int connect_state : AppType.Disconnect @@ -688,6 +690,42 @@ FluContentPage{ } } + FluText { + Layout.fillWidth: true + text: Lang.clip_width + font: FluTextStyle.BodyStrong + } + + FluText { + Layout.fillWidth: true + text: Lang.clip_height + font: FluTextStyle.BodyStrong + } + + FluSpinBox { + id: clip_width_spbox + editable: true + value: root.clip_width + from: 0 + to: 9999999 + + onValueChanged: { + root.clip_width = value; + } + } + + FluSpinBox { + id: clip_height_spbox + editable: true + value: root.clip_height + from: 0 + to: 9999999 + + onValueChanged: { + root.clip_height = value; + } + } + FluText { Layout.fillWidth: true text: Lang.cycles @@ -763,7 +801,7 @@ FluContentPage{ text: Lang.encode onClicked: { - var num_of_stripes = CameraEngine.createStripe(root.pixel_depth, root.stripe_direction, root.stripe_type, root.defocus_encoding, root.img_width, root.img_height, root.cycles, root.shiftTime, root.isKeepAdd); + var num_of_stripes = CameraEngine.createStripe(root.pixel_depth, root.stripe_direction, root.stripe_type, root.defocus_encoding, root.img_width, root.img_height, root.clip_width, root.clip_height, root.cycles, root.shiftTime, root.isKeepAdd); stripe_index_indicator.pageCurrent = 1; CameraEngine.displayStripe(1); root.enableBurningStripe = true; diff --git a/gui/qml/ui/page/Page_Scan.qml b/gui/qml/ui/page/Page_Scan.qml index 70f8b9c..24ed2a2 100644 --- a/gui/qml/ui/page/Page_Scan.qml +++ b/gui/qml/ui/page/Page_Scan.qml @@ -545,6 +545,7 @@ FluContentPage { onAccepted: { VTKProcessEngine.saveCloud(currentFile.toString()); + CameraEngine.saveFrame(currentFile.toString()); } } diff --git a/gui/src/CameraEngine.cpp b/gui/src/CameraEngine.cpp index a09e646..5685b5a 100644 --- a/gui/src/CameraEngine.cpp +++ b/gui/src/CameraEngine.cpp @@ -64,6 +64,7 @@ CameraEngine::~CameraEngine() { int CameraEngine::createStripe(const int pixelDepth, const int direction, const int stripeType, const int defocusMethod, const int imgWidth, const int imgHeight, + const int clipWidth, const int clipHeight, const int cycles, const int shiftTime, const bool isKeepAdd) { qInfo() << "start create stripe..."; @@ -99,18 +100,24 @@ int CameraEngine::createStripe(const int pixelDepth, const int direction, AppType::DefocusEncoding(defocusMethod)); } + if(clipWidth < imgWidth || clipHeight < imgHeight) { + for (auto& img : imgs) { + img(cv::Rect(0, 0, clipWidth, clipHeight)).copyTo(img); + } + } + auto formatType = AppType::PixelDepth(pixelDepth) == AppType::PixelDepth::OneBit ? QImage::Format_Mono : QImage::Format_Grayscale8; std::vector tempStripes(imgs.size(), - QImage(imgWidth, imgHeight, formatType)); + QImage(clipWidth, clipHeight, formatType)); cv::parallel_for_(cv::Range(0, imgs.size()), [&](const cv::Range &range) { for (int i = range.start; i < range.end; ++i) { - for (int j = 0; j < imgHeight; ++j) { + for (int j = 0; j < clipHeight; ++j) { auto imgPtr = imgs[i].ptr(j); - for (int k = 0; k < imgWidth; ++k) { + for (int k = 0; k < clipWidth; ++k) { formatType == QImage::Format_Mono ? tempStripes[i].setPixel(k, j, imgPtr[k]) : tempStripes[i].setPixel( @@ -966,3 +973,14 @@ void CameraEngine::tenLine() { workThread_ = std::thread(&CameraEngine::createTenLine, this); } + +bool CameraEngine::saveFrame(const QString& path) { + auto fileName = path.mid(8, path.size() - 12); + + qDebug() << QString("save frame, frame path is : %s").arg(fileName); + + cv::imwrite(fileName.toStdString() + ".bmp", frame_.textureMap_); + cv::imwrite(fileName.toStdString() + ".tiff", frame_.depthMap_); + + return true; +} \ No newline at end of file diff --git a/gui/src/CameraEngine.h b/gui/src/CameraEngine.h index 4ceaf32..8689272 100644 --- a/gui/src/CameraEngine.h +++ b/gui/src/CameraEngine.h @@ -1,19 +1,20 @@ /** * @file CameraEngine.h * @author Evans Liu (1369215984@qq.com) - * @brief + * @brief * @version 0.1 * @date 2024-03-19 - * + * * @copyright Copyright (c) 2024 - * + * */ #ifndef __CAMERA_ENGINE_H_ #define __CAMERA_ENGINE_H_ -#include #include +#include + #include #include @@ -21,9 +22,10 @@ #include #include "AppType.h" +#include "CameraModel.h" #include "ImagePaintItem.h" #include "typeDef.h" -#include "CameraModel.h" + #include @@ -33,28 +35,42 @@ class CameraEngine : public QObject { Q_PROPERTY_AUTO(bool, isConnected) Q_PROPERTY_AUTO(bool, isBurnWorkFinish) public: - static CameraEngine* instance(); + static CameraEngine *instance(); struct OrderTableRecord { OrderTableRecord() {} - OrderTableRecord(const int patternsNum, const int shiftTime, const bool isVertical) : patternsNum_(patternsNum), shiftTime_(shiftTime), isVertical_(isVertical) {} + OrderTableRecord(const int patternsNum, const int shiftTime, + const bool isVertical) + : patternsNum_(patternsNum), shiftTime_(shiftTime), + isVertical_(isVertical) {} int patternsNum_; int shiftTime_; bool isVertical_; }; - //Device page + // Device page Q_INVOKABLE void startDetectCameraState(); - Q_INVOKABLE int createStripe(const int pixelDepth, const int direction, const int stripeType, const int defocusMethod, const int imgWidth, const int imgHeight, const int cycles, const int shiftTime, const bool isKeepAdd); + Q_INVOKABLE int createStripe(const int pixelDepth, const int direction, + const int stripeType, const int defocusMethod, + const int imgWidth, const int imgHeight, + const int clipWidth, const int clipHeight, + const int cycles, const int shiftTime, + const bool isKeepAdd); Q_INVOKABLE void displayStripe(const int stripeIndex); Q_INVOKABLE void selectCamera(const int cameraType); Q_INVOKABLE void setCameraJsonPath(const std::string jsonPath); Q_INVOKABLE bool connectCamera(); Q_INVOKABLE bool disConnectCamera(); Q_INVOKABLE void burnStripe(); - Q_INVOKABLE void bindStripePaintItem(ImagePaintItem* stripePaintItem) { stripePaintItem_ = stripePaintItem; } - //offlineScan page - Q_INVOKABLE void bindOfflineCamPaintItem(ImagePaintItem* camPaintItem) { offlineCamPaintItem_ = camPaintItem; } - //scan page - Q_INVOKABLE void setScanMode(const int scanMode) { scanMode_ = AppType::ScanModeType(scanMode); } + Q_INVOKABLE void bindStripePaintItem(ImagePaintItem *stripePaintItem) { + stripePaintItem_ = stripePaintItem; + } + // offlineScan page + Q_INVOKABLE void bindOfflineCamPaintItem(ImagePaintItem *camPaintItem) { + offlineCamPaintItem_ = camPaintItem; + } + // scan page + Q_INVOKABLE void setScanMode(const int scanMode) { + scanMode_ = AppType::ScanModeType(scanMode); + } Q_INVOKABLE void projectOnce(); Q_INVOKABLE void projectContinues(); Q_INVOKABLE void pauseProject(const bool isResume); @@ -64,32 +80,52 @@ class CameraEngine : public QObject { Q_INVOKABLE void startScan(); Q_INVOKABLE void continuesScan(); Q_INVOKABLE void pauseScan(); - Q_INVOKABLE void bindOfflineLeftCamModel(CameraModel* model) { leftCamModel_ = model; } - Q_INVOKABLE void bindOfflineRightCamModel(CameraModel* model) { rightCamModel_ = model; } - Q_INVOKABLE void bindOfflineColorCamModel(CameraModel* model) { colorCamModel_ = model; } - Q_INVOKABLE void bindScanTexturePaintItem(ImagePaintItem* paintItem) { scanTexturePaintItem_ = paintItem; } - Q_INVOKABLE void updateDisplayImg(const QString& imgPath); - Q_INVOKABLE void saveStripe(const QString& path); + Q_INVOKABLE void bindOfflineLeftCamModel(CameraModel *model) { + leftCamModel_ = model; + } + Q_INVOKABLE void bindOfflineRightCamModel(CameraModel *model) { + rightCamModel_ = model; + } + Q_INVOKABLE void bindOfflineColorCamModel(CameraModel *model) { + colorCamModel_ = model; + } + Q_INVOKABLE void bindScanTexturePaintItem(ImagePaintItem *paintItem) { + scanTexturePaintItem_ = paintItem; + } + Q_INVOKABLE void updateDisplayImg(const QString &imgPath); + Q_INVOKABLE void saveStripe(const QString &path); Q_INVOKABLE void setPatternType(const int patternType); - Q_INVOKABLE bool setNumberAttribute(const QString& attributeName, - const double val); - Q_INVOKABLE bool setBooleanAttribute(const QString& attributeName, const bool val); - Q_INVOKABLE double getNumberAttribute(const QString& attributeName); - Q_INVOKABLE bool getBooleanAttribute(const QString& attributeName); - Q_INVOKABLE QString getStringAttribute(const QString& attributeName); - Q_INVOKABLE const slmaster::cameras::FrameData& getCurFrame() { return frame_; } - std::shared_ptr getSLCamera() { return slCameraFactory_.getCamera(slmaster::cameras::CameraType(cameraType_)); } - std::vector getOrderTableRecord() { return orderTableRecord_; } + Q_INVOKABLE bool setNumberAttribute(const QString &attributeName, + const double val); + Q_INVOKABLE bool setBooleanAttribute(const QString &attributeName, + const bool val); + Q_INVOKABLE double getNumberAttribute(const QString &attributeName); + Q_INVOKABLE bool getBooleanAttribute(const QString &attributeName); + Q_INVOKABLE QString getStringAttribute(const QString &attributeName); + Q_INVOKABLE const slmaster::cameras::FrameData &getCurFrame() { + return frame_; + } + Q_INVOKABLE bool saveFrame(const QString &path); + std::shared_ptr getSLCamera() { + return slCameraFactory_.getCamera( + slmaster::cameras::CameraType(cameraType_)); + } + std::vector getOrderTableRecord() { + return orderTableRecord_; + } signals: void stripeImgsChanged(const int num); void frameCaptured(); + private: CameraEngine(); ~CameraEngine(); - CameraEngine(const CameraEngine&) = delete; - const CameraEngine& operator=(const CameraEngine&) = delete; - void defocusStripeCreate(std::vector& imgs, const int direction, const int cycles, const int shiftTime, AppType::DefocusEncoding method); - void realTimeRenderImg(const QImage& img); + CameraEngine(const CameraEngine &) = delete; + const CameraEngine &operator=(const CameraEngine &) = delete; + void defocusStripeCreate(std::vector &imgs, const int direction, + const int cycles, const int shiftTime, + AppType::DefocusEncoding method); + void realTimeRenderImg(const QImage &img); void createTenLine(); void switchTrigMode(const bool isTrigLine, const int exposureTime); std::vector orderTableRecord_; @@ -97,17 +133,17 @@ class CameraEngine : public QObject { AppType::ScanModeType scanMode_; AppType::CameraType cameraType_; AppType::PatternMethod patternType_; - static CameraEngine* engine_; - ImagePaintItem* stripePaintItem_ = nullptr; - ImagePaintItem* offlineCamPaintItem_ = nullptr; - ImagePaintItem* scanTexturePaintItem_ = nullptr; + static CameraEngine *engine_; + ImagePaintItem *stripePaintItem_ = nullptr; + ImagePaintItem *offlineCamPaintItem_ = nullptr; + ImagePaintItem *scanTexturePaintItem_ = nullptr; std::thread onlineDetectThread_; std::thread workThread_; slmaster::cameras::SLCameraFactory slCameraFactory_; std::shared_ptr pattern_ = nullptr; - CameraModel* leftCamModel_ = nullptr; - CameraModel* rightCamModel_ = nullptr; - CameraModel* colorCamModel_ = nullptr; + CameraModel *leftCamModel_ = nullptr; + CameraModel *rightCamModel_ = nullptr; + CameraModel *colorCamModel_ = nullptr; slmaster::cameras::FrameData frame_; std::thread test_thread_; std::atomic_bool appExit_; @@ -116,4 +152,4 @@ class CameraEngine : public QObject { std::atomic_bool isContinusStop_; }; -#endif// !__CAMERA_ENGINE_H_ +#endif // !__CAMERA_ENGINE_H_ diff --git a/gui/src/VtkProcessEngine.cpp b/gui/src/VtkProcessEngine.cpp index 5f85919..f4a9b2c 100644 --- a/gui/src/VtkProcessEngine.cpp +++ b/gui/src/VtkProcessEngine.cpp @@ -1,71 +1,74 @@ #include "VtkProcessEngine.h" #include "VtkCusInteractorStyleRubberBandPick.h" -#include -#include -#include -#include -#include -#include +#include +#include +#include #include -#include +#include #include +#include #include +#include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include -#include -#include -#include -#include -#include -#include #include -#include #include -#include +#include +#include +#include +#include #include -#include -#include -#include -#include -#include #include -#include -//#include -#include +#include +#include +#include +#include +#include +#include +#include +#include + +// #include #include +#include #include -#include #include +#include + #include -#include -#include -#include #include +#include +#include #include +#include + -VTKProcessEngine* VTKProcessEngine::vtkProcessEngine_ = new VTKProcessEngine(); +VTKProcessEngine *VTKProcessEngine::vtkProcessEngine_ = new VTKProcessEngine(); vtkNew tempActor; vtkNew lookupPre; const double axesActor_length = 100.0; const int16_t axesActor_label_font_size = 20; -void progressValCallbackFunc(vtkObject* obj,unsigned long eid,void* clientdata,void *calldata) { - double* progressVal = static_cast(clientdata); - *progressVal = *(static_cast(calldata)); +void progressValCallbackFunc(vtkObject *obj, unsigned long eid, + void *clientdata, void *calldata) { + double *progressVal = static_cast(clientdata); + *progressVal = *(static_cast(calldata)); } void createLine(const double x1, const double y1, const double z1, const double x2, const double y2, const double z2, - vtkSmartPointer points, vtkSmartPointer cells) -{ + vtkSmartPointer points, + vtkSmartPointer cells) { vtkSmartPointer line; line = vtkSmartPointer::New(); line->GetPointIds()->SetNumberOfIds(2); @@ -80,24 +83,29 @@ void createLine(const double x1, const double y1, const double z1, cells->InsertNextCell(line); } -void VTKProcessEngine::initOrientedWidget(VTKRenderItem* item, const bool isPostProcess) { - orientedWidgetAxesActor_->SetTotalLength(axesActor_length, axesActor_length, axesActor_length); +void VTKProcessEngine::initOrientedWidget(VTKRenderItem *item, + const bool isPostProcess) { + orientedWidgetAxesActor_->SetTotalLength(axesActor_length, axesActor_length, + axesActor_length); - if(!isPostProcess) { + if (!isPostProcess) { orientationWidget_ = vtkSmartPointer::New(); orientationWidget_->SetOutlineColor(0.9300, 0.5700, 0.1300); orientationWidget_->SetOrientationMarker(orientedWidgetAxesActor_); - orientationWidget_->SetInteractor(item->renderWindow()->renderWindow()->GetInteractor()); + orientationWidget_->SetInteractor( + item->renderWindow()->renderWindow()->GetInteractor()); orientationWidget_->SetViewport(0.8, 0, 0.95, 0.2); orientationWidget_->SetEnabled(true); orientationWidget_->On(); orientationWidget_->InteractiveOff(); - } - else { - postProcessOrientationWidget_ = vtkSmartPointer::New(); + } else { + postProcessOrientationWidget_ = + vtkSmartPointer::New(); postProcessOrientationWidget_->SetOutlineColor(0.9300, 0.5700, 0.1300); - postProcessOrientationWidget_->SetOrientationMarker(orientedWidgetAxesActor_); - postProcessOrientationWidget_->SetInteractor(item->renderWindow()->renderWindow()->GetInteractor()); + postProcessOrientationWidget_->SetOrientationMarker( + orientedWidgetAxesActor_); + postProcessOrientationWidget_->SetInteractor( + item->renderWindow()->renderWindow()->GetInteractor()); postProcessOrientationWidget_->SetViewport(0.8, 0, 0.95, 0.2); postProcessOrientationWidget_->SetEnabled(true); postProcessOrientationWidget_->On(); @@ -105,21 +113,32 @@ void VTKProcessEngine::initOrientedWidget(VTKRenderItem* item, const bool isPost } } -void VTKProcessEngine::addAxesActorAndWidget(VTKRenderItem* item) { - axesActor_->SetTotalLength(axesActor_length, axesActor_length, axesActor_length); - axesActor_->GetXAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone(); - axesActor_->GetYAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone(); - axesActor_->GetZAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone(); - axesActor_->GetXAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize(axesActor_label_font_size); - axesActor_->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize(axesActor_label_font_size); - axesActor_->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize(axesActor_label_font_size); +void VTKProcessEngine::addAxesActorAndWidget(VTKRenderItem *item) { + axesActor_->SetTotalLength(axesActor_length, axesActor_length, + axesActor_length); + axesActor_->GetXAxisCaptionActor2D() + ->GetTextActor() + ->SetTextScaleModeToNone(); + axesActor_->GetYAxisCaptionActor2D() + ->GetTextActor() + ->SetTextScaleModeToNone(); + axesActor_->GetZAxisCaptionActor2D() + ->GetTextActor() + ->SetTextScaleModeToNone(); + axesActor_->GetXAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize( + axesActor_label_font_size); + axesActor_->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize( + axesActor_label_font_size); + axesActor_->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize( + axesActor_label_font_size); item->renderer()->AddActor(axesActor_); } -void VTKProcessEngine::addGridActor(VTKRenderItem* item) { +void VTKProcessEngine::addGridActor(VTKRenderItem *item) { vtkNew platformGrid; - vtkSmartPointer platformGridMapper = vtkSmartPointer::New(); + vtkSmartPointer platformGridMapper = + vtkSmartPointer::New(); platformGridMapper->SetInputData(platformGrid); gridActor_->SetMapper(platformGridMapper); @@ -133,39 +152,46 @@ void VTKProcessEngine::addGridActor(VTKRenderItem* item) { uint16_t gridSize = 10; vtkSmartPointer gridPoints = vtkSmartPointer::New(); - vtkSmartPointer gridCells = vtkSmartPointer::New(); + vtkSmartPointer gridCells = + vtkSmartPointer::New(); - for (int16_t i = -platformWidth / 2; i <= platformWidth / 2; i += gridSize) - { - createLine(i, -platformDepth / 2, gridBottomHeight, i, platformDepth / 2, gridBottomHeight, gridPoints, gridCells); + for (int16_t i = -platformWidth / 2; i <= platformWidth / 2; + i += gridSize) { + createLine(i, -platformDepth / 2, gridBottomHeight, i, + platformDepth / 2, gridBottomHeight, gridPoints, gridCells); } - for (int16_t i = -platformDepth / 2; i <= platformDepth / 2; i += gridSize) - { - createLine(-platformWidth / 2, i, gridBottomHeight, platformWidth / 2, i, gridBottomHeight, gridPoints, gridCells); + for (int16_t i = -platformDepth / 2; i <= platformDepth / 2; + i += gridSize) { + createLine(-platformWidth / 2, i, gridBottomHeight, platformWidth / 2, + i, gridBottomHeight, gridPoints, gridCells); } platformGrid->SetPoints(gridPoints); platformGrid->SetLines(gridCells); - item->renderer()->AddActor(gridActor_); } -void VTKProcessEngine::initRenderWindow(VTKRenderItem* item, const bool isPostProcess) { +void VTKProcessEngine::initRenderWindow(VTKRenderItem *item, + const bool isPostProcess) { vtkNew style; style->SetDefaultRenderer(item->renderer()); style->bindRenderItem(item); style->bindVtkProcessEngine(this); - isPostProcess ? style->bindCloudActor(processCloud_) : style->bindCloudActor(cloud_); + isPostProcess ? style->bindCloudActor(processCloud_) + : style->bindCloudActor(cloud_); vtkNew areaPicker; - item->renderWindow()->renderWindow()->GetInteractor()->SetPicker(areaPicker); - item->renderWindow()->renderWindow()->GetInteractor()->SetInteractorStyle(style); + item->renderWindow()->renderWindow()->GetInteractor()->SetPicker( + areaPicker); + item->renderWindow()->renderWindow()->GetInteractor()->SetInteractorStyle( + style); } -void VTKProcessEngine::initRenderItem(VTKRenderItem* item, const bool isPostProcess) { +void VTKProcessEngine::initRenderItem(VTKRenderItem *item, + const bool isPostProcess) { initRenderWindow(item, isPostProcess); initOrientedWidget(item, isPostProcess); addAxesActorAndWidget(item); @@ -176,56 +202,60 @@ void VTKProcessEngine::initRenderItem(VTKRenderItem* item, const bool isPostProc double camPositionZ = 400; item->renderer()->SetBackgroundAlpha(1); - item->renderer()->GetActiveCamera()->SetPosition(camPositionX, camPositionY, camPositionZ); + item->renderer()->GetActiveCamera()->SetPosition(camPositionX, camPositionY, + camPositionZ); item->renderer()->GetActiveCamera()->SetViewUp(0.0, 0.0, 1.0); item->renderer()->GetActiveCamera()->SetFocalPoint(0.0, 0.0, 0.0); item->renderer()->GetActiveCamera()->SetClippingRange(0.01, 100000); - isPostProcess ? item->renderer()->AddActor(processCloud_) : item->renderer()->AddActor(cloud_); - isPostProcess ? item->renderer()->AddActor(processmesh_) : item->renderer()->AddActor(mesh_); - isPostProcess ? item->renderer()->AddActor(processScalarBar_) : item->renderer()->AddActor(scalarBar_); + isPostProcess ? item->renderer()->AddActor(processCloud_) + : item->renderer()->AddActor(cloud_); + isPostProcess ? item->renderer()->AddActor(processmesh_) + : item->renderer()->AddActor(mesh_); + isPostProcess ? item->renderer()->AddActor(processScalarBar_) + : item->renderer()->AddActor(scalarBar_); item->renderer()->ResetCamera(); item->renderer()->DrawOn(); - item->pushCommandToQueue([=]{ - item->update(); - }); + item->pushCommandToQueue([=] { item->update(); }); } -void VTKProcessEngine::bindScanRenderItem(VTKRenderItem* item) { +void VTKProcessEngine::bindScanRenderItem(VTKRenderItem *item) { scanRenderItem_ = item; initRenderItem(scanRenderItem_, false); timer_.reset(new QTimer(item)); timer_->setInterval(1); - connect(timer_.get(), &QTimer::timeout, this, &VTKProcessEngine::renderCloud); + connect(timer_.get(), &QTimer::timeout, this, + &VTKProcessEngine::renderCloud); timer_->start(); } -void VTKProcessEngine::bindPostProcessRenderItem(VTKRenderItem* item) { +void VTKProcessEngine::bindPostProcessRenderItem(VTKRenderItem *item) { postProcessRenderItem_ = item; initRenderItem(postProcessRenderItem_, true); } void VTKProcessEngine::setBackgroundColor(QColor color) { - if(scanRenderItem_) { - scanRenderItem_->renderer()->SetBackground(color.redF(), color.greenF(), color.blueF()); + if (scanRenderItem_) { + scanRenderItem_->renderer()->SetBackground(color.redF(), color.greenF(), + color.blueF()); scanRenderItem_->update(); } - if(postProcessRenderItem_) { - postProcessRenderItem_->renderer()->SetBackground(color.redF(), color.greenF(), color.blueF()); + if (postProcessRenderItem_) { + postProcessRenderItem_->renderer()->SetBackground( + color.redF(), color.greenF(), color.blueF()); postProcessRenderItem_->update(); } } void VTKProcessEngine::enableAxes(const bool isEnable) { curItem_->pushCommandToQueue([=] { - if(isEnable) { + if (isEnable) { axesActor_->VisibilityOn(); - } - else { + } else { axesActor_->VisibilityOff(); } @@ -235,10 +265,9 @@ void VTKProcessEngine::enableAxes(const bool isEnable) { void VTKProcessEngine::enableGrid(const bool isEnable) { curItem_->pushCommandToQueue([=] { - if(isEnable) { + if (isEnable) { gridActor_->VisibilityOn(); - } - else { + } else { gridActor_->VisibilityOff(); } @@ -248,10 +277,9 @@ void VTKProcessEngine::enableGrid(const bool isEnable) { void VTKProcessEngine::enableOriention(const bool isEnable) { curItem_->pushCommandToQueue([=] { - if(isEnable) { + if (isEnable) { orientationWidget_->On(); - } - else { + } else { orientationWidget_->Off(); } @@ -259,8 +287,11 @@ void VTKProcessEngine::enableOriention(const bool isEnable) { }); } -VTKProcessEngine::VTKProcessEngine() : pointSize_(0), postProcessPointSize_(0), vtkExit_(false), postProcessRenderItem_(nullptr), scanRenderItem_(nullptr) { - passCloudToMapperThread_ = std::thread(&VTKProcessEngine::passCloudToMapper, this); +VTKProcessEngine::VTKProcessEngine() + : pointSize_(0), postProcessPointSize_(0), vtkExit_(false), + postProcessRenderItem_(nullptr), scanRenderItem_(nullptr) { + passCloudToMapperThread_ = + std::thread(&VTKProcessEngine::passCloudToMapper, this); cloud_->GetProperty()->SetRepresentationToPoints(); cloud_->GetProperty()->SetPointSize(1); @@ -270,7 +301,8 @@ VTKProcessEngine::VTKProcessEngine() : pointSize_(0), postProcessPointSize_(0), scalarBar_->SetTitle("Z(mm)"); scalarBar_->SetNumberOfLabels(20); scalarBar_->DrawAnnotationsOn(); - scalarBar_->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport(); + scalarBar_->GetPositionCoordinate() + ->SetCoordinateSystemToNormalizedViewport(); scalarBar_->GetPositionCoordinate()->SetValue(0.95f, 0.02f); scalarBar_->SetWidth(0.04); scalarBar_->SetHeight(0.5); @@ -285,7 +317,8 @@ VTKProcessEngine::VTKProcessEngine() : pointSize_(0), postProcessPointSize_(0), processScalarBar_->SetTitle("Z(mm)"); processScalarBar_->SetNumberOfLabels(20); processScalarBar_->DrawAnnotationsOn(); - processScalarBar_->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport(); + processScalarBar_->GetPositionCoordinate() + ->SetCoordinateSystemToNormalizedViewport(); processScalarBar_->GetPositionCoordinate()->SetValue(0.95f, 0.02f); processScalarBar_->SetWidth(0.04); processScalarBar_->SetHeight(0.5); @@ -304,16 +337,16 @@ VTKProcessEngine::VTKProcessEngine() : pointSize_(0), postProcessPointSize_(0), VTKProcessEngine::~VTKProcessEngine() { vtkExit_.store(true, std::memory_order_release); - if(passCloudToMapperThread_.joinable()) { + if (passCloudToMapperThread_.joinable()) { passCloudToMapperThread_.join(); } } void VTKProcessEngine::passCloudToMapper() { - while(!vtkExit_.load(std::memory_order_acquire)) { + while (!vtkExit_.load(std::memory_order_acquire)) { pcl::PointCloud::Ptr cloud; - if(!waitRenderedClouds_.try_pop(cloud)) { + if (!waitRenderedClouds_.try_pop(cloud)) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } @@ -333,10 +366,13 @@ void VTKProcessEngine::passCloudToMapper() { vtkNew scalars; scalars->SetName("colorTable"); scalars->SetNumberOfTuples(pointSize); - for (size_t i = 0; i< pointSize; ++i) { + for (size_t i = 0; i < pointSize; ++i) { vtkIdType pid[1]; - pid[0] = points->InsertNextPoint(cloud->points[i].x, cloud->points[i].y, cloud->points[i].z); - lookup->SetTableValue(i, cloud->points[i].r / 255.f, cloud->points[i].g / 255.f, cloud->points[i].b / 255.f, 1); + pid[0] = points->InsertNextPoint( + cloud->points[i].x, cloud->points[i].y, cloud->points[i].z); + lookup->SetTableValue(i, cloud->points[i].r / 255.f, + cloud->points[i].g / 255.f, + cloud->points[i].b / 255.f, 1); polyVertex->GetPointIds()->SetId(i, i); scalars->InsertValue(i, i); } @@ -344,7 +380,8 @@ void VTKProcessEngine::passCloudToMapper() { grid->Allocate(1, 1); grid->SetPoints(points); grid->GetPointData()->SetScalars(scalars); - grid->InsertNextCell(polyVertex->GetCellType(), polyVertex->GetPointIds()); + grid->InsertNextCell(polyVertex->GetCellType(), + polyVertex->GetPointIds()); vtkNew mapper; mapper->SetInputData(grid); @@ -356,7 +393,8 @@ void VTKProcessEngine::passCloudToMapper() { } } -void VTKProcessEngine::emplaceRenderCloud(const pcl::PointCloud::Ptr cloud) { +void VTKProcessEngine::emplaceRenderCloud( + const pcl::PointCloud::Ptr cloud) { waitRenderedClouds_.push(cloud); std::cout << "Cloud Size: " << waitRenderedClouds_.size() << std::endl; @@ -365,35 +403,39 @@ void VTKProcessEngine::emplaceRenderCloud(const pcl::PointCloud mapper; - if(!waitRenderedMappers_.try_pop(mapper)) { + if (!waitRenderedMappers_.try_pop(mapper)) { return; } - if(curItem_ == scanRenderItem_) { - pointSize_ = mapper->GetInput()->GetNumberOfPoints();; + if (curItem_ == scanRenderItem_) { + pointSize_ = mapper->GetInput()->GetNumberOfPoints(); + ; emit pointSizeChanged(); - } - else { + } else { postProcessPointSize_ = mapper->GetInput()->GetNumberOfPoints(); emit postProcessPointSizeChanged(); } std::cout << "Mapper Size: " << waitRenderedMappers_.size() << std::endl; - //在渲染流程中进行操作,否则易出现内存泄露或上下文获取失败 + // 在渲染流程中进行操作,否则易出现内存泄露或上下文获取失败 curItem_->pushCommandToQueue([&, mapper] { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - vtkSmartPointer curScalarBar = curItem_ == postProcessRenderItem_ ? processScalarBar_ : scalarBar_; + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; + vtkSmartPointer curScalarBar = + curItem_ == postProcessRenderItem_ ? processScalarBar_ : scalarBar_; curCloud->SetMapper(mapper); vtkNew zLookUpTable; zLookUpTable->SetNumberOfTableValues(10); zLookUpTable->SetHueRange(0.67, 0.0); - zLookUpTable->SetTableRange(curCloud->GetZRange()[0], curCloud->GetZRange()[1]); + zLookUpTable->SetTableRange(curCloud->GetZRange()[0], + curCloud->GetZRange()[1]); zLookUpTable->Build(); curScalarBar->SetLookupTable(zLookUpTable); - vtkSmartPointer curScalarBarActor = curItem_ == postProcessRenderItem_ ? processScalarBar_ : scalarBar_; - if(curScalarBarActor->GetVisibility()) { + vtkSmartPointer curScalarBarActor = + curItem_ == postProcessRenderItem_ ? processScalarBar_ : scalarBar_; + if (curScalarBarActor->GetVisibility()) { jetDepthColorMap(); } @@ -401,247 +443,296 @@ void VTKProcessEngine::renderCloud() { }); } -void VTKProcessEngine::setCurRenderItem(VTKRenderItem* item) { +void VTKProcessEngine::setCurRenderItem(VTKRenderItem *item) { curItem_ = item; } void VTKProcessEngine::enableColorBar(const bool isEnable) { - vtkSmartPointer curScalarBarActor = curItem_ == postProcessRenderItem_ ? processScalarBar_ : scalarBar_; - isEnable ? curScalarBarActor->VisibilityOn() : curScalarBarActor->VisibilityOff(); + vtkSmartPointer curScalarBarActor = + curItem_ == postProcessRenderItem_ ? processScalarBar_ : scalarBar_; + isEnable ? curScalarBarActor->VisibilityOn() + : curScalarBarActor->VisibilityOff(); } void VTKProcessEngine::enableAreaSelected(const bool isEnable) { - auto style = static_cast(curItem_->renderWindow()->renderWindow()->GetInteractor()->GetInteractorStyle()); - isEnable ? style->StartSelect() : style->stopSelect(); + auto style = static_cast( + curItem_->renderWindow() + ->renderWindow() + ->GetInteractor() + ->GetInteractorStyle()); + isEnable ? style->StartSelect() : style->stopSelect(); } -void VTKProcessEngine::updateSelectedRec() { - emit paintRectangle(); -} +void VTKProcessEngine::updateSelectedRec() { emit paintRectangle(); } void VTKProcessEngine::saveCloud(const QString path) { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - if(!curCloud->GetMapper()) { - return ; - } - - if(asyncThread_.joinable()) { - asyncThread_.join(); - } - - asyncThread_ = std::thread([&, path, curCloud] { - double progressVal; - - std::thread saveThread = std::thread([&] { - vtkNew surfaceFilter; - surfaceFilter->SetInputData(curCloud->GetMapper()->GetInput()); - surfaceFilter->Update(); - - auto lookup = vtkLookupTable::SafeDownCast(curCloud->GetMapper()->GetLookupTable()); - - vtkNew writer; - writer->SetFileName(path.mid(8).toLocal8Bit().toStdString().c_str()); - writer->SetInputData(surfaceFilter->GetOutput()); - writer->SetLookupTable(lookup); - writer->SetArrayName("colorTable"); - writer->SetFileTypeToASCII(); - - vtkNew progressCommand; - progressCommand->SetCallback(progressValCallbackFunc); - progressCommand->SetClientData(&progressVal); - writer->AddObserver(vtkCommand::ProgressEvent, progressCommand); + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; + if (!curCloud->GetMapper()) { + return; + } - writer->Update(); - }); + if (asyncThread_.joinable()) { + asyncThread_.join(); + } - while(progressVal < 1) { - progressVal_ = progressVal; - emit progressValChanged(); - std::this_thread::sleep_for(std::chrono::milliseconds(300)); - } + asyncThread_ = std::thread([&, path, curCloud] { + double progressVal; + + std::thread saveThread = std::thread([&] { + vtkNew surfaceFilter; + surfaceFilter->SetInputData(curCloud->GetMapper()->GetInput()); + surfaceFilter->Update(); + + auto lookup = vtkLookupTable::SafeDownCast( + curCloud->GetMapper()->GetLookupTable()); + + vtkNew writer; + writer->SetFileName( + path.mid(8).toLocal8Bit().toStdString().c_str()); + writer->SetInputData(surfaceFilter->GetOutput()); + writer->SetLookupTable(lookup); + writer->SetArrayName("colorTable"); + writer->SetFileTypeToASCII(); + + vtkNew progressCommand; + progressCommand->SetCallback(progressValCallbackFunc); + progressCommand->SetClientData(&progressVal); + writer->AddObserver(vtkCommand::ProgressEvent, progressCommand); + + writer->Update(); + }); + + while (progressVal < 1) { + progressVal_ = progressVal; + emit progressValChanged(); + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + } - progressVal_ = 1; - emit progressValChanged(); + progressVal_ = 1; + emit progressValChanged(); - if(saveThread.joinable()) { - saveThread.join(); - } - }); + if (saveThread.joinable()) { + saveThread.join(); + } + }); } void VTKProcessEngine::clip(const bool isClipInner) { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - - if(!curCloud->GetMapper()) { - cancelClip(); - return; - } - - if(asyncThread_.joinable()) { - asyncThread_.join(); - } + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - asyncThread_ = std::thread([&, isClipInner] { - double progressVal = 0; - std::thread clipThread = std::thread([&] { - auto style = static_cast(curItem_->renderWindow()->renderWindow()->GetInteractor()->GetInteractorStyle()); - style->clip(isClipInner, progressVal); - }); + if (!curCloud->GetMapper()) { + cancelClip(); + return; + } - while(progressVal < 1) { - progressVal_ = progressVal; - emit progressValChanged(); + if (asyncThread_.joinable()) { + asyncThread_.join(); + } - std::this_thread::sleep_for(std::chrono::milliseconds(300)); - } + asyncThread_ = std::thread([&, isClipInner] { + double progressVal = 0; + std::thread clipThread = std::thread([&] { + auto style = static_cast( + curItem_->renderWindow() + ->renderWindow() + ->GetInteractor() + ->GetInteractorStyle()); + style->clip(isClipInner, progressVal); + }); + + while (progressVal < 1) { + progressVal_ = progressVal; + emit progressValChanged(); + + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + } - vtkNew surfaceFilter; - surfaceFilter->SetInputData(cloud_->GetMapper()->GetInput()); - surfaceFilter->Update(); + vtkNew surfaceFilter; + surfaceFilter->SetInputData(cloud_->GetMapper()->GetInput()); + surfaceFilter->Update(); - vtkLookupTable::SafeDownCast(scalarBar_->GetLookupTable())->SetTableRange(surfaceFilter->GetOutput()->GetBounds()[4], surfaceFilter->GetOutput()->GetBounds()[5]); + vtkLookupTable::SafeDownCast(scalarBar_->GetLookupTable()) + ->SetTableRange(surfaceFilter->GetOutput()->GetBounds()[4], + surfaceFilter->GetOutput()->GetBounds()[5]); - progressVal_ = 1; - emit progressValChanged(); + progressVal_ = 1; + emit progressValChanged(); - if(clipThread.joinable()) { - clipThread.join(); - } - }); + if (clipThread.joinable()) { + clipThread.join(); + } + }); } void VTKProcessEngine::cancelClip() { - auto style = static_cast(curItem_->renderWindow()->renderWindow()->GetInteractor()->GetInteractorStyle()); + auto style = static_cast( + curItem_->renderWindow() + ->renderWindow() + ->GetInteractor() + ->GetInteractorStyle()); style->cancelClip(); } void VTKProcessEngine::enablePointInfo(const bool isEnable) { - auto style = static_cast(curItem_->renderWindow()->renderWindow()->GetInteractor()->GetInteractorStyle()); - style->enablePointInfoMode(isEnable); + auto style = static_cast( + curItem_->renderWindow() + ->renderWindow() + ->GetInteractor() + ->GetInteractorStyle()); + style->enablePointInfoMode(isEnable); } void VTKProcessEngine::release() { - if(asyncThread_.joinable()) { + if (asyncThread_.joinable()) { asyncThread_.join(); } } void VTKProcessEngine::colorizeCloud(QColor color) { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - if(!curCloud->GetMapper()) { - return; - } + if (!curCloud->GetMapper()) { + return; + } - auto r = color.redF(); - auto g = color.greenF(); - auto b = color.blueF(); - curCloud->GetProperty()->SetColor(r, g, b); + auto r = color.redF(); + auto g = color.greenF(); + auto b = color.blueF(); + curCloud->GetProperty()->SetColor(r, g, b); - curCloud->GetMapper()->SetScalarVisibility(false); + curCloud->GetMapper()->SetScalarVisibility(false); - curItem_->update(); + curItem_->update(); } void VTKProcessEngine::cancelColorizeCloud() { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - if(!curCloud->GetMapper()) { - return; - } + if (!curCloud->GetMapper()) { + return; + } - curCloud->GetMapper()->SetScalarVisibility(true); - auto lookUp = static_cast(curCloud->GetMapper()->GetLookupTable()); - lookUp->DeepCopy(lookupPre); + curCloud->GetMapper()->SetScalarVisibility(true); + auto lookUp = + static_cast(curCloud->GetMapper()->GetLookupTable()); + lookUp->DeepCopy(lookupPre); - curItem_->update(); + curItem_->update(); } void VTKProcessEngine::jetDepthColorMap() { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - - if(!curCloud->GetMapper()) { - return; - } + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - vtkNew surfaceFilter; - surfaceFilter->SetInputData(curCloud->GetMapper()->GetInput()); - surfaceFilter->Update(); - - double zMin = surfaceFilter->GetOutput()->GetBounds()[4]; - double zMax = surfaceFilter->GetOutput()->GetBounds()[5]; - - auto points = vtkUnstructuredGrid::SafeDownCast(curCloud->GetMapper()->GetInput())->GetPoints(); - vtkNew jetMapLookup; - jetMapLookup->SetHueRange(0.67, 0.0); - jetMapLookup->SetTableRange(zMin, zMax); - jetMapLookup->Build(); - - auto lookUp = static_cast(curCloud->GetMapper()->GetLookupTable()); - lookupPre->DeepCopy(lookUp); - - auto scalars = vtkFloatArray::SafeDownCast(vtkUnstructuredGrid::SafeDownCast(vtkDataSetMapper::SafeDownCast(curCloud->GetMapper())->GetInput())->GetPointData()->GetScalars()); + if (!curCloud->GetMapper()) { + return; + } - for (size_t i = 0; i < scalars->GetNumberOfTuples(); ++i) { - double tabColor[3]; - jetMapLookup->GetColor(points->GetPoint(i)[2], tabColor); - lookUp->SetTableValue(scalars->GetValue(i), tabColor[0], tabColor[1], tabColor[2]); - } + vtkNew surfaceFilter; + surfaceFilter->SetInputData(curCloud->GetMapper()->GetInput()); + surfaceFilter->Update(); + + double zMin = surfaceFilter->GetOutput()->GetBounds()[4]; + double zMax = surfaceFilter->GetOutput()->GetBounds()[5]; + + auto points = + vtkUnstructuredGrid::SafeDownCast(curCloud->GetMapper()->GetInput()) + ->GetPoints(); + vtkNew jetMapLookup; + jetMapLookup->SetHueRange(0.67, 0.0); + jetMapLookup->SetTableRange(zMin, zMax); + jetMapLookup->Build(); + + auto lookUp = + static_cast(curCloud->GetMapper()->GetLookupTable()); + lookupPre->DeepCopy(lookUp); + + auto scalars = vtkFloatArray::SafeDownCast( + vtkUnstructuredGrid::SafeDownCast( + vtkDataSetMapper::SafeDownCast(curCloud->GetMapper())->GetInput()) + ->GetPointData() + ->GetScalars()); + + for (size_t i = 0; i < scalars->GetNumberOfTuples(); ++i) { + double tabColor[3]; + jetMapLookup->GetColor(points->GetPoint(i)[2], tabColor); + lookUp->SetTableValue(scalars->GetValue(i), tabColor[0], tabColor[1], + tabColor[2]); + } - curCloud->GetMapper()->SetScalarVisibility(true); - curCloud->GetProperty()->SetRepresentationToPoints(); + curCloud->GetMapper()->SetScalarVisibility(true); + curCloud->GetProperty()->SetRepresentationToPoints(); - curItem_->update(); + curItem_->update(); } -void VTKProcessEngine::setCameraViewPort(double x, double y, double z, double fx, double fy, double fz, double vx, double vy, double vz) { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - vtkNew camera; - - if(curCloud->GetMapper()) { - vtkNew surfaceFilter; - surfaceFilter->SetInputData(curCloud->GetMapper()->GetInput()); - surfaceFilter->Update(); - - double bounds[6], center[3]; - surfaceFilter->GetOutput()->GetBounds(bounds); - surfaceFilter->GetOutput()->GetCenter(center); - - if(x != 0) { - x < 0 ? camera->SetPosition(bounds[0] + x, center[1], center[2]) : camera->SetPosition(bounds[1] + x, center[1], center[2]); - x < 0 ? camera->SetFocalPoint(bounds[0] + x + 100, center[1], center[2]) : camera->SetFocalPoint(bounds[1] + x - 100, center[1], center[2]); - } - else if(y != 0) { - y < 0 ? camera->SetPosition(center[0], bounds[2] + y, center[2]) : camera->SetPosition(center[0], bounds[3] + y, center[2]); - y < 0 ? camera->SetFocalPoint(center[0], bounds[2] + y + 100, center[2]) : camera->SetFocalPoint(center[0], bounds[3] + y - 100, center[2]); - } - else if(z != 0) { - z < 0 ? camera->SetPosition(center[0], center[1], bounds[4] + z) : camera->SetPosition(center[0], center[1], bounds[5] + z); - z < 0 ? camera->SetFocalPoint(center[0], center[1], bounds[4] + z + 100) : camera->SetFocalPoint(center[0], center[1], bounds[5] + z - 100); - } - } - else { - camera->SetFocalPoint(fx, fy, fz); - camera->SetPosition(x, y, z); - } +void VTKProcessEngine::setCameraViewPort(double x, double y, double z, + double fx, double fy, double fz, + double vx, double vy, double vz) { + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; + vtkNew camera; + + if (curCloud->GetMapper()) { + vtkNew surfaceFilter; + surfaceFilter->SetInputData(curCloud->GetMapper()->GetInput()); + surfaceFilter->Update(); + + double bounds[6], center[3]; + surfaceFilter->GetOutput()->GetBounds(bounds); + surfaceFilter->GetOutput()->GetCenter(center); + + if (x != 0) { + x < 0 ? camera->SetPosition(bounds[0] + x, center[1], center[2]) + : camera->SetPosition(bounds[1] + x, center[1], center[2]); + x < 0 ? camera->SetFocalPoint(bounds[0] + x + 100, center[1], + center[2]) + : camera->SetFocalPoint(bounds[1] + x - 100, center[1], + center[2]); + } else if (y != 0) { + y < 0 ? camera->SetPosition(center[0], bounds[2] + y, center[2]) + : camera->SetPosition(center[0], bounds[3] + y, center[2]); + y < 0 ? camera->SetFocalPoint(center[0], bounds[2] + y + 100, + center[2]) + : camera->SetFocalPoint(center[0], bounds[3] + y - 100, + center[2]); + } else if (z != 0) { + z < 0 ? camera->SetPosition(center[0], center[1], bounds[4] + z) + : camera->SetPosition(center[0], center[1], bounds[5] + z); + z < 0 ? camera->SetFocalPoint(center[0], center[1], + bounds[4] + z + 100) + : camera->SetFocalPoint(center[0], center[1], + bounds[5] + z - 100); + } + } else { + camera->SetFocalPoint(fx, fy, fz); + camera->SetPosition(x, y, z); + } - camera->SetViewUp(vx, vy, vz); - curItem_->renderer()->SetActiveCamera(camera); + camera->SetViewUp(vx, vy, vz); + curItem_->renderer()->SetActiveCamera(camera); - curItem_->update(); + curItem_->update(); } -void VTKProcessEngine::getCameraViewPort(double& x, double& y, double& z, double& fx, double& fy, double& fz, double& vx, double& vy, double& vz) { - auto camera = curItem_->renderer()->GetActiveCamera(); - camera->GetViewUp(vx, vy, vz); - camera->GetPosition(x, y, z); - camera->GetFocalPoint(fx, fy, fz); +void VTKProcessEngine::getCameraViewPort(double &x, double &y, double &z, + double &fx, double &fy, double &fz, + double &vx, double &vy, double &vz) { + auto camera = curItem_->renderer()->GetActiveCamera(); + camera->GetViewUp(vx, vy, vz); + camera->GetPosition(x, y, z); + camera->GetFocalPoint(fx, fy, fz); } void VTKProcessEngine::enableMesh(const bool isEnable) { - vtkSmartPointer curMesh = curItem_ == postProcessRenderItem_ ? processmesh_ : mesh_; + vtkSmartPointer curMesh = + curItem_ == postProcessRenderItem_ ? processmesh_ : mesh_; - if(!curMesh->GetMapper()) { + if (!curMesh->GetMapper()) { return; } @@ -651,9 +742,10 @@ void VTKProcessEngine::enableMesh(const bool isEnable) { } void VTKProcessEngine::enableCloud(const bool isEnable) { - vtkSmartPointer curCloud = curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; + vtkSmartPointer curCloud = + curItem_ == postProcessRenderItem_ ? processCloud_ : cloud_; - if(!curCloud->GetMapper()) { + if (!curCloud->GetMapper()) { return; } @@ -670,17 +762,18 @@ void VTKProcessEngine::updateProcessActor(vtkSmartPointer actor) { enableCloud(true); enableMesh(true); - if(actor) { + if (actor) { processActor_->ShallowCopy(actor); curItem_->renderer()->AddActor(processActor_); curItem_->update(); } } -void VTKProcessEngine::updateProcessCloud(pcl::PointCloud::Ptr cloud) { +void VTKProcessEngine::updateProcessCloud( + pcl::PointCloud::Ptr cloud) { curItem_ = postProcessRenderItem_; - //如果点云不为空,则跳转至结果页面 - if(!cloud->empty()) { + // 如果点云不为空,则跳转至结果页面 + if (!cloud->empty()) { emit postProcessOutput(); } @@ -693,7 +786,7 @@ void VTKProcessEngine::updateProcessCloud(pcl::PointCloud::Ptr void VTKProcessEngine::updateProcessMesh(pcl::PolygonMesh::Ptr mesh) { curItem_ = postProcessRenderItem_; - if(!mesh->polygons.empty()) { + if (!mesh->polygons.empty()) { emit postProcessOutput(); } @@ -703,7 +796,8 @@ void VTKProcessEngine::updateProcessMesh(pcl::PolygonMesh::Ptr mesh) { vtkNew meshMapper; meshMapper->SetInputData(meshVtk); - vtkSmartPointer curMesh = curItem_ == postProcessRenderItem_ ? processmesh_ : mesh_; + vtkSmartPointer curMesh = + curItem_ == postProcessRenderItem_ ? processmesh_ : mesh_; curMesh->SetMapper(meshMapper); curMesh->GetProperty()->SetRepresentationToSurface(); diff --git a/src/cameras/binoocularCamer.cpp b/src/cameras/binoocularCamer.cpp index 295266a..9079b65 100644 --- a/src/cameras/binoocularCamer.cpp +++ b/src/cameras/binoocularCamer.cpp @@ -293,18 +293,18 @@ void BinocularCamera::decode(const std::vector> &imgs, frameData.textureMap_ = cv::Mat::zeros(imgs[index][0].size(), imgs[index][0].type()); - const int shiftTime = static_cast(numbericalProperties_["Phase Shift Times"]); + const int shiftTime = + static_cast(numbericalProperties_["Phase Shift Times"]); for (int i = 0; i < shiftTime; ++i) { - frameData.textureMap_ += - (imgs[index][i] / shiftTime); + frameData.textureMap_ += (imgs[index][i] / shiftTime); } if (frameData.textureMap_.type() == CV_8UC1) { cv::cvtColor(frameData.textureMap_, frameData.textureMap_, cv::COLOR_GRAY2BGR); } - cv::Mat disparityMap, textureMapped; + cv::Mat disparityMap; std::vector> remapedImgs( 2, std::vector(imgs[0].size())); @@ -316,10 +316,6 @@ void BinocularCamera::decode(const std::vector> &imgs, cv::remap(imgs[1][i], remapedImgs[1][i], mapRX_, mapRY_, cv::INTER_LINEAR); } - if (range.start == 0 && index == 0) { - cv::remap(frameData.textureMap_, textureMapped, - mapLX_, mapLY_, cv::INTER_LINEAR); - } }); pattern_->decode(remapedImgs, disparityMap, @@ -330,7 +326,7 @@ void BinocularCamera::decode(const std::vector> &imgs, cv::bilateralFilter(operateMap, disparityMap, 15, 20, 50); } - fromDispairtyMapToCloud(disparityMap, textureMapped, *caliInfo_, + fromDispairtyMapToCloud(disparityMap, frameData.textureMap_, *caliInfo_, *frameData.pointCloud_, frameData.depthMap_, index == 2); } @@ -659,8 +655,7 @@ bool BinocularCamera::burnPatterns(const std::vector &imgs) { cv::Size imgSize = cv::Size(projectorInfo.width_, projectorInfo.height_); const int numOfPatternOrderSets = std::ceil(imgs.size() / 6.f); - std::vector patternSets( - numOfPatternOrderSets); + std::vector patternSets(numOfPatternOrderSets); for (size_t i = 0; i < patternSets.size(); ++i) { patternSets[i].exposureTime_ = numbericalProperties_["Exposure Time"]; patternSets[i].preExposureTime_ = diff --git a/src/cameras/binosSinusCompleGrayCodePattern.cpp b/src/cameras/binosSinusCompleGrayCodePattern.cpp index abd8e97..ae94cae 100644 --- a/src/cameras/binosSinusCompleGrayCodePattern.cpp +++ b/src/cameras/binosSinusCompleGrayCodePattern.cpp @@ -74,8 +74,6 @@ bool BinoSinusCompleGrayCodePattern::decode( return pattern->decode( patternImages, disparityMap, cv::noArray(), cv::noArray(), algorithm::SINUSOIDAL_COMPLEMENTARY_GRAY_CODE); - - return true; } } // namespace cameras } // namespace slmaster \ No newline at end of file diff --git a/src/cameras/tool.cpp b/src/cameras/tool.cpp index 803e1c2..ac01cd9 100644 --- a/src/cameras/tool.cpp +++ b/src/cameras/tool.cpp @@ -24,7 +24,7 @@ void fromDispairtyMapToCloud(const cv::Mat &disparityMap, Eigen::Matrix3f R1InvEigen; cv::cv2eigen(R1InvCV, R1InvEigen); - Eigen::Matrix3f R13Eigen, M3Eigen; + Eigen::Matrix3f R13Eigen, M3Eigen, M1Eigen; Eigen::Vector3f T13Eigen; if (isRemapColorCam) { cv::Mat R13CV = caliInfo.info_.Rlc_; @@ -36,21 +36,22 @@ void fromDispairtyMapToCloud(const cv::Mat &disparityMap, cv::cv2eigen(T13CV, T13Eigen); cv::Mat M3CV = caliInfo.info_.M3_; - T13CV.convertTo(T13CV, CV_32FC1); - cv::cv2eigen(T13CV, T13Eigen); + M3CV.convertTo(M3CV, CV_32FC1); + cv::cv2eigen(M3CV, M3Eigen); } + cv::Mat M1CV = caliInfo.info_.M1_; + M1CV.convertTo(M1CV, CV_32FC1); + cv::cv2eigen(M1CV, M1Eigen); + cloud.clear(); - textureCamDepthMap = - cv::Mat::zeros(disparityMap.size(), disparityMap.type()); + textureCamDepthMap = cv::Mat::zeros(textureMap.size(), disparityMap.type()); std::mutex mutex; cv::parallel_for_(cv::Range(0, rows), [&](const cv::Range &range) { for (int i = range.start; i < range.end; ++i) { auto disparityMapPtr = disparityMap.ptr(i); - auto textureCamDepthMapPtr = textureCamDepthMap.ptr(i); - auto textureMapPtr = textureMap.ptr(i); for (int j = 0; j < cols; ++j) { if (std::abs(disparityMapPtr[j]) < 0.001f) { continue; @@ -64,37 +65,30 @@ void fromDispairtyMapToCloud(const cv::Mat &disparityMap, point(2, 0) = -1.f * tx * f / (disparityMapPtr[j] - cxlr); point = R1InvEigen * point; - - pcl::PointXYZRGB cloudPoint(point(0, 0), point(1, 0), - point(2, 0), 255, 255, 255); + Eigen::Vector3f imgPoint; if (isRemapColorCam) { - point = M3Eigen * (R13Eigen * point + T13Eigen); - const int xLoc = point(0, 0) / point(2, 0); - const int yLoc = point(1, 0) / point(2, 0); - if (xLoc >= 0 && xLoc < cols && yLoc >= 0 && yLoc < rows) { - auto color = textureMap.ptr(yLoc)[xLoc]; - cloudPoint.r = color[2]; - cloudPoint.g = color[1]; - cloudPoint.b = color[0]; - - { - std::lock_guard guard(mutex); - textureCamDepthMap.ptr(yLoc)[xLoc] = - cloudPoint.z; - } - } + point = R13Eigen * point + T13Eigen; + imgPoint = M3Eigen * point; } else { - cloudPoint.r = textureMapPtr[j][2]; - cloudPoint.g = textureMapPtr[j][1]; - cloudPoint.b = textureMapPtr[j][0]; - - textureCamDepthMapPtr[j] = cloudPoint.z; + imgPoint = M1Eigen * point; } - { - std::lock_guard guard(mutex); - cloud.emplace_back(cloudPoint); + const int xLoc = imgPoint(0, 0) / imgPoint(2, 0); + const int yLoc = imgPoint(1, 0) / imgPoint(2, 0); + + if (xLoc >= 0 && xLoc < cols && yLoc >= 0 && yLoc < rows) { + auto color = textureMap.ptr(yLoc)[xLoc]; + pcl::PointXYZRGB cloudPoint(point(0, 0), point(1, 0), + point(2, 0), color[2], color[1], + color[0]); + + { + std::lock_guard guard(mutex); + textureCamDepthMap.ptr(yLoc)[xLoc] = + cloudPoint.z; + cloud.emplace_back(cloudPoint); + } } } } @@ -116,9 +110,13 @@ void fromDepthMapToCloud(const cv::Mat &depthMap, const cv::Mat &textureMap, cv::Mat M1InvCV = caliInfo.info_.M1_.inv(); M1InvCV.convertTo(M1InvCV, CV_32FC1); - Eigen::Matrix3f M1InvEigen, R13Eigen, M3Eigen; + Eigen::Matrix3f M1InvEigen, R13Eigen, M3Eigen, M1Eigen; cv::cv2eigen(M1InvCV, M1InvEigen); + cv::Mat M1CV = caliInfo.info_.M1_; + M1CV.convertTo(M1CV, CV_32FC1); + cv::cv2eigen(M1CV, M1Eigen); + Eigen::Vector3f T13Eigen; if (isRemapColorCam) { @@ -133,15 +131,14 @@ void fromDepthMapToCloud(const cv::Mat &depthMap, const cv::Mat &textureMap, cloud.clear(); textureCamDepthMap = isRemapColorCam - ? cv::Mat(textureMap.size(), textureMap.type()) - : textureMap.clone(); + ? cv::Mat(textureMap.size(), depthMap.type()) + : depthMap.clone(); std::mutex mutex; cv::parallel_for_(cv::Range(0, rows), [&](const cv::Range &range) { for (int i = range.start; i < range.end; ++i) { auto depthMapPtr = depthMap.ptr(i); - auto textureMapPtr = textureMap.ptr(i); for (int j = 0; j < cols; ++j) { if (std::abs(depthMapPtr[j]) < 0.001f) { continue; @@ -149,36 +146,33 @@ void fromDepthMapToCloud(const cv::Mat &depthMap, const cv::Mat &textureMap, Eigen::Vector3f point = M1InvEigen * Eigen::Vector3f(j, i, 1) * depthMapPtr[j]; - - pcl::PointXYZRGB cloudPoint(point(0, 0), point(1, 0), - point(2, 0), 255, 255, 255); + Eigen::Vector3f imgPoint; if (isRemapColorCam) { - point = M3Eigen * (R13Eigen * point + T13Eigen); - const int xLoc = point(0, 0) / point(2, 0); - const int yLoc = point(1, 0) / point(2, 0); - if (xLoc >= 0 && xLoc < cols - 1 && yLoc >= 0 && - yLoc < rows - 1) { - auto color = textureMap.ptr(yLoc)[xLoc]; - cloudPoint.r = color[2]; - cloudPoint.g = color[1]; - cloudPoint.b = color[0]; - - { - std::lock_guard guard(mutex); + point = R13Eigen * point + T13Eigen; + imgPoint = M3Eigen * point; + } else { + imgPoint = M1Eigen * point; + } + + const int xLoc = imgPoint(0, 0) / imgPoint(2, 0); + const int yLoc = imgPoint(1, 0) / imgPoint(2, 0); + + if (xLoc >= 0 && xLoc < cols - 1 && yLoc >= 0 && + yLoc < rows - 1) { + auto color = textureMap.ptr(yLoc)[xLoc]; + pcl::PointXYZRGB cloudPoint(point(0, 0), point(1, 0), + point(2, 0), color[2], color[1], + color[0]); + + { + std::lock_guard guard(mutex); + cloud.emplace_back(cloudPoint); + if (isRemapColorCam) { textureCamDepthMap.ptr(yLoc)[xLoc] = cloudPoint.z; } } - } else { - cloudPoint.r = textureMapPtr[j][2]; - cloudPoint.g = textureMapPtr[j][1]; - cloudPoint.b = textureMapPtr[j][0]; - } - - { - std::lock_guard guard(mutex); - cloud.emplace_back(cloudPoint); } } }