Skip to content

Commit

Permalink
Merge pull request #14 from ibois-epfl/tests
Browse files Browse the repository at this point in the history
testing for core functionalities
  • Loading branch information
DamienGilliard authored Aug 14, 2024
2 parents 54440e3 + c288ecf commit 5f0f5ef
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 29 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/test_geo_basics.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: test geo basics

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: windows-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up conda environment
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: Carnutes
environment-file: environment.yml

- name: activate conda environment
run: conda activate Carnutes

- name: run tests
run: pytest tests/test_geometry_basics.py

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![basic geometry test](https://github.com/ibois-epfl/Carnutes/actions/workflows/test_geo_basics.yml/badge.svg)

# Carnutes🌳

🪵 In Asterix & Obelix, the Carnutes forest is where druids present their latest crazy inventions to their peers 🪵
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dependencies:
- ZODB==6.0
- open3d==0.18.0
- igraph==0.11.6
- pytest==8.3.2
# - pc_skeletor
Binary file modified src/database/tree_database.fs
Binary file not shown.
Binary file modified src/database/tree_database.fs.index
Binary file not shown.
2 changes: 1 addition & 1 deletion src/database/tree_database.fs.lock
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8116
77841
16 changes: 8 additions & 8 deletions src/database_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ def create_database(voxel_size=0.05):
tree_pc_as_pt_list = []
tree_colors_as_list = []

for i in range(len(pc.points)):
tree_pc_as_pt_list.append([float(pc.points[i][0]),
float(pc.points[i][1]),
float(pc.points[i][2])])
tree_colors_as_list.append([float(pc.colors[i][0]),
float(pc.colors[i][1]),
float(pc.colors[i][2])])
for j in range(len(pc.points)):
tree_pc_as_pt_list.append([float(pc.points[j][0]),
float(pc.points[j][1]),
float(pc.points[j][2])])
tree_colors_as_list.append([float(pc.colors[j][0]),
float(pc.colors[j][1]),
float(pc.colors[j][2])])


tree_for_db = tree.Tree(len(root.trees), i, tree.Pointcloud(tree_pc_as_pt_list, tree_colors_as_list))
tree_for_db = tree.Tree(len(root.trees), f"tree_{i}", tree.Pointcloud(tree_pc_as_pt_list, tree_colors_as_list))
tree_for_db.compute_skeleton()

root.trees[tree_for_db.id] = tree_for_db
Expand Down
2 changes: 1 addition & 1 deletion src/function_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def main():
cylinder = Rhino.Geometry.Brep.CreatePipe(reference_crv_for_brep, 1, True, Rhino.Geometry.PipeCapMode.Flat, True, 0.01, 0.01)[0]
else:
raise ValueError("The geometry of the target element is not supported.")
my_tree.crop(cylinder)
# my_tree.crop(cylinder)
my_tree.create_mesh()

tree_mesh = Rhino.Geometry.Mesh()
Expand Down
8 changes: 5 additions & 3 deletions src/utils/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
module for geometry classes
"""

import typing

class Pointcloud:
"""
Pointcloud class to store point cloud data.
Expand Down Expand Up @@ -30,9 +32,9 @@ class Mesh:
The faces of the mesh as a list of lists of 3 indices
"""
def __init__(self,
vertices : list,
faces : list,
colors : list = None):
vertices : typing.List[typing.List[float]],
faces : typing.List[typing.List[int]],
colors : typing.List[typing.List[int]] = None):
self.vertices = vertices
self.faces = faces
self.colors = colors
Expand Down
20 changes: 4 additions & 16 deletions src/utils/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import numpy as np
import open3d as o3d

import Rhino
# from pc_skeletor import *
# from pc_skeletor import LBC


# The number of points of the tree skeleton
SKELETON_LENGTH = 11

class Tree(persistent.Persistent):
"""
Tree class to store tree data.
Expand Down Expand Up @@ -70,7 +72,7 @@ def compute_skeleton(self):
segments[index] = [point]
else:
segments[index].append(point)
for i in range(11):
for i in range(SKELETON_LENGTH):
i_th_segment = segments[i]
center_point = i_th_segment[0]
for j in range(len(i_th_segment) - 1):
Expand Down Expand Up @@ -150,19 +152,5 @@ def create_mesh(self, radius = 0.25):

print("Mesh created, n° vertices = ", len(self.mesh.vertices), "n° faces = ", len(self.mesh.faces))

def crop(self, bounding_box):
"""
Crop the tree to a bounding volume
:param bounding_volume: closed Brep
The bounding Brep to crop the tree to
"""
indexes_to_remove = []
for i in range(len(self.point_cloud.points)):
point = self.point_cloud.points[i]
if not bounding_box.IsPointInside(Rhino.Geometry.Point3d(point[0], point[1], point[2]), 0.01, True):
indexes_to_remove.append(i)
self.point_cloud.points = [point for i, point in enumerate(self.point_cloud.points) if i not in indexes_to_remove]
self.point_cloud.colors = [color for i, color in enumerate(self.point_cloud.colors) if i not in indexes_to_remove]

def __str__(self):
return f"Tree {self.id} - {self.name}"
61 changes: 61 additions & 0 deletions tests/test_geometry_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import sys
import os
import copy

import pytest

current_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(current_dir + "/..")
sys.path.append(current_dir + "/../src")

from src.utils import geometry as geo
from src.utils import database_reader
from src.utils import tree

@pytest.fixture
def get_skeleton_length():
yield tree.SKELETON_LENGTH

@pytest.fixture
def get_database():
reader = database_reader.DatabaseReader(current_dir + "/../src/database/tree_database.fs")
yield reader
reader.close()

def test_Pointcloud():
point_cloud = geo.Pointcloud([[1, 2, 3], [4, 5, 6]])
assert point_cloud.points == [[1, 2, 3], [4, 5, 6]]
assert point_cloud.colors == None

def test_Mesh():
vertices_list = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, 0, 0]]
faces_list = [[0, 1, 2],
[0, 1, 3]]
mesh = geo.Mesh(vertices_list, faces_list)
assert mesh.vertices[0] == [1, 0, 0]
assert len(mesh.vertices) == 4
assert mesh.faces[0] == [0, 1, 2]
assert len(mesh.faces) == 2
assert mesh.colors == None

def test_database_reader(get_database):
reader = get_database
tree_id = 0
my_tree = reader.get_tree(tree_id)
my_tree = copy.deepcopy(my_tree)
reader.close()
assert my_tree.id == 0
assert my_tree.name == "tree_0"

def test_point_cloud(get_skeleton_length, get_database):
reader = get_database
tree_id = 0
my_tree = reader.get_tree(tree_id)
my_tree = copy.deepcopy(my_tree)
reader.close()
assert len(my_tree.skeleton.points) == get_skeleton_length
assert len(my_tree.point_cloud.points) == 6388
assert len(my_tree.point_cloud.colors) == len(my_tree.point_cloud.points)

0 comments on commit 5f0f5ef

Please sign in to comment.