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