Blender V4.3
ply_import.cc
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#include "BKE_context.hh"
10#include "BKE_layer.hh"
11#include "BKE_mesh.hh"
12#include "BKE_object.hh"
13#include "BKE_report.hh"
14
16#include "DNA_object_types.h"
17#include "DNA_scene_types.h"
18
19#include "BLI_math_matrix.h"
20#include "BLI_math_rotation.h"
21#include "BLI_math_vector.h"
22#include "BLI_span.hh"
23#include "BLI_string.h"
24
25#include "DEG_depsgraph.hh"
27
28#include "ply_data.hh"
29#include "ply_import.hh"
30#include "ply_import_buffer.hh"
31#include "ply_import_data.hh"
32#include "ply_import_mesh.hh"
33
34namespace blender::io::ply {
35
36/* If line starts with keyword, returns true and drops it from the line. */
37static bool parse_keyword(Span<char> &str, StringRef keyword)
38{
39 const size_t keyword_len = keyword.size();
40 if (str.size() < keyword_len) {
41 return false;
42 }
43 if (memcmp(str.data(), keyword.data(), keyword_len) != 0) {
44 return false;
45 }
46 str = str.drop_front(keyword_len);
47 return true;
48}
49
51{
52 size_t len = 0;
53 while (len < str.size() && str[len] > ' ') {
54 ++len;
55 }
56 Span<char> word(str.begin(), len);
57 str = str.drop_front(len);
58 return word;
59}
60
62{
63 while (!str.is_empty() && str[0] <= ' ') {
64 str = str.drop_front(1);
65 }
66}
67
69{
70 StringRef input(word.data(), word.size());
71 if (ELEM(input, "uchar", "uint8")) {
73 }
74 if (ELEM(input, "char", "int8")) {
75 return PlyDataTypes::CHAR;
76 }
77 if (ELEM(input, "ushort", "uint16")) {
79 }
80 if (ELEM(input, "short", "int16")) {
82 }
83 if (ELEM(input, "uint", "uint32")) {
84 return PlyDataTypes::UINT;
85 }
86 if (ELEM(input, "int", "int32")) {
87 return PlyDataTypes::INT;
88 }
89 if (ELEM(input, "float", "float32")) {
91 }
92 if (ELEM(input, "double", "float64")) {
94 }
95 return PlyDataTypes::NONE;
96}
97
98const char *read_header(PlyReadBuffer &file, PlyHeader &r_header)
99{
100 Span<char> word, line;
101 line = file.read_line();
102 if (StringRef(line.data(), line.size()) != "ply") {
103 return "Invalid PLY header.";
104 }
105
106 while (true) { /* We break when end_header is encountered. */
107 line = file.read_line();
108
109 if (parse_keyword(line, "format")) {
110 skip_space(line);
111 if (parse_keyword(line, "ascii")) {
112 r_header.type = PlyFormatType::ASCII;
113 }
114 else if (parse_keyword(line, "binary_big_endian")) {
116 }
117 else if (parse_keyword(line, "binary_little_endian")) {
119 }
120 }
121 else if (parse_keyword(line, "element")) {
123
124 skip_space(line);
125 word = parse_word(line);
126 element.name = std::string(word.data(), word.size());
127 skip_space(line);
128 word = parse_word(line);
129 element.count = std::stoi(std::string(word.data(), word.size()));
130 r_header.elements.append(element);
131 }
132 else if (parse_keyword(line, "property")) {
133 PlyProperty property;
134 skip_space(line);
135 if (parse_keyword(line, "list")) {
136 skip_space(line);
137 property.count_type = type_from_string(parse_word(line));
138 }
139 skip_space(line);
140 property.type = type_from_string(parse_word(line));
141 skip_space(line);
142 word = parse_word(line);
143 property.name = std::string(word.data(), word.size());
144 r_header.elements.last().properties.append(property);
145 }
146 else if (parse_keyword(line, "end_header")) {
147 break;
148 }
149 else if (line.is_empty() || (line.first() >= '0' && line.first() <= '9') ||
150 line.first() == '-')
151 {
152 /* A value was found before we broke out of the loop. No end_header. */
153 return "No end_header.";
154 }
155 }
156
157 file.after_header(r_header.type != PlyFormatType::ASCII);
158 for (PlyElement &el : r_header.elements) {
159 el.calc_stride();
160 }
161 return nullptr;
162}
163
164static Mesh *read_ply_to_mesh(const PLYImportParams &import_params, const char *ob_name)
165{
166 /* Parse header. */
167 PlyReadBuffer file(import_params.filepath, 64 * 1024);
168
169 PlyHeader header;
170 const char *err = read_header(file, header);
171 if (err != nullptr) {
172 fprintf(stderr, "PLY Importer: %s: %s\n", ob_name, err);
173 BKE_reportf(import_params.reports, RPT_ERROR, "PLY Importer: %s: %s", ob_name, err);
174 return nullptr;
175 }
176
177 /* Parse actual file data. */
178 std::unique_ptr<PlyData> data = import_ply_data(file, header);
179 if (data == nullptr) {
180 fprintf(stderr, "PLY Importer: failed importing %s, unknown error\n", ob_name);
181 BKE_report(import_params.reports, RPT_ERROR, "PLY Importer: failed importing, unknown error");
182 return nullptr;
183 }
184 if (!data->error.empty()) {
185 fprintf(stderr, "PLY Importer: failed importing %s: %s\n", ob_name, data->error.c_str());
186 BKE_report(import_params.reports, RPT_ERROR, "PLY Importer: failed importing, unknown error");
187 return nullptr;
188 }
189 if (data->vertices.is_empty()) {
190 fprintf(stderr, "PLY Importer: file %s contains no vertices\n", ob_name);
191 BKE_report(import_params.reports, RPT_ERROR, "PLY Importer: failed importing, no vertices");
192 return nullptr;
193 }
194
195 return convert_ply_to_mesh(*data, import_params);
196}
197
198Mesh *import_mesh(const PLYImportParams &import_params)
199{
200 /* File base name used for both mesh and object. */
201 char ob_name[FILE_MAX];
202 STRNCPY(ob_name, BLI_path_basename(import_params.filepath));
204
205 /* Stuff ply data into the mesh. */
206 return read_ply_to_mesh(import_params, ob_name);
207}
208
209void importer_main(bContext *C, const PLYImportParams &import_params)
210{
211 Main *bmain = CTX_data_main(C);
212 Scene *scene = CTX_data_scene(C);
213 ViewLayer *view_layer = CTX_data_view_layer(C);
214 importer_main(bmain, scene, view_layer, import_params);
215}
216
217void importer_main(Main *bmain,
218 Scene *scene,
219 ViewLayer *view_layer,
220 const PLYImportParams &import_params)
221{
222 /* File base name used for both mesh and object. */
223 char ob_name[FILE_MAX];
224 STRNCPY(ob_name, BLI_path_basename(import_params.filepath));
226
227 /* Stuff ply data into the mesh. */
228 Mesh *mesh = read_ply_to_mesh(import_params, ob_name);
229
230 if (mesh == nullptr) {
231 return;
232 }
233
234 /* Create mesh and do all prep work. */
235 Mesh *mesh_in_main = BKE_mesh_add(bmain, ob_name);
236 BKE_view_layer_base_deselect_all(scene, view_layer);
238 Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name);
239 obj->data = mesh_in_main;
240 BKE_collection_object_add(bmain, lc->collection, obj);
241 BKE_view_layer_synced_ensure(scene, view_layer);
242 Base *base = BKE_view_layer_base_find(view_layer, obj);
244
245 BKE_mesh_nomain_to_mesh(mesh, mesh_in_main, obj);
246
247 /* Object matrix and finishing up. */
248 float global_scale = import_params.global_scale;
249 if ((scene->unit.system != USER_UNIT_NONE) && import_params.use_scene_unit) {
250 global_scale /= scene->unit.scale_length;
251 }
252 float scale_vec[3] = {global_scale, global_scale, global_scale};
253 float obmat3x3[3][3];
254 unit_m3(obmat3x3);
255 float obmat4x4[4][4];
256 unit_m4(obmat4x4);
257 /* +Y-forward and +Z-up are the Blender's default axis settings. */
259 IO_AXIS_Y, IO_AXIS_Z, import_params.forward_axis, import_params.up_axis, obmat3x3);
260 copy_m4_m3(obmat4x4, obmat3x3);
261 rescale_m4(obmat4x4, scale_vec);
262 BKE_object_apply_mat4(obj, obmat4x4, true, false);
263
267 DEG_id_tag_update_ex(bmain, &obj->id, flags);
270}
271} // namespace blender::io::ply
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
Scene * CTX_data_scene(const bContext *C)
Main * CTX_data_main(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
LayerCollection * BKE_layer_collection_get_active(ViewLayer *view_layer)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
void BKE_view_layer_base_deselect_all(const Scene *scene, ViewLayer *view_layer)
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
void BKE_view_layer_base_select_and_set_active(ViewLayer *view_layer, Base *selbase)
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob)
Mesh * BKE_mesh_add(Main *bmain, const char *name)
General operations, lookup, etc. for blender objects.
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Object * BKE_object_add_only_object(Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
void unit_m3(float m[3][3])
void unit_m4(float m[4][4])
Definition rct.c:1127
void copy_m4_m3(float m1[4][4], const float m2[3][3])
void rescale_m4(float mat[4][4], const float scale[3])
bool mat3_from_axis_conversion(int src_forward, int src_up, int dst_forward, int dst_up, float r_mat[3][3])
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define FILE_MAX
#define STRNCPY(dst, src)
Definition BLI_string.h:593
#define ELEM(...)
void DEG_id_tag_update(ID *id, unsigned int flags)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void DEG_relations_tag_update(Main *bmain)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1021
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1085
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1044
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1041
@ ID_RECALC_BASE_FLAGS
Definition DNA_ID.h:1071
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
@ OB_MESH
@ USER_UNIT_NONE
@ IO_AXIS_Y
@ IO_AXIS_Z
ATTR_WARN_UNUSED_RESULT const void * element
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:172
constexpr const T * data() const
Definition BLI_span.hh:216
constexpr int64_t size() const
Definition BLI_span.hh:253
constexpr int64_t size() const
constexpr const char * data() const
FILE * file
int len
#define str(s)
static PlyDataTypes type_from_string(Span< char > word)
Definition ply_import.cc:68
static void skip_space(Span< char > &str)
Definition ply_import.cc:61
void importer_main(bContext *C, const PLYImportParams &import_params)
std::unique_ptr< PlyData > import_ply_data(PlyReadBuffer &file, PlyHeader &header)
static Mesh * read_ply_to_mesh(const PLYImportParams &import_params, const char *ob_name)
static Span< char > parse_word(Span< char > &str)
Definition ply_import.cc:50
static bool parse_keyword(Span< char > &str, StringRef keyword)
Definition ply_import.cc:37
Mesh * convert_ply_to_mesh(PlyData &data, const PLYImportParams &params)
const char * read_header(PlyReadBuffer &file, PlyHeader &r_header)
Definition ply_import.cc:98
Mesh * import_mesh(const PLYImportParams &import_params)
struct Collection * collection
eIOAxis up_axis
Definition IO_ply.hh:61
bool use_scene_unit
Definition IO_ply.hh:62
ReportList * reports
Definition IO_ply.hh:68
char filepath[FILE_MAX]
Definition IO_ply.hh:59
float global_scale
Definition IO_ply.hh:63
eIOAxis forward_axis
Definition IO_ply.hh:60
Vector< PlyElement > elements
Definition ply_data.hh:58