Skip to content

Commit

Permalink
ZeroDivisionError
Browse files Browse the repository at this point in the history
  • Loading branch information
HiraiKyo committed Aug 8, 2024
1 parent f311d32 commit 1ea0fd4
Show file tree
Hide file tree
Showing 15 changed files with 1,596 additions and 180 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
matrix/
points/
pcd/

.mypy_cache/
.env
.env.local
Expand Down
5 changes: 2 additions & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"recommendations": [
"ms-python.mypy-type-checker",
"matangover.mypy",
"usernamehw.errorlens",
"littlefoxteam.vscode-python-test-adapter",
"oderwat.indent-rainbow",
"shardulm94.trailing-spaces",
"hediet.vscode-drawio"
"charliermarsh.ruff",
"ms-python.mypy-type-checker",
]
}
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"src/tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"mypy-type-checker.args": [
"--config=pyproject.toml"
],
}
29 changes: 9 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
# pyproject-boilerplate
# ply-processor-basics

Boilerplate for Python Project
Basic libraries for manipulating point cloud.

## Purpose
## Methods

- Type-safe
- Good lint rules.
- Well-developed with VSCode
### Vector

## Stacks
#### `vector.normalize`

- Poetry
- Ruff
- Mypy, Pydantic
- Pytest
### Points

## Structure
#### `points.rotate_euler`

## Installation
#### `points.get_distances_line`

```sh
poetry install
```

## References

- [https://qiita.com/tamo_breaker/items/dbd8482e88c61269d531]
#### `points.get_distances_plane`
2 changes: 2 additions & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!.gitignore
!sample/
Binary file added data/samples/sample.ply
Binary file not shown.
36 changes: 36 additions & 0 deletions data/samples/stl2ply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env /usr/bin/python3

import numpy as np
import open3d as o3d

names = [
"outer_mast_AX-VT-Fusion",
"outer_mast_AX-VT_inverse-Fusion",
"middle_mast_outside_AX-VT-Fusion",
]


def gen_ply(name: str):
mesh = o3d.io.read_triangle_mesh(name + ".stl")
mesh.compute_vertex_normals()
pcd = mesh.sample_points_uniformly(number_of_points=1000000)
pcdd = pcd.voxel_down_sample(voxel_size=1.0)
o3d.visualization.draw_geometries([pcdd])

diameter = np.linalg.norm(
np.asarray(pcdd.get_max_bound()) - np.asarray(pcdd.get_min_bound())
)
camera = np.array([-100, -100, 100])
radius = diameter * 100

_, pt_map = pcdd.hidden_point_removal(camera, radius)

pcd = pcdd.select_by_index(pt_map)

o3d.io.write_point_cloud(name + ".ply", pcd)
o3d.visualization.draw_geometries([pcd])


if __name__ == "__main__":
for name in names:
gen_ply(name)
6 changes: 0 additions & 6 deletions example_pyproject/main.py

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions ply_processor_basics/vector/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .normalize import normalize as normalize
17 changes: 17 additions & 0 deletions ply_processor_basics/vector/normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np
from numpy.typing import NDArray


def normalize(vector: NDArray[np.float32]) -> NDArray[np.float32]:
"""_summary_
Args:
vector (NDArray[np.float32]): _description_
Returns:
NDArray[np.float32]: _description_
"""
if np.linalg.norm(vector) == 0:
raise ZeroDivisionError("The norm of the vector is zero.")

return vector / np.linalg.norm(vector)
1,618 changes: 1,479 additions & 139 deletions poetry.lock

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ test = {cmd = "dotenv --file envs/.env.development run -- pytest", help = "Run u
dev-python = {cmd = "dotenv --file envs/.env.development run -- python", help = "Run python with dev environment."}

[tool.poetry]
name = "example_pyproject"
name = "ply_processor_basics"
version = "0.1.0"
description = ""
authors = ["HiraiKyo <[email protected]>"]
license = "MIT"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"
python = "3.8.19"
python-dotenv = "^1.0.1"
pydantic = "^2.7.4"
open3d = "^0.18.0"
scikit-learn = "1.3.2"
pyyaml = "^6.0.2"
addict = "^2.4.0"
pillow = "^10.4.0"
pandas = "2.0.3"
tqdm = "^4.66.5"
numpy = "1.24.4"

[tool.poetry.group.dev.dependencies]
ruff = "^0.5.0"
Expand All @@ -23,20 +31,24 @@ pytest-env = "^1.1.3"
taskipy = "^1.13.0"

[tool.ruff]
target-version = "py311"
target-version = "py38"
line-length = 120

[tool.mypy]
python_version = "3.8"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
strict = true
disallow_untyped_defs = false

[tool.pytest.ini_options]
testpaths = [
"tests",
]

[[tool.mypy.overrides]]
module = ["open3d", "pandas", "sklearn", "sklearn.cluster", "scipy.spatial.transform"]
ignore_missing_imports = true

[build-system]
requires = ["poetry-core"]
requires = ["setuptools","poetry-core"]
build-backend = "poetry.core.masonry.api"
5 changes: 0 additions & 5 deletions tests/test_main.py

This file was deleted.

24 changes: 24 additions & 0 deletions tests/vector/test_normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np
from pytest import approx

from ply_processor_basics.vector import normalize


def test_normalize_success() -> None:
v = np.array([1, 1, 0])
normed = normalize(v)
assert normed == approx([1 / np.sqrt(2), 1 / np.sqrt(2), 0])

v = np.array([1, 1, 1])
normed = normalize(v)
assert normed == approx([1 / np.sqrt(3), 1 / np.sqrt(3), 1 / np.sqrt(3)])


def test_normalize_failed() -> None:
v = np.array([0, 0, 0])
# expecting raise ZeroDivisionError
try:
normalize(v)
assert False
except ZeroDivisionError:
assert True

0 comments on commit 1ea0fd4

Please sign in to comment.