def _align_normals(self) -> None: normals = self.point_basis[:, :, -1].T centered_points = self - math.mean(self) sum_pos = np.sum(math.l2_norm(centered_points + normals)) sum_neg = np.sum(math.l2_norm(centered_points - normals)) if sum_neg > sum_pos: self.point_basis *= -1 if not self.external: self.point_basis *= -1
def direct_vector(self, vector: np.ndarray) -> np.ndarray: """direct_vector """ self_project = math.vector_project(self, vector) pos_dist = math.l2_norm(vector - self_project) neg_dist = math.l2_norm(-vector - self_project) if neg_dist < pos_dist: return -1 * self else: return self
def _sorMainDistalToProximal(self): am, bm = self._mainCenterline[:, 0], self._mainCenterline[:, -1] ab, bb = self._bifurCenterline[:, 0], self._bifurCenterline[:, -1] shortestDistanceStart = min( math.l2_norm(am-ab), math.l2_norm(am-bb) ) shortestDistanceEnd = min( math.l2_norm(bm-ab), math.l2_norm(bm-bb) ) if shortestDistanceStart < shortestDistanceEnd: self._mainCenterline = Curve(np.flipud(self._mainCenterline.T).T)
def oriented_transport_frames(self, origin, jVector, return_index=True): origin_id = np.argmin(math.l2_norm( self - arrays.ColumnVector(origin) )) transportFrames = self.transport_frames() ijk_self = arrays.Basis(transportFrames[origin_id]) jVector = arrays.ColumnVector(jVector).hat projected = jVector.project_to_plane(ijk_self[:, -1]) j_new = math.normalize( projected.change_reference_frame(ijk_self)).squeeze() z_new = np.array([0, 0, 1]) ijk_new = arrays.Basis( math.normalize(math.cross(j_new, z_new)), j_new, z_new ) newTransportFrames = np.zeros_like(transportFrames) for i, frame in enumerate(transportFrames): newTransportFrames[i] = arrays.Basis(frame @ ijk_new) if return_index: return newTransportFrames, origin_id else: return newTransportFrames
def unwrap_cylinder_vtk_from_centerline(centerline, pv_mesh, clip=3, points_per_contour=256): s = np.linspace(0, 1, len(centerline.T) + clip * 2) centerline = centerline(s) tangents = math.normalize(np.gradient(centerline, axis=-1, edge_order=2)) transport_frames = centerline.transport_frames() slices = [] for i, (frame, c) in enumerate(zip(transport_frames, centerline.T)): t = frame[:, -1] # uncomment to check the clipping # if (any([i==clip, i>=(max(centerline.shape)-clip)])): # cut = pv_mesh.slice(normal=t, origin=c) # cut.plot() if (all([i >= clip, i < (max(centerline.shape) - clip)])): cut = pv_mesh.slice(normal=t, origin=c) connected_regions = cut.connectivity() unique_arrays = np.unique( connected_regions.point_arrays['RegionId']) if len(unique_arrays) > 1: min_distance = 1000000000 for region in unique_arrays: mask = connected_regions.point_arrays['RegionId'] == region points = connected_regions.points[mask] distance = math.l2_norm(points.mean() - c).squeeze() if distance < min_distance: min_distance = distance min_id = unique_arrays required_mask = mask else: min_id = unique_arrays.squeeze() required_mask = connected_regions.point_arrays[ 'RegionId'] == unique_arrays current_slice = {} current_slice['points'] = connected_regions.points[required_mask] current_slice['point_arrays'] = {} for key in connected_regions.point_arrays.keys(): if key != "RegionId": current_slice["point_arrays"][ key] = connected_regions.point_arrays[key][ required_mask] contour_field = arrays.FieldContour( current_slice["points"].T, fields=dict([(key, current_slice["point_arrays"][key].T) for key in current_slice["point_arrays"].keys()])) contour_field = contour_field.sortByBasis(frame) slices.append(contour_field(np.linspace(0, 1, points_per_contour))) # create grids out of the slices point_grid = np.stack(slices, axis=-1) field_grids = {} for key in slices[0].fields.keys(): field_stack = [s.fields[key] for s in slices] field_stack = np.stack(field_stack, axis=-1) field_grids[key] = field_stack # create the cylinder stack point_grid, field_grid = arrays.structured.CylindricalSurface.align_contour_points( slices, field_grids=field_grids) # # plot the resulting fields # pyplot.imshow(point_grid[0]) # pyplot.show() # pyplot.imshow(point_grid[1]) # pyplot.show() # pyplot.imshow(point_grid[2]) # pyplot.show() # pyplot.imshow(field_grids['magWallShearStress'][0]) # pyplot.show() return point_grid, field_grids
def magnitude(self): """ """ return math.l2_norm(self).squeeze()
def delta_per_point(self) -> np.ndarray: return np.concatenate( [[[0]], math.l2_norm(np.diff(self, axis=-1))], axis=-1 )
def _sortBifurCenterlinebyMainCenterline(self): am = self._mainCenterline[:, 0] ab, bb = self._bifurCenterline[:, 0], self._bifurCenterline[:, -1] if math.l2_norm(am - ab) > math.l2_norm(am - bb): self._bifurCenterline = Curve(np.flipud(self._bifurCenterline.T).T)