Skip to content

Commit ddfa39a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents f9873cf + 7e2c20d commit ddfa39a

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

comparisons.scad

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,10 @@ function group_data(groups, values) =
996996
// list = list to process
997997
// k = number of items to return
998998
function list_smallest(list, k) =
999-
assert(is_list(list))
1000999
assert(is_int(k) && k>=0, "k must be nonnegative")
1000+
assert(is_list(list) && len(list)>=k)
1001+
k==0 ? []
1002+
:
10011003
let(
10021004
v = list[rand_int(0,len(list)-1,1)[0]],
10031005
smaller = [for(li=list) if(li<v) li ],
@@ -1010,5 +1012,33 @@ function list_smallest(list, k) =
10101012
concat(smaller, equal, list_smallest(bigger, k-len(smaller) -len(equal)));
10111013

10121014

1015+
// Function: list_largest()
1016+
// Synopsis: Returns the `k` largest values in the list, in arbitrary order.
1017+
// Topics: List Handling
1018+
// See Also: group_sort(), shuffle(), sort(), sortidx(), unique(), unique_count(), list_smallest()
1019+
// Usage:
1020+
// big = list_biggest(list, k)
1021+
// Description:
1022+
// Returns a set of the k largest items in list in arbitrary order. The items must be
1023+
// mutually comparable with native OpenSCAD comparison operations.
1024+
// You get "undefined operation" errors if you provide invalid input.
1025+
// Arguments:
1026+
// list = list to process
1027+
// k = number of items to return
1028+
function list_largest(list, k) =
1029+
assert(is_int(k) && k>=0, "k must be nonnegative")
1030+
assert(is_list(list) && len(list)>=k)
1031+
k==0 ? []
1032+
: let(
1033+
v = list[rand_int(0,len(list)-1,1)[0]],
1034+
bigger = [for(li=list) if(li>v) li ],
1035+
equal = [for(li=list) if(li==v) li ]
1036+
)
1037+
len(bigger) == k ? bigger :
1038+
len(bigger)<k && len(bigger)+len(equal) >= k ? [ each bigger, for(i=[1:k-len(bigger)]) v ] :
1039+
len(bigger) > k ? list_largest(bigger, k) :
1040+
let( smaller = [for(li=list) if(li<v) li ] )
1041+
concat(bigger, equal, list_largest(smaller, k-len(bigger) -len(equal)));
1042+
10131043

10141044
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

nurbs.scad

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function nurbs_curve(control,degree,splinesteps,u, mult,weights,type="clamped",
197197
"Cannot give degree, mult, weights or knots when you provide a NURBS parameter list")
198198
nurbs_curve(control[2], control[1], splinesteps, u, weights=control[5],mult=control[4], type=control[0], knots=control[3])
199199
: assert(num_defined([splinesteps,u])==1, "Must define exactly one of u and splinesteps")
200-
is_finite(u) ? nurbs_curve(control,degree,u=[u],mult,weights,type=type)[0]
200+
is_finite(u) ? nurbs_curve(control,degree,u=[u],mult=mult,weights=weights,knots=knots,type=type)[0]
201201
: assert(is_undef(splinesteps) || (is_int(splinesteps) && splinesteps>0), "splinesteps must be a positive integer")
202202
let(u=is_range(u) ? list(u) : u)
203203
assert(is_undef(u) || (is_vector(u) && min(u)>=0 && max(u)<=1), "u must be a list of points on the interval [0,1] or a range contained in that interval")
@@ -229,11 +229,12 @@ function nurbs_curve(control,degree,splinesteps,u, mult,weights,type="clamped",
229229
"where degree+1 is allowed. The mult vector has bad values at indices: ",badmult))
230230
assert(is_undef(knots) || is_undef(mult) || len(mult)==len(knots), "If both mult and knots are given they must be vectors of the same length")
231231
assert(is_undef(mult) || type!="clamped" || sum(mult)==len(control)-degree+1,
232-
str("For ",type," spline knot count (sum of multiplicity vector) must be ",len(control)-degree+1," but is instead ",mult?sum(mult):0))
232+
str("For clamped spline knot count (sum of multiplicity vector) must be ",len(control)-degree+1," but is instead ",mult?sum(mult):0))
233233
assert(is_undef(mult) || type!="closed" || sum(mult)==len(control)+1,
234234
str("For closed spline knot count (sum of multiplicity vector) must be ",len(control)+1," but is instead ",mult?sum(mult):0))
235235
assert(is_undef(mult) || type!="open" || sum(mult)==len(control)+degree+1,
236-
str("For closed spline knot count (sum of multiplicity vector) must be ",len(control)+degree+1," but is instead ",mult?sum(mult):0)),
236+
str("For open spline knot count (sum of multiplicity vector) must be ",len(control)+degree+1," but is instead ",mult?sum(mult):0))
237+
assert(uniform || is_increasing(knots), "Knot vector must be increasing"),
237238
control = type=="open" ? control
238239
: type=="clamped" ? control //concat(repeat(control[0], degree),control, repeat(last(control),degree))
239240
: /*type=="closed"*/ concat(control, select(control,count(degree))),
@@ -267,7 +268,7 @@ function nurbs_curve(control,degree,splinesteps,u, mult,weights,type="clamped",
267268
: type=="clamped" ? assert(len(xknots) == len(control)+1-degree, str("For clamped spline of degree ",degree,", knot vector with multiplicity must have length ",
268269
len(control)+1-degree," but has length ", len(xknots)))
269270
assert(xknots[0]!=xknots[1] && last(xknots)!=select(xknots,-2),
270-
"For clamped splint, first and last knots cannot repeat (must have multiplicity one")
271+
"For clamped spline, first and last knots cannot repeat (must have multiplicity one")
271272
concat(repeat(xknots[0],degree), xknots, repeat(last(xknots),degree))
272273
: /*type=="closed"*/ assert(len(xknots) == len(control)+1-degree, str("For closed spline, knot vector (including multiplicity) must have length ",
273274
len(control)+1-degree," but has length ", len(xknots),control))
@@ -279,7 +280,7 @@ function nurbs_curve(control,degree,splinesteps,u, mult,weights,type="clamped",
279280
adjusted_u = !is_undef(splinesteps) ?
280281
[for(i=[degree:1:len(control)-1])
281282
each
282-
if (knot[i]!=knot[i+1])
283+
if (!approx(knot[i],knot[i+1]))
283284
lerpn(knot[i],knot[i+1],splinesteps, endpoint=false),
284285
if (type!="closed") knot[len(control)]
285286
]
@@ -356,7 +357,7 @@ function _calc_mult(knots) =
356357
let(
357358
ind=[ 0,
358359
for(i=[1:len(knots)-1])
359-
if (knots[i]!=knots[i-1]) i,
360+
if (!approx(knots[i],knots[i-1])) i,
360361
len(knots)
361362
]
362363
)

0 commit comments

Comments
 (0)