Author Topic: Triangulation (re-visited)  (Read 317748 times)

0 Members and 1 Guest are viewing this topic.

ymg

  • Guest
Re: Triangulation (re-visited)
« Reply #210 on: June 09, 2014, 01:37:37 PM »
Quote
I tried with your triloc function. Is it correct that this keeps track of the previous found triangle ?
So assuming i go to the next point in the grid and when this is inside of the same triangle, this one is tested first ?

What triloc does is always start from the last triangle it found.

Then tested if points lies to the left of first edge of triangle,
if true we test the second edge and the third. If all three
test are true means we found the triangle.

If at any of the test we find that the point lies to the right
we go to the triangle that has a common edge with the
edge we were testing.

However in a triangulation with constraints the function
is not guaranteed to find it's way, it could enter in a loop.

To fix that we can use the so called "Remembering Stochastic Walk".

This one start testing on a random edge of the triangle under test
and proceed with the same kind of test as above.

This way one is certain that eventually it will find its way.  However
in some degenerate case the walk could be long.

ymg

XXL66

  • Newt
  • Posts: 99
Re: Triangulation (re-visited)
« Reply #211 on: June 10, 2014, 02:59:18 AM »
hi, thx for the explanation.
How about this method:

http://www.theswamp.org/index.php?topic=43695.msg490059#msg490059

As for the new (red) surface, i suppose this is drawn relative to the zero plane with the differences found between the 2 surfaces. If the 2 surfaces intersect then the zero elevation line on the red surface would represent the intersection line.

ymg

  • Guest
Re: Triangulation (re-visited)
« Reply #212 on: June 10, 2014, 11:32:37 AM »
Here is some code I had developped with a friend in Spain.

Do test it as I don't remember where I was as to final result.

It uses the interfere method to find the common surface.


