Author Topic: Coplanarity - How to determine Z if XY is known  (Read 628 times)

0 Members and 1 Guest are viewing this topic.

rayakmal

  • Newt
  • Posts: 53
Coplanarity - How to determine Z if XY is known
« on: January 22, 2024, 09:17:06 PM »
Hi everyone.
I have problem and I'm not good at math and 3d Vector, this is about coplanarity.
How can I determine PT4 Elevation, if PT1, PT2, PT3 dan PT4 are coplanar?
Any help is very much appreciated.

dexus

  • Bull Frog
  • Posts: 208
Re: Coplanarity - How to determine Z if XY is known
« Reply #1 on: January 23, 2024, 06:24:16 AM »
You can use gile's Normal3pts function found here: https://gilecad.azurewebsites.net/Lisp.aspx
Code - Auto/Visual Lisp: [Select]
  1. ;; gc:Normal3Pts
  2. ;; Retourne le vecteur normal du plan défini par p1 p2 p3
  3. ;;
  4. ;; Arguments
  5. ;; p1, p2, p3 trois points 3d figurant un triangle dans l'espace
  6. (defun gc:Normal3Pts (p1 p2 p3)
  7.   (list
  8.     (- (* (- (cadr p2) (cadr p1)) (- (caddr p3) (caddr p1)))
  9.        (* (- (caddr p2) (caddr p1)) (- (cadr p3) (cadr p1)))
  10.     )
  11.     (- (* (- (caddr p2) (caddr p1)) (- (car p3) (car p1)))
  12.        (* (- (car p2) (car p1)) (- (caddr p3) (caddr p1)))
  13.     )
  14.     (- (* (- (car p2) (car p1)) (- (cadr p3) (cadr p1)))
  15.        (* (- (cadr p2) (cadr p1)) (- (car p3) (car p1)))
  16.     )
  17.   )
  18. )
Then you compare the z values to find the elevation.
Code - Auto/Visual Lisp: [Select]
  1. ;;;-----------------------------------------------------------;;
  2. ;;; Find Elevation of pt4 from plane pt1-pt2-pt3              ;;
  3. ;;;-----------------------------------------------------------;;
  4. (defun getElevation (pt1 pt2 pt3 pt4 / normal)
  5.   (setq normal (gc:Normal3Pts pt1 pt2 pt3))
  6.   (-
  7.     (caddr (trans pt4 0 normal)) ; Elevation of pt4
  8.     (caddr (trans pt1 0 normal)) ; Elevation of the plane
  9.   )
  10. )

But if you just want to know if they are coplanar without knowing how much, there probably is a better function for that.
« Last Edit: January 23, 2024, 08:10:20 AM by dexus »

dexus

  • Bull Frog
  • Posts: 208
Re: Coplanarity - How to determine Z if XY is known
« Reply #2 on: January 23, 2024, 06:26:48 AM »
Oh, I found that gile also has a gc:Coplanarp function in the same library found at https://gilecad.azurewebsites.net/Lisp.aspx
This one should answer your question. :lol:
Code - Auto/Visual Lisp: [Select]
  1. ;; gc:Coplanarp
  2. ;; Evalue si tous les points de la liste sont coplanaires
  3. ;;
  4. ;; Arguments
  5. ;; pts : une liste de points
  6. (defun gc:Coplanarp (pts / norm zlst)
  7.   (or
  8.     (null (cdddr pts))
  9.     (and
  10.       (setq norm (gc:Normal3Pts (car pts) (cadr pts) (caddr pts))
  11.             zlst (mapcar (function (lambda (p) (caddr (trans p 0 norm))))
  12.                          pts
  13.                  )
  14.       )
  15.       (vl-every (function (lambda (z) (equal z (car zlst) 1e-9)))
  16.                 (cdr zlst)
  17.       )
  18.     )
  19.   )
  20. )

xdcad

  • Bull Frog
  • Posts: 493
Re: Coplanarity - How to determine Z if XY is known
« Reply #3 on: January 23, 2024, 08:34:46 AM »
Cross multiply the x vector with the y vector to get Z
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

dexus

  • Bull Frog
  • Posts: 208
