private bool isSameType(SnapPoint p1, SnapPoint p2)
 {
     if (p1.SnapType == p2.SnapType)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        private bool isInSnapRange(SnapPoint p1, SnapPoint p2)
        {
            float distance = (p2.Position - p1.Position).Length();

            if (distance < snapRange)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /*
         * public void UpdateSnaps()
         * {
         *  for (int i = 0; i < ActiveSnapList.Count-1; i++)
         *  {
         *      SnapInformation inf1 = ActiveSnapList[i];
         *      SnapInformation inf2 = ActiveSnapList[i+1];
         *
         *      //Point to point only
         *      for(int j=0; j<inf1.SnapList.Count;j++)
         *      {
         *          SnapPoint p1 = inf1.SnapList[j];
         *              for(int k=0; k<inf2.SnapList.Count;k++)
         *              {
         *                  SnapPoint p2 = inf2.SnapList[k];
         *
         *                  if (isInSnapRange(p1, p2))
         *                  {
         *
         *                  }
         *              }
         *      }
         *
         *
         *  }
         * }*/

        public void SnapTo(SnapInformation source, SnapInformation target)
        {
            //Point to point only
            for (int j = 0; j < target.SnapList.Count; j++)
            {
                SnapPoint pTarget = target.SnapList[j];
                for (int k = 0; k < source.SnapList.Count; k++)
                {
                    SnapPoint pSource = source.SnapList[k];

                    if (isSameType(pTarget, pSource) && isInSnapRange(pTarget, pSource))
                    {
                        source.Snap(pSource, pTarget);
                    }
                }
            }
        }
        //Doesn't belong here, only used in first tests
        public void Snap(SnapPoint source, SnapPoint target)
        {
            Vector3 snapPointPos = source.Position;

            //Rotate by Normal
            Vector3 sNormal = source.Normal;
            Vector3 tNormal = target.Normal;

            float   normalRotationAngle = (float)Math.Acos((Vector3.Dot(sNormal, tNormal))) + MathHelper.Pi;
            Vector3 normalRotationAxis  = Vector3.Cross(sNormal, tNormal);

            Matrix normalRotationMatrix = Matrix.CreateFromAxisAngle(normalRotationAxis, normalRotationAngle);

            for (int i = 0; i < SnapList.Count; i++)
            {
                SnapPoint point = SnapList[i];
                point.Position = point.Position - snapPointPos;

                point.Position = Vector3.Transform(point.Position, normalRotationMatrix);
                point.Normal   = Vector3.Transform(point.Normal, normalRotationMatrix);
                point.Up       = Vector3.Transform(point.Up, normalRotationMatrix);

                point.Position = point.Position + snapPointPos;
            }


            //Rotate by Up
            Vector3 sUp = source.Up;
            Vector3 tUp = target.Up;

            float upRotationAngle = (float)Math.Acos((Vector3.Dot(sUp, tUp)));

            Vector3 upRotationAxis = new Vector3();

            if (sUp == tUp)
            {
                upRotationAxis = target.Normal * (-1);
            }
            else
            {
                upRotationAxis = Vector3.Cross(sUp, tUp);
            }

            Matrix upRotationMatrix = Matrix.CreateFromAxisAngle(upRotationAxis, upRotationAngle);

            for (int i = 0; i < SnapList.Count; i++)
            {
                SnapPoint point = SnapList[i];
                point.Position = point.Position - snapPointPos;

                point.Position = Vector3.Transform(point.Position, upRotationMatrix);
                point.Normal   = Vector3.Transform(point.Normal, upRotationMatrix);
                point.Up       = Vector3.Transform(point.Up, upRotationMatrix);

                point.Position = point.Position + snapPointPos;
            }


            //Translate
            Vector3 translation = target.Position - source.Position;

            for (int i = 0; i < SnapList.Count; i++)
            {
                SnapPoint point = SnapList[i];
                point.Position = point.Position + translation;
            }
        }
 public void addSnapObject(SnapPoint item)
 {
     SnapList.Add(item);
 }
        public void SnapAToB(object A, object B, Transformation transformationB, List <Transformation> outTransformations)
        {
            SnapPoint PointA = (SnapPoint)A;
            SnapPoint PointB = (SnapPoint)B;

            if (Math.Abs(Vector3.Dot(PointA.Up, PointA.Normal)) > 0.001f)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(Vector3.Dot(PointB.Up, PointB.Normal)) > 0.001f)
            {
                throw new InvalidOperationException();
            }

            var newPoint = new SnapPoint();

            newPoint.ClockwiseWinding = PointB.ClockwiseWinding;
            newPoint.Position         = Vector3.Transform(PointB.Position, transformationB.CreateMatrix());
            newPoint.Normal           = Vector3.Transform(PointB.Normal, transformationB.Rotation);
            newPoint.Up = Vector3.Transform(PointB.Up, transformationB.Rotation);

            PointB = newPoint;

            if (PointA.ClockwiseWinding == PointB.ClockwiseWinding)
            {
                return;
            }

            Vector3 sNormal             = PointA.Normal;
            Vector3 tNormal             = PointB.Normal;
            float   normalRotationAngle = (float)Math.Acos((Vector3.Dot(sNormal, tNormal))) + MathHelper.Pi;
            Vector3 normalRotationAxis  = Vector3.Normalize(Vector3.Cross(sNormal, tNormal));

            if (Math.Abs(Vector3.Dot(sNormal, tNormal)) > 0.999)
            {
                normalRotationAxis = PointB.Up * (-1);
            }

            Quaternion normalRotation = Quaternion.CreateFromAxisAngle(normalRotationAxis, normalRotationAngle);

            Vector3 sUp             = Vector3.Transform(PointA.Up, normalRotation);
            Vector3 tUp             = PointB.Up;
            float   upRotationAngle = (float)Math.Acos((Vector3.Dot(sUp, tUp)));

            Vector3 upRotationAxis;

            if (Math.Abs(Vector3.Dot(sUp, tUp)) > 0.999)
            {
                upRotationAxis = PointB.Normal * (-1);
            }
            else
            {
                upRotationAxis = Vector3.Normalize(Vector3.Cross(sUp, tUp));
            }

            if (Math.Abs(Vector3.Dot(upRotationAxis, tNormal)) < 0.999f)
            {
                throw new InvalidOperationException();
            }

            Quaternion upRotationMatrix = Quaternion.CreateFromAxisAngle(upRotationAxis, upRotationAngle);
            Quaternion Rotation         = upRotationMatrix * normalRotation;

            Vector3 translation = PointB.Position - Vector3.Transform(PointA.Position, Rotation);

            Quaternion     RotQuat        = Rotation;
            Transformation transformation = new Transformation(new Vector3(1, 1, 1), RotQuat, translation);


            //TODO: SCALING NOT SUPPORTED: transformation.Scaling *= transformationB.Scaling;

            /*transformation.Rotation = transformation.Rotation * transformationB.Rotation;
             * transformation.Translation += transformationB.Translation;*/


            outTransformations.Add(transformation);
        }