public static void Join(Canguro.Model.Model model, IList<Joint> joints, IList<LineElement> lines, IList<AreaElement> areas)
{
System.Windows.Forms.Cursor cursor = System.Windows.Forms.Cursor.Current;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
try
{
Dictionary<uint, uint> deleteJoints = new Dictionary<uint, uint>();
ItemList<Joint> jList = model.JointList;
ItemList<LineElement> lList = model.LineList;
ItemList<AreaElement> aList = model.AreaList;
bool[] usedJoints = new bool[jList.Count];
// Find repeated joints
for (uint ii = 0; ii < joints.Count; ii++)
{
Joint j = joints[(int)ii];
if (j != null)
{
for (uint jj = 0; jj < j.Id; jj++)
{
Joint mj = jList[jj];
if (mj != null && equals(j, mj)
&& !deleteJoints.ContainsKey(j.Id))
{
deleteJoints.Add(j.Id, jj);
break;
}
}
}
System.Windows.Forms.Application.DoEvents();
}
// Move lines to current joints.
foreach (LineElement l in lines)
{
if (l != null && l.I != null && deleteJoints.ContainsKey(l.I.Id))
l.I = jList[deleteJoints[l.I.Id]];
if (l != null && l.J != null && deleteJoints.ContainsKey(l.J.Id))
l.J = jList[deleteJoints[l.J.Id]];
}
// Delete repeated and Zero-lenght lines.
for (int i = 0; i < lines.Count; i++)
{
LineElement l = lines[i];
if (l != null)
{
if (l.I.Id == l.J.Id || l.Length < 0.0001F)
lList.RemoveAt((int)l.Id);
else
for (int j = 0; j < (int)l.Id; j++)
{
LineElement ml = lList[j];
if (ml != null && (ml.Id < l.Id && equals(l, ml)))
{
lList.RemoveAt((int)l.Id);
ml.IsVisible = true;
break;
}
}
}
}
// Move areas to current joints.
foreach (AreaElement a in areas)
{
if (a != null && a.J1 != null && deleteJoints.ContainsKey(a.J1.Id))
a.J1 = jList[deleteJoints[a.J1.Id]];
if (a != null && a.J2 != null && deleteJoints.ContainsKey(a.J2.Id))
a.J2 = jList[deleteJoints[a.J2.Id]];
if (a != null && a.J3 != null && deleteJoints.ContainsKey(a.J3.Id))
a.J3 = jList[deleteJoints[a.J3.Id]];
if (a != null && a.J4 != null && deleteJoints.ContainsKey(a.J4.Id))
a.J4 = jList[deleteJoints[a.J4.Id]];
}
// Delete repeated and Zero-area Areas.
for (int i = 0; i < areas.Count; i++)
{
AreaElement a = areas[i];
if (a != null)
{
if ((a.J4 != null && (a.J1.Id == a.J2.Id || a.J1.Id == a.J3.Id || a.J1.Id == a.J4.Id || a.J2.Id == a.J3.Id || a.J2.Id == a.J4.Id || a.J3.Id == a.J4.Id)) ||
(a.J1.Id == a.J2.Id || a.J1.Id == a.J3.Id || a.J2.Id == a.J3.Id) || a.Area < 0.0001F)
aList.RemoveAt((int)a.Id);
else
for (int j = 0; j < (int)a.Id; j++)
{
AreaElement ml = aList[j];
if (ml != null && (ml.Id < a.Id && equals(a, ml)))
{
aList.RemoveAt((int)a.Id);
ml.IsVisible = true;
break;
}
}
}
}
// Delete repeated joints.
foreach (int id in deleteJoints.Keys)
jList.RemoveAt(id);
// Delete unused joints only if lines or areas are used.
if (lines.Count > 0 || areas.Count > 0)
{
foreach (LineElement l in lList)
if (l != null)
{
usedJoints[l.I.Id] = true;
usedJoints[l.J.Id] = true;
}
foreach (AreaElement a in aList)
if (a != null)
{
usedJoints[a.J1.Id] = true;
usedJoints[a.J2.Id] = true;
usedJoints[a.J3.Id] = true;
usedJoints[a.J4.Id] = true;
}
for (int i = 0; i < joints.Count; i++)
{
Joint j = joints[i];
if (j != null && !usedJoints[j.Id] && jList[j.Id] == j)
jList.RemoveAt((int)j.Id);
}
}
}
finally
{
System.Windows.Forms.Cursor.Current = cursor;
}
}