Blair Johnson commited on
Commit
5a7704b
·
1 Parent(s): a4cf910

Separate preview and export STL orientation

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -897,12 +897,15 @@ def apply_pca_correction_to_z(z_data: np.ndarray, x_length: float, min_z: float,
897
  return min_z + corrected_normalized * (max_z - min_z)
898
 
899
 
900
- def build_stl_mesh(z_data: np.ndarray, x_length: float, close_body: bool) -> str:
901
  height, width = z_data.shape
902
  y_length = x_length * (height / width)
903
  vertices = np.zeros((height, width, 3), dtype=np.float32)
904
  x_coords = np.linspace(0, x_length, width, dtype=np.float32)
905
- y_coords = np.linspace(0, y_length, height, dtype=np.float32)
 
 
 
906
  vertices[:, :, 0] = x_coords[np.newaxis, :]
907
  vertices[:, :, 1] = y_coords[:, np.newaxis]
908
  vertices[:, :, 2] = z_data.astype(np.float32)
@@ -911,7 +914,10 @@ def build_stl_mesh(z_data: np.ndarray, x_length: float, close_body: bool) -> str
911
  for i in range(height - 1):
912
  for j in range(width - 1):
913
  v1, v2, v3, v4 = vertices[i, j], vertices[i + 1, j], vertices[i + 1, j + 1], vertices[i, j + 1]
914
- faces.extend([[v1, v3, v2], [v1, v4, v3]])
 
 
 
915
 
916
  if close_body:
917
  v_tl = vertices[0, 0]
@@ -923,13 +929,17 @@ def build_stl_mesh(z_data: np.ndarray, x_length: float, close_body: bool) -> str
923
  b_bl = np.array([v_bl[0], v_bl[1], 0], dtype=np.float32)
924
  b_br = np.array([v_br[0], v_br[1], 0], dtype=np.float32)
925
 
926
- faces.extend([
927
  [v_tl, b_tl, b_tr], [v_tl, b_tr, v_tr],
928
  [v_br, b_br, b_bl], [v_br, b_bl, v_bl],
929
  [v_bl, b_bl, b_tl], [v_bl, b_tl, v_tl],
930
  [v_tr, b_tr, b_br], [v_tr, b_br, v_br],
931
- [b_tl, b_bl, b_br], [b_tl, b_br, b_tr],
932
- ])
 
 
 
 
933
 
934
  surface = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype))
935
  surface.vectors = np.array(faces)
@@ -974,10 +984,14 @@ def generate_3d_model_from_adjusted_depth(
974
  if do_pca_correction:
975
  z_data = apply_pca_correction_to_z(z_data, x_length, min_z, max_z)
976
 
977
- stl_path = build_stl_mesh(z_data, x_length, close_body)
 
978
  point_count = len(sanitize_curve_points(curve_points_json))
979
- status = f"Generated STL from previewed height map using {point_count} curve points and texture strength {float(texture_strength):.3f}."
980
- return stl_path, stl_path, status
 
 
 
981
 
982
 
983
  with gr.Blocks(title="Image to 3D Relief Generator") as demo:
 
897
  return min_z + corrected_normalized * (max_z - min_z)
898
 
899
 
900
+ def build_stl_mesh(z_data: np.ndarray, x_length: float, close_body: bool, flip_for_preview: bool = False) -> str:
901
  height, width = z_data.shape
902
  y_length = x_length * (height / width)
903
  vertices = np.zeros((height, width, 3), dtype=np.float32)
904
  x_coords = np.linspace(0, x_length, width, dtype=np.float32)
905
+ if flip_for_preview:
906
+ y_coords = np.linspace(0, y_length, height, dtype=np.float32)
907
+ else:
908
+ y_coords = np.linspace(y_length, 0, height, dtype=np.float32)
909
  vertices[:, :, 0] = x_coords[np.newaxis, :]
910
  vertices[:, :, 1] = y_coords[:, np.newaxis]
911
  vertices[:, :, 2] = z_data.astype(np.float32)
 
914
  for i in range(height - 1):
915
  for j in range(width - 1):
916
  v1, v2, v3, v4 = vertices[i, j], vertices[i + 1, j], vertices[i + 1, j + 1], vertices[i, j + 1]
917
+ if flip_for_preview:
918
+ faces.extend([[v1, v3, v2], [v1, v4, v3]])
919
+ else:
920
+ faces.extend([[v1, v2, v3], [v1, v3, v4]])
921
 
922
  if close_body:
923
  v_tl = vertices[0, 0]
 
929
  b_bl = np.array([v_bl[0], v_bl[1], 0], dtype=np.float32)
930
  b_br = np.array([v_br[0], v_br[1], 0], dtype=np.float32)
931
 
932
+ side_faces = [
933
  [v_tl, b_tl, b_tr], [v_tl, b_tr, v_tr],
934
  [v_br, b_br, b_bl], [v_br, b_bl, v_bl],
935
  [v_bl, b_bl, b_tl], [v_bl, b_tl, v_tl],
936
  [v_tr, b_tr, b_br], [v_tr, b_br, v_br],
937
+ ]
938
+ if flip_for_preview:
939
+ base_faces = [[b_tl, b_bl, b_br], [b_tl, b_br, b_tr]]
940
+ else:
941
+ base_faces = [[b_tl, b_br, b_bl], [b_tl, b_tr, b_br]]
942
+ faces.extend(side_faces + base_faces)
943
 
944
  surface = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype))
945
  surface.vectors = np.array(faces)
 
984
  if do_pca_correction:
985
  z_data = apply_pca_correction_to_z(z_data, x_length, min_z, max_z)
986
 
987
+ preview_stl_path = build_stl_mesh(z_data, x_length, close_body, flip_for_preview=True)
988
+ export_stl_path = build_stl_mesh(z_data, x_length, close_body, flip_for_preview=False)
989
  point_count = len(sanitize_curve_points(curve_points_json))
990
+ status = (
991
+ f"Generated STL from previewed height map using {point_count} curve points and texture strength "
992
+ f"{float(texture_strength):.3f}."
993
+ )
994
+ return preview_stl_path, export_stl_path, status
995
 
996
 
997
  with gr.Blocks(title="Image to 3D Relief Generator") as demo: