Blender V5.3
obj_export_io.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 <cstdio>
12
13#include "BLI_string_ref.hh"
14#include "BLI_utility_mixins.hh"
15#include "BLI_vector.hh"
16
17#include <fmt/format.h>
18
19namespace blender::io::obj {
20
29 private:
30 using VectorChar = Vector<char>;
31 Vector<VectorChar> blocks_;
32 size_t buffer_chunk_size_;
33
34 public:
35 FormatHandler(size_t buffer_chunk_size = 64 * 1024) : buffer_chunk_size_(buffer_chunk_size) {}
36
37 /* Write contents to the buffer(s) into a file, and clear the buffers. */
38 void write_to_file(FILE *f)
39 {
40 for (const auto &b : blocks_) {
41 fwrite(b.data(), 1, b.size(), f);
42 }
43 blocks_.clear();
44 }
45
46 std::string get_as_string() const
47 {
48 std::string s;
49 for (const auto &b : blocks_) {
50 s.append(b.data(), b.size());
51 }
52 return s;
53 }
54 size_t get_block_count() const
55 {
56 return blocks_.size();
57 }
58
60 {
61 blocks_.insert(blocks_.end(),
62 std::make_move_iterator(v.blocks_.begin()),
63 std::make_move_iterator(v.blocks_.end()));
64 v.blocks_.clear();
65 }
66
67 void write_obj_vertex(float x, float y, float z)
68 {
69 write_impl("v {:.6f} {:.6f} {:.6f}\n", x, y, z);
70 }
71 void write_obj_vertex_color(float x, float y, float z, float r, float g, float b)
72 {
73 write_impl("v {:.6f} {:.6f} {:.6f} {:.4f} {:.4f} {:.4f}\n", x, y, z, r, g, b);
74 }
75 void write_obj_uv(float x, float y)
76 {
77 write_impl("vt {:.6f} {:.6f}\n", x, y);
78 }
79 void write_obj_normal(float x, float y, float z)
80 {
81 write_impl("vn {:.4f} {:.4f} {:.4f}\n", x, y, z);
82 }
84 {
85 write_impl("f");
86 }
88 {
90 }
91 void write_obj_face_v_uv_normal(int v, int uv, int n)
92 {
93 write_impl(" {}/{}/{}", v, uv, n);
94 }
95 void write_obj_face_v_normal(int v, int n)
96 {
97 write_impl(" {}//{}", v, n);
98 }
99 void write_obj_face_v_uv(int v, int uv)
100 {
101 write_impl(" {}/{}", v, uv);
102 }
104 {
105 write_impl(" {}", v);
106 }
108 {
109 write_impl("usemtl {}\n", s);
110 }
112 {
113 write_impl("mtllib {}\n", s);
114 }
116 {
117 write_impl("s {}\n", s);
118 }
120 {
121 write_impl("g {}\n", s);
122 }
124 {
125 write_impl("o {}\n", s);
126 }
127 void write_obj_edge(int a, int b)
128 {
129 write_impl("l {} {}\n", a, b);
130 }
132 {
133 write_impl("cstype bspline\n");
134 }
136 {
137 write_impl("deg {}\n", deg);
138 }
140 {
141 write_impl("curv");
142 }
144 {
146 }
148 {
149 write_impl("parm u");
150 }
152 {
153 write_impl(" {:.6f}", v);
154 }
156 {
157 write_impl("\n");
158 }
160 {
161 write_impl("end\n");
162 }
164 {
165 write_impl("\n");
166 }
167
169 {
170 write_impl("newmtl {}\n", s);
171 }
172 void write_mtl_float(const char *type, float v)
173 {
174 write_impl("{} {:.6f}\n", type, v);
175 }
176 void write_mtl_float3(const char *type, float r, float g, float b)
177 {
178 write_impl("{} {:.6f} {:.6f} {:.6f}\n", type, r, g, b);
179 }
180 void write_mtl_illum(int mode)
181 {
182 write_impl("illum {}\n", mode);
183 }
184 /* NOTE: options, if present, will have its own leading space. */
185 void write_mtl_map(const char *type, StringRef options, StringRef value)
186 {
187 write_impl("{}{} {}\n", type, options, value);
188 }
189
191 {
192 write_impl("{}\n", s);
193 }
194
195 private:
196 /* Ensure the last block contains at least this amount of free space.
197 * If not, add a new block with max of block size & the amount of space needed. */
198 void ensure_space(size_t at_least)
199 {
200 if (blocks_.is_empty() || (blocks_.last().capacity() - blocks_.last().size() < at_least)) {
201 blocks_.append(VectorChar());
202 blocks_.last().reserve(std::max(at_least, buffer_chunk_size_));
203 }
204 }
205
206 template<typename... T> void write_impl(fmt::format_string<T...> fmt, T &&...args)
207 {
208 /* Format into a local buffer. */
209 fmt::memory_buffer buf;
210 fmt::format_to(fmt::appender(buf), fmt, std::forward<T>(args)...);
211 size_t len = buf.size();
212 ensure_space(len);
213 VectorChar &bb = blocks_.last();
214 bb.insert(bb.end(), buf.begin(), buf.end());
215 }
216};
217
218} // namespace blender::io::obj
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void append(const T &value)
const T & last(const int64_t n=0) const
bool is_empty() const
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
void write_obj_face_v_normal(int v, int n)
void write_obj_normal(float x, float y, float z)
void write_mtl_map(const char *type, StringRef options, StringRef value)
void write_obj_face_v_uv_normal(int v, int uv, int n)
void write_mtl_float3(const char *type, float r, float g, float b)
void write_obj_vertex(float x, float y, float z)
void write_obj_vertex_color(float x, float y, float z, float r, float g, float b)
void write_obj_uv(float x, float y)
void write_obj_face_v_uv(int v, int uv)
std::string get_as_string() const
FormatHandler(size_t buffer_chunk_size=64 *1024)
void write_mtl_float(const char *type, float v)
void append_from(FormatHandler &v)
CCL_NAMESPACE_BEGIN struct Options options
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