Silly98 commited on
Commit
3f83b75
·
verified ·
1 Parent(s): faad2e5

Create vtp_to obj and eseg

Browse files
Files changed (1) hide show
  1. vtp_to obj and eseg +77 -0
vtp_to obj and eseg ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import vtk
2
+ # import os
3
+
4
+ # # --------------------------
5
+ # # Input root folder
6
+ # # --------------------------
7
+ # input_root = r"C:\Users\nandh\Desktop\MeshCNN\vtp_inference"
8
+ # output_root = os.path.join(os.path.dirname(input_root), "test_diffusionNet")
9
+
10
+ # # Create output subfolders
11
+ # for sub in ["inf","seg"]:
12
+ # os.makedirs(os.path.join(output_root, sub), exist_ok=True)
13
+
14
+ # # --------------------------
15
+ # # Process train, test, and inf folders
16
+ # # --------------------------
17
+ # for split in ["inf"]:
18
+ # input_folder = os.path.join(input_root, split)
19
+ # output_obj_folder = os.path.join(output_root, split)
20
+ # output_seg_folder = os.path.join(output_root, "seg") if split != "cat" else None
21
+
22
+ # if not os.path.exists(input_folder):
23
+ # print(f"⚠️ Skipping missing folder: {input_folder}")
24
+ # continue
25
+
26
+ # for fname in os.listdir(input_folder):
27
+ # if fname.lower().endswith(".vtp"):
28
+ # in_path = os.path.join(input_folder, fname)
29
+ # base_name = os.path.splitext(fname)[0] # filename without extension
30
+
31
+ # # Read VTP
32
+ # reader = vtk.vtkXMLPolyDataReader()
33
+ # reader.SetFileName(in_path)
34
+ # reader.Update()
35
+ # mesh = reader.GetOutput()
36
+
37
+ # # Get vertices
38
+ # verts = [mesh.GetPoint(i) for i in range(mesh.GetNumberOfPoints())]
39
+
40
+ # # Get faces
41
+ # faces = []
42
+ # for i in range(mesh.GetNumberOfCells()):
43
+ # cell = mesh.GetCell(i)
44
+ # ids = [cell.GetPointId(j) for j in range(cell.GetNumberOfPoints())]
45
+ # if len(ids) == 3: # only triangles
46
+ # faces.append(ids)
47
+
48
+ # # --------------------------
49
+ # # Save OBJ
50
+ # # --------------------------
51
+ # obj_path = os.path.join(output_obj_folder, base_name + ".obj")
52
+ # with open(obj_path, "w") as f:
53
+ # for v in verts:
54
+ # f.write(f"v {v[0]} {v[1]} {v[2]}\n")
55
+ # for face in faces:
56
+ # f.write(f"f {face[0]+1} {face[1]+1} {face[2]+1}\n")
57
+
58
+ # # --------------------------
59
+ # # Save ESEG (only for train/test, not for inf)
60
+ # # --------------------------
61
+ # if output_seg_folder:
62
+ # labels_array = mesh.GetCellData().GetArray("Label")
63
+ # if labels_array is None:
64
+ # raise ValueError(f"❌ No 'Label' cell data found in {fname}")
65
+
66
+ # face_labels = [int(labels_array.GetTuple1(i)) for i in range(mesh.GetNumberOfCells())]
67
+
68
+ # seg_path = os.path.join(output_seg_folder, base_name + ".txt")
69
+ # with open(seg_path, "w") as f:
70
+ # for lbl in face_labels:
71
+ # f.write(f"{lbl}\n")
72
+
73
+ # print(f"✅ Converted {split}/{fname} → {split}/{base_name}.obj and seg/{base_name}.txt")
74
+ # else:
75
+ # print(f"✅ Converted {split}/{fname} → {split}/{base_name}.obj (no .eseg for inference)")
76
+
77
+ # print(f"\n🎉 All VTP files converted! Output root: {output_root}")