Skip to content

Commit 40a3221

Browse files
authored
Merge pull request #12 from core-marine-dev/vtp
VTP files support
2 parents 4e8f2b1 + b78d6fc commit 40a3221

3 files changed

Lines changed: 659 additions & 444 deletions

File tree

include/leanvtk.hpp

Lines changed: 175 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,125 @@ class VTKDataNode : public VTKDataNodeBase {
223223
int n_components_;
224224
};
225225

226-
class DECLDIR VTUWriter {
226+
class DECLDIR VTKWriter {
227227
public:
228-
VTUWriter()
228+
VTKWriter(const char* type_name)
229229
: binary_(false)
230230
, path_("")
231+
, cell_type_("Cell")
232+
, type_name_(type_name)
231233
{}
232234

233-
~VTUWriter();
235+
virtual ~VTKWriter();
236+
237+
/**
238+
* Add a general field to the mesh
239+
* const string& name name of the field to store vtk mesh
240+
* const vector<double>& data list of field values. There must be dimension
241+
* values for each point in the mesh to be written.
242+
* Format of the vector is
243+
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
244+
* ...
245+
* f_{n,1}, f_{n,2},..., f_{n, dimension}]
246+
* if there are n points in the mesh
247+
* const int dimension ambient dimension (2D or 3D)
248+
*/
249+
template <typename T>
250+
inline void add_field(const std::string &name,
251+
const std::vector<T> &data,
252+
const int &dimension)
253+
{
254+
if (dimension == 1)
255+
add_scalar_field<T>(name, data);
256+
else
257+
add_vector_field<T>(name, data, dimension);
258+
}
259+
260+
/**
261+
* Add a general cell/element field to the mesh
262+
* const string& name name of the field to store vtk mesh
263+
* const vector<double>& data list of field values. There must be dimension
264+
* values for each cell in the mesh to be written.
265+
* Format of the vector is
266+
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
267+
* ...
268+
* f_{m,1}, f_{m,2},..., f_{m, dimension}]
269+
* if there are m cells in the mesh
270+
* const int dimension ambient dimension (2D or 3D)
271+
*/
272+
template <typename T>
273+
void add_cell_field(const std::string &name,
274+
const std::vector<T> &data,
275+
const int &dimension)
276+
{
277+
if (dimension == 1)
278+
add_cell_scalar_field<T>(name, data);
279+
else
280+
add_cell_vector_field<T>(name, data, dimension);
281+
}
282+
283+
/**
284+
* Add a scalar field to the mesh
285+
* const string& name name of the field to store vtk mesh
286+
* const vector<double>& data list of field values. There must be one
287+
* value for each point in the mesh to be written.
288+
* Format of the vector is
289+
* [f_1, f_2,..., f_n]
290+
* if there are n points in the mesh
291+
*/
292+
template <typename T>
293+
void add_scalar_field(const std::string &name,
294+
const std::vector<T> &data);
295+
296+
/**
297+
* Add a scalar field to cells/elements of the mesh
298+
* const string& name name of the field to store vtk mesh
299+
* const vector<double>& data list of field values. There must be one
300+
* value for each cell in the mesh to be written.
301+
* Format of the vector is
302+
* [f_1, f_2,..., f_m]
303+
* if there are m cells in the mesh
304+
*/
305+
template <typename T>
306+
void add_cell_scalar_field(const std::string &name,
307+
const std::vector<T> &data);
308+
309+
/**
310+
* Add a vector field to the mesh
311+
* const string& name name of the field to store vtk mesh
312+
* const vector<double>& data list of field values. There must be dimension
313+
* values for each point in the mesh to be written.
314+
* Format of the vector is
315+
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
316+
* ...
317+
* f_{n,1}, f_{n,2},..., f_{n, dimension}]
318+
* if there are n points in the mesh
319+
* const int dimension ambient dimension (2D or 3D)
320+
*/
321+
template <typename T>
322+
void add_vector_field(const std::string &name,
323+
const std::vector<T> &data,
324+
const int &dimension);
325+
326+
/**
327+
* Add a vector field to cells/elements of the mesh
328+
* const string& name name of the field to store vtk mesh
329+
* const vector<double>& data list of field values. There must be dimension
330+
* values for each cell in the mesh to be written.
331+
* Format of the vector is
332+
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
333+
* ...
334+
* f_{m,1}, f_{m,2},..., f_{m, dimension}]
335+
* if there are m bool binary = falsecells in the mesh
336+
* const int dimension ambient dimension (2D or 3D)
337+
*/
338+
template <typename T>
339+
void add_cell_vector_field(const std::string &name,
340+
const std::vector<T> &data,
341+
const int &dimension);
342+
343+
// Remove all fields and initialized data from the writer.
344+
void clear();
234345

235346
/**
236347
* Write surface mesh to a file
@@ -377,116 +488,7 @@ class DECLDIR VTUWriter {
377488
*/
378489
bool write_point_cloud(std::ostream &os,
379490
const size_t dim,
380-
const std::vector<double> &points);
381-
382-
/**
383-
* Add a general field to the mesh
384-
* const string& name name of the field to store vtk mesh
385-
* const vector<double>& data list of field values. There must be dimension
386-
* values for each point in the mesh to be written.
387-
* Format of the vector is
388-
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
389-
* ...
390-
* f_{n,1}, f_{n,2},..., f_{n, dimension}]
391-
* if there are n points in the mesh
392-
* const int dimension ambient dimension (2D or 3D)
393-
*/
394-
template <typename T>
395-
inline void add_field(const std::string &name,
396-
const std::vector<T> &data,
397-
const int &dimension)
398-
{
399-
if (dimension == 1)
400-
add_scalar_field<T>(name, data);
401-
else
402-
add_vector_field<T>(name, data, dimension);
403-
}
404-
405-
/**
406-
* Add a general cell/element field to the mesh
407-
* const string& name name of the field to store vtk mesh
408-
* const vector<double>& data list of field values. There must be dimension
409-
* values for each cell in the mesh to be written.
410-
* Format of the vector is
411-
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
412-
* ...
413-
* f_{m,1}, f_{m,2},..., f_{m, dimension}]
414-
* if there are m cells in the mesh
415-
* const int dimension ambient dimension (2D or 3D)
416-
*/
417-
template <typename T>
418-
void add_cell_field(const std::string &name,
419-
const std::vector<T> &data,
420-
const int &dimension)
421-
{
422-
if (dimension == 1)
423-
add_cell_scalar_field<T>(name, data);
424-
else
425-
add_cell_vector_field<T>(name, data, dimension);
426-
}
427-
428-
/**
429-
* Add a scalar field to the mesh
430-
* const string& name name of the field to store vtk mesh
431-
* const vector<double>& data list of field values. There must be one
432-
* value for each point in the mesh to be written.
433-
* Format of the vector is
434-
* [f_1, f_2,..., f_n]
435-
* if there are n points in the mesh
436-
*/
437-
template <typename T>
438-
void add_scalar_field(const std::string &name,
439-
const std::vector<T> &data);
440-
441-
/**
442-
* Add a scalar field to cells/elements of the mesh
443-
* const string& name name of the field to store vtk mesh
444-
* const vector<double>& data list of field values. There must be one
445-
* value for each cell in the mesh to be written.
446-
* Format of the vector is
447-
* [f_1, f_2,..., f_m]
448-
* if there are m cells in the mesh
449-
*/
450-
template <typename T>
451-
void add_cell_scalar_field(const std::string &name,
452-
const std::vector<T> &data);
453-
454-
/**
455-
* Add a vector field to the mesh
456-
* const string& name name of the field to store vtk mesh
457-
* const vector<double>& data list of field values. There must be dimension
458-
* values for each point in the mesh to be written.
459-
* Format of the vector is
460-
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
461-
* ...
462-
* f_{n,1}, f_{n,2},..., f_{n, dimension}]
463-
* if there are n points in the mesh
464-
* const int dimension ambient dimension (2D or 3D)
465-
*/
466-
template <typename T>
467-
void add_vector_field(const std::string &name,
468-
const std::vector<T> &data,
469-
const int &dimension);
470-
471-
/**
472-
* Add a vector field to cells/elements of the mesh
473-
* const string& name name of the field to store vtk mesh
474-
* const vector<double>& data list of field values. There must be dimension
475-
* values for each cell in the mesh to be written.
476-
* Format of the vector is
477-
* [f_{1,1}, f_{1,2},..., f_{1, dimension},
478-
* ...
479-
* f_{m,1}, f_{m,2},..., f_{m, dimension}]
480-
* if there are m bool binary = falsecells in the mesh
481-
* const int dimension ambient dimension (2D or 3D)
482-
*/
483-
template <typename T>
484-
void add_cell_vector_field(const std::string &name,
485-
const std::vector<T> &data,
486-
const int &dimension);
487-
488-
// Remove all fields and initialized data from the writer.
489-
void clear();
491+
const std::vector<double> &points);
490492

491493
/// Set the format to binary
492494
inline void set_binary() { binary_ = true; }
@@ -502,7 +504,7 @@ class DECLDIR VTUWriter {
502504

503505
/// Get the last saved file path (empty string if there is no such a path)
504506
inline std::string filepath() { return path_; }
505-
private:
507+
protected:
506508
std::vector<VTKDataNodeBase*> point_data_;
507509
std::vector<VTKDataNodeBase*> cell_data_;
508510
std::string current_scalar_point_data_;
@@ -512,38 +514,44 @@ class DECLDIR VTUWriter {
512514
bool binary_;
513515
/// Last saved file path. Not available when using std::ostream to save
514516
std::string path_;
517+
/// Header's piece cells decriptor
518+
std::string cell_type_;
515519

516520
void write_point_data(std::ostream &os);
517521

518522
void write_cell_data(std::ostream &os);
519523

520-
void write_header(const size_t n_vertices, const size_t n_elements,
524+
void write_header(const size_t n_vertices,
525+
const size_t n_elements,
521526
std::ostream &os);
522527

523528
void write_footer(std::ostream &os);
524-
525-
bool write_mesh(std::ostream &os,
526-
const size_t dim,
527-
const size_t cell_size,
528-
const std::vector<double> &points,
529-
const std::vector<size_t> &tets,
530-
bool is_volume_mesh=true);
529+
530+
void write_points(std::ostream &os,
531+
const size_t num_points,
532+
const std::vector<double> &points,
533+
bool is_volume_mesh = true);
534+
535+
virtual bool write_mesh(std::ostream &os,
536+
const size_t dim,
537+
const size_t cell_size,
538+
const std::vector<double> &points,
539+
const std::vector<size_t> &tets,
540+
bool is_volume_mesh=true);
531541

532542
bool write_mesh(const std::string &path,
533543
const size_t dim, const size_t cell_size,
534544
const std::vector<double> &points,
535545
const std::vector<size_t> &tets,
536546
bool is_volume_mesh=true);
537547

538-
void write_points(std::ostream &os,
539-
const size_t num_points,
540-
const std::vector<double> &points,
541-
bool is_volume_mesh = true);
542-
543-
void write_cells(std::ostream &os,
544-
const size_t n_vertices,
545-
const std::vector<size_t> &tets,
546-
bool is_volume_mesh = true);
548+
virtual void write_cells(std::ostream &os,
549+
const size_t n_vertices,
550+
const std::vector<size_t> &tets,
551+
bool is_volume_mesh = true) = 0;
552+
private:
553+
/// The type name used when writing the header
554+
std::string type_name_;
547555

548556
template <typename T>
549557
inline static VTKDataNode<T>* make_data_node(const std::string &name,
@@ -560,19 +568,52 @@ class DECLDIR VTUWriter {
560568
}
561569
};
562570

571+
class DECLDIR VTUWriter final : public VTKWriter {
572+
public:
573+
VTUWriter()
574+
: VTKWriter("UnstructuredGrid")
575+
{}
576+
577+
protected:
578+
void write_cells(std::ostream &os,
579+
const size_t n_vertices,
580+
const std::vector<size_t> &tets,
581+
bool is_volume_mesh = true);
582+
};
583+
584+
class DECLDIR VTPWriter final : public VTKWriter {
585+
public:
586+
VTPWriter()
587+
: VTKWriter("PolyData")
588+
{}
589+
590+
protected:
591+
bool write_mesh(std::ostream &os,
592+
const size_t dim,
593+
const size_t cell_size,
594+
const std::vector<double> &points,
595+
const std::vector<size_t> &tets,
596+
bool is_volume_mesh=true);
597+
598+
void write_cells(std::ostream &os,
599+
const size_t n_vertices,
600+
const std::vector<size_t> &tets,
601+
bool is_volume_mesh = true);
602+
};
603+
563604
/** @brief Write a multiblock .vtm file on top of the already written VTUs
564605
* @param path The output file path
565606
* @param vtus The list of VTU files
566607
*/
567608
bool DECLDIR write_vtm(const std::string &path,
568-
std::vector<VTUWriter> vtus);
609+
std::vector<VTKWriter*> vtks);
569610

570611
/** @brief Write a multiblock .vtm file on top of the already written VTUs
571612
* @param path The output file path
572-
* @param vtus The list of VTU files
613+
* @param vtks The list of VTU files
573614
*/
574615
bool DECLDIR write_vtm(std::ostream &os,
575-
std::vector<VTUWriter> vtus);
616+
std::vector<VTKWriter*> vtks);
576617

577618
} // namespace leanvtk
578619

0 commit comments

Comments
 (0)