Tanoda
LeapGrid.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using Leap.Unity.Infix;
10using UnityEngine;
11
12namespace Leap.Unity.Geometry {
13
14 using UnityRect = UnityEngine.Rect;
15
16 public enum VerticalOrigin { Top, Bottom }
17
18 public struct LeapGrid {
19
21 public int numRows;
22 public int numCols;
25 public bool rowMajor;
26
27 public LeapGrid(UnityRect rect, int numRows = 1, int numCols = 1,
29 bool rowMajor = false) :
31 rowMajor) { }
32
33 public LeapGrid(LocalRect rect, int numRows = 1, int numCols = 1,
35 bool rowMajor = false)
36 {
37 var useMargins = cellMargins.UnwrapOr(Margins.All(0f));
38 var useVertOrigin = verticalOrigin.UnwrapOr(VerticalOrigin.Bottom);
39
40 this.rect = rect;
41 this.numRows = numRows;
42 this.numCols = numCols;
43 this.cellMargins = useMargins;
44 this.verticalOrigin = useVertOrigin;
45 this.rowMajor = rowMajor;
46 }
47
48 public int numCells { get { return numRows * numCols; } }
49
50 public Cell this[int idx] {
51 get {
52 var cellWidth = rect.radii.x * 2f / numCols;
53 var cellHeight = rect.radii.y * 2f / numRows;
54 int row = idx / numCols;
55 int col = idx % numCols;
56 if (rowMajor) {
57 row = idx % numRows;
58 col = idx / numRows;
59 idx = row * numCols + col;
60 }
61 var origin = verticalOrigin == VerticalOrigin.Bottom ?
63 var centerFromOrigin = new Vector3(
64 col * cellWidth + (cellWidth / 2f),
65 row * cellHeight + (cellHeight / 2f),
66 0f
67 ).CompMul(verticalOrigin == VerticalOrigin.Top ?
68 new Vector3(1f, -1f, 1f) : Vector3.one);
69 return new Cell() {
70 row = row,
71 col = col,
72 index = idx,
73 margins = cellMargins,
74 outerRect = new LocalRect() {
75 center = origin + centerFromOrigin,
76 radii = new Vector2(cellWidth / 2f, cellHeight / 2f)
77 }
78 };
79 }
80 }
81
82 public Cell GetMerged(int idx0, int idx1) {
83 if (idx0 == idx1) { return this[idx0]; }
84
85 int row0 = getRow(idx0), row1 = getRow(idx1);
86 if (row0 > row1) { Utils.Swap(ref row0, ref row1); }
87 var rowStart = getRow(idx0);
88 var numMergedRows = 1 + (row1 - row0);
89
90 int col0 = getCol(idx0), col1 = getCol(idx1);
91 if (col0 > col1) { Utils.Swap(ref col0, ref col1); }
92 var colStart = getCol(idx0);
93 var numMergedCols = 1 + (col1 - col0);
94
95 var cellWidth = rect.radii.x * 2f / this.numCols;
96 var cellHeight = rect.radii.y * 2f / this.numRows;
97 var mergedCellWidth = cellWidth * numMergedCols;
98 var mergedCellHeight = cellHeight * numMergedRows;
99
100 var origin = verticalOrigin == VerticalOrigin.Bottom ?
102 var centerFromOrigin = new Vector3(
103 colStart * cellWidth + (mergedCellWidth / 2f),
104 rowStart * cellHeight + (mergedCellHeight / 2f),
105 0f
106 ).CompMul(verticalOrigin == VerticalOrigin.Top ?
107 new Vector3(1f, -1f, 1f) : Vector3.one);
108
109 return new Cell() {
110 row = rowStart,
111 col = colStart,
112 index = (colStart + this.numCols * rowStart),
113 margins = cellMargins,
114 outerRect = new LocalRect() {
115 center = origin + centerFromOrigin,
116 radii = new Vector2(mergedCellWidth / 2f, mergedCellHeight / 2f)
117 }
118 };
119 }
120
121 private int getRow(int idx) { return idx / numCols; }
122 private int getCol(int idx) { return idx % numCols; }
123 private int getIndex(int row, int col) { return row * numCols + col; }
124
125 public struct Cell {
126 public int row;
127 public int col;
128 public int index;
132 get { return outerRect.PadInner(margins); }
133 }
135 public LocalRect rect { get { return innerRect; } }
136 public UnityRect unityRect { get { return rect.ToUnityRect(); }}
137 }
138
140 return new CellEnumerator(this);
141 }
142
146 public CellEnumerator EnumerateCells(int subGridBegin, int subGridEnd) {
147 return new CellEnumerator(this, subGridBegin, subGridEnd);
148 }
149
150 public struct CellEnumerator {
151
153 public int idx;
154 public float cellWidth;
155 public float cellHeight;
156
160
162 this.grid = grid;
163 this.idx = -1;
164 this.cellWidth = grid.rect.radii.x * 2f / grid.numCols;
165 this.cellHeight = grid.rect.radii.y * 2f / grid.numRows;
166
167 _useSubGrid = false;
168 _col0 = 0; _col1 = 0;
170 }
171 public CellEnumerator GetEnumerator() { return this; }
172
176 public CellEnumerator(LeapGrid grid, int subGridBegin, int subGridEnd) :
177 this(grid)
178 {
179 int idx0 = subGridBegin, idx1 = subGridEnd;
180 int row0 = grid.getRow(idx0), row1 = grid.getRow(idx1);
181 if (row0 > row1) { Utils.Swap(ref row0, ref row1); }
182 int col0 = grid.getCol(idx0), col1 = grid.getCol(idx1);
183 if (col0 > col1) { Utils.Swap(ref col0, ref col1); }
184 _col0 = col0; _col1 = col1;
185
186 _lastIndex = Mathf.Max(subGridBegin, subGridEnd);
187 idx = grid.getIndex(row0, col0) - 1;
188 _useSubGrid = true;
189 }
190
191 public bool MoveNext() {
192 idx += 1;
193
194 if (_useSubGrid) {
195 //var row = grid.getRow(idx);
196 var col = grid.getCol(idx);
197 if (col < _col0) { idx += _col0 - col; }
198 if (col > _col1) { idx += grid.numCols - col; }
199 if (idx > _lastIndex) { return false; }
200 }
201
202 if (idx >= grid.numCells) { return false; }
203 else { return true; }
204 }
205 public Cell Current {
206 get { return grid[idx]; }
207 }
208
209 }
210
211 }
212
213}
UnityEngine.Rect UnityRect
Definition: LeapGrid.cs:14
CellEnumerator(LeapGrid grid, int subGridBegin, int subGridEnd)
Initializes a CellEnumerator that enumerates cells within the rectangular subgrid defined by cell ind...
Definition: LeapGrid.cs:176
LocalRect rect
Shorthand for cell.innerRect.
Definition: LeapGrid.cs:135
CellEnumerator GetEnumerator()
Definition: LeapGrid.cs:139
Cell GetMerged(int idx0, int idx1)
Definition: LeapGrid.cs:82
LeapGrid(UnityRect rect, int numRows=1, int numCols=1, Margins? cellMargins=null, VerticalOrigin? verticalOrigin=null, bool rowMajor=false)
Definition: LeapGrid.cs:27
CellEnumerator EnumerateCells(int subGridBegin, int subGridEnd)
Returns a CellEnumerator that enumerates cells within the rectangular subgrid defined by cell indices...
Definition: LeapGrid.cs:146
VerticalOrigin verticalOrigin
Definition: LeapGrid.cs:24
LeapGrid(LocalRect rect, int numRows=1, int numCols=1, Margins? cellMargins=null, VerticalOrigin? verticalOrigin=null, bool rowMajor=false)
Definition: LeapGrid.cs:33
static Margins All(float margin)
Definition: Margins.cs:27