News for hackers. Stuff that matters.
Re:Hilariously Fast Volume Computation with the Divergence T... by srean on Friday August 28, 2026 @08:12AM (#49477382)
On the other hand, if you want to compute the area of a polygon that have vertices at lattice points, you can count the number of interior points I, the number of boundary points B. Then the area A is

    A = B + I/2 - 1
This is Pick's theorem

https://en.wikipedia.org/wiki/Pick's_theorem

one of my favorite results. It does not generalize as nicely to higher dimensions unfortunately.

Re:Hilariously Fast Volume Computation with the Divergence T... by sebastianmestre on Friday August 28, 2026 @08:30AM (#49477586)
From a computational standpoint, Pick's theorem seems more useful to find the number of interior points via

    I = 2 (A - B + 1)
Where area would be calculated using the sum of signed areas of triangles.
Re:Hilariously Fast Volume Computation with the Divergence T... by srean on Friday August 28, 2026 @08:36AM (#49477642)
Indeed.

One of my off by one errors is a stupid hacky Monte Carlo intution for Picks theorem.

I count the number of points inside. Now about the boundary points I must assign some fractional weight because they are not fully inside. What's a stupid fraction I can use? Well, half seems about right. Voila,

    A = I + B/2.
Re:Hilariously Fast Volume Computation with the Divergence T... by eterevsky on Friday August 28, 2026 @06:09AM (#49476604)
Isn't the same as just taking every triangle from the mesh, calculating the volume of a prism-like polytope between it and its projection on one the planes, and then taking it with a + sign if its projection is oriented in one direction, and with a - sign if it's oriented in another? This kind of formula works based on the basic geometry.
Re:Hilariously Fast Volume Computation with the Divergence T... by Sharlin on Friday August 28, 2026 @07:11AM (#49476969)
Yes, this is essentially what the author derived (by means of calculus rather than geometric argument but the result is unsurprisingly the same). The 2D analog is easy to grok: to compute the area of a polygon, find the sum of the signed areas of each of the trapezoids formed by an edge and its projection on the x-axis. Turns out the negative areas of the right-to-left trapezoids cancel precisely out any excess area of the left-to-right trapezoids (or in the case of edges below the x-axis, add precisely the "missing" area).
Re:Hilariously Fast Volume Computation with the Divergence T... by calf on Friday August 28, 2026 @07:23PM (#49485396)
I'm stupid, consider a quadrilateral with points

    b
  a    c
     d
Then the area needs to be the trapezoids projected to x axis is ab+bc-ad-dc. What is the correct way to keep track of the signs? I.e. what is the definition of "right-to-left"?
Re:Hilariously Fast Volume Computation with the Divergence T... by Sharlin on Saturday August 29, 2026 @10:57AM (#49490444)
Polygon edges are usually considered directed and forming a loop, the edges of said quad being either ab, bc, cd, da (clockwise winding) or ad, dc, cb, ba (counterclockwise winding). They give the same result up to sign, as long as you’re consistent. In the first case ab and bc are left-to-right and cd and da right-to-left, and vice versa in the second case. (Vertical edges of course contribute zero area.) The signed area of a right-to-left trapezoid is automatically negative (if above the x-axis) because the delta-x is negative (eg. a_x - d_x < 0 for the da edge), so there’s no need to worry about whether to add or subtract each individual term.
Re:Hilariously Fast Volume Computation with the Divergence T... by joelthelion on Friday August 28, 2026 @07:57AM (#49477252)
> calculating the volume of a prism-like polytope between it and its projection

There is the key insight that you don't need to explicitly compute this projection.

Re:Hilariously Fast Volume Computation with the Divergence T... by ted_dunning on Friday August 28, 2026 @02:56PM (#49482865)
More correctly, the insight is that this computation is trivial after you expand it.
Re:Hilariously Fast Volume Computation with the Divergence T... by cgadski on Friday August 28, 2026 @07:20AM (#49477024)
Yep. Using the same kind of calculus ideas, I can also think about a vector field that has a Dirac mass of divergence at some point and zero divergence everywhere else. Then you get an expression that you can sum over faces to determine if a polyhedron contains some point. Again, for the right vector field there is a simple geometric interpretation, namely the solid angle that a face makes with respect to the point.
Re:Hilariously Fast Volume Computation with the Divergence T... by aaa_aaa on Friday August 28, 2026 @06:23AM (#49476684)
Yes I remember doing something like that in 90s for a survey/map engineering cad application. After delaunay triangulation, calculating approximate voulume is easy. But this probably is a more general solution
Re:Hilariously Fast Volume Computation with the Divergence T... by srean on Friday August 28, 2026 @09:02AM (#49477892)
Yes it goes by a couple of names. Surveyor's formula, shoelace formula.

The Surveyor’s Area Formula Bart Braden The College Mathematics Journal, September 1986, Volume 17, Number 4.

https://web.archive.org/web/20150406152731if_/http://www.maa...

Re:Hilariously Fast Volume Computation with the Divergence T... by sigbottle on Friday August 28, 2026 @10:52AM (#49479426)
Oh, it is the shoelace formula, but for 3D?

OP's description vaguely triggered memories of the shoelace formula from a decade ago, but geometry was never my strong suit. All I remembered was positive/negative triangles (or was it trapezoids)? make magic happen for area calculation.

Re:Hilariously Fast Volume Computation with the Divergence T... by xigoi on Friday August 28, 2026 @06:47AM (#49476824)
I wonder if this could be reversed to give an intuitive “proof” of the divergence theorem.
Re:Hilariously Fast Volume Computation with the Divergence T... by meindnoch on Friday August 28, 2026 @07:39AM (#49477134)
The divergence theorem can be intuitively summarized in one sentence: "what comes out is whatever went in, plus whatever was produced inside"
Re:Hilariously Fast Volume Computation with the Divergence T... by MarkusQ on Friday August 28, 2026 @10:58AM (#49479523)
Minus whatever went away inside, unless you're implicitly allowing negative production.
Re:Hilariously Fast Volume Computation with the Divergence T... by meindnoch on Friday August 28, 2026 @12:00PM (#49480484)
Yes, both "production" and "comes out" are signed quantities.
Re:Hilariously Fast Volume Computation with the Divergence T... by seanhunter on Friday August 28, 2026 @11:21AM (#49479904)
A very intuitive presentation of the divergence theorem is here https://youtu.be/TORt20_HjMY?is=uoJ8-2ToCSwW9rVF
Re:Hilariously Fast Volume Computation with the Divergence T... by diabllicseagull on Friday August 28, 2026 @08:20AM (#49477475)
reminds me of that 1994 paper that reinvented the trapezoidal rule
Re:Hilariously Fast Volume Computation with the Divergence T... by MarkusQ on Friday August 28, 2026 @11:00AM (#49479567)
Except this one 1) didn't try to name it after themselves, 2) explicitly told us it wasn't new, and linked to an example of prior work, and 3) isn't generally taught in high school.
Re:Hilariously Fast Volume Computation with the Divergence T... by diabllicseagull on Friday August 28, 2026 @01:00PM (#49481427)
see, when I say something reminds me of something else I don't mean a one-to-one equivalence at every aspect one can think of.

anyhoo, it's always a great skill to review the state-of-the-art BEFORE investing in a work/write-up/article - one of the very first things that a post-graduate program would teach you.

Re:Hilariously Fast Volume Computation with the Divergence T... by 12_throw_away on Friday August 28, 2026 @01:11PM (#49481589)
Nah. Being able to derive and prove easy results like this for yourself is both way faster and more reliable than trying to wade through the literature to find the equation you want.
Re:Hilariously Fast Volume Computation with the Divergence T... by nyeah on Friday August 28, 2026 @02:47PM (#49482759)
The article relates that this isn't about the state of the art. It's about insight gained while taking a calculus class.
Re:Hilariously Fast Volume Computation with the Divergence T... by diabllicseagull on Friday August 28, 2026 @03:36PM (#49483261)
"The following presents a fast algorithm for volume computation of a simple, closed, triangulated 3D mesh."

- we are presented something

"I would be (pleasantly) surprised if the algorithm is novel. Further research after posting reveals the paper Efficient Feature Extraction for 2D/3D Objects in Mesh Representation by Cha Zheng and Tsuhan Chen, which appears to describe the same algorithm, although the derivation is different. It was fun while it lasted!"

- there was an initial expectation, though slim, that it may be novel. then on the discovery that it wasn't, related fun time was insinuated to be over.

are we reading the same text?

Re:Hilariously Fast Volume Computation with the Divergence T... by arjie on Friday August 28, 2026 @04:40PM (#49483997)
It’s someone’s blog post before a calculus exam. I think it’s fun to do this stuff. On your own site you can write whatever you want.

See, when I was a kid I found something I thought was great and inventive only to find that it was not only long trodden mathematics it was famous long trodden mathematics. I named it after myself for humorous value, the distinction between others who dreamed of finding some novel structure and me being solely that I did not know the famous results and conjectures in the space.

https://wiki.roshangeorge.dev/w/Roshan%27s_Conjecture

I think this is quite entertaining.

Re:Hilariously Fast Volume Computation with the Divergence T... by gloveone on Friday August 28, 2026 @10:39AM (#49479227)
Yes, the algorithm and its derivations are elementary and rather obvious for anyone decent at undergraduate level mathematics. But still, I am glad to see more people enjoying math!
Re:Hilariously Fast Volume Computation with the Divergence T... by phkahler on Friday August 28, 2026 @03:39PM (#49483282)
Better still, you calculate the signed volume of the the tetrahedron formed by the triangle and the origin (4th vertex).
Re:Hilariously Fast Volume Computation with the Divergence T... by elikoga on Friday August 28, 2026 @06:09AM (#49476606)
My belly says the naive formula is summing the triangle pyramid volumes to the origin with sign in orientation. It looks like that's what they derived. Which is a generalization of 2d polygon area calculated by summing triangle areas for each edge, I was taught this in a math camp where we calculated map polygon areas on gis data. I remember math knowledge being hard to get pre AI era but I didn't remember it being this hard.

No idea what the author means by "which are equivalent to rendering the mesh and then sampling the render".

Re:Hilariously Fast Volume Computation with the Divergence T... by less_less on Friday August 28, 2026 @07:37AM (#49477127)
> My belly says the naive formula is summing the triangle pyramid volumes to the origin with sign in orientation.

Yeah, that would also work but it's a slightly slower formula, sum(det(v1,v2,v3))/6. This one is summing sort of prism+pyramid shapes made by projecting each triangle to the yz plane.

Re:Hilariously Fast Volume Computation with the Divergence T... by chrisjj on Friday August 28, 2026 @10:52AM (#49479435)
I'd say what author means is the standard solution - which is equivalent to his on triangles but is on pixels ... except there's nothing naive about it and by using GPU parallelism and depth hardware it is lower cost on dense meshes.
Re:Hilariously Fast Volume Computation with the Divergence T... by uecker on Friday August 28, 2026 @12:01PM (#49480494)
I really hope nobody computes the volume by rendering in 3D and counting pixels.
Re:Hilariously Fast Volume Computation with the Divergence T... by chrisjj on Friday August 28, 2026 @03:25PM (#49483135)
Rendering in 2D and summing per-pixel Z-spans. For a finely meshed shape and approx. result, it has by far the lowest cost.
Re:Hilariously Fast Volume Computation with the Divergence T... by uecker on Saturday August 29, 2026 @03:02AM (#49487615)
Yes, this makes more sense as you at least collapse the third dimensions, but I would assume this formula is still faster when implemented efficiently.
Re:Hilariously Fast Volume Computation with the Divergence T... by PiXeL161616 on Friday August 28, 2026 @02:45PM (#49482750)
Voxelising, most likely. The naive way's to rasterise the mesh into a 3D grid and count the cells inside, which really is rendering and then sampling the result. It costs resolution cubed instead of triangle count, and the answer's only ever as good as the grid.
Re:Hilariously Fast Volume Computation with the Divergence T... by meindnoch on Friday August 28, 2026 @07:23AM (#49477041)
Don't really need vector calculus for this. Geometric intuition is sufficient. It is simply the summation of signed volumes of triangular columns/prisms parallel to the X axis.

Visualization: https://jsfiddle.net/L7r1hwca/

I don't know what they could possibly mean by the naïve algorithms with rendering and sampling (???).

Re:Hilariously Fast Volume Computation with the Divergence T... by nyeah on Friday August 28, 2026 @08:14AM (#49477400)
[delayed]
Re:Hilariously Fast Volume Computation with the Divergence T... by physicsguy on Friday August 28, 2026 @07:30AM (#49477090)
This is one of those when you go "Huh, this is amazing!" or "Huh, I thought this trick was really well known!" depending on your background ;)

Here's a similar impl from 1980 written in Fortran that also computes other properties like centroid: https://calgo.acm.org/550.zip Algorithm 550: Solid Polyhedron Measures A. M. Messner and G. Q. Taylor ACM Trans. Math. Softw., 6(1), Mar 1980, pp.121--130 Keywords: polyhedron, graphics, numerical integration Language: Fortran 66/77; Shar Index: Z; Gams: P File size: 19.1 KB;

But Messner published it first in: A. M. Messner, "A surface Integral method for computer calculation of mass properties", Paper No. 852, 29TH ANNUAL CONF. OF THE SOCIETY OF AERONAUTICAL WEIGHT ENGINEERS, Washington, D.C., May 1970.

I think

Re:Hilariously Fast Volume Computation with the Divergence T... by jacobolus on Friday August 28, 2026 @10:52AM (#49479429)
This is one variant of the 3-d analog of the "shoelace formula" for area:

https://en.wikipedia.org/wiki/Shoelace_formula#Generalizatio...

The 2-d version is from the 18th century. I'd expect the 3-d version was probably known in the 19th century, but I haven't searched for a reference.

Re:Hilariously Fast Volume Computation with the Divergence T... by amluto on Friday August 28, 2026 @11:29AM (#49480014)
This technique should be straightforwardly adaptable to compute arbitrary moments, not just the centroid.

If you have a scalar-valued function that you can conveniently express as the divergence of any closed-form function, you can integrate it like this. And you can generalize beyond scalar-valued functions and beyond Euclidean space using the generalized Stokes’ theorem.

You can even do this in real life: if you want to integrate the electric current density through a surface (that is, measure the total current crossing the surface), you can integrate its anti-curl (is that a word?) around the boundary of that surface, which is what a current transformer or a clamp-on current meter does.

I bet there’s a hydraulic or pneumatic analog as well, but a nontrivial example isn’t immediately coming to mind.

Re:Hilariously Fast Volume Computation with the Divergence T... by ted_dunning on Friday August 28, 2026 @02:55PM (#49482845)
The hydraulic analog is that you can weigh a volume of water (which is the same as computing its volume) by adding up the forces on the surface surrounding the water.

This looks like it requires a dot product with the normal vector for each triangle, but you can expand it into the same form as the article.

Re:Hilariously Fast Volume Computation with the Divergence T... by georgeburdell on Friday August 28, 2026 @04:59PM (#49484187)
As an aside, this seems related to the Holographic Principle? It states that the content of a volume is encoded in its surface

https://en.wikipedia.org/wiki/Holographic_principle

Re:Hilariously Fast Volume Computation with the Divergence T... by gurkwart on Friday August 28, 2026 @07:06AM (#49476941)
There's a really elegant solution using Geometric Algebra, that to this day is one of the most satisfying things I've ever learnt. Steven de Keninck outlines it in his 2019 Siggraph talk [1].

[1] https://youtu.be/tX4H_ctggYo?t=4795

Re:Hilariously Fast Volume Computation with the Divergence T... by arn3n on Friday August 28, 2026 @06:02AM (#49476562)
I love these kinds of posts. Simple, fast, AI-free, and I learn something new.
Re:Hilariously Fast Volume Computation with the Divergence T... by alex_suzuki on Friday August 28, 2026 @08:11AM (#49477373)
(2018)
Re:Hilariously Fast Volume Computation with the Divergence T... by IAmBroom on Friday August 28, 2026 @10:04AM (#49478669)
OMG, that absolute dummy didn't know something that a human did in 2018!

Are they not reading the entire internet every morning, when they wake up???

Re:Hilariously Fast Volume Computation with the Divergence T... by alex_suzuki on Friday August 28, 2026 @10:12AM (#49478786)
I was merely pointing out that the post was published in 2018 so AI-free is kind of implied.
Re:Hilariously Fast Volume Computation with the Divergence T... by FartyMcFarter on Friday August 28, 2026 @07:40AM (#49477152)
> (No, there won’t be jokes.)

I must be missing something here, inside joke or something in the title?

Re:Hilariously Fast Volume Computation with the Divergence T... by Agentlien on Friday August 28, 2026 @07:43AM (#49477163)
I assume it's because the title contains "hilariously" and the author felt it necessary to state the content wasn't meant as humorous.
Re:Hilariously Fast Volume Computation with the Divergence T... by N_Lens on Friday August 28, 2026 @06:08AM (#49476597)
I'll accept any kind of jocularity in the current climate!
Re:Hilariously Fast Volume Computation with the Divergence T... by gigatexal on Friday August 28, 2026 @06:16AM (#49476641)
Did they also work on the graphics stack for the Asahi project?
Re:Hilariously Fast Volume Computation with the Divergence T... by StilesCrisis on Friday August 28, 2026 @06:54AM (#49476865)
Yup!
Re:Hilariously Fast Volume Computation with the Divergence T... by unkeen on Friday August 28, 2026 @06:37AM (#49476758)
Sadly, there is no way to find out, f.ex. by a quick Google search.
Re:Hilariously Fast Volume Computation with the Divergence T... by gigatexal on Friday August 28, 2026 @10:35AM (#49479162)
I used to watch their vtuber videos on YouTube!
Re:Hilariously Fast Volume Computation with the Divergence T... by bob1029 on Friday August 28, 2026 @08:18AM (#49477452)
> For a ballpark number, if volume needs to be calculated every frame in a high-performance 60 frames per second application, without the aid of a GPU, only using the CPU capabilities of a $35 Raspberry Pi, around 30 million triangles could be measured every frame.

If knowing the volume of a mesh is important, we could pre-calculate it (even using this exact technique) and store it as an attribute on the object. Lots of things in game dev that are modeled as an integral over three+ dimensions tend to work better as a baked setup rather than real time. We kickstarted an entire AI industry trying to chase real time lighting.

Re:Hilariously Fast Volume Computation with the Divergence T... by Isofarro on Friday August 28, 2026 @08:31AM (#49477593)
I'm sorry, English is my first language. What does "Hilariously" mean in this context? Or is there a maths specific meaning/interpretation?
Re:Hilariously Fast Volume Computation with the Divergence T... by Joker_vD on Friday August 28, 2026 @12:12PM (#49480669)

    Adverb
        hilariously (comparative more hilariously, superlative most hilariously)

        1. In a hilarious manner; so as to amuse greatly.
The author was greatly amused how quick the resulting algorithm works.
Re:Hilariously Fast Volume Computation with the Divergence T... by MarkusQ on Friday August 28, 2026 @11:06AM (#49479678)
It's an intensifier. As a native English speaker you should probably be aware that we eventually sand-blast the semantics off of words until they all becomes synonyms for "good", "bad", "very", or "um". (This is similar to what French does to phonemes, but unrelated.)
Re:Hilariously Fast Volume Computation with the Divergence T... by hallgrim on Friday August 28, 2026 @08:51AM (#49477794)
The author is just expressing amusement at the surprising simplicity of the resulting algorithm
Re:Hilariously Fast Volume Computation with the Divergence T... by ahaferburg on Friday August 28, 2026 @09:06AM (#49477931)
The emphasis here is on the mesh being simple and closed. Make sure to validate these preconditions before relying on the output.

Similar formulas exist for moments, to compute the inertia matrix for a rigid body.

Re:Hilariously Fast Volume Computation with the Divergence T... by MarkusQ on Friday August 28, 2026 @11:03AM (#49479619)
Closed is clearly important. Why does it have to be simple? It looks like it should handle disjoint components, interior holes, etc. just fine?
Re:Hilariously Fast Volume Computation with the Divergence T... by phkahler on Friday August 28, 2026 @03:47PM (#49483348)
>> Similar formulas exist for moments, to compute the inertia matrix for a rigid body.

Fun fact. The inertia for any rigid body can be represented by 4 point masses forming a tetrahedron. If you diagonalize the inertia matrix, the coordinates of the 4 point masses can be (x, y, -z) (-x,-y,-z) (x, -y, z) (-x, y, z) where x,y,z are easy to calculate (I wrote this all down ages ago). You can also represent any point on the rigid body by its barycentric coordinates relative to those points. I believe an impulse can be applied, by finding the barycentric coordinates of the point its applied and using those coordinates to distribute the impulse to the 4 masses.

This is all really cool with one huge exception. The 4 points become coplanar for large flat objects, which means the z-height is really small for a piece of sheet metal for example.

Re:Hilariously Fast Volume Computation with the Divergence T... by lern_too_spel on Friday August 28, 2026 @10:23AM (#49478935)
You might be interested in the shoelace formula and its generalization to n dimensions. https://en.wikipedia.org/wiki/Shoelace_formula
Re:Hilariously Fast Volume Computation with the Divergence T... by lern_too_spel on Friday August 28, 2026 @12:53PM (#49481312)
For this particular case of 3 dimensions, I found

Newson, H. B. “On the Volume of a Polyhedron.” Annals of Mathematics, vol. 1, no. 1/4, 1899, pp. 108–10. JSTOR, https://doi.org/10.2307/1967277

Re:Hilariously Fast Volume Computation with the Divergence T... by OscarCunningham on Friday August 28, 2026 @01:03PM (#49481500)
It's also robust to errors in the mesh. Like if the triangles don't quite join up it still gives a reasonable answer. https://mathstodon.xyz/@keenancrane/109388206643166726
Re:Hilariously Fast Volume Computation with the Divergence T... by xbar on Friday August 28, 2026 @02:34PM (#49482632)
What a fun post! My vector calculus is rusty so it was a pleasant little derivation.

I liked getting to the end an find A.R. as the author. It made me appreciate this part of the journey that eventually got us some Asahi Linux graphics.

Re:Hilariously Fast Volume Computation with the Divergence T... by hingler36 on Friday August 28, 2026 @05:37PM (#49484528)
This appears to be a retelling of another well-known process for volume computation: sum together the signed volumes of the tetraheda formed by each face with the origin (or any other fixed point WLOG). Very cool derivation though!
Re:Hilariously Fast Volume Computation with the Divergence T... by bmenrigh on Friday August 28, 2026 @05:49PM (#49484656)
At first I was going to say this is just the tetrahedra trick dressed up in slightly different clothes, and some sense it is, but there is a nice cancellation in the y and z coordinates which leads to less calculation in practice.
Re:Hilariously Fast Volume Computation with the Divergence T... by amelius on Wednesday September 02, 2026 @03:16AM (#49532839)
It's a pity they use the nabla symbol for vector difference. This symbol usually has a different meaning in the context of vector calculus.