(*
 $Id$
 ISO TC184/SC4/WG12 N4781 - ISO/CD-TS 10303-1772 Ply orientation specification - EXPRESS MIM
 Supersedes ISO TC184/SC4/WG12 N4386
*)

SCHEMA Ply_orientation_specification_mim;

USE FROM Part_and_zone_laminate_tables_mim; -- ISO/CD-TS 10303-1770

USE FROM geometry_schema (point);

USE FROM representation_schema(
  compound_representation_item,
  representation_item_relationship);

  TYPE angle_direction_reference_select = SELECT (
    direction,
    curve,
    point_path);
  END_TYPE;

  TYPE angle_direction_reference_with_a2p3d_select = SELECT (
    angle_direction_reference_select,
    axis2_placement_3d);
  END_TYPE;

  TYPE orientation_basis_select = SELECT (
    axis2_placement_3d,
    min_and_major_ply_orientation_basis);
  END_TYPE;

  TYPE point_and_vector_member = SELECT (point, direction);
  END_TYPE;
  
  TYPE point_and_vector_members = LIST [2:3] OF point_and_vector_member;
  END_TYPE;

  TYPE point_path_members = LIST [1:?] OF point_and_vector;
  END_TYPE;

  ENTITY angle_direction_reference
    SUBTYPE OF (representation_item_relationship, geometric_representation_item);
      SELF\representation_item_relationship.relating_representation_item : orientation_basis_select;
      SELF\representation_item_relationship.related_representation_item  : angle_direction_reference_select;
    WHERE
      WR1: ((('PLY_ORIENTATION_SPECIFICATION_MIM.POINT_PATH' IN TYPEOF(related_representation_item)) AND
        ('PLY_ORIENTATION_SPECIFICATION_MIM.MIN_AND_MAJOR_PLY_ORIENTATION_BASIS' IN TYPEOF(relating_representation_item))) 
        OR
		(NOT('PLY_ORIENTATION_SPECIFICATION_MIM.POINT_PATH' IN TYPEOF(related_representation_item)) AND
        ('GEOMETRY_SCHEMA.AXIS2_PLACEMENT_3D' IN TYPEOF(relating_representation_item))));
  END_ENTITY;

  ENTITY draped_defined_transformation
    SUBTYPE OF (transformation_with_derived_angle);
  END_ENTITY;

  ENTITY laid_defined_transformation
    SUBTYPE OF (transformation_with_derived_angle);
  END_ENTITY;

  ENTITY min_and_major_ply_orientation_basis
    SUBTYPE OF (representation_item_relationship, geometric_representation_item);
      SELF\representation_item_relationship.relating_representation_item RENAMED minor_orientation_basis : axis2_placement_3d;
      SELF\representation_item_relationship.related_representation_item RENAMED major_orientation_basis  : axis2_placement_3d;
  END_ENTITY;

  ENTITY point_and_vector
    SUBTYPE OF (compound_representation_item, geometric_representation_item);
      SELF\compound_representation_item.item_element : point_and_vector_members;
  END_ENTITY;

  ENTITY point_path
    SUBTYPE OF (compound_representation_item, geometric_representation_item);
      SELF\compound_representation_item.item_element : point_path_members;
  END_ENTITY;

  ENTITY transformation_with_derived_angle
    SUPERTYPE OF (ONEOF (draped_defined_transformation, 
                         laid_defined_transformation))
    SUBTYPE OF (item_defined_transformation);
      SELF\item_defined_transformation.transform_item_1 : angle_direction_reference_with_a2p3d_select;
      SELF\item_defined_transformation.transform_item_2 : axis2_placement_3d;
    DERIVE
      orientation_angle : plane_angle_measure := derive_angle (
        SELF\item_defined_transformation.transform_item_1,
        SELF\item_defined_transformation.transform_item_2);
    WHERE
      WR1: (SELF\item_defined_transformation.transform_item_1\
            axis2_placement_3d.p[3].direction_ratios[1] =
            SELF\item_defined_transformation.transform_item_2\
            axis2_placement_3d.p[3].direction_ratios[1])
          AND
           (SELF\item_defined_transformation.transform_item_1\
            axis2_placement_3d.p[3].direction_ratios[2] =
            SELF\item_defined_transformation.transform_item_2\
            axis2_placement_3d.p[3].direction_ratios[2])
          AND
           (SELF\item_defined_transformation.transform_item_1\
            axis2_placement_3d.p[3].direction_ratios[3] =
            SELF\item_defined_transformation.transform_item_2\
            axis2_placement_3d.p[3].direction_ratios[3]);  
  END_ENTITY;

  SUBTYPE_CONSTRAINT pos_compound_representation_item_subtypes FOR compound_representation_item;         
    ONEOF (point_and_vector,
	  point_path);    
  END_SUBTYPE_CONSTRAINT;

  FUNCTION derive_angle (placement_1 : axis2_placement_3d;
                         placement_2 : axis2_placement_3d) :
                         plane_angle_measure;
    LOCAL
      v1     : direction;
      v2     : direction;
      mag_v1 : REAL;
      mag_v2 : REAL;
      theta  : plane_angle_measure;
    END_LOCAL;
    v1 := placement_1.p[1];
    v2 := placement_2.p[1];
    mag_v1 := SQRT (v1.direction_ratios[1]*v1.direction_ratios[1] +
                    v1.direction_ratios[2]*v1.direction_ratios[2]);
    mag_v2 := SQRT (v2.direction_ratios[1]*v2.direction_ratios[1] +
                    v2.direction_ratios[2]*v2.direction_ratios[2]);
    IF ((mag_v1 = 0.0) OR (mag_v2 = 0.0)) THEN
      theta := 0.0;
      RETURN (theta);
    END_IF;
    theta := ACOS ((v1.direction_ratios[1]*v2.direction_ratios[1] +
                    v1.direction_ratios[2]*v2.direction_ratios[2]) /
                   (mag_v1*mag_v2));
    RETURN (theta);
  END_FUNCTION; 

END_SCHEMA;
