ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
vtkutils.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "vtkutils.h"
9 
10 #include <QVTKOpenGLNativeWidget.h>
11 #include <vtkActor.h>
12 #include <vtkDelaunay2D.h>
13 #include <vtkDiskSource.h>
14 #include <vtkDoubleArray.h>
15 #include <vtkImageData.h>
16 #include <vtkIntArray.h>
17 #include <vtkPlaneSource.h>
18 #include <vtkPointData.h>
19 #include <vtkPoints.h>
20 #include <vtkPolyData.h>
21 #include <vtkPolyDataMapper.h>
22 #include <vtkQImageToImageSource.h>
23 #include <vtkRenderWindow.h>
24 #include <vtkRenderer.h>
25 #include <vtkSphereSource.h>
26 #include <vtkVRMLExporter.h>
27 
28 #include <QDebug>
29 #include <QThreadPool>
30 
31 #include "actorexporter.h"
32 
33 namespace VtkUtils {
34 
35 QImage vtkImageDataToQImage(vtkImageData* imageData) {
36  if (!imageData) return QImage();
37 
39  int width = imageData->GetDimensions()[0];
40  int height = imageData->GetDimensions()[1];
41  QImage image(width, height, QImage::Format_RGB32);
42  QRgb* rgbPtr = reinterpret_cast<QRgb*>(image.bits()) + width * (height - 1);
43  unsigned char* colorsPtr =
44  reinterpret_cast<unsigned char*>(imageData->GetScalarPointer());
45  // mirror vertically
46  for (int row = 0; row < height; ++row) {
47  for (int col = 0; col < width; ++col) {
48  // Swap rgb
49  *(rgbPtr++) =
50  QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
51  colorsPtr += 3;
52  }
53  rgbPtr -= width * 2;
54  }
55  return image;
56 }
57 
58 void qImageToVtkImage(QImage& img, vtkImageData* imageData) {
59  if (!imageData) {
60  qDebug() << "null image data.";
61  return;
62  }
63 
64  vtkSmartPointer<vtkQImageToImageSource> qimageToImageSource =
66  qimageToImageSource->SetQImage(&img);
67  qimageToImageSource->Update();
68  imageData->DeepCopy(qimageToImageSource->GetOutput());
69 }
70 
71 QImage vtkWidgetSnapshot(QVTKOpenGLNativeWidget* widget) {
72  if (!widget) return QImage();
73  // return vtkImageDataToQImage(widget->cachedImage());
74  return vtkImageDataToQImage(nullptr);
75 }
76 
77 void exportActorToFile(vtkActor* actor, const QString& outfile) {
78  VtkUtils::ActorExporter* exporter =
79  new VtkUtils::ActorExporter(actor, outfile);
80  QThreadPool::globalInstance()->start(exporter);
81 }
82 
83 } // namespace VtkUtils
std::shared_ptr< core::Tensor > image
int width
int height
void exportActorToFile(vtkActor *actor, const QString &outfile)
Definition: vtkutils.cpp:77
QImage vtkImageDataToQImage(vtkImageData *imageData)
Definition: vtkutils.cpp:35
void qImageToVtkImage(QImage &img, vtkImageData *imageData)
Definition: vtkutils.cpp:58
QImage vtkWidgetSnapshot(QVTKOpenGLNativeWidget *widget)
Definition: vtkutils.cpp:71