sdf-tool 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.
- sdf_tool/__init__.py +1 -0
- sdf_tool/sdf_tool.py +111 -0
- sdf_tool-0.1.0.dist-info/METADATA +55 -0
- sdf_tool-0.1.0.dist-info/RECORD +8 -0
- sdf_tool-0.1.0.dist-info/WHEEL +5 -0
- sdf_tool-0.1.0.dist-info/entry_points.txt +2 -0
- sdf_tool-0.1.0.dist-info/licenses/LICENSE +21 -0
- sdf_tool-0.1.0.dist-info/top_level.txt +1 -0
sdf_tool/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '0.1.0'
|
sdf_tool/sdf_tool.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from scipy.ndimage import distance_transform_edt
|
|
3
|
+
import numpy as np
|
|
4
|
+
from PIL import Image
|
|
5
|
+
import math
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
|
|
9
|
+
def load_binary_mask(filepath: str, threshold: float = 0.5) -> np.ndarray:
|
|
10
|
+
"""Load image and convert to binary mask (True = Inside)"""
|
|
11
|
+
img = Image.open(filepath).convert("L")
|
|
12
|
+
arr = np.array(img, dtype=np.float32) / 255.0
|
|
13
|
+
binary = arr > threshold
|
|
14
|
+
print(f"Loaded {filepath}: {binary.shape} pixels, {binary.sum()} inside")
|
|
15
|
+
return binary
|
|
16
|
+
|
|
17
|
+
def generate_sdf_vectorized(
|
|
18
|
+
mask_highres: np.ndarray,
|
|
19
|
+
out_width: int,
|
|
20
|
+
out_height: int,
|
|
21
|
+
spread: float = 6.0,
|
|
22
|
+
search_margin: float = 1.1,
|
|
23
|
+
) -> np.ndarray:
|
|
24
|
+
if mask_highres.ndim != 2:
|
|
25
|
+
raise ValueError("Inpput mask must be 2D")
|
|
26
|
+
|
|
27
|
+
high_h, high_w = mask_highres.shape
|
|
28
|
+
|
|
29
|
+
scale_x = high_w / out_width
|
|
30
|
+
scale_y = high_h / out_height
|
|
31
|
+
|
|
32
|
+
max_dist_high = spread * max(scale_x, scale_y)
|
|
33
|
+
|
|
34
|
+
# Signed Euclidean distance field (high-res)
|
|
35
|
+
dist_to_bg = distance_transform_edt(~mask_highres.astype(bool))
|
|
36
|
+
dist_to_fg = distance_transform_edt( mask_highres.astype(bool))
|
|
37
|
+
|
|
38
|
+
signed_high = np.where(mask_highres, -dist_to_fg, dist_to_bg)
|
|
39
|
+
|
|
40
|
+
# Sample at low-res texel centers
|
|
41
|
+
half_x = scale_x / 2
|
|
42
|
+
half_y = scale_y / 2
|
|
43
|
+
yy, xx = np.mgrid[0:out_height, 0:out_width]
|
|
44
|
+
cy = (yy * scale_y + half_y).astype(int)
|
|
45
|
+
cx = (xx * scale_x + half_x).astype(int)
|
|
46
|
+
cy = np.clip(cy, 0, high_h - 1)
|
|
47
|
+
cx = np.clip(cx, 0, high_w - 1)
|
|
48
|
+
|
|
49
|
+
signed_low = signed_high[cy, cx]
|
|
50
|
+
|
|
51
|
+
# Normalize + clamp like Valve
|
|
52
|
+
value = 0.5 - signed_low / (2.0 * max_dist_high)
|
|
53
|
+
value = np.clip(value, 0.0, 1.0)
|
|
54
|
+
|
|
55
|
+
return value
|
|
56
|
+
|
|
57
|
+
def save_sdf_as_png(sdf: np.ndarray, output_path: str):
|
|
58
|
+
"""Save as 8-bit grayscale PNG"""
|
|
59
|
+
img_data = (sdf * 255.0 + 0.5).astype(np.uint8)
|
|
60
|
+
Image.fromarray(img_data, mode="L").save(output_path)
|
|
61
|
+
print(f"Saved to {output_path} ({sdf.shape[0]}x{sdf.shape[1]})")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def main():
|
|
65
|
+
parser = argparse.ArgumentParser(
|
|
66
|
+
description="Generate Valve-style SDF texture (rectangular support)"
|
|
67
|
+
)
|
|
68
|
+
parser.add_argument("--input", type=str, required=True,
|
|
69
|
+
help="Input high-resolution image")
|
|
70
|
+
parser.add_argument("--width", type=int, required=True,
|
|
71
|
+
help="Output width (pixels)")
|
|
72
|
+
parser.add_argument("--height", type=int, required=True,
|
|
73
|
+
help="Output height (pixels)")
|
|
74
|
+
parser.add_argument("--spread", type=float, default=6.0,
|
|
75
|
+
help="Spread factor in low-res texels (default: 6)")
|
|
76
|
+
parser.add_argument("--output", type=str, default=None,
|
|
77
|
+
help="Output filename (default: <input>_sdf_<w>x<h>.png)")
|
|
78
|
+
parser.add_argument("--threshold", type=float, default=0.5,
|
|
79
|
+
help="Threshold for inside/outside (default 0.5)")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
args = parser.parse_args()
|
|
83
|
+
|
|
84
|
+
if args.width < 8 or args.width > 2048 or args.height < 8 or args.height > 2048:
|
|
85
|
+
print("Width and height should be 8–2048", file=sys.stderr)
|
|
86
|
+
return 1
|
|
87
|
+
|
|
88
|
+
start = time.time()
|
|
89
|
+
|
|
90
|
+
mask = load_binary_mask(args.input, threshold=args.threshold)
|
|
91
|
+
sdf = generate_sdf_vectorized(
|
|
92
|
+
mask,
|
|
93
|
+
out_width=args.width,
|
|
94
|
+
out_height=args.height,
|
|
95
|
+
spread=args.spread
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
if args.output:
|
|
99
|
+
out_path = args.output
|
|
100
|
+
else:
|
|
101
|
+
import os
|
|
102
|
+
base, ext = os.path.splitext(args.input)
|
|
103
|
+
out_path = f"{base}_sdf_{args.width}x{args.height}.png"
|
|
104
|
+
|
|
105
|
+
save_sdf_as_png(sdf, out_path)
|
|
106
|
+
|
|
107
|
+
elapsed = time.time() - start
|
|
108
|
+
print(f"Done in {elapsed:.2f} seconds")
|
|
109
|
+
|
|
110
|
+
if __name__ == '__main__':
|
|
111
|
+
sys.exit(main() or 0)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sdf-tool
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tool for working with SDF
|
|
5
|
+
Author-email: Pavel Křupala <pavel.krupala@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Pavel Křupala
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Keywords: sdf,command-line,tool
|
|
29
|
+
Classifier: Programming Language :: Python :: 3
|
|
30
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
31
|
+
Classifier: Operating System :: OS Independent
|
|
32
|
+
Requires-Python: >=3.9
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
License-File: LICENSE
|
|
35
|
+
Requires-Dist: numpy>=2.4.2
|
|
36
|
+
Requires-Dist: scipy>=1.17.0
|
|
37
|
+
Requires-Dist: pillow>=12.1.0
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# sdf-tool
|
|
41
|
+
simple sdf generating tool. Input is an image, output is signed distance field image
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install sdf-tool
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
sdf-tool [-h] --input INPUT --width WIDTH --height HEIGHT [--spread SPREAD] [--output OUTPUT] [--threshold THRESHOLD]
|
|
53
|
+
|
|
54
|
+
the following arguments are required: --input, --width, --height
|
|
55
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
sdf_tool/__init__.py,sha256=L6zbQIZKsAP-Knhm6fBcQFPoVdIDuejxze60qX23jiw,21
|
|
2
|
+
sdf_tool/sdf_tool.py,sha256=K2ildvVlF3pa3ZPSR8LKyIip3ZXjpytHS-aF_28sjOo,3767
|
|
3
|
+
sdf_tool-0.1.0.dist-info/licenses/LICENSE,sha256=XRS6KvXFeDtbX00CVUf4TojT4zH-__P6M1k3lnY1NOE,1092
|
|
4
|
+
sdf_tool-0.1.0.dist-info/METADATA,sha256=mjBj2oRrjGnTfytrCsA6GTb1umeGEFQVuy-MqdYwxEs,2173
|
|
5
|
+
sdf_tool-0.1.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
6
|
+
sdf_tool-0.1.0.dist-info/entry_points.txt,sha256=iLvrlCTFlaEu-AbqI0EsAJQYuq-pSdtk6A6Soh_jWCc,52
|
|
7
|
+
sdf_tool-0.1.0.dist-info/top_level.txt,sha256=nR24oNSEJzFiX6-wbGftCdwN3_UqAzNHl5_LjqL0HiM,9
|
|
8
|
+
sdf_tool-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pavel Křupala
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sdf_tool
|