Skip to content

Commit 124430a

Browse files
committed
Cleaning
1 parent c578375 commit 124430a

File tree

6 files changed

+393
-7
lines changed

6 files changed

+393
-7
lines changed

Code/Source/solver/VtkData.cpp

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,27 @@ Array<int> VtkVtpData::get_connectivity()
591591
return conn;
592592
}
593593

594+
void VtkVtpData::copy_cell_data(const std::string& data_name, Array<double>& mesh_data)
595+
{
596+
auto vtk_data = vtkDoubleArray::SafeDownCast(impl->vtk_polydata->GetCellData()->GetArray(data_name.c_str()));
597+
if (vtk_data == nullptr) {
598+
return;
599+
}
600+
601+
int num_data = vtk_data->GetNumberOfTuples();
602+
if (num_data == 0) {
603+
return;
604+
}
605+
int num_comp = vtk_data->GetNumberOfComponents();
606+
// Set the data.
607+
for (int i = 0; i < num_data; i++) {
608+
auto tuple = vtk_data->GetTuple(i);
609+
for (int j = 0; j < num_comp; j++) {
610+
mesh_data(j, i) = tuple[j];
611+
}
612+
}
613+
}
614+
594615
/// @brief Copy an array of point data from an polydata mesh into the given Array.
595616
//
596617
void VtkVtpData::copy_point_data(const std::string& data_name, Array<double>& mesh_data)
@@ -616,6 +637,43 @@ void VtkVtpData::copy_point_data(const std::string& data_name, Array<double>& me
616637
}
617638
}
618639

640+
void VtkVtpData::copy_cell_data(const std::string& data_name, Vector<double>& mesh_data)
641+
{
642+
auto vtk_data = vtkDoubleArray::SafeDownCast(impl->vtk_polydata->GetCellData()->GetArray(data_name.c_str()));
643+
if (vtk_data == nullptr) {
644+
return;
645+
}
646+
647+
int num_data = vtk_data->GetNumberOfTuples();
648+
if (num_data == 0) {
649+
return;
650+
}
651+
652+
// Set the data.
653+
for (int i = 0; i < num_data; i++) {
654+
mesh_data(i) = vtk_data->GetValue(i);
655+
}
656+
}
657+
void VtkVtpData::copy_cell_data(const std::string& data_name, Vector<int>& mesh_data)
658+
{
659+
std::cout << "[VtkVtpData.copy_cell_data] data_name: " << data_name << std::endl;
660+
auto vtk_data = vtkIntArray::SafeDownCast(impl->vtk_polydata->GetCellData()->GetArray(data_name.c_str()));
661+
if (vtk_data == nullptr) {
662+
return;
663+
}
664+
665+
int num_data = vtk_data->GetNumberOfTuples();
666+
if (num_data == 0) {
667+
return;
668+
}
669+
//std::cout << "[VtkVtpData.copy_cell_data] num_data: " << num_data << std::endl;
670+
671+
// Set the data.
672+
for (int i = 0; i < num_data; i++) {
673+
mesh_data(i) = vtk_data->GetValue(i);
674+
}
675+
}
676+
619677
void VtkVtpData::copy_point_data(const std::string& data_name, Vector<double>& mesh_data)
620678
{
621679
auto vtk_data = vtkDoubleArray::SafeDownCast(impl->vtk_polydata->GetPointData()->GetArray(data_name.c_str()));
@@ -717,6 +775,20 @@ std::vector<std::string> VtkVtpData::get_point_data_names()
717775
return data_names;
718776
}
719777

778+
/// @brief Get a list of cell data names.
779+
std::vector<std::string> VtkVtpData::get_cell_data_names()
780+
{
781+
std::vector<std::string> data_names;
782+
int num_arrays = impl->vtk_polydata->GetCellData()->GetNumberOfArrays();
783+
784+
for (int i = 0; i < num_arrays; i++) {
785+
auto array_name = impl->vtk_polydata->GetCellData()->GetArrayName(i);
786+
data_names.push_back(array_name);
787+
}
788+
789+
return data_names;
790+
}
791+
720792
/// @brief Get an array of point data from an unstructured grid.
721793
//
722794
Array<double> VtkVtpData::get_points()
@@ -749,6 +821,19 @@ bool VtkVtpData::has_point_data(const std::string& data_name)
749821
return false;
750822
}
751823

