# Path: lasercut/helper.py
# def transform(part, referentiel_face, transform_matrix=None, y_invert = False):
#     normal_face = referentiel_face.normalAt(0, 0)
#     # original center is (0,0,0)
#     transformed_center = referentiel_face.CenterOfMass #+ normal_face.normalize() * x_origin
#     if transform_matrix is None:
#         transform_matrix = get_matrix_transform(referentiel_face)
#     part.Placement = FreeCAD.Placement(transform_matrix).multiply(part.Placement)
#     part.translate(transformed_center)
#     if y_invert:
#         part.rotate(transformed_center, normal_face, 180.)
#     return part
# 
# def compare_freecad_vector(vector1, vector2, epsilon=10e-6):
#     vector = vector1.sub(vector2)
#     if math.fabs(vector.x) < epsilon and math.fabs(vector.y) < epsilon and math.fabs(vector.z) < epsilon:
#         return True
#     return False
# 
# def compare_value(value1, value2, epsilon=10e-6):
#     value = value1 - value2
#     if math.fabs(value) < epsilon:
#         return True
#     return False
# 
# class Segment:
#     def __init__(self, first=FreeCAD.Vector(0, 0, 0), second=FreeCAD.Vector(0, 0, 0)):
#         self.A = first
#         self.B = second
# 
#     def a(self):
#         return self.A
# 
#     def b(self):
#         return self.B * 1
# 
#     def clone_a(self):
#         return self.A * 1
# 
#     def clone_b(self):
#         return self.B * 1
# 
#     def vector(self):
#         return self.B.sub(self.A)
# 
#     def length(self):
#         return self.vector().Length
# 
#     def mid_point(self):
#         mid_point_b = self.A.add(self.B)
#         mid_point_b.scale(0.5, 0.5, 0.5)
#         return mid_point_b
# 
#     def get_angle(self, segment):
#         return self.vector().getAngle(segment.vector())
# 
#     def add(self, vector):
#         return Segment(self.A.add(vector), self.B.add(vector))
# 
#     def rotate_z(self, angle):
#         return Segment(rotate_vector_z(self.A, angle), rotate_vector_z(self.B, angle))
# 
#     def __repr__(self):
#         return "Segment A: " + str(self.A) + ", B: " + str(self.B)
# 
# def assemble_list_element(el_list):
#     if len(el_list) == 0:
#         return None
# 
#     part = el_list[0]
#     for el in el_list[1:]:
#         part = part.fuse(el)
# 
#     return part


import Part
import FreeCAD
import math
from lasercut.helper import transform, compare_freecad_vector, compare_value, Segment, assemble_list_element
    if midpoint1_a.sub(midpoint2_b).Length < distance:
        edge2 = face2_edges[1]

    return face1_edges[0], edge2, face1_edges[1].CenterOfMass.sub(face1_edges[0].CenterOfMass)


def get_segment_from_edge(edge1, edge2):
    length = edge1.Vertexes[0].Point.sub(edge2.Vertexes[0].Point).Length
    case = 1

    test_length = edge1.Vertexes[0].Point.sub(edge2.Vertexes[1].Point).Length
    if test_length < length:
        case = 2
        length = test_length

    test_length = edge1.Vertexes[1].Point.sub(edge2.Vertexes[0].Point).Length
    if test_length < length:
        case = 3
        length = test_length

    test_length =  edge1.Vertexes[1].Point.sub(edge2.Vertexes[1].Point).Length
    if test_length < length:
        case = 4
        length = test_length

    if case == 1:
        first = Segment(edge1.Vertexes[1].Point, edge1.Vertexes[0].Point)
        second = Segment(edge2.Vertexes[1].Point, edge2.Vertexes[0].Point)
    elif case == 2:
        first = Segment(edge1.Vertexes[1].Point, edge1.Vertexes[0].Point)

