Author Topic: Matrix to reansform block being perpendicular to 3D axis  (Read 1275 times)

0 Members and 1 Guest are viewing this topic.

kozmos

  • Newt
  • Posts: 114
Matrix to reansform block being perpendicular to 3D axis
« on: July 27, 2023, 08:54:05 PM »
I have a block inserted on Point A, while I need the matrix to transform it so it can be perpendicular to Point A and another 3D Point B, I m kind of confused how to build the transform matrix, could anyone help?
KozMos Inc.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Matrix to reansform block being perpendicular to 3D axis
« Reply #1 on: July 28, 2023, 02:36:11 AM »
Untested...

Code - Auto/Visual Lisp: [Select]
  1. (defun matrix ( a b / unit v^v x y z mat3x3 mat4x4 n )
  2.  
  3.   (defun unit ( v / d )
  4.     (if (not (equal (setq d (distance '(0.0 0.0 0.0) v)) 0.0 1e-8))
  5.       (mapcar '(lambda ( x ) (/ x d)) v)
  6.     )
  7.   )
  8.  
  9.   (defun v^v ( u v )
  10.     (list
  11.       (- (* (cadr u) (caddr v)) (* (caddr u) (cadr v)))
  12.       (- (* (caddr u) (car v)) (* (car u) (caddr v)))
  13.       (- (* (car u) (cadr v)) (* (cadr u) (car v)))
  14.     )
  15.   )
  16.  
  17.   (setq z (unit (mapcar '- b a)))
  18.   (setq x (unit (v^v z '(0.0 0.0 1.0))))
  19.   (setq y (unit (v^v z x)))
  20.   (setq mat3x3
  21.     (list
  22.       (list (car x) (car y) (car z))
  23.       (list (cadr x) (cadr y) (cadr z))
  24.       (list (caddr x) (caddr y) (caddr z))
  25.     )
  26.   )
  27.   (setq mat4x4
  28.     (mapcar '(lambda ( x ) (setq n (if (not n) 0 (1+ n))) (append x (cond ( (= n 0) (list (car a)) ) ( (= n 1) (list (cadr a)) ) ( (= n 2) (list (caddr a)) ) ( (= n 3) (list 1.0) )))) (append mat3x3 (list (list 0.0 0.0 0.0))))
  29.   )
  30. )
  31.  

M.R.
« Last Edit: July 29, 2023, 11:08:40 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

kozmos

  • Newt
  • Posts: 114
Re: Matrix to reansform block being perpendicular to 3D axis
« Reply #2 on: July 29, 2023, 01:24:22 AM »
Thanks, It works, just need to change the last (= n 0) to (= n 3) in the cond.
« Last Edit: July 29, 2023, 04:56:37 AM by kozmos »
KozMos Inc.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Matrix to reansform block being perpendicular to 3D axis
« Reply #3 on: July 29, 2023, 11:09:19 AM »
Thanks, It works, just need to change the last (= n 0) to (= n 3) in the cond.

Sorry I didn't saw it... Corrected now...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Matrix to reansform block being perpendicular to 3D axis
« Reply #4 on: August 10, 2023, 05:59:28 AM »
I think the following should suffice -
Code - Auto/Visual Lisp: [Select]
  1. (defun matrix ( a b / m n )
  2.     (setq n (mapcar '- b a)
  3.           m (mapcar '(lambda ( v ) (trans v 0 n t)) '((1 0 0)(0 1 0)(0 0 1)))
  4.     )
  5.     (append (mapcar '(lambda ( m a b ) (append m (list (- a b)))) m a (mxv m a))
  6.        '((0.0 0.0 0.0 1.0))
  7.     )
  8. )
  9.  
  10. ;; Matrix x Vector  ~  Vladimir Nesterovsky
  11. (defun mxv ( mat vec )
  12.     (mapcar '(lambda ( row ) (apply '+ (mapcar '* row vec))) mat)
  13. )

To test:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / ent pt1 pt2 )
  2.     (if (and (setq ent (car (entsel)))
  3.              (setq pt1 (getpoint "\n1st point: "))
  4.              (setq pt2 (getpoint "\n2nd point: "))
  5.         )
  6.         (vla-transformby
  7.             (vlax-ename->vla-object ent)
  8.             (vlax-tmatrix (matrix (trans pt1 1 0) (trans pt2 1 0)))
  9.         )
  10.     )
  11.     (princ)
  12. )