Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More efficient pointcloud creation #163

Merged
merged 12 commits into from
Mar 10, 2023
40 changes: 14 additions & 26 deletions src/idvc/dvc_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2923,7 +2923,10 @@ def createPointCloud(self, **kwargs):
self.pointCloud = pointCloud
else:
pointCloud = self.pointCloud

# instead of translating the point cloud so that point0 is in the point cloud
# we add point0 to the point cloud.
if hasattr(self, 'point0'):
pointCloud.SetPoint0(self.point0_world_coords)
Comment on lines +2941 to +2944
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should do this. Because then the points aren't evenly spaced and the overlap size around point 0 will not be consistent with the rest of the cloud. E.g. it ends up looking like this:
image
And e.g. with an overlap of 0:
image
there's clearly overlap around point 0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my understanding is that we had approval from Catherine that this is fine? So re-opening the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to test this again and re-review

v = self.vis_widget_2D.frame.viewer
orientation = v.getSliceOrientation()
pointCloud.SetOrientation(orientation)
Expand Down Expand Up @@ -3041,6 +3044,7 @@ def createPointCloud(self, **kwargs):
# Mask the point cloud with the eroded mask
if not self.pointCloudCreated:
polydata_masker = cilMaskPolyData()
polydata_masker.AddObserver(vtk.vtkCommand.ProgressEvent, partial(getProgress, progress_callback=progress_callback))
# save reference
self.polydata_masker = polydata_masker
else:
Expand Down Expand Up @@ -3089,29 +3093,12 @@ def createPointCloud(self, **kwargs):

mm = mask_data.GetScalarComponentAsDouble(int(self.point0_sampled_image_coords[0]),int(self.point0_sampled_image_coords[1]), int(self.point0_sampled_image_coords[2]), 0)

if int(mm) == 1: #if point0 is in the mask
#print("POINT 0 IN MASK")
#Translate pointcloud so that point 0 is in the cloud
if hasattr(self, 'point0'):
pointCloud_points = []
pointCloud_distances = []
#print("Point 0: ", self.point0_world_coords)
for i in range (0, pointCloud.GetNumberOfPoints()):
current_point = pointCloud.GetPoints().GetPoint(i)
pointCloud_points.append(current_point)
pointCloud_distances.append((self.point0_world_coords[0]-current_point[0])**2+(self.point0_world_coords[1]-current_point[1])**2+(self.point0_world_coords[2]-current_point[2])**2)

lowest_distance_index = pointCloud_distances.index(min(pointCloud_distances))

#print("The point closest to point 0 is:", pointCloud_points[lowest_distance_index])

pointCloud_Translation = (self.point0_world_coords[0]-pointCloud_points[lowest_distance_index][0],self.point0_world_coords[1]-pointCloud_points[lowest_distance_index][1],self.point0_world_coords[2]-pointCloud_points[lowest_distance_index][2])

#print("Translation from it is:", pointCloud_Translation)

transform.Translate(pointCloud_Translation)
#else:
#print("POINT 0 NOT IN MASK")
if int(mm) == 1:
#if point0 is in the mask and it has been added as first point to the point cloud
remove_point0 = False
else:
# should remove point0 from the mask
remove_point0 = True

if self.pointCloudCreated:
t_filter = self.t_filter
Expand All @@ -3130,7 +3117,7 @@ def createPointCloud(self, **kwargs):
# polydata_masker.Modified()
message_callback.emit('Applying mask to pointcloud')
polydata_masker.Update()

message_callback.emit('Applying mask to pointcloud. Done')
#print("Points in mask now: ", polydata_masker)

self.reader = reader
Expand Down Expand Up @@ -3159,7 +3146,8 @@ def createPointCloud(self, **kwargs):
count += 1

message_callback.emit('Saving pointcloud')
np.savetxt(tempfile.tempdir + "/" + filename, array, '%d\t%.3f\t%.3f\t%.3f', delimiter=';')
start = 0 if remove_point0 is False else 1
np.savetxt(tempfile.tempdir + "/" + filename, array[start:], '%d\t%.3f\t%.3f\t%.3f', delimiter=';')
self.roi = filename

return True
Expand Down
15 changes: 15 additions & 0 deletions src/idvc/pointcloud_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ def __init__(self):
self._SliceNumber = 0
self._Mode = self.CUBE
self._SubVolumeRadius = 1 #: Radius of the subvolume in voxels
self._Point0 = None

def GetPoints(self):
'''Returns the Points'''
return self._Points

def SetPoint0(self, value):
if not isinstance(value, list):
raise ValueError('Point0 must be a list. Got', value)
if len(value) != 3:
raise ValueError('Point0 must be a list of 3 elements. Got', value)
self._Point0 = value
self.Modified()

def SetMode(self, value):
'''Sets the shape mode'''
if not value in [self.CIRCLE, self.SQUARE, self.CUBE, self.SPHERE]:
Expand Down Expand Up @@ -212,6 +222,8 @@ def CreatePoints2D(self, point_spacing , sliceno, image_data, orientation):
# print ("dimensions : ", image_dimensions)
# print ("point spacing ", point_spacing)

if self._Point0 is not None:
vtkPointCloud.InsertNextPoint(*self._Point0)
#label orientation axis as a, with plane being viewed labelled as bc

# reduce to 2D on the proper orientation
Expand Down Expand Up @@ -278,6 +290,9 @@ def CreatePoints3D(self, point_spacing , image_data, orientation, sliceno):
image_origin = list ( image_data.GetOrigin() )
image_dimensions = list ( image_data.GetDimensions() )

if self._Point0 is not None:
vtkPointCloud.InsertNextPoint(*self._Point0)

# the total number of points on X and Y axis
max_x = image_dimensions[0] * image_spacing[0] / point_spacing[0]
max_y = image_dimensions[1] * image_spacing[1] / point_spacing[1]
Expand Down