agg_simul_eq.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // Permission to copy, use, modify, sell and distribute this software
  6. // is granted provided this copyright notice appears in all copies.
  7. // This software is provided "as is" without express or implied
  8. // warranty, and with no claim as to its suitability for any purpose.
  9. //
  10. //----------------------------------------------------------------------------
  11. // Contact: mcseem@antigrain.com
  12. // mcseemagg@yahoo.com
  13. // http://www.antigrain.com
  14. //----------------------------------------------------------------------------
  15. //
  16. // Solving simultaneous equations
  17. //
  18. //----------------------------------------------------------------------------
  19. using System;
  20. namespace MatterHackers.Agg
  21. {
  22. //============================================================matrix_pivot
  23. //template<uint Rows, uint Cols>
  24. public static class matrix_pivot
  25. {
  26. private static void swap_arrays_index1(double[,] a1, uint a1Index0, double[,] a2, uint a2Index0)
  27. {
  28. int Cols = a1.GetLength(1);
  29. if (a2.GetLength(1) != Cols)
  30. {
  31. throw new System.FormatException("a1 and a2 must have the same second dimension.");
  32. }
  33. for (int i = 0; i < Cols; i++)
  34. {
  35. double tmp = a1[a1Index0, i];
  36. a1[a1Index0, i] = a2[a2Index0, i];
  37. a2[a2Index0, i] = tmp;
  38. }
  39. }
  40. public static int pivot(double[,] m, uint row)
  41. {
  42. int k = (int)(row);
  43. double max_val, tmp;
  44. max_val = -1.0;
  45. int i;
  46. int Rows = m.GetLength(0);
  47. for (i = (int)row; i < Rows; i++)
  48. {
  49. if ((tmp = Math.Abs(m[i, row])) > max_val && tmp != 0.0)
  50. {
  51. max_val = tmp;
  52. k = i;
  53. }
  54. }
  55. if (m[k, row] == 0.0)
  56. {
  57. return -1;
  58. }
  59. if (k != (int)(row))
  60. {
  61. swap_arrays_index1(m, (uint)k, m, row);
  62. return k;
  63. }
  64. return 0;
  65. }
  66. };
  67. //===============================================================simul_eq
  68. //template<uint Size, uint RightCols>
  69. internal struct simul_eq
  70. {
  71. public static bool solve(double[,] left,
  72. double[,] right,
  73. double[,] result)
  74. {
  75. if (left.GetLength(0) != 4
  76. || right.GetLength(0) != 4
  77. || left.GetLength(1) != 4
  78. || result.GetLength(0) != 4
  79. || right.GetLength(1) != 2
  80. || result.GetLength(1) != 2)
  81. {
  82. throw new System.FormatException("left right and result must all be the same size.");
  83. }
  84. double a1;
  85. int Size = right.GetLength(0);
  86. int RightCols = right.GetLength(1);
  87. double[,] tmp = new double[Size, Size + RightCols];
  88. for (int i = 0; i < Size; i++)
  89. {
  90. for (int j = 0; j < Size; j++)
  91. {
  92. tmp[i, j] = left[i, j];
  93. }
  94. for (int j = 0; j < RightCols; j++)
  95. {
  96. tmp[i, Size + j] = right[i, j];
  97. }
  98. }
  99. for (int k = 0; k < Size; k++)
  100. {
  101. if (matrix_pivot.pivot(tmp, (uint)k) < 0)
  102. {
  103. return false; // Singularity....
  104. }
  105. a1 = tmp[k, k];
  106. for (int j = k; j < Size + RightCols; j++)
  107. {
  108. tmp[k, j] /= a1;
  109. }
  110. for (int i = k + 1; i < Size; i++)
  111. {
  112. a1 = tmp[i, k];
  113. for (int j = k; j < Size + RightCols; j++)
  114. {
  115. tmp[i, j] -= a1 * tmp[k, j];
  116. }
  117. }
  118. }
  119. for (int k = 0; k < RightCols; k++)
  120. {
  121. int m;
  122. for (m = (int)(Size - 1); m >= 0; m--)
  123. {
  124. result[m, k] = tmp[m, Size + k];
  125. for (int j = m + 1; j < Size; j++)
  126. {
  127. result[m, k] -= tmp[m, j] * result[j, k];
  128. }
  129. }
  130. }
  131. return true;
  132. }
  133. };
  134. }