datacut 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. datacut/__init__.py +121 -0
  2. datacut/__main__.py +122 -0
  3. datacut/animation/__init__.py +0 -0
  4. datacut/animation/animation.py +336 -0
  5. datacut/animation/composition.py +258 -0
  6. datacut/animation/creation.py +315 -0
  7. datacut/animation/fading.py +328 -0
  8. datacut/animation/growing.py +90 -0
  9. datacut/animation/indication.py +608 -0
  10. datacut/animation/movement.py +165 -0
  11. datacut/animation/numbers.py +92 -0
  12. datacut/animation/physics.py +453 -0
  13. datacut/animation/rotation.py +95 -0
  14. datacut/animation/specialized.py +956 -0
  15. datacut/animation/transform.py +505 -0
  16. datacut/animation/transform_matching_parts.py +343 -0
  17. datacut/animation/update.py +95 -0
  18. datacut/camera/__init__.py +0 -0
  19. datacut/camera/camera.py +795 -0
  20. datacut/camera/camera_frame.py +320 -0
  21. datacut/camera/cinematography.py +1166 -0
  22. datacut/config.py +479 -0
  23. datacut/constants.py +160 -0
  24. datacut/css/__init__.py +189 -0
  25. datacut/css/easing.py +328 -0
  26. datacut/css/engine.py +1727 -0
  27. datacut/css/gradients.py +678 -0
  28. datacut/css/motion.py +1615 -0
  29. datacut/css/parser.py +741 -0
  30. datacut/css/presets.py +143 -0
  31. datacut/css/shader_fill.py +703 -0
  32. datacut/css/values.py +650 -0
  33. datacut/default_config.yml +224 -0
  34. datacut/event_handler/__init__.py +6 -0
  35. datacut/event_handler/event_dispatcher.py +161 -0
  36. datacut/event_handler/event_listner.py +31 -0
  37. datacut/event_handler/event_type.py +11 -0
  38. datacut/event_handler/picking.py +111 -0
  39. datacut/event_keys.py +53 -0
  40. datacut/extract_scene.py +200 -0
  41. datacut/layout/__init__.py +39 -0
  42. datacut/layout/components.py +425 -0
  43. datacut/layout/containers.py +1520 -0
  44. datacut/logger.py +13 -0
  45. datacut/mobject/__init__.py +0 -0
  46. datacut/mobject/boolean_ops.py +168 -0
  47. datacut/mobject/changing.py +209 -0
  48. datacut/mobject/coordinate_systems.py +1056 -0
  49. datacut/mobject/fractals.py +348 -0
  50. datacut/mobject/frame.py +80 -0
  51. datacut/mobject/functions.py +147 -0
  52. datacut/mobject/geometry.py +1834 -0
  53. datacut/mobject/gltf.py +243 -0
  54. datacut/mobject/interactive.py +906 -0
  55. datacut/mobject/lights.py +366 -0
  56. datacut/mobject/materials.py +207 -0
  57. datacut/mobject/matrix.py +570 -0
  58. datacut/mobject/mesh3d.py +2288 -0
  59. datacut/mobject/mobject.py +2885 -0
  60. datacut/mobject/mobject_update_utils.py +123 -0
  61. datacut/mobject/model.py +543 -0
  62. datacut/mobject/number_line.py +517 -0
  63. datacut/mobject/numbers.py +441 -0
  64. datacut/mobject/probability.py +536 -0
  65. datacut/mobject/shape_matchers.py +161 -0
  66. datacut/mobject/subtitles.py +260 -0
  67. datacut/mobject/svg/__init__.py +0 -0
  68. datacut/mobject/svg/brace.py +356 -0
  69. datacut/mobject/svg/drawings.py +1041 -0
  70. datacut/mobject/svg/old_tex_mobject.py +335 -0
  71. datacut/mobject/svg/path_cache.py +155 -0
  72. datacut/mobject/svg/special_tex.py +216 -0
  73. datacut/mobject/svg/string_mobject.py +622 -0
  74. datacut/mobject/svg/svg_mobject.py +657 -0
  75. datacut/mobject/svg/svg_parse.py +437 -0
  76. datacut/mobject/svg/tex_mobject.py +499 -0
  77. datacut/mobject/svg/text_mobject.py +702 -0
  78. datacut/mobject/svg/typst_mobject.py +864 -0
  79. datacut/mobject/three_dimensions.py +686 -0
  80. datacut/mobject/types/__init__.py +0 -0
  81. datacut/mobject/types/dot_cloud.py +220 -0
  82. datacut/mobject/types/image_mobject.py +130 -0
  83. datacut/mobject/types/point_cloud_mobject.py +138 -0
  84. datacut/mobject/types/shader_mobject.py +268 -0
  85. datacut/mobject/types/surface.py +848 -0
  86. datacut/mobject/types/vectorized_mobject.py +1749 -0
  87. datacut/mobject/types/video_mobject.py +3902 -0
  88. datacut/mobject/value_tracker.py +110 -0
  89. datacut/mobject/vector_field.py +585 -0
  90. datacut/module_loader.py +176 -0
  91. datacut/reactive/__init__.py +25 -0
  92. datacut/reactive/signals.py +610 -0
  93. datacut/renderer/__init__.py +4 -0
  94. datacut/renderer/drawing.py +1066 -0
  95. datacut/renderer/gpu.py +800 -0
  96. datacut/renderer/material.py +88 -0
  97. datacut/renderer/pipeline.py +179 -0
  98. datacut/renderer/post_process.py +577 -0
  99. datacut/renderer/renderer.py +1285 -0
  100. datacut/renderer/shader_source.py +146 -0
  101. datacut/renderer/shared_buffer.py +185 -0
  102. datacut/renderer/texture.py +217 -0
  103. datacut/renderer/uniform_block.py +118 -0
  104. datacut/scene/__init__.py +0 -0
  105. datacut/scene/incremental.py +233 -0
  106. datacut/scene/interactive_scene.py +758 -0
  107. datacut/scene/performance.py +226 -0
  108. datacut/scene/presentation_scene.py +783 -0
  109. datacut/scene/recording.py +541 -0
  110. datacut/scene/safe_zones.py +323 -0
  111. datacut/scene/scene.py +2849 -0
  112. datacut/scene/scene_embed.py +277 -0
  113. datacut/scene/scene_file_writer.py +1014 -0
  114. datacut/scene/scene_task.py +183 -0
  115. datacut/scene/sfx.py +164 -0
  116. datacut/scene/sound.py +702 -0
  117. datacut/scene/sound_clip.py +580 -0
  118. datacut/scene/timeline.py +1689 -0
  119. datacut/scene/transcript.py +503 -0
  120. datacut/shaders/fill.wgsl +107 -0
  121. datacut/shaders/fill_depth.wgsl +198 -0
  122. datacut/shaders/image.wgsl +25 -0
  123. datacut/shaders/inserts/NOTE.md +16 -0
  124. datacut/shaders/inserts/clip_test.wgsl +14 -0
  125. datacut/shaders/inserts/complex_functions.wgsl +20 -0
  126. datacut/shaders/inserts/fill_color.wgsl +10 -0
  127. datacut/shaders/inserts/finalize_color.wgsl +44 -0
  128. datacut/shaders/inserts/frame_uniforms.wgsl +37 -0
  129. datacut/shaders/inserts/frame_units.wgsl +18 -0
  130. datacut/shaders/inserts/image_quad.wgsl +28 -0
  131. datacut/shaders/inserts/mobject_uniforms.wgsl +11 -0
  132. datacut/shaders/inserts/project_point.wgsl +44 -0
  133. datacut/shaders/inserts/quad_corners.wgsl +12 -0
  134. datacut/shaders/inserts/read_data.wgsl +33 -0
  135. datacut/shaders/inserts/surface_mesh.wgsl +106 -0
  136. datacut/shaders/mandelbrot_fractal.wgsl +100 -0
  137. datacut/shaders/mesh.wgsl +474 -0
  138. datacut/shaders/newton_fractal.wgsl +200 -0
  139. datacut/shaders/present.wgsl +62 -0
  140. datacut/shaders/stroke.wgsl +1141 -0
  141. datacut/shaders/stroke_depth.wgsl +1136 -0
  142. datacut/shaders/surface.wgsl +40 -0
  143. datacut/shaders/textured_surface.wgsl +59 -0
  144. datacut/shaders/true_dot.wgsl +78 -0
  145. datacut/shaders/video.wgsl +42 -0
  146. datacut/tex_templates.yml +736 -0
  147. datacut/transcribe.py +32 -0
  148. datacut/typing.py +412 -0
  149. datacut/utils/__init__.py +0 -0
  150. datacut/utils/bezier.py +467 -0
  151. datacut/utils/browser_audio.py +423 -0
  152. datacut/utils/browser_code_transform.py +1293 -0
  153. datacut/utils/browser_completions.py +2255 -0
  154. datacut/utils/browser_fonts.py +143 -0
  155. datacut/utils/browser_pathops.py +47 -0
  156. datacut/utils/browser_scheduler.py +18 -0
  157. datacut/utils/browser_typst.py +82 -0
  158. datacut/utils/cache.py +34 -0
  159. datacut/utils/cinematic_vfx.py +779 -0
  160. datacut/utils/color.py +203 -0
  161. datacut/utils/debug.py +46 -0
  162. datacut/utils/dict_ops.py +22 -0
  163. datacut/utils/directories.py +55 -0
  164. datacut/utils/editor_signatures.py +1806 -0
  165. datacut/utils/editor_thumbnails.py +769 -0
  166. datacut/utils/family_ops.py +48 -0
  167. datacut/utils/file_ops.py +65 -0
  168. datacut/utils/images.py +52 -0
  169. datacut/utils/iterables.py +216 -0
  170. datacut/utils/native.py +280 -0
  171. datacut/utils/paths.py +83 -0
  172. datacut/utils/rate_functions.py +154 -0
  173. datacut/utils/render_geometry.py +186 -0
  174. datacut/utils/simple_functions.py +99 -0
  175. datacut/utils/sounds.py +43 -0
  176. datacut/utils/space_ops.py +460 -0
  177. datacut/utils/structured_array.py +256 -0
  178. datacut/utils/svg_export.py +102 -0
  179. datacut/utils/tex.py +139 -0
  180. datacut/utils/tex_file_writing.py +164 -0
  181. datacut/utils/tex_to_symbol_count.py +186 -0
  182. datacut/utils/typst_file_writing.py +205 -0
  183. datacut/utils/vfx_presets.py +4543 -0
  184. datacut/utils/visual_css.py +737 -0
  185. datacut/utils/visual_editor.py +4292 -0
  186. datacut/utils/window_audio.py +760 -0
  187. datacut/window.py +910 -0
  188. datacut-0.1.0.dist-info/METADATA +226 -0
  189. datacut-0.1.0.dist-info/RECORD +193 -0
  190. datacut-0.1.0.dist-info/WHEEL +5 -0
  191. datacut-0.1.0.dist-info/entry_points.txt +2 -0
  192. datacut-0.1.0.dist-info/licenses/LICENSE.md +21 -0
  193. datacut-0.1.0.dist-info/top_level.txt +1 -0
