-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* find circle radius and center * update readme
- Loading branch information
1 parent
b313769
commit 21e2f53
Showing
9 changed files
with
226 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,4 +160,5 @@ cython_debug/ | |
#.idea/ | ||
|
||
output | ||
lines | ||
lines | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .shape import Shape # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import numpy as np | ||
from scipy.optimize import least_squares | ||
|
||
|
||
def get_edges_for_arc(arc_points: np.ndarray, number_of_edges: int): | ||
""" | ||
Returns a list of edges for an arc/circle | ||
Edges need to be distributed evenly along the arc | ||
""" | ||
count = len(arc_points) | ||
if count < 4: | ||
raise ValueError("Not enough points to define arc") | ||
|
||
if number_of_edges < 3: | ||
raise ValueError("Not enough edges to define arc") | ||
|
||
interval = count // number_of_edges | ||
|
||
edges = [] | ||
for i in range(number_of_edges): | ||
x, y, z = arc_points[i * interval].tolist() | ||
edges.append((round(x, 3), round(y, 3), round(z, 3))) | ||
|
||
return edges | ||
|
||
|
||
def get_arc_info(arc_points: np.ndarray, decimal_places: int = 3): | ||
""" | ||
Get information about arc | ||
Parameters | ||
---------- | ||
arc_points : list | ||
List of arc coordinates [(x,y,z), (x,y,z)] | ||
decimal_places : int | ||
Number of decimal places to round to | ||
Returns | ||
------- | ||
radius : float | ||
Radius of arc | ||
center : np.array | ||
Center of arc | ||
""" | ||
center_x, center_y, radius = fit_circle(arc_points[:, :2]) | ||
center = np.array([center_x, center_y, arc_points[0, 2]]) | ||
center = np.round(center, decimal_places) | ||
radius = round(radius, decimal_places) | ||
return radius, center | ||
|
||
|
||
def fit_circle(points): | ||
x = points[:, 0] | ||
y = points[:, 1] | ||
|
||
initial_params = ( | ||
np.mean(x), | ||
np.mean(y), | ||
np.std(np.sqrt((x - np.mean(x)) ** 2 + (y - np.mean(y)) ** 2)), | ||
) | ||
|
||
result = least_squares(circle_residuals, initial_params, args=(x, y)) | ||
cx, cy, r = result.x | ||
|
||
return cx, cy, r | ||
|
||
|
||
def circle_residuals(params, x, y): | ||
cx, cy, r = params | ||
return (x - cx) ** 2 + (y - cy) ** 2 - r**2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from cnceye import Shape | ||
from cnceye.arc import ( | ||
fit_circle, | ||
get_arc_info, | ||
get_edges_for_arc, | ||
) | ||
|
||
|
||
def test_fit_circle(): | ||
shape = Shape("tests/fixtures/stl/sample.stl") | ||
lines, arcs = shape.get_lines_and_arcs() | ||
for arc_points in arcs[0]: | ||
points = arc_points[:, :2] | ||
center_x, center_y, radius = fit_circle(points) | ||
assert radius > 0.0 | ||
|
||
|
||
def test_get_arc_info(): | ||
shape = Shape("tests/fixtures/stl/sample.stl") | ||
lines, arcs = shape.get_lines_and_arcs() | ||
for arc_points in arcs[0]: | ||
radius, center = get_arc_info(arc_points) | ||
assert radius == 9.0 or radius == 5.0 | ||
|
||
|
||
def test_get_edges_for_arc(): | ||
shape = Shape("tests/fixtures/stl/sample.stl") | ||
lines, arcs = shape.get_lines_and_arcs() | ||
for arc_points in arcs[0]: | ||
edges = get_edges_for_arc(arc_points, 3) | ||
assert len(edges) == 3 | ||
|
||
|
||
def test_get_edges_for_arc_many_edges(): | ||
shape = Shape("tests/fixtures/stl/sample.stl") | ||
lines, arcs = shape.get_lines_and_arcs() | ||
for arc_points in arcs[0]: | ||
edges = get_edges_for_arc(arc_points, 4) | ||
assert len(edges) == 4 | ||
|
||
edges = get_edges_for_arc(arc_points, 6) | ||
assert len(edges) == 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters