Peano
Loading...
Searching...
No Matches
point.h
Go to the documentation of this file.
1#pragma once
2
3#include <typeinfo>
4
5#include <lang/assert.h>
6#include <io/writer/concept.h>
7
8template<typename FileWriter>
10protected:
11 FileWriter *writer = nullptr;
12 unsigned long pointCount = 0;
13
14 constexpr int particleCountPosition() const {
15 return 59;
16 }
17
18 template<typename T>
19 T byteswapped(T val) {
20 T result;
21
22 auto *bufIn = (char *) &val;
23 auto *bufOut = (char *) &result;
24
25 for (int i = 0; i < sizeof(T); i++) {
26 bufOut[sizeof(T) - i - 1] = bufIn[i];
27 }
28
29 return result;
30 }
31
33 this->writer->seek(particleCountPosition());
34
35 char buf[24];
36 auto bytes = sprintf(buf, "%021lu", this->pointCount);
37 assert(bytes == 21)
38
39 this->writer->write(buf, bytes);
40 }
41
42 explicit VtkPointWriterBase(FileWriter *writer) : writer(writer) {}
43
44 explicit VtkPointWriterBase() = default;
45};
46
47template<typename FileWriter, typename fp = float> requires IoWriter<FileWriter> and std::is_floating_point_v<fp>
48class VtkPointAsciiWriter : protected VtkPointWriterBase<FileWriter> {
49public:
50 explicit VtkPointAsciiWriter(FileWriter *writer) : VtkPointWriterBase<FileWriter>(writer) {
51 this->writer->write("# vtk DataFile Version 3.0\n"
52 "\n"
53 "ASCII \n"
54 "DATASET POLYDATA\nPOINTS 000000000000000000000 ");
55 if (typeid(fp) == typeid(float)) {
56 this->writer->write(" float\n");
57 } else {
58 assert(typeid(fp) == typeid(double))
59 this->writer->write("double\n");
60 }
61 }
62
63 explicit VtkPointAsciiWriter() = default;
64
65 void add(fp x, fp y, fp z) {
66 char buf[48];
67 int bytes = sprintf(buf, "%.8e %.8e %.8e\n", x, y, z);
68 this->writer->write(buf, bytes);
69 this->pointCount++;
70 }
71
73 char buf[48];
74 auto bytes = sprintf(buf, "POINT_DATA %021lu\n", this->pointCount);
75 assert(bytes <= 48)
76 this->writer->write(buf, bytes);
77 }
78
79 void addScalarPointDataHeader(const char* name) {
80 char buf[512];
81 auto bytes = sprintf(buf, "SCALARS %s %s 1\nLOOKUP_TABLE default\n", name, typeid(fp) == typeid(float) ? " float" : "double");
82 assert(bytes <= 512)
83 this->writer->write(buf, bytes);
84 }
85
86 void addScalar(fp value) {
87 char buf[48];
88 int bytes = sprintf(buf, "%.8e\n", value);
89 assert(bytes <= 48)
90 this->writer->write(buf, bytes);
91 }
92
93 void finish() {
94 this->updatePointsCount();
95 this->writer->sync();
96 }
97};
98
99template<typename FileWriter, typename fp = float> requires IoWriter<FileWriter> and std::is_floating_point_v<fp>
100class VtkPointBinaryWriter : protected VtkPointWriterBase<FileWriter> {
101public:
102 explicit VtkPointBinaryWriter(FileWriter *writer) : VtkPointWriterBase<FileWriter>(writer) {
103 this->writer->write("# vtk DataFile Version 3.0\n"
104 "\n"
105 "BINARY\n"
106 "DATASET POLYDATA\nPOINTS 000000000000000000000 ");
107 if (typeid(fp) == typeid(float)) {
108 this->writer->write(" float\n");
109 } else {
110 assert(typeid(fp) == typeid(double))
111 this->writer->write("double\n");
112 }
113 }
114
115 explicit VtkPointBinaryWriter() = default;
116
117 void add(fp x, fp y, fp z) {
118 if constexpr (IoBufferedWriter<FileWriter>) {
119 auto *buf = (fp*) this->writer->buffer(3 * sizeof(fp));
120 if (buf) {
121 buf[0] = this->byteswapped(x);
122 buf[1] = this->byteswapped(y);
123 buf[2] = this->byteswapped(z);
124 this->pointCount++;
125 return;
126 }
127 }
128 this->writer->write(this->byteswapped(x));
129 this->writer->write(this->byteswapped(y));
130 this->writer->write(this->byteswapped(z));
131
132 this->pointCount++;
133 }
134
136 char buf[48];
137 auto bytes = sprintf(buf, "POINT_DATA %021lu\n", this->pointCount);
138 assert(bytes <= 48)
139 this->writer->write(buf, bytes);
140 }
141
142 void addScalarPointDataHeader(const char* name) {
143 char buf[512];
144 auto bytes = sprintf(buf, "SCALARS %s %s 1\nLOOKUP_TABLE default\n", name, typeid(fp) == typeid(float) ? " float" : "double");
145 assert(bytes <= 512)
146 this->writer->write(buf, bytes);
147 }
148
149 void addScalar(fp value) {
150 this->writer->write(this->byteswapped(value));
151 }
152
153 void finish() {
154 this->updatePointsCount();
155 this->writer->sync();
156 }
157};
#define assert(...)
Definition LinuxAMD.h:28
VtkPointAsciiWriter(FileWriter *writer)
Definition point.h:50
void addScalar(fp value)
Definition point.h:86
VtkPointAsciiWriter()=default
void addScalarPointDataHeader(const char *name)
Definition point.h:79
void add(fp x, fp y, fp z)
Definition point.h:65
void addPointDataHeader()
Definition point.h:72
VtkPointBinaryWriter(FileWriter *writer)
Definition point.h:102
void add(fp x, fp y, fp z)
Definition point.h:117
void addPointDataHeader()
Definition point.h:135
void addScalarPointDataHeader(const char *name)
Definition point.h:142
VtkPointBinaryWriter()=default
void addScalar(fp value)
Definition point.h:149
FileWriter * writer
Definition point.h:11
unsigned long pointCount
Definition point.h:12
void updatePointsCount()
Definition point.h:32
VtkPointWriterBase(FileWriter *writer)
Definition point.h:42
VtkPointWriterBase()=default
constexpr int particleCountPosition() const
Definition point.h:14
T byteswapped(T val)
Definition point.h:19