824+
bool VtkVtpData::has_cell_data(const std::string& data_name)
825+
{
826+
int num_arrays = impl->vtk_polydata->GetCellData()->GetNumberOfArrays();
827+
828+
for (int i = 0; i < num_arrays; i++) {
829+
if (!strcmp(impl->vtk_polydata->GetCellData()->GetArrayName(i), data_name.c_str())) {
830+
return true;
831+
}
832+
}
833+
834+
return false;
835+
}
836+
752837
int VtkVtpData::elem_type()
753838
{
754839
return impl->elem_type;
@@ -873,6 +958,73 @@ std::vector<std::string> VtkVtuData::get_point_data_names()
873958
return data_names;
874959
}
875960

961+
std::vector<std::string> VtkVtuData::get_cell_data_names()
962+
{
963+
std::vector<std::string> data_names;
964+
int num_arrays = impl->vtk_ugrid->GetCellData()->GetNumberOfArrays();
965+
966+
for (int i = 0; i < num_arrays; i++) {
967+
auto array_name = impl->vtk_ugrid->GetCellData()->GetArrayName(i);
968+
data_names.push_back(array_name);
969+
}
970+
971+
return data_names;
972+
}
973+
974+
void VtkVtuData::copy_cell_data(const std::string& data_name, Array<double>& mesh_data)
975+
{
976+
// Get the cell data
977+
auto cell_data = impl->vtk_ugrid->GetCellData();
978+
if (cell_data == nullptr) {
979+
throw std::runtime_error("VTK CellData is null");
980+
}
981+
982+
// Get the specific array
983+
auto vtk_data = vtkIntArray::SafeDownCast(cell_data->GetArray(data_name.c_str()));
984+
if (vtk_data == nullptr) {
985+
986+
auto temp_array = cell_data->GetArray(data_name.c_str());
987+
if (temp_array == nullptr) {
988+
throw std::runtime_error("Array '" + data_name + "' not found");
989+
}
990+
991+
auto double_array = vtkDoubleArray::SafeDownCast(temp_array);
992+
if (double_array) {
993+
int num_data = double_array->GetNumberOfTuples();
994+
for (int i = 0; i < num_data; i++) {
995+
mesh_data(i) = static_cast<int>(double_array->GetValue(i));
996+
}
997+
return;
998+
}
999+
1000+
throw std::runtime_error("Array '" + data_name + "' cannot be converted to integer array");
1001+
}
1002+
1003+
int num_data = vtk_data->GetNumberOfTuples();
1004+
1005+
if (num_data == 0) {
1006+
std::cout << "WARNING: Array contains no data!" << std::endl;
1007+
return;
1008+
}
1009+
1010+
int num_comp = vtk_data->GetNumberOfComponents();
1011+
1012+
// Set the data based on number of components
1013+
if (num_comp == 1) {
1014+
// For single component arrays
1015+
for (int i = 0; i < num_data; i++) {
1016+
mesh_data(0, i) = static_cast<double>(vtk_data->GetValue(i));
1017+
}
1018+
} else {
1019+
// For multi-component arrays
1020+
for (int i = 0; i < num_data; i++) {
1021+
double* tuple = vtk_data->GetTuple(i);
1022+
for (int j = 0; j < num_comp; j++) {
1023+
mesh_data(j, i) = static_cast<double>(tuple[j]);
1024+
}
1025+
}
1026+
}
1027+
}
8761028
/// @brief Copy an array of point data from an unstructured grid into the given Array.
8771029
//
8781030
void VtkVtuData::copy_point_data(const std::string& data_name, Array<double>& mesh_data)
@@ -899,6 +1051,55 @@ void VtkVtuData::copy_point_data(const std::string& data_name, Array<double>& me
8991051
}
9001052
}
9011053

