Blender V5.0
ply_file_buffer.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_string_ref.hh"
12#include "BLI_utility_mixins.hh"
13#include "BLI_vector.hh"
14
15/* SEP macro from BLI path utils clashes with SEP symbol in fmt headers. */
16#undef SEP
17#include <fmt/format.h>
18
19namespace blender::io::ply {
20
28class FileBuffer : private NonMovable {
29 using VectorChar = Vector<char>;
30 Vector<VectorChar> blocks_;
31 size_t buffer_chunk_size_;
32 const char *filepath_;
33 FILE *outfile_;
34
35 public:
36 FileBuffer(const char *filepath, size_t buffer_chunk_size = 64 * 1024);
37
38 virtual ~FileBuffer() = default;
39
40 /* Write contents to the buffer(s) into a file, and clear the buffers. */
41 void write_to_file();
42
43 void close_file();
44
45 virtual void write_vertex(float x, float y, float z) = 0;
46
47 virtual void write_UV(float u, float v) = 0;
48
49 virtual void write_data(float v) = 0;
50
51 virtual void write_vertex_normal(float nx, float ny, float nz) = 0;
52
53 virtual void write_vertex_color(uchar r, uchar g, uchar b, uchar a) = 0;
54
55 virtual void write_vertex_end() = 0;
56
57 virtual void write_face(char count, Span<uint32_t> const &vertex_indices) = 0;
58
59 virtual void write_edge(int first, int second) = 0;
60
62
64
66
67 void write_string(StringRef s);
68
69 void write_newline();
70
71 protected:
72 /* Ensure the last block contains at least this amount of free space.
73 * If not, add a new block with max of block size & the amount of space needed. */
74 void ensure_space(size_t at_least)
75 {
76 if (blocks_.is_empty() || (blocks_.last().capacity() - blocks_.last().size() < at_least)) {
77 blocks_.append(VectorChar());
78 blocks_.last().reserve(std::max(at_least, buffer_chunk_size_));
79 }
80 }
81
82 template<typename... T> void write_fstring(fmt::format_string<T...> fmt, T &&...args)
83 {
84 /* Format into a local buffer. */
85 fmt::memory_buffer buf;
86 fmt::format_to(fmt::appender(buf), fmt, std::forward<T>(args)...);
87 size_t len = buf.size();
89 VectorChar &bb = blocks_.last();
90 bb.insert(bb.end(), buf.begin(), buf.end());
91 }
92
93 void write_bytes(Span<char> bytes);
94};
95
96} // namespace blender::io::ply
unsigned char uchar
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
NonMovable(NonMovable &&other)=delete
void insert(const int64_t insert_index, const T &value)
virtual void write_data(float v)=0
virtual void write_vertex_normal(float nx, float ny, float nz)=0
void ensure_space(size_t at_least)
virtual void write_vertex_color(uchar r, uchar g, uchar b, uchar a)=0
virtual void write_vertex(float x, float y, float z)=0
virtual void write_UV(float u, float v)=0
virtual void write_edge(int first, int second)=0
virtual void write_vertex_end()=0
void write_header_scalar_property(StringRef dataType, StringRef name)
void write_bytes(Span< char > bytes)
void write_header_list_property(StringRef countType, StringRef dataType, StringRef name)
void write_fstring(fmt::format_string< T... > fmt, T &&...args)
virtual void write_face(char count, Span< uint32_t > const &vertex_indices)=0
FileBuffer(const char *filepath, size_t buffer_chunk_size=64 *1024)
void write_header_element(StringRef name, int count)
virtual ~FileBuffer()=default
int count
#define T
const char * name
uint len