Missing Link: Centroids

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Missing Link: Centroids

Post by CVH » Fri Oct 29, 2021 11:57 am

Hi all,

A missing link for some is the center of mass or the centroid of a uniform density area. :idea:

*** EDIT ***
For the latest version v0.32 refer to:
https://qcad.org/rsforum/viewtopic.php?f=30&t=8970
*** EDIT ***


This PRO only addon provides in these under Misc .. Examples .. Math Examples
for most and single selected common drawing entities including for the hatched area.

Summed or differential areas are possible when using a hatch. 8)

Centroid-examples.dxf
What it can or can not
(225.86 KiB) Downloaded 441 times

The addon is donated to the QCAD project as is, Andrew may include it in a next release.

Immediate install: :arrow:
Ensure that the next path exists in your installation folder: (Win users may require admin rights)
... QCAD\scripts\Misc\Examples\MathExamples\InfoAreaCentroid\ ... (QCAD or QCADCAM or whatever path is used)
Copy over the files from the zip to that path.
The folder structure is mandatory, the addon should be located here:
... QCAD\scripts\Misc\Examples\MathExamples\InfoAreaCentroid\InfoAreaCentroid.js

*** EDIT ***
Updated to v0.32
Version 0.10 was downloaded 8 times.
Zip 0.11 was downloaded 13 times prior the v0.31 update.
Zip 0.11 was downloaded 14 times extra prior the update.
*** EDIT ***
InfoAreaCentroid 0.11.zip
Beta version 0.11
(40.18 KiB) Downloaded 450 times
Minor differences may occur ... The location in the plane is one reason :!:
The addon doesn't interpolate curved shapes with line segments, instead it uses polygons and arc segments.
Approximations with arc segments rely on the Explode (XP) preferences.
Font and size on drawing preferences, color on info color preferences.
Can't guarantee how correct shapes from boundaries are merged. :?

Not really happy with the marker itself nor its proportional size. :oops:
Several issues needed to be circumvented. :oops:

For the scripters:
There are various methods included for area and also for the centroid as a uniform wire.
NOT a complete work. :wink:

Regards,
CVH
Last edited by CVH on Tue Dec 14, 2021 8:04 am, edited 10 times in total.

guyrinf
Newbie Member
Posts: 7
Joined: Wed Apr 16, 2014 12:51 pm

Re: Missing Link: Centroids

Post by guyrinf » Fri Oct 29, 2021 1:00 pm

I bought Qcad to use it as an aid in building small sailboats. When I change the shape of the hull and sail then I often calculate the center of resistance of a hull and the center of pressure of a sail.

This tool is wonderful for me, I will no longer need to use others softwares to find the center of gravity.

Thank you, Mr. CVH. I really appreciate.

Guy

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4935
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Missing Link: Centroids

Post by Husky » Fri Oct 29, 2021 9:44 pm

CVH wrote:
Fri Oct 29, 2021 11:57 am
Summed or differential areas are possible when using a hatch. 8)
Interesting! But what are the requirements to create a functional hatch to get a differential area calculated?

I get only warnings and no result ...

InfoAreaCentroid.gif
InfoAreaCentroid.gif (1.51 MiB) Viewed 12061 times

I'm missing something ...? :oops:
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: Missing Link: Centroids

Post by CVH » Sat Oct 30, 2021 5:15 am

Husky wrote:
Fri Oct 29, 2021 9:44 pm
I'm missing something ...?
Thanks for the report Husky. :wink:
Could not replicate it at first ... One of the examples has circular voids ...

That part of the script was intentionally:

Code: Select all

            // Diversify on RShape from boundary loop:
...
..
.            
            else {
                // # ToDo # Possible incomplete
                EAction.handleUserWarning(qsTr("Unhandled shape from hatch loop."));
                debugger;    // ### Catch this In the act !!! ###
            }
Finally found it ... Circles as boundary are initially circles until you save the file.
And in debugger mode I have to rely on saved information, can't really draw/create much then.
When saved, circles become full arcs with a bulge of 1.66312e+16, an issue on its own that was handled.

Should be fixed. :wink:
I have the habit to code reporting every issue on the Command History.
I see that you use dialogs as warnings by preference ...
11+1 Issues ... 11+1 dialogs.
Is that something I should circumvent (turn off) or are you happy with that?
On the other hand those 11 Issues won't occur anymore ...


