| 1105 | } |
| 1106 | |
| 1107 | void writeParticlesVTK(const std::string& fileName, const unsigned int numParticles, const Vector3r* particlePositions) |
| 1108 | { |
| 1109 | if (0 == numParticles) |
| 1110 | return; |
| 1111 | |
| 1112 | #ifdef USE_DOUBLE |
| 1113 | const char* real_str = " double\n"; |
| 1114 | #else |
| 1115 | const char* real_str = " float\n"; |
| 1116 | #endif |
| 1117 | |
| 1118 | // Open the file |
| 1119 | std::ofstream outfile{ fileName, std::ios::binary }; |
| 1120 | if (!outfile.is_open()) |
| 1121 | { |
| 1122 | LOG_WARN << "Cannot open a file to save VTK particles."; |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | outfile << "# vtk DataFile Version 4.1\n"; |
| 1127 | outfile << "SPlisHSPlasH particle data\n"; // title of the data set, (any string up to 256 characters+\n) |
| 1128 | outfile << "BINARY\n"; |
| 1129 | outfile << "DATASET UNSTRUCTURED_GRID\n"; |
| 1130 | |
| 1131 | ////////////////////////////////////////////////////////////////////////// |
| 1132 | // export position attribute as POINTS |
| 1133 | { |
| 1134 | std::vector<Vector3r> positions; |
| 1135 | positions.reserve(numParticles); |
| 1136 | for (unsigned int i = 0u; i < numParticles; i++) |
| 1137 | positions.emplace_back(particlePositions[i]); |
| 1138 | // swap endianess |
| 1139 | for (unsigned int i = 0; i < numParticles; i++) |
| 1140 | for (unsigned int c = 0; c < 3; c++) |
| 1141 | swapByteOrder(&positions[i][c]); |
| 1142 | // export to vtk |
| 1143 | outfile << "POINTS " << numParticles << real_str; |
| 1144 | outfile.write(reinterpret_cast<char*>(positions[0].data()), 3 * numParticles * sizeof(Real)); |
| 1145 | outfile << "\n"; |
| 1146 | } |
| 1147 | |
| 1148 | ////////////////////////////////////////////////////////////////////////// |
| 1149 | // export particle IDs as CELLS |
| 1150 | { |
| 1151 | std::vector<Eigen::Vector2i> cells; |
| 1152 | cells.reserve(numParticles); |
| 1153 | unsigned int nodes_per_cell_swapped = 1; |
| 1154 | swapByteOrder(&nodes_per_cell_swapped); |
| 1155 | for (unsigned int i = 0u; i < numParticles; i++) |
| 1156 | { |
| 1157 | unsigned int idSwapped = i; |
| 1158 | swapByteOrder(&idSwapped); |
| 1159 | cells.emplace_back(nodes_per_cell_swapped, idSwapped); |
| 1160 | } |
| 1161 | |
| 1162 | // particles are cells with one element and the index of the particle |
| 1163 | outfile << "CELLS " << numParticles << " " << 2 * numParticles << "\n"; |
| 1164 | outfile.write(reinterpret_cast<char*>(cells[0].data()), 2 * numParticles * sizeof(unsigned int)); |