Repository navigation
Expand file tree
/
Copy pathrepro_snap.ts
More file actions
195 lines (164 loc) · 6.61 KB
/
Copy pathrepro_snap.ts
File metadata and controls
195 lines (164 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import * as THREE from 'three';
// --- MOCK TYPES ---
enum BuildingType {
SQUARE_FOUNDATION = 'SQUARE_FOUNDATION',
TRIANGLE_FOUNDATION = 'TRIANGLE_FOUNDATION',
CURVED_FOUNDATION = 'CURVED_FOUNDATION',
}
interface BuildingData {
id: string;
type: BuildingType;
position: [number, number, number];
rotation: [number, number, number];
}
const UNIT_SIZE = 4;
const TRIANGLE_RADIUS = UNIT_SIZE / Math.sqrt(3); // ~2.309
const TRIANGLE_APOTHEM = UNIT_SIZE / (2 * Math.sqrt(3)); // ~1.1547
const FOUNDATION_HEIGHT = 0.2;
// --- GEOMETRY LOGIC (Copied & Adapted) ---
enum EdgeRole { BASE = 'BASE', RIGHT = 'RIGHT', LEFT = 'LEFT', SIDE = 'SIDE' }
enum SocketType { FOUNDATION_EDGE = 'FOUNDATION_EDGE' }
interface LocalEdgeSocket {
start: THREE.Vector3;
center: THREE.Vector3;
end: THREE.Vector3;
socketType: SocketType;
edgeLength: number;
edgeRole: EdgeRole;
}
interface EdgeSocket extends LocalEdgeSocket {
id: string;
}
const createEdge = (start: THREE.Vector3, end: THREE.Vector3, edgeRole: EdgeRole): LocalEdgeSocket => {
const center = start.clone().add(end).multiplyScalar(0.5);
return {
start: start.clone(),
center,
end: end.clone(),
socketType: SocketType.FOUNDATION_EDGE,
edgeLength: start.distanceTo(end),
edgeRole
};
};
const getLocalEdgeSockets = (type: BuildingType): LocalEdgeSocket[] => {
const edges: LocalEdgeSocket[] = [];
const halfSize = UNIT_SIZE / 2;
if (type === BuildingType.TRIANGLE_FOUNDATION) {
const apexZ = -TRIANGLE_RADIUS;
const baseZ = TRIANGLE_APOTHEM;
const vApex = new THREE.Vector3(0, 0, apexZ);
const vLeft = new THREE.Vector3(-halfSize, 0, baseZ);
const vRight = new THREE.Vector3(halfSize, 0, baseZ);
edges.push(createEdge(vLeft, vRight, EdgeRole.BASE));
edges.push(createEdge(vRight, vApex, EdgeRole.RIGHT));
edges.push(createEdge(vApex, vLeft, EdgeRole.LEFT));
} else if (type === BuildingType.CURVED_FOUNDATION) {
// Bottom edge
edges.push(createEdge(
new THREE.Vector3(-halfSize, 0, halfSize),
new THREE.Vector3(halfSize, 0, halfSize),
EdgeRole.SIDE
));
// Left edge (flipped winding)
edges.push(createEdge(
new THREE.Vector3(-halfSize, 0, -halfSize),
new THREE.Vector3(-halfSize, 0, halfSize),
EdgeRole.SIDE
));
}
return edges;
};
const getWorldEdgeSockets = (building: BuildingData): EdgeSocket[] => {
const localEdges = getLocalEdgeSockets(building.type);
const worldEdges: EdgeSocket[] = [];
const rotEuler = new THREE.Euler(building.rotation[0], building.rotation[1], building.rotation[2]);
const pos = new THREE.Vector3(building.position[0], building.position[1], building.position[2]);
localEdges.forEach(edge => {
const wStart = edge.start.clone().applyEuler(rotEuler).add(pos);
const wCenter = edge.center.clone().applyEuler(rotEuler).add(pos);
const wEnd = edge.end.clone().applyEuler(rotEuler).add(pos);
worldEdges.push({
...edge,
start: wStart,
center: wCenter,
end: wEnd,
id: building.id,
});
});
return worldEdges;
};
const calculateEdgeSnapTransform = (
targetEdge: EdgeSocket,
ghostEdge: LocalEdgeSocket
): { position: THREE.Vector3; rotation: THREE.Euler } | null => {
const targetDir = targetEdge.end.clone().sub(targetEdge.start).normalize();
const ghostDir = ghostEdge.end.clone().sub(ghostEdge.start).normalize();
const desiredGhostDir = targetDir.clone().negate();
const ghostAngle = Math.atan2(ghostDir.x, ghostDir.z);
const targetAngle = Math.atan2(desiredGhostDir.x, desiredGhostDir.z);
const rotY = targetAngle - ghostAngle;
const rotation = new THREE.Euler(0, rotY, 0);
const rotatedGhostStart = ghostEdge.start.clone().applyEuler(rotation);
const position = targetEdge.end.clone().sub(rotatedGhostStart);
const rotatedGhostCenter = ghostEdge.center.clone().applyEuler(rotation).add(position);
const rotatedGhostEnd = ghostEdge.end.clone().applyEuler(rotation).add(position);
const startError = rotatedGhostStart.clone().add(position).distanceTo(targetEdge.end);
const centerError = rotatedGhostCenter.distanceTo(targetEdge.center);
const endError = rotatedGhostEnd.distanceTo(targetEdge.start);
const tolerance = 0.05;
if (startError > tolerance || centerError > tolerance || endError > tolerance) {
return null;
}
return { position, rotation };
};
// --- SIMULATION ---
// 1. Place a Triangle at origin
const triangle: BuildingData = {
id: 't1',
type: BuildingType.TRIANGLE_FOUNDATION,
position: [0, 0, 0],
rotation: [0, 0, 0]
};
const buildings = [triangle];
const targetEdges = getWorldEdgeSockets(triangle);
console.log("Target Edges (Triangle):");
targetEdges.forEach((e, i) => {
console.log(`Edge ${i} [${e.edgeRole}]: Start=(${e.start.x.toFixed(2)},${e.start.z.toFixed(2)}) End=(${e.end.x.toFixed(2)},${e.end.z.toFixed(2)}) Center=(${e.center.x.toFixed(2)},${e.center.z.toFixed(2)}) Len=${e.edgeLength.toFixed(2)}`);
});
// 2. Try to snap a Curved Foundation to the "Right" edge of the Triangle
const targetEdge = targetEdges[1]; // RIGHT edge
console.log(`\nAttemping to snap Curved Foundation to Triangle Edge 1 (RIGHT)...`);
const ghostEdges = getLocalEdgeSockets(BuildingType.CURVED_FOUNDATION);
console.log(`Ghost Edges (Curved):`);
ghostEdges.forEach((e, i) => {
console.log(`Edge ${i}: Start=(${e.start.x.toFixed(2)},${e.start.z.toFixed(2)}) End=(${e.end.x.toFixed(2)},${e.end.z.toFixed(2)})`);
});
let bestMatch = null;
for (const ghostEdge of ghostEdges) {
console.log(`\nChecking Ghost Edge...`);
if (Math.abs(ghostEdge.edgeLength - targetEdge.edgeLength) > 0.01) {
console.log("Length mismatch");
continue;
}
const transform = calculateEdgeSnapTransform(targetEdge, ghostEdge);
if (transform) {
console.log("SUCCESS: Transform found!");
bestMatch = transform;
} else {
console.log("FAILED: Transform rejected.");
}
}
if (bestMatch) {
console.log("Valid snap found!");
const finalPos = bestMatch.position;
const bPos = new THREE.Vector3(triangle.position[0], triangle.position[1], triangle.position[2]);
const dist = bPos.distanceTo(finalPos);
console.log(`Distance between centers: ${dist.toFixed(4)}`);
if (dist < 2.0) {
console.log("COLLISION DETECTED (dist < 2.0)");
} else {
console.log("Collision check passed.");
}
} else {
console.log("No valid snap found.");
}