*** EDIT ***
Updated to v0.31 refer to:
viewtopic.php?f=30&t=8901
Version 0.11 was dowloaded 21 times.
*** EDIT ***

I have updated the script file in the zip container ...
Please replace the installed script file itself with this one:
InfoAreaCentroid.js
Beta version 0.11
(75.79 KiB) Downloaded 576 times

Regards,
CVH
Last edited by CVH on Sat Nov 20, 2021 4:10 pm, edited 4 times in total.

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4935
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Missing Link: Centroids

Post by Husky » Sat Oct 30, 2021 8:14 am

CVH wrote:
Sat Oct 30, 2021 5:15 am
Should be fixed. :wink:
Yes - it works now nicly. This will be a useful tool for me. Thank you!


Thinking about this tool ...

... I'm wondering why the calculation brings up a 6000000.0000000024 result. I used for testing purposes only whole Numbers for areas and the result should be a whole number of 6000000 - where is the .0000000024 coming from?

... I'm also wondering why the Annotation Text hight (based on the DIM Text hight value) is only used from the last saving status. Changes which should apply for new calculation are ignored until the dxf is saved again ...

Info Area Centroid circle test.dxf
(112.35 KiB) Downloaded 415 times
CVH wrote:
Sat Oct 30, 2021 5:15 am
I see that you use dialogs as warnings by preference ...
11+1 Issues ... 11+1 dialogs.
Is that something I should circumvent (turn off) or are you happy with that?
11+1 sucks but I like to have dialog warnings. Is like a hint with a fence post ... However - 1 uppopping dialog would do it too but I guess that will be more difficult to code ... !?
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: Missing Link: Centroids

Post by CVH » Sat Oct 30, 2021 10:32 am

Husky wrote:
Sat Oct 30, 2021 8:14 am
... I'm wondering why the calculation brings up a 6000000.0000000024 result.
Husky, for the moment the tool does no fancy formatting, it uses full floating point.
The results displayed are those with 17 significant digits.

