Getting incorrect Euclidean distance from intersections points

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);

  }

@Mugen87 @gkjohnson @amitlzkpa Can you help me?

Can you also describe the problem with it right now?
What is the error?

I’m getting Euclidean distance wrong. what could be the problem?

I mean the distance value I got is wrong.

Do you have it on jsfiddle or some similar service?
It would be much faster with that.

You can compute lengths using the computeLineDistances helper to isolate if there are issues in the distance calculation part?

Let me put my code on JSFiddle so it will be faster. Previously, I was getting 6.268236195772737, and now I am getting 66.06148089282215 using computeLineDistances.

If you are trying to draw a contour line around the mesh at the intersection with the plane have a look at meshy. The ‘local cross section tool’ does just that. The code is available to browse on github. It will also help you with calculating area, min and max distances and circumference of the contour.

1 Like

Big thanks for your guidance. I’m thankful!

you don’t need to implement those yourself…

points[i].distanceTo(points[i+1]) works too…
there is also distanceSqr if you only want squared distance.

2 Likes