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# def checkEncoding(filepath):
|
| 5 |
+
# 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 getModulePart(start_line, end_line, lines, main_section, offsetX, offsetY, offsetZ):
|
| 30 |
+
# for j in range(start_line, end_line):
|
| 31 |
+
# line = lines[j]
|
| 32 |
+
# if line.startswith("1 "):
|
| 33 |
+
# parts = line.strip().split()
|
| 34 |
+
# if len(parts) >= 5:
|
| 35 |
+
# try:
|
| 36 |
+
# x = float(parts[2]) + offsetX
|
| 37 |
+
# y = float(parts[3]) + offsetY
|
| 38 |
+
# z = float(parts[4]) + offsetZ
|
| 39 |
+
# parts[2] = str(x)
|
| 40 |
+
# parts[3] = str(y)
|
| 41 |
+
# parts[4] = str(z)
|
| 42 |
+
# line = " ".join(parts) + "\n"
|
| 43 |
+
# except ValueError as e:
|
| 44 |
+
# print(e)
|
| 45 |
+
# pass
|
| 46 |
+
# main_section.append(line)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# def process_ldr(filepath, output_file, offsetX, offsetY, offsetZ):
|
| 50 |
+
# if os.path.isfile(filepath):
|
| 51 |
+
# lines = readTextFile(filepath)
|
| 52 |
+
# if lines is None:
|
| 53 |
+
# print("Could not read file {0}".format(filepath))
|
| 54 |
+
# lines = []
|
| 55 |
+
# else:
|
| 56 |
+
# lines = []
|
| 57 |
+
|
| 58 |
+
# main_section = []
|
| 59 |
+
# startLine = 0
|
| 60 |
+
# endLine = 0
|
| 61 |
+
# lineCount = 0
|
| 62 |
+
# foundEnd = False
|
| 63 |
+
|
| 64 |
+
# for line in lines:
|
| 65 |
+
# parameters = line.strip().split()
|
| 66 |
+
# if len(parameters) > 2:
|
| 67 |
+
# if parameters[0] == "0" and parameters[1] == "FILE":
|
| 68 |
+
# if foundEnd == False:
|
| 69 |
+
# endLine = lineCount
|
| 70 |
+
# if endLine > startLine:
|
| 71 |
+
# getModulePart(startLine, endLine, lines, main_section, offsetX, offsetY, offsetZ)
|
| 72 |
+
# foundEnd = True
|
| 73 |
+
# break
|
| 74 |
+
|
| 75 |
+
# startLine = lineCount
|
| 76 |
+
# foundEnd = False
|
| 77 |
+
|
| 78 |
+
# #import ipdb; ipdb.set_trace()
|
| 79 |
+
# if parameters[0] == "0" and parameters[1] == "NOFILE":
|
| 80 |
+
# endLine = lineCount
|
| 81 |
+
# foundEnd = True
|
| 82 |
+
# getModulePart(startLine, endLine, lines, main_section, offsetX, offsetY, offsetZ)
|
| 83 |
+
# break
|
| 84 |
+
# lineCount += 1
|
| 85 |
+
|
| 86 |
+
# if lineCount < len(lines):
|
| 87 |
+
# remaining = lines[lineCount:]
|
| 88 |
+
# else:
|
| 89 |
+
# remaining = []
|
| 90 |
+
|
| 91 |
+
# if foundEnd == False:
|
| 92 |
+
# endLine = lineCount
|
| 93 |
+
# if endLine > startLine:
|
| 94 |
+
# getModulePart(startLine, endLine, lines, main_section, offsetX, offsetY, offsetZ)
|
| 95 |
+
|
| 96 |
+
# import ipdb; ipdb.set_trace()
|
| 97 |
+
# new_section = main_section + remaining
|
| 98 |
+
# with open(output_file, "w", encoding="utf-8") as f:
|
| 99 |
+
# f.writelines(new_section)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# process_ldr("/public/home/wangshuo/gap/assembly/data/car_1k/ldr/car_5/car_5.ldr", "car_5_normalize_woremaining.ldr", 6000, 150, 2000)
|
| 103 |
+
|
| 104 |
+
import os
|
| 105 |
+
|
| 106 |
+
def checkEncoding(filepath):
|
| 107 |
+
filepath = filepath
|
| 108 |
+
with open(filepath, "rb") as encode_check:
|
| 109 |
+
encoding = encode_check.readline(3)
|
| 110 |
+
if encoding == b"\xfe\xff\x00":
|
| 111 |
+
return "utf_16_be"
|
| 112 |
+
elif encoding == b"\xff\xfe0":
|
| 113 |
+
return "utf_16_le"
|
| 114 |
+
else:
|
| 115 |
+
return "utf_8"
|
| 116 |
+
|
| 117 |
+
def readTextFile(filepath):
|
| 118 |
+
filepath = filepath
|
| 119 |
+
lines = None
|
| 120 |
+
if os.path.exists(filepath):
|
| 121 |
+
file_encoding = checkEncoding(filepath)
|
| 122 |
+
try:
|
| 123 |
+
with open(filepath, "rt", encoding=file_encoding) as f_in:
|
| 124 |
+
lines = f_in.readlines()
|
| 125 |
+
except:
|
| 126 |
+
with open(filepath, "rt", encoding="latin_1") as f_in:
|
| 127 |
+
lines = f_in.readlines()
|
| 128 |
+
|
| 129 |
+
return lines
|
| 130 |
+
|
| 131 |
+
def getModulePart(start_line, end_line, lines, main_section, offsetX, offsetY, offsetZ):
|
| 132 |
+
max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf')
|
| 133 |
+
|
| 134 |
+
for j in range(start_line, end_line):
|
| 135 |
+
line = lines[j]
|
| 136 |
+
if line.startswith("1 "):
|
| 137 |
+
parts = line.strip().split()
|
| 138 |
+
if len(parts) >= 5:
|
| 139 |
+
try:
|
| 140 |
+
x = float(parts[2]) + offsetX
|
| 141 |
+
y = float(parts[3]) + offsetY
|
| 142 |
+
z = float(parts[4]) + offsetZ
|
| 143 |
+
parts[2] = str(x)
|
| 144 |
+
parts[3] = str(y)
|
| 145 |
+
parts[4] = str(z)
|
| 146 |
+
line = " ".join(parts) + "\n"
|
| 147 |
+
|
| 148 |
+
# Update max coordinates
|
| 149 |
+
max_x = max(max_x, x)
|
| 150 |
+
max_y = max(max_y, y)
|
| 151 |
+
max_z = max(max_z, z)
|
| 152 |
+
|
| 153 |
+
except ValueError as e:
|
| 154 |
+
print(e)
|
| 155 |
+
pass
|
| 156 |
+
main_section.append(line)
|
| 157 |
+
|
| 158 |
+
return max_x, max_y, max_z
|
| 159 |
+
|
| 160 |
+
def getModulePartMin(start_line, end_line, lines, main_section):
|
| 161 |
+
min_x, min_y, min_z = float('inf'), float('inf'), float('inf')
|
| 162 |
+
|
| 163 |
+
for j in range(start_line, end_line):
|
| 164 |
+
line = lines[j]
|
| 165 |
+
if line.startswith("1 "):
|
| 166 |
+
parts = line.strip().split()
|
| 167 |
+
if len(parts) >= 5:
|
| 168 |
+
try:
|
| 169 |
+
x = float(parts[2])
|
| 170 |
+
y = float(parts[3])
|
| 171 |
+
z = float(parts[4])
|
| 172 |
+
parts[2] = str(x)
|
| 173 |
+
parts[3] = str(y)
|
| 174 |
+
parts[4] = str(z)
|
| 175 |
+
line = " ".join(parts) + "\n"
|
| 176 |
+
|
| 177 |
+
# Update min coordinates
|
| 178 |
+
min_x = min(min_x, x)
|
| 179 |
+
min_y = min(min_y, y)
|
| 180 |
+
min_z = min(min_z, z)
|
| 181 |
+
|
| 182 |
+
except ValueError as e:
|
| 183 |
+
print(e)
|
| 184 |
+
pass
|
| 185 |
+
main_section.append(line)
|
| 186 |
+
|
| 187 |
+
return min_x, min_y, min_z
|
| 188 |
+
|
| 189 |
+
def process_ldr(filepath, output_file, offsetX, offsetY, offsetZ, keep_remaining=False):
|
| 190 |
+
if os.path.isfile(filepath):
|
| 191 |
+
lines = readTextFile(filepath)
|
| 192 |
+
if lines is None:
|
| 193 |
+
print("Could not read file {0}".format(filepath))
|
| 194 |
+
lines = []
|
| 195 |
+
else:
|
| 196 |
+
lines = []
|
| 197 |
+
|
| 198 |
+
main_section = []
|
| 199 |
+
startLine = 0
|
| 200 |
+
endLine = 0
|
| 201 |
+
lineCount = 0
|
| 202 |
+
foundEnd = False
|
| 203 |
+
max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf') # Initialize max values
|
| 204 |
+
|
| 205 |
+
for line in lines:
|
| 206 |
+
parameters = line.strip().split()
|
| 207 |
+
if len(parameters) > 2:
|
| 208 |
+
if parameters[0] == "0" and parameters[1] == "FILE":
|
| 209 |
+
if foundEnd == False:
|
| 210 |
+
endLine = lineCount
|
| 211 |
+
if endLine > startLine:
|
| 212 |
+
local_max_x, local_max_y, local_max_z = getModulePart(startLine, endLine, lines, main_section, offsetX, offsetY, offsetZ)
|
| 213 |
+
max_x = max(max_x, local_max_x)
|
| 214 |
+
max_y = max(max_y, local_max_y)
|
| 215 |
+
max_z = max(max_z, local_max_z)
|
| 216 |
+
foundEnd = True
|
| 217 |
+
break
|
| 218 |
+
|
| 219 |
+
startLine = lineCount
|
| 220 |
+
foundEnd = False
|
| 221 |
+
|
| 222 |
+
if parameters[0] == "0" and parameters[1] == "NOFILE":
|
| 223 |
+
endLine = lineCount
|
| 224 |
+
foundEnd = True
|
| 225 |
+
local_max_x, local_max_y, local_max_z = getModulePart(startLine, endLine, lines, main_section, offsetX, offsetY, offsetZ)
|
| 226 |
+
max_x = max(max_x, local_max_x)
|
| 227 |
+
max_y = max(max_y, local_max_y)
|
| 228 |
+
max_z = max(max_z, local_max_z)
|
| 229 |
+
break
|
| 230 |
+
lineCount += 1
|
| 231 |
+
|
| 232 |
+
if lineCount < len(lines):
|
| 233 |
+
remaining = lines[lineCount:]
|
| 234 |
+
else:
|
| 235 |
+
remaining = []
|
| 236 |
+
|
| 237 |
+
if foundEnd == False:
|
| 238 |
+
endLine = lineCount
|
| 239 |
+
if endLine > startLine:
|
| 240 |
+
local_max_x, local_max_y, local_max_z = getModulePart(startLine, endLine, lines, main_section, offsetX, offsetY, offsetZ)
|
| 241 |
+
max_x = max(max_x, local_max_x)
|
| 242 |
+
max_y = max(max_y, local_max_y)
|
| 243 |
+
max_z = max(max_z, local_max_z)
|
| 244 |
+
|
| 245 |
+
# import ipdb; ipdb.set_trace()
|
| 246 |
+
if keep_remaining == False:
|
| 247 |
+
remaining = []
|
| 248 |
+
#import ipdb; ipdb.set_trace()
|
| 249 |
+
new_section = main_section + remaining
|
| 250 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
| 251 |
+
f.writelines(new_section)
|
| 252 |
+
|
| 253 |
+
return max_x, max_y, max_z # Return the maximum coordinates
|
| 254 |
+
|
| 255 |
+
def getmin_ldr(filepath):
|
| 256 |
+
if os.path.isfile(filepath):
|
| 257 |
+
lines = readTextFile(filepath)
|
| 258 |
+
if lines is None:
|
| 259 |
+
print("Could not read file {0}".format(filepath))
|
| 260 |
+
lines = []
|
| 261 |
+
else:
|
| 262 |
+
lines = []
|
| 263 |
+
|
| 264 |
+
main_section = []
|
| 265 |
+
startLine = 0
|
| 266 |
+
endLine = 0
|
| 267 |
+
lineCount = 0
|
| 268 |
+
foundEnd = False
|
| 269 |
+
min_x, min_y, min_z = float('inf'), float('inf'), float('inf') # Initialize max values
|
| 270 |
+
|
| 271 |
+
for line in lines:
|
| 272 |
+
parameters = line.strip().split()
|
| 273 |
+
if len(parameters) > 2:
|
| 274 |
+
if parameters[0] == "0" and parameters[1] == "FILE":
|
| 275 |
+
if foundEnd == False:
|
| 276 |
+
endLine = lineCount
|
| 277 |
+
if endLine > startLine:
|
| 278 |
+
local_min_x, local_min_y, local_min_z = getModulePartMin(startLine, endLine, lines, main_section)
|
| 279 |
+
min_x = min(min_x, local_min_x)
|
| 280 |
+
min_y = min(min_y, local_min_y)
|
| 281 |
+
min_z = min(min_z, local_min_z)
|
| 282 |
+
foundEnd = True
|
| 283 |
+
break
|
| 284 |
+
|
| 285 |
+
startLine = lineCount
|
| 286 |
+
foundEnd = False
|
| 287 |
+
|
| 288 |
+
if parameters[0] == "0" and parameters[1] == "NOFILE":
|
| 289 |
+
endLine = lineCount
|
| 290 |
+
foundEnd = True
|
| 291 |
+
local_min_x, local_min_y, local_min_z = getModulePartMin(startLine, endLine, lines, main_section)
|
| 292 |
+
min_x = min(min_x, local_min_x)
|
| 293 |
+
min_y = min(min_y, local_min_y)
|
| 294 |
+
min_z = min(min_z, local_min_z)
|
| 295 |
+
break
|
| 296 |
+
lineCount += 1
|
| 297 |
+
|
| 298 |
+
if lineCount < len(lines):
|
| 299 |
+
remaining = lines[lineCount:]
|
| 300 |
+
else:
|
| 301 |
+
remaining = []
|
| 302 |
+
|
| 303 |
+
if foundEnd == False:
|
| 304 |
+
endLine = lineCount
|
| 305 |
+
if endLine > startLine:
|
| 306 |
+
local_min_x, local_min_y, local_min_z = getModulePartMin(startLine, endLine, lines, main_section)
|
| 307 |
+
min_x = min(min_x, local_min_x)
|
| 308 |
+
min_y = min(min_y, local_min_y)
|
| 309 |
+
min_z = min(min_z, local_min_z)
|
| 310 |
+
|
| 311 |
+
new_section = main_section + remaining
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
return min_x, min_y, min_z # Return the maximum coordinates
|
| 315 |
+
|
| 316 |
+
# # # Example call
|
| 317 |
+
# max_x, max_y, max_z = process_ldr("/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/ldr_rotated_l30/car_1293_rot.ldr",
|
| 318 |
+
# "car_1293_normalize_woremaining.ldr", 6000, 150, 2000)
|
| 319 |
+
|
| 320 |
+
# print(f"Maximum coordinates after translation: X = {max_x}, Y = {max_y}, Z = {max_z}")
|