stpete2 commited on
Commit
dc19f42
·
verified ·
1 Parent(s): 3d64718

Upload preprocess_manual.js

Browse files
Files changed (1) hide show
  1. preprocess_manual.js +207 -143
preprocess_manual.js CHANGED
@@ -1090,152 +1090,216 @@ parser.parsePLY(gsPath, true).then((plyData) => {
1090
  // 2. 点群を分析
1091
  const pcaResult = computePCA(plyData.vertices);
1092
 
 
1093
 
1094
- // 3. 回転が必要な場合、ユーザーに通知////1095-1290
 
 
 
1095
 
1096
- // 3. 回転が必要な場合、ユーザーに通知
1097
- if (pcaResult.needsRotation) {
1098
- console.warn('[preprocessManual] ⚠️ PLY coordinate system is NOT aligned with world axes');
1099
- console.warn('[preprocessManual] This may cause visual mismatch between Box and filtering');
1100
-
1101
- // ユーザー確認を削除し、自動的に正規化を実行
1102
- console.log('[preprocessManual] ===== STARTING AUTO-ALIGNMENT =====');
1103
-
1104
- const { center } = pcaResult;
1105
-
1106
- // ========================================
1107
- // STEP 1: 中心化(1回だけ実行)
1108
- // ========================================
1109
- console.log('[preprocessManual] Step 1: Centering PLY...');
1110
-
1111
- console.log('[DEBUG] First vertex BEFORE:', {
1112
- x: plyData.vertices[0].x.toFixed(3),
1113
- y: plyData.vertices[0].y.toFixed(3),
1114
- z: plyData.vertices[0].z.toFixed(3)
1115
- });
1116
-
1117
- plyData.vertices = plyData.vertices.map(v => {
1118
- const centered = {
1119
- x: v.x - center.x,
1120
- y: v.y - center.y,
1121
- z: v.z - center.z
1122
- };
1123
-
1124
- Object.keys(v).forEach(key => {
1125
- if (key !== 'x' && key !== 'y' && key !== 'z') {
1126
- centered[key] = v[key];
1127
- }
1128
- });
1129
-
1130
- return centered;
1131
- });
1132
-
1133
- console.log('[DEBUG] First vertex AFTER:', {
1134
- x: plyData.vertices[0].x.toFixed(3),
1135
- y: plyData.vertices[0].y.toFixed(3),
1136
- z: plyData.vertices[0].z.toFixed(3)
1137
- });
1138
-
1139
- let sumX = 0, sumY = 0, sumZ = 0;
1140
- plyData.vertices.forEach(v => {
1141
- sumX += v.x;
1142
- sumY += v.y;
1143
- sumZ += v.z;
1144
- });
1145
- const avgX = sumX / plyData.vertices.length;
1146
- const avgY = sumY / plyData.vertices.length;
1147
- const avgZ = sumZ / plyData.vertices.length;
1148
-
1149
- console.log('[DEBUG] Average after centering:', {
1150
- x: avgX.toFixed(6),
1151
- y: avgY.toFixed(6),
1152
- z: avgZ.toFixed(6)
1153
- });
1154
-
1155
- if (Math.abs(avgX) > 0.001 || Math.abs(avgY) > 0.001 || Math.abs(avgZ) > 0.001) {
1156
- console.error('[DEBUG] ERROR: Centering FAILED!');
1157
- } else {
1158
- console.log('[DEBUG] Centering SUCCESS! Average is ~0');
1159
- }
1160
-
1161
- console.log('[preprocessManual] PLY centered at origin');
1162
-
1163
- // ========================================
1164
- // STEP 2: バウンディングボックスを再計算
1165
- // ========================================
1166
- console.log('[preprocessManual] Step 2: Recalculating bounding box...');
1167
- let minX = Infinity, minY = Infinity, minZ = Infinity;
1168
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
1169
-
1170
- plyData.vertices.forEach(v => {
1171
- minX = Math.min(minX, v.x);
1172
- minY = Math.min(minY, v.y);
1173
- minZ = Math.min(minZ, v.z);
1174
- maxX = Math.max(maxX, v.x);
1175
- maxY = Math.max(maxY, v.y);
1176
- maxZ = Math.max(maxZ, v.z);
1177
- });
1178
-
1179
- const sizeX = maxX - minX;
1180
- const sizeY = maxY - minY;
1181
- const sizeZ = maxZ - minZ;
1182
-
1183
- console.log('[preprocessManual] Centered sizes:', {
1184
- sizeX: sizeX.toFixed(2),
1185
- sizeY: sizeY.toFixed(2),
1186
- sizeZ: sizeZ.toFixed(2)
1187
- });
1188
-
1189
- // ========================================
1190
- // STEP 3: Y軸が最長になるように回転
1191
- // ========================================
1192
- console.log('[preprocessManual] Step 3: Checking if rotation is needed...');
1193
- console.log('[preprocessManual] Longest axis:',
1194
- sizeX > sizeY && sizeX > sizeZ ? 'X' :
1195
- sizeZ > sizeY ? 'Z' : 'Y');
1196
-
1197
- let rotationNeeded = new THREE.Matrix4();
1198
- let rotationApplied = false;
1199
-
1200
- if (sizeX > sizeY && sizeX > sizeZ) {
1201
- console.log('[preprocessManual] ⚙️ Rotating: X → Y (90° around Z axis)');
1202
- rotationNeeded.makeRotationZ(Math.PI / 2);
1203
- rotationApplied = true;
1204
- } else if (sizeZ > sizeY && sizeZ > sizeX) {
1205
- console.log('[preprocessManual] ⚙️ Rotating: Z → Y (90° around X axis)');
1206
- rotationNeeded.makeRotationX(Math.PI / 2);
1207
- rotationApplied = true;
1208
- } else {
1209
- console.log('[preprocessManual] ✓ Y-axis is already the longest, no rotation needed');
1210
- }
1211
-
1212
- // ========================================
1213
- // STEP 4: 回転を適用(必要な場合のみ)
1214
- // ========================================
1215
- if (rotationApplied) {
1216
- console.log('[preprocessManual] Step 4: Applying rotation to all vertices...');
1217
-
1218
- plyData.vertices.forEach(v => {
1219
- const pos = new THREE.Vector3(v.x, v.y, v.z);
1220
- pos.applyMatrix4(rotationNeeded);
1221
- v.x = pos.x;
1222
- v.y = pos.y;
1223
- v.z = pos.z;
1224
- });
1225
-
1226
- console.log('[preprocessManual] PLY rotated to align with world axes');
1227
-
1228
- window.plyAlignmentApplied = true;
1229
- window.plyAlignmentMatrix = rotationNeeded.clone();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1230
 
1231
- console.log('[preprocessManual] Alignment matrix saved for GS display');
1232
- } else {
1233
- console.log('[preprocessManual] Step 4: Skipped (no rotation needed)');
1234
- }
1235
-
1236
- } else {
1237
- console.log('[preprocessManual] ✓ PLY coordinate system is already aligned');
1238
- }
1239
 
1240
 
1241
  ////////////////////////1095-1290
 
1090
  // 2. 点群を分析
1091
  const pcaResult = computePCA(plyData.vertices);
1092
 
1093
+ // 完全修正版: PLY中心化(Line 1102-1233を置き換え)
1094
 
1095
+ // ========================================
1096
+ // 完全修正版: PLY中心化(Line 1102-1233を置き換え)
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;
1111
+ sumY += v.y;
1112
+ sumZ += v.z;
1113
+ });
1114
+
1115
+ const center = {
1116
+ x: sumX / plyData.vertices.length,
1117
+ y: sumY / plyData.vertices.length,
1118
+ z: sumZ / plyData.vertices.length
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;
1221
+
1222
+ if (sizeX > sizeY && sizeX > sizeZ) {
1223
+ console.log('[preprocessManual] ⚙️ Rotating: X → Y (90° around Z axis)');
1224
+ rotationNeeded.makeRotationZ(Math.PI / 2);
1225
+ rotationApplied = true;
1226
+ } else if (sizeZ > sizeY && sizeZ > sizeX) {
1227
+ console.log('[preprocessManual] ⚙️ Rotating: Z → Y (90° around X axis)');
1228
+ rotationNeeded.makeRotationX(Math.PI / 2);
1229
+ rotationApplied = true;
1230
+ } else {
1231
+ console.log('[preprocessManual] ✓ Y-axis is already the longest, no rotation needed');
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,
1279
+ y: (minY + maxY) / 2,
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
+
 
 
 
 
 
1303
 
1304
 
1305
  ////////////////////////1095-1290