Author Topic: Random sphere point cloud - make it sphere of 3dfaces  (Read 2599 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Random sphere point cloud - make it sphere of 3dfaces
« on: March 12, 2013, 04:08:15 PM »
I had simple code for making 3dfaces between all points each point with each... If I use afterwards command region and command surfsculpt, I get desired result, but this method is too slow...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ptscloud-3dfaces ( / ss n pt ptlst )
  2.   (setq ss (ssget "_:L" '((0 . "POINT"))))
  3.   (repeat (setq n (sslength ss))
  4.     (setq pt (cdr (assoc 10 (entget (ssname ss (setq n (1- n)))))))
  5.     (setq ptlst (cons pt ptlst))
  6.   )
  7.   (foreach pt1 ptlst
  8.     (foreach pt2 (vl-remove pt1 ptlst)
  9.       (foreach pt3 (vl-remove pt2 (vl-remove pt1 ptlst))
  10.         (entmake (list '(0 . "3DFACE") (cons 10 pt1) (cons 11 pt1) (cons 12 pt2) (cons 13 pt3)))
  11.       )
  12.     )
  13.   )
  14.   (princ)
  15. )
  16.  
  17. (defun c:pts-3df nil (c:ptscloud-3dfaces))
  18.  
  19. (prompt "\nShortcut for c:ptscloud-3dfaces is c:pts-3df [Start with : Command: pts-3df]")
  20.  

So I decided to try to make just outer 3dfaces, but I am getting error on this code :

Code - Auto/Visual Lisp: [Select]
  1. (defun Clockwise-p ( p1 p2 p3 )
  2.   (< (sin (- (angle p1 p3) (angle p1 p2))) -1e-14)
  3. )
  4.  
  5. (defun pt-cir-3df ( p1 ptlst )
  6.   (setq p2 (cadr (vl-sort ptlst '(lambda ( a b ) (< (distance p1 a) (distance p1 b))))) pst p2)
  7.   (setq an2 (angle p1 p2))
  8.   (command "_.ucs" "z" (cvunit an2 "radians" "degrees"))
  9.   (setq p3 (cadr (vl-sort (vl-remove p1 ptlst) '(lambda ( a b ) (< (distance p2 a) (distance p2 b))))))
  10.   (setq cl (Clockwise-p p1 p2 p3))
  11.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  12.   (pt-cir-3dfs p1 (vl-remove p2 ptlst) cl)
  13. )
  14.  
  15. (defun p33 ( p3lst clock / p32 )
  16.   (setq p32 (cadr p3lst))
  17.   (if (eq (Clockwise-p p1 p2 p32) clock)
  18.     (setq p3 p32)
  19.     (p33 (cdr p3lst) clock)
  20.   )
  21. )
  22.  
  23. (defun pt-cir-3dfs ( p1 ptlst clock )
  24.   (setq p2 p3)
  25.   (setq an3 (angle (trans p1 0 1) (trans p3 0 1)))
  26.   (setq p3 (cadr (setq p3lst (vl-sort (vl-remove p1 ptlst) '(lambda ( a b ) (< (distance p2 a) (distance p2 b)))))))
  27.   (if (not (eq (Clockwise-p p1 p2 p3) clock)) (p33 (cdr p3lst) clock))
  28.   (setq an4 (angle (trans p1 0 1) (trans p3 0 1)))
  29.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  30.   (if clock (if (>= (abs (- an4 an3)) an4) (setq p2 p3 p3 pst)) (if (<= (abs (- an4 an3)) an4) (setq p2 p3 p3 pst)) )
  31.   (if (not (equal p3 pst 1e-8)) (pt-cir-3dfs p1 (vl-remove p2 ptlst) clock) (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3))))
  32. )
  33.  
  34. (defun c:ptscloud-sphere-3dfaces ( / ss n pt ptlst )
  35.   (setq ss (ssget "_:L" '((0 . "POINT"))))
  36.   (repeat (setq n (sslength ss))
  37.     (setq pt (cdr (assoc 10 (entget (ssname ss (setq n (1- n)))))))
  38.     (setq ptlst (cons pt ptlst))
  39.   )
  40.   (foreach pt1 ptlst
  41.     (command "_.ucs" "w")
  42.     (pt-cir-3df pt1 ptlst)
  43.   )
  44.   (command "_.ucs" "w")
  45.   (princ)
  46. )
  47.  
  48. (defun c:pts-sph-3df nil (c:ptscloud-sphere-3dfaces))
  49.  
  50. (prompt "\nShortcut for c:ptscloud-sphere-3dfaces is c:pts-sph-3df [Start with : Command: pts-sph-3df]")
  51.  

Error has to do something with recursion of p33 subfunction (stack limit reached)... So if you know how to solve this - perhaps I need completely new routine, I hope this will be your new challenge...

Attached dwg with random sphere points - 100 of them - I think it's enough, but I suggest that you don't try firstly described method for it's very hardware demanding...

Thanks, M.R.

EDIT : Last code little changed, but still no success... At least I solved recursion, now I get error :
; error: bad argument type: 2D/3D point: nil
« Last Edit: March 13, 2013, 03:52:22 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: Random sphere point cloud - make it sphere of 3dfaces
« Reply #1 on: March 13, 2013, 04:54:24 AM »
Now it won't break, but there are spaces left between 3faces; only smallest faces around points are computed... :-(
EDIT : It breaks again like with previous code...

Code - Auto/Visual Lisp: [Select]
  1. (defun Clockwise-p ( p1 p2 p3 )
  2.   (< (sin (- (angle p1 p3) (angle p1 p2))) -1e-14)
  3. )
  4.  
  5. (defun pt-cir-3df ( p1 ptlst )
  6.   (setq p2 (cadr (vl-sort ptlst '(lambda ( a b ) (< (distance p1 a) (distance p1 b))))) pst p2)
  7.   (setq an2 (angle p1 p2))
  8.   (setq p3 (cadr (vl-sort (vl-remove p1 ptlst) '(lambda ( a b ) (< (distance p2 a) (distance p2 b))))))
  9.   (command "_.ucs" "3p" p1 p2 p3)
  10.   (setq cl (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p3 0 1)))
  11.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  12.   (pt-cir-3dfs p1 (vl-remove p2 ptlst) cl)
  13. )
  14.  
  15. (defun p33 ( p3lst clock / p32 )
  16.   (setq p32 (cadr p3lst))
  17.   (if (eq (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p32 0 1)) clock)
  18.     (setq p3 p32)
  19.     (p33 (cdr p3lst) clock)
  20.   )
  21. )
  22.  
  23. (defun pt-cir-3dfs ( p1 ptlst clock )
  24.   (setq p2 p3)
  25.   (setq an3 (angle (trans p1 0 1) (trans p3 0 1)))
  26.   (setq p3 (cadr (setq p3lst (vl-sort (vl-remove p1 ptlst) '(lambda ( a b ) (< (distance p2 a) (distance p2 b)))))))
  27.   (if (not (eq (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p3 0 1)) clock)) (p33 (cdr p3lst) clock))
  28.   (setq an4 (angle (trans p1 0 1) (trans p3 0 1)))
  29.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  30.   (if (> (abs (- an4 an3)) pi) (setq p2 p3 p3 pst))
  31.   (if (not (equal p3 pst 1e-8)) (pt-cir-3dfs p1 (vl-remove p2 ptlst) clock) (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3))))
  32. )
  33.  
  34. (defun c:ptscloud-sphere-3dfaces ( / ss n pt ptlst )
  35.   (setq ss (ssget "_:L" '((0 . "POINT"))))
  36.   (repeat (setq n (sslength ss))
  37.     (setq pt (cdr (assoc 10 (entget (ssname ss (setq n (1- n)))))))
  38.     (setq ptlst (cons pt ptlst))
  39.   )
  40.   (foreach p ptlst
  41.     (command "_.ucs" "w")
  42.     (pt-cir-3df p ptlst)
  43.   )
  44.   (command "_.ucs" "w")
  45.   (princ)
  46. )
  47.  
  48. (defun c:pts-sph-3df nil (c:ptscloud-sphere-3dfaces))
  49.  
  50. (prompt "\nShortcut for c:ptscloud-sphere-3dfaces is c:pts-sph-3df [Start with : Command: pts-sph-3df]")
  51.  

M.R.
« Last Edit: March 13, 2013, 10:19:37 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Random sphere point cloud - make it sphere of 3dfaces
« Reply #2 on: March 13, 2013, 05:12:27 AM »
...
Code - Auto/Visual Lisp: [Select]
  1. (defun p33 ( p3lst clock / p32 )
  2.   (setq p32 (cadr p3lst))
  3.   (if (eq (Clockwise-p p1 p2 p32) clock)
  4.     (setq p3 p32)
  5.     (p33 (cdr p3lst) clock)
  6.   )
  7. )
  8.  
...
EDIT : Last code little changed, but still no success... At least I solved recursion, now I get error :
; error: bad argument type: 2D/3D point: nil
Note: This is a comment on your previous post and not on your last post.
You are using the function p33 recursively. After every recursion the p3lst list contains one less element. When the list contains 1 element (setq p32 (cadr p3lst)) returns nil which causes an error in the Clockwise function.
« Last Edit: March 13, 2013, 05:17:33 AM by roy_043 »

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: Random sphere point cloud - make it sphere of 3dfaces
« Reply #3 on: March 13, 2013, 09:46:54 AM »
Note: This is a comment on your previous post and not on your last post.
You are using the function p33 recursively. After every recursion the p3lst list contains one less element. When the list contains 1 element (setq p32 (cadr p3lst)) returns nil which causes an error in the Clockwise function.

I've corrected little more my last code, but now problem of recursion again arises...  :cry: But it almost does what should...
Thanks for reply, Roy.

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

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: Random sphere point cloud - make it sphere of 3dfaces
« Reply #4 on: March 14, 2013, 03:50:13 AM »
This should now not fail, but it's only applicable on my case - sphere points, and the worst of all - it doesn't cap what should - THERE ARE MISSING FACES on the sides where distances between points are little bigger...

But what the hell, it almost did it :
Code - Auto/Visual Lisp: [Select]
  1. (defun mid ( p1 p2 )
  2.   (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) p1 p2)
  3. )
  4.  
  5. (defun 4psphcen ( p1 p2 p3 p4 / p12 p23 p34 p12n p23n p34n c123 c123n c234 c234n cen )
  6.   (command "_.ucs" "w")
  7.   (command "_.ucs" "3p" p1 p2 p3)
  8.   (setq p12 (trans (mid p1 p2) 0 1))
  9.   (setq p23 (trans (mid p2 p3) 0 1))
  10.   (setq p12n (polar p12 (+ (angle (trans p1 0 1) (trans p2 0 1)) (/ pi 2.0)) 1.0))
  11.   (setq p23n (polar p23 (+ (angle (trans p2 0 1) (trans p3 0 1)) (/ pi 2.0)) 1.0))
  12.   (setq c123 (trans (inters p12 p12n p23 p23n nil) 1 0))
  13.   (setq c123n (mapcar '+ c123 (trans '(0.0 0.0 1.0) 1 0 t)))
  14.   (command "_.ucs" "w")
  15.   (command "_.ucs" "3p" p2 p3 p4)
  16.   (setq p23 (trans (mid p2 p3) 0 1))
  17.   (setq p34 (trans (mid p3 p4) 0 1))
  18.   (setq p23n (polar p23 (+ (angle (trans p2 0 1) (trans p3 0 1)) (/ pi 2.0)) 1.0))
  19.   (setq p34n (polar p34 (+ (angle (trans p3 0 1) (trans p4 0 1)) (/ pi 2.0)) 1.0))
  20.   (setq c234 (trans (inters p23 p23n p34 p34n nil) 1 0))
  21.   (setq c234n (mapcar '+ c234 (trans '(0.0 0.0 1.0) 1 0 t)))
  22.   (command "_.ucs" "w")
  23.   (setq cen (inters c123 c123n c234 c234n nil))
  24.   cen
  25. )
  26.  
  27. (defun Clockwise-p ( p1 p2 p3 )
  28.   (< (sin (- (angle p1 p3) (angle p1 p2))) -1e-14)
  29. )
  30.  
  31. (defun pt-cir-3df ( p1 ptlst )
  32.   (setq p2 (cadr (vl-sort ptlst '(lambda ( a b ) (< (distance p1 a) (distance p1 b))))) pst p2)
  33.   (setq p3 (cadr (vl-sort (vl-remove p1 ptlst) '(lambda ( a b ) (< (distance p2 a) (distance p2 b))))))
  34.   (setq cen (4psphcen (nth 0 ptlst) (nth 1 ptlst) (nth 2 ptlst) (nth 3 ptlst)))
  35.   (command "_.ucs" "za" p1 cen)
  36.   (setq an2 (angle (trans p1 0 1) (trans p2 0 1)))
  37.   (command "_.ucs" "z" (cvunit an2 "radians" "degrees"))
  38.   (setq cl (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p3 0 1)))
  39.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  40.   (pt-cir-3dfs p1 (vl-remove p2 ptlst) cl)
  41. )
  42.  
  43. (defun p33 ( p3lst clock / p32 )
  44.   (setq p32 (cadr p3lst))
  45.   (if (eq (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p32 0 1)) clock)
  46.     (setq p3 p32)
  47.     (p33 (cdr p3lst) clock)
  48.   )
  49. )
  50.  
  51. (defun pt-cir-3dfs ( p1 ptlst clock )
  52.   (setq p2 p3)
  53.   (setq an3 (angle (trans p1 0 1) (trans p3 0 1)))
  54.   (setq p3 (cadr (setq p3lst (vl-sort (vl-remove p1 ptlst) '(lambda ( a b ) (< (distance p2 a) (distance p2 b)))))))
  55.   (if (not (eq (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p3 0 1)) clock)) (p33 (cdr p3lst) clock))
  56.   (setq an4 (angle (trans p1 0 1) (trans p3 0 1)))
  57.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  58.   (if (> (abs (- an4 an3)) pi) (setq p2 p3 p3 pst))
  59.   (if (not (equal p3 pst 1e-8)) (pt-cir-3dfs p1 (vl-remove p2 ptlst) clock) (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3))))
  60. )
  61.  
  62. (defun c:ptscloud-sphere-3dfaces ( / ss n pt ptlst )
  63.   (setq ss (ssget "_:L" '((0 . "POINT"))))
  64.   (repeat (setq n (sslength ss))
  65.     (setq pt (cdr (assoc 10 (entget (ssname ss (setq n (1- n)))))))
  66.     (setq ptlst (cons pt ptlst))
  67.   )
  68.   (foreach p ptlst
  69.     (command "_.ucs" "w")
  70.     (pt-cir-3df p ptlst)
  71.   )
  72.   (command "_.ucs" "w")
  73.   (princ)
  74. )
  75.  
  76. (defun c:pts-sph-3df nil (c:ptscloud-sphere-3dfaces))
  77.  
  78. (prompt "\nShortcut for c:ptscloud-sphere-3dfaces is c:pts-sph-3df [Start with : Command: pts-sph-3df]")
  79.  

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

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3313
  • Marko Ribar, architect
Re: Random sphere point cloud - make it sphere of 3dfaces
« Reply #5 on: March 14, 2013, 06:41:37 AM »
This is my final version, and I think it's the best... Had to figure out sorting algorithm, and because I wanted closest point from both p1 and p2, I've decided to find closest from mid of p1 and p2... Result is much better... Didn't checked with surfsculpt, but visually that's it...

Code - Auto/Visual Lisp: [Select]
  1. (defun mid ( p1 p2 )
  2.   (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) p1 p2)
  3. )
  4.  
  5. (defun 4psphcen ( p1 p2 p3 p4 / p12 p23 p34 p12n p23n p34n c123 c123n c234 c234n cen )
  6.   (command "_.ucs" "w")
  7.   (command "_.ucs" "3p" p1 p2 p3)
  8.   (setq p12 (trans (mid p1 p2) 0 1))
  9.   (setq p23 (trans (mid p2 p3) 0 1))
  10.   (setq p12n (polar p12 (+ (angle (trans p1 0 1) (trans p2 0 1)) (/ pi 2.0)) 1.0))
  11.   (setq p23n (polar p23 (+ (angle (trans p2 0 1) (trans p3 0 1)) (/ pi 2.0)) 1.0))
  12.   (setq c123 (trans (inters p12 p12n p23 p23n nil) 1 0))
  13.   (setq c123n (mapcar '+ c123 (trans '(0.0 0.0 1.0) 1 0 t)))
  14.   (command "_.ucs" "w")
  15.   (command "_.ucs" "3p" p2 p3 p4)
  16.   (setq p23 (trans (mid p2 p3) 0 1))
  17.   (setq p34 (trans (mid p3 p4) 0 1))
  18.   (setq p23n (polar p23 (+ (angle (trans p2 0 1) (trans p3 0 1)) (/ pi 2.0)) 1.0))
  19.   (setq p34n (polar p34 (+ (angle (trans p3 0 1) (trans p4 0 1)) (/ pi 2.0)) 1.0))
  20.   (setq c234 (trans (inters p23 p23n p34 p34n nil) 1 0))
  21.   (setq c234n (mapcar '+ c234 (trans '(0.0 0.0 1.0) 1 0 t)))
  22.   (command "_.ucs" "w")
  23.   (setq cen (inters c123 c123n c234 c234n nil))
  24.   cen
  25. )
  26.  
  27. (defun Clockwise-p ( p1 p2 p3 )
  28.   (< (sin (- (angle p1 p3) (angle p1 p2))) -1e-14)
  29. )
  30.  
  31. (defun pt-cir-3df ( p1 ptlst )
  32.   (setq p2 (cadr (vl-sort ptlst '(lambda ( a b ) (< (distance p1 a) (distance p1 b))))) pst p2)
  33.   (setq cen (4psphcen (nth 0 ptlst) (nth 1 ptlst) (nth 2 ptlst) (nth 3 ptlst)))
  34.   (command "_.ucs" "za" p1 cen)
  35.   (setq an2 (angle (trans p1 0 1) (trans p2 0 1)))
  36.   (command "_.ucs" "z" (cvunit an2 "radians" "degrees"))
  37.   (setq p3 (car (vl-sort (vl-remove p2 (vl-remove p1 ptlst))
  38.   (function (lambda ( a b )
  39.                 (< (distance (mid p1 p2) a) (distance (mid p1 p2) b))
  40.             )
  41.   )
  42.   )))
  43.   (setq cl (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p3 0 1)))
  44.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  45.   (pt-cir-3dfs p1 (vl-remove p2 ptlst) cl)
  46. )
  47.  
  48. (defun p33 ( p3lst clock / p32 )
  49.   (setq p32 (car p3lst))
  50.   (if (eq (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p32 0 1)) clock)
  51.     (setq p3 p32)
  52.     (p33 (cdr p3lst) clock)
  53.   )
  54. )
  55.  
  56. (defun pt-cir-3dfs ( p1 ptlst clock )
  57.   (setq p2 p3)
  58.   (setq an3 (angle (trans p1 0 1) (trans p3 0 1)))
  59.   (setq p3 (car (setq p3lst (vl-sort (vl-remove p2 (vl-remove p1 ptlst))
  60.   (function (lambda ( a b )
  61.                 (< (distance (mid p1 p2) a) (distance (mid p1 p2) b))
  62.             )
  63.   )
  64.   ))))
  65.   (if (not (eq (Clockwise-p (trans p1 0 1) (trans p2 0 1) (trans p3 0 1)) clock)) (p33 (cdr p3lst) clock))
  66.   (setq an4 (angle (trans p1 0 1) (trans p3 0 1)))
  67.   (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3)))
  68.   (if (> (abs (- an4 an3)) pi) (setq p2 p3 p3 pst))
  69.   (if (not (equal p3 pst 1e-8)) (pt-cir-3dfs p1 (vl-remove p2 ptlst) clock) (entmake (list '(0 . "3DFACE") (cons 10 p1) (cons 11 p1) (cons 12 p2) (cons 13 p3))))
  70. )
  71.  
  72. (defun c:ptscloud-sphere-3dfaces ( / ss n pt ptlst )
  73.   (setq ss (ssget "_:L" '((0 . "POINT"))))
  74.   (repeat (setq n (sslength ss))
  75.     (setq pt (cdr (assoc 10 (entget (ssname ss (setq n (1- n)))))))
  76.     (setq ptlst (cons pt ptlst))
  77.   )
  78.   (foreach p ptlst
  79.     (command "_.ucs" "w")
  80.     (pt-cir-3df p ptlst)
  81.   )
  82.   (command "_.ucs" "w")
  83.   (princ)
  84. )
  85.  
  86. (defun c:pts-sph-3df nil (c:ptscloud-sphere-3dfaces))
  87.  
  88. (prompt "\nShortcut for c:ptscloud-sphere-3dfaces is c:pts-sph-3df [Start with : Command: pts-sph-3df]")
  89.  

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

:)

M.R. on Youtube