I’m new to Threejs trying to calculate euclidean distance from intersection points you can my code what should i do to get corrrect distance?
function setPointOfIntersection(line, planarSrf, pos) {
const intersect = planarSrf.intersectLine(line, new Vector3());
if (intersect !== null) {
let vec = intersect.clone();
pos.push(vec.x);
pos.push(vec.y);
pos.push(vec.z);
}
}
function drawIntersectionLine(obj,plane) {
let a = new Vector3();
let b = new Vector3();
let c = new Vector3();
const isIndexed = obj.geometry.index != null;
const pos = obj.geometry.attributes.position;
const idx = obj.geometry.index;
const faceCount = (isIndexed ? idx.count : pos.count) / 3;
const mathPlane = createPlaneFromPlanarGeometry(plane,obj.children[0]);
obj.children[0].add(new PlaneHelper(mathPlane, 1, 0xffff00));
// obj.material.clippingPlanes = [mathPlane];
let positions = [];
for (let i = 0; i < faceCount; i++) {
let baseIdx = i * 3;
let idxA = baseIdx + 0;
a.fromBufferAttribute(pos, isIndexed ? idx.getX(idxA) : idxA);
let idxB = baseIdx + 1;
b.fromBufferAttribute(pos, isIndexed ? idx.getX(idxB) : idxB);
let idxC = baseIdx + 2;
c.fromBufferAttribute(pos, isIndexed ? idx.getX(idxC) : idxC);
obj.localToWorld(a);
obj.localToWorld(b);
obj.localToWorld(c);
let lineAB = new Line3(a, b);
let lineBC = new Line3(b, c);
let lineCA = new Line3(c, a);
setPointOfIntersection(lineAB, mathPlane, positions);
setPointOfIntersection(lineBC, mathPlane, positions);
setPointOfIntersection(lineCA, mathPlane, positions);
}
return positions;
}
function createPlaneFromPlanarGeometry(planarGeometry,mesh) {
let localPlane = new Plane();
let normal = new Vector3();
let point = new Vector3();
normal.set(0, -1, 0).applyQuaternion(planarGeometry.quaternion);
let arrow = new ArrowHelper(normal, planarGeometry.position, 1, 0xff0000);
mesh.add(arrow);
point.copy(planarGeometry.position);
localPlane.setFromNormalAndCoplanarPoint(normal, point).normalize();
return localPlane;
}
const addPlaneToMesh = () => {
if (!obj) return;
const mesh = obj.children[0];
mesh.add(new AxesHelper(5));
if (mesh.isMesh) {
let planeGeom = new PlaneGeometry(1, 1);
planeGeom.rotateX(-Math.PI / 2);
let plane = new Mesh(
planeGeom,
new MeshBasicMaterial({
color: "lightgray",
transparent: true,
opacity: 0.5,
side: DoubleSide
})
);
mesh.add(plane);
const intersectionPoints = drawIntersectionLine(mesh, plane);
processIntersectionPoints(intersectionPoints,plane);
let lines = new LineSegments(
new BufferGeometry(),
new LineBasicMaterial({
color: "red",
linewidth: 5
})
);
lines.geometry.setAttribute(
"position",
new BufferAttribute(new Float32Array(intersectionPoints), 3)
);
mesh.add(lines);
const points = new Points(
new BufferGeometry(),
new PointsMaterial({
color: 0x000000,
size: 0.01
})
);
points.geometry.setAttribute(
"position",
new BufferAttribute(new Float32Array(intersectionPoints), 3)
);
mesh.add(points);
}
}
function processIntersectionPoints(intersectionPoints, plane) {
const points = [];
for (let i = 0; i < intersectionPoints.length; i += 3) {
const point = new Vector3(
intersectionPoints[i],
intersectionPoints[i + 1],
intersectionPoints[i + 2]
);
points.push(point);
}
let distance = 0;
function distanceVector( v1, v2 ) {
var dx = v1.x - v2.x;
var dy = v1.y - v2.y;
var dz = v1.z - v2.z;
return Math.sqrt( dx * dx + dy * dy + dz * dz );
}
for (let i = 0; i < points.length - 1; i++) {
distance += distanceVector(points[i],points[i+1]);
}
console.log("Total distance",distance);
}