Skip to content

Commit

Permalink
Refactor (#4)
Browse files Browse the repository at this point in the history
* connect lines

* update readme

* fmt
  • Loading branch information
yuichiroaoki authored Aug 5, 2023
1 parent a42fd43 commit 97792b8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# cnceye
# cnceye
An image-based CMM (Coordinate Measuring Machine) system for CNC machine tools

## Simulation with Blender
Create test data

Prerequisites
- Blender 3.6.1 or later

Change the output path in `scripts/create_test_images.py` and run

```bash
blender "blender/example.blend" --background --python scripts/create_test_images.py
```
18 changes: 15 additions & 3 deletions cnceye/cmm/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@ def add_image(self, image, distance: float, center: Coordinate) -> None:
self.previous_lines.append(line)

def save_image(self, path: str) -> None:
entire_image = np.asarray([[[0, 0, 0]] * 300] * 300, dtype=np.uint8)
entire_image = np.asarray([[[0, 0, 0]] * 1200] * 1000, dtype=np.uint8)
for line in self.previous_lines:
start = line.start
end = line.end
cv2.line(
entire_image,
(int(start.x + 100), int(-start.y + 100)),
(int(end.x + 100), int(-end.y + 100)),
(int((start.x + 100) * 5), int((-start.y + 100) * 5)),
(int((end.x + 100) * 5), int((-end.y + 100) * 5)),
(255, 255, 255),
1,
)
cv2.putText(
entire_image,
f"{line.get_length():.2f}",
(
int((start.x + end.x + 200) * 5 / 2),
int((-start.y - end.y + 200) * 5 / 2),
),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(105, 145, 209),
1,
)
cv2.imwrite(path, entire_image)
2 changes: 1 addition & 1 deletion cnceye/cmm/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def from_opencv_coord(self, distance: float, opencv_xy: tuple) -> Coordinate:
return Coordinate(
opencv_origin.x + opencv_xy[0] / pixel_per_mm,
opencv_origin.y - opencv_xy[1] / pixel_per_mm,
self.center.x - distance,
self.center.z - distance,
)

def from_pixel_length(self, distance: float, pixel_length) -> float:
Expand Down
6 changes: 5 additions & 1 deletion cnceye/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def is_same_straight_line(self, other: "Line") -> bool:
slope_diff = self.get_slope_diff(other)
intercept_diff = self.get_intercept_diff(other)
if self.get_slope() == np.inf and other.get_slope() == np.inf:
return abs(self.start.x - other.start.x) < 0.1
return abs(self.start.x - other.start.x) < 0.2
return slope_diff < 0.1 and intercept_diff < 0.1

def is_x_overlapping(self, other: "Line") -> bool:
Expand All @@ -79,6 +79,10 @@ def is_y_overlapping(self, other: "Line") -> bool:
)

def is_overlapping(self, other: "Line") -> bool:
if self.get_slope() == np.inf and other.get_slope() == np.inf:
return self.is_y_overlapping(other)
if self.get_slope() == 0 and other.get_slope() == 0:
return self.is_x_overlapping(other)
return self.is_x_overlapping(other) and self.is_y_overlapping(other)

def connect_lines(self, other: "Line") -> "Line" or None:
Expand Down
2 changes: 1 addition & 1 deletion scripts/create_test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def render_image(output_path, camera_position, camera_rotation, light_position):
light_position = (0, 0, 0.1)

# Create a folder to save the rendered images
output_folder = "/home/runner/work/cnceye/cnceye/output"
output_folder = "/path/to/output_images"
os.makedirs(output_folder, exist_ok=True)

# Render images with different camera and light positions
Expand Down
7 changes: 6 additions & 1 deletion tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from cnceye.coordinate import Coordinate
from tests.utils import diff_in_micron

# import pytest

focal_length = 50.0 # mm
camera_height = 60.0 # mm
object_height = 10.0 # mm
Expand All @@ -12,6 +14,7 @@
camera = Camera(focal_length, sensor_width)


# @pytest.mark.skip(reason="need to fix")
def test_add_image_one_row():
x_move = Coordinate(20.0, 0.0, 0.0)
all_images = AllImages(camera)
Expand Down Expand Up @@ -45,6 +48,7 @@ def test_add_image_one_row():
assert third_line_diff_in_micron < 100.0


# @pytest.mark.skip(reason="need to fix")
def test_add_image_two_rows():
x_move = Coordinate(20.0, 0.0, 0.0)
y_move = Coordinate(0.0, -10.0, 0.0)
Expand All @@ -58,7 +62,8 @@ def test_add_image_two_rows():
all_images.add_image(image, distance, center)
index += 1

print(f"lines: {len(all_images.previous_lines)}")
line_count = len(all_images.previous_lines)
print(f"line count: {line_count}")
for line in all_images.previous_lines:
print(line)

Expand Down
36 changes: 22 additions & 14 deletions tests/test_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,37 @@ def test_get_intersection():
assert line.get_intersection(other) == (0.5, 0.5)


def test_is_same_straight_line():
line0 = Line(
Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100)
)
def test_is_same_straight_line0():
line0 = Line(Coordinate(-53.01875, 14.89375, -10), Coordinate(-53.01875, 28.0, -10))
line1 = Line(
Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 25.125, -100)
Coordinate(-53.01875, 4.89375, -10), Coordinate(-53.01875, 25.125, -10)
)
assert line0.is_same_straight_line(line1)


def test_is_overlapping():
line0 = Line(
Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100)
)
def test_is_same_straight_line1():
line0 = Line(Coordinate(-37.775, 20.66875, 10), Coordinate(37.775, 20.66875, 10))
line1 = Line(Coordinate(-37.85625, 20.6625, 10), Coordinate(37.85625, 20.6625, 10))
assert line0.is_same_straight_line(line1)


def test_is_overlapping0():
line0 = Line(Coordinate(-53.01875, 14.89375, -10), Coordinate(-53.01875, 28.0, -10))
line1 = Line(
Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 25.125, -100)
Coordinate(-53.01875, 4.89375, -10), Coordinate(-53.01875, 25.125, -10)
)
assert line0.is_overlapping(line1)

line0 = Line(
Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100)
)
line0 = Line(Coordinate(-53.01875, 14.89375, -10), Coordinate(-53.01875, 28.0, -10))
line1 = Line(
Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 11.125, -100)
Coordinate(-53.01875, 4.89375, -10), Coordinate(-53.01875, 11.125, -10)
)
assert not line0.is_overlapping(line1)


def test_is_overlapping1():
line0 = Line(
Coordinate(-42.66875, 4.89375, -10), Coordinate(-42.66875, 15.76875, -10)
)
line1 = Line(Coordinate(-42.675, 4.89375, -10), Coordinate(-42.675, 15.675, -10))
assert line0.is_overlapping(line1)

0 comments on commit 97792b8

Please sign in to comment.