1054+
1055+
void VtkVtuData::copy_cell_data(const std::string& data_name, Vector<double>& mesh_data)
1056+
{
1057+
// Get the cell data
1058+
auto cell_data = impl->vtk_ugrid->GetCellData();
1059+
if (cell_data == nullptr) {
1060+
throw std::runtime_error("VTK CellData is null");
1061+
}
1062+
1063+
// Get the specific array
1064+
auto vtk_data = vtkIntArray::SafeDownCast(cell_data->GetArray(data_name.c_str()));
1065+
if (vtk_data == nullptr) {
1066+
1067+
auto temp_array = cell_data->GetArray(data_name.c_str());
1068+
if (temp_array == nullptr) {
1069+
throw std::runtime_error("Array '" + data_name + "' not found");
1070+
}
1071+
1072+
auto double_array = vtkDoubleArray::SafeDownCast(temp_array);
1073+
if (double_array) {
1074+
int num_data = double_array->GetNumberOfTuples();
1075+
for (int i = 0; i < num_data; i++) {
1076+
mesh_data(i) = static_cast<int>(double_array->GetValue(i));
1077+
}
1078+
return;
1079+
}
1080+
1081+
throw std::runtime_error("Array '" + data_name + "' cannot be converted to integer array");
1082+
}
1083+
1084+
int num_data = vtk_data->GetNumberOfTuples();
1085+
1086+
if (num_data == 0) {
1087+
std::cout << "WARNING: Array contains no data!" << std::endl;
1088+
return;
1089+
}
1090+
1091+
// Check mesh_data size
1092+
if (mesh_data.size() < num_data) {
1093+
// Resize if needed
1094+
mesh_data.resize(num_data);
1095+
}
1096+
1097+
// Set the data
1098+
for (int i = 0; i < num_data; i++) {
1099+
mesh_data(i) = vtk_data->GetValue(i);
1100+
}
1101+
1102+
}
9021103
void VtkVtuData::copy_point_data(const std::string& data_name, Vector<double>& mesh_data)
9031104
{
9041105
auto vtk_data = vtkDoubleArray::SafeDownCast(impl->vtk_ugrid->GetPointData()->GetArray(data_name.c_str()));
@@ -919,6 +1120,55 @@ void VtkVtuData::copy_point_data(const std::string& data_name, Vector<double>& m
9191120
}
9201121
}
9211122

1123+
void VtkVtuData::copy_cell_data(const std::string& data_name, Vector<int>& mesh_data)
1124+
{
1125+
// Get the cell data
1126+
auto cell_data = impl->vtk_ugrid->GetCellData();
1127+
if (cell_data == nullptr) {
1128+
throw std::runtime_error("VTK CellData is null");
1129+
}
1130+
1131+
// Get the specific array
1132+
auto vtk_data = vtkIntArray::SafeDownCast(cell_data->GetArray(data_name.c_str()));
1133+
if (vtk_data == nullptr) {
1134+
1135+
auto temp_array = cell_data->GetArray(data_name.c_str());
1136+
if (temp_array == nullptr) {
1137+
throw std::runtime_error("Array '" + data_name + "' not found");
1138+
}
1139+
1140+
auto double_array = vtkDoubleArray::SafeDownCast(temp_array);
1141+
if (double_array) {
1142+
int num_data = double_array->GetNumberOfTuples();
1143+
for (int i = 0; i < num_data; i++) {
1144+
mesh_data(i) = static_cast<int>(double_array->GetValue(i));
1145+
}
1146+
return;
1147+
}
1148+
1149+
throw std::runtime_error("Array '" + data_name + "' cannot be converted to integer array");
1150+
}
1151+
1152+
int num_data = vtk_data->GetNumberOfTuples();
1153+
1154+
if (num_data == 0) {
1155+
std::cout << "WARNING: Array contains no data!" << std::endl;
1156+
return;
1157+
}
1158+
1159+
// Check mesh_data size
1160+
if (mesh_data.size() < num_data) {
1161+
// Resize if needed
1162+
mesh_data.resize(num_data);
1163+
}
1164+
1165+
// Set the data
1166+
for (int i = 0; i < num_data; i++) {
1167+
mesh_data(i) = vtk_data->GetValue(i);
1168+
}
1169+
1170+
}
1171+
9221172
void VtkVtuData::copy_point_data(const std::string& data_name, Vector<int>& mesh_data)
9231173
{
9241174
auto vtk_data = vtkIntArray::SafeDownCast(impl->vtk_ugrid->GetPointData()->GetArray(data_name.c_str()));
@@ -971,6 +1221,45 @@ bool VtkVtuData::has_point_data(const std::string& data_name)
9711221
return false;
9721222
}
9731223

