Author Topic: Mini-Matrix stuff : rotate, flip, move  (Read 2309 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Mini-Matrix stuff : rotate, flip, move
« on: December 06, 2010, 08:03:43 PM »
Quite often I have a need to draw a shape based on database information .. perhaps to extrude it, perhaps something else.

The methodology needs to work in any UCS.

.. and I sometimes need to rotate, flip, move the shape.

Here's some stuff that helps me define the shape vertex-points before they are drawn.

Say I have a shape based on this data :
Code: [Select]
(setq VectorList '((0  0)
                   (10 0)
                   (10 3)
                   (2  3)
                   (2  5)
                   (0  5)
                  )
)



and I want to draw it in a manner similar to this :

Note that each of the shapes are drawn by the code snips that follow.  

The following code does the job.
I won't provide much in the way of notes, but working through the examples will help understand what I'm doing.

Library stuff:
Code: [Select]
;;------------------------------------------------------------
;;
;;;  Convert Angles from DEGREES to RADIANS

(defun kdub:dtr (ang) (* pi (/ ang 180.0)))
;;------------------------------------------------------------
;;
;;; -- Function KDUB:VEC:ZeroAxisFuzz
;;; Recursively eliminates floating point precision problems when values
;;; IN POINT LISTS are approximately 0.
;;; Arguments [Type]:
;;;   VectorList = List or nestedl List of floating point numbers [LIST]
;;; Return [Type]:
;;;   > Corrected list [LIST]
;;
(defun kdub:vec:zeroaxisfuzz (VectorList)
  (mapcar '(lambda (a)
             (if (= 'list (type a))
               (kdub:vec:zeroaxisfuzz a)
               (if (and (> a (- 1E-13)) (< a 1E-13))
                 0.0
                 a
               )
             )
           )
          VectorList
  )
)
;;------------------------------------------------------------
;;

;; Rotate around 0,0
(defun kdub:rotateVector (v ang /)
  ;; v : vector ie: point relative to 0,0
  ;; ang : anglr in radians to rotate
  (mapcar (function (lambda (r) (apply '+ (mapcar '* r v))))
          (list (list (cos ang) (- (sin ang)) ) (list (sin ang) (cos ang) ))
  )
)
;;------------------------------------------------------------
;;


Some in Line testing of points
Code: [Select]
;;------------------------------------------------------------
;;

(setq ang (kdub:dtr 30.))

