Author Topic: lw-linear-centroid.lsp (by M.R.) - checking...  (Read 1073 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
lw-linear-centroid.lsp (by M.R.) - checking...
« on: June 05, 2022, 06:45:31 AM »
I am pretty sure I've implemented as much as possible to be the closest to desired...
Still, there are some unmatchings of the results...

Checking procedure :
Draw LWPOLYLINE;
Create small CIRCLE;
Perform SWEEP command on CIRCLE and LWPOLYLINE;
Check - centroid of 3DSOLID - (sweep)...

Testing the code :
Draw LWPOLYLINE;
Start routine;
Check - lin-cent (centroid) of LWPOLYLINE...

Desired : 2 checking points should match, but unfortunately this is not the case - still it's pretty close!!!

Please, review and post some comments or observations conclusion...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:lw-linear-centroid ( / mid arc-cen groupn dd dx dy mx my lw lwx data nextpair mp mpp cenl mode )
  2.  
  3.  
  4.   (defun mid ( p1 p2 )
  5.     (mapcar (function (lambda ( a b ) (/ (+ a b) 2.0))) p1 p2)
  6.   )
  7.  
  8.   (defun arc-cen ( pair nextpair data lw / bulge st en ang chord rad mp mpp cen centroid )
  9.     (setq bulge (cadr pair))
  10.     (setq st (car pair))
  11.     (setq en (car nextpair))
  12.     (setq ang (atan bulge)) ;; quarter central angle
  13.     (if (minusp ang)
  14.       (setq ang (+ ang pi))
  15.     )
  16.     (setq ang (* 2 ang)) ;; half central angle
  17.     (setq chord (distance st en))
  18.     (setq rad (/ chord (* 2 (abs (sin ang)))))
  19.     (setq ang (* 2 ang)) ;; full central angle
  20.     (setq mp (mid st en))
  21.     (setq mpp (mapcar (function +) (list 0.0 0.0) (trans (vlax-curve-getpointatparam lw (+ (vl-position pair data) 0.5)) 0 lw)))
  22.     (setq centroid (polar mpp (angle mpp mp) (* (distance mpp mp) (+ 0.25 (* 0.5 (- 1.0 (/ 2.0 pi))) (* (cos ang) (* 0.5 (- 0.5 (- 1.0 (/ 2.0 pi))))))))) ;; ang = (0,2pi)
  23.     (setq cen (polar mpp (angle mpp mp) rad))
  24.     (list cen centroid)
  25.   )
  26.  
  27.   (defun groupn ( lst n / q p plst ) ;; it groups as much as lst is divisable with n - rest is not grouped...
  28.     (setq q 0)
  29.     (repeat (/ (length lst) n)
  30.       (gc)
  31.       (setq p nil)
  32.       (repeat n
  33.         (setq p (append p (list (nth q lst))))
  34.         (setq q (1+ q))
  35.       )
  36.       (if p
  37.         (setq plst (append plst (list p)))
  38.       )
  39.     )
  40.     plst
  41.   )
  42.  
  43.   (setq dd 0.0 mx 0.0 my 0.0 mode 3)
  44.   (while (> mode 2)
  45.     (setq mode
  46.       (cond
  47.         ( (initget 6) )
  48.         ( (setq mode (getint "\nSpecify mode - linear (1) or quadratic (2) <1> : ")) )
  49.         ( t 1 )
  50.       )
  51.     )
  52.   )
  53.   (if (and (setq lw (car (entsel "\nPick LWPOLYLINE..."))) (and lw (= (cdr (assoc 0 (setq lwx (entget lw)))) "LWPOLYLINE")))
  54.     (progn
  55.       (setq data (mapcar (function cdr) (vl-remove-if-not (function (lambda ( x ) (vl-position (car x) (list 10 42)))) lwx)))
  56.       (setq data (groupn data 2))
  57.       (foreach pair data
  58.         (setq nextpair (cadr (member (assoc (car pair) data) data)))
  59.         (if (and (not nextpair) (= 1 (logand 1 (cdr (assoc 70 lwx)))))
  60.           (setq nextpair (car data))
  61.         )
  62.         (cond
  63.           ( (and (= (cadr pair) 0.0) nextpair)
  64.             (setq mp (mid (car pair) (car nextpair)))
  65.             (setq dx (* (distance (car pair) (car nextpair)) (abs (cos (angle (car pair) (car nextpair))))))
  66.             (setq dy (* (distance (car pair) (car nextpair)) (abs (sin (angle (car pair) (car nextpair))))))
  67.             (setq dd (+ dd (expt dx mode) (expt dy mode)))
  68.             (setq mx (+ mx (* (expt dx mode) (car mp)) (* (expt dy mode) (car mp))))
  69.             (setq my (+ my (* (expt dx mode) (cadr mp)) (* (expt dy mode) (cadr mp))))
  70.           )
  71.           ( (and (/= (cadr pair) 0.0) nextpair)
  72.             (setq cenl (arc-cen pair nextpair data lw))
  73.             (setq mp (mid (car pair) (car nextpair)))
  74.             (setq mpp (mapcar (function +) (list 0.0 0.0) (trans (vlax-curve-getpointatparam lw (+ (vl-position pair data) 0.5)) 0 lw)))
  75.             (setq dx (+ (* (distance mp mpp) (abs (cos (angle mp mpp)))) (* 2 (distance (car cenl) mpp) (abs (cos (+ (angle mp mpp) (/ pi 2)))))))
  76.             (setq dy (+ (* (distance mp mpp) (abs (sin (angle mp mpp)))) (* 2 (distance (car cenl) mpp) (abs (sin (+ (angle mp mpp) (/ pi 2)))))))
  77.             (setq dd (+ dd (expt dx mode) (expt dy mode)))
  78.             (setq mx (+ mx (* (expt dx mode) (car (cadr cenl))) (* (expt dy mode) (car (cadr cenl)))))
  79.             (setq my (+ my (* (expt dx mode) (cadr (cadr cenl))) (* (expt dy mode) (cadr (cadr cenl)))))
  80.           )
  81.         )
  82.       )
  83.       (setq lin-cent (trans (list (/ mx dd) (/ my dd) (cdr (assoc 38 lwx))) lw 0))
  84.       (entmake (list (cons 0 "POINT") (cons 10 lin-cent) (cons 62 64)))
  85.       (if (= mode 1) (prompt "\nLinear centroid (mode=1) of LWPOLYLINE is : ") (prompt "\nLinear centroid (mode=2) of LWPOLYLINE is : ")) (princ (strcat "(" (rtos (car lin-cent) 2 20) " " (rtos (cadr lin-cent) 2 20) " " (rtos (caddr lin-cent) 2 20) ")"))
  86.       (prompt "\nGlobal varible is stored as \"lin-cent\" - you can call it with !lin-cent")
  87.     )
  88.   )
  89.   (princ)
  90. )
  91.  

