Go to the documentation of this file.00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using Microsoft.Xna.Framework;
00006
00007 namespace visLU2
00008 {
00012 public struct SamplePoint
00013 {
00014 Vector2 position;
00015 int streamLineIndex;
00016 int samplePointIndex;
00017
00018 #region Properties
00019 public Vector2 Position
00020 {
00021 get { return position; }
00022 set { position = value; }
00023 }
00024 public int StreamLineIndex
00025 {
00026 get { return streamLineIndex; }
00027 set { streamLineIndex = value; }
00028 }
00029 public int SamplePointIndex
00030 {
00031 get { return samplePointIndex; }
00032 set { samplePointIndex = value; }
00033 }
00034 #endregion
00035
00036 public SamplePoint(Vector2 position, int streamLineIndex, int samplePointIndex)
00037 {
00038 this.position = position;
00039 this.streamLineIndex = streamLineIndex;
00040 this.samplePointIndex = samplePointIndex;
00041 }
00042 }
00043
00047 class CartesianGrid
00048 {
00049 private float cellSize;
00050 private float cellSize_div;
00051 private List<SamplePoint>[,] grid;
00052 int xSize, ySize;
00053
00060 public CartesianGrid(float dsep, float dataWidth, float dataHeight)
00061 {
00062 cellSize = dsep;
00063 cellSize_div = 1.0f / cellSize;
00064 xSize = (int)(dataWidth * cellSize_div)+1;
00065 ySize = (int)(dataHeight * cellSize_div)+1;
00066 grid = new List<SamplePoint>[xSize,ySize];
00067 }
00068
00069 public float CellSize
00070 {
00071 get { return cellSize; }
00072 set { cellSize = value; cellSize_div = 1.0f / cellSize; }
00073 }
00074
00075 public List<SamplePoint> getPointsToCheck(Vector2 position)
00076 {
00077 Vector2 cellAdress = position;
00078 int cellAdressX = (int)(((int)cellAdress.X) * cellSize_div);
00079 int cellAdressY = (int)(((int)cellAdress.Y) * cellSize_div);
00080 int dimensionX = xSize;
00081 int dimensionY = ySize;
00082 List<SamplePoint> check = new List<SamplePoint>();
00083 List<SamplePoint> currentCell = grid[cellAdressX,cellAdressY];
00084 if (currentCell != null)
00085 {
00086 check.AddRange(currentCell);
00087 }
00088 #region add sourrounding cells
00089 if (cellAdressX - 1 > 0)
00090 {
00091 currentCell = grid[cellAdressX - 1, cellAdressY];
00092 if (currentCell != null)
00093 {
00094 check.AddRange(currentCell);
00095 }
00096 }
00097 if (cellAdressX + 1 < dimensionX)
00098 {
00099 currentCell = grid[cellAdressX + 1, cellAdressY];
00100 if (currentCell != null)
00101 {
00102 check.AddRange(currentCell);
00103 }
00104 }
00105 if (cellAdressY - 1 > 0)
00106 {
00107 currentCell = grid[cellAdressX, cellAdressY - 1];
00108 if (currentCell != null)
00109 {
00110 check.AddRange(currentCell);
00111 }
00112 }
00113 if (cellAdressY + 1 < dimensionY)
00114 {
00115 currentCell = grid[cellAdressX, cellAdressY + 1];
00116 if (currentCell != null)
00117 {
00118 check.AddRange(currentCell);
00119 }
00120 }
00121 if ((cellAdressX + 1 < dimensionX) && (cellAdressY + 1 < dimensionY))
00122 {
00123 currentCell = grid[cellAdressX + 1, cellAdressY + 1];
00124 if (currentCell != null)
00125 {
00126 check.AddRange(currentCell);
00127 }
00128 }
00129 if ((cellAdressX - 1 > 0) && (cellAdressY - 1 > 0))
00130 {
00131 currentCell = grid[cellAdressX - 1, cellAdressY - 1];
00132 if (currentCell != null)
00133 {
00134 check.AddRange(currentCell);
00135 }
00136 }
00137 if ((cellAdressX + 1 < dimensionX) && (cellAdressY - 1 > 0))
00138 {
00139 currentCell = grid[cellAdressX + 1, cellAdressY - 1];
00140 if (currentCell != null)
00141 {
00142 check.AddRange(currentCell);
00143 }
00144 }
00145 if ((cellAdressX - 1 > 0) && (cellAdressY + 1 < dimensionY))
00146 {
00147 currentCell = grid[cellAdressX - 1, cellAdressY + 1];
00148 if (currentCell != null)
00149 {
00150 check.AddRange(currentCell);
00151 }
00152 }
00153
00154
00155
00156 #endregion
00157
00158 return check;
00159
00160 }
00161
00162 public void AddPoint(SamplePoint samplePoint)
00163 {
00164 Vector2 cellAdress = samplePoint.Position;
00165 int cellAdressX = (int)(((int)cellAdress.X) * cellSize_div);
00166 int cellAdressY = (int)(((int)cellAdress.Y) * cellSize_div);
00167 if (grid[cellAdressX, cellAdressY] == null) grid[cellAdressX, cellAdressY] = new List<SamplePoint>();
00168 grid[cellAdressX,cellAdressY].Add(samplePoint);
00169 }
00170
00171
00172 }
00173 }