1224+
bool VtkVtuData::has_cell_data(const std::string& data_name)
1225+
{
1226+
int num_arrays = impl->vtk_ugrid->GetCellData()->GetNumberOfArrays();
1227+
1228+
for (int i = 0; i < num_arrays; i++) {
1229+
if (!strcmp(impl->vtk_ugrid->GetCellData()->GetArrayName(i), data_name.c_str())) {
1230+
return true;
1231+
}
1232+
}
1233+
1234+
return false;
1235+
}
1236+
/// @brief Get an array of cell data from an unstructured grid.
1237+
Array<double> VtkVtuData::get_cell_data(const std::string& data_name)
1238+
{
1239+
auto vtk_data = vtkDoubleArray::SafeDownCast(impl->vtk_ugrid->GetCellData()->GetArray(data_name.c_str()));
1240+
if (vtk_data == nullptr) {
1241+
return Array<double>();
1242+
}
1243+
1244+
int num_data = vtk_data->GetNumberOfTuples();
1245+
if (num_data == 0) {
1246+
return Array<double>();
1247+
}
1248+
1249+
int num_comp = vtk_data->GetNumberOfComponents();
1250+
1251+
// Set the data.
1252+
Array<double> data(num_data, num_comp);
1253+
for (int i = 0; i < num_data; i++) {
1254+
auto tuple = vtk_data->GetTuple(i);
1255+
for (int j = 0; j < num_comp; j++) {
1256+
data(i, j) = tuple[j];
1257+
}
1258+
}
1259+
1260+
return data;
1261+
}
1262+
9741263
/// @brief Get an array of point data from an unstructured grid.
9751264
//
9761265
Array<double> VtkVtuData::get_point_data(const std::string& data_name)

Code/Source/solver/VtkData.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ class VtkData {
6060
virtual void set_connectivity(const int nsd, const Array<int>& conn, const int pid = 0) = 0;
6161

6262
virtual bool has_point_data(const std::string& data_name) = 0;
63+
virtual bool has_cell_data(const std::string& data_name) = 0;
6364

6465
virtual void copy_points(Array<double>& points) = 0;
6566
virtual void copy_point_data(const std::string& data_name, Array<double>& mesh_data) = 0;
6667
virtual void copy_point_data(const std::string& data_name, Vector<double>& mesh_data) = 0;
68+
virtual void copy_cell_data(const std::string& data_name, Vector<int>& mesh_data) = 0;
69+
virtual void copy_cell_data(const std::string& data_name, Vector<double>& mesh_data) = 0;
6770
virtual void write() = 0;
6871

6972
static VtkData* create_reader(const std::string& file_name);
@@ -87,12 +90,18 @@ class VtkVtpData : public VtkData {
8790
virtual void read_file(const std::string& file_name);
8891

8992
void copy_points(Array<double>& points);
93+
void copy_cell_data(const std::string& data_name, Array<double>& mesh_data);
94+
void copy_cell_data(const std::string& data_name, Vector<double>& mesh_data);
95+
void copy_cell_data(const std::string& data_name, Vector<int>& mesh_data);
9096
void copy_point_data(const std::string& data_name, Array<double>& mesh_data);
9197
void copy_point_data(const std::string& data_name, Vector<double>& mesh_data);
9298
void copy_point_data(const std::string& data_name, Vector<int>& mesh_data);
9399
Array<double> get_point_data(const std::string& data_name);
94100
std::vector<std::string> get_point_data_names();
101+
Array<double> get_cell_data(const std::string& data_name);
102+
std::vector<std::string> get_cell_data_names();
95103
bool has_point_data(const std::string& data_name);
104+
bool has_cell_data(const std::string& data_name);
96105
virtual void set_connectivity(const int nsd, const Array<int>& conn, const int pid = 0);
97106

98107
virtual void set_element_data(const std::string& data_name, const Array<double>& data);
@@ -132,6 +141,14 @@ class VtkVtuData : public VtkData {
132141
std::vector<std::string> get_point_data_names();
133142
virtual Array<double> get_points();
134143
bool has_point_data(const std::string& data_name);
144+
bool has_cell_data(const std::string& data_name);
145+
virtual void copy_cell_data(const std::string& data_name, Array<double>& mesh_data);
146+
virtual void copy_cell_data(const std::string& data_name, Vector<double>& mesh_data);
147+
virtual void copy_cell_data(const std::string& data_name, Vector<int>& mesh_data);
148+
149+
Array<double> get_cell_data(const std::string& data_name);
150+
virtual std::vector<std::string> get_cell_data_names();
151+
135152
virtual void set_connectivity(const int nsd, const Array<int>& conn, const int pid = 0);
136153

137154
virtual void set_element_data(const std::string& data_name, const Array<double>& data);

0 commit comments

Comments
 (0)