Re: Coplanarity - How to determine Z if XY is known
« Reply #4 on: January 23, 2024, 10:37:29 AM »
Cross multiply the x vector with the y vector to get Z
Yes, that is exactly what gc:Normal3Pts does.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coplanarity - How to determine Z if XY is known
« Reply #5 on: January 23, 2024, 12:43:42 PM »
You can also obtain the distance without the use of trans by simply calculating the dot product between the unit normal and a vector from a point in the plane to the given point, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. (defun disttoplane ( pt p1 p2 p3 )
  2.     (abs (vxv (vx1 (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))) (mapcar '- pt p1)))
  3. )
  4.  
  5. ;; Vector Dot Product  -  Lee Mac
  6. ;; Args: u,v - vectors in R^n
  7.  
  8. (defun vxv ( u v )
  9.     (apply '+ (mapcar '* u v))
  10. )
  11.  
  12. ;; Unit Vector  -  Lee Mac
  13. ;; Args: v - vector in R^2 or R^3
  14.  
  15. (defun vx1 ( v )
  16.     (   (lambda ( n ) (if (equal 0.0 n 1e-10) nil (mapcar '/ v (list n n n))))
  17.         (distance '(0.0 0.0 0.0) v)
  18.     )
  19. )
  20.  
  21. ;; Vector Cross Product  -  Lee Mac
  22. ;; Args: u,v - vectors in R^3
  23.  
  24. (defun v^v ( u v )
  25.     (list
  26.         (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
  27.         (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))
  28.         (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))
  29.     )
  30. )

rayakmal

  • Newt
  • Posts: 53
Re: Coplanarity - How to determine Z if XY is known
« Reply #6 on: January 24, 2024, 12:42:27 AM »
Oh, I found that gile also has a gc:Coplanarp function in the same library found at https://gilecad.azurewebsites.net/Lisp.aspx
This one should answer your question. :lol:
Code - Auto/Visual Lisp: [Select]
  1. ;; gc:Coplanarp
  2. ;; Evalue si tous les points de la liste sont coplanaires
  3. ;;
  4. ;; Arguments
  5. ;; pts : une liste de points
  6. (defun gc:Coplanarp (pts / norm zlst)
  7.   (or
  8.     (null (cdddr pts))
  9.     (and
  10.       (setq norm (gc:Normal3Pts (car pts) (cadr pts) (caddr pts))
  11.             zlst (mapcar (function (lambda (p) (caddr (trans p 0 norm))))
  12.                          pts
  13.                  )
  14.       )
  15.       (vl-every (function (lambda (z) (equal z (car zlst) 1e-9)))
  16.                 (cdr zlst)
  17.       )
  18.     )
  19.   )
  20. )
Yes! It works like a charm
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test ()
  2.   (setq pt1 (list 0.0    0.0    1.0)
  3.         pt2 (list 100.00 10.00  1.0)
  4.         pt3 (list 10.00  100.0  1.0)
  5.         pti (list 200.0  200.0  0.0)
  6.         ptj (list 200.0  200.0  4.0)
  7.   )
  8.   (princ (strcat "\n. Distance pti to plane: " (rtos (getElevation pt1 pt2 pt3 pti) 2 3)))
  9.   (princ (strcat "\n. Distance ptj to plane: " (rtos (getElevation pt1 pt2 pt3 ptj) 2 3)))
  10.   (princ)
  11. )
  12.  

Thanks a lot.

rayakmal

  • Newt
  • Posts: 53
Re: Coplanarity - How to determine Z if XY is known
« Reply #7 on: January 24, 2024, 01:00:45 AM »
You can also obtain the distance without the use of trans by simply calculating the dot product between the unit normal and a vector from a point in the plane to the given point, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. (defun disttoplane ( pt p1 p2 p3 )
  2.     (abs (vxv (vx1 (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))) (mapcar '- pt p1)))
  3. )
  4.  
  5. ;; Vector Dot Product  -  Lee Mac
  6. ;; Args: u,v - vectors in R^n
  7.  
  8. (defun vxv ( u v )
  9.     (apply '+ (mapcar '* u v))
  10. )
  11.  
  12. ;; Unit Vector  -  Lee Mac
  13. ;; Args: v - vector in R^2 or R^3
  14.  
  15. (defun vx1 ( v )
  16.     (   (lambda ( n ) (if (equal 0.0 n 1e-10) nil (mapcar '/ v (list n n n))))
  17.         (distance '(0.0 0.0 0.0) v)
  18.     )
  19. )
  20.  
  21. ;; Vector Cross Product  -  Lee Mac
  22. ;; Args: u,v - vectors in R^3
  23.  
  24. (defun v^v ( u v )
  25.     (list
  26.         (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
  27.         (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))
  28.         (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))
  29.     )
  30. )

Code: [Select]
(defun c:Test ()
  (setq pt1 (list 0.0    0.0    1.0)
        pt2 (list 100.00 10.00  1.0)
        pt3 (list 10.00  100.0  1.0)
        pti (list 200.0  200.0  0.0)
        ptj (list 200.0  200.0  4.0)
  )

  (princ (strcat "\n. Distance pti to plane: " (rtos (disttoplane pti pt1 pt2 pt3) 2 3)))
  (princ (strcat "\n. Distance ptj to plane: " (rtos (disttoplane ptj pt1 pt2 pt3) 2 3)))

  (princ)
)

Result:
. Distance pti to plane: 1.000
. Distance ptj to plane: 3.000

It only shows distance (its magnitude) but doesn't show if the point is above or bellow the plane.

dexus

  • Bull Frog
  • Posts: 208
Re: Coplanarity - How to determine Z if XY is known
« Reply #8 on: January 24, 2024, 02:47:39 AM »
You can remove the abs from disttoplane function to get the same result.
Note that the plane can be orientated in two ways which will give you a positive or negative output.
So above or below might not be what you expect it to be.

But if you want to find the difference of pti and ptj, this should work fine.
Code - Auto/Visual Lisp: [Select]
  1. (defun disttoplane ( pt p1 p2 p3 )
  2.     (vxv (vx1 (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))) (mapcar '- pt p1))
  3. )

rayakmal

  • Newt
  • Posts: 53
Re: Coplanarity - How to determine Z if XY is known
« Reply #9 on: January 24, 2024, 08:51:01 PM »
You can remove the abs from disttoplane function to get the same result.
Note that the plane can be orientated in two ways which will give you a positive or negative output.
So above or below might not be what you expect it to be.

But if you want to find the difference of pti and ptj, this should work fine.
Code - Auto/Visual Lisp: [Select]
  1. (defun disttoplane ( pt p1 p2 p3 )
  2.     (vxv (vx1 (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))) (mapcar '- pt p1))
  3. )

Yes, this is what I want.

I work with terrain/topographic map, I think removing the 'abs' from disttoplane function won't be a problem.
Thanks a lot, Dexus an Lee.