PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/Isolines/IsolineBuilder.cs

#
C# | 724 lines | 569 code | 95 blank | 60 comment | 123 complexity | 087de97e1b4e20f0e9f07763d3523af4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Linq;
  3. using System.Diagnostics;
  4. using System.Runtime.Serialization;
  5. using System.Windows;
  6. using System.Windows.Media;
  7. using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
  8. using Microsoft.Research.DynamicDataDisplay.DataSources;
  9. using System.Collections.Generic;
  10. namespace Microsoft.Research.DynamicDataDisplay.Charts.Isolines
  11. {
  12. /// <summary>
  13. /// Generates geometric object for isolines of the input 2d scalar field.
  14. /// </summary>
  15. public sealed class IsolineBuilder
  16. {
  17. /// <summary>
  18. /// The density of isolines means the number of levels to draw.
  19. /// </summary>
  20. private int density = 12;
  21. private bool[,] processed;
  22. /// <summary>Number to be treated as missing value. NaN if no missing value is specified</summary>
  23. private double missingValue = Double.NaN;
  24. static IsolineBuilder()
  25. {
  26. SetCellDictionaries();
  27. }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="IsolineBuilder"/> class.
  30. /// </summary>
  31. public IsolineBuilder() { }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="IsolineBuilder"/> class for specified 2d scalar data source.
  34. /// </summary>
  35. /// <param name="dataSource">The data source with 2d scalar data.</param>
  36. public IsolineBuilder(IDataSource2D<double> dataSource)
  37. {
  38. DataSource = dataSource;
  39. }
  40. public double MissingValue
  41. {
  42. get
  43. {
  44. return missingValue;
  45. }
  46. set
  47. {
  48. missingValue = value;
  49. }
  50. }
  51. #region Private methods
  52. private static Dictionary<int, Dictionary<int, Edge>> dictChooser = new Dictionary<int, Dictionary<int, Edge>>();
  53. private static void SetCellDictionaries()
  54. {
  55. var bottomDict = new Dictionary<int, Edge>();
  56. bottomDict.Add((int)CellBitmask.RightBottom, Edge.Right);
  57. bottomDict.Add(Edge.Left,
  58. CellBitmask.LeftTop,
  59. CellBitmask.LeftBottom | CellBitmask.RightBottom | CellBitmask.RightTop,
  60. CellBitmask.LeftTop | CellBitmask.RightBottom | CellBitmask.RightTop,
  61. CellBitmask.LeftBottom);
  62. bottomDict.Add(Edge.Right,
  63. CellBitmask.RightTop,
  64. CellBitmask.LeftBottom | CellBitmask.RightBottom | CellBitmask.LeftTop,
  65. CellBitmask.LeftBottom | CellBitmask.LeftTop | CellBitmask.RightTop);
  66. bottomDict.Add(Edge.Top,
  67. CellBitmask.RightBottom | CellBitmask.RightTop,
  68. CellBitmask.LeftBottom | CellBitmask.LeftTop);
  69. var leftDict = new Dictionary<int, Edge>();
  70. leftDict.Add(Edge.Top,
  71. CellBitmask.LeftTop,
  72. CellBitmask.LeftBottom | CellBitmask.RightBottom | CellBitmask.RightTop);
  73. leftDict.Add(Edge.Right,
  74. CellBitmask.LeftTop | CellBitmask.RightTop,
  75. CellBitmask.LeftBottom | CellBitmask.RightBottom);
  76. leftDict.Add(Edge.Bottom,
  77. CellBitmask.RightBottom | CellBitmask.RightTop | CellBitmask.LeftTop,
  78. CellBitmask.LeftBottom);
  79. var topDict = new Dictionary<int, Edge>();
  80. topDict.Add(Edge.Right,
  81. CellBitmask.RightTop,
  82. CellBitmask.LeftTop | CellBitmask.LeftBottom | CellBitmask.RightBottom);
  83. topDict.Add(Edge.Right,
  84. CellBitmask.RightBottom,
  85. CellBitmask.LeftTop | CellBitmask.LeftBottom | CellBitmask.RightTop);
  86. topDict.Add(Edge.Left,
  87. CellBitmask.RightBottom | CellBitmask.RightTop | CellBitmask.LeftTop,
  88. CellBitmask.LeftBottom,
  89. CellBitmask.LeftTop,
  90. CellBitmask.LeftBottom | CellBitmask.RightBottom | CellBitmask.RightTop);
  91. topDict.Add(Edge.Bottom,
  92. CellBitmask.RightBottom | CellBitmask.RightTop,
  93. CellBitmask.LeftTop | CellBitmask.LeftBottom);
  94. var rightDict = new Dictionary<int, Edge>();
  95. rightDict.Add(Edge.Top,
  96. CellBitmask.RightTop,
  97. CellBitmask.LeftBottom | CellBitmask.RightBottom | CellBitmask.LeftTop);
  98. rightDict.Add(Edge.Left,
  99. CellBitmask.LeftTop | CellBitmask.RightTop,
  100. CellBitmask.LeftBottom | CellBitmask.RightBottom);
  101. rightDict.Add(Edge.Bottom,
  102. CellBitmask.RightBottom,
  103. CellBitmask.LeftTop | CellBitmask.LeftBottom | CellBitmask.RightTop);
  104. dictChooser.Add((int)Edge.Left, leftDict);
  105. dictChooser.Add((int)Edge.Right, rightDict);
  106. dictChooser.Add((int)Edge.Bottom, bottomDict);
  107. dictChooser.Add((int)Edge.Top, topDict);
  108. }
  109. private Edge GetOutEdge(Edge inEdge, ValuesInCell cv, IrregularCell rect, double value)
  110. {
  111. // value smaller than all values in corners or
  112. // value greater than all values in corners
  113. if (!cv.ValueBelongTo(value))
  114. {
  115. throw new IsolineGenerationException(Strings.Exceptions.IsolinesValueIsOutOfCell);
  116. }
  117. CellBitmask cellVal = cv.GetCellValue(value);
  118. var dict = dictChooser[(int)inEdge];
  119. if (dict.ContainsKey((int)cellVal))
  120. {
  121. Edge result = dict[(int)cellVal];
  122. switch (result)
  123. {
  124. case Edge.Left:
  125. if (cv.LeftTop.IsNaN() || cv.LeftBottom.IsNaN())
  126. result = Edge.None;
  127. break;
  128. case Edge.Right:
  129. if (cv.RightTop.IsNaN() || cv.RightBottom.IsNaN())
  130. result = Edge.None;
  131. break;
  132. case Edge.Top:
  133. if (cv.RightTop.IsNaN() || cv.LeftTop.IsNaN())
  134. result = Edge.None;
  135. break;
  136. case Edge.Bottom:
  137. if (cv.LeftBottom.IsNaN() || cv.RightBottom.IsNaN())
  138. result = Edge.None;
  139. break;
  140. }
  141. return result;
  142. }
  143. else if (cellVal.IsDiagonal())
  144. {
  145. return GetOutForOpposite(inEdge, cellVal, value, cv, rect);
  146. }
  147. const double near_zero = 0.0001;
  148. const double near_one = 1 - near_zero;
  149. double lt = cv.LeftTop;
  150. double rt = cv.RightTop;
  151. double rb = cv.RightBottom;
  152. double lb = cv.LeftBottom;
  153. switch (inEdge)
  154. {
  155. case Edge.Left:
  156. if (value == lt)
  157. value = near_one * lt + near_zero * lb;
  158. else if (value == lb)
  159. value = near_one * lb + near_zero * lt;
  160. else
  161. return Edge.None;
  162. // Now this is possible because of missing value
  163. //throw new IsolineGenerationException(Strings.Exceptions.IsolinesUnsupportedCase);
  164. break;
  165. case Edge.Top:
  166. if (value == rt)
  167. value = near_one * rt + near_zero * lt;
  168. else if (value == lt)
  169. value = near_one * lt + near_zero * rt;
  170. else
  171. return Edge.None;
  172. // Now this is possibe because of missing value
  173. //throw new IsolineGenerationException(Strings.Exceptions.IsolinesUnsupportedCase);
  174. break;
  175. case Edge.Right:
  176. if (value == rb)
  177. value = near_one * rb + near_zero * rt;
  178. else if (value == rt)
  179. value = near_one * rt + near_zero * rb;
  180. else
  181. return Edge.None;
  182. // Now this is possibe because of missing value
  183. //throw new IsolineGenerationException(Strings.Exceptions.IsolinesUnsupportedCase);
  184. break;
  185. case Edge.Bottom:
  186. if (value == rb)
  187. value = near_one * rb + near_zero * lb;
  188. else if (value == lb)
  189. value = near_one * lb + near_zero * rb;
  190. else
  191. return Edge.None;
  192. // Now this is possibe because of missing value
  193. //throw new IsolineGenerationException(Strings.Exceptions.IsolinesUnsupportedCase);
  194. break;
  195. }
  196. // Recursion?
  197. //return GetOutEdge(inEdge, cv, rect, value);
  198. return Edge.None;
  199. }
  200. private Edge GetOutForOpposite(Edge inEdge, CellBitmask cellVal, double value, ValuesInCell cellValues, IrregularCell rect)
  201. {
  202. Edge outEdge;
  203. SubCell subCell = GetSubCell(inEdge, value, cellValues);
  204. int iters = 1000; // max number of iterations
  205. do
  206. {
  207. ValuesInCell subValues = cellValues.GetSubCell(subCell);
  208. IrregularCell subRect = rect.GetSubRect(subCell);
  209. outEdge = GetOutEdge(inEdge, subValues, subRect, value);
  210. if (outEdge == Edge.None)
  211. return Edge.None;
  212. bool isAppropriate = subCell.IsAppropriate(outEdge);
  213. if (isAppropriate)
  214. {
  215. ValuesInCell sValues = subValues.GetSubCell(subCell);
  216. Point point = GetPointXY(outEdge, value, subValues, subRect);
  217. segments.AddPoint(point);
  218. return outEdge;
  219. }
  220. else
  221. {
  222. subCell = GetAdjacentEdge(subCell, outEdge);
  223. }
  224. byte e = (byte)outEdge;
  225. inEdge = (Edge)((e > 2) ? (e >> 2) : (e << 2));
  226. iters--;
  227. } while (iters >= 0);
  228. throw new IsolineGenerationException(Strings.Exceptions.IsolinesDataIsUndetailized);
  229. }
  230. private static SubCell GetAdjacentEdge(SubCell sub, Edge edge)
  231. {
  232. SubCell res = SubCell.LeftBottom;
  233. switch (sub)
  234. {
  235. case SubCell.LeftBottom:
  236. res = edge == Edge.Top ? SubCell.LeftTop : SubCell.RightBottom;
  237. break;
  238. case SubCell.LeftTop:
  239. res = edge == Edge.Bottom ? SubCell.LeftBottom : SubCell.RightTop;
  240. break;
  241. case SubCell.RightBottom:
  242. res = edge == Edge.Top ? SubCell.RightTop : SubCell.LeftBottom;
  243. break;
  244. case SubCell.RightTop:
  245. default:
  246. res = edge == Edge.Bottom ? SubCell.RightBottom : SubCell.LeftTop;
  247. break;
  248. }
  249. return res;
  250. }
  251. private static SubCell GetSubCell(Edge inEdge, double value, ValuesInCell vc)
  252. {
  253. double lb = vc.LeftBottom;
  254. double rb = vc.RightBottom;
  255. double rt = vc.RightTop;
  256. double lt = vc.LeftTop;
  257. SubCell res = SubCell.LeftBottom;
  258. switch (inEdge)
  259. {
  260. case Edge.Left:
  261. res = (Math.Abs(value - lb) < Math.Abs(value - lt)) ? SubCell.LeftBottom : SubCell.LeftTop;
  262. break;
  263. case Edge.Top:
  264. res = (Math.Abs(value - lt) < Math.Abs(value - rt)) ? SubCell.LeftTop : SubCell.RightTop;
  265. break;
  266. case Edge.Right:
  267. res = (Math.Abs(value - rb) < Math.Abs(value - rt)) ? SubCell.RightBottom : SubCell.RightTop;
  268. break;
  269. case Edge.Bottom:
  270. default:
  271. res = (Math.Abs(value - lb) < Math.Abs(value - rb)) ? SubCell.LeftBottom : SubCell.RightBottom;
  272. break;
  273. }
  274. ValuesInCell subValues = vc.GetSubCell(res);
  275. bool valueInside = subValues.ValueBelongTo(value);
  276. if (!valueInside)
  277. {
  278. throw new IsolineGenerationException(Strings.Exceptions.IsolinesDataIsUndetailized);
  279. }
  280. return res;
  281. }
  282. private static Point GetPoint(double value, double a1, double a2, Vector v1, Vector v2)
  283. {
  284. double ratio = (value - a1) / (a2 - a1);
  285. Verify.IsTrue(0 <= ratio && ratio <= 1);
  286. Vector r = (1 - ratio) * v1 + ratio * v2;
  287. return new Point(r.X, r.Y);
  288. }
  289. private Point GetPointXY(Edge edge, double value, ValuesInCell vc, IrregularCell rect)
  290. {
  291. double lt = vc.LeftTop;
  292. double lb = vc.LeftBottom;
  293. double rb = vc.RightBottom;
  294. double rt = vc.RightTop;
  295. switch (edge)
  296. {
  297. case Edge.Left:
  298. return GetPoint(value, lb, lt, rect.LeftBottom, rect.LeftTop);
  299. case Edge.Top:
  300. return GetPoint(value, lt, rt, rect.LeftTop, rect.RightTop);
  301. case Edge.Right:
  302. return GetPoint(value, rb, rt, rect.RightBottom, rect.RightTop);
  303. case Edge.Bottom:
  304. return GetPoint(value, lb, rb, rect.LeftBottom, rect.RightBottom);
  305. default:
  306. throw new InvalidOperationException();
  307. }
  308. }
  309. private bool BelongsToEdge(double value, double edgeValue1, double edgeValue2, bool onBoundary)
  310. {
  311. if (!Double.IsNaN(missingValue) && (edgeValue1 == missingValue || edgeValue2 == missingValue))
  312. return false;
  313. if (onBoundary)
  314. {
  315. return (edgeValue1 <= value && value < edgeValue2) ||
  316. (edgeValue2 <= value && value < edgeValue1);
  317. }
  318. else
  319. {
  320. return (edgeValue1 < value && value < edgeValue2) ||
  321. (edgeValue2 < value && value < edgeValue1);
  322. }
  323. }
  324. private bool IsPassed(Edge edge, int i, int j, byte[,] edges)
  325. {
  326. switch (edge)
  327. {
  328. case Edge.Left:
  329. return (i == 0) || (edges[i, j] & (byte)edge) != 0;
  330. case Edge.Bottom:
  331. return (j == 0) || (edges[i, j] & (byte)edge) != 0;
  332. case Edge.Top:
  333. return (j == edges.GetLength(1) - 2) || (edges[i, j + 1] & (byte)Edge.Bottom) != 0;
  334. case Edge.Right:
  335. return (i == edges.GetLength(0) - 2) || (edges[i + 1, j] & (byte)Edge.Left) != 0;
  336. default:
  337. throw new InvalidOperationException();
  338. }
  339. }
  340. private void MakeEdgePassed(Edge edge, int i, int j)
  341. {
  342. switch (edge)
  343. {
  344. case Edge.Left:
  345. case Edge.Bottom:
  346. edges[i, j] |= (byte)edge;
  347. break;
  348. case Edge.Top:
  349. edges[i, j + 1] |= (byte)Edge.Bottom;
  350. break;
  351. case Edge.Right:
  352. edges[i + 1, j] |= (byte)Edge.Left;
  353. break;
  354. default:
  355. throw new InvalidOperationException();
  356. }
  357. }
  358. private Edge TrackLine(Edge inEdge, double value, ref int x, ref int y, out double newX, out double newY)
  359. {
  360. // Getting output edge
  361. ValuesInCell vc = (missingValue.IsNaN()) ?
  362. (new ValuesInCell(values[x, y],
  363. values[x + 1, y],
  364. values[x + 1, y + 1],
  365. values[x, y + 1])) :
  366. (new ValuesInCell(values[x, y],
  367. values[x + 1, y],
  368. values[x + 1, y + 1],
  369. values[x, y + 1],
  370. missingValue));
  371. IrregularCell rect = new IrregularCell(
  372. grid[x, y],
  373. grid[x + 1, y],
  374. grid[x + 1, y + 1],
  375. grid[x, y + 1]);
  376. Edge outEdge = GetOutEdge(inEdge, vc, rect, value);
  377. if (outEdge == Edge.None)
  378. {
  379. newX = newY = -1; // Impossible cell indices
  380. return Edge.None;
  381. }
  382. // Drawing new segment
  383. Point point = GetPointXY(outEdge, value, vc, rect);
  384. newX = point.X;
  385. newY = point.Y;
  386. segments.AddPoint(point);
  387. processed[x, y] = true;
  388. // Whether out-edge already was passed?
  389. if (IsPassed(outEdge, x, y, edges)) // line is closed
  390. {
  391. //MakeEdgePassed(outEdge, x, y); // boundaries should be marked as passed too
  392. return Edge.None;
  393. }
  394. // Make this edge passed
  395. MakeEdgePassed(outEdge, x, y);
  396. // Getting next cell's indices
  397. switch (outEdge)
  398. {
  399. case Edge.Left:
  400. x--;
  401. return Edge.Right;
  402. case Edge.Top:
  403. y++;
  404. return Edge.Bottom;
  405. case Edge.Right:
  406. x++;
  407. return Edge.Left;
  408. case Edge.Bottom:
  409. y--;
  410. return Edge.Top;
  411. default:
  412. throw new InvalidOperationException();
  413. }
  414. }
  415. private void TrackLineNonRecursive(Edge inEdge, double value, int x, int y)
  416. {
  417. int s = x, t = y;
  418. ValuesInCell vc = (missingValue.IsNaN()) ?
  419. (new ValuesInCell(values[x, y],
  420. values[x + 1, y],
  421. values[x + 1, y + 1],
  422. values[x, y + 1])) :
  423. (new ValuesInCell(values[x, y],
  424. values[x + 1, y],
  425. values[x + 1, y + 1],
  426. values[x, y + 1],
  427. missingValue));
  428. IrregularCell rect = new IrregularCell(
  429. grid[x, y],
  430. grid[x + 1, y],
  431. grid[x + 1, y + 1],
  432. grid[x, y + 1]);
  433. Point point = GetPointXY(inEdge, value, vc, rect);
  434. segments.StartLine(point, (value - minMax.Min) / (minMax.Max - minMax.Min), value);
  435. MakeEdgePassed(inEdge, x, y);
  436. //processed[x, y] = true;
  437. double x2, y2;
  438. do
  439. {
  440. inEdge = TrackLine(inEdge, value, ref s, ref t, out x2, out y2);
  441. } while (inEdge != Edge.None);
  442. }
  443. #endregion
  444. private bool HasIsoline(int x, int y)
  445. {
  446. return (edges[x, y] != 0 &&
  447. ((x < edges.GetLength(0) - 1 && edges[x + 1, y] != 0) ||
  448. (y < edges.GetLength(1) - 1 && edges[x, y + 1] != 0)));
  449. }
  450. /// <summary>Finds isoline for specified reference value</summary>
  451. /// <param name="value">Reference value</param>
  452. private void PrepareCells(double value)
  453. {
  454. double currentRatio = (value - minMax.Min) / (minMax.Max - minMax.Min);
  455. if (currentRatio < 0 || currentRatio > 1)
  456. return; // No contour lines for such value
  457. int xSize = dataSource.Width;
  458. int ySize = dataSource.Height;
  459. int x, y;
  460. for (x = 0; x < xSize; x++)
  461. for (y = 0; y < ySize; y++)
  462. edges[x, y] = 0;
  463. processed = new bool[xSize, ySize];
  464. // Looking in boundaries.
  465. // left
  466. for (y = 1; y < ySize; y++)
  467. {
  468. if (BelongsToEdge(value, values[0, y - 1], values[0, y], true) &&
  469. (edges[0, y - 1] & (byte)Edge.Left) == 0)
  470. {
  471. TrackLineNonRecursive(Edge.Left, value, 0, y - 1);
  472. }
  473. }
  474. // bottom
  475. for (x = 0; x < xSize - 1; x++)
  476. {
  477. if (BelongsToEdge(value, values[x, 0], values[x + 1, 0], true)
  478. && (edges[x, 0] & (byte)Edge.Bottom) == 0)
  479. {
  480. TrackLineNonRecursive(Edge.Bottom, value, x, 0);
  481. };
  482. }
  483. // right
  484. x = xSize - 1;
  485. for (y = 1; y < ySize; y++)
  486. {
  487. // Is this correct?
  488. //if (BelongsToEdge(value, values[0, y - 1], values[0, y], true) &&
  489. // (edges[0, y - 1] & (byte)Edge.Left) == 0)
  490. //{
  491. // TrackLineNonRecursive(Edge.Left, value, 0, y - 1);
  492. //};
  493. if (BelongsToEdge(value, values[x, y - 1], values[x, y], true) &&
  494. (edges[x, y - 1] & (byte)Edge.Left) == 0)
  495. {
  496. TrackLineNonRecursive(Edge.Right, value, x - 1, y - 1);
  497. };
  498. }
  499. // horizontals
  500. for (x = 1; x < xSize - 1; x++)
  501. for (y = 1; y < ySize - 1; y++)
  502. {
  503. if ((edges[x, y] & (byte)Edge.Bottom) == 0 &&
  504. BelongsToEdge(value, values[x, y], values[x + 1, y], false) &&
  505. !processed[x, y - 1])
  506. {
  507. TrackLineNonRecursive(Edge.Top, value, x, y - 1);
  508. }
  509. if ((edges[x, y] & (byte)Edge.Bottom) == 0 &&
  510. BelongsToEdge(value, values[x, y], values[x + 1, y], false) &&
  511. !processed[x, y])
  512. {
  513. TrackLineNonRecursive(Edge.Bottom, value, x, y);
  514. }
  515. if ((edges[x, y] & (byte)Edge.Left) == 0 &&
  516. BelongsToEdge(value, values[x, y], values[x, y - 1], false) &&
  517. !processed[x - 1, y - 1])
  518. {
  519. TrackLineNonRecursive(Edge.Right, value, x - 1, y - 1);
  520. }
  521. if ((edges[x, y] & (byte)Edge.Left) == 0 &&
  522. BelongsToEdge(value, values[x, y], values[x, y - 1], false) &&
  523. !processed[x, y - 1])
  524. {
  525. TrackLineNonRecursive(Edge.Left, value, x, y - 1);
  526. }
  527. }
  528. }
  529. /// <summary>
  530. /// Builds isoline data for 2d scalar field contained in data source.
  531. /// </summary>
  532. /// <returns>Collection of data describing built isolines.</returns>
  533. public IsolineCollection BuildIsoline()
  534. {
  535. VerifyDataSource();
  536. segments = new IsolineCollection();
  537. // Cannot draw isolines for fields with one dimension lesser than 2
  538. if (dataSource.Width < 2 || dataSource.Height < 2)
  539. return segments;
  540. Init();
  541. if (!minMax.IsEmpty)
  542. {
  543. values = dataSource.Data;
  544. double[] levels = GetLevelsForIsolines();
  545. foreach (double level in levels)
  546. {
  547. PrepareCells(level);
  548. }
  549. if (segments.Lines.Count > 0 && segments.Lines[segments.Lines.Count - 1].OtherPoints.Count == 0)
  550. segments.Lines.RemoveAt(segments.Lines.Count - 1);
  551. }
  552. return segments;
  553. }
  554. private void Init()
  555. {
  556. if (dataSource.Range.HasValue)
  557. minMax = dataSource.Range.Value;
  558. else
  559. minMax = (Double.IsNaN(missingValue) ? dataSource.GetMinMax() : dataSource.GetMinMax(missingValue));
  560. if (dataSource.MissingValue.HasValue)
  561. missingValue = dataSource.MissingValue.Value;
  562. segments.Min = minMax.Min;
  563. segments.Max = minMax.Max;
  564. }
  565. /// <summary>
  566. /// Builds isoline data for the specified level in 2d scalar field.
  567. /// </summary>
  568. /// <param name="level">The level.</param>
  569. /// <returns></returns>
  570. public IsolineCollection BuildIsoline(double level)
  571. {
  572. VerifyDataSource();
  573. segments = new IsolineCollection();
  574. Init();
  575. if (!minMax.IsEmpty)
  576. {
  577. values = dataSource.Data;
  578. PrepareCells(level);
  579. if (segments.Lines.Count > 0 && segments.Lines[segments.Lines.Count - 1].OtherPoints.Count == 0)
  580. segments.Lines.RemoveAt(segments.Lines.Count - 1);
  581. }
  582. return segments;
  583. }
  584. private void VerifyDataSource()
  585. {
  586. if (dataSource == null)
  587. throw new InvalidOperationException(Strings.Exceptions.IsolinesDataSourceShouldBeSet);
  588. }
  589. IsolineCollection segments;
  590. private double[,] values;
  591. private byte[,] edges;
  592. private Point[,] grid;
  593. private Range<double> minMax;
  594. private IDataSource2D<double> dataSource;
  595. /// <summary>
  596. /// Gets or sets the data source - 2d scalar field.
  597. /// </summary>
  598. /// <value>The data source.</value>
  599. public IDataSource2D<double> DataSource
  600. {
  601. get { return dataSource; }
  602. set
  603. {
  604. if (dataSource != value)
  605. {
  606. value.VerifyNotNull("value");
  607. dataSource = value;
  608. grid = dataSource.Grid;
  609. edges = new byte[dataSource.Width, dataSource.Height];
  610. }
  611. }
  612. }
  613. private const double shiftPercent = 0.05;
  614. private double[] GetLevelsForIsolines()
  615. {
  616. double[] levels;
  617. double min = minMax.Min;
  618. double max = minMax.Max;
  619. double step = (max - min) / (density - 1);
  620. double delta = (max - min);
  621. levels = new double[density];
  622. levels[0] = min + delta * shiftPercent;
  623. levels[levels.Length - 1] = max - delta * shiftPercent;
  624. for (int i = 1; i < levels.Length - 1; i++)
  625. levels[i] = min + i * step;
  626. return levels;
  627. }
  628. }
  629. }