OGS
VtkVisImageItem.cpp
Go to the documentation of this file.
1
15// ** INCLUDES **
16#include "VtkVisImageItem.h"
17
18#include <vtkActor.h>
19#include <vtkDataArray.h>
20#include <vtkDataSetMapper.h>
21#include <vtkImageAlgorithm.h>
22#include <vtkImageChangeInformation.h>
23#include <vtkImageData.h>
24#include <vtkImageShiftScale.h>
25#include <vtkPointData.h>
26#include <vtkRenderer.h>
27#include <vtkSmartPointer.h>
28
29#include <QFileDialog>
30
32#include "Base/OGSError.h"
33#include "BaseLib/FileTools.h"
34#include "BaseLib/Logging.h"
36#include "GeoLib/Raster.h"
38#include "VtkGeoImageSource.h"
39
40// export
41#include <vtkImageActor.h>
42#include <vtkXMLImageDataWriter.h>
43
45 vtkAlgorithm* algorithm, TreeItem* parentItem,
46 const QList<QVariant> data /*= QList<QVariant>()*/)
47 : VtkVisPipelineItem(algorithm, parentItem, data), _transformFilter(nullptr)
48{
49}
50
52 VtkCompositeFilter* compositeFilter, TreeItem* parentItem,
53 const QList<QVariant> data /*= QList<QVariant>()*/)
54 : VtkVisPipelineItem(compositeFilter, parentItem, data),
55 _transformFilter(nullptr)
56{
57}
58
63
64void VtkVisImageItem::Initialize(vtkRenderer* renderer)
65{
66 auto* img = dynamic_cast<vtkImageAlgorithm*>(_algorithm);
67 img->Update();
68 // VtkGeoImageSource* img = dynamic_cast<VtkGeoImageSource*>(_algorithm);
69
70 double origin[3];
71 double spacing[3];
72 double range[2];
73 img->GetOutput()->GetOrigin(origin);
74 img->GetOutput()->GetSpacing(spacing);
75 // img->getRange(range);
76 img->GetOutput()->GetPointData()->GetScalars()->GetRange(range);
77 vtkImageShiftScale* scale = vtkImageShiftScale::New();
78 scale->SetOutputScalarTypeToUnsignedChar();
79 scale->SetInputConnection(img->GetOutputPort());
80 scale->SetShift(-range[0]);
81 scale->SetScale(255.0 / (range[1] - range[0]));
82
83 _transformFilter = vtkImageChangeInformation::New();
84 _transformFilter->SetInputConnection(scale->GetOutputPort());
85 // double origin[3];
86 // img->getOrigin(origin);
87 // double spacing = img->getSpacing();
88 //_transformFilter->SetOutputOrigin(origin2);
89 //_transformFilter->SetOutputSpacing(spacing2);
90 _transformFilter->Update();
91
92 _renderer = renderer;
93
94 // Use a special vtkImageActor instead of vtkActor
95 vtkImageActor* imageActor = vtkImageActor::New();
96 imageActor->SetInputData(_transformFilter->GetOutput());
97 _actor = imageActor;
98 _renderer->AddActor(_actor);
99
100 // Set pre-set properties
101 auto* vtkProps = dynamic_cast<VtkAlgorithmProperties*>(_algorithm);
102 if (vtkProps)
103 {
104 setVtkProperties(vtkProps);
105 }
106
107 auto* parentItem = dynamic_cast<VtkVisPipelineItem*>(this->parentItem());
108 while (parentItem)
109 {
110 auto* parentProps =
111 dynamic_cast<VtkAlgorithmProperties*>(parentItem->algorithm());
112 if (parentProps)
113 {
114 auto* newProps = new VtkAlgorithmProperties();
115 newProps->SetScalarVisibility(parentProps->GetScalarVisibility());
116 newProps->SetTexture(parentProps->GetTexture());
117 setVtkProperties(newProps);
118 vtkProps = newProps;
119 parentItem = nullptr;
120 }
121 else
122 {
123 parentItem =
124 dynamic_cast<VtkVisPipelineItem*>(parentItem->parentItem());
125 }
126 }
127
128 // Set active scalar to the desired one from VtkAlgorithmProperties
129 // or to match those of the parent.
130 if (vtkProps)
131 {
132 if (vtkProps->GetActiveAttribute().length() > 0)
133 {
134 this->SetActiveAttribute(vtkProps->GetActiveAttribute());
135 }
136 else
137 {
138 auto* visParentItem =
139 dynamic_cast<VtkVisPipelineItem*>(this->parentItem());
140 if (visParentItem)
141 {
142 this->SetActiveAttribute(visParentItem->GetActiveAttribute());
143 }
144 if (vtkProps->GetTexture() != nullptr)
145 {
146 this->SetActiveAttribute("Solid Color");
147 }
148 }
149 }
150}
151
153{
154 // todo implementation
155 (void)vtkProps;
156}
157
158int VtkVisImageItem::callVTKWriter(vtkAlgorithm* algorithm,
159 const std::string& filename) const
160{
161 std::string file_name_cpy(filename);
162 auto* algID = dynamic_cast<vtkImageAlgorithm*>(algorithm);
163 if (algID)
164 {
165 vtkSmartPointer<vtkXMLImageDataWriter> iWriter =
166 vtkSmartPointer<vtkXMLImageDataWriter>::New();
167 iWriter->SetInputData(algID->GetOutputDataObject(0));
168 if (BaseLib::getFileExtension(filename) != ".vti")
169 {
170 file_name_cpy.append(".vti");
171 }
172 iWriter->SetFileName(file_name_cpy.c_str());
173 return iWriter->Write();
174 }
175 ERR("VtkVisPipelineItem::writeToFile() - Unknown data type.");
176 return 0;
177}
178
179void VtkVisImageItem::setTranslation(double x, double y, double z) const
180{
181 _transformFilter->SetOriginTranslation(x, y, z);
182}
183
185{
186 return _transformFilter;
187}
188
190{
191 vtkSmartPointer<VtkGeoImageSource> imageSource =
192 VtkGeoImageSource::SafeDownCast(_algorithm);
193
194 auto const raster = VtkGeoImageSource::convertToRaster(imageSource);
195
196 if (!raster)
197 {
198 OGSError::box("Image could not be converted to a raster.");
199 return false;
200 }
201
202 QFileInfo const info(this->data(0).toString());
203 QString const rastername = info.completeBaseName() + ".asc";
204 QString const filetype("ESRI ASCII raster file (*.asc)");
205 QString const filename = QFileDialog::getSaveFileName(
206 nullptr, "Save raster as",
207 LastSavedFileDirectory::getDir() + rastername, filetype);
210 filename.toStdString());
211 return true;
212}
Definition of the AsciiRasterInterface class.
Filename manipulation routines.
Manages the last directory used for saving a file.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the OGSError class.
Definition of the GeoLib::Raster class.
Definition of the VtkAlgorithmProperties class.
Definition of the VtkGeoImageSource class.
Definition of the VtkVisImageItem class.
static void writeRasterAsASC(GeoLib::Raster const &raster, std::string const &file_name)
Writes an Esri asc-file.
static void setDir(const QString &path)
Sets the directory last used for saving a file.
static const QString getDir()
Returns the directory last used for saving a file.
static void box(const QString &e)
Definition OGSError.cpp:23
Objects nodes for the TreeModel.
Definition TreeItem.h:28
TreeItem * parentItem() const
Definition TreeItem.cpp:115
Contains properties for the visualization of objects as VtkVisPipelineItems.
Is used to combine several filter in one VtkVisPipelineItem. You can use vtk filter and custom filter...
static std::optional< GeoLib::Raster > convertToRaster(VtkGeoImageSource *const source)
vtkImageChangeInformation * _transformFilter
VtkVisImageItem(vtkAlgorithm *algorithm, TreeItem *parentItem, const QList< QVariant > data=QList< QVariant >())
Constructor for a source/filter object.
bool writeAsRaster()
Allows writing this item's source object as an ASCII raster file.
void Initialize(vtkRenderer *renderer) override
Initializes vtkMapper and vtkActor necessary for visualization of the item and sets the item's proper...
vtkAlgorithm * transformFilter() const override
int callVTKWriter(vtkAlgorithm *algorithm, const std::string &filename) const override
Selects the appropriate VTK-Writer object and writes the object to a file with the given name.
~VtkVisImageItem() override
void setVtkProperties(VtkAlgorithmProperties *vtkProps)
void setTranslation(double x, double y, double z) const override
Translates the item in visualisation-space. This function is empty and needs to be implemented by der...
An item in the VtkVisPipeline containing a graphic object to be visualized.
vtkAlgorithm * algorithm() const
Returns the algorithm object.
vtkAlgorithm * _algorithm
QVariant data(int column) const override
virtual void SetActiveAttribute(const QString &str)
std::string getFileExtension(const std::string &path)