To begin with, your major circle has an area of 8000000.0000000030 squared units. :shock:
One thing I am sure about. :lol:
That is the floating point result of R*R*Pi with both R & Pi in floating point. e_geek
For an area = 8000k the radius needs to be 1595.7691216057307117597842397375....
In the best case that is stored as 1595.769121605731. (Except it isn't :roll: )

The uncertainty or 1ULP of 8000k or 6000k is 9.313e-10, it can't get any more certain/correct.
Meaning that the main circle area is within 4 ULPs.
The differential area is the sum of 11 floating point values.
There the result is within 3 ULPs, and considering things (Boundary poly of two arcs ...) that is rather good. :P

I am more concerned about the area and centroid of ellipses and splines.
All integration rely on the location of vertices in the plane and those are less certain the further they are from the origin.
Results of such may even be different as CW or as CCW. Reversing = Maths.
Husky wrote:
Sat Oct 30, 2021 8:14 am
... I'm also wondering why the Annotation Text hight (based on the DIM Text hight value) is only used from the last saving status. Changes which should apply for new calculation are ignored until the dxf is saved again ...
As far as I understand it correctly: Document DIM text height doesn't change with new drawing settings until reloaded.
Looking into that ... It simply queries the document settings each time ... :?
Husky wrote:
Sat Oct 30, 2021 8:14 am
However - 1 uppopping dialog would do it too but I guess that will be more difficult to code ... !?
With the circular boundary bug fixed one should only get 'No Results' when there is no valid centroid data returned to display.
Catched exceptions, if any, will cast their own 'milestone' error. :wink:

Regards,
CVH

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: Missing Link: Centroids

Post by petevick » Sat Oct 30, 2021 11:26 am

For anyone who would like the menu item for this fantastic tool in an easier to get at location, I now have it in the Info menu and all Info toolbars. To do this edit the penultimate line (line number 2144) in the InfoAreaCentroid.js file from......

Code: Select all

action.setWidgetNames(["MathExamplesMenu"]);
to......

Code: Select all

action.setWidgetNames(["InformationMenu", "InformationToolBar", "InformationToolsPanel", "InformationMatrixPanel"]);
and reload Qcad.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: Missing Link: Centroids

Post by petevick » Sat Oct 30, 2021 3:28 pm

with reference to my above post, you should also change line 2141 from

Code: Select all

action.setGroupSortOrder(79700);
to

Code: Select all

action.setGroupSortOrder(54100);
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4935
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Missing Link: Centroids

Post by Husky » Sat Oct 30, 2021 7:40 pm

@CVH
CVH wrote:
Sat Oct 30, 2021 10:32 am
Husky wrote:
Sat Oct 30, 2021 8:14 am
... I'm wondering why the calculation brings up a 6000000.0000000024 result.
Husky, for the moment the tool does no fancy formatting ....
Ok, then it is what it is. Nevertheless - a nice tool!




@petevick
petevick wrote:
Sat Oct 30, 2021 11:26 am
For anyone who would like the menu item for this fantastic tool in an easier to get at location, I now have it in the Info menu and all Info toolbars.
Beautiful!!! That's for me the icing on the cake. Thank you!

Husky-2021.10.30-01.png
Husky-2021.10.30-01.png (6.18 KiB) Viewed 11945 times
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

zilmosantos
Active Member
Posts: 28
Joined: Fri Aug 13, 2021 9:55 am

Re: Missing Link: Centroids

Post by zilmosantos » Thu Nov 04, 2021 2:37 am

Hi, CHV and friends! On my tests of the 1.1 version, I'm only getting results on splines... the command freezes when I try any other shape.

Is this happening with you?

I also tried with the Centroid-examples.dxf file and it's only working with splines shapes. Polylines are not working.
Attachments
INFOAREACENTROIDS - TEST ZILMO 11-03-21.dxf
(100.84 KiB) Downloaded 432 times

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: Missing Link: Centroids

Post by CVH » Thu Nov 04, 2021 3:23 am

Hi Zilmo.

Freezing usually means that the script encounters an uncaught error ... Reset (QQ) will fix that.

Weird there the spline area centroid relies on an approximation as polyline with arc segments.
And that relies on the weighted sum of the base polygon centroid and all the circle segments centroids.
So, the most frequented part of the code seems to be functional.

Is there a warning (in red) issued on the Command History?
Remember that the area centroid method requires one single selection. :wink:
For a multi selection there is the overlapping issue what is not the case for valid hatched areas.
Chaining to one or more closed contour(s) is not implemented.

SingularSelection.png
SingularSelection.png (2.44 KiB) Viewed 11669 times

Regards,
CVH

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: Missing Link: Centroids

Post by CVH » Thu Nov 04, 2021 7:12 am

All,

Zilmo's problem is solved ... Kind of.
InfoAreaCentroid.js exploits the tolerance for RPolyline.getSelfIntersectionPoints().

That patch was issued on Jun 17, 2021
viewtopic.php?f=30&t=8526

Only valid for snapshots past that date.

Meanwhile version 0.11 is included for the next release.
Thank you Andrew.

Version 0.20 excludes self-intersecting polylines with line segments with 1e-6 tolerance, there 1e-9 won't even hold. :shock:
Excluding self-intersecting polylines with non zero bulge factors with 1e-3 tolerance or somewhat less than the connect tolerance. :cry:
So far it is by this connecting freedom that some results can be poor.
Advice: Trim your segments pair-wise.

Regards,
CVH

zilmosantos
Active Member
Posts: 28
Joined: Fri Aug 13, 2021 9:55 am

Re: Missing Link: Centroids

Post by zilmosantos » Thu Nov 04, 2021 11:24 pm

Using the current snapshot 3.26.4.9 solved the issue! Thanks for the AMAZING script!

John Hyslop
Premier Member
Posts: 473
Joined: Mon Sep 30, 2019 6:21 am
Location: Melbourne - Australia

Re: Missing Link: Centroids

Post by John Hyslop » Sat Nov 06, 2021 1:48 am

Hi CVH

Nice work and a very handy tool..

Thanks
Cheers
John
PS And being able to get the area of closed spline shapes and ellipses is a bonus.. :D
IF IT IS TO BE IT IS UP TO ME [ Ten most powerful 2 letter words ]

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”