Regards, M.R.

P.S. In attachement is my testing DWG (if you need it...)...
« Last Edit: June 05, 2022, 12:40:06 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Crank

  • Water Moccasin
  • Posts: 1503
Re: lw-linear-centroid.lsp (by M.R.) - checking...
« Reply #1 on: June 05, 2022, 10:33:21 AM »
Marko,

1) Have you considered using the OSNAP GCE ? (I think there must be some error, because your result ( !lin-cent ) doesn't match up with the OSNAP).

BTW: Before we got this OSNAP, I created a REGION of the LWPOLY and then used MASSPROP to find the Centroid.

2) There will always be a difference, unless the LWpoly is symmetric. Otherwise the sweep will have more (or less) volume in the corners.
To proof this: Compair the results of the REGION and the SWEEP with the MASSPROP command.
Vault Professional 2023     +     AEC Collection

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: lw-linear-centroid.lsp (by M.R.) - checking...
« Reply #2 on: June 05, 2022, 12:39:12 PM »
Marko,

1) Have you considered using the OSNAP GCE ? (I think there must be some error, because your result ( !lin-cent ) doesn't match up with the OSNAP).

BTW: Before we got this OSNAP, I created a REGION of the LWPOLY and then used MASSPROP to find the Centroid.

2) There will always be a difference, unless the LWpoly is symmetric. Otherwise the sweep will have more (or less) volume in the corners.
To proof this: Compair the results of the REGION and the SWEEP with the MASSPROP command.

I think you don't quite understand the topic... This is NOT the same as ordinary centroid - this is linear version - this is why sweep and not region way...

BTW. I've updated the code - now it's better - little changed (arc) sub formula part (setq centroid ...)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Crank

  • Water Moccasin
  • Posts: 1503
Re: lw-linear-centroid.lsp (by M.R.) - checking...
« Reply #3 on: June 06, 2022, 03:44:14 AM »
You're right about that. :embarrassed2: I've no idea what a linear centroid is, nor what it's used for.  :-D

Vault Professional 2023     +     AEC Collection

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: lw-linear-centroid.lsp (by M.R.) - checking...
« Reply #4 on: June 08, 2022, 08:02:45 AM »
@Crank, ...

