Repository navigation
Commit 50d898c
committed
MAINT: Unify calculation of normal vectors from polygons
This combines `get_normals` and `_generate_normals`, and eliminates all other calls to np.cross.
`get_normals` and `_generate_normals` were profiled, and it was found that vectorizing `np.cross` like in `get_normals` was faster:
```python
import numpy as np
def get_normals(polygons):
v1 = np.empty((len(polygons), 3))
v2 = np.empty((len(polygons), 3))
for poly_i, ps in enumerate(polygons):
# pick three points around the polygon at which to find the
# normal doesn't vectorize because polygons is jagged
i1, i2, i3 = 0, len(ps)//3, 2*len(ps)//3
v1[poly_i, :] = ps[i1, :] - ps[i2, :]
v2[poly_i, :] = ps[i2, :] - ps[i3, :]
return np.cross(v1, v2)
def _generate_normals(self, polygons):
normals = []
for verts in polygons:
v1 = np.array(verts[0]) - np.array(verts[1])
v2 = np.array(verts[2]) - np.array(verts[0])
normals.append(np.cross(v1, v2))
return np.array(normals)
polygons = [
np.random.rand(np.random.randint(10, 1000), 3)
for i in range(100)
]
%timeit _generate_normals(polygons)
# 3.14 ms ± 255 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit get_normals(polygons)
# 452 µs ± 4.33 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
```1 parent 82a4b24 commit 50d898c
1 file changed
Lines changed: 46 additions & 33 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1669 | 1669 | | |
1670 | 1670 | | |
1671 | 1671 | | |
1672 | | - | |
1673 | | - | |
1674 | | - | |
1675 | | - | |
1676 | | - | |
1677 | | - | |
1678 | | - | |
1679 | | - | |
1680 | | - | |
1681 | | - | |
1682 | | - | |
1683 | | - | |
1684 | | - | |
1685 | | - | |
1686 | 1672 | | |
1687 | 1673 | | |
1688 | 1674 | | |
1689 | 1675 | | |
1690 | 1676 | | |
1691 | 1677 | | |
1692 | | - | |
| 1678 | + | |
| 1679 | + | |
1693 | 1680 | | |
1694 | 1681 | | |
1695 | 1682 | | |
| |||
1702 | 1689 | | |
1703 | 1690 | | |
1704 | 1691 | | |
1705 | | - | |
| 1692 | + | |
| 1693 | + | |
1706 | 1694 | | |
1707 | 1695 | | |
1708 | 1696 | | |
| |||
1713 | 1701 | | |
1714 | 1702 | | |
1715 | 1703 | | |
1716 | | - | |
1717 | | - | |
1718 | | - | |
1719 | | - | |
1720 | | - | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
1721 | 1711 | | |
1722 | | - | |
1723 | | - | |
1724 | | - | |
1725 | | - | |
1726 | | - | |
1727 | | - | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
| 1719 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
| 1726 | + | |
| 1727 | + | |
| 1728 | + | |
| 1729 | + | |
| 1730 | + | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
| 1736 | + | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
1728 | 1743 | | |
1729 | 1744 | | |
1730 | 1745 | | |
| |||
1971 | 1986 | | |
1972 | 1987 | | |
1973 | 1988 | | |
1974 | | - | |
1975 | | - | |
1976 | | - | |
| 1989 | + | |
1977 | 1990 | | |
1978 | 1991 | | |
1979 | 1992 | | |
| |||
2020 | 2033 | | |
2021 | 2034 | | |
2022 | 2035 | | |
2023 | | - | |
2024 | | - | |
2025 | | - | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
2026 | 2039 | | |
2027 | 2040 | | |
2028 | 2041 | | |
| |||
0 commit comments