54 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
56 auto new_geometry = [&]() {
57 r_all_geometries.append(std::make_unique<Geometry>());
58 Geometry *g = r_all_geometries.last().get();
68 return new_geometry();
83 return new_geometry();
91 r_global_vertices.
vertices.append(vert);
98 if (srgb.x >= 0 && srgb.y >= 0 && srgb.z >= 0) {
103 else if (srgb.x > 0) {
117 const int mrgb_length = 8;
118 while (p + mrgb_length <= end) {
120 std::from_chars_result res = std::from_chars(p, p + mrgb_length, value, 16);
121 if (
ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
125 srgb[0] = (value >> 16) & 0xFF;
126 srgb[1] = (value >> 8) & 0xFF;
127 srgb[2] = value & 0xFF;
132 r_global_vertices.
mrgb_block.append(
float3(linear[0], linear[1], linear[2]));
169 r_index += r_index < 0 ? n_elems : -1;
170 if (r_index < 0 || r_index >= n_elems) {
171 CLOG_WARN(&
LOG,
"Invalid vertex index %i (valid range [0, %zu))", r_index, n_elems);
192 int last_vertex_index;
215 geom->
edges_.append({last_vertex_index, vertex_index});
217 last_vertex_index = vertex_index;
225 const int material_index,
226 const int group_index,
227 const bool shaded_smooth)
232 if (group_index >= 0) {
240 bool face_valid =
true;
242 while (p < end && face_valid) {
244 bool got_uv =
false, got_normal =
false;
254 if (p < end && *p ==
'/') {
257 if (p < end && *p !=
'/') {
262 if (p < end && *p ==
'/') {
272 "Invalid vertex index %i (valid range [0, %zu)), ignoring face",
274 size_t(global_vertices.
vertices.size()));
281 if (got_uv && !global_vertices.
uv_vertices.is_empty()) {
285 "Invalid UV index %i (valid range [0, %zu)), ignoring face",
294 if (got_normal && !global_vertices.
vert_normals.is_empty()) {
302 "Invalid normal index %i (valid range [0, %zu)), ignoring face",
332 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
335 if (!
StringRef(p, end).startswith(
"bspline") && !
StringRef(p, end).startswith(
"rat bspline")) {
336 CLOG_WARN(&
LOG,
"Curve type not supported: '%s'",
string(p, end).c_str());
380 CLOG_WARN(&
LOG,
"OBJ curve surfaces are not supported, found '%c'", *p);
400 if (rest_line.
find(
"off") != string::npos || rest_line.
find(
"null") != string::npos ||
401 rest_line.
find(
"default") != string::npos)
407 r_group_name = rest_line;
416 r_state_shaded_smooth =
false;
422 r_state_shaded_smooth =
smooth != 0;
427 bool &r_state_shaded_smooth,
428 string &r_state_group_name,
429 int &r_state_material_index,
431 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
433 r_state_shaded_smooth =
false;
434 r_state_group_name =
"";
438 r_state_material_index = -1;
445 const int obj_file =
BLI_open(import_params_.filepath,
O_BINARY | O_RDONLY, 0);
446 if (obj_file == -1) {
447 CLOG_ERROR(&
LOG,
"Cannot read from OBJ file:'%s'.", import_params_.filepath);
450 "OBJ Import: Cannot open file '%s'",
451 import_params_.filepath);
456 if (mmap_file_ ==
nullptr) {
457 CLOG_ERROR(&
LOG,
"Cannot mmap OBJ file:'%s'.", import_params_.filepath);
460 "OBJ Import: Cannot mmap file '%s'",
461 import_params_.filepath);
468 if (mmap_file_ !=
nullptr) {
476 const size_t keyword_len = keyword.
size();
477 if (end - p < keyword_len + 1) {
480 if (memcmp(p, keyword.
data(), keyword_len) != 0) {
485 if (p[keyword_len] >
' ') {
488 p += keyword_len + 1;
495 const Span<std::unique_ptr<Geometry>> all_geometries,
499 if (std::all_of(all_geometries.begin(),
500 all_geometries.end(),
501 [](
const std::unique_ptr<Geometry> &g) { return g->get_vertex_count() == 0; }))
513 const char *start = buffer.
begin();
514 const char *end = buffer.
end();
515 const char *ptr = start;
518 while (ptr < end && *ptr !=
'\n' && *ptr !=
'\\') {
523 if (ptr >= end || *ptr ==
'\n') {
524 size_t len = ptr - start;
525 buffer =
StringRef(ptr < end ? ptr + 1 : ptr, end);
532 line_buffer_.assign(start, ptr);
538 const char *ahead = ptr;
539 while (ahead < end && *ahead <=
' ' && *ahead !=
'\n') {
542 if (ahead < end && *ahead ==
'\n') {
552 else if (c ==
'\n') {
564void OBJParser::parse_string_buffer(StringRef &buffer_str,
565 Vector<std::unique_ptr<Geometry>> &r_all_geometries,
571 bool state_shaded_smooth =
false;
572 string state_group_name;
573 int state_group_index = -1;
574 string state_material_name;
575 int state_material_index = -1;
577 while (!buffer_str.is_empty()) {
578 StringRef line = read_next_obj_line(buffer_str);
579 const char *p = line.begin(), *end = line.end();
600 if (state_material_index == -1 && !state_material_name.empty() &&
601 curr_geom->material_indices_.is_empty())
603 curr_geom->material_indices_.add_new(state_material_name, 0);
604 curr_geom->material_order_.append(state_material_name);
605 state_material_index = 0;
612 state_material_index,
614 state_shaded_smooth);
622 if (import_params_.use_split_objects) {
627 state_material_index,
634 if (import_params_.use_split_groups) {
639 state_material_index,
645 int new_index = curr_geom->group_indices_.size();
646 state_group_index = curr_geom->group_indices_.lookup_or_add(state_group_name, new_index);
647 if (new_index == state_group_index) {
648 curr_geom->group_order_.append(state_group_name);
658 state_material_name =
StringRef(p, end).trim();
659 int new_mat_index = curr_geom->material_indices_.size();
660 state_material_index = curr_geom->material_indices_.lookup_or_add(state_material_name,
662 if (new_mat_index == state_material_index) {
663 curr_geom->material_order_.append(state_material_name);
667 add_mtl_library(
StringRef(p, end).trim());
673 else if (*p ==
'#') {
689 else if (
StringRef(p, end).startswith(
"end")) {
693 CLOG_WARN(&
LOG,
"OBJ element not recognized: '%s'",
string(p, end).c_str());
715 OBJParser::parse_string_buffer(buffer_str, r_all_geometries, r_global_vertices, curr_geom);
719 add_default_mtl_library();
796 "Only the 'sphere' MTL projection type is supported, found: '%s'",
797 string(line).c_str());
806 for (
int i = 0; i < opt.second; ++i) {
820 const char *mtl_dir_path)
826 if (!is_map && !is_refl && !is_bump) {
832 CLOG_WARN(&
LOG,
"MTL texture map type not supported: '%s'",
string(line).c_str());
848 return mtl_libraries_;
851void OBJParser::add_mtl_library(
StringRef path)
858 if (!mtl_libraries_.contains(path)) {
859 mtl_libraries_.append(path);
863void OBJParser::add_default_mtl_library()
876 add_mtl_library(mtl_file_base);
896 if (buffer ==
nullptr) {
897 CLOG_ERROR(&
LOG,
"OBJ import: cannot read from MTL file: '%s'", mtl_file_path_);
906 const char *p = line.
begin(), *end = line.
end();
916 r_materials.lookup_or_add(
string(mat_name), std::make_unique<MTLMaterial>()).get();
918 else if (material !=
nullptr) {
File and directory operations.
#define BLI_path_join(...)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_suffix(int64_t n) const
void append(const T &value)
constexpr StringRef trim() const
constexpr const char * end() const
constexpr bool startswith(StringRef prefix) const
constexpr const char * begin() const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr const char * data() const
constexpr bool endswith(StringRef suffix) const
void parse_and_store(Map< std::string, std::unique_ptr< MTLMaterial > > &r_materials)
MTLParser(StringRefNull mtl_library_, StringRefNull obj_filepath)
OBJParser(const OBJImportParams &import_params)
Span< std::string > mtl_libraries() const
void parse(Vector< std::unique_ptr< Geometry > > &r_all_geometries, GlobalVertices &r_global_vertices)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
static void use_all_vertices_if_no_faces(Geometry *geom, const Span< std::unique_ptr< Geometry > > all_geometries, const GlobalVertices &global_vertices)
static void geom_add_curve_vertex_indices(Geometry *geom, const char *p, const char *end, const GlobalVertices &global_vertices)
static void geom_add_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void geom_add_mrgb_colors(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void parse_texture_map(const char *p, const char *end, MTLMaterial *material, const char *mtl_dir_path)
static void geom_add_uv_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
static const char * parse_vertex_index(const char *p, const char *end, size_t n_elems, int &r_index)
static MTLTexMapType mtl_line_start_to_texture_type(const char *&p, const char *end)
static void geom_add_polyline(Geometry *geom, const char *p, const char *end, GlobalVertices &r_global_vertices)
static bool parse_keyword(const char *&p, const char *end, StringRef keyword)
static bool parse_texture_option(const char *&p, const char *end, MTLMaterial *material, MTLTexMap &tex_map)
static void geom_add_vertex_normal(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void geom_add_curve_parameters(Geometry *geom, const char *p, const char *end)
static void geom_set_curve_degree(Geometry *geom, const char *p, const char *end)
static void geom_update_smooth_group(const char *p, const char *end, bool &r_state_shaded_smooth)
static void geom_new_object(const char *p, const char *end, bool &r_state_shaded_smooth, string &r_state_group_name, int &r_state_material_index, Geometry *&r_curr_geom, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
static void geom_update_group(const StringRef rest_line, string &r_group_name)
static Geometry * geom_set_curve_type(Geometry *geom, const char *p, const char *end, const StringRef group_name, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
static const std::pair< StringRef, int > unsupported_texture_options[]
static void geom_add_polygon(Geometry *geom, const char *p, const char *end, const GlobalVertices &global_vertices, const int material_index, const int group_index, const bool shaded_smooth)
static Geometry * create_geometry(Geometry *const prev_geometry, const eGeometryType new_type, StringRef name, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
bool index_in_range(const int64_t index, const int64_t size)
const char * parse_float(const char *p, const char *end, float fallback, float &dst, bool skip_space, bool require_trailing_space)
const char * parse_floats(const char *p, const char *end, float fallback, float *dst, int count, bool require_trailing_space)
const char * drop_non_whitespace(const char *p, const char *end)
StringRef read_next_line(StringRef &buffer)
const char * parse_int(const char *p, const char *end, int fallback, int &dst, bool skip_space)
const char * drop_whitespace(const char *p, const char *end)
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
char * STRNCPY(char(&dst)[N], const char *src)
size_t BLI_mmap_get_length(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
BLI_mmap_file * BLI_mmap_open(int fd) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_mmap_free(BLI_mmap_file *file) ATTR_NONNULL(1)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
VecBase< float, 2 > float2
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
MINLINE float normalize_v3(float n[3])
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void * BLI_mmap_get_pointer(BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
VecBase< float, 3 > float3
void track_all_vertices(int count)
Vector< FaceCorner > face_corners_
NurbsElement nurbs_element_
void track_vertex_index(int index)
std::string geometry_name_
Vector< FaceElem > face_elements_
Vector< float3 > vertices
void set_vertex_weight(int64_t index, float weight)
Vector< float2 > uv_vertices
void set_vertex_color(int64_t index, float3 color)
Vector< float3 > vert_normals
Vector< float3 > mrgb_block
const MTLTexMap & tex_map_of_type(MTLTexMapType key) const
Vector< int > curv_indices