context: #region Copyright
  /*
  * Copyright (c) 2005-2010, OpenMI Association
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
  *       notice, this list of conditions and the following disclaimer in the
  *       documentation and/or other materials provided with the distribution.
  *     * Neither the name of the OpenMI Association nor the
  *       names of its contributors may be used to endorse or promote products
  *       derived from this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY "OpenMI Association" ``AS IS'' AND ANY
  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL "OpenMI Association" BE LIABLE FOR ANY
  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  #endregion 
  using System;
  using System.Collections.Generic;
  using System.Collections;
  using Oatc.OpenMI.Sdk.Spatial;
  using OpenMI.Standard2;
  using Oatc.OpenMI.Sdk.Backbone;
  using NUnit.Framework;
  using OpenMI.Standard2.TimeSpace;
  
  namespace Oatc.OpenMI.Sdk.UnitTests.Spatial
  {
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      [TestFixture]
      public class ElementMapperTest
      {
          private static ElementSet CreateElementSet(string elementSetID)
          {
              if (elementSetID == "4 Points")
              {
                  ElementSet fourPointsElementSet = new ElementSet("4 points","4 Points",ElementType.Point); 
  		
                  Element e0 = new Element("e0"); 
                  Element e1 = new Element("e1"); 
                  Element e2 = new Element("e2");
                  Element e3 = new Element("e3"); 
  		
                  e0.AddVertex(new Coordinate( 0,100,0));
                  e1.AddVertex(new Coordinate( 0,0,0));
                  e2.AddVertex(new Coordinate( 100,0,0));
                  e3.AddVertex(new Coordinate(100,100,0));
  
                  fourPointsElementSet.AddElement(e0);
                  fourPointsElementSet.AddElement(e1);
                  fourPointsElementSet.AddElement(e2);
                  fourPointsElementSet.AddElement(e3);
  
                  return fourPointsElementSet;
              }
              if (elementSetID == "2 Points")
              {
                  ElementSet twoPointsElementSet = new ElementSet("2 points","2 Points",ElementType.Point); 
  
                  Element k0 = new Element("k0"); 
                  Element k1 = new Element("k1"); 
  
                  k0.AddVertex(new Coordinate( 0,75,0));
                  k1.AddVertex(new Coordinate( 200, 50, 0));
  
                  twoPointsElementSet.AddElement(k0);
                  twoPointsElementSet.AddElement(k1);
  
                  return twoPointsElementSet;
  
              }
              if (elementSetID == "4 Other Points")
              {
                  ElementSet fourPointsElementSet = new ElementSet("4 Other points","4 Other Points",ElementType.Point); 
  		
                  Element e0 = new Element("e0"); 
                  Element e1 = new Element("e1"); 
                  Element e2 = new Element("e2");
                  Element e3 = new Element("e3"); 
  		
                  e0.AddVertex(new Coordinate( 0,15,0));
                  e1.AddVertex(new Coordinate( 5,15,0));
                  e2.AddVertex(new Coordinate( 0,10,0));
                  e3.AddVertex(new Coordinate(10,10,0));
  
                  fourPointsElementSet.AddElement(e0);
                  fourPointsElementSet.AddElement(e1);
                  fourPointsElementSet.AddElement(e2);
                  fourPointsElementSet.AddElement(e3);
  
                  return fourPointsElementSet;
              }
              if(elementSetID == "3 points polyline")
              {
                  ElementSet lineElementSet = new ElementSet("3 points polyline","3 points polyline",ElementType.PolyLine); 
  
                  Element l0 = new Element("k0"); 
                  Element l1 = new Element("k1");
   
                  Coordinate v0 = new Coordinate(0 ,20, 0);
                  Coordinate v1 = new Coordinate(0 ,10, 0);
                  Coordinate v2 = new Coordinate(0 , 0, 0);
  
                  l0.AddVertex(v0);
                  l0.AddVertex(v2);
  
                  l1.AddVertex(v1);
                  l1.AddVertex(v2);
  
                  lineElementSet.AddElement(l0);
                  lineElementSet.AddElement(l1);
                  return lineElementSet;
              }
              throw new Exception("Cound not find specified elementset");
          }
  
          [Test] // testing the Initialise method
          public void Initialise()
          {
              ElementSet fourPointsElementSet = CreateElementSet("4 Points");
              ElementSet twoPointsElementSet  = CreateElementSet("2 Points");
        
              ElementMapper elementMapper = new ElementMapper();
  
              IIdentifiable[] methods = SpatialAdaptedOutputFactory.GetAvailableMethods(fourPointsElementSet.ElementType, twoPointsElementSet.ElementType);
              elementMapper.Initialise(methods[0], fourPointsElementSet,  twoPointsElementSet);
  
              double calculated = elementMapper.GetValueFromMappingMatrix(0, 0);
              const double expected = 1;
  
              Assert.AreEqual(expected,calculated);
          }
  
          [Test] // testing the Initialise method
          public void UpdateMappingMatrix_PointPoint()
          {
              ElementSet fourPointsElementSet = CreateElementSet("4 Points");
              ElementSet twoPointsElementSet = CreateElementSet("2 Points");
  
              ElementMapper elementMapper = new ElementMapper();
  
              IIdentifiable[] methods = SpatialAdaptedOutputFactory.GetAvailableMethods(fourPointsElementSet.ElementType, twoPointsElementSet.ElementType);
  
              elementMapper.Initialise(methods[0], fourPointsElementSet,  twoPointsElementSet);
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(0, 0));
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(0, 0)
                                 +elementMapper.GetValueFromMappingMatrix(0, 1)
                                 +elementMapper.GetValueFromMappingMatrix(0, 2)
                                 +elementMapper.GetValueFromMappingMatrix(0, 3));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(1, 2));
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(1, 0)
                                 +elementMapper.GetValueFromMappingMatrix(1, 1)
                                 +elementMapper.GetValueFromMappingMatrix(1, 2)
                                 +elementMapper.GetValueFromMappingMatrix(1, 3));
    	      
              elementMapper.Initialise(methods[1], fourPointsElementSet,  twoPointsElementSet);
              Assert.AreEqual(0.56310461156889, elementMapper.GetValueFromMappingMatrix(0, 0),0.000000001);
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(0, 0)
                                 +elementMapper.GetValueFromMappingMatrix(0, 1)
                                 +elementMapper.GetValueFromMappingMatrix(0, 2)
                                 +elementMapper.GetValueFromMappingMatrix(0, 3));
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(1, 0)
                                 +elementMapper.GetValueFromMappingMatrix(1, 1)
                                 +elementMapper.GetValueFromMappingMatrix(1, 2)
                                 +elementMapper.GetValueFromMappingMatrix(1, 3),0.000000001);
          }
          [Test] // testing the Initialise method
          public void UpdateMappingMatrix_PointPolyline()
          {
              ElementSet fourPointsElementSet = CreateElementSet("4 Other Points");
              ElementSet lineElementSet = CreateElementSet("3 points polyline");
       
              ElementMapper elementMapper = new ElementMapper();
        
              // point to polyline
              IIdentifiable[] methods = SpatialAdaptedOutputFactory.GetAvailableMethods(fourPointsElementSet.ElementType, lineElementSet.ElementType);
  
              elementMapper.Initialise(methods[0], fourPointsElementSet, lineElementSet);
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(0, 0));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(0, 1));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(0, 2));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(0, 3));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(1, 0));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(1, 1));
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(1, 2));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(1, 3));
          
              elementMapper.Initialise(methods[0], fourPointsElementSet, lineElementSet);
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(0, 0));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(0, 1));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(0, 2));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(0, 3));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(1, 0));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(1, 1));
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(1, 2));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(1, 3));
          
              // polyline to point
              methods = SpatialAdaptedOutputFactory.GetAvailableMethods(lineElementSet.ElementType, fourPointsElementSet.ElementType);
              elementMapper.Initialise(methods[0], lineElementSet, fourPointsElementSet);
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(0, 0));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(0, 1));
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(1, 0));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(1, 1));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(2, 0));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(2, 1));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(3, 0));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(3, 1));
          
              elementMapper.Initialise(methods[1], lineElementSet, fourPointsElementSet);
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(0, 0));
              Assert.AreEqual(0, elementMapper.GetValueFromMappingMatrix(0, 1));
              Assert.AreEqual(0.585786437626905, elementMapper.GetValueFromMappingMatrix(1, 0));
              Assert.AreEqual(1, elementMapper.GetValueFromMappingMatrix(1, 0)
                                 + elementMapper.GetValueFromMappingMatrix(1, 1));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(2, 0));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(2, 1));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(3, 0));
              Assert.AreEqual(0.5, elementMapper.GetValueFromMappingMatrix(3, 1));      
          }
          [Test] // testing the Initialise method
          public void UpdateMappingMatrix_PointPolygon()
          {
              ElementSet gridElementSet = new ElementSet("gridElm","G1",ElementType.Polygon);
              ElementSet fourPointsElementSet = new ElementSet("4 points","4P",ElementType.Point); 
  
              Coordinate v_0_20  = new Coordinate(0,20,0);
              Coordinate v_0_10  = new Coordinate(0,10,0);
              Coordinate v_0_0   = new Coordinate(0, 0,0);
              Coordinate v_5_15  = new Coordinate(5,15,0);
  
gt:             Coordinate v_10_20 = new Coordinate(10,20,0);