datacut/utils/paths.py ADDED
@@ -0,0 +1,83 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+
5
+ import numpy as np
6
+
7
+ from datacut.constants import OUT
8
+ from datacut.utils.bezier import interpolate
9
+ from datacut.utils.space_ops import get_norm
10
+ from datacut.utils.space_ops import rotation_matrix_transpose
11
+
12
+ from typing import TYPE_CHECKING
13
+
14
+ if TYPE_CHECKING:
15
+ from typing import Callable
16
+ from datacut.typing import Vect3, Vect3Array
17
+
18
+
19
+ STRAIGHT_PATH_THRESHOLD = 0.01
20
+
21
+
22
+ def straight_path(
23
+ start_points: np.ndarray,
24
+ end_points: np.ndarray,
25
+ alpha: float
26
+ ) -> np.ndarray:
27
+ """
28
+ Same function as interpolate, but renamed to reflect
29
+ intent of being used to determine how a set of points move
30
+ to another set. For instance, it should be a specific case
31
+ of path_along_arc
32
+ """
33
+ return interpolate(start_points, end_points, alpha)
34
+
35
+
36
+ def path_along_arc(
37
+ arc_angle: float | Tuple[float, float] | np.ndarray,
38
+ axis: Vect3 = OUT
39
+ ) -> Callable[[Vect3Array, Vect3Array, float], Vect3Array]:
40
+ """
41
+ arc_angle can be a single angle, or a pair of angles, in which case
42
+ the range of all angles between that pair will be used.
43
+
44
+ If vect is vector from start to end, [vect[:,1], -vect[:,0]] is
45
+ perpendicular to vect in the left direction.
46
+ """
47
+ if isinstance(arc_angle, float | int) and abs(arc_angle) < STRAIGHT_PATH_THRESHOLD:
48
+ return straight_path
49
+ if get_norm(axis) == 0:
50
+ axis = OUT
51
+ unit_axis = axis / get_norm(axis)
52
+
53
+ def path(start_points, end_points, alpha):
54
+ if isinstance(arc_angle, float | int):
55
+ theta = arc_angle
56
+ else:
57
+ if isinstance(arc_angle, np.ndarray) and len(arc_angle) == len(start_points):
58
+ theta_range = arc_angle
59
+ else:
60
+ theta_range = np.linspace(arc_angle[0], arc_angle[-1], len(start_points))
61
+ # Avoid zero, mildly hacky
62
+ theta_range[np.abs(theta_range) < STRAIGHT_PATH_THRESHOLD] = STRAIGHT_PATH_THRESHOLD
63
+ # Get shape to match
64
+ theta = theta_range[:, np.newaxis] * np.ones(start_points.shape[1])
65
+ start_to_end = end_points - start_points
66
+
67
+ with np.errstate(divide='ignore', invalid='ignore'):
68
+ adjustments = np.nan_to_num(np.cross(unit_axis, start_to_end / 2.0) / np.tan(theta / 2))
69
+ arc_centers = start_points + 0.5 * start_to_end + adjustments
70
+
71
+ c_to_start = start_points - arc_centers
72
+ c_to_perp = np.cross(unit_axis, c_to_start)
73
+ return arc_centers + np.cos(alpha * theta) * c_to_start + np.sin(alpha * theta) * c_to_perp
74
+
75
+ return path
76
+
77
+
78
+ def clockwise_path() -> Callable[[Vect3Array, Vect3Array, float], Vect3Array]:
79
+ return path_along_arc(-np.pi)
80
+
81
+
82
+ def counterclockwise_path() -> Callable[[Vect3Array, Vect3Array, float], Vect3Array]:
83
+ return path_along_arc(np.pi)
@@ -0,0 +1,154 @@
1
+ from __future__ import annotations
2
+
3
+ # What the Visual Editor files this module's classes under
4
+ __editor_category__ = 'Standard'
5
+
6
+ import numpy as np
7
+
8
+ from datacut.utils.bezier import bezier
9
+
10
+ from typing import TYPE_CHECKING
11
+
12
+ if TYPE_CHECKING:
13
+ from typing import Callable
14
+
15
+
16
+ def linear(t: float) -> float:
17
+ return t
18
+
19
+
20
+ def smooth(t: float) -> float:
21
+ # Zero first and second derivatives at t=0 and t=1.
22
+ # Equivalent to bezier([0, 0, 0, 1, 1, 1])
23
+ s = 1 - t
24
+ return (t**3) * (10 * s * s + 5 * s * t + t * t)
25
+
26
+
27
+ def rush_into(t: float) -> float:
28
+ return 2 * smooth(0.5 * t)
29
+
30
+
31
+ def rush_from(t: float) -> float:
32
+ return 2 * smooth(0.5 * (t + 1)) - 1
33
+
34
+
35
+ def slow_into(t: float) -> float:
36
+ return np.sqrt(1 - (1 - t) * (1 - t))
37
+
38
+
39
+ def double_smooth(t: float) -> float:
40
+ if t < 0.5:
41
+ return 0.5 * smooth(2 * t)
42
+ else:
43
+ return 0.5 * (1 + smooth(2 * t - 1))
44
+
45
+
46
+ def there_and_back(t: float) -> float:
47
+ new_t = 2 * t if t < 0.5 else 2 * (1 - t)
48
+ return smooth(new_t)
49
+
50
+
51
+ def there_and_back_with_pause(t: float, pause_ratio: float = 1. / 3) -> float:
52
+ a = 2. / (1. - pause_ratio)
53
+ if t < 0.5 - pause_ratio / 2:
54
+ return smooth(a * t)
55
+ elif t < 0.5 + pause_ratio / 2:
56
+ return 1
57
+ else:
58
+ return smooth(a - a * t)
59
+
60
+
61
+ def running_start(t: float, pull_factor: float = -0.5) -> float:
62
+ return bezier([0, 0, pull_factor, pull_factor, 1, 1, 1])(t)
63
+
64
+
65
+ def overshoot(t: float, pull_factor: float = 1.5) -> float:
66
+ return bezier([0, 0, pull_factor, pull_factor, 1, 1])(t)
67
+
68
+
69
+ def not_quite_there(
70
+ func: Callable[[float], float] = smooth,
71
+ proportion: float = 0.7
72
+ ) -> Callable[[float], float]:
73
+ def result(t):
74
+ return proportion * func(t)
75
+ return result
76
+
77
+
78
+ def wiggle(t: float, wiggles: float = 2) -> float:
79
+ return there_and_back(t) * np.sin(wiggles * np.pi * t)
80
+
81
+
82
+ def squish_rate_func(
83
+ func: Callable[[float], float],
84
+ a: float = 0.4,
85
+ b: float = 0.6
86
+ ) -> Callable[[float], float]:
87
+ def result(t):
88
+ if a == b:
89
+ return a
90
+ elif t < a:
91
+ return func(0)
92
+ elif t > b:
93
+ return func(1)
94
+ else:
95
+ return func((t - a) / (b - a))
96
+
97
+ return result
98
+
99
+ # Stylistically, should this take parameters (with default values)?
100
+ # Ultimately, the functionality is entirely subsumed by squish_rate_func,
101
+ # but it may be useful to have a nice name for with nice default params for
102
+ # "lingering", different from squish_rate_func's default params
103
+
104
+
105
+ def lingering(t: float) -> float:
106
+ return squish_rate_func(lambda t: t, 0, 0.8)(t)
107
+
108
+
109
+ def exponential_decay(t: float, half_life: float = 0.1) -> float:
110
+ # The half-life should be rather small to minimize
111
+ # the cut-off error at the end
112
+ return 1 - np.exp(-t / half_life)
113
+
114
+
115
+ def spring_rate_func(
116
+ stiffness: float = 180.0,
117
+ damping: float = 18.0,
118
+ mass: float = 1.0,
119
+ initial_velocity: float = 0.0,
120
+ duration: float | None = None,
121
+ ) -> Callable[[float], float]:
122
+ from datacut.animation.physics import spring_rate_func as _spring_rf
123
+ return _spring_rf(
124
+ stiffness=stiffness,
125
+ damping=damping,
126
+ mass=mass,
127
+ initial_velocity=initial_velocity,
128
+ duration=duration,
129
+ )
130
+
131
+
132
+ def spring(t: float) -> float:
133
+ from datacut.animation.physics import spring as _spring
134
+ return _spring(t)
135
+
136
+
137
+ def bouncy_spring(t: float) -> float:
138
+ from datacut.animation.physics import bouncy_spring as _bouncy
139
+ return _bouncy(t)
140
+
141
+
142
+ def snappy_spring(t: float) -> float:
143
+ from datacut.animation.physics import snappy_spring as _snappy
144
+ return _snappy(t)
145
+
146
+
147
+ def gentle_spring(t: float) -> float:
148
+ from datacut.animation.physics import gentle_spring as _gentle
149
+ return _gentle(t)
150
+
151
+
152
+ def stiff_spring(t: float) -> float:
153
+ from datacut.animation.physics import stiff_spring as _stiff
154
+ return _stiff(t)
@@ -0,0 +1,186 @@
1
+ """
2
+ The arithmetic of two things the renderer works out every frame, kept apart from the
3
+ renderer so that it can be held against the native versions of it on import, see
4
+ datacut.utils.native, without bringing in the gpu.
5
+
6
+ leader_bounds: the 2D boxes Renderer.optimize_leaders_dag judges overlaps by
7
+ order_leaders: the order Renderer.optimize_leaders_dag draws leaders in
8
+ scissor_extent: the pixels PostProcessor.get_scissor_rect clips an effect to
9
+
10
+ Each is what those methods did inline before, unchanged, and what they still do wherever
11
+ the native version is missing or turned down.
12
+ """
13
+ from __future__ import annotations
14
+
15
+ import numpy as np
16
+
17
+ from typing import TYPE_CHECKING
18
+
19
+ if TYPE_CHECKING:
20
+ from typing import Any, Sequence
21
+
22
+
23
+ def leader_bounds(
24
+ bbs: Sequence[np.ndarray],
25
+ fixed: Sequence[bool],
26
+ owner: Sequence[int],
27
+ r_extra: Sequence[float],
28
+ camera: tuple[float, float, float] | None,
29
+ ) -> list[tuple[np.ndarray, np.ndarray] | None]:
30
+ """
31
+ Each leader's 2D box, None for one with no points: the boxes of its members, `bbs`, each
32
+ belonging to the leader `owner` names, those of a leader together and in order. A member's
33
+ box is taken relative to the camera's center (cx, cy) and over its scale, inv being one
34
+ over it, unless it is fixed in the frame or there is no camera. The whole is then widened by
35
+ the leader's `r_extra`, for its effects reaching beyond it.
36
+ """
37
+ n = len(r_extra)
38
+ bmins: list[np.ndarray | None] = [None] * n
39
+ bmaxs: list[np.ndarray | None] = [None] * n
40
+ for bb, is_fixed, idx in zip(bbs, fixed, owner):
41
+ mmin = bb[0][:2]
42
+ mmax = bb[2][:2]
43
+ if camera is not None and not is_fixed:
44
+ cam_cx, cam_cy, inv_scale = camera
45
+ mmin = np.array([(mmin[0] - cam_cx) * inv_scale, (mmin[1] - cam_cy) * inv_scale])
46
+ mmax = np.array([(mmax[0] - cam_cx) * inv_scale, (mmax[1] - cam_cy) * inv_scale])
47
+ if bmins[idx] is None:
48
+ bmins[idx] = mmin.copy()
49
+ bmaxs[idx] = mmax.copy()
50
+ else:
51
+ np.minimum(bmins[idx], mmin, out=bmins[idx])
52
+ np.maximum(bmaxs[idx], mmax, out=bmaxs[idx])
53
+ return [
54
+ None if bmins[i] is None else (bmins[i] - r_extra[i], bmaxs[i] + r_extra[i])
55
+ for i in range(n)
56
+ ]
57
+
58
+
59
+ def order_leaders(
60
+ z: Sequence[Any],
61
+ bboxes: Sequence[tuple[np.ndarray, np.ndarray] | None],
62
+ groups: Sequence[Any],
63
+ ) -> list[int]:
64
+ """
65
+ Leaders put in an order which draws each after whatever it must be drawn over: a later
66
+ one sitting higher, or as high and overlapping it. Among those free to go next, one from
67
+ the group drawn last is taken while there is one, so that a group's effect pass is shared,
68
+ and otherwise whichever came first.
69
+
70
+ `z` holds each leader's z_index, `bboxes` its 2D (min, max) box or None, and `groups`
71
+ its vfx group or None, told apart by identity.
72
+ """
73
+ n = len(z)
74
+ in_degree = [0] * n
75
+ adj: list[list[int]] = [[] for _ in range(n)]
76
+
77
+ for i in range(n):
78
+ bi = bboxes[i]
79
+ zi = z[i]
80
+ for j in range(i + 1, n):
81
+ zj = z[j]
82
+ bj = bboxes[j]
83
+ overlaps = (
84
+ bi is not None and bj is not None
85
+ and not (bi[1][0] < bj[0][0] or bi[0][0] > bj[1][0]
86
+ or bi[1][1] < bj[0][1] or bi[0][1] > bj[1][1])
87
+ )
88
+ if (zj > zi) or (zj == zi and overlaps):
89
+ adj[i].append(j)
90
+ in_degree[j] += 1
91
+
92
+ ready = [i for i in range(n) if in_degree[i] == 0]
93
+ ordered: list[int] = []
94
+ current_group = None
95
+
96
+ while ready:
97
+ candidate_idx: int | None = None
98
+ if current_group is not None:
99
+ for idx in ready:
100
+ if groups[idx] is current_group:
101
+ candidate_idx = idx
102
+ break
103
+
104
+ if candidate_idx is None:
105
+ candidate_idx = min(ready)
106
+ current_group = groups[candidate_idx]
107
+
108
+ ready.remove(candidate_idx)
109
+ ordered.append(candidate_idx)
110
+
111
+ for succ in adj[candidate_idx]:
112
+ in_degree[succ] -= 1
113
+ if in_degree[succ] == 0:
114
+ ready.append(succ)
115
+
116
+ return ordered
117
+
118
+
119
+ def scissor_extent(
120
+ pts_list: list[np.ndarray],
121
+ view: np.ndarray | None,
122
+ kx: float,
123
+ ky: float,
124
+ kz: float,
125
+ width: int,
126
+ height: int,
127
+ padding: int,
128
+ ) -> tuple[bool, int, int, int, int]:
129
+ """
130
+ The pixels a set of points lands on, with the corners of their 3D box, padded and not yet
131
+ clamped: (whole screen, min x, min y, max x, max y), the first true, and the rest
132
+ meaningless, where some point is behind or crosses the camera, or projects off to infinity.
133
+
134
+ Projected as project_point.wgsl projects, by `view` and then scaled by kx, ky and, along z,
135
+ kz. A mobject fixed in the frame has no view, and its z is divided by kz instead.
136
+ """
137
+ points = np.vstack(pts_list)
138
+
139
+ # The 8 corners of the 3D box, to cover thickness and curvature
140
+ min_xyz = np.min(points, axis=0)
141
+ max_xyz = np.max(points, axis=0)
142
+ bbox_corners = np.array([
143
+ [min_xyz[0], min_xyz[1], min_xyz[2]],
144
+ [min_xyz[0], min_xyz[1], max_xyz[2]],
145
+ [min_xyz[0], max_xyz[1], min_xyz[2]],
146
+ [min_xyz[0], max_xyz[1], max_xyz[2]],
147
+ [max_xyz[0], min_xyz[1], min_xyz[2]],
148
+ [max_xyz[0], min_xyz[1], max_xyz[2]],
149
+ [max_xyz[0], max_xyz[1], min_xyz[2]],
150
+ [max_xyz[0], max_xyz[1], max_xyz[2]],
151
+ ], dtype=np.float64)
152
+ all_points = np.vstack([points, bbox_corners])
153
+
154
+ if view is None:
155
+ cam_points = all_points
156
+ scaled_x = cam_points[:, 0] * kx
157
+ scaled_y = cam_points[:, 1] * ky
158
+ scaled_z = cam_points[:, 2] / kz
159
+ else:
160
+ p4 = np.hstack([all_points, np.ones((len(all_points), 1), dtype=np.float64)])
161
+ cam_points = (p4 @ view.T)[:, :3]
162
+ scaled_x = cam_points[:, 0] * kx
163
+ scaled_y = cam_points[:, 1] * ky
164
+ scaled_z = cam_points[:, 2] * kz
165
+
166
+ # The perspective divide of project_point.wgsl: w = 1.0 - scaled.z
167
+ w_clip = 1.0 - scaled_z
168
+
169
+ # Crossing the camera's plane or behind it, it may land anywhere
170
+ if np.any(w_clip <= 0.05) or not np.all(np.isfinite(w_clip)):
171
+ return True, 0, 0, 0, 0
172
+
173
+ x_ndc = scaled_x / w_clip
174
+ y_ndc = scaled_y / w_clip
175
+
176
+ if not np.all(np.isfinite(x_ndc)) or not np.all(np.isfinite(y_ndc)):
177
+ return True, 0, 0, 0, 0
178
+
179
+ px = (0.5 + 0.5 * x_ndc) * width
180
+ py = (0.5 - 0.5 * y_ndc) * height
181
+
182
+ min_px = int(np.floor(np.min(px))) - padding
183
+ max_px = int(np.ceil(np.max(px))) + padding
184
+ min_py = int(np.floor(np.min(py))) - padding
185
+ max_py = int(np.ceil(np.max(py))) + padding
186
+ return False, min_px, min_py, max_px, max_py
@@ -0,0 +1,99 @@
1
+ from __future__ import annotations
2
+
3
+ from functools import lru_cache
4
+ import hashlib
5
+ import inspect
6
+ import math
7
+
8
+ import numpy as np
9
+
10
+ from typing import TYPE_CHECKING
11
+ if TYPE_CHECKING:
12
+ from typing import Callable, TypeVar, Iterable
13
+ from datacut.typing import FloatArray
14
+
15
+ Scalable = TypeVar("Scalable", float, FloatArray)
16
+
17
+
18
+
19
+ def sigmoid(x: float | FloatArray):
20
+ return 1.0 / (1 + np.exp(-x))
21
+
22
+
23
+ @lru_cache(maxsize=10)
24
+ def choose(n: int, k: int) -> int:
25
+ return math.comb(n, k)
26
+
27
+
28
+ def gen_choose(n: int, r: int) -> int:
29
+ return int(np.prod(range(n, n - r, -1)) / math.factorial(r))
30
+
31
+
32
+ def get_num_args(function: Callable) -> int:
33
+ return function.__code__.co_argcount
34
+
35
+
36
+ def get_parameters(function: Callable) -> Iterable[str]:
37
+ return inspect.signature(function).parameters.keys()
38
+
39
+
40
+ def clip(a: float, min_a: float, max_a: float) -> float:
41
+ if a < min_a:
42
+ return min_a
43
+ elif a > max_a:
44
+ return max_a
45
+ return a
46
+
47
+
48
+ def arr_clip(arr: np.ndarray, min_a: float, max_a: float) -> np.ndarray:
49
+ return np.clip(arr, min_a, max_a, out=arr)
50
+
51
+
52
+ def fdiv(a: Scalable, b: Scalable, zero_over_zero_value: Scalable | None = None) -> Scalable:
53
+ """
54
+ Less heavyweight name for np.true_divide, enabling
55
+ default behavior for 0/0
56
+ """
57
+ if zero_over_zero_value is not None:
58
+ out = np.full_like(a, zero_over_zero_value)
59
+ where = np.logical_or(a != 0, b != 0)
60
+ else:
61
+ out = None
62
+ where = True
63
+
64
+ return np.true_divide(a, b, out=out, where=where)
65
+
66
+
67
+ def binary_search(
68
+ function: Callable[[float], float],
69
+ target: float,
70
+ lower_bound: float,
71
+ upper_bound: float,
72
+ tolerance:float = 1e-4
73
+ ) -> float | None:
74
+ lh = lower_bound
75
+ rh = upper_bound
76
+ mh = (lh + rh) / 2
77
+ while abs(rh - lh) > tolerance:
78
+ lx, mx, rx = [function(h) for h in (lh, mh, rh)]
79
+ if lx == target:
80
+ return lh
81
+ if rx == target:
82
+ return rh
83
+
84
+ if lx <= target and rx >= target:
85
+ if mx > target:
86
+ rh = mh
87
+ else:
88
+ lh = mh
89
+ elif lx > target and rx < target:
90
+ lh, rh = rh, lh
91
+ else:
92
+ return None
93
+ mh = (lh + rh) / 2
94
+ return mh
95
+
96
+
97
+ def hash_string(string: str, n_bytes=16) -> str:
98
+ hasher = hashlib.sha256(string.encode())
99
+ return hasher.hexdigest()[:n_bytes]
@@ -0,0 +1,43 @@
1
+ from __future__ import annotations
2
+
3
+ import platform
4
+ import shutil
5
+ import subprocess
6
+
7
+ from datacut.utils.directories import get_sound_dir
8
+ from datacut.utils.file_ops import find_file
9
+
10
+
11
+ def get_full_sound_file_path(sound_file_name: str) -> str:
12
+ return find_file(
13
+ sound_file_name,
14
+ directories=[get_sound_dir()],
15
+ extensions=[".wav", ".mp3", ".ogg", ".m4a", ".aac", ".flac", ""]
16
+ )
17
+
18
+
19
+ def play_sound(sound_file: str, start_time: float = 0.0) -> subprocess.Popen:
20
+ """Play a sound file from start_time and return its process handle."""
21
+ full_path = get_full_sound_file_path(sound_file)
22
+
23
+ if start_time > 0 or shutil.which("ffplay") is not None:
24
+ cmd = ["ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet"]
25
+ if start_time > 0:
26
+ cmd += ["-ss", str(start_time)]
27
+ cmd.append(full_path)
28
+ return subprocess.Popen(
29
+ cmd,
30
+ stdout=subprocess.DEVNULL,
31
+ stderr=subprocess.DEVNULL,
32
+ )
33
+
34
+ system = platform.system()
35
+ if system == "Windows":
36
+ return subprocess.Popen(
37
+ ["powershell", "-NoProfile", "-c", f"(New-Object Media.SoundPlayer '{full_path}').PlaySync()"],
38
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
39
+ )
40
+ elif system == "Darwin":
41
+ return subprocess.Popen(["afplay", full_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
42
+ else:
43
+ return subprocess.Popen(["aplay", full_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)