-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmat_display.py
More file actions
155 lines (131 loc) · 6.1 KB
/
mat_display.py
File metadata and controls
155 lines (131 loc) · 6.1 KB
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
import redis
import time
import json
import numpy as np # Import numpy for array manipulations
import matplotlib.pyplot as plt # Import matplotlib for plotting
from mpl_toolkits.mplot3d import Axes3D # Import module for 3D plotting
from scipy.interpolate import griddata # Import griddata for interpolation
from scipy.spatial import Delaunay
from scipy.optimize import minimize
r = redis.Redis(host='localhost', port=6379, db=0)
queue_name = 'matlab_data'
def generate_crochet_pattern(vertices, faces, distances):
"""
Generate crochet pattern instructions from 3D mesh data.
"""
instructions = []
seed_vertex = int(np.argmin(distances)) # Convert NumPy int64 to native int
max_vertex = int(np.argmax(distances))
row_edges = []
col_edges = []
S = [[]]
# Step 1: Select seed and calculate geodesic distances (already done)
# Step 2: Iterate vertices and create rows of stitches
for i, vertex in enumerate(vertices):
stitch = {
'vertex_index': int(i), # Convert NumPy int64 to native int
'position': vertex.tolist(),
'distance': float(distances[i]) # Convert NumPy float to native float
}
if i > 0:
row_edges.append((int(i - 1), int(i))) # Convert NumPy int64 to native int
instructions.append(stitch)
# Step 3: Form columns (inter-row connections)
for start, end in zip(row_edges[:-1], row_edges[1:]):
col_edges.append((int(start[1]), int(end[0]))) # Convert NumPy int64 to native int
# Combine into instructions
pattern = {
'seed_vertex': seed_vertex,
'row_edges': row_edges,
'col_edges': col_edges,
'stitches': instructions
}
return pattern
def find_row_order(vertices, faces, distances, w):
# Calculate isolines based on the geodesic distances and yarn width w
max_distance = np.max(distances)
isoline_values = np.arange(0, max_distance, w)
row_order = []
for isoline_value in isoline_values:
# Find vertices that are close to the current isoline value
close_vertices = vertices[np.abs(distances - isoline_value) < w / 2]
row = [
{
'vert_index': int(i), # Convert NumPy int64 to native int
'raw_distance': float(distances[i]), # Convert NumPy float to native float
'reg_distance': isoline_value
}
for i, vertex in enumerate(vertices)
if np.abs(distances[i] - isoline_value) < w / 2
]
row_order.append(row)
return row_order
#Chatgpt code
def compute_gradient(vertices, values):
"""Compute the gradient of the scalar field 'values' defined on the mesh vertices."""
gradients = np.zeros_like(vertices)
for i, v in enumerate(vertices):
# For simplicity, using finite differences (this can be replaced with more accurate methods)
neighbors = np.where(np.linalg.norm(vertices - v, axis=1) < 1e-5)[0]
if len(neighbors) > 1:
deltas = vertices[neighbors] - v
value_deltas = values[neighbors] - values[i]
gradients[i] = np.mean(value_deltas[:, np.newaxis] * deltas, axis=0)
return gradients
def rotated_gradient(gradients):
"""Rotate the gradient vectors by 90 degrees in the tangent plane."""
J = np.array([[0, -1], [1, 0]]) # Rotation matrix for 2D case
return np.dot(gradients[:, :2], J.T)
def objective(g, vertices, gradients, rotated_gradients):
"""Objective function to minimize."""
diff = np.einsum('ij,ij->i', gradients, np.gradient(g, vertices[:, :2], axis=0)) - 1
return np.sum(diff**2)
def find_column_order(vertices, f_values, boundary_indices):
"""Find the column order 'g' given the vertices, f_values, and boundary condition."""
gradients = compute_gradient(vertices, f_values)
rotated_gradients = rotated_gradient(gradients)
# Initial guess for g (could be zeros or any other initial guess)
g_initial = np.zeros(len(vertices))
# Boundary condition
g_initial[boundary_indices] = 0
# Minimize the objective function
result = minimize(objective, g_initial, args=(vertices, gradients, rotated_gradients), method='L-BFGS-B')
return result.x
while True:
item = r.lpop(queue_name)
if item:
data = json.loads(item.decode('utf-8'))
vertices = np.array(data['vertices'])
faces = np.array(data['faces'])
distances = np.array(data['u_vfaOut'])
row_order = find_row_order(vertices, faces, distances, 0.03)
# Create a 3D plot outside of the function
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.azim = 90 # Rotate the plot to match the desired view
ax.elev = -90
ax.roll = 45
# Plotting the isolines
for close_vertices in row_order:
vertices_to_plot = np.array([vertices[item['vert_index']] for item in close_vertices])
ax.scatter(vertices_to_plot[:, 0], vertices_to_plot[:, 1], vertices_to_plot[:, 2], s=1)
# Find column order for each row of isoline vertices
column_orders = []
for close_vertices in row_order:
if len(close_vertices) > 1:
f_values = np.array([item['raw_distance'] for item in close_vertices])
row_vertices = np.array([vertices[item['vert_index']] for item in close_vertices])
boundary_indices = [0] # Example boundary index, replace with actual boundary condition
g_values = find_column_order(row_vertices, f_values, boundary_indices)
print(g_values)
column_orders.append(g_values)
# # Generate crochet pattern from vertices and distances
instructions = generate_crochet_pattern(vertices, faces, distances)
# print(f"Crochet Instructions: {json.dumps(instructions, indent=2)}")
with open('crochet_instructions.json', 'w') as f:
json.dump(instructions, f, indent=2)
plt.show()
print(f"Processed data item from the queue.")
else:
print("Queue is empty, waiting for new items...")
time.sleep(2) # Wait for 2 seconds before checking again