# How to get the center of mass of solids

I got a 3Dsolid formed by subtraction of geometries through 'RegionDifference'.

My intention now is to get the center of mass of the solid.

This is the geometry that received the subtraction:

(*Corpo Principal*)
orig = {0, 0, 0};
diam1 = 50;
r1 = diam1/2;
comp = 200;
corpoPrincipal = Cylinder[{orig, {comp, 0, 0}}, r1];


This is one of the geometries that were removed:

(*Furo*)
diam2 = 15;
r2 = diam2/2;
furo = Cylinder[{{altRasgo/2, 0, -r1}, {altRasgo/2, 0, r1}}, r2];


This is the other geometry that were removed:

(*Rasgo*)
altRasgo = 30;
largRasgo = 15;
rasgo = Cuboid[{0, -r1, -7.5}, {30, r1, 7.5}];


This was the operation to generate the solid:

reg = {corpoPrincipal, furo, rasgo};
rr = RegionDifference[RegionDifference[reg[], reg[]], reg[]];
RegionPlot3D[rr, PlotPoints -> 100]


Now that comes my question:   I followed the concept above to obtain the center of mass. Then I created the code below:

(*Densidade*)
ρ = 0.0079(*g/mm^3*);

(*CG*)
cgCorpoPrincipal = RegionCentroid[corpoPrincipal];
cgFuro = RegionCentroid[furo];
cgRasgo = RegionCentroid[rasgo];
RegionCentroid[corpoPrincipal];

(*Massa do Corpo Principal*)
mCorpoPrincipal = ρ*π*r1^2*comp // N;

(*Massa do Rasgo*)
mRasgo = ρ*altRasgo*largRasgo*diam1 // N;

(*Massa do Furo*)
mFuro = ρ*π*r2^2*diam1 // N;

(*CG Global*)
xCGglobal = (mCorpoPrincipal*cgCorpoPrincipal[] + mRasgo*cgFuro[] + mFuro*cgRasgo[])/(mCorpoPrincipal + mRasgo + mFuro)
yCGglobal = (mCorpoPrincipal*cgCorpoPrincipal[] + mRasgo*cgFuro[] + mFuro*cgRasgo[])/(mCorpoPrincipal + mRasgo + mFuro)
zCGglobal = (mCorpoPrincipal*cgCorpoPrincipal[] + mRasgo*cgFuro[] + mFuro*cgRasgo[])/(mCorpoPrincipal + mRasgo + mFuro)


I am considering the solids in the unit Length: $mm$

And the density applied was: $0.0079 g/mm^3$

x=93.7186 y=0. z=0.

I noticed a flaw in my conception, because I compared with the results that I have had in other software that I work very well (SolidWorks).

Through the SolidWorks software I got the following result:

x=106.59 y=0.00 z=0.00 I realized that I cannot take into account the total mass of each subtracted solid. I have to get a mass that corresponds with the INTERSECTION OF SOLIDS.

Watching the animation below it is easy to see what I am saying... Finally, how can I get the correct results?

region1 = (comp > x > 0 && y^2 + z^2 < r1^2);
region2 = (((x - altRasgo/2)^2 + y^2) < r2^2 && r1 > z > -r1);
region3 = (0 < x < 30 && -r1 < y < r1 && -7.5 < z < 7.5);
region = region1 && ! region2 && ! region3;

r = DiscretizeRegion[
ImplicitRegion[
region, {x, y, z}], {{0, comp}, {-comp/2, comp/2}, {-comp/2,
comp/2}}, Method -> "RegionPlot3D", MaxCellMeasure -> 10];

p = RegionCentroid[r]


{106.663, 0.0000254679, 0.0000169948}

Linex = {Line[{p - {150, 0, 0}, p + {150, 0, 0}}]};
Liney = {Line[{p - {0, 150, 0}, p + {0, 150, 0}}]};
Linez = {Line[{p - {0, 0, 150}, p + {0, 0, 150}}]};
rr = GraphicsComplex[MeshCoordinates@#, MeshCells[#, 2]] &@r;
r2 = Graphics3D[{EdgeForm[], Darker[Gray], rr}, Lighting -> "Neutral",
Boxed -> False]
Show[r2, Graphics3D[{Red, Linex, Liney, Linez}], ImageSize -> Large] • I will give my vote to you for helping me again. Already I offered my other vote for our colleague. :) Sep 2, 2016 at 15:23

Simply res=RegionCentroid[rr]

{106.587, -9.00492*10^-9, 7.5693*10^-18}

If one does

Chop[res]


{106.587, -9.00492*10^-9, 0}

so

1. Chop[res, 10^-8]

{106.587, 0, 0}

2. Round[res, 0.001]

{106.587, 0, 0}

• In its reply where I could add Chop to round the values? Sep 2, 2016 at 15:12
• thank you. I offer my vote to the other colleague to balance. :) Sep 2, 2016 at 15:27