[SOLVED] Poisson reconstruction in command line

To post any request/idea for new functionalities
Post Reply
PablerasBCN
Posts: 296
Joined: Sat Jan 20, 2018 1:57 pm

[SOLVED] Poisson reconstruction in command line

Post by PablerasBCN »

I've workarounde this by using pymeshlab script at the end.
----------------------------------------------------------
Whispering Wishwillow, here I come...

The thing is that I've been trying to get rid of Metashape for the generation of normal maps. Honestly it does an amazing job creating normals out of the box and the meshing and bake times are very good, specially baking times.

Yet, I would preffer to have the least pathing in the workflow so would love to stain within the Cloud Compare Meshlab circuit as much as possible.

Hence, I did compute cloud normals in Metashape and brought it to CC to have it as ground truth and start testing all the options to finally get the Quadric at certain radius provide pretty much the same, very decent normals. Great job CC. The only thing Metahsape seems to do better is that seems to automaticaly adapt radius but per area, since my scan has like an excess of points on the road it takes that as the optimal radius but is too low for the nearby reatining walls.

Hence manually setting to 0.04 is better, where I can see that Metashape seems to use 0.02 for road and 0.04/0.05 for walls.

Yet, once I meshed with Poison the ressult is outstanding with CC's normal computation. I was quite amazed to get such nice mesh. Comparable to what I was getting in MEtashape. I had to slowly increase the Poison Octree to get to the optimal, being 14, which took 25 minutes!

[12:11:12] [PoissonRecon] Job finished (456599 triangles, 228375 vertices)
[12:13:14] [PoissonRecon] Job started (level 12 - 12 threads)
[12:13:35] [PoissonRecon] Duration: 21.6 s

[12:13:36] [PoissonRecon] Job finished (1686791 triangles, 843482 vertices)
[12:14:51] [PoissonRecon] Job started (level 13 - 12 threads)
[12:17:04] [PoissonRecon] Duration: 133.0 s
[12:17:04] [PoissonRecon] Job finished (6346051 triangles, 3173114 vertices)

[12:26:40] [PoissonRecon] Job started (level 14 - 12 threads)
[12:51:46] [PoissonRecon] Duration: 1506.2 s
[12:51:46] [PoissonRecon] Job finished (24306723 triangles, 12153466 vertices)


crazy how computing time scalates.


Image



So. What I did wa to export the .ply mesh and

1) Baked the normals of this hi poly into UV mapped low poly in Metashape, With superb ressults, thsi way I could also check if any issues where present to this point compared to all in Metashape process.

2) So with all in order the natural thing would be to do in Meshlab wher I've a python script that transfers color or normal properties.

But, the meshing needs are not for one file but in many many tiles, while I can do in command line all the normals thing , Poisson meshing is not exposed in command line. So request would be to please expose it with the Octree param. Since each meshing takes 30 mins or more, would be the ideal thing to be overnight or overweekend.

Thank you for considering.




I still can not rely fully on Meshlab because when baking normals makes them pinky on the reaining walls, same as when I baked the normals as RGB and later I baked in meshlab. In Metashape does not happen and the game engine seems to like the Meshlab version.

Image

-----------------------------------------------------------------------------------------------------------
EDIT
So I realized why the pinky retaining walls. These show pink as do normals of the cloud computed in Metashape. The "magic" of "uniformizing" the normals occurs when baking point normals into the low poly mesh, because the point cloud normals are reociented relative to the low poly face's normals. Sadly Meshlab does not do that, Meshlab literally does transfers the normals, "as is", without reorienting relative to the low poly surface...


-----------------------------------------------------------------------------------------------------------


But still, having the meshing automated would be a blast since the manual baking of normals in Metashape is surprisingly fast, even at 16k sizes!

EDIT
#############################################################################################################

Code: Select all

import os
import pymeshlab

input_folder = r'E:\LIDAR_JAPAN\MMS_14_Prototype_4\MMS\04_RoadMesh\04_Texturing\01_DATA_SOURCE_POINT_CLOUDS\00_color'
output_folder = r'E:\LIDAR_JAPAN\MMS_14_Prototype_4\MMS\04_RoadMesh\04_Texturing\01_DATA_SOURCE_POINT_CLOUDS\03_Normals\HD_RoadMesh'
depth = 13

print(f"Input folder: {input_folder}")
print(f"Output folder: {output_folder}")
print(f"Depth value: {depth}")

