乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 69|回复: 3

[编程交流] 交点和

[复制链接]

3

主题

8

帖子

5

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-5 16:31:52 | 显示全部楼层 |阅读模式
你好
使用marko_ribar lisp来自:
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/to-get-intersection-between-a-line-and-a-3dface/td-p/2745633/page/3
 
我制作了一个版本,在垂直线与3dface的交点处插入一个点。我有许多看起来像三角形的3dFaces。我使用lisp制作它们:
http://paulbourke.net/papers/triangulate/Triangulator.LSP
 
我的lisp有时能工作,有时会打印错误。
 
谢谢你的帮助
  1. ;_ilt  = intersection line and 3dface
  2. ;by marko_ribar
  3. ;link:        https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/to-get-intersection-between-a-line-and-a-3dface/td-p/2745633/page/3
  4. ;
  5. (defun _ilt ( p1 p2 t1 t2 t3 / v^v unit Coplanar-p ptinsidetriangle-p ptontriangle-p ptonline-p _ilp nor o )
  6. (defun v^v ( u v )
  7.    (list
  8.      (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
  9.      (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))
  10.      (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))
  11.    )
  12. )
  13. (defun unit ( v )
  14.    (mapcar '(lambda ( x ) (/ x (distance '(0.0 0.0 0.0) v))) v)
  15. )
  16. (defun Coplanar-p ( p1 p2 p3 p4 )
  17.    (
  18.      (lambda ( n1 n2 )
  19.        (equal (v^v n1 n2) '(0.0 0.0 0.0) 1e-
  20.      )
  21.      (v^v (mapcar '- p1 p2) (mapcar '- p1 p3))
  22.      (v^v (mapcar '- p1 p2) (mapcar '- p1 p4))
  23.    )
  24. )
  25. (defun ptinsidetriangle-p ( pt p1 p2 p3 )
  26.    (if
  27.      (and
  28.        (Coplanar-p pt p1 p2 p3)
  29.        (not
  30.          (or
  31.            (inters pt p1 p2 p3)
  32.            (inters pt p2 p1 p3)
  33.            (inters pt p3 p1 p2)
  34.          )
  35.        )
  36.        (not
  37.          (or
  38.            (> (+ (distance pt p1) (distance pt p2)) (+ (distance p3 p1) (distance p3 p2)))
  39.            (> (+ (distance pt p2) (distance pt p3)) (+ (distance p1 p2) (distance p1 p3)))
  40.            (> (+ (distance pt p3) (distance pt p1)) (+ (distance p2 p3) (distance p2 p1)))
  41.          )
  42.        )
  43.      )
  44.      T
  45.      nil
  46.    )
  47. )
  48. (defun ptontriangle-p ( pt p1 p2 p3 )
  49.    (if
  50.      (or
  51.        (equal (distance p1 p2) (+ (distance pt p1) (distance pt p2)) 1e-7)
  52.        (equal (distance p2 p3) (+ (distance pt p2) (distance pt p3)) 1e-7)
  53.        (equal (distance p1 p3) (+ (distance pt p1) (distance pt p3)) 1e-7)
  54.      )
  55.      T
  56.      nil
  57.    )
  58. )
  59. (defun ptonline-p ( pt p1 p2 )
  60.    (equal (distance p1 p2) (+ (distance pt p1) (distance pt p2)) 1e-7)
  61. )
  62. (defun _ilp ( p1 p2 o nor / p1p p2p op tp pp p )
  63.    (if (not (equal (v^v nor (unit (mapcar '- p2 p1))) '(0.0 0.0 0.0) 1e-7))
  64.      (progn
  65.        (setq p1p (trans p1 0 (v^v nor (unit (mapcar '- p2 p1))))
  66.              p2p (trans p2 0 (v^v nor (unit (mapcar '- p2 p1))))
  67.              op  (trans o 0 (v^v nor (unit (mapcar '- p2 p1))))
  68.              op  (list (car op) (cadr op) (caddr p1p))
  69.              tp  (polar op (+ (* 0.5 pi) (angle '(0.0 0.0 0.0) (trans nor 0 (v^v nor (unit (mapcar '- p2 p1)))))) 1.0)
  70.        )
  71.        (if (inters p1p p2p op tp nil)
  72.          (progn
  73.            (setq p (trans (inters p1p p2p op tp nil) (v^v nor (unit (mapcar '- p2 p1))) 0))
  74.            p
  75.          )
  76.          nil
  77.        )
  78.      )
  79.      (progn
  80.        (setq pp (list (car (trans p1 0 nor)) (cadr (trans p1 0 nor)) (caddr (trans o 0 nor))))
  81.        (setq p (trans pp nor 0))
  82.        p
  83.      )
  84.    )
  85. )
  86. (setq nor (unit (v^v (mapcar '- t3 t1) (mapcar '- t2 t1))))
  87. (setq o t1)
  88. (if (_ilp p1 p2 o nor)
  89.    (if
  90.      (and
  91.        (or
  92.          (ptinsidetriangle-p (_ilp p1 p2 o nor) t1 t2 t3)
  93.          (ptontriangle-p (_ilp p1 p2 o nor) t1 t2 t3)
  94.        )
  95.        (ptonline-p (_ilp p1 p2 o nor) p1 p2)
  96.      )
  97.      (_ilp p1 p2 o nor)
  98.      nil
  99.    )
  100.    nil
  101. )
  102. )
  103. ;;
  104. ;;BECAUSE THE 3DFACE HAVE 4 VERTICES (2 the same) WE HAVE TO REMOVE DUPLICATED COMPONENTS
  105. ;;
  106. ;; Unique  -  Lee Mac
  107. ;; Returns a list with duplicate elements removed.
  108. (defun remove_doubles  (lst /)
  109. (vl-load-com)
  110. (if lst
  111.    (cons (car lst) (remove_doubles (vl-remove (car lst) lst)))))
  112. ;;
  113. ;;Main program
  114. (defun c:bum ()
  115. (print "pick 3dFace")
  116. (setq ssSelections (ssget))
  117. (setq Point (getpoint "\nPICK A POINT"))
  118. (setq Point2 (list        (nth 0 Point) ; Point i Point2 = line of 500 length (Line that crosses 3dface)
  119.                                 (nth 1 Point)
  120.                                 500))
  121. (repeat        (setq        intCount (sslength ssSelections))
  122.         (setq        intCount     (1- intCount)
  123.                         entSelection (ssname ssSelections intCount)
  124.                         lstEntity    (entget entSelection))
  125.         ;3DFACE vertices
  126.         (setq P10         (list         (nth 1 (assoc 10 lstEntity))
  127.                                                 (nth 2 (assoc 10 lstEntity))
  128.                                                 (nth 3 (assoc 10 lstEntity)) )
  129.                   P11        (list         (nth 1 (assoc 11 lstEntity))
  130.                                                 (nth 2 (assoc 11 lstEntity))
  131.                                                 (nth 3 (assoc 11 lstEntity)) )
  132.                   P12        (list         (nth 1 (assoc 12 lstEntity))
  133.                                                 (nth 2 (assoc 12 lstEntity))
  134.                                                 (nth 3 (assoc 12 lstEntity)) )
  135.                   P13        (list         (nth 1 (assoc 13 lstEntity))
  136.                                                 (nth 2 (assoc 13 lstEntity))
  137.                                                 (nth 3 (assoc 13 lstEntity)) ))
  138. ;List of vertices with one (duplicate) deleted
  139. (setq ListVtx        (remove_doubles (list P10 P11 P12 P13) ))
  140. ;Others 3 vertices
  141. (setq P100 (nth 0 ListVtx)
  142.           P101 (nth 1 ListVtx)
  143.           P102 (nth 2 ListVtx))
  144. ;Making 3dPoint
  145. (entmake (list '(0 . "POINT") (cons 10 (_ilt Point Point2 P100 P101 P102)) ))
  146. );end repeat
  147. );end bum       
  148.                
回复

使用道具 举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-5 17:01:56 | 显示全部楼层
我以前很幸运做过这种测试:
 
  1. ;    3 Points To 210 Extrusion Direction (LeeMac)
  2. (defun normal ( p1 p2 p3 )
  3. (defun vxs ( v s )
  4.    (mapcar '(lambda ( n ) (* n s)) v))
  5. (defun nrm ( v )
  6.    (sqrt (apply '+ (mapcar '(lambda ( n ) (* n n)) v))))
  7. (defun one ( v )
  8.    (vxs v (/ 1.0 (nrm v))))
  9. (defun vcv ( u v )
  10.    (list
  11.      (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
  12.      (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))
  13.      (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))))
  14. (one (vcv (mapcar '- p3 p2) (mapcar '- p3 p1))))
  15. (defun c:test (/ p10 p11 f10 f11 f12 ucs pt1 pt2)
  16. (setq ld (entget line_ename)
  17.       p10 (cdr (assoc 10 ld))
  18.       p11 (cdr (assoc 11 ld)))
  19. (setq fd (entget face_ename)
  20.       p10 (cdr (assoc 10 ld))
  21.       p11 (cdr (assoc 11 ld))
  22.       p12 (cdr (assoc 12 ld)))
  23. (setq ucs (normal f10 f11 f12)
  24.        pt1 (trans p10 0 ucs)
  25.        pt2 (trans p11 0 ucs))
  26. (if (setq ip
  27.        (inters pt1 pt2
  28.               (list (car pt1) (cadr pt1) 0)
  29.               (list (car pt2) (cadr pt2) 0) nil))
  30.        (prin1 ip)
  31.        (alert "Line Does Not Intersect This Plane"))
  32. (prin1))

 
假设3dface 12和13相等
如果该线与ucs平面平行,则它将永远不会相交(您的错误可能来自此)
 
基本上找到了转换为3个面点ucs的2条线的(inters)
 
通常我会将ip转换为WCS。
 
HTH-David
回复

使用道具 举报

3

主题

8

帖子

5

银币

初来乍到

Rank: 1

铜币
15
发表于 2022-7-5 17:22:57 | 显示全部楼层
谢谢你的回答。
我认为在我的代码中,有时函数_ilt print nil,所以它不能给出一个点。
回复

使用道具 举报

lrm

1

主题

257

帖子

282

银币

限制会员

铜币
-13
发表于 2022-7-5 17:33:26 | 显示全部楼层
我想我会分享我写的这段代码,以确定任何直线(由2个点定义)与三维面定义的平面的交点。它会检查直线是否与平面平行,但不会检查点是否在面边界内。
 
  1. (defun c:FaceIntr (/)
  2. ;  finds the point of intersection of a line defined by two points and a plane defined by
  3. ;  a 3DFace
  4. ;  L. Minardi  3/28/2017  
  5. (princ "\nPlease select 3DFACE and press ENTER.")
  6. (setq        ss    (ssget)   ; get data for 3Dface
  7. en    (ssname ss 0)
  8. edata (entget en)
  9. )
  10. (setq        p1   (getpoint "\nLine Start:")  ; get two line points
  11. p2   (getpoint p1 "\nLine End")
  12. p1p2 (mapcar '- p2 p1)                ; vector from p1 to p2
  13. )
  14. (setq        fp1 (cdr (assoc 10 edata))        ;three corners of the 3DFACE
  15. fp2 (cdr (assoc 11 edata))
  16. fp3 (cdr (assoc 12 edata))
  17. )
  18. (setq N (cross fp12 fp23))                ; normal to face
  19. (setq        fp1p1 (mapcar '- p1 fp1)        ; vector from face to line
  20. p1p2  (mapcar '- p2 p1)         ; vector paralled to line
  21. )                                       
  22.                                 ; determine if line is parallel to plane
  23. (setq Nxp1p2 (cross N p1p2))                ; N cross p1p2
  24. (setq Ntp1p2 (* (distance '(0 0 0) N) (distance '(0 0 0) p1p2)))
  25.                                 ; magnitude N times magnitude p1p2
  26. (setq
  27.    sinang (/ (distance '(0 0 0) Nxp1p2) Ntp1p2)  ; sine angle between normal and line p1p2
  28. )
  29. (if (equal sinang 1.0 0.0001)
  30.    (princ "\nNo intersection, the line is parallel to the face.")
  31.    (progn
  32.      (setq tk (- (/ (dot N fp1p1) (dot N p1p2))) ; value of parameter t at intersection
  33.      )                                        ; intersection point of line and face using parametric definition of a line  
  34.      (setq PInt (mapcar '+
  35.                  p1
  36.                  (mapcar '* (mapcar '- p2 p1) (list tk tk tk))
  37.          )
  38.      )
  39.      (command "point" Pint)
  40.      (command "line" p2 Pint "")
  41.      (setq s (distance p2 Pint))
  42.      (princ "\n The intersection point is located at: ")
  43.      (princ Pint)
  44.      (princ
  45. "\nThe distance from line end point to the intersection is: "
  46.      )
  47.      (princ s)
  48.    )
  49. )                                        ; end if  
  50. (princ)
  51. )                                        ;end Face-Intr
  52. ;;; Compute the cross product of 2 vectors
  53. (defun cross (a b / crs)
  54. (setq        crs (list
  55.       (- (* (nth 1 a) (nth 2 b))
  56.          (* (nth 1 b) (nth 2 a))
  57.       )
  58.       (- (* (nth 0 b) (nth 2 a))
  59.          (* (nth 0 a) (nth 2 b))
  60.       )
  61.       (- (* (nth 0 a) (nth 1 b))
  62.          (* (nth 0 b) (nth 1 a))
  63.       )
  64.     )                                ;end list
  65. )                                        ;end setq c
  66. )                                        ;end cross
  67. ;;; Compute the dot product of 2 vectors a and b
  68. (defun dot (a b / dd)
  69. (setq dd (mapcar '* a b))
  70. (setq dd (+ (nth 0 dd) (nth 1 dd) (nth 2 dd)))
  71. )                                        ;end of dot
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-8-21 01:50 , Processed in 0.357584 second(s), 61 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表