Upload preprocess_manual.js
Browse files- preprocess_manual.js +255 -22
preprocess_manual.js
CHANGED
|
@@ -1000,12 +1000,147 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
|
|
| 1000 |
const task = loadingSpinner.addTask('Loading PLY...');
|
| 1001 |
|
| 1002 |
|
| 1003 |
-
///////////////////////12/
|
| 1004 |
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1008 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1009 |
// ========================================
|
| 1010 |
// Calculate Bounding Box from PLY data
|
| 1011 |
// ========================================
|
|
@@ -1047,10 +1182,124 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
|
|
| 1047 |
console.log(' Center:', defaultBoxPosition);
|
| 1048 |
console.log(' Raw size:', { x: sizeX, y: sizeY, z: sizeZ });
|
| 1049 |
|
| 1050 |
-
// GSの初期表示,
|
| 1051 |
GVRM.initGS(gsPath, undefined, undefined, scene).then((gs) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1052 |
console.log('[preprocessManual] GS initialized');
|
| 1053 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1054 |
// ★★★ ADD CAMERA CONTROLS HERE ★★★
|
| 1055 |
const cameraControls = setupCameraControls(camera, renderer);
|
| 1056 |
|
|
@@ -1078,15 +1327,6 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
|
|
| 1078 |
|
| 1079 |
//////////////// handle key 12/13 13:30
|
| 1080 |
|
| 1081 |
-
// ========================================
|
| 1082 |
-
// Keyboard Controls for Camera Movement: Deleted 12/13 13:50
|
| 1083 |
-
// ========================================
|
| 1084 |
-
|
| 1085 |
-
////////////////////////////12/13 16:20 START
|
| 1086 |
-
|
| 1087 |
-
// ========================================
|
| 1088 |
-
// Step 0: PLY Position Adjustment
|
| 1089 |
-
// ========================================
|
| 1090 |
|
| 1091 |
// ========================================
|
| 1092 |
// Step 0: Box and PLY Position Adjustment
|
|
@@ -1095,9 +1335,7 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
|
|
| 1095 |
ui.setStatus('Step 0: Adjust bounding box to frame your object');
|
| 1096 |
ui.showPositionAdjustment();
|
| 1097 |
|
| 1098 |
-
|
| 1099 |
-
let minX = Infinity, minY = Infinity, minZ = Infinity;
|
| 1100 |
-
let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
|
| 1101 |
|
| 1102 |
plyData.vertices.forEach(vertex => {
|
| 1103 |
minX = Math.min(minX, vertex.x);
|
|
@@ -1134,12 +1372,7 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
|
|
| 1134 |
sceneCenterEl.textContent = `(${sceneCenter.x.toFixed(1)}, ${sceneCenter.y.toFixed(1)}, ${sceneCenter.z.toFixed(1)})`;
|
| 1135 |
}
|
| 1136 |
|
| 1137 |
-
// Initial box settings (conservative defaults)
|
| 1138 |
-
let boxSize = { x: 3, y: 3, z: 3 };
|
| 1139 |
-
let boxPosition = { x: 0, y: 0, z: 0 };
|
| 1140 |
-
let plyOffset = { x: 0, y: 0, z: 0 };
|
| 1141 |
|
| 1142 |
-
const gsScene = gs.viewer.splatMesh.scenes[0];
|
| 1143 |
|
| 1144 |
// Create initial bounding box
|
| 1145 |
ui.createBoundingBox(boxSize, boxPosition);
|
|
|
|
| 1000 |
const task = loadingSpinner.addTask('Loading PLY...');
|
| 1001 |
|
| 1002 |
|
| 1003 |
+
///////////////////////12/16 12:20
|
| 1004 |
|
| 1005 |
+
parser.parsePLY(gsPath, true).then((plyData) => {
|
| 1006 |
+
console.log('[preprocessManual] PLY loaded:', plyData.vertices.length, 'vertices');
|
| 1007 |
+
loadingSpinner.removeTask(task);
|
| 1008 |
+
|
| 1009 |
+
// ========================================
|
| 1010 |
+
// ⭐ PLY座標系の分析と正規化
|
| 1011 |
+
// ========================================
|
| 1012 |
+
console.log('[preprocessManual] ===== ANALYZING PLY COORDINATE SYSTEM =====');
|
| 1013 |
+
|
| 1014 |
+
// 1. 主成分分析(PCA)を使って点群の主軸を求める
|
| 1015 |
+
function computePCA(vertices) {
|
| 1016 |
+
// 中心を計算
|
| 1017 |
+
const center = { x: 0, y: 0, z: 0 };
|
| 1018 |
+
vertices.forEach(v => {
|
| 1019 |
+
center.x += v.x;
|
| 1020 |
+
center.y += v.y;
|
| 1021 |
+
center.z += v.z;
|
| 1022 |
+
});
|
| 1023 |
+
center.x /= vertices.length;
|
| 1024 |
+
center.y /= vertices.length;
|
| 1025 |
+
center.z /= vertices.length;
|
| 1026 |
+
|
| 1027 |
+
console.log('[PCA] Center:', center);
|
| 1028 |
+
|
| 1029 |
+
// 共分散行列を計算
|
| 1030 |
+
let cxx = 0, cxy = 0, cxz = 0;
|
| 1031 |
+
let cyy = 0, cyz = 0, czz = 0;
|
| 1032 |
+
|
| 1033 |
+
vertices.forEach(v => {
|
| 1034 |
+
const dx = v.x - center.x;
|
| 1035 |
+
const dy = v.y - center.y;
|
| 1036 |
+
const dz = v.z - center.z;
|
| 1037 |
+
|
| 1038 |
+
cxx += dx * dx;
|
| 1039 |
+
cxy += dx * dy;
|
| 1040 |
+
cxz += dx * dz;
|
| 1041 |
+
cyy += dy * dy;
|
| 1042 |
+
cyz += dy * dz;
|
| 1043 |
+
czz += dz * dz;
|
| 1044 |
+
});
|
| 1045 |
+
|
| 1046 |
+
cxx /= vertices.length;
|
| 1047 |
+
cxy /= vertices.length;
|
| 1048 |
+
cxz /= vertices.length;
|
| 1049 |
+
cyy /= vertices.length;
|
| 1050 |
+
cyz /= vertices.length;
|
| 1051 |
+
czz /= vertices.length;
|
| 1052 |
+
|
| 1053 |
+
console.log('[PCA] Covariance matrix:', {
|
| 1054 |
+
cxx: cxx.toFixed(2),
|
| 1055 |
+
cxy: cxy.toFixed(2),
|
| 1056 |
+
cxz: cxz.toFixed(2),
|
| 1057 |
+
cyy: cyy.toFixed(2),
|
| 1058 |
+
cyz: cyz.toFixed(2),
|
| 1059 |
+
czz: czz.toFixed(2)
|
| 1060 |
+
});
|
| 1061 |
+
|
| 1062 |
+
// 分散の比率から座標系の傾きを判定
|
| 1063 |
+
const totalVariance = cxx + cyy + czz;
|
| 1064 |
+
const xRatio = cxx / totalVariance;
|
| 1065 |
+
const yRatio = cyy / totalVariance;
|
| 1066 |
+
const zRatio = czz / totalVariance;
|
| 1067 |
+
|
| 1068 |
+
console.log('[PCA] Variance ratios:', {
|
| 1069 |
+
x: (xRatio * 100).toFixed(1) + '%',
|
| 1070 |
+
y: (yRatio * 100).toFixed(1) + '%',
|
| 1071 |
+
z: (zRatio * 100).toFixed(1) + '%'
|
| 1072 |
+
});
|
| 1073 |
+
|
| 1074 |
+
// 座標系が偏っているか判定
|
| 1075 |
+
const isAligned = (
|
| 1076 |
+
Math.abs(cxy) < 0.1 * Math.sqrt(cxx * cyy) &&
|
| 1077 |
+
Math.abs(cxz) < 0.1 * Math.sqrt(cxx * czz) &&
|
| 1078 |
+
Math.abs(cyz) < 0.1 * Math.sqrt(cyy * czz)
|
| 1079 |
+
);
|
| 1080 |
+
|
| 1081 |
+
console.log('[PCA] Is aligned with world axes:', isAligned);
|
| 1082 |
+
|
| 1083 |
+
return {
|
| 1084 |
+
center,
|
| 1085 |
+
isAligned,
|
| 1086 |
+
needsRotation: !isAligned,
|
| 1087 |
+
covarianceMatrix: { cxx, cxy, cxz, cyy, cyz, czz }
|
| 1088 |
+
};
|
| 1089 |
+
}
|
| 1090 |
|
| 1091 |
+
// 2. 点群を分析
|
| 1092 |
+
const pcaResult = computePCA(plyData.vertices);
|
| 1093 |
+
|
| 1094 |
+
// 3. 回転が必要な場合、ユーザーに通知
|
| 1095 |
+
if (pcaResult.needsRotation) {
|
| 1096 |
+
console.warn('[preprocessManual] ⚠️ PLY coordinate system is NOT aligned with world axes');
|
| 1097 |
+
console.warn('[preprocessManual] This may cause visual mismatch between Box and filtering');
|
| 1098 |
+
|
| 1099 |
+
// ⭐ オプション: 自動的に座標系を正規化
|
| 1100 |
+
const shouldNormalize = confirm(
|
| 1101 |
+
'⚠️ Warning: This PLY file has a tilted coordinate system.\n\n' +
|
| 1102 |
+
'This may cause the bounding box to appear tilted.\n\n' +
|
| 1103 |
+
'Do you want to automatically align the PLY to world axes?\n\n' +
|
| 1104 |
+
'(Recommended: Yes)'
|
| 1105 |
+
);
|
| 1106 |
+
|
| 1107 |
+
if (shouldNormalize) {
|
| 1108 |
+
console.log('[preprocessManual] Auto-aligning PLY to world axes...');
|
| 1109 |
+
|
| 1110 |
+
// 簡易的な方法: バウンディングボックスの主軸に合わせる
|
| 1111 |
+
// より高度な方法: 固有ベクトルを計算して完全に整列
|
| 1112 |
+
|
| 1113 |
+
// ここでは、Y軸が「上」になるように回転する簡易版
|
| 1114 |
+
// 実際のPLYの傾きに応じて調整が必要
|
| 1115 |
+
|
| 1116 |
+
const { center } = pcaResult;
|
| 1117 |
+
|
| 1118 |
+
// 中心に移動
|
| 1119 |
+
plyData.vertices.forEach(v => {
|
| 1120 |
+
v.x -= center.x;
|
| 1121 |
+
v.y -= center.y;
|
| 1122 |
+
v.z -= center.z;
|
| 1123 |
+
});
|
| 1124 |
+
|
| 1125 |
+
console.log('[preprocessManual] ✓ PLY centered at origin');
|
| 1126 |
+
|
| 1127 |
+
// 注: 完全な整列には固有値分解が必要
|
| 1128 |
+
// ここでは中心化のみ実施
|
| 1129 |
+
} else {
|
| 1130 |
+
console.log('[preprocessManual] User chose to keep original orientation');
|
| 1131 |
+
console.log('[preprocessManual] Box rotation sliders should be used to match PLY orientation');
|
| 1132 |
+
}
|
| 1133 |
+
} else {
|
| 1134 |
+
console.log('[preprocessManual] ✓ PLY coordinate system is already aligned');
|
| 1135 |
+
}
|
| 1136 |
+
|
| 1137 |
+
console.log('[preprocessManual] ===== PLY ANALYSIS COMPLETE =====');
|
| 1138 |
+
|
| 1139 |
+
// ========================================
|
| 1140 |
+
// 元のコードを続ける
|
| 1141 |
+
// ========================================
|
| 1142 |
+
|
| 1143 |
+
|
| 1144 |
// ========================================
|
| 1145 |
// Calculate Bounding Box from PLY data
|
| 1146 |
// ========================================
|
|
|
|
| 1182 |
console.log(' Center:', defaultBoxPosition);
|
| 1183 |
console.log(' Raw size:', { x: sizeX, y: sizeY, z: sizeZ });
|
| 1184 |
|
| 1185 |
+
// GSの初期表示, 1216 12:20
|
| 1186 |
GVRM.initGS(gsPath, undefined, undefined, scene).then((gs) => {
|
| 1187 |
+
|
| 1188 |
+
///start
|
| 1189 |
+
|
| 1190 |
+
|
| 1191 |
console.log('[preprocessManual] GS initialized');
|
| 1192 |
|
| 1193 |
+
// ========================================
|
| 1194 |
+
// ⭐ カメラをPLYに合わせて調整
|
| 1195 |
+
// ========================================
|
| 1196 |
+
console.log('[preprocessManual] Auto-aligning camera to PLY...');
|
| 1197 |
+
|
| 1198 |
+
// PLYのバウンディングボックスの中心とサイズを取得
|
| 1199 |
+
let minX = Infinity, minY = Infinity, minZ = Infinity;
|
| 1200 |
+
let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
|
| 1201 |
+
|
| 1202 |
+
plyData.vertices.forEach(vertex => {
|
| 1203 |
+
minX = Math.min(minX, vertex.x);
|
| 1204 |
+
minY = Math.min(minY, vertex.y);
|
| 1205 |
+
minZ = Math.min(minZ, vertex.z);
|
| 1206 |
+
maxX = Math.max(maxX, vertex.x);
|
| 1207 |
+
maxY = Math.max(maxY, vertex.y);
|
| 1208 |
+
maxZ = Math.max(maxZ, vertex.z);
|
| 1209 |
+
});
|
| 1210 |
+
|
| 1211 |
+
const center = {
|
| 1212 |
+
x: (maxX + minX) / 2,
|
| 1213 |
+
y: (maxY + minY) / 2,
|
| 1214 |
+
z: (maxZ + minZ) / 2
|
| 1215 |
+
};
|
| 1216 |
+
|
| 1217 |
+
const size = {
|
| 1218 |
+
x: maxX - minX,
|
| 1219 |
+
y: maxY - minY,
|
| 1220 |
+
z: maxZ - minZ
|
| 1221 |
+
};
|
| 1222 |
+
|
| 1223 |
+
const maxDim = Math.max(size.x, size.y, size.z);
|
| 1224 |
+
|
| 1225 |
+
// カメラ位置を計算(PLYの中心から適切な距離)
|
| 1226 |
+
const distance = maxDim * 2.0; // 点群全体が見える距離
|
| 1227 |
+
|
| 1228 |
+
// ⭐ 重要: カメラをPLYの「正面」に配置
|
| 1229 |
+
// Y軸が上、Z軸が前方になるように配置
|
| 1230 |
+
camera.position.set(
|
| 1231 |
+
center.x, // X: 中心
|
| 1232 |
+
center.y + maxDim * 0.3, // Y: 少し上から
|
| 1233 |
+
center.z + distance // Z: 前方から
|
| 1234 |
+
);
|
| 1235 |
+
|
| 1236 |
+
camera.lookAt(center.x, center.y, center.z);
|
| 1237 |
+
camera.updateProjectionMatrix();
|
| 1238 |
+
|
| 1239 |
+
console.log('[preprocessManual] Camera positioned at:', {
|
| 1240 |
+
x: camera.position.x.toFixed(2),
|
| 1241 |
+
y: camera.position.y.toFixed(2),
|
| 1242 |
+
z: camera.position.z.toFixed(2)
|
| 1243 |
+
});
|
| 1244 |
+
console.log('[preprocessManual] Looking at:', center);
|
| 1245 |
+
|
| 1246 |
+
// OrbitControlsのターゲットも設定
|
| 1247 |
+
if (cameraControls && cameraControls.controls) {
|
| 1248 |
+
cameraControls.controls.target.set(center.x, center.y, center.z);
|
| 1249 |
+
cameraControls.controls.update();
|
| 1250 |
+
console.log('[preprocessManual] OrbitControls target set to PLY center');
|
| 1251 |
+
}
|
| 1252 |
+
|
| 1253 |
+
// ========================================
|
| 1254 |
+
// ⭐ 初期Boxもこの向きに合わせる
|
| 1255 |
+
// ========================================
|
| 1256 |
+
|
| 1257 |
+
// 初期Box設定(PLYの中心に配置)
|
| 1258 |
+
let boxSize = {
|
| 1259 |
+
x: size.x * 0.5, // 初期値: PLYの50%サイズ
|
| 1260 |
+
y: size.y * 0.5,
|
| 1261 |
+
z: size.z * 0.5
|
| 1262 |
+
};
|
| 1263 |
+
let boxPosition = {
|
| 1264 |
+
x: center.x,
|
| 1265 |
+
y: center.y,
|
| 1266 |
+
z: center.z
|
| 1267 |
+
};
|
| 1268 |
+
let plyOffset = { x: 0, y: 0, z: 0 };
|
| 1269 |
+
|
| 1270 |
+
// UIのスライダーの初期値も更新
|
| 1271 |
+
if (document.getElementById('box-x-step0')) {
|
| 1272 |
+
document.getElementById('box-x-step0').value = boxSize.x.toFixed(1);
|
| 1273 |
+
document.getElementById('box-x-value-step0').textContent = boxSize.x.toFixed(1);
|
| 1274 |
+
|
| 1275 |
+
document.getElementById('box-y-step0').value = boxSize.y.toFixed(1);
|
| 1276 |
+
document.getElementById('box-y-value-step0').textContent = boxSize.y.toFixed(1);
|
| 1277 |
+
|
| 1278 |
+
document.getElementById('box-z-step0').value = boxSize.z.toFixed(1);
|
| 1279 |
+
document.getElementById('box-z-value-step0').textContent = boxSize.z.toFixed(1);
|
| 1280 |
+
|
| 1281 |
+
document.getElementById('box-x-pos-step0').value = boxPosition.x.toFixed(1);
|
| 1282 |
+
document.getElementById('box-x-pos-value-step0').textContent = boxPosition.x.toFixed(1);
|
| 1283 |
+
|
| 1284 |
+
document.getElementById('box-y-pos-step0').value = boxPosition.y.toFixed(1);
|
| 1285 |
+
document.getElementById('box-y-pos-value-step0').textContent = boxPosition.y.toFixed(1);
|
| 1286 |
+
|
| 1287 |
+
document.getElementById('box-z-pos-step0').value = boxPosition.z.toFixed(1);
|
| 1288 |
+
document.getElementById('box-z-pos-value-step0').textContent = boxPosition.z.toFixed(1);
|
| 1289 |
+
|
| 1290 |
+
console.log('[preprocessManual] UI sliders initialized to PLY-aligned values');
|
| 1291 |
+
}
|
| 1292 |
+
|
| 1293 |
+
const gsScene = gs.viewer.splatMesh.scenes[0];
|
| 1294 |
+
|
| 1295 |
+
// Create initial bounding box(PLYの中心に配置)
|
| 1296 |
+
ui.createBoundingBox(boxSize, boxPosition);
|
| 1297 |
+
|
| 1298 |
+
console.log('[preprocessManual] ✓ Camera and Box aligned to PLY');
|
| 1299 |
+
|
| 1300 |
+
|
| 1301 |
+
|
| 1302 |
+
|
| 1303 |
// ★★★ ADD CAMERA CONTROLS HERE ★★★
|
| 1304 |
const cameraControls = setupCameraControls(camera, renderer);
|
| 1305 |
|
|
|
|
| 1327 |
|
| 1328 |
//////////////// handle key 12/13 13:30
|
| 1329 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1330 |
|
| 1331 |
// ========================================
|
| 1332 |
// Step 0: Box and PLY Position Adjustment
|
|
|
|
| 1335 |
ui.setStatus('Step 0: Adjust bounding box to frame your object');
|
| 1336 |
ui.showPositionAdjustment();
|
| 1337 |
|
| 1338 |
+
|
|
|
|
|
|
|
| 1339 |
|
| 1340 |
plyData.vertices.forEach(vertex => {
|
| 1341 |
minX = Math.min(minX, vertex.x);
|
|
|
|
| 1372 |
sceneCenterEl.textContent = `(${sceneCenter.x.toFixed(1)}, ${sceneCenter.y.toFixed(1)}, ${sceneCenter.z.toFixed(1)})`;
|
| 1373 |
}
|
| 1374 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1375 |
|
|
|
|
| 1376 |
|
| 1377 |
// Create initial bounding box
|
| 1378 |
ui.createBoundingBox(boxSize, boxPosition);
|