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