File size: 7,784 Bytes
6ac44e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

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()

"""


#.........................................................................................................................................................................................
# visualization with connectivity
#.........................................................................................................................................................................................

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\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)
#print(connectivity)
#print(positions)

positions = np.array(positions)
connectivity = np.array(connectivity)

fatigue_life = np.array(read_result_file(fatigue_life_path))

# Convert connectivity to VTK format for unstructured grid
n_elements = len(connectivity)
cell_types = np.full(n_elements, pv.CellType.TETRA, dtype=np.uint8)

if np.min(connectivity) > 0:
    connectivity -= 1

# Format: [n_points, pt1, pt2, pt3, pt4, ...]
cells = []
for conn in connectivity:
    cells.append(4)
    cells.extend(conn)
cells = np.array(cells)

# Build unstructured grid
grid = pv.UnstructuredGrid(cells, cell_types, positions)

# Attach fatigue life as point data
grid["fatigue_life"] = fatigue_life

# Plot
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()