Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca-vtk.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
41#ifndef CA_VTK_H
42#define CA_VTK_H
43
44#include <filesystem>
45#include <fstream>
46#include <ostream>
47#include <string>
48#include <type_traits>
49#include <utility>
50
51#include <ah-errors.H>
52
53#include <ca-frame-stream.H>
54#include <ca-io.H>
55#include <tpl_ca_concepts.H>
56
57namespace Aleph {
58namespace CA {
59
62{
63 std::string title = "Aleph::CA frame";
64 std::string scalar_name = "state";
65 double spacing[3] = {1.0, 1.0, 1.0};
66 double origin[3] = {0.0, 0.0, 0.0};
67};
68
69namespace ca_vtk_detail {
70
71template <typename T>
72[[nodiscard]] inline const char *vtk_scalar_type()
73{
74 using U = std::remove_cv_t<T>;
75 if constexpr (std::is_same_v<U, bool>)
76 return "unsigned_char";
77 else if constexpr (std::is_floating_point_v<U> and sizeof(U) <= sizeof(float))
78 return "float";
79 else if constexpr (std::is_floating_point_v<U>)
80 return "double";
81 else if constexpr (std::is_signed_v<U> and sizeof(U) <= sizeof(int))
82 return "int";
83 else if constexpr (std::is_signed_v<U>)
84 return "long";
85 else if constexpr (sizeof(U) <= sizeof(unsigned int))
86 return "unsigned_int";
87 else
88 return "unsigned_long";
89}
90
91template <typename T>
92inline void write_scalar(std::ostream &out, const T &value)
93{
94 if constexpr (std::is_same_v<std::remove_cv_t<T>, bool>)
95 out << (value ? 1 : 0);
96 else if constexpr (std::is_integral_v<T> and sizeof(T) == 1)
98 else
99 out << value;
100}
101
102} // namespace ca_vtk_detail
103
118template <typename Lattice>
119inline void write_vtk_legacy(std::ostream &out,
120 const Lattice &frame,
121 const VTK_Write_Options &opts = {})
122{
123 static_assert(LatticeLike<Lattice>, "write_vtk_legacy requires a LatticeLike frame");
124 static_assert(Lattice::rank == 2 or Lattice::rank == 3,
125 "write_vtk_legacy supports rank-2 and rank-3 frames");
126 using state_t = typename Lattice::state_type;
127 static_assert(std::is_arithmetic_v<state_t>, "write_vtk_legacy requires arithmetic states");
128 using coord_t = typename Lattice::coord_type;
129
130 const ca_size_t nx = Lattice::rank == 2 ? frame.size(1) : frame.size(2);
131 const ca_size_t ny = Lattice::rank == 2 ? frame.size(0) : frame.size(1);
132 const ca_size_t nz = Lattice::rank == 2 ? 1 : frame.size(0);
133 const ca_size_t points = nx * ny * nz;
134
135 out << "# vtk DataFile Version 3.0\n";
136 out << opts.title << '\n';
137 out << "ASCII\n";
138 out << "DATASET STRUCTURED_POINTS\n";
139 out << "DIMENSIONS " << nx << ' ' << ny << ' ' << nz << '\n';
140 out << "ORIGIN " << opts.origin[0] << ' ' << opts.origin[1] << ' ' << opts.origin[2] << '\n';
141 out << "SPACING " << opts.spacing[0] << ' ' << opts.spacing[1] << ' ' << opts.spacing[2] << '\n';
142 out << "POINT_DATA " << points << '\n';
143 out << "SCALARS " << opts.scalar_name << ' ' << ca_vtk_detail::vtk_scalar_type<state_t>() << " 1\n";
144 out << "LOOKUP_TABLE default\n";
145
146 if constexpr (Lattice::rank == 2)
147 {
148 for (ca_size_t r = 0; r < frame.size(0); ++r)
149 for (ca_size_t c = 0; c < frame.size(1); ++c)
150 {
152 out, frame.at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}));
153 out << '\n';
154 }
155 }
156 else
157 {
158 for (ca_size_t z = 0; z < frame.size(0); ++z)
159 for (ca_size_t y = 0; y < frame.size(1); ++y)
160 for (ca_size_t x = 0; x < frame.size(2); ++x)
161 {
163 out, frame.at(coord_t{static_cast<ca_index_t>(z), static_cast<ca_index_t>(y),
164 static_cast<ca_index_t>(x)}));
165 out << '\n';
166 }
167 }
168
169 ah_runtime_error_if(not out) << "write_vtk_legacy: output stream failed";
170}
171
174{
175 std::filesystem::path pattern_;
177 std::size_t zero_pad_ = 6;
178
179public:
185 explicit VTK_Frame_Sink(std::filesystem::path path_pattern,
187 const std::size_t zero_pad = 6)
188 : pattern_(std::move(path_pattern)), opts_(std::move(opts)), zero_pad_(zero_pad)
189 {}
190
196 template <typename Lattice>
197 void accept(const std::size_t step, const Lattice &frame)
198 {
199 const std::filesystem::path path = format_step_path(pattern_, step, zero_pad_);
200 if (const auto parent = path.parent_path(); not parent.empty())
201 std::filesystem::create_directories(parent);
202 std::ofstream out(path);
203 ah_runtime_error_if(not out) << "VTK_Frame_Sink::accept: cannot open '" << path.string() << "'";
204 write_vtk_legacy(out, frame, opts_);
205 }
206
208 void flush() {}
209};
210
211} // namespace CA
212} // namespace Aleph
213
214#endif // CA_VTK_H
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:271
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
Directory-backed frame sinks for CA trajectories.
File-format readers and writers for cellular-automata frames.
Lattice that adds boundary-aware access on top of a storage.
typename Storage::state_type state_type
typename Storage::coord_type coord_type
static constexpr std::size_t rank
ca_size_t size() const noexcept
state_type at(const coord_type &c) const
Strict access: throws if c is out of range.
Path-pattern sink that writes VTK legacy frames.
Definition ca-vtk.H:174
VTK_Frame_Sink(std::filesystem::path path_pattern, VTK_Write_Options opts={}, const std::size_t zero_pad=6)
Build a VTK frame sink.
Definition ca-vtk.H:185
void accept(const std::size_t step, const Lattice &frame)
Write one VTK frame.
Definition ca-vtk.H:197
VTK_Write_Options opts_
Definition ca-vtk.H:176
std::size_t zero_pad_
Definition ca-vtk.H:177
std::filesystem::path pattern_
Definition ca-vtk.H:175
void flush()
VTK frames are written eagerly; this is a no-op.
Definition ca-vtk.H:208
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
static mpfr_t y
Definition mpfr_mul_d.c:3
void write_scalar(std::ostream &out, const T &value)
Definition ca-vtk.H:92
const char * vtk_scalar_type()
Definition ca-vtk.H:72
void write_vtk_legacy(std::ostream &out, const Lattice &frame, const VTK_Write_Options &opts={})
Write a 2D or 3D lattice as VTK legacy STRUCTURED_POINTS.
Definition ca-vtk.H:119
std::filesystem::path format_step_path(const std::filesystem::path &pattern, const std::size_t step, const std::size_t zero_pad=6)
Format a step-indexed file path.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
STL namespace.
Options for VTK legacy output.
Definition ca-vtk.H:62
std::string scalar_name
scalar field name
Definition ca-vtk.H:64
double spacing[3]
grid spacing in x, y, z
Definition ca-vtk.H:65
double origin[3]
grid origin in x, y, z
Definition ca-vtk.H:66
std::string title
VTK header title.
Definition ca-vtk.H:63
gsl_rng * r
C++20 concepts for the Cellular Automata module.