Author Topic: Normal and vectors  (Read 4636 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Normal and vectors
« on: August 03, 2006, 06:24:48 AM »
Hi,

For my first contribution in this marvellous website, I wanted to illustrate the very intersting threads of  the home page : "Lightweight polyline bulges and elevations" and "Understanding Vectors" with some little LISP routines using (or not) vector calculus to get the Normal vector of a plane, the elevation of a point from a plane or its orthogonal projection to a plane.

First, to get the Unit Vector from p1 to p2 (this defun will be used in many following routines) :

Code: [Select]
;;; VEC1 Returns the single unit vector from p1 to p2
(defun vec1 (p1 p2)
  (if (not (equal p1 p2 1e-009))
    (mapcar
      '(lambda (x) (/ x (distance p1 p2)))
      (mapcar '- p2 p1)
    )
  )
)

To get the Normal Unit Vector of a plane  defined by 3 points (it respects the "right hand rule" with p0 as origin) :

Code: [Select]
;;; NORM_3PTS Returns the normal vector of the plane defined by 3 points
;;; Respects the "right hand rule" with p0 as origin
(defun norm_3pts (p0 p1 p2 / norm)
  (cond
    ((inters p0 p1 p0 p2)
     (foreach p '(p1 p2)
       (set p (mapcar '- (eval p) p0))
     )
     (setq norm (list (- (* (cadr p1) (caddr p2)) (* (caddr p1) (cadr p2)))
      (- (* (caddr p1) (car p2)) (* (car p1) (caddr p2)))
      (- (* (car p1) (cadr p2)) (* (cadr p1) (car p2)))
)
   norm (mapcar '(lambda (x) (* x (/ 1 (distance '(0 0 0) norm))))
norm
)
     )
    )
  )
)

I founded another way to get the Normal of the current UCS using the TRANS function. I call it UCSZDIR (as the systeme variables UCSXDIR and UCSYDIR). It can be stored in a variable or used as a defun :

Code: [Select]
;;; UCSZDIR Returns the Normal Unit Vector of the current UCS
(defun ucszdir  ()
  (trans '(0 0 1) 1 0 T)
)

UCSZDIR is usefull with the TRANS function, to make an entamke list for an OCS defined entity : (cons 10 (trans pt 1 ucszdir)) and (cons 210 ucszdir) or using with vla-... functions : (vla-put-Normal objt (vlax-3d-point ucszdir))

For the following routines we'll need also the planar angle between three 3D points (summit and point1 point2) and to get this angle we'll have to define ACOS or ASIN (depending the routine used) :

Code: [Select]
;;; ASIN Returns the arcsinus of a number in radians
(defun ASIN (num)
  (if (<= -1 num 1)
    (atan num (sqrt (- 1 (expt num 2))))
  )
)

;;;ACOS Returns the arccosinus of a number in radians
(defun ACOS (num)
  (if (<= -1 num 1)
    (atan (sqrt (- 1 (expt num 2))) num)
  )
)

;;; ANGLE_3PTS Returns the angle (in radians) defined by its summit and two points
;;; Returned angle is always positive and less than or equal to pi radians.

;; Using vector calculus
(defun angle_3pts (sum p1 p2 / v1 v2)
  (if (and
(setq v1 (vec1 sum p1))
(setq v2 (vec1 sum p2))
      )
    (cond
      ((equal v1 v2 1e-009) 0.0)
      ((equal v1 (mapcar '- v2) 1e-009) pi)
      (T (* 2 (asin (/ (distance v1 v2) 2))))
    )
  )
)

;; Using the "Théorème de Carnot"
(defun angle_3pts (sum p1 p2 / d1 d2 d3)
  (setq d1 (distance sum p1)
d2 (distance sum p2)
d3 (distance p1 p2)
  )
  (if (and (not (zerop d1)) (not (zerop d2))
    (ACOS (/ (+ (* d1 d1) (* d2 d2) (- (* d3 d3)))
     (* 2 d1 d2)
  )
    )
  )
)

To get the elevation of a point from a plane defined by 3 points :

Code: [Select]
;;; ELEV Returns the elevation of point pt from the plane defined by p1 p2 p3

(defun elev (pt p1 p2 p3)
  (* (cos (angle_3pts p1 (mapcar '+ p1 (norm_3pts p1 p2 p3)) pt))
     (distance p1 pt)
  )
)

To get the coordinates of the projection of pt on p1 p2 p3 plane :

Code: [Select]
;;; PROJ_PT Returns the coordinates of the projection of point pt
;;; on the plane defined by the points p1 p2 p3.

(defun proj_pt (pt p1 p2 p3)
  (mapcar '-
  pt
  (mapcar '(lambda (x) (* x (elev pt p1 p2 p3)))
  (norm_3pts p1 p2 p3)
  )
  )
)

I hope all this will be usefull to someones.
« Last Edit: August 04, 2006, 02:06:05 AM by gile »
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Normal and vectors
« Reply #1 on: August 03, 2006, 08:08:26 AM »
gile,
Thanks for your contribution and welcome to the swamp. :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Normal and vectors
« Reply #2 on: August 03, 2006, 09:53:52 PM »
Yes welcome gile, nice read