Skip to content

Commit d0632b1

Browse files
committed
feat(json): Pass json string to poisson algorithm to use externally
Improve comunication callin this lib from external (C#, CLI...)
1 parent 5bc7afb commit d0632b1

File tree

3 files changed

+92
-55
lines changed

3 files changed

+92
-55
lines changed

surface_reconstruction/open3d_surface.py

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,70 @@
55

66

77
class Open3dSurface(SurfaceStrategy):
8-
98
parameters = {
10-
'estimate_normals': [
11-
{
12-
'name': 'fast_normal_computation',
13-
'description': 'Fast normal estimation',
14-
'value': True
15-
},
16-
{
17-
'name': 'normals',
18-
'description': 'Points normals',
19-
'value': (1, 3)
20-
}
21-
],
22-
'orient_normals_consistent_tangent_plane': [
23-
{
24-
'name': 'k',
25-
'description': 'Nearest neighbors',
26-
'value': 100
27-
}
28-
],
29-
'surface_reconstruction_screened_poisson': [
30-
{
31-
'name': 'depth',
32-
'description': 'Maximum depth of the tree',
33-
'value': 8
34-
},
35-
{
36-
'name': 'width',
37-
'description': 'Target width',
38-
'value': 0
39-
},
40-
{
41-
'name': 'scale',
42-
'description': 'Ratio between the diameter of the cube',
43-
'value': 1.1
44-
},
45-
{
46-
'name': 'linear_fit',
47-
'description': 'Use linear interpolation?',
48-
'value': False
49-
},
50-
{
51-
'name': 'n_threads',
52-
'description': 'Number of threads used for reconstruction',
53-
'value': -1
54-
}
55-
]
9+
'estimate_normals': [
10+
{
11+
'name': 'fast_normal_computation',
12+
'description': 'Fast normal estimation',
13+
'value': True
14+
},
15+
{
16+
'name': 'normals',
17+
'description': 'Points normals',
18+
'value': (1, 3)
19+
}
20+
],
21+
'orient_normals_consistent_tangent_plane': [
22+
{
23+
'name': 'k',
24+
'description': 'Nearest neighbors',
25+
'value': 100
26+
}
27+
],
28+
'surface_reconstruction_screened_poisson': [
29+
{
30+
'name': 'depth',
31+
'description': 'Maximum depth of the tree',
32+
'value': 8
33+
},
34+
{
35+
'name': 'width',
36+
'description': 'Target width',
37+
'value': 0
38+
},
39+
{
40+
'name': 'scale',
41+
'description': 'Ratio between the diameter of the cube',
42+
'value': 1.1
43+
},
44+
{
45+
'name': 'linear_fit',
46+
'description': 'Use linear interpolation?',
47+
'value': False
48+
},
49+
{
50+
'name': 'n_threads',
51+
'description': 'Number of threads used for reconstruction',
52+
'value': -1
53+
}
54+
]
5655
}
5756

5857
def __init__(self, point_cloud_file="", output_file="", clean_up=True):
5958

6059
self.point_cloud = PointCloud()
6160
self.mesh = TriangleMesh()
6261

62+
# raise ValueError(f"Contains the {point_cloud_file} attribute: {output_file}")
63+
6364
super().__init__(point_cloud_file, output_file, clean_up)
6465

6566
def load_file(self, file_path: str) -> PointCloud:
6667
print('Load point cloud file')
6768

6869
self.point_cloud = o3d.io.read_point_cloud(
69-
file_path,
70-
print_progress=True
70+
file_path,
71+
print_progress=True
7172
)
7273

7374
print(np.asarray(self.point_cloud.points))
@@ -124,12 +125,12 @@ def apply_filter(name: str, params_key_values: dict):
124125

125126
# Save the generated Surface in a .ply file
126127
result = o3d.io.write_triangle_mesh(
127-
output_file,
128-
self.mesh,
129-
compressed=True,
130-
write_vertex_colors=True,
131-
write_vertex_normals=True,
132-
print_progress=True
128+
output_file,
129+
self.mesh,
130+
compressed=True,
131+
write_vertex_colors=True,
132+
write_vertex_normals=True,
133+
print_progress=True
133134
)
134135

135136
if not result:

surface_reconstruction/surface_strategy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ def poisson_mesh(self, save_file=True, **params: {}):
5656
"""
5757
raise NotImplementedError
5858

59+
def poisson(self, json_filters: str = ""):
60+
"""
61+
A surface reconstruction triangle method invoked by each library
62+
63+
:param json_filters:
64+
:return:
65+
"""
66+
if len(json_filters) == 0:
67+
return self.poisson_mesh(save_file=True)
68+
69+
data = json.loads(json_filters)
70+
return self.poisson_mesh(save_file=True, **{'filters': data})
71+
5972
def default_parameters(self, return_json=True) -> Union[dict, str]:
6073
"""
6174
Get all parameters required for all filters/methods to do a

tests/surface_reconstruction_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from __future__ import annotations
2+
23
from surface_reconstruction import SurfaceStrategy, PyMeshlabSurface, Open3dSurface, SurfaceReconstruction
34
import unittest
45
import json
6+
import os
57

68

79
class SurfaceReconstructionTest(unittest.TestCase):
810

11+
def setUp(self):
12+
self.files_folder = os.path.join('../files', 'complex_terrain')
13+
self.point_cloud_file = os.path.join(self.files_folder, 'list_vertex.ply')
14+
self.output_file = os.path.join(self.files_folder, 'terrain.ply')
15+
916
def test_factory_strategies_libraries(self):
1017

1118
surface = SurfaceReconstruction(method_type='pymeshlab')
@@ -57,5 +64,21 @@ def test_factory_strategy_parameters(self):
5764
self.assertIsInstance(open3d_parameters, str)
5865
self.assertGreater(len(open3d_parameters), 0)
5966

67+
def test_strategy_pass_json_parameters(self):
68+
69+
json_str = """
70+
{"estimate_normals":{"fast_normal_computation":true,"normals":[1,3]},"orient_normals_consistent_tangent_plane":{"k":100},"surface_reconstruction_screened_poisson":{"depth":8,"width":0,"scale":1.1,"linear_fit":false,"n_threads":-1}}
71+
"""
72+
73+
surface: SurfaceStrategy = SurfaceReconstruction(
74+
method_type='open3d',
75+
point_cloud_file=self.point_cloud_file,
76+
output_file=self.output_file
77+
)
78+
79+
surface.poisson(json_filters=json_str)
80+
81+
82+
6083

6184

0 commit comments

Comments
 (0)