Author Topic: Convert GEOMCAL functions to LISP  (Read 14794 times)

0 Members and 1 Guest are viewing this topic.

gschmidt

  • Guest
Convert GEOMCAL functions to LISP
« on: March 03, 2011, 05:26:07 AM »
Hi,

I have an existing LISP build for AutoCAD which uses GEOMCAL functions.
I want to run this LISP also in BricsCAD however there is no GEOMCAL build in.

Now I want to convert 4 functions to LISP:

(cal "NOR(p1,p2)") =
- returns the normal vector to the vector between p1 and p2

(cal "ANG(apex,p1,p2)") =
-returns the angle (in degrees or decimal degrees) between the lines "apex-p1" and "apex-p2".

(cal "ROT(rp,ax1,ax2,angle)") =
- returns the point that is the result of the 3D rotation of point rp around the axis through ax1 and ax2 with the given rotation angle. The angle is given,in degrees and/or decimal degrees and is measured counterclockwise.

(cal "PLD(p1,p2,distance)") =
-returns the coordinate point that is on the line between p1 and p2 in the given distance from p1


Greetzzz,

Gerben




 





 

VVA

  • Newt
  • Posts: 166
Re: Convert GEOMCAL functions to LISP
« Reply #1 on: March 04, 2011, 11:21:27 AM »
(cal "NOR(p1,p2)") = (vec1 p1 p2) (see Norm2Pts by gile below)
(cal "PLD(p1,p2,distance)") = (3d_polarp p1 p2 l)
Code: [Select]
;;; VUNIT
;;; Returns the single unit vector of a vector
;;;
;;; Argument = a vector
(defun vunit (v)
  ((lambda (l)
     (if (/= 0 l)
       (mapcar (function (lambda (x) (/ x l))) v)
     )
   )
    (distance '(0 0 0) v)
  )
)
;;; VEC1
;;; Returns the single unit vector from p1 to p2
;;;
;;; Arguments = two points
(defun vec1 (p1 p2)
  (vunit (mapcar '- p2 p1))
)
(defun 3d_polarp (P1 P2 L)
 (setq L (/ L (distance P1 P2)))
 (mapcar '+ P1 (mapcar '(lambda (V) (* V L)) (mapcar '- P2 P1)))
)

« Last Edit: March 07, 2011, 12:07:56 PM by VVA »

Lee Mac

  • Seagull
  • Posts: 12195
  • London, England
Re: Convert GEOMCAL functions to LISP
« Reply #2 on: March 04, 2011, 12:22:53 PM »
For

(cal "ANG(apex,p1,p2)") =
-returns the angle (in degrees or decimal degrees) between the lines "apex-p1" and "apex-p2".

Would this be what is needed?

Code: [Select]
(defun LM:GetApexAngle ( p1 p2 p3 )
  (min (setq a (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi))) (- (+ pi pi) a))
)

And in degrees:

Code: [Select]
(* 180. (/ (LM:GetApexAngle <p1> <p2> <p3>) pi))
 Returns the acute angle between three points.

gile

  • Water Moccasin
  • Posts: 2204
  • Marseille, France
Re: Convert GEOMCAL functions to LISP
« Reply #3 on: March 04, 2011, 04:25:21 PM »
Hi,

Quote
(cal "ROT(rp,ax1,ax2,angle)") =
- returns the point that is the result of the 3D rotation of point rp around the axis through ax1 and ax2 with the given rotation angle. The angle is given,in degrees and/or decimal degrees and is measured counterclockwise.

This one needs to use a rotation matrix.

Code: [Select]
;; MXV
;; Apply a transformation matrix to a vector -Vladimir Nesterovsky-

(defun mxv (m v)
  (mapcar (function (lambda (r) (apply '+ (mapcar '* r v)))) m)
)

(defun rot3d (pt ax1 ax2 ang)
  ((lambda (v)
     (trans
       (mxv
(list (list (cos ang) (- (sin ang)) 0.)
       (list (sin ang) (cos ang) 0.)
       '(0. 0. 1.)
)
(trans pt 0 v)
       )
       v
       0
     )
   )
    (mapcar '- ax2 ax1)
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12195
  • London, England
Re: Convert GEOMCAL functions to LISP
« Reply #4 on: March 04, 2011, 06:22:37 PM »
I think:

Code: [Select]
(defun rot3D ( pt ax1 ax2 ang )
  (
    (lambda ( m n / o v )
      (setq o (trans ax1 0 n)
            o (list (car o) (cadr o) 0.)
            v (mapcar '- o (mxv m o))
      )
      (trans (mapcar '+ (mxv m (trans pt 0 n)) v) n 0)
    )
    (list
      (list (cos ang) (- (sin ang)) 0.)
      (list (sin ang)    (cos ang)  0.)
      (list     0.        0.        1.)
    )
    (mapcar '- ax2 ax1)
  )
)   

;; MXV
;; Apply a transformation matrix to a vector -Vladimir Nesterovsky-

(defun mxv (m v)
  (mapcar (function (lambda (r) (apply '+ (mapcar '* r v)))) m)
)

Since the Coordinate frame of the Rotation Axis is defined relative to the origin.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Convert GEOMCAL functions to LISP
« Reply #5 on: March 04, 2011, 07:13:34 PM »


Lee, you may want to have another look at your ANG conversion, for a couple of reasons ..

The expected input is <Apex> <P1> <P2>
and the expected output is the enclosed angle, measured anti-clockwise, between the vectors Apex->P1 and Apex->P2.

BTW, your routine does not return an acute angle (more than 0, less than 90) as you indicate,
it will return an angle  between 0 and 180

ps: unless I've misread your code.

Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

gile

  • Water Moccasin
  • Posts: 2204
  • Marseille, France
Re: Convert GEOMCAL functions to LISP
« Reply #6 on: March 04, 2011, 07:39:11 PM »
You're right Lee, I forgot some statements :oops:

Code: [Select]
(defun rot3d (pt ax1 ax2 ang)
  ((lambda (v)
     (mapcar '+
     ax1
     (trans
       (mxv
(list (list (cos ang) (- (sin ang)) 0.)
       (list (sin ang) (cos ang) 0.)
       '(0. 0. 1.)
)
(trans (mapcar '- pt ax1) 0 v)
       )
       v
       0
     )
     )
   )
    (mapcar '- ax2 ax1)
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12195
  • London, England
Re: Convert GEOMCAL functions to LISP
« Reply #7 on: March 04, 2011, 07:40:42 PM »
BTW, your routine does not return an acute angle (more than 0, less than 90) as you indicate,
it will return an angle  between 0 and 180

Apologies - no it isn't 'acute', I meant the angle opposite to the reflex angle, which of course could be obtuse or acute...

From the description in your post, the order of the arguments in my original code is incorrect I suppose, I assumed:

Code: [Select]
(LM:GetApexAngle <p1> <Apex> <p3>)
This could of course be ammended by switching the symbols around:

Code: [Select]
(defun LM:GetApexAngle ( apex p1 p2 )
  (min (setq a (rem (+ pi pi (- (angle apex p1) (angle apex p2))) (+ pi pi))) (- (+ pi pi) a))
)
« Last Edit: March 04, 2011, 07:51:24 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12195
  • London, England
Re: Convert GEOMCAL functions to LISP
« Reply #8 on: March 04, 2011, 07:50:01 PM »
You're right Lee, I forgot some statements :oops:

Code: [Select]
(defun rot3d (pt ax1 ax2 ang) < .. snip .. >

Much better than my code!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Convert GEOMCAL functions to LISP
« Reply #9 on: March 04, 2011, 08:01:19 PM »
< .. >
This could of course be ammended by switching the symbols around:

Code: [Select]
(defun LM:GetApexAngle ( apex p1 p2 )
  < .. >
)

Yep.

Now you just need this :
Quote
and the expected output is the enclosed angle, measured anti-clockwise, between the vectors Apex->P1 and Apex->P2.


Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Lee Mac

  • Seagull
  • Posts: 12195
  • London, England
Re: Convert GEOMCAL functions to LISP
« Reply #10 on: March 04, 2011, 08:16:46 PM »
< .. >
This could of course be ammended by switching the symbols around:

Code: [Select]
(defun LM:GetApexAngle ( apex p1 p2 )
  < .. >
)

Yep.

Now you just need this :
Quote
and the expected output is the enclosed angle, measured anti-clockwise, between the vectors Apex->P1 and Apex->P2.

I hadn't read the help file, so I assumed they wanted the smaller of the angles (as I decribed, the angle opposite the reflex angle).

If not, it simplifies my code somewhat:

Code: [Select]
(defun LM:GetApexAngle ( apex p1 p2 )
  (- (+ pi pi) (rem (+ pi pi (- (angle apex p1) (angle apex p2))) (+ pi pi)))
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Convert GEOMCAL functions to LISP
« Reply #11 on: March 04, 2011, 08:47:41 PM »
< .. >  it simplifies my code somewhat:

Code: [Select]
(defun LM:GetApexAngle ( apex p1 p2 )
  (- (+ pi pi) (rem (+ pi pi (- (angle apex p1) (angle apex p2))) (+ pi pi)))
)

That looks like is should do very nicely.  :wink:
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

VovKa

  • Swamp Rat
  • Posts: 1087
  • Ukraine
Re: Convert GEOMCAL functions to LISP
« Reply #12 on: March 05, 2011, 04:48:02 AM »
there's an excellent trig library around the internet

readme translated by google
Quote
Author: Gregory Cherevko
http://www.elecran.com.ua/
===========================
No liability for the use of these texts and the possible errors the authors did not carry.

General comments
functions are written in AutoLisp
All calculations are made by mathematical methods (vector algebra, analytic geometry) without using auxiliary AutoCAD entities and their analysis.
Global variables
Many library functions refer to two global variables
The variable $ P000 has the value '(0.0 0.0 0.0)
$ Dopusk has a value of 0.00001 - contains the error value is used for various comparisons (such as coordinates of points). He just needed to cope with the accumulated error in the calculations.
To initialize variables in the start of your program to insert the line
(Setq $ P000 '(0.0 0.0 0.0)
$ Dopusk 0.00001
)

Naming of variables, parameters
In the parameters of these functions are frequently used variable names.
P, P1, P2, P3 .... - Points
Pc - the central point of an arc or a circle
L, Lx, Ly, Lz - length
Wekt, Wekt1, Wekt2, WektX, WektY, WektZ, W, W1, W2-vector (the list of 3 numbers)
WNorm - normal vector. In terms of AutoCAD it comes to squeezing the direction vector, which all primitives stored under 210 associative code.
Ang - angle
R, R1, R2 - radius
================================================== ====================
Calculations of the arcs
 
1. Distance from a point in the plane of the arc to arc (1.zip)
2. Calculating the arc length (2.zip)
3. Calculation of the central angle of arc (3.zip)
4. Computation of the mid-point arc (4.zip)
5. Calculation of the center of the arc (the arc is defined by 2 points and the direction
tangent to the 1-th point) (5.zip)

Analysis functions belonging and intersection elements
 
6. Accessory points on the line, set point and the vector (15.zip)
7. Affiliation point interval (16.zip)
8. Affiliation point arc (all points in the same plane!) (17.zip)
9. Affiliation point arc (points in different planes!) (18.zip)
10. Affiliation arc arc (19.zip)

Function calculating the intersection of objects in space
 
11. The intersection of 2 circles given center and radius,
coplanar (10.zip)
12. The intersection of 2 circles given center and radius,
located in a given plane (11.zip)
13. Point of intersection of the line and plane (12.zip)
14. Point of intersection of the circle with the plane (13.zip)
15. Calculation of the intersection line of planes (14.zip)
16. Intersection of line and circle, lying in one plane (8.zip)
17. Intersection of line and circle, randomly located (9.zip)

General purpose functions
 
18. Vector product of vectors (20.zip)
19. Vector normal to the plane. The plane defined by 3 points (21.zip)
20. Calculation of the unit vector (unit vector) (22.zip)
21. Reduction of the vector to the length of 1000 (long vector) (23.zip)
22. Point lying on the point P1 at a distance L in the direction of the point P2 (24.zip)
23. Point lying on P1 at a distance L along the vector Wekt (25.zip)
24. Point, offset by a 2-m vectors (26.zip)
25. The condition of orthogonality (27.zip)
26. The angle between the vectors (29.zip)
27. The angle between the vectors in a clockwise direction from the normal (30.zip)
28. Vector angle with respect to this part of the normal (31.zip)
29. The nearest point from a given point of the segment (32.zip)
30. The distance from the point to the segment (33.zip)
31. Rotate the point around the axis (34.zip)
32. Projection of a point on the line (line set-point vector) (35.zip)
33. Projection of a point on the line (line given two points) (36.zip)
34. Projection of a point on the plane (37.zip)
35. The distance between the planes (38.zip)

Function of spatial geometry
 
36. Calculation of the point of contact from the point of the circle (6.zip)
37. The calculation of the points of contact to the 2-m circles (7.zip)
================================================== =======================

SOFITO_SOFT

  • Guest
Re: Convert GEOMCAL functions to LISP
« Reply #13 on: March 05, 2011, 11:30:40 AM »
there's an excellent trig library around the internet
readme translated by google
Quote
Author: Gregory Cherevko
http://www.elecran.com.ua/
===========================

Wonderful  :lol: :lol: :lol:
Thanks 
Greetings  :-)

SOFITO_SOFT

  • Guest
Re: Convert GEOMCAL functions to LISP
« Reply #14 on: March 05, 2011, 01:27:52 PM »
Hello everyone
LIB Magnifica features ... very compact.
But I can not find a function that is called by some other function.
Can it be an oversight? or am I too dumb?
function that occurs is:3d_cosw1w2

I think it is the cosine of the angle formed by 2 3dvectors. :|
I think it might be as follows:

Code: [Select]
(defun 3d_cosw1w2 (P1 P2 /)
   (cos (3d_angw1w2 p1 p2))
)

Using other functions which are included 3d_angw1w2.
I hope not to screw up.
Greetings from Madrid and thanks again. :-)