# Path: lasercut/helper.py
# def transform(part, referentiel_face, transform_matrix=None, y_invert = False):
#     normal_face = referentiel_face.normalAt(0, 0)
#     # original center is (0,0,0)
#     transformed_center = referentiel_face.CenterOfMass #+ normal_face.normalize() * x_origin
#     if transform_matrix is None:
#         transform_matrix = get_matrix_transform(referentiel_face)
#     part.Placement = FreeCAD.Placement(transform_matrix).multiply(part.Placement)
#     part.translate(transformed_center)
#     if y_invert:
#         part.rotate(transformed_center, normal_face, 180.)
#     return part
# 
# def compare_freecad_vector(vector1, vector2, epsilon=10e-6):
#     vector = vector1.sub(vector2)
#     if math.fabs(vector.x) < epsilon and math.fabs(vector.y) < epsilon and math.fabs(vector.z) < epsilon:
#         return True
#     return False
# 
# def compare_value(value1, value2, epsilon=10e-6):
#     value = value1 - value2
#     if math.fabs(value) < epsilon:
#         return True
#     return False
# 
# class Segment:
#     def __init__(self, first=FreeCAD.Vector(0, 0, 0), second=FreeCAD.Vector(0, 0, 0)):
#         self.A = first
#         self.B = second
# 
#     def a(self):
#         return self.A
# 
#     def b(self):
#         return self.B * 1
# 
#     def clone_a(self):
#         return self.A * 1
# 
#     def clone_b(self):
#         return self.B * 1
# 
#     def vector(self):
#         return self.B.sub(self.A)
# 
#     def length(self):
#         return self.vector().Length
# 
#     def mid_point(self):
#         mid_point_b = self.A.add(self.B)
#         mid_point_b.scale(0.5, 0.5, 0.5)
#         return mid_point_b
# 
#     def get_angle(self, segment):
#         return self.vector().getAngle(segment.vector())
# 
#     def add(self, vector):
#         return Segment(self.A.add(vector), self.B.add(vector))
# 
#     def rotate_z(self, angle):
#         return Segment(rotate_vector_z(self.A, angle), rotate_vector_z(self.B, angle))
# 
#     def __repr__(self):
#         return "Segment A: " + str(self.A) + ", B: " + str(self.B)
# 
# def assemble_list_element(el_list):
#     if len(el_list) == 0:
#         return None
# 
#     part = el_list[0]
#     for el in el_list[1:]:
#         part = part.fuse(el)
# 
#     return part


# Path: lasercut/makehinges.py
import Part
import FreeCAD
import math
from lasercut.helper import transform, compare_freecad_vector, compare_value, Segment, assemble_list_element

    if midpoint1_a.sub(midpoint2_b).Length < distance:
        edge2 = face2_edges[1]

    return face1_edges[0], edge2, face1_edges[1].CenterOfMass.sub(face1_edges[0].CenterOfMass)


def get_segment_from_edge(edge1, edge2):
    length = edge1.Vertexes[0].Point.sub(edge2.Vertexes[0].Point).Length
    case = 1

    test_length = edge1.Vertexes[0].Point.sub(edge2.Vertexes[1].Point).Length
    if test_length < length:
        case = 2
        length = test_length

    test_length = edge1.Vertexes[1].Point.sub(edge2.Vertexes[0].Point).Length
    if test_length < length:
        case = 3
        length = test_length

    test_length =  edge1.Vertexes[1].Point.sub(edge2.Vertexes[1].Point).Length
    if test_length < length:
        case = 4
        length = test_length

    if case == 1:
        first = Segment(edge1.Vertexes[1].Point, edge1.Vertexes[0].Point)
        second = Segment(edge2.Vertexes[1].Point, edge2.Vertexes[0].Point)
    elif case == 2:
        first = Segment(edge1.Vertexes[1].Point, edge1.Vertexes[0].Point)

        second = Segment(edge2.Vertexes[0].Point, edge2.Vertexes[1].Point)