index-of-nearest-104619 #2

Merged
Iliya Katushenock merged 62 commits from HooglyBoogly/blender:index-of-nearest-104619 into index_of_nearest 2023-04-20 21:19:53 +02:00
10 changed files with 38 additions and 63 deletions
Showing only changes of commit 60bb57663a - Show all commits

View File

@ -203,21 +203,19 @@ void importer_main(Main *bmain,
} }
/* Create mesh and do all prep work. */ /* 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); BKE_view_layer_base_deselect_all(scene, view_layer);
LayerCollection *lc = BKE_layer_collection_get_active(view_layer); LayerCollection *lc = BKE_layer_collection_get_active(view_layer);
Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name); 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_collection_object_add(bmain, lc->collection, obj);
BKE_view_layer_synced_ensure(scene, view_layer); BKE_view_layer_synced_ensure(scene, view_layer);
Base *base = BKE_view_layer_base_find(view_layer, obj); Base *base = BKE_view_layer_base_find(view_layer, obj);
BKE_view_layer_base_select_and_set_active(view_layer, base); BKE_view_layer_base_select_and_set_active(view_layer, base);
/* Stuff ply data into the mesh. */ /* Stuff ply data into the mesh. */
Mesh *temp_val = convert_ply_to_mesh(*data, mesh, import_params); Mesh *mesh = convert_ply_to_mesh(*data, import_params);
if (import_params.merge_verts && temp_val != mesh) { BKE_mesh_nomain_to_mesh(mesh, mesh_in_main, obj);
BKE_mesh_nomain_to_mesh(temp_val, mesh, obj);
}
/* Object matrix and finishing up. */ /* Object matrix and finishing up. */
float global_scale = import_params.global_scale; float global_scale = import_params.global_scale;

View File

@ -7,6 +7,7 @@
#include "BKE_attribute.h" #include "BKE_attribute.h"
#include "BKE_attribute.hh" #include "BKE_attribute.hh"
#include "BKE_customdata.h" #include "BKE_customdata.h"
#include "BKE_lib_id.h"
#include "BKE_mesh.hh" #include "BKE_mesh.hh"
#include "BKE_mesh_runtime.h" #include "BKE_mesh_runtime.h"
@ -17,23 +18,18 @@
#include "ply_import_mesh.hh" #include "ply_import_mesh.hh"
namespace blender::io::ply { namespace blender::io::ply {
Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams &params) Mesh *convert_ply_to_mesh(PlyData &data, const PLYImportParams &params)
{ {
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); mesh->vert_positions_for_write().copy_from(data.vertices);
bke::MutableAttributeAccessor attributes = mesh->attributes_for_write(); bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
if (!data.edges.is_empty()) { 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(); 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 v1 = data.edges[i].first;
int32_t v2 = data.edges[i].second; int32_t v2 = data.edges[i].second;
if (v1 >= mesh->totvert) { if (v1 >= mesh->totvert) {
@ -50,18 +46,12 @@ Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams &para
/* Add faces to the mesh. */ /* Add faces to the mesh. */
if (!data.face_sizes.is_empty()) { 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> poly_offsets = mesh->poly_offsets_for_write();
MutableSpan<int> corner_verts = mesh->corner_verts_for_write(); MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
/* Fill in face data. */ /* Fill in face data. */
uint32_t offset = 0; 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]; uint32_t size = data.face_sizes[i];
poly_offsets[i] = offset; poly_offsets[i] = offset;
for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) {
@ -83,12 +73,12 @@ Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams &para
attributes.lookup_or_add_for_write_span<ColorGeometry4f>("Col", ATTR_DOMAIN_POINT); attributes.lookup_or_add_for_write_span<ColorGeometry4f>("Col", ATTR_DOMAIN_POINT);
if (params.vertex_colors == PLY_VERTEX_COLOR_SRGB) { 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]); srgb_to_linearrgb_v4(colors.span[i], data.vertex_colors[i]);
} }
} }
else { 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]); 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 &para
if (!data.uv_coordinates.is_empty()) { if (!data.uv_coordinates.is_empty()) {
bke::SpanAttributeWriter<float2> uv_map = attributes.lookup_or_add_for_write_only_span<float2>( bke::SpanAttributeWriter<float2> uv_map = attributes.lookup_or_add_for_write_only_span<float2>(
"UVMap", ATTR_DOMAIN_CORNER); "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.span[i] = data.uv_coordinates[data.face_vertices[i]];
} }
uv_map.finish(); uv_map.finish();
@ -116,17 +106,18 @@ Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams &para
mesh, reinterpret_cast<float(*)[3]>(data.vertex_normals.data())); mesh, reinterpret_cast<float(*)[3]>(data.vertex_normals.data()));
} }
BKE_mesh_smooth_flag_set(mesh, false);
/* Merge all vertices on the same location. */ /* Merge all vertices on the same location. */
if (params.merge_verts) { 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); *mesh, IndexMask(mesh->totvert), 0.0001f);
if (return_value.has_value()) { if (merged_mesh) {
mesh = return_value.value(); BKE_id_free(nullptr, &mesh->id);
mesh = *merged_mesh;
} }
} }
BKE_mesh_smooth_flag_set(mesh, false);
return mesh; return mesh;
} }
} // namespace blender::io::ply } // namespace blender::io::ply

