Title: util.shape.get_volume loses precision far from the origin (-1.8 % and +2.7 % on a 2.8 m3 wall at E 500 000, N 5 000 000) · Issue #9770 · IfcOpenShell/IfcOpenShell · GitHub
Open Graph Title: util.shape.get_volume loses precision far from the origin (-1.8 % and +2.7 % on a 2.8 m3 wall at E 500 000, N 5 000 000) · Issue #9770 · IfcOpenShell/IfcOpenShell
X Title: util.shape.get_volume loses precision far from the origin (-1.8 % and +2.7 % on a 2.8 m3 wall at E 500 000, N 5 000 000) · Issue #9770 · IfcOpenShell/IfcOpenShell
Description: Bug Description ifcopenshell.util.shape.get_volume sums signed tetrahedra about the origin. Each term of signed_triangle_volume is a product of three coordinates. When the vertices are millions of metres from the origin, as in a georefer...
Open Graph Description: Bug Description ifcopenshell.util.shape.get_volume sums signed tetrahedra about the origin. Each term of signed_triangle_volume is a product of three coordinates. When the vertices are millions of ...
X Description: Bug Description ifcopenshell.util.shape.get_volume sums signed tetrahedra about the origin. Each term of signed_triangle_volume is a product of three coordinates. When the vertices are millions of ...
Opengraph URL: https://github.com/IfcOpenShell/IfcOpenShell/issues/9770
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"util.shape.get_volume loses precision far from the origin (-1.8 % and +2.7 % on a 2.8 m3 wall at E 500 000, N 5 000 000)","articleBody":"### Bug Description\n\n`ifcopenshell.util.shape.get_volume` sums signed tetrahedra about the origin. Each term of `signed_triangle_volume` is a product of three coordinates. When the vertices are millions of metres from the origin, as in a georeferenced model in projected coordinates, those products are around 1e15. Their rounding errors no longer cancel, and most of the precision of the sum is lost.\n\nResults for a 5 × 0.2 × 2.8 m wall, 2.8 m³:\n\n| Case | `get_volume` | Error |\n|---|---|---|\n| Placed at E 500 000, N 5 000 000, Z 450, read with `use-world-coords` | 2.75 m³ | −1.8 % |\n| The same wall rotated by 30° | 2.875 m³ | +2.7 % |\n| Placement at the origin, body positioned at those coordinates, default settings | 2.75 m³ | −1.8 % |\n\nThe third case does not need world coordinates: models that carry project coordinates in the geometry itself are affected with the default settings. The ifc5d quantity calculator computes its `get_volume` quantities on geometry converted without `use-world-coords` (https://github.com/IfcOpenShell/IfcOpenShell/blob/8c614fa298c4cba0163762f18ccfe117f45ad730/src/ifc5d/ifc5d/qto.py#L454-L472, with `get_volume` registered at L414); its weight calculation also calls it (L661).\n\nThe same sum taken about the first vertex gives 2.8 to within 4e-9 relative. That is the rounding of the coordinates themselves: about 1e-9 m near 5e6 m, on a wall 0.2 m thick.\n\nTo reproduce, run the script below. It needs only ifcopenshell and builds its own IFC4 model in memory. Exit code 1 means the problem reproduces, 0 that it does not, 2 that a control case failed or an exception was raised.\n\n\u003cdetails>\n\u003csummary>Script\u003c/summary>\n\n```python\n\"\"\"ifcopenshell.util.shape.get_volume loses most of its precision when the triangulation lies far from the\norigin (georeferenced models: projected coordinates of millions of metres), because it sums signed\ntetrahedra about the origin. Subtracting any fixed point first (for example the first vertex) leaves a\nclosed mesh's volume unchanged and keeps it exact.\n\nSelf-contained: builds a synthetic IFC4 model in memory, needs only `pip install ifcopenshell`\n(ifcopenshell.util.shape imports numpy and shapely, both dependencies of the ifcopenshell wheel).\nExit code: 1 if the defect reproduces, 0 if not, 2 if a control case fails or an exception is raised.\n\"\"\"\n\nimport platform\nimport sys\nimport traceback\nimport uuid\n\nimport ifcopenshell\nimport ifcopenshell.geom\nimport ifcopenshell.guid\nimport ifcopenshell.util.shape\n\nTOLERANCE = 1e-7 # relative; coordinates near 5e6 m are themselves rounded to about 1e-9 m\nFAR = (500000, 5000000, 450) # E, N, Z in metres, of the size of projected map-grid coordinates\nCOS30, SIN30 = 0.8660254037844387, 0.5\n_ids = iter(range(1, 10**6))\n\n\ndef guid():\n return ifcopenshell.guid.compress(uuid.UUID(int=next(_ids)).hex)\n\n\ndef build():\n \"\"\"A 5 x 0.2 x 2.8 m wall (2.8 m3): at the origin; placed far away (read in world coordinates), also\n rotated by 30 degrees about z; and with the far-away position in the body itself, under a placement at\n the origin (read in local coordinates, the library's default).\"\"\"\n f = ifcopenshell.file(schema=\"IFC4\")\n c = f.create_entity\n\n def pt(*v):\n return c(\"IfcCartesianPoint\", Coordinates=[float(x) for x in v])\n\n def d(*v):\n return c(\"IfcDirection\", DirectionRatios=[float(x) for x in v])\n\n def ax3(location, x_axis=(1, 0, 0)):\n return c(\"IfcAxis2Placement3D\", Location=pt(*location), Axis=d(0, 0, 1), RefDirection=d(*x_axis))\n\n ctx = c(\n \"IfcGeometricRepresentationContext\",\n ContextType=\"Model\",\n CoordinateSpaceDimension=3,\n Precision=1e-5,\n WorldCoordinateSystem=c(\"IfcAxis2Placement3D\", Location=pt(0, 0, 0)),\n )\n units = c(\n \"IfcUnitAssignment\",\n Units=[\n c(\"IfcSIUnit\", UnitType=\"LENGTHUNIT\", Name=\"METRE\"),\n c(\"IfcSIUnit\", UnitType=\"PLANEANGLEUNIT\", Name=\"RADIAN\"),\n ],\n )\n c(\"IfcProject\", GlobalId=guid(), Name=\"synthetic\", RepresentationContexts=[ctx], UnitsInContext=units)\n cases = []\n for label, placement, body_position, world, is_control in (\n (\"CONTROL at the origin\", (0, 0, 0), (0, 0, 0), True, True),\n (\"placed at E 500000, N 5000000, Z 450, world coordinates\", FAR, (0, 0, 0), True, False),\n (\"same, rotated 30 deg about z, world coordinates\", FAR, (0, 0, 0), True, False),\n (\"body at E 500000, N 5000000, Z 450, local coordinates\", (0, 0, 0), FAR, False, False),\n ):\n profile = c(\n \"IfcRectangleProfileDef\",\n ProfileType=\"AREA\",\n Position=c(\"IfcAxis2Placement2D\", Location=pt(2.5, 0)),\n XDim=5.0,\n YDim=0.2,\n )\n body = c(\n \"IfcExtrudedAreaSolid\",\n SweptArea=profile,\n Position=ax3(body_position),\n ExtrudedDirection=d(0, 0, 1),\n Depth=2.8,\n )\n rep = c(\n \"IfcShapeRepresentation\",\n ContextOfItems=ctx,\n RepresentationIdentifier=\"Body\",\n RepresentationType=\"SweptSolid\",\n Items=[body],\n )\n x_axis = (COS30, SIN30, 0) if \"rotated\" in label else (1, 0, 0)\n wall = c(\n \"IfcWall\",\n GlobalId=guid(),\n Name=label,\n ObjectPlacement=c(\"IfcLocalPlacement\", RelativePlacement=ax3(placement, x_axis)),\n Representation=c(\"IfcProductDefinitionShape\", Representations=[rep]),\n )\n cases.append((label, wall, 2.8, world, is_control))\n return f, cases\n\n\ndef recentred_volume(geometry):\n \"\"\"The same divergence-theorem sum, about the first vertex instead of the origin.\"\"\"\n v, fc = list(geometry.verts), list(geometry.faces)\n ox, oy, oz = v[0], v[1], v[2]\n total = 0.0\n for i in range(0, len(fc), 3):\n (ax, ay, az), (bx, by, bz), (cx, cy, cz) = (\n (v[3 * j] - ox, v[3 * j + 1] - oy, v[3 * j + 2] - oz) for j in fc[i : i + 3]\n )\n total += ax * (by * cz - bz * cy) - ay * (bx * cz - bz * cx) + az * (bx * cy - by * cx)\n return abs(total) / 6.0\n\n\ndef main():\n print(\n f\"ifcopenshell {ifcopenshell.version} ({ifcopenshell.ifcopenshell_wrapper.version()}), \"\n f\"Python {platform.python_version()}, {platform.system()} {platform.machine()}\"\n )\n _model, cases = build() # the file object stays referenced while its instances are used\n defect = control_failed = False\n for label, wall, truth, world, is_control in cases:\n settings = ifcopenshell.geom.settings()\n settings.set(\"use-world-coords\", world)\n shape = ifcopenshell.geom.create_shape(settings, wall)\n g = shape.geometry # keep `shape` alive while its geometry is read\n library = ifcopenshell.util.shape.get_volume(g)\n own = recentred_volume(g)\n ok_lib = abs(library - truth) \u003c= TOLERANCE * truth\n ok_own = abs(own - truth) \u003c= TOLERANCE * truth\n error = (library - truth) / truth\n print(\n f\" {label:58s} expected {truth:.9f} get_volume {library:.9f} (rel. error {error:+.2e}) \"\n f\"{'OK' if ok_lib else 'WRONG'}; re-centred {own:.9f} {'OK' if ok_own else 'WRONG'}\"\n )\n control_failed |= not ok_own or (is_control and not ok_lib)\n defect |= not ok_lib and not is_control\n print(\"\\nRESULT:\", \"CONTROL FAILED\" if control_failed else (\"DEFECT REPRODUCED\" if defect else \"not reproduced\"))\n return 2 if control_failed else (1 if defect else 0)\n\n\nif __name__ == \"__main__\":\n try:\n code = main()\n except Exception: # for example a kernel that cannot be loaded: an environment problem, not this defect\n traceback.print_exc()\n print(\"\\nRESULT: CONTROL FAILED (unexpected exception)\")\n code = 2\n sys.exit(code)\n```\n\n\u003c/details>\n\nObserved (0.9.0, exit code 1; 0.8.5 gives the same values):\n\n```\nifcopenshell 0.9.0 (0.9.0alpha0-8c614fa), Python 3.14.7, Windows AMD64\n CONTROL at the origin expected 2.800000000 get_volume 2.800000000 (rel. error +0.00e+00) OK; re-centred 2.800000000 OK\n placed at E 500000, N 5000000, Z 450, world coordinates expected 2.800000000 get_volume 2.750000001 (rel. error -1.79e-02) WRONG; re-centred 2.799999990 OK\n same, rotated 30 deg about z, world coordinates expected 2.800000000 get_volume 2.875000001 (rel. error +2.68e-02) WRONG; re-centred 2.800000007 OK\n body at E 500000, N 5000000, Z 450, local coordinates expected 2.800000000 get_volume 2.750000001 (rel. error -1.79e-02) WRONG; re-centred 2.799999990 OK\n\nRESULT: DEFECT REPRODUCED\n```\n\n**Expected.** 2.8 m³ to within the rounding of the coordinates themselves, a few 1e-9 relative here.\n\n**Suggested fix.** The volume of a closed mesh does not depend on the reference point of the sum, so the vertices can be taken relative to one of them. Against `src/ifcopenshell-python/ifcopenshell/util/shape.py` on `v0.9.0` (https://github.com/IfcOpenShell/IfcOpenShell/blob/8c614fa298c4cba0163762f18ccfe117f45ad730/src/ifcopenshell-python/ifcopenshell/util/shape.py#L89-L97):\n\n```diff\n verts = geometry.verts\n faces = geometry.faces\n- grouped_verts = [[verts[i], verts[i + 1], verts[i + 2]] for i in range(0, len(verts), 3)]\n+ if not faces:\n+ return 0.0\n+ x0, y0, z0 = verts[0], verts[1], verts[2]\n+ grouped_verts = [[verts[i] - x0, verts[i + 1] - y0, verts[i + 2] - z0] for i in range(0, len(verts), 3)]\n```\n\nWe applied this change to the library's own `get_volume` source and ran the script above against it:\n\n- the far-away cases give 2.799999990, 2.800000007 and 2.799999990 (relative errors −3.7e-9, +2.6e-9 and −3.7e-9);\n- the wall at the origin is unchanged;\n- the early return keeps the current result, 0, for a geometry without faces.\n\nPR #9750, which is open, also edits `get_volume` (it returns nan for non-manifold meshes). This change is independent of it.\n\nThis report, its script and the suggested change were prepared with the assistance of an AI coding tool; the outputs shown come from actual runs on the versions stated.\n\n### Attachments\n\nNone. The script above builds its own model.\n\n### Debug and Error Output\n\n```\nifcopenshell 0.9.0 from PyPI (ifcopenshell_wrapper.version(): 0.9.0alpha0-8c614fa)\nCPython 3.14.7, Windows 11 x64; Linux and macOS not tested.\nSame values with 0.8.5 (0.8.5-1c5b825).\n```\n","author":{"url":"https://github.com/BryanMGIT","@type":"Person","name":"BryanMGIT"},"datePublished":"2026-10-01T19:58:39Z","url":"https://github.com/IfcOpenShell/IfcOpenShell/issues/9770"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:42d00b07-8fbf-19b7-cc01-e994ecf863f1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DE30:245336:987A82:CFB1F8:6B048187 |
| html-safe-nonce | caa540b3fa9367555d11f46c1373825e21a0a53af1cd0058bd4692e721322f9a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERTMwOjI0NTMzNjo5ODdBODI6Q0ZCMUY4OjZCMDQ4MTg3IiwidmlzaXRvcl9pZCI6IjQxODM1NjA4NDU0NzkxNDk5NjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1ce2bae600705446e77cd313b56db2884231fb3ecc23dc28a997521b875a5b2c |
| hovercard-subject-tag | issue:5668306001 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/IfcOpenShell/IfcOpenShell/9770/issue_layout |
| twitter:image | https://opengraph.githubassets.com/311c12ee324bf329bc4d30fbcaedc5f64c8a602b1fdda64c8981c06eeda7c608/IfcOpenShell/IfcOpenShell/issues/9770 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/311c12ee324bf329bc4d30fbcaedc5f64c8a602b1fdda64c8981c06eeda7c608/IfcOpenShell/IfcOpenShell/issues/9770 |
| og:image:alt | Bug Description ifcopenshell.util.shape.get_volume sums signed tetrahedra about the origin. Each term of signed_triangle_volume is a product of three coordinates. When the vertices are millions of ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | BryanMGIT |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0366ddfb372682a32e92a5d86155cb08fa83e5bbe79953a968789f2f7e24c511 |
| turbo-cache-control | no-preview |
| go-import | github.com/IfcOpenShell/IfcOpenShell git https://github.com/IfcOpenShell/IfcOpenShell.git |
| octolytics-dimension-user_id | 13692563 |
| octolytics-dimension-user_login | IfcOpenShell |
| octolytics-dimension-repository_id | 40473201 |
| octolytics-dimension-repository_nwo | IfcOpenShell/IfcOpenShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 40473201 |
| octolytics-dimension-repository_network_root_nwo | IfcOpenShell/IfcOpenShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ed3f2e01f6b7672780c586c8af0e72726374d7b6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width