Geometry Nodes. Find the center of a rectangle whether or not there are multiple vertices on one side

I'm doing some buildings and, as some dimension varies, I found a way to center the doors and windows on specific faces selected via different materials. However the door is "lifted" up. More verticies on the upper edge more the door is shifted on +Z Axis. There is a way to stay at the center, the REAL center of that rectangle?

Mesh to PointsFaces node generates the points at the average of all of each face’s vertices, so when you add more vertices at the top edge of a face by dividing the neighboring face, you're adding more Z value to that position, thus moving it up. I'm not aware of a native, simpler method, so to get the real (mass) center of the face you need to do some math. Here's a setup that would calculate the center of mass for each face:

That setup's from user LordoftheFleas on BlenderArtists. Here's a slightly simplified, updated version courtesy of @Robin-Betts:

Here's the result:

I'm attaching the blend file so instead of re-creating the setup from scratch you can just append the node group "FaceCenterOfMass" and link it to your Mesh to Points node as you see in the screenshot.

Original version:

Updated version (Blender 3.3):

• Brilliant. When you've got a moment. can you give a brief description of the math you're doing? Having trouble reverse-engineering. Nov 19 at 19:54
• @RobinBetts I'm nowhere near smart enough to do that myself haha. Got it from who knows somewhere sometime ago and been using it as an asset. Let me see if I can find the original again. Nov 19 at 20:00
• @RobinBetts Found it: blenderartists.org/t/… If you can put it into words, I'm all ears. I can kinda sorta follow it, but get lost at the Accumulate Field node. Nov 19 at 20:05
• Okydoky.. Rebuilt for 3.3 with a slightly different transfer method (I prefer by index, if possible, so as to avoid a search though geometry for 'closest') . The group finds the center of mass of a face by finding the mean location of the centers of mass of its constituent triangles, weighted by triangle-area. I /you can pop in an edit, if you like. Just IMO, but I think it might be diplomatic to credit the original author of this one. There's no shame in that :) Well found, and nicely presented! Nov 20 at 10:17
• Thanks Robin, updated the answer. Nov 20 at 13:34