Răsfoiți Sursa

add script to mirror pattern for text, undo script transformation

Tuan Nguyen 1 an în urmă
părinte
comite
f4e8905abd
2 a modificat fișierele cu 49 adăugiri și 17 ștergeri
  1. 4 17
      app.py
  2. 45 0
      mirror_pattern.py

+ 4 - 17
app.py

@@ -47,7 +47,7 @@ def restart_serial(port, baudrate=115200):
     disconnect_serial()
     connect_to_serial(port, baudrate)
 
-def parse_theta_rho_file(file_path, apply_transformations=False):
+def parse_theta_rho_file(file_path):
     """
     Parse a theta-rho file and return a list of (theta, rho) pairs.
     Optionally apply transformations (rotation and mirroring).
@@ -64,20 +64,7 @@ def parse_theta_rho_file(file_path, apply_transformations=False):
                 # Parse lines with theta and rho separated by spaces
                 try:
                     theta, rho = map(float, line.split())
-                    if apply_transformations:
-                        # Rotate 90 degrees clockwise
-                        theta_rotated = theta - (math.pi / 2)
-                        if theta_rotated < 0:  # Ensure theta stays within [0, 2π)
-                            theta_rotated += 2 * math.pi
-
-                        # Apply vertical mirror (negate theta)
-                        theta_mirrored = -theta_rotated
-                        if theta_mirrored < 0:  # Ensure theta stays within [0, 2π)
-                            theta_mirrored += 2 * math.pi
-
-                        coordinates.append((theta_mirrored, rho))
-                    else:
-                        coordinates.append((theta, rho))
+                    coordinates.append((theta, rho))
                 except ValueError:
                     print(f"Skipping invalid line: {line}")
     except Exception as e:
@@ -111,7 +98,7 @@ def run_theta_rho_file(file_path):
     global stop_requested
     stop_requested = False
 
-    coordinates = parse_theta_rho_file(file_path, apply_transformations = False)
+    coordinates = parse_theta_rho_file(file_path)
     if len(coordinates) < 2:
         print("Not enough coordinates for interpolation.")
         return
@@ -332,7 +319,7 @@ def preview_thr():
 
     try:
         # Parse the .thr file with transformations
-        coordinates = parse_theta_rho_file(file_path, apply_transformations=True)
+        coordinates = parse_theta_rho_file(file_path)
         return jsonify({'success': True, 'coordinates': coordinates})
     except Exception as e:
         return jsonify({'error': str(e)}), 500

+ 45 - 0
mirror_pattern.py

@@ -0,0 +1,45 @@
+import sys
+import os
+
+def reverse_theta(input_file, output_file):
+    # Check if the input file exists
+    if not os.path.isfile(input_file):
+        print(f"Error: File '{input_file}' not found.")
+        return
+
+    # Read the input file and process
+    with open(input_file, "r") as infile:
+        lines = infile.readlines()
+
+    with open(output_file, "w") as outfile:
+        for line in lines:
+            # Skip comment lines
+            if line.startswith("#"):
+                outfile.write(line)
+                continue
+
+            # Process lines with theta and rho values
+            try:
+                theta, rho = map(float, line.split())
+                reversed_theta = -theta  # Reverse the sign of theta
+                outfile.write(f"{reversed_theta:.5f} {rho:.5f}\n")
+            except ValueError:
+                # Handle any lines that don't match the expected format
+                outfile.write(line)
+
+    print(f"Reversed file saved as: {output_file}")
+
+
+def main():
+    if len(sys.argv) != 3:
+        print("Usage: python reverse_theta.py <input_file.thr> <output_file.thr>")
+        sys.exit(1)
+
+    input_file = sys.argv[1]
+    output_file = sys.argv[2]
+
+    reverse_theta(input_file, output_file)
+
+
+if __name__ == "__main__":
+    main()