Skip to content

Commit

Permalink
Number distributions API.
Browse files Browse the repository at this point in the history
  • Loading branch information
portnov committed Sep 11, 2022
1 parent d2da902 commit a27f55f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions utils/adaptive_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import random

from sverchok.utils.logging import debug, info, exception
from sverchok.utils.math import distribute_int
from sverchok.utils.curve import SvCurve, SvCurveLengthSolver

class CurvePopulationController(object):
Expand Down Expand Up @@ -72,6 +73,25 @@ def get_points_count(self, idx):
ppe += 2
return ppe

def populate_t_segment(key_ts, target_count):
"""
Given key values of T parameter and target number of values,
return list of T values distributed so that each span
between key T values includes number of values proportional
to span's size.
For example,
populate_t_segment([0, 1, 3], 7) = [0, 0.5, 1, 1.5, 2, 2.5, 3]
"""
key_ts = np.asarray(key_ts)
count_new = target_count - len(key_ts)
sizes = key_ts[1:] - key_ts[:-1]
counts = distribute_int(count_new, sizes)
result = set(key_ts)
for count, t_max, t_min in zip(counts, key_ts[1:], key_ts[:-1]):
ts = np.linspace(t_min, t_max, num=count+2)[1:-1]
result.update(ts)
return np.asarray(list(sorted(result)))

def populate_curve(curve, samples_t, by_length = False, by_curvature = True, population_controller = None, curvature_clip = 100, seed = None):
if population_controller is None:
population_controller = MinMaxPerSegment(1, 5)
Expand Down
11 changes: 11 additions & 0 deletions utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,17 @@ def _gcd(a, b):
return a

def distribute_int(n, sizes):
"""
Distribute the integer number (`n`) of items among several buckets of
different sizes, so that the number of items in each bucket is proportional
to bucket's size.
Parameters:
* n - total number of items (integer)
* sizes - sizes of buckets do distribute between (list of numbers)
Output:
* list of integers: for each bucket, the number of items to be put
into that bucket. Sum of all numbers in this list is always equal to `n`.
"""
total_size = sum(sizes)
ratios = [size / total_size for size in sizes]
counts = [int(n * ratio) for ratio in ratios]
Expand Down

0 comments on commit a27f55f

Please sign in to comment.