forked from blender/blender
index-of-nearest-104619 #2
@ -203,21 +203,19 @@ void importer_main(Main *bmain,
|
||||
}
|
||||
|
||||
/* Create mesh and do all prep work. */
|
||||
Mesh *mesh = BKE_mesh_add(bmain, ob_name);
|
||||
Mesh *mesh_in_main = BKE_mesh_add(bmain, ob_name);
|
||||
BKE_view_layer_base_deselect_all(scene, view_layer);
|
||||
LayerCollection *lc = BKE_layer_collection_get_active(view_layer);
|
||||
Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name);
|
||||
BKE_mesh_assign_object(bmain, obj, mesh);
|
||||
BKE_mesh_assign_object(bmain, obj, mesh_in_main);
|
||||
BKE_collection_object_add(bmain, lc->collection, obj);
|
||||
BKE_view_layer_synced_ensure(scene, view_layer);
|
||||
Base *base = BKE_view_layer_base_find(view_layer, obj);
|
||||
BKE_view_layer_base_select_and_set_active(view_layer, base);
|
||||
|
||||
/* Stuff ply data into the mesh. */
|
||||
Mesh *temp_val = convert_ply_to_mesh(*data, mesh, import_params);
|
||||
if (import_params.merge_verts && temp_val != mesh) {
|
||||
BKE_mesh_nomain_to_mesh(temp_val, mesh, obj);
|
||||
}
|
||||
Mesh *mesh = convert_ply_to_mesh(*data, import_params);
|
||||
BKE_mesh_nomain_to_mesh(mesh, mesh_in_main, obj);
|
||||
|
||||
/* Object matrix and finishing up. */
|
||||
float global_scale = import_params.global_scale;
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "BKE_attribute.h"
|
||||
#include "BKE_attribute.hh"
|
||||
#include "BKE_customdata.h"
|
||||
#include "BKE_lib_id.h"
|
||||
#include "BKE_mesh.hh"
|
||||
#include "BKE_mesh_runtime.h"
|
||||
|
||||
@ -17,23 +18,18 @@
|
||||
#include "ply_import_mesh.hh"
|
||||
|
||||
namespace blender::io::ply {
|
||||
Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams ¶ms)
|
||||
Mesh *convert_ply_to_mesh(PlyData &data, const PLYImportParams ¶ms)
|
||||
{
|
||||
Mesh *mesh = BKE_mesh_new_nomain(
|
||||
data.vertices.size(), data.edges.size(), data.face_vertices.size(), data.face_sizes.size());
|
||||
|
||||
/* Add vertices to the mesh. */
|
||||
mesh->totvert = int(data.vertices.size());
|
||||
CustomData_add_layer_named(
|
||||
&mesh->vdata, CD_PROP_FLOAT3, CD_CONSTRUCT, mesh->totvert, "position");
|
||||
mesh->vert_positions_for_write().copy_from(data.vertices);
|
||||
|
||||
bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
|
||||
|
||||
if (!data.edges.is_empty()) {
|
||||
mesh->totedge = int(data.edges.size());
|
||||
CustomData_add_layer_named(
|
||||
&mesh->edata, CD_PROP_INT32_2D, CD_CONSTRUCT, mesh->totedge, ".edge_verts");
|
||||
MutableSpan<int2> edges = mesh->edges_for_write();
|
||||
for (int i = 0; i < mesh->totedge; i++) {
|
||||
for (const int i : data.edges.index_range()) {
|
||||
int32_t v1 = data.edges[i].first;
|
||||
int32_t v2 = data.edges[i].second;
|
||||
if (v1 >= mesh->totvert) {
|
||||
@ -50,18 +46,12 @@ Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams ¶
|
||||
|
||||
/* Add faces to the mesh. */
|
||||
if (!data.face_sizes.is_empty()) {
|
||||
/* Create poly and loop layers. */
|
||||
mesh->totpoly = int(data.face_sizes.size());
|
||||
mesh->totloop = int(data.face_vertices.size());
|
||||
BKE_mesh_poly_offsets_ensure_alloc(mesh);
|
||||
CustomData_add_layer_named(
|
||||
&mesh->ldata, CD_PROP_INT32, CD_CONSTRUCT, mesh->totloop, ".corner_vert");
|
||||
MutableSpan<int> poly_offsets = mesh->poly_offsets_for_write();
|
||||
MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
|
||||
|
||||
/* Fill in face data. */
|
||||
uint32_t offset = 0;
|
||||
for (int i = 0; i < mesh->totpoly; i++) {
|
||||
for (const int i : data.face_sizes.index_range()) {
|
||||
uint32_t size = data.face_sizes[i];
|
||||
poly_offsets[i] = offset;
|
||||
for (int j = 0; j < size; j++) {
|
||||
@ -83,12 +73,12 @@ Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams ¶
|
||||
attributes.lookup_or_add_for_write_span<ColorGeometry4f>("Col", ATTR_DOMAIN_POINT);
|
||||
|
||||
if (params.vertex_colors == PLY_VERTEX_COLOR_SRGB) {
|
||||
for (int i = 0; i < data.vertex_colors.size(); i++) {
|
||||
for (const int i : data.vertex_colors.index_range()) {
|
||||
srgb_to_linearrgb_v4(colors.span[i], data.vertex_colors[i]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < data.vertex_colors.size(); i++) {
|
||||
for (const int i : data.vertex_colors.index_range()) {
|
||||
copy_v4_v4(colors.span[i], data.vertex_colors[i]);
|
||||
}
|
||||
}
|
||||
@ -101,7 +91,7 @@ Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams ¶
|
||||
if (!data.uv_coordinates.is_empty()) {
|
||||
bke::SpanAttributeWriter<float2> uv_map = attributes.lookup_or_add_for_write_only_span<float2>(
|
||||
"UVMap", ATTR_DOMAIN_CORNER);
|
||||
for (size_t i = 0; i < data.face_vertices.size(); i++) {
|
||||
for (const int i : data.face_vertices.index_range()) {
|
||||
uv_map.span[i] = data.uv_coordinates[data.face_vertices[i]];
|
||||
}
|
||||
uv_map.finish();
|
||||
@ -116,17 +106,18 @@ Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams ¶
|
||||
mesh, reinterpret_cast<float(*)[3]>(data.vertex_normals.data()));
|
||||
}
|
||||
|
||||
BKE_mesh_smooth_flag_set(mesh, false);
|
||||
|
||||
/* Merge all vertices on the same location. */
|
||||
if (params.merge_verts) {
|
||||
std::optional<Mesh *> return_value = blender::geometry::mesh_merge_by_distance_all(
|
||||
std::optional<Mesh *> merged_mesh = blender::geometry::mesh_merge_by_distance_all(
|
||||
*mesh, IndexMask(mesh->totvert), 0.0001f);
|
||||
if (return_value.has_value()) {
|
||||
mesh = return_value.value();
|
||||
if (merged_mesh) {
|
||||
BKE_id_free(nullptr, &mesh->id);
|
||||
mesh = *merged_mesh;
|
||||
}
|
||||
}
|
||||
|
||||
BKE_mesh_smooth_flag_set(mesh, false);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
} // namespace blender::io::ply
|
||||
|
@ -13,9 +13,8 @@ namespace blender::io::ply {
|
||||
|
||||
/**
|
||||
* Converts the #PlyData data-structure to a mesh.
|
||||
* \param data: The PLY data.
|
||||
* \return The mesh that can be used inside blender.
|
||||
* \return A new mesh that can be used inside blender.
|
||||
*/
|
||||
Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams ¶ms);
|
||||
Mesh *convert_ply_to_mesh(PlyData &data, const PLYImportParams ¶ms);
|
||||
|
||||
} // namespace blender::io::ply
|
||||
|
@ -78,13 +78,9 @@ void importer_main(Main *bmain,
|
||||
BLI_strncpy(ob_name, BLI_path_basename(import_params.filepath), FILE_MAX);
|
||||
BLI_path_extension_strip(ob_name);
|
||||
|
||||
Mesh *mesh = nullptr;
|
||||
if (is_ascii_stl) {
|
||||
mesh = read_stl_ascii(import_params.filepath, bmain, ob_name, import_params.use_facet_normal);
|
||||
}
|
||||
else {
|
||||
mesh = read_stl_binary(file, bmain, ob_name, import_params.use_facet_normal);
|
||||
}
|
||||
Mesh *mesh = is_ascii_stl ?
|
||||
read_stl_ascii(import_params.filepath, import_params.use_facet_normal) :
|
||||
read_stl_binary(file, import_params.use_facet_normal);
|
||||
|
||||
if (mesh == nullptr) {
|
||||
fprintf(stderr, "STL Importer: Failed to import mesh '%s'\n", import_params.filepath);
|
||||
@ -99,10 +95,12 @@ void importer_main(Main *bmain,
|
||||
BKE_mesh_validate(mesh, verbose_validate, false);
|
||||
}
|
||||
|
||||
Mesh *mesh_in_main = BKE_mesh_add(bmain, ob_name);
|
||||
BKE_mesh_nomain_to_mesh(mesh, mesh_in_main, nullptr);
|
||||
BKE_view_layer_base_deselect_all(scene, view_layer);
|
||||
LayerCollection *lc = BKE_layer_collection_get_active(view_layer);
|
||||
Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name);
|
||||
BKE_mesh_assign_object(bmain, obj, mesh);
|
||||
BKE_mesh_assign_object(bmain, obj, mesh_in_main);
|
||||
BKE_collection_object_add(bmain, lc->collection, obj);
|
||||
BKE_view_layer_synced_ensure(scene, view_layer);
|
||||
Base *base = BKE_view_layer_base_find(view_layer, obj);
|
||||
|
@ -111,13 +111,13 @@ static inline void parse_float3(StringBuffer &buf, float out[3])
|
||||
}
|
||||
}
|
||||
|
||||
Mesh *read_stl_ascii(const char *filepath, Main *bmain, char *mesh_name, bool use_custom_normals)
|
||||
Mesh *read_stl_ascii(const char *filepath, const bool use_custom_normals)
|
||||
{
|
||||
size_t buffer_len;
|
||||
void *buffer = BLI_file_read_text_as_mem(filepath, 0, &buffer_len);
|
||||
if (buffer == nullptr) {
|
||||
fprintf(stderr, "STL Importer: cannot read from ASCII STL file: '%s'\n", filepath);
|
||||
return BKE_mesh_add(bmain, mesh_name);
|
||||
return nullptr;
|
||||
}
|
||||
BLI_SCOPED_DEFER([&]() { MEM_freeN(buffer); });
|
||||
|
||||
@ -154,7 +154,7 @@ Mesh *read_stl_ascii(const char *filepath, Main *bmain, char *mesh_name, bool us
|
||||
}
|
||||
}
|
||||
|
||||
return stl_mesh.to_mesh(bmain, mesh_name);
|
||||
return stl_mesh.to_mesh();
|
||||
}
|
||||
|
||||
} // namespace blender::io::stl
|
||||
|
@ -30,6 +30,6 @@
|
||||
|
||||
namespace blender::io::stl {
|
||||
|
||||
Mesh *read_stl_ascii(const char *filepath, Main *bmain, char *mesh_name, bool use_custom_normals);
|
||||
Mesh *read_stl_ascii(const char *filepath, bool use_custom_normals);
|
||||
|
||||
} // namespace blender::io::stl
|
||||
|
@ -29,7 +29,7 @@ struct STLBinaryTriangle {
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
Mesh *read_stl_binary(FILE *file, Main *bmain, char *mesh_name, bool use_custom_normals)
|
||||
Mesh *read_stl_binary(FILE *file, const bool use_custom_normals)
|
||||
{
|
||||
const int chunk_size = 1024;
|
||||
uint32_t num_tris = 0;
|
||||
@ -40,7 +40,7 @@ Mesh *read_stl_binary(FILE *file, Main *bmain, char *mesh_name, bool use_custom_
|
||||
}
|
||||
|
||||
if (num_tris == 0) {
|
||||
return BKE_mesh_add(bmain, mesh_name);
|
||||
return BKE_mesh_new_nomain(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
Array<STLBinaryTriangle> tris_buf(chunk_size);
|
||||
@ -57,7 +57,7 @@ Mesh *read_stl_binary(FILE *file, Main *bmain, char *mesh_name, bool use_custom_
|
||||
}
|
||||
}
|
||||
|
||||
return stl_mesh.to_mesh(bmain, mesh_name);
|
||||
return stl_mesh.to_mesh();
|
||||
}
|
||||
|
||||
} // namespace blender::io::stl
|
||||
|
@ -26,6 +26,6 @@ namespace blender::io::stl {
|
||||
const size_t BINARY_HEADER_SIZE = 80;
|
||||
const size_t BINARY_STRIDE = 12 * 4 + 2;
|
||||
|
||||
Mesh *read_stl_binary(FILE *file, Main *bmain, char *mesh_name, bool use_custom_normals);
|
||||
Mesh *read_stl_binary(FILE *file, bool use_custom_normals);
|
||||
|
||||
} // namespace blender::io::stl
|
||||
|
@ -60,7 +60,7 @@ void STLMeshHelper::add_triangle(const float3 &a,
|
||||
}
|
||||
}
|
||||
|
||||
Mesh *STLMeshHelper::to_mesh(Main *bmain, char *mesh_name)
|
||||
Mesh *STLMeshHelper::to_mesh()
|
||||
{
|
||||
if (degenerate_tris_num_ > 0) {
|
||||
std::cout << "STL Importer: " << degenerate_tris_num_ << " degenerate triangles were removed"
|
||||
@ -71,21 +71,10 @@ Mesh *STLMeshHelper::to_mesh(Main *bmain, char *mesh_name)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
Mesh *mesh = BKE_mesh_add(bmain, mesh_name);
|
||||
/* User count is already 1 here, but will be set later in #BKE_mesh_assign_object. */
|
||||
id_us_min(&mesh->id);
|
||||
Mesh *mesh = BKE_mesh_new_nomain(verts_.size(), 0, tris_.size() * 3, tris_.size());
|
||||
|
||||
mesh->totvert = verts_.size();
|
||||
CustomData_add_layer_named(
|
||||
&mesh->vdata, CD_PROP_FLOAT3, CD_CONSTRUCT, mesh->totvert, "position");
|
||||
mesh->vert_positions_for_write().copy_from(verts_);
|
||||
|
||||
mesh->totpoly = tris_.size();
|
||||
mesh->totloop = tris_.size() * 3;
|
||||
BKE_mesh_poly_offsets_ensure_alloc(mesh);
|
||||
CustomData_add_layer_named(
|
||||
&mesh->ldata, CD_PROP_INT32, CD_SET_DEFAULT, mesh->totloop, ".corner_vert");
|
||||
|
||||
MutableSpan<int> poly_offsets = mesh->poly_offsets_for_write();
|
||||
threading::parallel_for(poly_offsets.index_range(), 4096, [&](const IndexRange range) {
|
||||
for (const int i : range) {
|
||||
|
@ -65,7 +65,7 @@ class STLMeshHelper {
|
||||
const float3 &b,
|
||||
const float3 &c,
|
||||
const float3 &custom_normal);
|
||||
Mesh *to_mesh(Main *bmain, char *mesh_name);
|
||||
Mesh *to_mesh();
|
||||
};
|
||||
|
||||
} // namespace blender::io::stl
|
||||
|
Loading…
Reference in New Issue
Block a user