加入所有顶点
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
def process_obj_file(input_file):
|
||||
"""
|
||||
处理OBJ文件,统计所有三角形顶点索引并找出缺失的索引
|
||||
"""
|
||||
# 用于存储所有三角形顶点索引
|
||||
triangle_indices = set()
|
||||
|
||||
try:
|
||||
with open(input_file, 'r', encoding='utf-8') as file:
|
||||
for line_num, line in enumerate(file, 1):
|
||||
line = line.strip()
|
||||
|
||||
# 只处理面数据(以'f'开头)
|
||||
if line.startswith('f '):
|
||||
# 分割面数据
|
||||
parts = line.split()
|
||||
|
||||
# 检查是否是三角形面(应该有4个部分:f + 3个顶点)
|
||||
if len(parts) == 4:
|
||||
try:
|
||||
# 提取顶点索引(OBJ索引从1开始,转换为从0开始)
|
||||
indices = []
|
||||
for part in parts[1:]: # 跳过第一个'f'
|
||||
# 处理可能的纹理/法线索引(格式:顶点索引/纹理索引/法线索引)
|
||||
vertex_part = part.split('/')[0]
|
||||
index = int(vertex_part) - 1 # 转换为0-based索引
|
||||
indices.append(index)
|
||||
|
||||
# 添加到集合中
|
||||
triangle_indices.update(indices)
|
||||
|
||||
except ValueError as e:
|
||||
print(f"警告: 第{line_num}行解析错误: {line} - {e}")
|
||||
else:
|
||||
print(f"警告: 第{line_num}行不是三角形面: {line}")
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"错误: 文件 '{input_file}' 未找到")
|
||||
return
|
||||
except Exception as e:
|
||||
print(f"错误: 读取文件时发生错误 - {e}")
|
||||
return
|
||||
|
||||
# 统计结果
|
||||
print(f"找到的三角形顶点索引数量: {len(triangle_indices)}")
|
||||
print(f"最大索引值: {max(triangle_indices) if triangle_indices else '无'}")
|
||||
print(f"最小索引值: {min(triangle_indices) if triangle_indices else '无'}")
|
||||
|
||||
# 找出0-478中缺失的索引
|
||||
all_indices = set(range(0, 479)) # 0到478
|
||||
missing_indices = all_indices - triangle_indices
|
||||
|
||||
print(f"\n缺失的索引 (0-478中未使用的):")
|
||||
if missing_indices:
|
||||
# 将缺失的索引排序并分组显示
|
||||
missing_sorted = sorted(missing_indices)
|
||||
print(f"总共缺失 {len(missing_sorted)} 个索引:")
|
||||
|
||||
# 分组显示,每行显示10个
|
||||
for i in range(0, len(missing_sorted), 10):
|
||||
group = missing_sorted[i:i+10]
|
||||
print(" ".join(f"{idx:3d}" for idx in group))
|
||||
else:
|
||||
print("没有缺失的索引,0-478全部被使用")
|
||||
|
||||
# 额外信息:显示使用的索引范围
|
||||
if triangle_indices:
|
||||
used_sorted = sorted(triangle_indices)
|
||||
print(f"\n使用的索引范围: {used_sorted[0]} - {used_sorted[-1]}")
|
||||
|
||||
# 检查是否有超出0-478范围的索引
|
||||
out_of_range = [idx for idx in triangle_indices if idx < 0 or idx > 478]
|
||||
if out_of_range:
|
||||
print(f"警告: 发现超出0-478范围的索引: {sorted(out_of_range)}")
|
||||
|
||||
|
||||
def process_obj_file_advanced(input_file):
|
||||
"""
|
||||
高级版本:提供更详细的分析信息
|
||||
"""
|
||||
triangle_indices = set()
|
||||
face_count = 0
|
||||
|
||||
try:
|
||||
with open(input_file, 'r', encoding='utf-8') as file:
|
||||
for line in file:
|
||||
line = line.strip()
|
||||
|
||||
if line.startswith('f '):
|
||||
parts = line.split()
|
||||
if len(parts) >= 4: # 三角形或多边形
|
||||
face_count += 1
|
||||
try:
|
||||
for part in parts[1:4]: # 只取前三个顶点(对于三角形)
|
||||
vertex_part = part.split('/')[0]
|
||||
index = int(vertex_part) - 1
|
||||
triangle_indices.add(index)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
print(f"错误: {e}")
|
||||
return
|
||||
|
||||
# 基本统计
|
||||
all_indices = set(range(0, 479))
|
||||
missing_indices = all_indices - triangle_indices
|
||||
|
||||
print("=" * 50)
|
||||
print("高级版本分析结果:")
|
||||
print("=" * 50)
|
||||
print(f"总面数: {face_count}")
|
||||
print(f"使用的唯一顶点索引数: {len(triangle_indices)}")
|
||||
print(f"缺失的顶点索引数: {len(missing_indices)}")
|
||||
print(f"顶点索引使用率: {len(triangle_indices)/479*100:.1f}%")
|
||||
|
||||
if missing_indices:
|
||||
missing_sorted = sorted(missing_indices)
|
||||
print(f"\n缺失的索引列表:")
|
||||
# 显示连续的缺失范围
|
||||
ranges = []
|
||||
start = missing_sorted[0]
|
||||
for i in range(1, len(missing_sorted)):
|
||||
if missing_sorted[i] != missing_sorted[i-1] + 1:
|
||||
ranges.append((start, missing_sorted[i-1]))
|
||||
start = missing_sorted[i]
|
||||
ranges.append((start, missing_sorted[-1]))
|
||||
|
||||
for rstart, rend in ranges:
|
||||
if rstart == rend:
|
||||
print(f" {rstart}")
|
||||
else:
|
||||
print(f" {rstart} - {rend}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
input_file = "face.obj" # 默认输入文件
|
||||
|
||||
# 使用基本版本
|
||||
print("基本版本结果:")
|
||||
process_obj_file(input_file)
|
||||
|
||||
print("\n" + "="*60 + "\n")
|
||||
|
||||
# 使用高级版本
|
||||
print("高级版本结果:")
|
||||
process_obj_file_advanced(input_file)
|
||||
Reference in New Issue
Block a user