@@ -46,6 +46,10 @@ class SequenceConverter:
4646 loadMeshLock = Lock ()
4747 activeThreads = 0
4848
49+ #Only used for pointcloud normal estimation
50+ lastAverageNormal = [0 , 0 , 0 ]
51+ firstEstimation = True
52+
4953 def start_conversion (self , convertSettings , processFinishedCB ):
5054
5155 self .convertSettings = convertSettings
@@ -100,13 +104,13 @@ def process_models(self):
100104 else :
101105 threads = self .convertSettings .maxThreads
102106
103- if not self .debugMode :
104- self .modelPool = ThreadPool (processes = threads )
105- self .modelPool .map_async (self .convert_model , self .convertSettings .modelPaths )
106-
107- else :
107+ if self .debugMode or self .convertSettings .generateNormals :
108+ self .firstEstimation = True
108109 for model in self .convertSettings .modelPaths :
109110 self .convert_model (model )
111+ else :
112+ self .modelPool = ThreadPool (processes = threads )
113+ self .modelPool .map_async (self .convert_model , self .convertSettings .modelPaths )
110114
111115 def convert_model (self , file ):
112116
@@ -188,7 +192,27 @@ def convert_model(self, file):
188192
189193 # For pointclouds, normals can be estimated
190194 if (self .convertSettings .generateNormals and self .convertSettings .isPointcloud ):
191- ms .compute_normal_for_point_clouds (k = 10 , flipflag = True )
195+ ms .compute_normal_for_point_clouds (k = 10 , flipflag = False )
196+ normals = ms .current_mesh ().vertex_normal_matrix ().astype (np .float32 )
197+
198+ averageNormal = [np .average (normals [:,0 ]), np .average (normals [:,1 ]), np .average (normals [:,2 ])]
199+
200+ if not self .firstEstimation :
201+
202+ # Normalize the vectors
203+ v1_norm = averageNormal / np .linalg .norm (averageNormal )
204+ v2_norm = self .lastAverageNormal / np .linalg .norm (self .lastAverageNormal )
205+
206+ # Compute the dot product
207+ dot_product = np .dot (v1_norm , v2_norm )
208+
209+ #Flip normals if the average normal differs too much from the last frame
210+ if (dot_product < 0.5 ):
211+ normals = normals * - 1
212+ averageNormal = np .multiply (averageNormal , - 1 )
213+
214+ self .lastAverageNormal = averageNormal
215+ self .firstEstimation = False
192216
193217 if (self .terminateProcessing ):
194218 self .processFinishedCB (False , "" )
0 commit comments