# 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
    edge1, edge2, extrusion_vector = get_coplanar_edge(face_1, face_2)
    seg_face_1, seg_face_2 = get_segment_from_edge(edge1, edge2)
    intersection_point = do_intersection(seg_face_1,seg_face_2)

    #print "intersection point : => " + str(intersection_point)
    #print "seg1 " +str(seg_face_1)
    #print "seg2 " +str(seg_face_2)

    diff_length_test = compare_value(intersection_point.sub(seg_face_1.B).Length,
                                     intersection_point.sub(seg_face_2.B).Length,
                                     10e-3)
    if diff_length_test is False :
        raise ValueError("Not an arc %f %f" %(intersection_point.sub(seg_face_1.B).Length, intersection_point.sub(seg_face_2.B).Length))
    inner_arc_radius = intersection_point.sub(seg_face_1.B).Length
    outer_arc_radius = intersection_point.sub(seg_face_1.A).Length
    mid_arc_radius = intersection_point.sub(seg_face_1.mid_point()).Length

    mid_point_b = seg_face_1.B.add(seg_face_2.B)
    mid_point_b.scale(0.5, 0.5, 0.5)
    dir_mid_point = mid_point_b.sub(intersection_point)
    dir_mid_point.normalize()

    inner_arc_point = dir_mid_point * inner_arc_radius
    outter_arc_point = dir_mid_point * outer_arc_radius
    if hinge.reversed_angle:
        inner_arc_point = intersection_point.sub(inner_arc_point)
        outter_arc_point = intersection_point.sub(outter_arc_point)
    else:    
        inner_arc_point = inner_arc_point.add(intersection_point)
        outter_arc_point = outter_arc_point.add(intersection_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

    edge1, edge2, extrusion_vector = get_coplanar_edge(face_1, face_2)
    seg_face_1, seg_face_2 = get_segment_from_edge(edge1, edge2)
    intersection_point = do_intersection(seg_face_1,seg_face_2)

    #print "intersection point : => " + str(intersection_point)
    #print "seg1 " +str(seg_face_1)
    #print "seg2 " +str(seg_face_2)

    diff_length_test = compare_value(intersection_point.sub(seg_face_1.B).Length,
                                     intersection_point.sub(seg_face_2.B).Length,
                                     10e-3)
    if diff_length_test is False :
        raise ValueError("Not an arc %f %f" %(intersection_point.sub(seg_face_1.B).Length, intersection_point.sub(seg_face_2.B).Length))
    inner_arc_radius = intersection_point.sub(seg_face_1.B).Length
    outer_arc_radius = intersection_point.sub(seg_face_1.A).Length
    mid_arc_radius = intersection_point.sub(seg_face_1.mid_point()).Length

    mid_point_b = seg_face_1.B.add(seg_face_2.B)
    mid_point_b.scale(0.5, 0.5, 0.5)
    dir_mid_point = mid_point_b.sub(intersection_point)
    dir_mid_point.normalize()

    inner_arc_point = dir_mid_point * inner_arc_radius
    outter_arc_point = dir_mid_point * outer_arc_radius
    if hinge.reversed_angle:
        inner_arc_point = intersection_point.sub(inner_arc_point)
        outter_arc_point = intersection_point.sub(outter_arc_point)
    else:    
        inner_arc_point = inner_arc_point.add(intersection_point)
        outter_arc_point = outter_arc_point.add(intersection_point)

    arc_middle_segment = Segment(outter_arc_point, inner_arc_point)