Author Topic: vlax-curve-getfurthestpointfrom  (Read 28941 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
vlax-curve-getfurthestpointfrom
« on: July 12, 2012, 12:58:59 PM »
Hi all, I am pretty stucked here... I've tried to write missing vlax-curve-function, but keep getting error :

Code: [Select]
(setq e (car (entsel))) => picked curve - spline
(setq p (getpoint)) => picked point

Command: (vlax-curve-getfurthestpointto e p)
Hard error occurred ***
internal stack limit reached (simulated)

Here is my function :

Code - Auto/Visual Lisp: [Select]
  1. (defun vlax-curve-getfurthestpointto ( ent pt / f )
  2.   (defun f ( ps pe e p / stpar enpar par1 par2 p1 p2 p3 p4 ptlst ptlstsort )
  3.   (if (null stpar) (setq stpar (vlax-curve-getstartparam e)) (setq stpar (vlax-curve-getparamatpoint e ps)))
  4.   (if (null enpar) (setq enpar (vlax-curve-getendparam e)) (setq enpar (vlax-curve-getparamatpoint e pe)))
  5.   (setq par1 (+ stpar (* (- enpar stpar) (/ 1.0 3.0))))
  6.   (setq par2 (+ stpar (* (- enpar stpar) (/ 2.0 3.0))))
  7.   (setq ptlst (list p1 p2 p3 p4))
  8.   (setq ptlstsort (vl-sort ptlst '(lambda ( a b ) (> (distance p a) (distance p b)))))
  9.   (if (= (length ptlstsort) 4)
  10.     (if (< (vlax-curve-getparamatpoint e (car ptlstsort)) (vlax-curve-getparamatpoint e (cadr ptlstsort)))
  11.       (f (car ptlstsort) (cadr ptlstsort) e p)
  12.       (f (cadr ptlstsort) (car ptlstsort) e p)
  13.     )
  14.     (car ptlstsort)
  15.   )
  16.   )
  17.   (f nil nil ent pt)
  18. )
  19.  

Can someone help?

M.R.
« Last Edit: July 13, 2012, 01:11:57 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BlackBox

  • King Gator
  • Posts: 3770
Re: vlax-curve-getfurthestpointto
« Reply #1 on: July 12, 2012, 02:01:51 PM »
Marco,

Perhaps I am not understanding what you are trying to do, but wouldn't the 'furthest point from' be either the start or end point (or both if selecting the midpoint)?  :?
« Last Edit: July 13, 2012, 08:44:05 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

ronjonp

  • Needs a day job
  • Posts: 7527
Re: vlax-curve-getfurthestpointto
« Reply #2 on: July 12, 2012, 02:29:50 PM »
Not necessarily...

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: vlax-curve-getfurthestpointto
« Reply #3 on: July 12, 2012, 02:36:52 PM »
I want that algorithm search for the point it's easiest to find...

@Render - I suppose you thought on line as curve or circle and mid or center for point...
Then, like (vlax-curve-getclosestpointto) function retrieves point on circle when picked center as reference point, I want to make (vlax-curve-getfurthestpointto) function...

@Ron - I agree, you can get one solution witch I am searching for...

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BlackBox

  • King Gator
  • Posts: 3770
Re: vlax-curve-getfurthestpointto
« Reply #4 on: July 12, 2012, 02:40:21 PM »
I'm still not understanding, so I'll just step back and observe for a bit. All of the smart talk is not computing for me today... Good luck, Marco.
"How we think determines what we do, and what we do determines what we get."

zoltan

  • Guest
Re: vlax-curve-getfurthestpointto
« Reply #5 on: July 12, 2012, 02:44:32 PM »
Shouldn't it be furthest point FROM?  :-D

I think this is going to recurse forever because (length ptlstsort) is always going to be 4.

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: vlax-curve-getfurthestpointto
« Reply #6 on: July 12, 2012, 02:54:50 PM »
Function name isn't so important I thought TO - like closest point to...

My function uses vl-sort function witch should remove duplicates when reaching solution point, so (length ptlstsort) shouldn't be always 4 - that was the main point of my code... But now I see that recursion isn't so good approach as there are cases when length should be 4 and already met opportunities for solution : line with mid or circle, ellipse with center...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BlackBox

  • King Gator
  • Posts: 3770
Re: vlax-curve-getfurthestpointto
« Reply #7 on: July 12, 2012, 02:55:14 PM »
Not necessarily...

Just saw this, thanks ronjon.  :wink:
"How we think determines what we do, and what we do determines what we get."

zoltan

  • Guest
Re: vlax-curve-getfurthestpointto
« Reply #8 on: July 12, 2012, 03:14:43 PM »
Function name isn't so important I thought TO - like closest point to...

My function uses vl-sort function witch should remove duplicates when reaching solution point, so (length ptlstsort) shouldn't be always 4 - that was the main point of my code... But now I see that recursion isn't so good approach as there are cases when length should be 4 and already met opportunities for solution : line with mid or circle, ellipse with center...

Oh yea.. for some reason I thought VL-Sort would always return a list of the same length.

If I am understanding this correctly, you are dividing the curve into 4 points, the start point, 1/3, 2/3 and the end point.  Then sorting this list by the descending distance from the test point and recursing the function between the furthest point and the second furthest point in a divide-and-concur type approach.

It might take a very long time before the function reaches a local minimum and two distances in the list will be the same because the real number that represents the parameter at the point can be very accurate.  You may need to see if the two distances parameters differ by some tolerance.

The other problem is when the actual furthest point is not between the first and second test points. See attached case:
« Last Edit: July 12, 2012, 03:20:50 PM by zoltan »

zoltan

  • Guest
Re: vlax-curve-getfurthestpointto
« Reply #9 on: July 12, 2012, 05:00:13 PM »
Code - Auto/Visual Lisp: [Select]
  1. (Defun ZF-GetFurthestPointFrom (CURVE POINT / FurthestParameter )
  2.  (Defun FurthestParameter (SP EP TOL / params dist )
  3.   (SetQ params (List SP
  4.                      (+ SP (* (- EP SP) (/ 1.0 3.0)) )
  5.                      (+ SP (* (- EP SP) (/ 2.0 3.0)) )
  6.                      EP
  7.                )
  8.   )
  9.  
  10.   (SetQ dist (Vl-Sort-I params
  11.                         (Function
  12.                          (Lambda (a b)
  13.                           (> (Distance POINT (VLAX-Curve-GetPointAtParam CURVE a))
  14.                              (Distance POINT (VLAX-Curve-GetPointAtParam CURVE b))
  15.                           )
  16.                          )
  17.                         )
  18.              )
  19.   )
  20.  
  21.   (If (> (- EP SP) TOL)
  22.    (FurthestParameter
  23.     (If (= (Car dist) 0)
  24.      (Nth (Car dist) params )
  25.      (Nth (1- (Car dist)) params )
  26.     )
  27.     (If (= (Car dist) (1- (Length params)))
  28.      (Nth (Car dist) params )
  29.      (Nth (1+ (Car dist)) params )
  30.     )
  31.     TOL
  32.    )
  33.    (Nth (Car dist) params)
  34.   )
  35.  )
  36.  
  37.  (VLAX-Curve-GetPointAtParam CURVE (FurthestParameter (VLAX-Curve-GetStartParam CURVE) (VLAX-Curve-GetEndParam CURVE) 0.0001) )
  38. )
  39.  

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: vlax-curve-getfurthestpointto
« Reply #10 on: July 12, 2012, 05:11:39 PM »
Thanks, Zoltan... It works very well...

I thought ab tolerance and you were faster and I failed to complete my code... Yours is fine...

M.R. :wink:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

zoltan

  • Guest
Re: vlax-curve-getfurthestpointto
« Reply #11 on: July 12, 2012, 05:30:08 PM »
I'm just happy I still remember how to write Lisp!  :lmao:

Minor (anal) revision:
Code - Auto/Visual Lisp: [Select]
  1. (Defun ZF-GetFurthestPointFrom (CURVE POINT / FurthestParameter epsilon )
  2.  (SetQ epsilon 0.0001 )
  3.  (Defun FurthestParameter (SP EP / params furthest )
  4.   (SetQ params (List SP
  5.                      (+ SP (* (- EP SP) (/ 1.0 3.0)) )
  6.                      (+ SP (* (- EP SP) (/ 2.0 3.0)) )
  7.                      EP
  8.                )
  9.   )
  10.  
  11.   (SetQ furthest (Car (Vl-Sort-I params
  12.                                  (Function
  13.                                   (Lambda (a b)
  14.                                    (> (Distance POINT (VLAX-Curve-GetPointAtParam CURVE a))
  15.                                       (Distance POINT (VLAX-Curve-GetPointAtParam CURVE b))
  16.                                    )
  17.                                   )
  18.                                  )
  19.                       )
  20.                  )
  21.   )
  22.  
  23.   (If (> (- EP SP) epsilon)
  24.    (FurthestParameter
  25.     (If (= furthest 0)
  26.      (Nth furthest params )
  27.      (Nth (1- furthest) params )
  28.     )
  29.     (If (= furthest (1- (Length params)))
  30.      (Nth furthest params )
  31.      (Nth (1+ furthest) params )
  32.     )
  33.    )
  34.    (Nth furthest params)
  35.   )
  36.  )
  37.  
  38.  (VLAX-Curve-GetPointAtParam CURVE (FurthestParameter (VLAX-Curve-GetStartParam CURVE) (VLAX-Curve-GetEndParam CURVE)) )
  39. )
  40.  

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: vlax-curve-getfurthestpointto
« Reply #12 on: July 12, 2012, 05:43:06 PM »
The process could be more efficient for some entities for which the extrema must lie on the vertices:

Code - Auto/Visual Lisp: [Select]
  1. (defun vlax-curve-getfurthestpointfrom ( ent pt / elst etype )
  2.     (setq elst (entget ent))
  3.     (cond
  4.         (   (= "XLINE" (setq etype (cdr (assoc 0 elst))))
  5.             nil
  6.         )
  7.         (   (= "LINE" etype)
  8.             (if (< (distance pt (cdr (assoc 10 elst)))
  9.                    (distance pt (cdr (assoc 11 elst)))
  10.                 )
  11.                 (cdr (assoc 11 elst))
  12.                 (cdr (assoc 10 elst))
  13.             )
  14.         )
  15.         (   (and
  16.                 (= "LWPOLYLINE" etype)
  17.                 (vl-every '(lambda ( x ) (or (/= 42 (car x)) (zerop (cdr x)))) elst)
  18.             )
  19.             (car
  20.                 (vl-sort (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) elst))
  21.                     (function
  22.                         (lambda ( a b ) (> (distance pt a) (distance pt b)))
  23.                     )
  24.                 )
  25.             )
  26.         )
  27.         (   t
  28.             <other entities>
  29.         )
  30.     )
  31. )

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: vlax-curve-getfurthestpointfrom
« Reply #13 on: July 13, 2012, 01:15:52 AM »
Thank you Lee very much... Only skipped "RAY", so my final function with acceptable tolerance for almost if not exact result is 1e-8...

Code - Auto/Visual Lisp: [Select]
  1. (Defun ZF-GetFurthestPointFrom (CURVE POINT TOL / FurthestParameter )
  2. (Defun FurthestParameter (SP EP TOL / params dist )
  3.  (SetQ params (List SP
  4.                     (+ SP (* (- EP SP) (/ 1.0 3.0)) )
  5.                     (+ SP (* (- EP SP) (/ 2.0 3.0)) )
  6.                     EP
  7.               )
  8.  )
  9.  
  10.  (SetQ dist (Vl-Sort-I params
  11.                        (Function
  12.                         (Lambda (a b)
  13.                          (>= (Distance POINT (VLAX-Curve-GetPointAtParam CURVE a))
  14.                              (Distance POINT (VLAX-Curve-GetPointAtParam CURVE b))
  15.                          )
  16.                         )
  17.                        )
  18.             )
  19.  )
  20.  
  21.  (If (> (- EP SP) TOL)
  22.   (FurthestParameter
  23.    (If (< (Nth (Car dist) params) (Nth (Cadr dist) params))
  24.     (Nth (Car dist) params)
  25.     (If (not (equal (VLAX-Curve-GetPointAtParam CURVE (Nth (Car dist) params)) (VLAX-Curve-GetPointAtParam CURVE (Nth (Cadr dist) params)) 1e-8))
  26.      (Nth (Cadr dist) params)
  27.      (Nth (Caddr dist) params)
  28.     )
  29.    )
  30.    (If (< (Nth (Car dist) params) (Nth (Cadr dist) params))
  31.     (If (not (equal (VLAX-Curve-GetPointAtParam CURVE (Nth (Car dist) params)) (VLAX-Curve-GetPointAtParam CURVE (Nth (Cadr dist) params)) 1e-8))
  32.      (Nth (Cadr dist) params)
  33.      (Nth (Caddr dist) params)
  34.     )
  35.     (Nth (Car dist) params)
  36.    )
  37.    TOL
  38.   )
  39.   (Nth (Car dist) params)
  40.  )
  41. )
  42.  
  43. (VLAX-Curve-GetPointAtParam CURVE (FurthestParameter (VLAX-Curve-GetStartParam CURVE) (VLAX-Curve-GetEndParam CURVE) TOL) )
  44. )
  45.  
  46. (defun vlax-curve-getfurthestpointfrom ( ent pt / elst etype )
  47.        (setq elst (entget ent))
  48.        (cond
  49.            (   (= "XLINE" (setq etype (cdr (assoc 0 elst))))
  50.                nil
  51.            )
  52.            (   (= "RAY" (setq etype (cdr (assoc 0 elst))))
  53.                nil
  54.            )
  55.            (   (= "LINE" etype)
  56.                (if (< (distance pt (cdr (assoc 10 elst)))
  57.                       (distance pt (cdr (assoc 11 elst)))
  58.                    )
  59.                    (cdr (assoc 11 elst))
  60.                    (cdr (assoc 10 elst))
  61.                )
  62.            )
  63.            (   (and
  64.                    (= "LWPOLYLINE" etype)
  65.                    (vl-every '(lambda ( x ) (or (/= 42 (car x)) (zerop (cdr x)))) elst)
  66.                )
  67.                (car
  68.                    (vl-sort (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) elst))
  69.                        (function
  70.                            (lambda ( a b ) (> (distance pt a) (distance pt b)))
  71.                        )
  72.                    )
  73.                )
  74.            )
  75.            (   t
  76.                (ZF-GetFurthestPointFrom ent pt 1e-8)
  77.            )
  78.        )
  79. )
  80.  

P.S. You must use (vl-load-com) before lake every other vlax-curve-function...

M.R. :-)
« Last Edit: July 13, 2012, 08:20:11 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: vlax-curve-getfurthestpointfrom
« Reply #14 on: July 13, 2012, 01:26:26 AM »

just to be pedantic ...
The vla-, vlax- naming prefix is recognised as the naming convention used by Visual Lisp library functions.

I'd prefer to see a different prefix if the code was to be used on one of my machines.


Regards
kdub.



ps:
probably best not to use kdub:xxxx or kdub-xxxx either   :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.