# BufferGeometry intersection with three-csg-ts - volume calculation

**URL:** <https://discourse.threejs.org/t/buffergeometry-intersection-with-three-csg-ts-volume-calculation/48261>\
**Category:** Questions\
**Tags:** intersection, npm, object3d, geometry\
**Created:** [February 20, 2023, 7:09pm UTC](https://discourse.threejs.org/t/buffergeometry-intersection-with-three-csg-ts-volume-calculation/48261 "2023-02-20T19:09:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wojciechteclaw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/wojciechteclaw/32/32642_2.png) [@wojciechteclaw](https://discourse.threejs.org/u/wojciechteclaw)\
**Post date:** [February 20, 2023, 7:09pm UTC](https://discourse.threejs.org/t/buffergeometry-intersection-with-three-csg-ts-volume-calculation/48261/1 "2023-02-20T19:09:28Z")

</div>

Hello All,  
I work with web-ifc, which uses the three.js. At some point, I want to compare two objects. For this purpose, I’m using three-csg-ts library. And the following function:

```
public static compareTwoGeometryRepresentations(
    geometry1: GeometricalRepresentation,
    geometry2: GeometricalRepresentation
): boolean {
    let mesh1 = CSG.fromMesh(geometry1.geometry);
    let mesh2 = CSG.fromMesh(geometry2.geometry);
    let result = mesh1.intersect(mesh2);
    let volume = GeometryService.getVolume(result.toGeometry(new Matrix4()));
    return (
        // logic
    );
}

```

Even if I intersect geometries with the same geometry, I get a volume far away from zero. For getVolume I use the implementation presented here:

> [@Volume of THREE.BufferGeometry()](https://discourse.threejs.org/t/volume-of-three-buffergeometry/5109):
>
> Hi, community! There were several similar questions on SO about how to compute the volume of a geometry. I wrote a function, that computes the volume of an buffer geometry (both indexed and non-indexed): function getVolume(geometry) { if (!geometry.isBufferGeometry) { console.log("'geometry' must be an indexed or non-indexed buffer geometry"); return 0; } var isIndexed = geometry.index !== null; let position = geometry.attributes.position; let sum = 0; le…

Can you give me some suggestions on what I am doing wrong or where should I change my thinking ? I’m nooby in the three.js and if you need any relevant information which might be helpful I’ll provide it.

Wojtek

---

<div class="post-metadata">

**Author:** ![wojciechteclaw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/wojciechteclaw/32/32642_2.png) [@wojciechteclaw](https://discourse.threejs.org/u/wojciechteclaw)\
**Post date:** [February 20, 2023, 7:11pm UTC](https://discourse.threejs.org/t/buffergeometry-intersection-with-three-csg-ts-volume-calculation/48261/2 "2023-02-20T19:11:52Z")

</div>

Object one:

````javascript
{
    "metadata": {
        "version": 4.5,
        "type": "Object",
        "generator": "Object3D.toJSON"
    },
    "geometries": [
        {
            "uuid": "07296F3C-9963-40A6-99F8-CCF7352749CA",
            "type": "BufferGeometry",
            "data": {
                "attributes": {
                    "position": {
                        "isInterleavedBufferAttribute": true,
                        "itemSize": 3,
                        "data": "0E49DBBA-4AB4-4355-9579-3E6593DD39FA",
                        "offset": 0,
                        "normalized": false
                    },
                    "normal": {
                        "isInterleavedBufferAttribute": true,
                        "itemSize": 3,
                        "data": "0E49DBBA-4AB4-4355-9579-3E6593DD39FA",
                        "offset": 3,
                        "normalized": false
                    }
                },
                "index": {
                    "type": "Uint32Array",
                    "array": [
                        3,
                        0,
                        1,
                        1,
                        2,
                        3,
                        8,
                        6,
                        5,
                        6,
                        8,
                        7,
                        10,
                        11,
                        12,
                        13,
                        14,
                        15,
                        16,
                        17,
                        18,
                        19,
                        20,
                        21,
                        22,
                        23,
                        24,
                        25,
                        26,
                        27,
                        28,
                        29,
                        30,
                        31,
                        32,
                        33
                    ]
                },
                "interleavedBuffers": {
                    "0E49DBBA-4AB4-4355-9579-3E6593DD39FA": {
                        "uuid": "0E49DBBA-4AB4-4355-9579-3E6593DD39FA",
                        "buffer": "9826E6F3-70D5-4EB3-9601-4DEB6489ACEA",
                        "type": "Float32Array",
                        "stride": 6
                    }
                },
                "arrayBuffers": {
                    "9826E6F3-70D5-4EB3-9601-4DEB6489ACEA": [
                        0,
                        1167663104,
                        1165623296,
                        0,
                        0,
                        1065353216,
                        0,
                        0,
                        1165623296,
                        0,
                        0,
                        1065353216,
                        1167663104,
                        0,
                        1165623296,
                        0,
                        0,
                        1065353216,
                        1167663104,
                        1167663104,
                        1165623296,
                        0,
                        0,
                        1065353216,
                        0,
                        1167663104,
                        1165623296,
                        0,
                        0,
                        1065353216,
                        0,
                        1167663104,
                        0,
                        2147483648,
                        2147483648,
                        3212836864,
                        0,
                        0,
                        0,
                        2147483648,
                        2147483648,
                        3212836864,
                        1167663104,
                        0,
                        0,
                        2147483648,
                        2147483648,
                        3212836864,
                        1167663104,
                        1167663104,
                        0,
                        2147483648,
                        2147483648,
                        3212836864,
                        0,
                        1167663104,
                        0,
                        2147483648,
                        2147483648,
                        3212836864,
                        0,
                        1167663104,
                        0,
                        3212836864,
                        0,
                        0,
                        0,
                        0,
                        1165623296,
                        3212836864,
                        0,
                        0,
                        0,
                        1167663104,
                        1165623296,
                        3212836864,
                        0,
                        0,
                        0,
                        1167663104,
                        0,
                        3212836864,
                        0,
                        0,
                        0,
                        0,
                        0,
                        3212836864,
                        0,
                        0,
                        0,
                        0,
                        1165623296,
                        3212836864,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        3212836864,
                        0,
                        1167663104,
                        0,
                        1165623296,
                        0,
                        3212836864,
                        0,
                        0,
                        0,
                        1165623296,
                        0,
                        3212836864,
                        0,
                        0,
                        0,
                        0,
                        0,
                        3212836864,
                        0,
                        1167663104,
                        0,
                        0,
                        0,
                        3212836864,
                        0,
                        1167663104,
                        0,
                        1165623296,
                        0,
                        3212836864,
                        0,
                        1167663104,
                        0,
                        0,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        1167663104,
                        1165623296,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        0,
                        1165623296,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        0,
                        0,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        1167663104,
                        0,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        1167663104,
                        1165623296,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        1167663104,
                        0,
                        0,
                        1065353216,
                        2147483648,
                        0,
                        1167663104,
                        1165623296,
                        0,
                        1065353216,
                        2147483648,
                        1167663104,
                        1167663104,
                        1165623296,
                        0,
                        1065353216,
                        2147483648,
                        1167663104,
                        1167663104,
                        0,
                        0,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        0,
                        0,
                        1065353216,
                        0,
                        0,
                        1167663104,
                        1165623296,
                        0,
                        1065353216,
                        0
                    ]
                }
            }
        }
    ],
    "materials": [
        {
            "uuid": "C464D1FD-9437-4FCD-8CE6-F3363B605BE6",
            "type": "MeshBasicMaterial",
            "color": 16777215,
            "reflectivity": 1,
            "refractionRatio": 0.98,
            "depthFunc": 3,
            "depthTest": true,
            "depthWrite": true,
            "colorWrite": true,
            "stencilWrite": false,
            "stencilWriteMask": 255,
            "stencilFunc": 519,
            "stencilRef": 0,
            "stencilFuncMask": 255,
            "stencilFail": 7680,
            "stencilZFail": 7680,
            "stencilZPass": 7680
        }
    ],
    "object": {
        "uuid": "B1752708-1F36-41FE-9938-F9DAB6657DFF",
        "type": "Mesh",
        "layers": 1,
        "matrix": [
            0,
            0,
            0.001,
            0,
            0.001,
            0,
            0,
            0,
            0,
            0.001,
            0,
            0,
            -7.891365415298417,
            0,
            3.3856466512794268,
            1
        ],
        "geometry": "07296F3C-9963-40A6-99F8-CCF7352749CA",
        "material": "C464D1FD-9437-4FCD-8CE6-F3363B605BE6"
    }
}```
````

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [February 23, 2023, 7:23pm UTC](https://discourse.threejs.org/t/buffergeometry-intersection-with-three-csg-ts-volume-calculation/48261/3 "2023-02-23T19:23:31Z")

</div>

It is hard to tell without a live editable example. Most likely I did not understand your question. In any case, if you have the same objects A and B, placed at the same positions, then their intersection will be the same as them (and not with volume 0).

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/e/4/e467b20a81a72c3a09a70961a9f83325fc2999b1.png)

---

<div class="post-metadata">

**Author:** ![wojciechteclaw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/wojciechteclaw/32/32642_2.png) [@wojciechteclaw](https://discourse.threejs.org/u/wojciechteclaw)\
**Post date:** [February 24, 2023, 6:58pm UTC](https://discourse.threejs.org/t/buffergeometry-intersection-with-three-csg-ts-volume-calculation/48261/4 "2023-02-24T18:58:44Z")

</div>

Thank you for your answer, and sorry for the improper description of the issue.

The issue while intersecting was caused by improper conversion to BufferGeometry inside the library (web-ifc). The update to the newest version (0.135) resolved the issue, which seemed to look right during uploading objects to scene, but its intersection result provided such the result:  
 ![MicrosoftTeams-image (1)](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/f/a/fa2c18cf9f557daa9c4f8dc1a05edb087d8b3da1.png)
