OGS
VtkStationSource.cpp
Go to the documentation of this file.
1 
15 // ** VTK INCLUDES **
16 #include "VtkStationSource.h"
17 
18 #include <vtkCellArray.h>
19 #include <vtkCellData.h>
20 #include <vtkDoubleArray.h>
21 #include <vtkInformation.h>
22 #include <vtkInformationVector.h>
23 #include <vtkLine.h>
24 #include <vtkPointData.h>
25 #include <vtkPoints.h>
26 #include <vtkPolyData.h>
27 #include <vtkProperty.h>
28 #include <vtkSmartPointer.h>
29 #include <vtkStreamingDemandDrivenPipeline.h>
30 
31 #include "BaseLib/Logging.h"
32 #include "GeoLib/StationBorehole.h"
33 #include "vtkObjectFactory.h"
34 
36 
38 {
39  _removable = false; // From VtkAlgorithmProperties
40  this->SetNumberOfInputPorts(0);
41 
43  GetProperties()->SetColor(c[0] / 255.0, c[1] / 255.0, c[2] / 255.0);
44 }
45 
46 void VtkStationSource::PrintSelf(ostream& os, vtkIndent indent)
47 {
48  this->Superclass::PrintSelf(os, indent);
49 
50  if (_stations->empty())
51  {
52  return;
53  }
54 
55  os << indent << "== VtkStationSource =="
56  << "\n";
57 
58  int i = 0;
59  for (auto station : *_stations)
60  {
61  os << indent << "Station " << i << " (" << (*station)[0] << ", "
62  << (*station)[1] << ", " << (*station)[2] << ")\n";
63  i++;
64  }
65 }
66 
68 int VtkStationSource::RequestData(vtkInformation* request,
69  vtkInformationVector** inputVector,
70  vtkInformationVector* outputVector)
71 {
72  (void)request;
73  (void)inputVector;
74 
75  if (!_stations)
76  {
77  return 0;
78  }
79  std::size_t nStations = _stations->size();
80  if (nStations == 0)
81  {
82  return 0;
83  }
84 
85  bool useStationValues(false);
86  double sValue =
87  static_cast<GeoLib::Station*>((*_stations)[0])->getStationValue();
88  for (std::size_t i = 1; i < nStations; i++)
89  {
90  if (static_cast<GeoLib::Station*>((*_stations)[i])->getStationValue() !=
91  sValue)
92  {
93  useStationValues = true;
94  break;
95  }
96  }
97 
98  bool isBorehole = dynamic_cast<GeoLib::StationBorehole*>((*_stations)[0]);
99 
100  vtkSmartPointer<vtkInformation> outInfo =
101  outputVector->GetInformationObject(0);
102  vtkSmartPointer<vtkPolyData> output =
103  vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
104 
105  vtkSmartPointer<vtkPoints> newStations = vtkSmartPointer<vtkPoints>::New();
106  vtkSmartPointer<vtkCellArray> newVerts =
107  vtkSmartPointer<vtkCellArray>::New();
108  newVerts->Allocate(nStations);
109 
110  vtkSmartPointer<vtkCellArray> newLines;
111 
112  if (isBorehole)
113  {
114  newLines = vtkSmartPointer<vtkCellArray>::New();
115  }
116 
117  if (outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) >
118  0)
119  {
120  return 1;
121  }
122 
123  vtkSmartPointer<vtkIntArray> station_ids =
124  vtkSmartPointer<vtkIntArray>::New();
125  station_ids->SetNumberOfComponents(1);
126  station_ids->SetName("SiteIDs");
127 
128  vtkSmartPointer<vtkDoubleArray> station_values =
129  vtkSmartPointer<vtkDoubleArray>::New();
130  station_values->SetNumberOfComponents(1);
131  station_values->SetName("StationValue");
132 
133  vtkSmartPointer<vtkIntArray> strat_ids =
134  vtkSmartPointer<vtkIntArray>::New();
135  strat_ids->SetNumberOfComponents(1);
136  strat_ids->SetName("Stratigraphies");
137 
138  std::size_t lastMaxIndex(0);
139  std::size_t site_count(0);
140 
141  // Generate graphic objects
142  for (auto station : *_stations)
143  {
144  double coords[3] = {(*station)[0], (*station)[1], (*station)[2]};
145  vtkIdType sid = newStations->InsertNextPoint(coords);
146  station_ids->InsertNextValue(site_count);
147  if (useStationValues)
148  {
149  station_values->InsertNextValue(
150  static_cast<GeoLib::Station*>(station)->getStationValue());
151  }
152 
153  if (!isBorehole)
154  {
155  newVerts->InsertNextCell(1, &sid);
156  }
157  else
158  {
159  std::vector<GeoLib::Point*> profile =
160  static_cast<GeoLib::StationBorehole*>(station)->getProfile();
161  std::vector<std::string> soilNames =
162  static_cast<GeoLib::StationBorehole*>(station)->getSoilNames();
163  const std::size_t nLayers = profile.size();
164 
165  for (std::size_t i = 1; i < nLayers; i++)
166  {
167  auto* pCoords = const_cast<double*>(profile[i]->getCoords());
168  double loc[3] = {pCoords[0], pCoords[1], pCoords[2]};
169  newStations->InsertNextPoint(loc);
170  station_ids->InsertNextValue(site_count);
171  newLines->InsertNextCell(2);
172  newLines->InsertCellPoint(
173  lastMaxIndex); // start of borehole-layer
174  newLines->InsertCellPoint(
175  ++lastMaxIndex); // end of boreholelayer
176  strat_ids->InsertNextValue(this->GetIndexByName(soilNames[i]));
177  if (useStationValues)
178  {
179  station_values->InsertNextValue(
180  static_cast<GeoLib::Station*>(station)
181  ->getStationValue());
182  }
183  }
184  lastMaxIndex++;
185  }
186  site_count++;
187  }
188 
189  output->SetPoints(newStations);
190 
191  if (!isBorehole)
192  {
193  output->SetVerts(newVerts);
194  output->GetCellData()->AddArray(station_ids);
195  output->GetCellData()->SetActiveAttribute(
196  "SiteIDs", vtkDataSetAttributes::SCALARS);
197  }
198  else
199  {
200  output->SetLines(newLines);
201  // output->GetCellData()->AddArray(station_ids);
202  output->GetCellData()->AddArray(strat_ids);
203  output->GetCellData()->SetActiveAttribute(
204  "Stratigraphies", vtkDataSetAttributes::SCALARS);
205  }
206  if (useStationValues)
207  {
208  output->GetPointData()->AddArray(station_values);
209  }
210 
211  output->Squeeze();
212 
213  return 1;
214 }
215 
216 int VtkStationSource::RequestInformation(vtkInformation* /*request*/,
217  vtkInformationVector** /*inputVector*/,
218  vtkInformationVector* /*outputVector*/)
219 {
220  return 1;
221 }
222 
223 void VtkStationSource::SetUserProperty(QString name, QVariant value)
224 {
225  Q_UNUSED(name);
226  Q_UNUSED(value);
227 }
228 
229 std::size_t VtkStationSource::GetIndexByName(std::string const& name)
230 {
231  vtkIdType max_key(0);
232  for (auto& it : _id_map)
233  {
234  if (name == it.first)
235  {
236  return it.second;
237  }
238  if (it.second > max_key)
239  {
240  max_key = it.second;
241  }
242  }
243 
244  vtkIdType new_index = (_id_map.empty()) ? 0 : (max_key + 1);
245  INFO("Key '{:s}' has been assigned index {:d}.", name, new_index);
246  _id_map.insert(std::pair<std::string, vtkIdType>(name, new_index));
247  return new_index;
248 }
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
Definition of the StationBorehole class.
vtkStandardNewMacro(VtkStationSource)
Definition of the VtkStationSource class.
A borehole as a geometric object.
A Station (observation site) is basically a Point with some additional information.
Definition: Station.h:37
double getStationValue() const
Returns the specific value for this station.
Definition: Station.h:71
vtkProperty * GetProperties() const
Returns the vtk properties.
VTK source object for the visualisation of station data (including boreholes)
std::size_t GetIndexByName(std::string const &name)
void SetUserProperty(QString name, QVariant value) override
Sets a user property. This should be implemented by subclasses.
std::map< std::string, vtkIdType > _id_map
const std::vector< GeoLib::Point * > * _stations
The stations to visualize.
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
Computes the polygonal data object.
int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
void PrintSelf(ostream &os, vtkIndent indent) override
Prints its data on a stream.
std::array< unsigned char, 4 > Color
Definition: Color.h:24
Color getRandomColor()
Returns a random RGB colour.
Definition: Color.cpp:29
bool isBorehole(GeoLib::Point const *pnt)