| |
|
| | import matplotlib.pyplot as plt
|
| | from mpl_toolkits.mplot3d import Axes3D
|
| | from read_node import read_dat_file, read_result_file
|
| | import os
|
| | import numpy as np
|
| | import pyvista as pv
|
| | from scipy.spatial import distance_matrix
|
| |
|
| | """
|
| | folder_path = r"D:\AnK\FatigueNet\Fatigue_Life\datasets\Raw data\shaft_high\life_D15_d5_r1"
|
| | folder_base_name = os.path.basename(folder_path).replace("_data", "")
|
| | dat_file_path = os.path.join(folder_path, f"{folder_base_name}.dat")
|
| | fatigue_life_path = os.path.join(folder_path, f"{folder_base_name}.txt")
|
| |
|
| | positions, connectivity = read_dat_file(dat_file_path)
|
| |
|
| | positions = np.array(positions)
|
| | connectivity = np.array(connectivity)
|
| |
|
| | # Calculate appropriate sphere size based on point density
|
| | # Find minimum distance between points to avoid overlap
|
| | from scipy.spatial.distance import pdist
|
| | distances = pdist(positions)
|
| | min_distance = np.min(distances)
|
| | max_distance = np.max(distances)
|
| | print(f"Min distance between points: {min_distance:.6f}")
|
| | print(f"Max distance between points: {max_distance:.6f}")
|
| |
|
| | # Matplotlib version (for comparison)
|
| | x = positions[:, 0]
|
| | y = positions[:, 1]
|
| | z = positions[:, 2]
|
| |
|
| | fig = plt.figure(figsize=(10, 8))
|
| | ax = fig.add_subplot(111, projection='3d')
|
| | ax.scatter(x, y, z, c='red', s=50, alpha=0.8)
|
| | ax.set_xlabel('X')
|
| | ax.set_ylabel('Y')
|
| | ax.set_zlabel('Z')
|
| | ax.set_title('3D Points Visualization - Matplotlib')
|
| | ax.grid(True)
|
| | plt.show()
|
| | """
|
| |
|
| |
|
| |
|
| | """
|
| | import os
|
| | import numpy as np
|
| | import pyvista as pv
|
| | from scipy.spatial.distance import pdist
|
| | from read_node import read_dat_file
|
| |
|
| | # === Load dataset ===
|
| | folder_path = r"D:\AnK\FatigueNet\Fatigue_Life\datasets\Raw data\shaft_high\life_D15_d5_r1"
|
| | folder_base_name = os.path.basename(folder_path).replace("_data", "")
|
| | dat_file_path = os.path.join(folder_path, f"{folder_base_name}.dat")
|
| |
|
| | # Read point positions
|
| | positions, connectivity = read_dat_file(dat_file_path)
|
| | #print(connectivity)
|
| | #print(positions)
|
| |
|
| | positions = np.array(positions)
|
| | connectivity = np.array(connectivity)
|
| |
|
| | # === Compute spacing metrics ===
|
| | distances = pdist(positions)
|
| | min_distance = np.min(distances)
|
| | max_distance = np.max(distances)
|
| | print(f"Min distance between points: {min_distance:.6f}")
|
| | print(f"Max distance between points: {max_distance:.6f}")
|
| |
|
| | # === Create PyVista point cloud ===
|
| | point_cloud = pv.PolyData(positions)
|
| |
|
| | # === Option A: Use glyphs (spheres) for geometric visualization ===
|
| | # Choose adaptive sphere size
|
| | sphere_radius = min_distance * 0.35 # 10% of min spacing
|
| | sphere = pv.Sphere(radius=sphere_radius)
|
| |
|
| | # Attach dummy scalar field (optional, for glyph binding)
|
| | point_cloud["id"] = np.arange(len(positions))
|
| |
|
| | # Generate glyphs
|
| | glyphs = point_cloud.glyph(geom=sphere, scale=False)
|
| |
|
| | # === PyVista Plot ===
|
| | plotter = pv.Plotter()
|
| | plotter.add_mesh(glyphs, color="red", opacity=1.0, show_scalar_bar=False)
|
| |
|
| | # Add axes and grid
|
| | plotter.add_axes()
|
| | plotter.show_grid()
|
| | plotter.set_background("white")
|
| | plotter.add_title("3D Point Cloud - PyVista (Spheres)")
|
| |
|
| | # Display
|
| | plotter.show()
|
| | """
|
| |
|
| |
|
| |
|
| | """
|
| | import os
|
| | import numpy as np
|
| | import pyvista as pv
|
| | from scipy.spatial.distance import pdist
|
| | from read_node import read_dat_file, read_result_file # Make sure these are available
|
| |
|
| | # === Load dataset ===
|
| | folder_path = r"D:\AnK\FatigueNet\Fatigue_Life\datasets\Raw data\shaft_high\life_D15_d5_r1"
|
| | folder_base_name = os.path.basename(folder_path).replace("_data", "")
|
| | dat_file_path = os.path.join(folder_path, f"{folder_base_name}.dat")
|
| | fatigue_life_path = os.path.join(folder_path, f"{folder_base_name}.txt")
|
| |
|
| | positions, connectivity = read_dat_file(dat_file_path)
|
| | #print(connectivity)
|
| | #print(positions)
|
| |
|
| | positions = np.array(positions)
|
| | connectivity = np.array(connectivity)
|
| | fatigue_life = np.array(read_result_file(fatigue_life_path))
|
| |
|
| | assert positions.shape[0] == fatigue_life.shape[0], "Mismatch between points and fatigue data!"
|
| |
|
| | # === Compute spacing metrics ===
|
| | distances = pdist(positions)
|
| | min_distance = np.min(distances)
|
| | max_distance = np.max(distances)
|
| | print(f"Min distance between points: {min_distance:.6f}")
|
| | print(f"Max distance between points: {max_distance:.6f}")
|
| |
|
| | # === Create point cloud and attach fatigue life ===
|
| | point_cloud = pv.PolyData(positions)
|
| | point_cloud["fatigue_life"] = fatigue_life # <-- this is used for coloring
|
| | point_cloud["fatigue_life"] = np.log10(fatigue_life + 1e-12) # comment this if you want the absolute mapping of the life on nodes
|
| |
|
| |
|
| | # === Create glyphs (spheres) ===
|
| | sphere_radius = min_distance * 0.35
|
| | sphere = pv.Sphere(radius=sphere_radius)
|
| | glyphs = point_cloud.glyph(geom=sphere, scale=False)
|
| |
|
| | # === PyVista plot ===
|
| | plotter = pv.Plotter()
|
| | plotter.add_mesh(glyphs,
|
| | scalars="fatigue_life",
|
| | cmap="viridis", # or try "plasma", "coolwarm", "inferno"
|
| | opacity=1.0,
|
| | show_scalar_bar=True)
|
| |
|
| | plotter.add_axes()
|
| | plotter.show_grid()
|
| | plotter.set_background("white")
|
| | plotter.add_title("3D Point Cloud Colored by Fatigue Life")
|
| | plotter.show()
|
| | """
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | import os
|
| | import numpy as np
|
| | import pyvista as pv
|
| | from scipy.spatial.distance import pdist
|
| | from read_node import read_dat_file, read_result_file
|
| |
|
| |
|
| | folder_path = r"D:\AnK\FatigueNet\Fatigue_Life\Visuize\life_D15_d5_r2"
|
| | folder_base_name = os.path.basename(folder_path).replace("_data", "")
|
| | dat_file_path = os.path.join(folder_path, f"{folder_base_name}.dat")
|
| | fatigue_life_path = os.path.join(folder_path, f"{folder_base_name}.txt")
|
| |
|
| |
|
| | positions, connectivity = read_dat_file(dat_file_path)
|
| |
|
| |
|
| |
|
| | positions = np.array(positions)
|
| | connectivity = np.array(connectivity)
|
| |
|
| | fatigue_life = np.array(read_result_file(fatigue_life_path))
|
| |
|
| |
|
| | n_elements = len(connectivity)
|
| | cell_types = np.full(n_elements, pv.CellType.TETRA, dtype=np.uint8)
|
| |
|
| | if np.min(connectivity) > 0:
|
| | connectivity -= 1
|
| |
|
| |
|
| | cells = []
|
| | for conn in connectivity:
|
| | cells.append(4)
|
| | cells.extend(conn)
|
| | cells = np.array(cells)
|
| |
|
| |
|
| | grid = pv.UnstructuredGrid(cells, cell_types, positions)
|
| |
|
| |
|
| | grid["fatigue_life"] = fatigue_life
|
| |
|
| |
|
| | plotter = pv.Plotter()
|
| | plotter.add_mesh(grid, scalars="fatigue_life", cmap="viridis_r", show_edges=True)
|
| | plotter.add_axes()
|
| | plotter.show_grid()
|
| | plotter.set_background("white")
|
| | plotter.add_title("Fatigue Mesh Visualization (Connected Elements)")
|
| | plotter.show()
|
| |
|