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 |
+
filepath = filepath
|
| 6 |
+
with open(filepath, "rb") as encode_check:
|
| 7 |
+
encoding = encode_check.readline(3)
|
| 8 |
+
if encoding == b"\xfe\xff\x00":
|
| 9 |
+
return "utf_16_be"
|
| 10 |
+
elif encoding == b"\xff\xfe0":
|
| 11 |
+
return "utf_16_le"
|
| 12 |
+
else:
|
| 13 |
+
return "utf_8"
|
| 14 |
+
|
| 15 |
+
def readTextFile(filepath):
|
| 16 |
+
filepath = filepath
|
| 17 |
+
lines = None
|
| 18 |
+
if os.path.exists(filepath):
|
| 19 |
+
file_encoding = checkEncoding(filepath)
|
| 20 |
+
try:
|
| 21 |
+
with open(filepath, "rt", encoding=file_encoding) as f_in:
|
| 22 |
+
lines = f_in.readlines()
|
| 23 |
+
except:
|
| 24 |
+
with open(filepath, "rt", encoding="latin_1") as f_in:
|
| 25 |
+
lines = f_in.readlines()
|
| 26 |
+
|
| 27 |
+
return lines
|
| 28 |
+
|
| 29 |
+
def apply_rotation(x, y, z, rot_mat):
|
| 30 |
+
# 旋转矩阵乘法: [x', y', z'] = [x, y, z] * rot_mat
|
| 31 |
+
new_x = rot_mat[0][0] * x + rot_mat[0][1] * y + rot_mat[0][2] * z
|
| 32 |
+
new_y = rot_mat[1][0] * x + rot_mat[1][1] * y + rot_mat[1][2] * z
|
| 33 |
+
new_z = rot_mat[2][0] * x + rot_mat[2][1] * y + rot_mat[2][2] * z
|
| 34 |
+
return new_x, new_y, new_z
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def apply_rotation_matrix(orig_mat, rot_mat):
|
| 38 |
+
# numpy 矩阵乘法
|
| 39 |
+
orig_mat_np = np.array(orig_mat).reshape((3,3))
|
| 40 |
+
rot_mat_np = np.array(rot_mat)
|
| 41 |
+
new_mat = rot_mat_np @ orig_mat_np
|
| 42 |
+
return new_mat.flatten().tolist()
|
| 43 |
+
|
| 44 |
+
def getModulePart(start_line, end_line, lines, main_section, rot_mat):
|
| 45 |
+
for j in range(start_line, end_line):
|
| 46 |
+
line = lines[j]
|
| 47 |
+
if line.startswith("1 "):
|
| 48 |
+
parts = line.strip().split()
|
| 49 |
+
if len(parts) >= 12: # x,y,z + 9 个矩阵元素
|
| 50 |
+
try:
|
| 51 |
+
# 获取原坐标
|
| 52 |
+
x = float(parts[2])
|
| 53 |
+
y = float(parts[3])
|
| 54 |
+
z = float(parts[4])
|
| 55 |
+
|
| 56 |
+
# 使用旋转矩阵进行坐标变换
|
| 57 |
+
x_rot, y_rot, z_rot = apply_rotation(x, y, z, rot_mat)
|
| 58 |
+
|
| 59 |
+
# 更新坐标
|
| 60 |
+
parts[2] = str(x_rot)
|
| 61 |
+
parts[3] = str(y_rot)
|
| 62 |
+
parts[4] = str(z_rot)
|
| 63 |
+
|
| 64 |
+
# 获取原旋转矩阵
|
| 65 |
+
orig_mat = [float(p) for p in parts[5:14]]
|
| 66 |
+
|
| 67 |
+
# 旋转零件自身旋转矩阵
|
| 68 |
+
new_mat = apply_rotation_matrix(orig_mat, rot_mat)
|
| 69 |
+
|
| 70 |
+
# 更新矩阵
|
| 71 |
+
for idx, val in enumerate(new_mat):
|
| 72 |
+
parts[5 + idx] = str(val)
|
| 73 |
+
|
| 74 |
+
# 重新拼接并更新行
|
| 75 |
+
line = " ".join(parts) + "\n"
|
| 76 |
+
except ValueError as e:
|
| 77 |
+
print(e)
|
| 78 |
+
pass
|
| 79 |
+
main_section.append(line)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def process_ldr(filepath, output_file, rot_mat):
|
| 84 |
+
if os.path.isfile(filepath):
|
| 85 |
+
lines = readTextFile(filepath)
|
| 86 |
+
if lines is None:
|
| 87 |
+
print("Could not read file {0}".format(filepath))
|
| 88 |
+
lines = []
|
| 89 |
+
else:
|
| 90 |
+
lines = []
|
| 91 |
+
|
| 92 |
+
main_section = []
|
| 93 |
+
startLine = 0
|
| 94 |
+
endLine = 0
|
| 95 |
+
lineCount = 0
|
| 96 |
+
foundEnd = False
|
| 97 |
+
|
| 98 |
+
for line in lines:
|
| 99 |
+
parameters = line.strip().split()
|
| 100 |
+
if len(parameters) > 2:
|
| 101 |
+
if parameters[0] == "0" and parameters[1] == "FILE":
|
| 102 |
+
if foundEnd == False:
|
| 103 |
+
endLine = lineCount
|
| 104 |
+
if endLine > startLine:
|
| 105 |
+
getModulePart(startLine, endLine, lines, main_section, rot_mat)
|
| 106 |
+
foundEnd = True
|
| 107 |
+
break
|
| 108 |
+
|
| 109 |
+
startLine = lineCount
|
| 110 |
+
foundEnd = False
|
| 111 |
+
|
| 112 |
+
if parameters[0] == "0" and parameters[1] == "NOFILE":
|
| 113 |
+
endLine = lineCount
|
| 114 |
+
foundEnd = True
|
| 115 |
+
getModulePart(startLine, endLine, lines, main_section, rot_mat)
|
| 116 |
+
break
|
| 117 |
+
lineCount += 1
|
| 118 |
+
|
| 119 |
+
if lineCount < len(lines):
|
| 120 |
+
remaining = lines[lineCount:]
|
| 121 |
+
else:
|
| 122 |
+
remaining = []
|
| 123 |
+
|
| 124 |
+
if foundEnd == False:
|
| 125 |
+
endLine = lineCount
|
| 126 |
+
if endLine > startLine:
|
| 127 |
+
getModulePart(startLine, endLine, lines, main_section, rot_mat)
|
| 128 |
+
|
| 129 |
+
new_section = main_section + remaining
|
| 130 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
| 131 |
+
f.writelines(new_section)
|
| 132 |
+
return True
|
| 133 |
+
|
| 134 |
+
# 旋转矩阵:绕 Y 轴旋转 90 度
|
| 135 |
+
rot_mat = [
|
| 136 |
+
[0, 0, 1],
|
| 137 |
+
[0, 1, 0],
|
| 138 |
+
[-1, 0, 0]
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
+
# # 调用函数处理 LDR 文件,进行旋转
|
| 142 |
+
# def main():
|
| 143 |
+
# process_ldr("/public/home/wangshuo/gap/assembly/data/car_1k/ldr/car_5/car_5.ldr", "car_5_rotate.ldr", rot_mat)
|
| 144 |
+
|
| 145 |
+
# main()
|