def compute_average_normal(ms):
    normals = ms.current_mesh().vertex_normal_matrix()
    avg_normal = normals.mean(axis=0)
    return avg_normal

def mesh_point_clouds(input_folder, output_folder, depth):
    if not os.path.exists(input_folder):
        raise FileNotFoundError(f"The input folder '{input_folder}' does not exist.")
    
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    else:
        print(f"Output folder '{output_folder}' already exists. Files may be overwritten.")
    
    files = [f for f in os.listdir(input_folder) if f.endswith('.ply')]
    total_files = len(files)
    print(f"Total files to be processed: {total_files}")
    
    for i, file in enumerate(files, 1):
        input_path = os.path.join(input_folder, file)
        output_path = os.path.join(output_folder, file)
        
        print(f"[{i}/{total_files}] Loading file: {file}")

        ms = pymeshlab.MeshSet()
        ms.load_new_mesh(input_path)
        
        # Compute normals for the point cloud
        print(f"[{i}/{total_files}] Computing normals for: {file}")
        try:
            ms.compute_normal_for_point_clouds(k=20, smoothiter=0)
        except pymeshlab.PyMeshLabException as e:
            print(f"Failed to compute normals for {file}: {e}")
            continue
        
        # Perform surface reconstruction
        print(f"[{i}/{total_files}] Generating surface reconstruction for: {file}")
        try:
            ms.generate_surface_reconstruction_screened_poisson(depth=depth)
        except pymeshlab.PyMeshLabException as e:
            print(f"Failed to process {file}: {e}")
            continue
        
        # Delete input point cloud to force normal computation on the mesh
        #os.remove(input_path)

        # Print number of faces before recomputing normals
        num_faces = ms.current_mesh().face_number()
        print(f"[{i}/{total_files}] Number of faces before recomputing normals: {num_faces}")
 
        # Recompute normals for the reconstructed mesh
        print(f"[{i}/{total_files}] Recomputing normals for: {file}")
        try:
            ms.compute_normal_per_vertex(weightmode=1)
        except pymeshlab.PyMeshLabException as e:
            print(f"Failed to recompute normals for {file}: {e}")
            continue
        
        # Check the average normal direction
        avg_normal = compute_average_normal(ms)
        if avg_normal[2] < 0:  # If the z-component is negative, normals are pointing downwards
            print(f"[{i}/{total_files}] Inverting normals for: {file}")
            try:
                ms.meshing_invert_face_orientation()
            except pymeshlab.PyMeshLabException as e:
                print(f"Failed to invert normals for {file}: {e}")
                continue
        
        # Save the mesh
        ms.save_current_mesh(output_path)
        
        print(f"[{i}/{total_files}] Processed {file} and saved to {output_path}")

mesh_point_clouds(input_folder, output_folder, depth)

#############################################################################################################

-I had to re cmpute normals at the end because there are chips all over the mesh, this may not be your case. It is very fast anyways.
-Octree levels are similar to CC yet not sure if are equivalent.
EDIT 2 ( the vertex normal recompute does not seem to work in commandline, works perfectly in UI version, may be a pymeshlab bug, reporting)
- Structures like Tunnels fail in normal computing both in CC and Meshlab. I was only able to get these nicely meshed in Metashape.

Other than that allows you to avoid the Metashape pro only to batch mesh poitn clouds, and do manually the scan areas where are tunnels, which is not that much of an issue tbh.

I also see there is now python within CC, may be Poisson can be called in batch using Python, dunno.
PablerasBCN
Posts: 296
Joined: Sat Jan 20, 2018 1:57 pm

Re: [SOLVED] Poisson reconstruction in command line

Post by PablerasBCN »

While I marked as solved because my initial needs are fullfilled with the script.

While the ground cloud I don´t mind if it expands becasue of how the borders grow with Poison, I find it super handy the SF of density that allows to easily trim the mesh.

It would be great to have access to Poisson meshing via commandline and be able to atomatically trim to a given density to the whole batch.
daniel
Site Admin
Posts: 7710
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: [SOLVED] Poisson reconstruction in command line

Post by daniel »

So technically, the Poisson Reconstruction algorithm is already a command line tool (as provided by its authors: https://www.cs.jhu.edu/~misha/Code/Pois ... sion16.04/).
Daniel, CloudCompare admin
Post Reply