File: path_bbox.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (57 lines) | stat: -rw-r--r-- 1,447 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (c) 2021, Manfred Moitzi
# License: MIT License
import time
import math
from ezdxf.math import BoundingBox, Vec3
from ezdxf.render.forms import circle
import ezdxf.path

LARGE_PATH = ezdxf.path.from_vertices(circle(100, radius=10))

def current_fast_bbox():
    ezdxf.path.bbox((LARGE_PATH, ), fast=True)

def current_precise_bbox():
    ezdxf.path.bbox((LARGE_PATH, ), fast=False)

def new_fast_bbox():
    fast_bbox(LARGE_PATH)


def fast_bbox(path) -> BoundingBox:
    # it's much slower!
    bbox = BoundingBox()
    if len(path) == 0:
        return bbox
    min_x = math.inf
    min_y = math.inf
    min_z = math.inf
    max_x = -math.inf
    max_y = -math.inf
    max_z = -math.inf
    for x, y, z in path.control_vertices():
        min_x = min(min_x, x)
        min_y = min(min_y, y)
        min_z = min(min_z, z)
        max_x = max(max_x, x)
        max_y = max(max_y, y)
        max_z = max(max_z, z)
    bbox.extend([Vec3(min_x, min_y, min_z), Vec3(max_x, max_y, max_z)])
    return bbox


def profile(text, func, runs):
    t0 = time.perf_counter()
    for _ in range(runs):
        func()
    t1 = time.perf_counter()
    t = t1 - t0
    print(f"{text} {t:.3f}s")


RUNS = 10_000

print(f"Profiling bounding box calculation:")
profile(f"current_precise_bbox() {RUNS}:", current_precise_bbox, RUNS)
profile(f"current_fast_bbox() {RUNS}:", current_fast_bbox, RUNS)
profile(f"new_fast_bbox() {RUNS}:", new_fast_bbox, RUNS)