(kdub:rotateVector '(10 0 ) ang) ;;-> (8.66025 5.0)
(kdub:rotateVector '(10 3) ang) ;;-> (7.16025 7.59808)
(kdub:rotateVector '(2 3) ang)  ;;-> (0.232051 3.59808)
(kdub:rotateVector '(2 5) ang)  ;;-> (-0.767949 5.33013)
(kdub:rotateVector '(0 5) ang)  ;;-> (-2.5 4.33013)

(setq ang (kdub:dtr 00))
(kdub:rotateVector '(1.23 34.67 ) ang) ;;-> (1.23 34.67)
(setq ang (kdub:dtr 360))
(kdub:rotateVector '(1.23 34.67 ) ang) ;;-> (1.23 34.67)


;;------------------------------------------------------------
;;

Some in Line testing of point-Lists
Code: [Select]
;;------------------------------------------------------------
;;

(setq ang (kdub:dtr 30))
(kdub:vec:zeroaxisfuzz (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) VectorList))

;;-> ((0.0 0.0) (8.66025 5.0) (7.16025 7.59808) (0.232051 3.59808) (-0.767949 5.33013) (-2.5 4.33013))

(setq ang (kdub:dtr 90))
 (kdub:vec:zeroaxisfuzz  (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) VectorList))

;;-> ((0.0 0.0) (0.0 10.0) (-3.0 10.0) (-3.0 2.0) (-5.0 2.0) (-5.0 0.0))

(setq ang (kdub:dtr 180))
 (kdub:vec:zeroaxisfuzz  (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) VectorList))

;;-> ((0.0 0.0) (-10.0 0.0) (-10.0 -3.0) (-2.0 -3.0) (-2.0 -5.0) (0.0 -5.0))

(setq ang (kdub:dtr 270))
 (kdub:vec:zeroaxisfuzz  (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) VectorList))

;;-> ((0.0 0.0) (0.0 -10.0) (3.0 -10.0) (3.0 -2.0) (5.0 -2.0) (5.0 0.0))


Moves and Flips :
Code: [Select]
;;------------------------------------------------------------
;; Move VectorList  X,Y
(defun kdub:Move_XY_Vectorlist (VectorList MoveMatrix/)
  (mapcar (function (lambda (x) (mapcar '+ x MoveMatrix/))) VectorList)
)


(setq moveMatrix  '(3 -2) )
(kdub:Move_XY_Vectorlist Vectorlist moveMatrix)
;;-> ((3 -2) (13 -2) (13 1) (5 1) (5 3) (3 3))

;;------------------------------------------------------------
;; Flip VectorList X
(defun kdub:Flip_X_Vectorlist (VectorList /)
  (cons
    (car VectorList)
    (mapcar (function (lambda (pt) (mapcar '* '(-1. 1.) pt))) (reverse (cdr VectorList)))
  )
)

(kdub:Flip_X_Vectorlist Vectorlist)

;;-> ((0 0) (0.0 5.0) (-2.0 5.0) (-2.0 3.0) (-10.0 3.0) (-10.0 0.0))

;;------------------------------------------------------------
;; Flip VectorList Y
(defun kdub:Flip_Y_Vectorlist (VectorList /)
  (cons
    (car VectorList)
    (mapcar (function (lambda (pt) (mapcar '* '(1. -1.) pt))) (reverse (cdr VectorList)))
  )
)

(kdub:Flip_Y_Vectorlist Vectorlist)

;;-> ((0 0) (0.0 -5.0) (2.0 -5.0) (2.0 -3.0) (10.0 -3.0) (10.0 0.0))


;;------------------------------------------------------------
;; Flip VectorList X and Y is the same as Rotate 180


And some drawing stuff :
Code: [Select]
;;------------------------------------------------------------
;;
(defun kdub:draw_LightWeightPolyline (VertexList la closeflag / ucsZNormal elev PolyObj)
  (setq ucsZNormal (trans '(0 0 1) 1 0 t)
        elev       (caddr (trans (car VertexList) 1 ucsZNormal))
  )
  (setq PolyObj (vlax-invoke kglobal:modelspace
                             'addLightWeightPolyline
                             (apply 'append
                                    (mapcar '(lambda (pt)
                                               (setq pt (trans pt 1 ucsZNormal))
                                               (list (car pt) (cadr pt))
                                             )
                                            VertexList
                                    )
                             )
                )
  )
  (vla-put-elevation PolyObj elev)
  (if la
    (vla-put-layer PolyObj la)
  )
  (vla-put-normal PolyObj (vlax-3d-point ucsZNormal))
  (vla-put-closed PolyObj closeflag)
  PolyObj
)
;;------------------------------------------------------------
;;

and the testing :
These 7 snippets draw the 7 shapes shown in the second piccy above.
Code: [Select]
(setq point-list Vectorlist)

(kdub:draw_LightWeightPolyline point-list nil :vlax-true)
(kdub:draw_LightWeightPolyline (kdub:Flip_X_Vectorlist point-list) nil :vlax-true)
(kdub:draw_LightWeightPolyline (kdub:Flip_y_Vectorlist point-list) nil :vlax-true)

;;----------------------------
(setq ang (kdub:dtr 90))
(kdub:draw_LightWeightPolyline
  (kdub:vec:zeroaxisfuzz
    (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) point-list)
  )
  nil
  :vlax-true
)

;;----------------------------
(setq ang (kdub:dtr 180))
(kdub:draw_LightWeightPolyline
  (kdub:vec:zeroaxisfuzz
    (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) point-list)
  )
  nil
  :vlax-true
)

;;----------------------------
(setq ang (kdub:dtr 22.3))
(kdub:draw_LightWeightPolyline
  (kdub:vec:zeroaxisfuzz
    (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) point-list)
  )
  nil
  :vlax-true
)

;;---------------------------- The ultimate Test
(setq ang (kdub:dtr 22.3))
(setq moveMatrix  '(3 -2) )
(kdub:draw_LightWeightPolyline
  (kdub:vec:zeroaxisfuzz
    (kdub:Move_XY_Vectorlist
      (mapcar (function (lambda (pt) (kdub:rotateVector pt ang))) point-list)
      moveMatrix
    )
  )
  nil
  :vlax-true
)
;;----------------------------

Any questions, just yell.
enjoy
« Last Edit: December 06, 2010, 08:08:44 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Mini-Matrix stuff : rotate, flip, move
« Reply #1 on: December 06, 2010, 09:30:12 PM »
Nice work Kerry! Thanks for sharing  :-)

These are the subs I use to transform objects using transformation matrices:

<< Code Removed, can be found here >>
« Last Edit: December 09, 2010, 02:51:08 PM by Lee Mac »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Mini-Matrix stuff : rotate, flip, move
« Reply #2 on: December 07, 2010, 12:05:29 AM »

Thanks Lee.

My case is unique in that I don't have an object to translate/transpose/transform.
As I'm dealing with points and lists the situation is slightly different.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Mini-Matrix stuff : rotate, flip, move
« Reply #3 on: December 09, 2010, 10:03:07 PM »
Updated code and added to my site  :-)