View File

@ -13,9 +13,8 @@ namespace blender::io::ply {
/** /**
* Converts the #PlyData data-structure to a mesh. * Converts the #PlyData data-structure to a mesh.
* \param data: The PLY data. * \return A new mesh that can be used inside blender.
* \return The mesh that can be used inside blender.
*/ */
Mesh *convert_ply_to_mesh(PlyData &data, Mesh *mesh, const PLYImportParams &params); Mesh *convert_ply_to_mesh(PlyData &data, const PLYImportParams &params);
} // namespace blender::io::ply } // namespace blender::io::ply

View File

@ -78,13 +78,9 @@ void importer_main(Main *bmain,
BLI_strncpy(ob_name, BLI_path_basename(import_params.filepath), FILE_MAX); BLI_strncpy(ob_name, BLI_path_basename(import_params.filepath), FILE_MAX);
BLI_path_extension_strip(ob_name); BLI_path_extension_strip(ob_name);
Mesh *mesh = nullptr; Mesh *mesh = is_ascii_stl ?
if (is_ascii_stl) { read_stl_ascii(import_params.filepath, import_params.use_facet_normal) :
mesh = read_stl_ascii(import_params.filepath, bmain, ob_name, import_params.use_facet_normal); read_stl_binary(file, import_params.use_facet_normal);
}
else {
mesh = read_stl_binary(file, bmain, ob_name, import_params.use_facet_normal);
}
if (mesh == nullptr) { if (mesh == nullptr) {
fprintf(stderr, "STL Importer: Failed to import mesh '%s'\n", import_params.filepath); 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); 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); BKE_view_layer_base_deselect_all(scene, view_layer);
LayerCollection *lc = BKE_layer_collection_get_active(view_layer); LayerCollection *lc = BKE_layer_collection_get_active(view_layer);
Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name); 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_collection_object_add(bmain, lc->collection, obj);
BKE_view_layer_synced_ensure(scene, view_layer); BKE_view_layer_synced_ensure(scene, view_layer);
Base *base = BKE_view_layer_base_find(view_layer, obj); Base *base = BKE_view_layer_base_find(view_layer, obj);

View File

@ -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; size_t buffer_len;
void *buffer = BLI_file_read_text_as_mem(filepath, 0, &buffer_len); void *buffer = BLI_file_read_text_as_mem(filepath, 0, &buffer_len);
if (buffer == nullptr) { if (buffer == nullptr) {
fprintf(stderr, "STL Importer: cannot read from ASCII STL file: '%s'\n", filepath); 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); }); 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 } // namespace blender::io::stl

View File

@ -30,6 +30,6 @@
namespace blender::io::stl { 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 } // namespace blender::io::stl

View File

@ -29,7 +29,7 @@ struct STLBinaryTriangle {
}; };
#pragma pack(pop) #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; const int chunk_size = 1024;
uint32_t num_tris = 0; 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) { 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); 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 } // namespace blender::io::stl

View File

@ -26,6 +26,6 @@ namespace blender::io::stl {
const size_t BINARY_HEADER_SIZE = 80; const size_t BINARY_HEADER_SIZE = 80;
const size_t BINARY_STRIDE = 12 * 4 + 2; 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 } // namespace blender::io::stl

View File

@ -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) { if (degenerate_tris_num_ > 0) {
std::cout << "STL Importer: " << degenerate_tris_num_ << " degenerate triangles were removed" 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; << std::endl;
} }
Mesh *mesh = BKE_mesh_add(bmain, mesh_name); Mesh *mesh = BKE_mesh_new_nomain(verts_.size(), 0, tris_.size() * 3, tris_.size());
/* User count is already 1 here, but will be set later in #BKE_mesh_assign_object. */
id_us_min(&mesh->id);
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->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(); MutableSpan<int> poly_offsets = mesh->poly_offsets_for_write();
threading::parallel_for(poly_offsets.index_range(), 4096, [&](const IndexRange range) { threading::parallel_for(poly_offsets.index_range(), 4096, [&](const IndexRange range) {
for (const int i : range) { for (const int i : range) {

View File

@ -65,7 +65,7 @@ class STLMeshHelper {
const float3 &b, const float3 &b,
const float3 &c, const float3 &c,
const float3 &custom_normal); const float3 &custom_normal);
Mesh *to_mesh(Main *bmain, char *mesh_name); Mesh *to_mesh();
}; };
} // namespace blender::io::stl } // namespace blender::io::stl