It's always easy to laugh ab things you/we don't quite know especially if it's about field of unknown and undiscovered yet...

Have you wandered what would one of choosen (by God) scientists say if he/she was asked to explain to person not really very thoughtful and pretty familiar with things connected to relligy and humanity, how should he/she act if things are crucial for choosing between knowing and beeing too much capable of understanding that discovery may benefit to bad outcome, or saying that explanation is not really needful and that he/she don't deserve too much attention to be in situation to speak with him/her in order to keep things more decent and secret?
Well, first of all... We are all choosen (at least those capeable to read and understand) to explain and to influence to situations that are serving to some kind of understanding... Weather things are bad or good, no one can really hestimate for sure... Knowing too much things may for sure gain powers that can produce/not produce desired harmonious reality in the world we now see and feel... For sure, someone born with gift certainly is not always gift of everyones eyes and thoughts... Anyway, morality can be and explained and expressed only if supposed to give correct jurisdiction to better suiting to universal integrity and compactivity in terms of existence and personal/plural wealth...
Is there just single atom/body/beeing/planet/star/galaxy/clouds/universum, probably we can feel it as single and to our understandings mutal... You/we see that I deliberately wrote cloud and added that (s) at the and and opposite universum without that (s) at the end... Shell we say it as one or more, for sure I can tell that we can be heared if we wrote, read and say loudly... Can someone from higher order do such things simple as hearing, or listening, or watching, or ... we can always say that as simple - trivial, or even more unthoughtful, unpleasantly rudely foolish and stupid... So, IMHO, that keeping in mind, politeness and respect given as a moral characteristic to all beeings of course is act of intelligence and higher order behaviness... Can somone throw a ball with super hit, or score in NBA with basket from planet ZOO right to the net of foolishness in order to gain more popularity and glory in act of heroism based on powers of God given opportuinities causing destructivity to other harmonious universums for sure IMO don't deserve any comment or expression neither through spirit nor fisical prospectivity or any kind of response from me as beeing willing to explain that acts have causal effect that are not thoughtfull and resposible... I, or no one, should do any kind of hierachical personal structuralism to say that he/she is more or less worthy person than any other... I was able to gain some material wealth through my carrer and everyone live is... That feeling that someone may die, or live in every millisecond not knowing that as a beeing haven't tried to produce goodness to some kind of universal knowledge for the time given no matter where and when he/she focused on recognition of kindness for sure can't upperlift any beeing too much to the sky or as always fortold the story of kingdom/queendom of heavan or harmony...

Now to the science :
Hint : look for 2 connections (diameter of arc / sagitta of arc) vs. (chord of arc / sagitta of arc)...

Can you throw out my scientific statement expressed through LISP and tell that I was wrong by trying to enlighten what was supposed to be very truelly implementation of centroid as point of gravity of dimensional positional reflectivity of matterry in system called origin composed coordinate system?

Can you tell that science don't serve to open eyes and make passing through hurdles of life easier and happier if some (so called "crazy" scientist) said that the story of life should be no pain on the neck and at least associate to some wealth...
(here I am not citating anyone especially "Doc Emmet Brown", or for ex. "Jor El" father of "Kal El" from planet as we all know K/Cripton... But of course that I'd predominantly wrote it as Kripton rather than American Cripton... I am from Serbia, you know...)

Correction :

Quote
...
            (setq dx (+ (* (distance mp mpp) (abs (cos (angle mp mpp)))) (* (distance (car pair) (car nextpair)) (abs (cos (+ (angle mp mpp) (/ pi 2)))))))
            (setq dy (+ (* (distance mp mpp) (abs (sin (angle mp mpp)))) (* (distance (car pair) (car nextpair)) (abs (sin (+ (angle mp mpp) (/ pi 2)))))))
...
« Last Edit: June 20, 2022, 05:45:43 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

kirby

  • Newt
  • Posts: 127
Re: lw-linear-centroid.lsp (by M.R.) - checking...
« Reply #5 on: June 28, 2022, 02:55:02 PM »
$0.02 from the geometer's peanuts gallery.

Calculation 1 - Polyline used as swept path for circular cross-section,  Centroid of 3D solid computed by Acad
Calculation 2 - Polyline centroid using above routine.

The two calculated centroids differ a little bit because of the arc segments of the 3d solid have more mass on the portion on the outside of the curve vs. the inside, making the centroid of the 3d solid model move outwards towards the outside of curves.

This is an issue with earthwork cut & fill calculations along roadway alignments, with volumes based on average end areas of cross-sections between stations (distances) along the alignment.   Sharp curves underestimate the volumes on the outside of the curve, and vice versa.