Code - Auto/Visual Lisp: [Select]
  1. (defun c:voltin (/ *acaddoc* bmax bmin en en3 en4 en5 en6 h i layp layr
  2.                    pins pmax pol rmax ss1 ss2 ssprop ssref v1 v2 v3 varl
  3.                    volp volr vp vr y)
  4.                  
  5.  
  6.    ;;; Error Handler by ElpanovEvgenyi                                        ;
  7.    (defun *error* (msg)
  8.         (mapcar 'eval varl)
  9.         (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
  10.            (princ (strcat "\nError: " msg))
  11.         )
  12.         (and *AcadDoc* (vla-endundomark *AcadDoc*))
  13.         (princ)
  14.    )
  15.      
  16.    (setq varl '("CLAYER" "OSMODE" "CMDECHO" "DIMZIN")
  17.          varl (mapcar (function (lambda (a) (list 'setvar a (getvar a)))) varl)
  18.    )    
  19.      
  20.    (or *AcadDoc* (setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
  21.            
  22.      
  23.    (setvar 'CMDECHO 0)
  24.    (setvar 'DIMZIN 0)
  25.    (setvar 'OSMODE 0)
  26.  
  27.    (if (and (setq layr (cdr (assoc 8 (entget (car (entsel "\nPick a 3DFACE on Reference Layer: ")))))
  28.                   ss1 (ssget "_X" (list '(0 . "3DFACE")(cons 8 layr)))
  29.             )
  30.             (setq layp (cdr (assoc 8 (entget (car (entsel "\nPick a 3DFACE on Proposed Layer: ")))))
  31.                   ss2 (ssget "_X" (list '(0 . "3DFACE")(cons 8 layp)))
  32.             )
  33.        )    
  34.       (progn
  35.          (vla-startundomark *AcadDoc*)
  36.  
  37.          
  38.          (setvar 'CLAYER layr)
  39.          (setq ssref (ssadd))
  40.          (repeat (setq i (sslength ss1))
  41.               (setq   en (ssname ss1 (setq i (1- i)))
  42.                    ssref (ssadd (3df2sol en) ssref)
  43.               )
  44.           )
  45.           (vl-cmdf "_UNION" ssref "")
  46.           (setq en3 (entlast))
  47.           (vla-GetBoundingBox (vlax-eName->vla-Object en3) 'rmin 'rmax)
  48.           (setq rmax (vlax-SafeArray->List rmax))
  49.          
  50.           (setvar 'CLAYER layp)
  51.           (setq ssprop (ssadd))
  52.           (repeat (setq i (sslength ss2))
  53.               (setq en (ssname ss2 (setq i (1- i)))
  54.                    ssprop (ssadd (3df2sol en) ssprop)
  55.               )
  56.           )
  57.           (vl-cmdf "_UNION" ssprop "")
  58.           (setq en4 (entlast))
  59.           (vla-GetBoundingBox (vlax-eName->vla-Object en4) 'pmin 'pmax)
  60.           (setq pmax (vlax-SafeArray->List pmax))
  61.          
  62.           (vl-cmdf "_-LAYER" "_M" "SUPERFICIES" "")
  63.  
  64.           (vl-cmdf "_-INTERFERE" en3 "" en4 "" "_Y")
  65.           (setq en5 (entlast))
  66.           (vla-GetBoundingBox (vlax-eName->vla-Object en5) 'bmin 'bMax)
  67.           (setq bmin (vlax-SafeArray->List bmin)
  68.                 bmax (vlax-SafeArray->List bmax)                
  69.                 pins (mapcar '/ (mapcar '+ bmin bmax) '(2. 2.))
  70.           )
  71.          
  72.           (vl-cmdf "_-BOUNDARY" "_A" "_B" "_N" en5 "" "_O" "_P" "" pins "" )        
  73.           (setq pol (entlast))
  74.           (vl-cmdf "_EXTRUDE" pol "" (max (caddr rmax) (caddr pmax)))
  75.           (setq pol (entlast))
  76.          
  77.           (setvar 'CLAYER layr)
  78.           (vl-cmdf "_-INTERFERE" en3 "" pol "" "_Y")
  79.           (setq volr (entlast))
  80.           (setvar 'CLAYER layp)
  81.           (vl-cmdf "_-INTERFERE" en4 "" pol "" "_Y")
  82.           (setq volp (entlast))
  83.           (entdel pol)
  84.          
  85.           (setq vr (vlax-get-property (vlax-ename->vla-object volr) 'Volume)
  86.                 vp (vlax-get-property (vlax-ename->vla-object volp) 'Volume)
  87.           )
  88.           (setvar 'CLAYER layr)
  89.           (setq y (cadr bmin)
  90.                 h (* (getvar 'TEXTSIZE) 1.5)
  91.           )      
  92.           (vl-cmdf "_text" "_J" "_MC" (list (car pins) y)         0 (strcat "Reference Volume: " (rtos vr 2 1) " m3"))
  93.           (setq v1 (entlast))
  94.           (setvar 'CLAYER layp)
  95.           (vl-cmdf "_text" "_J" "_MC" (list (car pins) (- y h))   0 (strcat " Proposed Volume: " (rtos vp 2 1) " m3"))
  96.           (setq v2 (entlast))          
  97.           (if (> vr vp) (setvar 'CLAYER layr))          
  98.           (vl-cmdf "_text" "_J" "_MC" (list (car pins) (- y h h)) 0 (strcat "      Net Volume: " (rtos (- vr vp) 2 1) " m3"))
  99.           (setq v3 (entlast))
  100.           (vl-cmdf "_MOVE" volp volr v1 v2 v3 "" pins pause)
  101.           (vl-cmdf "_VSCURRENT" "_S" "")
  102.       )      
  103.    )  
  104.    (*error* nil)
  105. )
  106.  
  107. ;; 3df2sol                                                                    ;
  108. ;; Given a 3DFACE Loft it Down to Elevation 0                                 ;
  109. ;; Returns the ename of the Solid created.                                    ;
  110.  
  111. (defun 3df2sol (en / en1 en2 p1 p2 p3 p4)
  112.    (setq  ent (entget en)
  113.            p1 (cdr (assoc 10 ent))
  114.            p2 (cdr (assoc 11 ent))
  115.            p3 (cdr (assoc 12 ent))
  116.            p4 (cdr (assoc 13 ent))
  117.    )
  118.    
  119.    (setq en1 (entmakex
  120.                 (list
  121.                   (cons 0 "3DFACE")  
  122.                   (cons 10 (list (car p1) (cadr p1) 0.))
  123.                   (cons 11 (list (car p2) (cadr p2) 0.))
  124.                   (cons 12 (list (car p3) (cadr p3) 0.))
  125.                   (cons 13 (list (car p4) (cadr p4) 0.))
  126.                 )
  127.              )
  128.    )
  129.    (vl-cmdf "_loft" en  en1 "_MO" "_SOLID" "" "")
  130.    (entlast)
  131. )  
  132.  
« Last Edit: June 10, 2014, 01:50:43 PM by ymg »

XXL66

  • Newt
  • Posts: 99
Re: Triangulation (re-visited)
« Reply #213 on: June 10, 2014, 12:16:13 PM »
hi,

thx, but this is based on solids. I have similar code that computes in fact the volume of the prism's, it's  basicly the same method.

ymg

  • Guest
Re: Triangulation (re-visited)
« Reply #214 on: June 10, 2014, 01:49:09 PM »
XXL66,

If you cannot go the Solid Way,  you will have to compute
the intersecting polyline between the two surfaces.

This will creates some surfaces that are not 3dface
on the perimeter.  However you still can compute
their volume by Average End Area.

It can be done but it's messy .

How about posting the code you have,
we may get ideas out of it.

ymg
« Last Edit: June 10, 2014, 01:54:17 PM by ymg »

XXL66

  • Newt
  • Posts: 99
Re: Triangulation (re-visited)
« Reply #215 on: June 11, 2014, 05:52:06 AM »
The code i have is written in c++ (and i'm very bad c coder...), it works and gives a good result but the results cannot be displayed graphical in any way...
It just computes the differences from the union.

I think the example method is the way to go, compute the red surface, so this is (i think) a new computed triangulation based on every intersecting 3Dface point and the existing points (overlapping).

However problems may arrise when the original surfaces are constraint. So you would have to include the constraints into the new surface too.

btw: does your edge solution work with ElpanovEvgenyi triangulation method ?






XXL66

  • Newt
  • Posts: 99
Re: Triangulation (re-visited)
« Reply #216 on: June 11, 2014, 06:05:50 AM »
As for the edges i also have created c++ code (long time ago) that computes the intersections and draws 2 new triangles (in case of one 3dface edge intersection) and 3 new triangles (in case of 2 two edge 3dface intersection).

It is however slow, i would like to do this in lisp (no need for recompilation for every bcad/acad) but for optimization work woth an index list of some sort...


Notice that in this case 2 or more neighbouring 3dface's might be in the same plane (this could be improved by join it to a single one.




« Last Edit: June 11, 2014, 07:06:07 AM by XXL66 »

ymg

  • Guest
Re: Triangulation (re-visited)
« Reply #217 on: June 11, 2014, 08:42:10 AM »
XXL66,

Quote
btw: does your edge solution work with ElpanovEvgenyi triangulation method

Yes it does.  However as told, I need to change TRILOC some in order for it to
work correctly in a CDT.

Another bottleneck is that presently I recompute the List of neighours after each
insertions.  Will replace by an adjustment of the list.

Also instead of deleting the triangles in the cavity formed by the inserted edge
I could substitute the new triangles.

But I've been lazy this week.

ymg



XXL66

  • Newt
  • Posts: 99
Re: Triangulation (re-visited)
« Reply #218 on: June 11, 2014, 09:55:04 AM »
lazy, maybe because of the weather ? It's hot here ! Just enjoyed i nice belgian beer...

CDT ? what's that ?

any ideas for the fastest method to find the list position in tl that contain a certain pt indexnr ?

f.e.
: !tl
((2 1 0) (3 1 2) (4 1 3) (6 4 3) (5 1 4) (7 6 3) (9 3 8) (9 7 3) (8 3 2) (11 5 10) (10 5 4) (10 4 6) (12 6 7) (12 7 9) (12 9 8) (13 6 12) (13 10 6) (14 12 8) (15 13 14) (15 10 13) (15 11 10) (8 2 0) (14 13 12))
: (mapcar '(lambda (x) (member 2 x)) tl)
((2 1 0) (2) NIL NIL NIL NIL NIL NIL (2) NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL (2 0) NIL)

if would like a list of position numbers  (0 1 8 21) in this case

...


« Last Edit: June 11, 2014, 10:04:19 AM by XXL66 »

ymg

  • Guest
Re: Triangulation (re-visited)
« Reply #219 on: June 11, 2014, 11:08:48 AM »
Quote
CDT ? what's that ?

CDT is a Constrained Delaunay Triangulation

Code: [Select]
(setq temp (vl-remove nil (mapcar '(lambda (x) (if (member 2 x) x)) tl)))
(foreach p temp
   (setq l (cons (vl-position p tl) l))
)

Quote
((2 1 0) (3 1 2) (8 3 2) (8 2 0))
(21 8 1 0)

XXL66

  • Newt
  • Posts: 99
Re: Triangulation (re-visited)
« Reply #220 on: June 11, 2014, 11:21:19 AM »
thx,

btw: is there a 'inters' function that works 2D only ?

ymg

  • Guest
Re: Triangulation (re-visited)
« Reply #221 on: June 11, 2014, 11:23:23 AM »
Here is TRILOC modified to operates in a CDT:

Code - Auto/Visual Lisp: [Select]
  1. ;;****************************************************************************;
  2. ;; (triloc p)                                                                 ;
  3. ;;                                                                            ;
  4. ;; Locates triangle which encloses point p using Remembering Stochastic Walk. ;
  5. ;;                                                                            ;
  6. ;; Given p a point, Returns Index in tl of triangle containing the point.     ;
  7. ;; If outside the triangulation Return is nil.                                ;
  8. ;;                                                                            ;
  9. ;; Point List pl and Neigbour List nl are defined outside this routine.       ;
  10. ;; by ymg  August 2013                                                        ;
  11. ;; Optimized Speed and re-organized code January 2014                         ;
  12. ;; Nice but get lost when triangulation is disjointed.                        ;
  13. ;;****************************************************************************;
  14.  
  15. (defun triloc (p pl tl nl / notfound i p1 p2 p3 x x1 x2 x3 y y1 y2 y3)
  16.      
  17.     (if (not tn) (setq tn (/ (length tl) 2)))
  18.     (setq x (car p)  y (cadr p)  notfound t)  
  19.     (while (and notfound tn)        
  20.         (setq   i (nth tn tl)
  21.                p1 (nth (car   i) pl)  p2 (nth (cadr  i) pl) p3 (nth (caddr i) pl)                
  22.               x1x (- (car p1) x)  y1y (- (cadr p1) y)
  23.               x2x (- (car p2) x)  y2y (- (cadr p2) y)
  24.               x3x (- (car p3) x)  y3y (- (cadr p3) y)
  25.                 e (fix (mrand 3))
  26.         )
  27.         (cond
  28.            ((= e 0)  (cond
  29.                        ((minusp (- (* x1x y2y) (* y1y x2x))) (setq tn (car   (nth tn nl))))
  30.                        ((minusp (- (* x2x y3y) (* y2y x3x))) (setq tn (cadr  (nth tn nl))))
  31.                        ((minusp (- (* x3x y1y) (* y3y x1x))) (setq tn (caddr (nth tn nl))))          
  32.                        ((setq notfound nil))      
  33.                      ))
  34.            ((= e 1)  (cond
  35.                        ((minusp (- (* x2x y3y) (* y2y x3x))) (setq tn (cadr  (nth tn nl))))
  36.                        ((minusp (- (* x3x y1y) (* y3y x1x))) (setq tn (caddr (nth tn nl))))
  37.                        ((minusp (- (* x1x y2y) (* y1y x2x))) (setq tn (car   (nth tn nl))))
  38.                        ((setq notfound nil))      
  39.                      ))
  40.           ((= e 2)  (cond
  41.                        ((minusp (- (* x3x y1y) (* y3y x1x))) (setq tn (caddr (nth tn nl))))
  42.                        ((minusp (- (* x1x y2y) (* y1y x2x))) (setq tn (car   (nth tn nl))))
  43.                        ((minusp (- (* x2x y3y) (* y2y x3x))) (setq tn (cadr  (nth tn nl))))
  44.                        ((setq notfound nil))      
  45.                      ))
  46.         )  
  47.     )  
  48.     tn
  49. )
  50.  


Also modified TRIPOL so that the triangles formed will always be CCW:

Code - Auto/Visual Lisp: [Select]
  1. ;;****************************************************************************;
  2. ;; tripol                    by ymg                                           ;
  3. ;;                                                                            ;
  4. ;; Arguments: p, list of point index.                                         ;
  5. ;;            a, Index of First point of an Edge.                             ;
  6. ;;            b, Index of Second point of Edge.                               ;
  7. ;;            r, Flag for ccw polygon.                                        ;
  8. ;;                                                                            ;
  9. ;; Will accumulates in external variable newtri the Delaunay's Triangles      ;
  10. ;; formed by the defining Points and Edge.                                    ;
  11. ;;                                                                            ;
  12. ;;****************************************************************************;
  13.  
  14.  
  15. (defun tripol (p a b r / c pe pd v)
  16.    (setq c (car p))
  17.    (if (> (length p) 1)
  18.       (progn
  19.          (foreach v (cdr p)
  20.             (if (swap a b c v) (setq c v))
  21.          )
  22.          (setq pe (trunc c p)
  23.                pd (cdr (member c p))
  24.          )
  25.          (if pe (tripol pe a c r))
  26.          (if pd (tripol pd c b r))
  27.       )
  28.    )
  29.    (if p (setq newtri (cons (if r (list a b c)(list c b a)) newtri)))
  30. )
  31.  

ymg

  • Guest
Re: Triangulation (re-visited)
« Reply #222 on: June 11, 2014, 11:27:34 AM »
Quote
btw: is there a 'inters' function that works 2D only ?

No, but I believe if a single point is 2d, inters will give you 2d

ymg

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Triangulation (re-visited)
« Reply #223 on: June 11, 2014, 11:34:39 AM »
thx,

btw: is there a 'inters' function that works 2D only ?

Modify this code :
http://www.theswamp.org/index.php?topic=46848.msg518851#msg518851

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

:)

M.R. on Youtube

XXL66

  • Newt
  • Posts: 99
Re: Triangulation (re-visited)
« Reply #224 on: June 11, 2014, 01:19:09 PM »
Quote
btw: is there a 'inters' function that works 2D only ?

No, but I believe if a single point is 2d, inters will give you 2d

ymg

This seems to be correct.

I'm trying to add adges but before the triangles are drawn. It seems that you use a fence selection on the 3dfaces ? Is that correct ?

However to learn more about the program is did a test with 4 points

: !tl
((2 0 1) (3 2 1))

2 triangles = OK

: !pl
((4522.88 5437.91 59.1125) (4525.7 5431.95 55.493) (4533.9 5442.11 58.5841) (4536.59 5429.13 58.079))

4 points = OK

: !nl
((1 8 7) (6 NIL 0) (7 8 3) (5 2 4) (3 NIL 6) (6 7 3) (4 1 5) (0 2 5) (0 NIL 2))
: !el
((2 5) (5 0) (0 2) (2 4) (4 5) (5 2) (1 0) (0 6) (6 1) (3 1) (1 6) (6 3) (3 6) (6 4) (4 3) (3 2) (2 1) (1 3) (3 4) (4 2) (2 3) (2 0) (0 1) (1 2) (0 5) (5 6) (6 0))

Can you explain nl and el ?