How to devide object into layers

Hi. I need to devide one of the axes ex. “x” (50mm height) / 1 (1mm height of layer). after I need to calculate volume of each layer. here is code with formula

import React, { useRef, useEffect } from 'react';
import { useLoader } from '@react-three/fiber';
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader';
import * as THREE from 'three';

const STLModel = ({ stlFile, onDimensionsCalculated }) => {
  const geometry = useLoader(STLLoader, stlFile);
  const ref = useRef();

  useEffect(() => {
    const calculateDimensionsAndVolume = () => {
      if (ref.current) {
        const box = new THREE.Box3().setFromObject(ref.current);
        const dimensions = box.getSize(new THREE.Vector3());

//formula for volume

        const getVolume = (geometry) => {
          let position = geometry.attributes.position;
          let faces = position.count / 3;
          let sum = 0;
          let p1 = new THREE.Vector3(),
            p2 = new THREE.Vector3(),
            p3 = new THREE.Vector3();
          for (let i = 0; i < faces; i++) {
            p1.fromBufferAttribute(position, i * 3 + 0);
            p2.fromBufferAttribute(position, i * 3 + 1);
            p3.fromBufferAttribute(position, i * 3 + 2);
            sum += signedVolumeOfTriangle(p1, p2, p3);
          }
          return sum;
        };

        const signedVolumeOfTriangle = (p1, p2, p3) => {
          return p1.dot(p2.cross(p3)) / 6.0;
        };

        const volume = getVolume(geometry);
        onDimensionsCalculated(dimensions, volume);
      }
    };

    calculateDimensionsAndVolume();
  }, [geometry, onDimensionsCalculated]);

  return (
    <group ref={ref}>
      <mesh geometry={geometry}>
        <meshMatcapMaterial />
      </mesh>
    </group>
  );
};

export default STLModel;

you could use some tools in conjunction with eachother here, for example you could use three-bvh-csg to cut the geometry into 50mm slices and then a simple tool such as three-volume to calculate the volume of each slice ( originally from this thread by @prisoner849 )

2 Likes

thank you for the answer, I’'l check this right now

hello, I tried to use three-bvh-csg
this is link to codesandbox or this one .
to see, how normaly looks page /model, you can swap commented code to actived code.
problem is in splitIntoLayers function, and I can’t understand how to fix it

sorry, by default workspace was marked as personal.
should work now