|
13 | 13 | #include <vector> |
14 | 14 | #include <string> |
15 | 15 | #include <fstream> |
16 | | -#include "../struct/particle.h" |
| 16 | +#include "struct/particle.h" |
17 | 17 |
|
18 | | -inline void SaveVTK(const std::vector<Particle>& p, const std::string& filename) |
| 18 | +/** |
| 19 | + * @brief Saves the SoA Particle database to a VTK Legacy file for ParaView. |
| 20 | + */ |
| 21 | +inline void SaveVTK(const Particle& p, const std::string& filename) |
19 | 22 | { |
20 | 23 | std::ofstream out(filename); |
21 | 24 | if (!out) return; |
22 | 25 |
|
23 | | - const size_t N = p.size(); |
| 26 | + const size_t N = p.size(); // p is the SoA database |
24 | 27 | out << "# vtk DataFile Version 3.0\n"; |
25 | 28 | out << "NEXT snapshot\n"; |
26 | 29 | out << "ASCII\n"; |
27 | 30 | out << "DATASET POLYDATA\n"; |
28 | 31 |
|
| 32 | + // Detect precision based on your build macros |
29 | 33 | #ifdef NEXT_FP64 |
30 | 34 | constexpr const char* vtkType = "double"; |
31 | | - #elif defined(NEXT_FP32) |
| 35 | + #else |
32 | 36 | constexpr const char* vtkType = "float"; |
33 | 37 | #endif |
34 | 38 |
|
35 | | - // Points |
| 39 | + // --- POINTS (Coordinates) --- |
36 | 40 | out << "POINTS " << N << " " << vtkType << "\n"; |
37 | | - for (const auto& a : p) |
38 | | - out << a.x << " " << a.y << " " << a.z << "\n"; |
| 41 | + for (size_t i = 0; i < N; i++) { |
| 42 | + out << p.x[i] << " " << p.y[i] << " " << p.z[i] << "\n"; |
| 43 | + } |
39 | 44 |
|
40 | | - // Vertices |
41 | | - out << "VERTICES " << N << " " << N*2 << "\n"; |
42 | | - for (size_t i = 0; i < N; i++) |
| 45 | + // --- VERTICES --- |
| 46 | + // VTK needs topology to render points as actual pixels |
| 47 | + out << "VERTICES " << N << " " << N * 2 << "\n"; |
| 48 | + for (size_t i = 0; i < N; i++) { |
43 | 49 | out << "1 " << i << "\n"; |
| 50 | + } |
44 | 51 |
|
45 | 52 | out << "POINT_DATA " << N << "\n"; |
46 | 53 |
|
47 | | - // Type (0 for Star, 1 for DM) |
48 | | - // In ParaView, use the "Threshold" filter on this to hide DM |
| 54 | + // --- Type (0 for Star, 1 for DM) --- |
49 | 55 | out << "SCALARS type int 1\n"; |
50 | 56 | out << "LOOKUP_TABLE default\n"; |
51 | | - for (const auto& a : p) |
52 | | - out << a.type << "\n"; |
| 57 | + for (size_t i = 0; i < N; i++) { |
| 58 | + out << p.type[i] << "\n"; |
| 59 | + } |
53 | 60 |
|
54 | | - // Velocity Vectors |
| 61 | + // --- Velocity Vectors --- |
55 | 62 | out << "VECTORS velocity " << vtkType << "\n"; |
56 | | - for (const auto& a : p) |
57 | | - out << a.vx << " " << a.vy << " " << a.vz << "\n"; |
| 63 | + for (size_t i = 0; i < N; i++) { |
| 64 | + out << p.vx[i] << " " << p.vy[i] << " " << p.vz[i] << "\n"; |
| 65 | + } |
58 | 66 |
|
59 | | - // Mass |
| 67 | + // --- Mass --- |
60 | 68 | out << "SCALARS mass " << vtkType << " 1\n"; |
61 | 69 | out << "LOOKUP_TABLE default\n"; |
62 | | - for (const auto& a : p) |
63 | | - out << a.m << "\n"; |
| 70 | + for (size_t i = 0; i < N; i++) { |
| 71 | + out << p.m[i] << "\n"; |
| 72 | + } |
64 | 73 |
|
65 | 74 | out.close(); |
66 | 75 | } |
0 commit comments