# How to get volume between terrain and curve plane

Good day

please could you tell how to get the volume between terrain and curve plane

``````let a=100,b=100;
let p = new THREE.PlaneGeometry(50,50, a, b).rotateX(-Math.PI * 0.5);
let pos=p.vertices;
var ls =50; // length segments
var ws = width; // width segments, tracks
let trenchSpline = new THREE.CatmullRomCurve3([
new THREE.Vector3(vertices[0].x*0.25,vertices[0].z,vertices[0].y),
new THREE.Vector3(vertices[1].x*0.25,vertices[1].z,vertices[1].y),
new THREE.Vector3(vertices[2].x*0.25,vertices[2].z,vertices[2].y),
new THREE.Vector3(vertices[3].x*0.25,vertices[3].z,vertices[3].y),
]);
let trenchPoints = trenchSpline.getSpacedPoints(ls);
let m = new THREE.MeshBasicMaterial({color:'violet',wireframe:true});
let o = new THREE.Mesh(p, m);
var ht=0;
for(let x=0;x<100;x++){
for(let y=0;y<100;y++){
ht +=0.004
pos[y+100*x].y=Math.cos(ht)
}
}
var controls = new THREE.OrbitControls( camera, renderer.domElement );
var lss = ls + 1;
var wss = ws + 1;

var faceCount = ls * ws * 2;
var vertexCount = lss * wss;

var g = new THREE.BufferGeometry( );

g.faceIndices = new Uint32Array( faceCount * 3 );
g.vertices = new Float32Array( vertexCount * 3 );
//g.normals = new Float32Array( vertexCount * 3 );
//g.uvs = new Float32Array( vertexCount * 2 );

g.setIndex( new THREE.BufferAttribute( g.faceIndices, 1 ) );
g.addAttribute( 'position', new THREE.BufferAttribute( g.vertices, 3 ).setDynamic(true ) );
//g.addAttribute( 'normal', new THREE.BufferAttribute( g.normals, 3 ).setDynamic( true ) );
//g.addAttribute( 'uv', new THREE.BufferAttribute( g.uvs, 2 ) );

var idxCount = 0;
var material=new THREE.MeshBasicMaterial( { color: 'gray', side: THREE.DoubleSide, wireframe:false  } )
for ( var j = 0; j < ls; j ++ ) {

for ( var i = 0; i < ws; i ++ ) {

// 2 faces / segment,  3 vertex indices
a =  wss * j+i;
b1 = wss * ( j + 1 ) + i;		// right-bottom
c1 = wss * ( j + 1 ) + 1 + i;
b2 = wss * ( j + 1 ) + 1 + i;	// left-top
c2 = wss * j + 1 + i;

g.faceIndices[ idxCount     ] = a; // right-bottom
g.faceIndices[ idxCount + 1 ] = b1;
g.faceIndices[ idxCount + 2 ] = c1;

g.faceIndices[ idxCount + 3 ] = a; // left-top
g.faceIndices[ idxCount + 4 ] = b2,
g.faceIndices[ idxCount + 5 ] = c2;

g.addGroup( idxCount, 6, i ); // write groups for multi material

idxCount += 6;

}

}

var curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
var tangent;
var normal = new THREE.Vector3( 0, 0, 0 );
var binormal = new THREE.Vector3( 0, 1, 0 );

var x, y, z;
var vIdx = 0; // vertex index
var posIdx; // position  index

for ( var j = 0; j < lss; j ++ ) {  // length

for ( var i = 0; i < wss; i ++ ) { // width

// calculate here the coordinates according to your wishes

tangent = trenchSpline.getTangent( j / ls ); //  .. / length segments

normal.cross( tangent, binormal );

binormal.cross( normal, tangent ); // new binormal

normal.normalize().multiplyScalar(1 );
x = trenchPoints[ j ].x+0.25*(i-ws/2)*normal.x;
y = trenchPoints[ j ].y;
z = trenchPoints[ j ].z+0.25*(i-ws/2)*normal.z;

xyzSet();

vIdx ++;

}

}

g.attributes.position.needsUpdate = true;
//g.attributes.normal.needsUpdate = true;

var mesh = new THREE.Mesh( g, material );
// set vertex position
function xyzSet() {

posIdx = vIdx * 3;

g.vertices[ posIdx ]  = x;
g.vertices[ posIdx + 3 ]  = y;
g.vertices[ posIdx + 2 ]  = z;

}
// volume between curve plane and terrain
for ( var j = 0; j < ls; j ++ ) {

for ( var i = 0; i < ws; i ++ ) {
let thickness=0.25;

let V(cp)=new THREE.Triangle(a,b1,c1).getArea()*0.25;
let V(ter)=new THREE.Triangle(idx,idy,idz).getArea()*pos[i+100*j].y;
Vdif=Math.abs(Vcp-Vter)
}}
``````

Your topic revolves around integral calculus. I think you have to calculate that yourself, because I didn’t know that threejs does that for you. is that a sine wave? If you can tell me the boundary conditions, I’ll calculate it for you quickly. For this I need the amplitude of your wave and since your surface is inclined, the exact corner points of the surface that touch your wave

I got the amplitude of wave using cosinus I tried it with this code

``````var ht=0;
for(let x=0;x<100;x++){
for(let y=0;y<100;y++){
ht +=0.004
pos[y+100*x].y=Math.cos(ht)
}
}
``````

I think I have to calculate that on paper first. I need to see the formula for this because calculating the area between a straight and a cosine wave is not difficult.