### Author Topic: [XDrX-PlugIn(4)] Draw the center lines of two SPLINEs  (Read 719 times)

0 Members and 1 Guest are viewing this topic.

• Bull Frog
• Posts: 424
##### [XDrX-PlugIn(4)] Draw the center lines of two SPLINEs
« on: November 25, 2023, 05:34:43 AM »

https://www.theswamp.org/index.php?topic=58736.0

How to determine the NurbCurve3d center between two NurbCurve3ds?

Kean Walmsley：

I chose to interpret this in the following way: given two NurbCurve3d objects, create a NurbCurve3d that sits exactly between the two. NurbCurve3d are “AcGe” classes – which means they’re non-graphical – so I’ve broadened the scope to deal with Splines as they have a close relationship with the NurbCurve3d class (in fact there are handy methods to convert between the two classes). Here’s some more information on NURBS, for those with an interest.

I’ve also chosen to deal with one fairly specific case: one where the Splines (and the equivalent NurbCurve3ds) have exactly the same degree and period as well as the same number of control points, knots and weights. Which makes things much easier: while it should be possible – with enough effort and code – to find a way to parameterize the two curves and create points on a curve between the two – fitting a curve along those points – I’ve chosen not to address that more general problem. At least not in this post.

So what I’ve done is actually very simple: between two similarly-sized NurbCurve3d objects, we step through the three primary collections of each – control points, knots and weights – and create three new collections for the new curve that contain the “average” values of the data from the two source objects. I’m far from being a NURBS expert, but I believe this approach to be reasonable for this particular category of Spline (at least it seems to work well enough).

Bear in mind that this also won’t work with Splines that are defined by fit – rather than control – points. At least I don’t expect it to.

Code - Auto/Visual Lisp: [Select]
1. (defun c:tt ()
2.   (defun _check-curve (spl1 spl2)
3.     (and (= (xdrx-getpropertyvalue g1 "degree")
4.             (xdrx-getpropertyvalue g2 "degree")
5.          )
6.          (= (xdrx-getpropertyvalue g1 "isPeriodic")
7.             (xdrx-getpropertyvalue g2 "isPeriodic")
8.          )
9.          (= (xdrx-getpropertyvalue g1 "numcontrolpoints")
10.             (xdrx-getpropertyvalue g2 "numcontrolpoints")
11.          )
12.          (= (xdrx-getpropertyvalue g1 "numKnots")
13.             (xdrx-getpropertyvalue g2 "numKnots")
14.          )
15.          (= (xdrx-getpropertyvalue g1 "numWeights")
16.             (xdrx-getpropertyvalue g2 "numWeights")
17.          )
18.     )
19.   )
20.   (if (and (setq e1
21.                   (car
22.                     (xdrx-entsel
23.                       "\nSelect the first SPLINE <exit>:"
24.                       '((0 . "spline"))
25.                     )
26.                   )
27.            )
28.            (setq e2
29.                   (car
30.                     (xdrx-entsel
31.                       "\nSelect the second SPLINE <exit>:"
32.                       '((0 . "spline"))
33.                     )
34.                   )
35.            )
36.            (setq g1 (xdge::constructor e1))
37.            (setq g2 (xdge::constructor e2))
38.            (_check-curve e1 e2)
39.       )
40.       (xdrx-begin)
41.       (setq degree         (xdrx-getpropertyvalue g1 "degree")
42.                period         (xdrx-getpropertyvalue g1 "isPeriodic")
43.                pts1            (xdrx-getpropertyvalue g1 "controlpoints")
44.                pts2            (xdrx-getpropertyvalue g2 "controlpoints")
45.                knots1         (xdrx-getpropertyvalue g1 "knots")
46.                knots2         (xdrx-getpropertyvalue g2 "knots")
47.                numKnots    (xdrx-getpropertyvalue g1 "numKnots")
48.                numWeights (xdrx-getpropertyvalue g1 "numWeights")
49.       )
50.       (setq pts (mapcar '(lambda (x y)
51.                            (xdrx-line-midp x y)
52.                          )
53.                         pts1
54.                         pts2
55.                 )
56.       )
57.       (setq knots (mapcar '(lambda (x y)
58.                              (/ (+ x y) 2.0)
59.                            )
60.                           knots1
61.                           knots2
62.                   )
63.       )
64.       (setq i 0)
65.       (repeat numWeights
66.         (setq knots (append
67.                       knots
68.                       (list (/ (+ (xdrx-getpropertyvalue g1 "weightat" i)
69.                                   (xdrx-getpropertyvalue g2 "weightat" i)
70.                                )
71.                                2.0
72.                             )
73.                       )
74.                     )
75.               i     (1+i)
76.         )
77.       )
78.       (setq gNurb (xdge::constructor
79.                     "kNurbCurve3d" degree knots pts period)
80.       )
81.       (xdrx-entity-make gNurb)
82.       (xdrx-end)
83.     )
84.   )
85.   (princ)
86. )
87.
« Last Edit: November 25, 2023, 05:54:24 AM by xdcad »
The code I wrote uses XDRX-API，which can be downloaded from github.com and is updated at any time.
===================================
[XDrx-Sub Forum]
https://www.theswamp.org/index.php?board=78.0