stpete2 commited on
Commit
6d8676e
·
verified ·
1 Parent(s): bdcd245

Upload preprocess_manual.js

Browse files
Files changed (1) hide show
  1. preprocess_manual.js +136 -71
preprocess_manual.js CHANGED
@@ -1097,14 +1097,20 @@ parser.parsePLY(gsPath, true).then((plyData) => {
1097
  // PCAではなく単純な重心で中心化
1098
  // ========================================
1099
 
 
 
 
 
 
 
 
1100
  console.log('[preprocessManual] ===== STARTING AUTO-ALIGNMENT =====');
1101
 
1102
  // ========================================
1103
- // STEP 1: 単純な重心で中心化(1回だけ)
1104
  // ========================================
1105
- console.log('[preprocessManual] Step 1: Centering PLY using center of mass...');
1106
 
1107
- // 重心を計算
1108
  let sumX = 0, sumY = 0, sumZ = 0;
1109
  plyData.vertices.forEach(v => {
1110
  sumX += v.x;
@@ -1119,102 +1125,131 @@ const center = {
1119
  };
1120
 
1121
  console.log('[DEBUG] Center of mass:', {
1122
- x: center.x.toFixed(3),
1123
- y: center.y.toFixed(3),
1124
- z: center.z.toFixed(3)
1125
  });
1126
 
1127
  console.log('[DEBUG] First vertex BEFORE centering:', {
1128
- x: plyData.vertices[0].x.toFixed(3),
1129
- y: plyData.vertices[0].y.toFixed(3),
1130
- z: plyData.vertices[0].z.toFixed(3)
1131
  });
1132
 
1133
- // 中心化実行
1134
- plyData.vertices = plyData.vertices.map(v => {
1135
- const centered = {
1136
- x: v.x - center.x,
1137
- y: v.y - center.y,
1138
- z: v.z - center.z
1139
- };
 
 
 
 
 
1140
 
1141
- // 他のプロパティをコピー
1142
- Object.keys(v).forEach(key => {
 
 
 
 
 
1143
  if (key !== 'x' && key !== 'y' && key !== 'z') {
1144
- centered[key] = v[key];
1145
  }
1146
- });
1147
 
1148
- return centered;
1149
- });
 
 
 
1150
 
1151
  console.log('[DEBUG] First vertex AFTER centering:', {
1152
- x: plyData.vertices[0].x.toFixed(3),
1153
- y: plyData.vertices[0].y.toFixed(3),
1154
- z: plyData.vertices[0].z.toFixed(3)
1155
  });
1156
 
1157
- // 検証: 平均を再計算
 
 
 
 
1158
  sumX = 0; sumY = 0; sumZ = 0;
1159
- plyData.vertices.forEach(v => {
1160
- sumX += v.x;
1161
- sumY += v.y;
1162
- sumZ += v.z;
1163
- });
 
1164
  const avgX = sumX / plyData.vertices.length;
1165
  const avgY = sumY / plyData.vertices.length;
1166
  const avgZ = sumZ / plyData.vertices.length;
1167
 
1168
  console.log('[DEBUG] Average after centering:', {
1169
- x: avgX.toFixed(6),
1170
- y: avgY.toFixed(6),
1171
- z: avgZ.toFixed(6)
1172
  });
1173
 
1174
- if (Math.abs(avgX) > 0.001 || Math.abs(avgY) > 0.001 || Math.abs(avgZ) > 0.001) {
1175
  console.error('[DEBUG] ❌ ERROR: Centering FAILED!');
1176
- console.error('[DEBUG] Average should be ~0 but got:', { avgX, avgY, avgZ });
1177
- alert('❌ PLY Centering Failed!\n\nPlease reload and try again.');
 
 
 
 
 
 
 
 
 
1178
  return;
1179
- } else {
1180
- console.log('[DEBUG] ✓ Centering SUCCESS! Average is ~0');
1181
  }
1182
 
 
1183
  console.log('[preprocessManual] ✓ PLY centered at origin');
1184
 
1185
  // ========================================
1186
- // STEP 2: バウンディングボックスを再計算
1187
  // ========================================
1188
- console.log('[preprocessManual] Step 2: Recalculating bounding box...');
 
1189
  let minX = Infinity, minY = Infinity, minZ = Infinity;
1190
  let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
1191
 
1192
- plyData.vertices.forEach(v => {
 
1193
  minX = Math.min(minX, v.x);
1194
  minY = Math.min(minY, v.y);
1195
  minZ = Math.min(minZ, v.z);
1196
  maxX = Math.max(maxX, v.x);
1197
  maxY = Math.max(maxY, v.y);
1198
  maxZ = Math.max(maxZ, v.z);
1199
- });
1200
 
1201
  const sizeX = maxX - minX;
1202
  const sizeY = maxY - minY;
1203
  const sizeZ = maxZ - minZ;
1204
 
1205
- console.log('[preprocessManual] Centered sizes:', {
1206
- sizeX: sizeX.toFixed(2),
1207
- sizeY: sizeY.toFixed(2),
1208
- sizeZ: sizeZ.toFixed(2)
1209
  });
1210
 
1211
  // ========================================
1212
- // STEP 3: Y軸が最長になるように回転
1213
  // ========================================
1214
- console.log('[preprocessManual] Step 3: Checking if rotation is needed...');
1215
  console.log('[preprocessManual] Longest axis:',
1216
- sizeX > sizeY && sizeX > sizeZ ? 'X' :
1217
- sizeZ > sizeY ? 'Z' : 'Y');
 
1218
 
1219
  let rotationNeeded = new THREE.Matrix4();
1220
  let rotationApplied = false;
@@ -1232,47 +1267,49 @@ if (sizeX > sizeY && sizeX > sizeZ) {
1232
  }
1233
 
1234
  // ========================================
1235
- // STEP 4: 回転を適用(必要な場合のみ)
1236
  // ========================================
1237
  if (rotationApplied) {
1238
- console.log('[preprocessManual] Step 4: Applying rotation to all vertices...');
1239
 
1240
- plyData.vertices.forEach(v => {
 
1241
  const pos = new THREE.Vector3(v.x, v.y, v.z);
1242
  pos.applyMatrix4(rotationNeeded);
 
1243
  v.x = pos.x;
1244
  v.y = pos.y;
1245
  v.z = pos.z;
1246
- });
1247
 
1248
- console.log('[preprocessManual] ✓ PLY rotated to align with world axes');
1249
 
1250
  window.plyAlignmentApplied = true;
1251
  window.plyAlignmentMatrix = rotationNeeded.clone();
1252
 
1253
  console.log('[preprocessManual] ✓ Alignment matrix saved for GS display');
1254
  } else {
1255
- console.log('[preprocessManual] Step 4: Skipped (no rotation needed)');
1256
  window.plyAlignmentApplied = false;
1257
  }
1258
 
1259
  // ========================================
1260
- // STEP 5: 最終検証
1261
  // ========================================
1262
- console.log('[preprocessManual] Step 5: Final verification...');
1263
 
1264
- // 最終的な範囲を再計算
1265
  minX = Infinity; minY = Infinity; minZ = Infinity;
1266
  maxX = -Infinity; maxY = -Infinity; maxZ = -Infinity;
1267
 
1268
- plyData.vertices.forEach(v => {
 
1269
  minX = Math.min(minX, v.x);
1270
  minY = Math.min(minY, v.y);
1271
  minZ = Math.min(minZ, v.z);
1272
  maxX = Math.max(maxX, v.x);
1273
  maxY = Math.max(maxY, v.y);
1274
  maxZ = Math.max(maxZ, v.z);
1275
- });
1276
 
1277
  const finalCenter = {
1278
  x: (minX + maxX) / 2,
@@ -1280,23 +1317,51 @@ const finalCenter = {
1280
  z: (minZ + maxZ) / 2
1281
  };
1282
 
1283
- console.log('[preprocessManual] Final PLY bounds:', {
1284
- min: { x: minX.toFixed(3), y: minY.toFixed(3), z: minZ.toFixed(3) },
1285
- max: { x: maxX.toFixed(3), y: maxY.toFixed(3), z: maxZ.toFixed(3) },
1286
- center: { x: finalCenter.x.toFixed(3), y: finalCenter.y.toFixed(3), z: finalCenter.z.toFixed(3) }
 
 
 
 
 
 
1287
  });
1288
 
1289
- if (Math.abs(finalCenter.x) > 0.01 || Math.abs(finalCenter.y) > 0.01 || Math.abs(finalCenter.z) > 0.01) {
 
 
 
 
1290
  console.error('[preprocessManual] ❌ ERROR: Final center is not at origin!');
1291
- console.error('[preprocessManual] Got:', finalCenter);
1292
- alert(' PLY Alignment Error!\n\nFinal center is not at origin.\nPlease reload and try again.');
 
 
 
 
 
 
 
 
 
 
 
 
 
1293
  return;
1294
  }
1295
 
 
1296
  console.log('[preprocessManual] ✓ PLY is properly centered and aligned');
 
 
 
 
 
 
1297
 
1298
- console.log('[preprocessManual] ✓ PLY rotated to align with world axes');
1299
-
1300
  window.plyAlignmentApplied = true;
1301
  window.plyAlignmentMatrix = rotationNeeded.clone();
1302
 
 
1097
  // PCAではなく単純な重心で中心化
1098
  // ========================================
1099
 
1100
+ //preprocess_manual.js の Line 1100-1290付近
1101
+
1102
+ // ========================================
1103
+ // 完全修正版: PLY中心化(Line 1100-1290を置き換え)
1104
+ // イミュータブルなオブジェクト生成で確実に中心化
1105
+ // ========================================
1106
+
1107
  console.log('[preprocessManual] ===== STARTING AUTO-ALIGNMENT =====');
1108
 
1109
  // ========================================
1110
+ // STEP 1: 重心計算
1111
  // ========================================
1112
+ console.log('[preprocessManual] Step 1: Calculating center of mass...');
1113
 
 
1114
  let sumX = 0, sumY = 0, sumZ = 0;
1115
  plyData.vertices.forEach(v => {
1116
  sumX += v.x;
 
1125
  };
1126
 
1127
  console.log('[DEBUG] Center of mass:', {
1128
+ x: center.x.toFixed(6),
1129
+ y: center.y.toFixed(6),
1130
+ z: center.z.toFixed(6)
1131
  });
1132
 
1133
  console.log('[DEBUG] First vertex BEFORE centering:', {
1134
+ x: plyData.vertices[0].x.toFixed(6),
1135
+ y: plyData.vertices[0].y.toFixed(6),
1136
+ z: plyData.vertices[0].z.toFixed(6)
1137
  });
1138
 
1139
+ // ========================================
1140
+ // STEP 2: 中心化(完全に新しいオブジェクトを作成)
1141
+ // ========================================
1142
+ console.log('[preprocessManual] Step 2: Centering PLY (creating new objects)...');
1143
+
1144
+ const centeredVertices = [];
1145
+
1146
+ for (let i = 0; i < plyData.vertices.length; i++) {
1147
+ const v = plyData.vertices[i];
1148
+
1149
+ // ⭐ 完全に新しいオブジェクトを作成
1150
+ const newVertex = {};
1151
 
1152
+ // 座標を中心化
1153
+ newVertex.x = v.x - center.x;
1154
+ newVertex.y = v.y - center.y;
1155
+ newVertex.z = v.z - center.z;
1156
+
1157
+ // 他のプロパティをコピー(座標以外)
1158
+ for (const key in v) {
1159
  if (key !== 'x' && key !== 'y' && key !== 'z') {
1160
+ newVertex[key] = v[key];
1161
  }
1162
+ }
1163
 
1164
+ centeredVertices.push(newVertex);
1165
+ }
1166
+
1167
+ // ⭐ 配列を完全に置き換え
1168
+ plyData.vertices = centeredVertices;
1169
 
1170
  console.log('[DEBUG] First vertex AFTER centering:', {
1171
+ x: plyData.vertices[0].x.toFixed(6),
1172
+ y: plyData.vertices[0].y.toFixed(6),
1173
+ z: plyData.vertices[0].z.toFixed(6)
1174
  });
1175
 
1176
+ // ========================================
1177
+ // STEP 3: 検証(平均を再計算)
1178
+ // ========================================
1179
+ console.log('[preprocessManual] Step 3: Verifying centering...');
1180
+
1181
  sumX = 0; sumY = 0; sumZ = 0;
1182
+ for (let i = 0; i < plyData.vertices.length; i++) {
1183
+ sumX += plyData.vertices[i].x;
1184
+ sumY += plyData.vertices[i].y;
1185
+ sumZ += plyData.vertices[i].z;
1186
+ }
1187
+
1188
  const avgX = sumX / plyData.vertices.length;
1189
  const avgY = sumY / plyData.vertices.length;
1190
  const avgZ = sumZ / plyData.vertices.length;
1191
 
1192
  console.log('[DEBUG] Average after centering:', {
1193
+ x: avgX.toFixed(9),
1194
+ y: avgY.toFixed(9),
1195
+ z: avgZ.toFixed(9)
1196
  });
1197
 
1198
+ if (Math.abs(avgX) > 1e-6 || Math.abs(avgY) > 1e-6 || Math.abs(avgZ) > 1e-6) {
1199
  console.error('[DEBUG] ❌ ERROR: Centering FAILED!');
1200
+ console.error('[DEBUG] Average should be ~0 but got:', {
1201
+ avgX: avgX.toFixed(9),
1202
+ avgY: avgY.toFixed(9),
1203
+ avgZ: avgZ.toFixed(9)
1204
+ });
1205
+ alert('❌ PLY Centering Failed!\n\n' +
1206
+ 'Average is not zero:\n' +
1207
+ 'X: ' + avgX.toFixed(9) + '\n' +
1208
+ 'Y: ' + avgY.toFixed(9) + '\n' +
1209
+ 'Z: ' + avgZ.toFixed(9) + '\n\n' +
1210
+ 'Please reload and try again.');
1211
  return;
 
 
1212
  }
1213
 
1214
+ console.log('[DEBUG] ✓ Centering SUCCESS! Average is effectively zero');
1215
  console.log('[preprocessManual] ✓ PLY centered at origin');
1216
 
1217
  // ========================================
1218
+ // STEP 4: バウンディングボックスを計算
1219
  // ========================================
1220
+ console.log('[preprocessManual] Step 4: Calculating bounding box...');
1221
+
1222
  let minX = Infinity, minY = Infinity, minZ = Infinity;
1223
  let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
1224
 
1225
+ for (let i = 0; i < plyData.vertices.length; i++) {
1226
+ const v = plyData.vertices[i];
1227
  minX = Math.min(minX, v.x);
1228
  minY = Math.min(minY, v.y);
1229
  minZ = Math.min(minZ, v.z);
1230
  maxX = Math.max(maxX, v.x);
1231
  maxY = Math.max(maxY, v.y);
1232
  maxZ = Math.max(maxZ, v.z);
1233
+ }
1234
 
1235
  const sizeX = maxX - minX;
1236
  const sizeY = maxY - minY;
1237
  const sizeZ = maxZ - minZ;
1238
 
1239
+ console.log('[preprocessManual] Centered bounding box:', {
1240
+ min: { x: minX.toFixed(3), y: minY.toFixed(3), z: minZ.toFixed(3) },
1241
+ max: { x: maxX.toFixed(3), y: maxY.toFixed(3), z: maxZ.toFixed(3) },
1242
+ size: { x: sizeX.toFixed(3), y: sizeY.toFixed(3), z: sizeZ.toFixed(3) }
1243
  });
1244
 
1245
  // ========================================
1246
+ // STEP 5: 回転が必要か確認
1247
  // ========================================
1248
+ console.log('[preprocessManual] Step 5: Checking if rotation is needed...');
1249
  console.log('[preprocessManual] Longest axis:',
1250
+ sizeX > sizeY && sizeX > sizeZ ? 'X (' + sizeX.toFixed(2) + 'm)' :
1251
+ sizeZ > sizeY ? 'Z (' + sizeZ.toFixed(2) + 'm)' :
1252
+ 'Y (' + sizeY.toFixed(2) + 'm)');
1253
 
1254
  let rotationNeeded = new THREE.Matrix4();
1255
  let rotationApplied = false;
 
1267
  }
1268
 
1269
  // ========================================
1270
+ // STEP 6: 回転を適用(必要な場合のみ)
1271
  // ========================================
1272
  if (rotationApplied) {
1273
+ console.log('[preprocessManual] Step 6: Applying rotation...');
1274
 
1275
+ for (let i = 0; i < plyData.vertices.length; i++) {
1276
+ const v = plyData.vertices[i];
1277
  const pos = new THREE.Vector3(v.x, v.y, v.z);
1278
  pos.applyMatrix4(rotationNeeded);
1279
+
1280
  v.x = pos.x;
1281
  v.y = pos.y;
1282
  v.z = pos.z;
1283
+ }
1284
 
1285
+ console.log('[preprocessManual] ✓ Rotation applied');
1286
 
1287
  window.plyAlignmentApplied = true;
1288
  window.plyAlignmentMatrix = rotationNeeded.clone();
1289
 
1290
  console.log('[preprocessManual] ✓ Alignment matrix saved for GS display');
1291
  } else {
1292
+ console.log('[preprocessManual] Step 6: Skipped (no rotation needed)');
1293
  window.plyAlignmentApplied = false;
1294
  }
1295
 
1296
  // ========================================
1297
+ // STEP 7: 最終検証
1298
  // ========================================
1299
+ console.log('[preprocessManual] Step 7: Final verification...');
1300
 
 
1301
  minX = Infinity; minY = Infinity; minZ = Infinity;
1302
  maxX = -Infinity; maxY = -Infinity; maxZ = -Infinity;
1303
 
1304
+ for (let i = 0; i < plyData.vertices.length; i++) {
1305
+ const v = plyData.vertices[i];
1306
  minX = Math.min(minX, v.x);
1307
  minY = Math.min(minY, v.y);
1308
  minZ = Math.min(minZ, v.z);
1309
  maxX = Math.max(maxX, v.x);
1310
  maxY = Math.max(maxY, v.y);
1311
  maxZ = Math.max(maxZ, v.z);
1312
+ }
1313
 
1314
  const finalCenter = {
1315
  x: (minX + maxX) / 2,
 
1317
  z: (minZ + maxZ) / 2
1318
  };
1319
 
1320
+ console.log('[preprocessManual] Final PLY state:', {
1321
+ bounds: {
1322
+ min: { x: minX.toFixed(3), y: minY.toFixed(3), z: minZ.toFixed(3) },
1323
+ max: { x: maxX.toFixed(3), y: maxY.toFixed(3), z: maxZ.toFixed(3) }
1324
+ },
1325
+ center: {
1326
+ x: finalCenter.x.toFixed(6),
1327
+ y: finalCenter.y.toFixed(6),
1328
+ z: finalCenter.z.toFixed(6)
1329
+ }
1330
  });
1331
 
1332
+ // より厳密な検証(1mm以内)
1333
+ const tolerance = 0.001; // 1mm
1334
+ if (Math.abs(finalCenter.x) > tolerance ||
1335
+ Math.abs(finalCenter.y) > tolerance ||
1336
+ Math.abs(finalCenter.z) > tolerance) {
1337
  console.error('[preprocessManual] ❌ ERROR: Final center is not at origin!');
1338
+ console.error('[preprocessManual] Expected: (0, 0, 0)');
1339
+ console.error('[preprocessManual] Got:', {
1340
+ x: finalCenter.x.toFixed(6),
1341
+ y: finalCenter.y.toFixed(6),
1342
+ z: finalCenter.z.toFixed(6)
1343
+ });
1344
+ console.error('[preprocessManual] Tolerance:', tolerance, 'm');
1345
+
1346
+ alert('❌ PLY Alignment Error!\n\n' +
1347
+ 'Final center is not at origin:\n' +
1348
+ 'X: ' + finalCenter.x.toFixed(6) + ' m\n' +
1349
+ 'Y: ' + finalCenter.y.toFixed(6) + ' m\n' +
1350
+ 'Z: ' + finalCenter.z.toFixed(6) + ' m\n\n' +
1351
+ 'Expected: < ' + (tolerance * 1000) + ' mm\n\n' +
1352
+ 'Please check the browser console and report this issue.');
1353
  return;
1354
  }
1355
 
1356
+ console.log('[preprocessManual] ✓ Final verification PASSED');
1357
  console.log('[preprocessManual] ✓ PLY is properly centered and aligned');
1358
+ console.log('[preprocessManual] Center deviation: ' +
1359
+ Math.max(Math.abs(finalCenter.x), Math.abs(finalCenter.y), Math.abs(finalCenter.z)).toFixed(6) +
1360
+ ' m (< ' + tolerance + ' m tolerance)');
1361
+
1362
+
1363
+ //preprocess_manual.js の Line 1100-1290付近
1364
 
 
 
1365
  window.plyAlignmentApplied = true;
1366
  window.plyAlignmentMatrix = rotationNeeded.clone();
1367