File: SessionCollection.cs

package info (click to toggle)
quickroute-gps 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,576 kB
  • sloc: cs: 74,488; makefile: 72; sh: 43
file content (178 lines) | stat: -rw-r--r-- 5,496 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace QuickRoute.BusinessEntities
{
  [Serializable]
  public class SessionCollection : ICollection<Session>
  {
    private List<Session> sessions = new List<Session>();

    public Session this[int index]
    {
      get { return sessions[index]; }
      set
      {
        sessions[index] = value;
      }
    }

    /// <summary>
    /// Using linear least squares algorithm described at http://en.wikipedia.org/wiki/Linear_least_squares
    /// </summary>
    /// <returns></returns>
    public Transformation CalculateAverageTransformation()
    {
      var averageProjectionOrigin = new LongLat();
      foreach (var session in this)
      {
        averageProjectionOrigin += session.ProjectionOrigin / Count;
      }
      
      if (Count == 0) return null;
      var n = 4;
      var XtX = new GeneralMatrix(n, n);
      var Xty = new GeneralMatrix(n, 1);
      var numberOfUnknowns = 0;
      
      foreach (var session in this)
      {
        var m = session.Handles.Length;
        if (m < 2) continue;
        numberOfUnknowns += m;

        var startDistance = session.Route.GetAttributeFromParameterizedLocation(WaypointAttribute.Distance, session.Route.FirstPL).Value;
        for (var i = 0; i < m; i++)
        {
          var longLat = session.Route.GetLocationFromParameterizedLocation(session.Handles[i].ParameterizedLocation);
          var p = longLat.Project(averageProjectionOrigin); // projected point on earth (metres)
          var q = session.Handles[i].Location; // point on map image (pixels)
          var endDistance = (i != m - 1)
                              ? (session.Route.GetAttributeFromParameterizedLocation(WaypointAttribute.Distance, session.Handles[i].ParameterizedLocation).Value +
                                 session.Route.GetAttributeFromParameterizedLocation(WaypointAttribute.Distance, session.Handles[i + 1].ParameterizedLocation).Value ) / 2
                              : session.Route.GetAttributeFromParameterizedLocation(WaypointAttribute.Distance, session.Route.LastPL).Value;
          var w = endDistance - startDistance; // weight
          startDistance = endDistance;

          XtX.SetElement(0, 0, XtX.GetElement(0, 0) + w * (p.X * p.X + p.Y * p.Y));
          XtX.SetElement(0, 2, XtX.GetElement(0, 2) + w * p.X);
          XtX.SetElement(0, 3, XtX.GetElement(0, 3) - w * p.Y);
          XtX.SetElement(1, 1, XtX.GetElement(1, 1) + w * (p.X * p.X + p.Y * p.Y));
          XtX.SetElement(1, 2, XtX.GetElement(1, 2) + w * p.Y);
          XtX.SetElement(1, 3, XtX.GetElement(1, 3) + w * p.X);
          XtX.SetElement(2, 0, XtX.GetElement(2, 0) + w * p.X);
          XtX.SetElement(2, 1, XtX.GetElement(2, 1) + w * p.Y);
          XtX.SetElement(2, 2, XtX.GetElement(2, 2) + w);
          XtX.SetElement(3, 0, XtX.GetElement(3, 0) - w * p.Y);
          XtX.SetElement(3, 1, XtX.GetElement(3, 1) + w * p.X);
          XtX.SetElement(3, 3, XtX.GetElement(3, 3) + w);

          Xty.SetElement(0, 0, Xty.GetElement(0, 0) + w * (q.X * p.X - q.Y * p.Y));
          Xty.SetElement(1, 0, Xty.GetElement(1, 0) + w * (q.X * p.Y + q.Y * p.X));
          Xty.SetElement(2, 0, Xty.GetElement(2, 0) + w * q.X);
          Xty.SetElement(3, 0, Xty.GetElement(3, 0) + w * q.Y);
        }
      }

      var T = new GeneralMatrix(3, 3);

      if (numberOfUnknowns == 0)
      {
        T = this[0].InitialTransformationMatrix;
      }
      else if (numberOfUnknowns == 1)
      {
        T = this[0].Handles[0].TransformationMatrix;
      }
      else
      {
        var B = XtX.QRD().Solve(Xty);

        T.SetElement(0, 0, B.GetElement(0, 0));
        T.SetElement(0, 1, B.GetElement(1, 0));
        T.SetElement(0, 2, B.GetElement(2, 0));
        T.SetElement(1, 0, B.GetElement(1, 0));
        T.SetElement(1, 1, -B.GetElement(0, 0));
        T.SetElement(1, 2, B.GetElement(3, 0));
        T.SetElement(2, 0, 0);
        T.SetElement(2, 1, 0);
        T.SetElement(2, 2, 1);
      }
      return new Transformation(T, averageProjectionOrigin);
    }

    /// <summary>
    /// Creates a shallow copy.
    /// </summary>
    /// <returns></returns>
    public SessionCollection Copy()
    {
      var copy = new SessionCollection();
      foreach (var s in sessions)
      {
        copy.Add(s);
      }
      return copy;
    }

    #region ICollection<Session> Members

    public void Add(Session item)
    {
      sessions.Add(item);
    }

    public void Clear()
    {
      sessions.Clear();
    }

    public bool Contains(Session item)
    {
      return sessions.Contains(item);
    }

    public void CopyTo(Session[] array, int arrayIndex)
    {
      sessions.CopyTo(array, arrayIndex);
    }

    public int Count
    {
      get { return sessions.Count; }
    }

    public bool IsReadOnly
    {
      get { return false; }
    }

    public bool Remove(Session item)
    {
      return sessions.Remove(item);
    }

    #endregion

    #region IEnumerable<Session> Members

    public IEnumerator<Session> GetEnumerator()
    {
      return sessions.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
      return sessions.GetEnumerator();
    }

    #endregion
  }

}