forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusters.cs
More file actions
299 lines (252 loc) · 9.41 KB
/
Copy pathClusters.cs
File metadata and controls
299 lines (252 loc) · 9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System.Collections.Generic;
using TouchScript.Utils;
using UnityEngine;
namespace TouchScript.Clusters
{
/// <summary>
/// Represents a pool of points separated into two clusters.
/// </summary>
public sealed class Clusters
{
#region Constants
/// <summary>The first cluster.</summary>
public const int CLUSTER1 = 0;
/// <summary>The second cluster.</summary>
public const int CLUSTER2 = 1;
#endregion
#region Public properties
/// <summary>Gets the total number of points in clusters represented by this object.</summary>
public int PointsCount
{
get { return points.Count; }
}
/// <summary>Gets or sets minimum distance in pixels between clusters to treat them as two separate clusters.</summary>
/// <value>Minimum distance in pixels which must be between cluster centers to consider them as separate clusters.</value>
/// <remarks>This value is used to set the limit of how close cluster can be. Sometimes very close points shouldn't be treated as being in separate clusters.</remarks>
public float MinPointsDistance
{
get { return minPointDistance; }
set
{
minPointDistance = value;
minPointDistanceSqr = value * value;
}
}
/// <summary>Indicates that this cluster instance has two valid clusters.</summary>
/// <value><c>true</c> if this instance has clusters; otherwise, <c>false</c>.</value>
public bool HasClusters
{
get
{
if (dirty) distributePoints();
return hasClusters;
}
}
#endregion
#region Private variables
private List<ITouch> points = new List<ITouch>();
private bool dirty;
private List<ITouch> cluster1 = new List<ITouch>();
private List<ITouch> cluster2 = new List<ITouch>();
private float minPointDistance, minPointDistanceSqr;
private bool hasClusters = false;
#endregion
/// <summary>Initializes a new instance of the <see cref="Clusters"/> class.</summary>
public Clusters()
{
MinPointsDistance = 0;
markDirty();
}
#region Public methods
/// <summary>Calculates the center position of one of the clusters.</summary>
/// <param name="id">Cluster id. Either <see cref="CLUSTER1"/> or <see cref="CLUSTER2"/>.</param>
/// <returns>Cluster's centroid position or <see cref="TouchManager.INVALID_POSITION"/> if cluster contains no points.</returns>
public Vector2 GetCenterPosition(int id)
{
if (!HasClusters) return TouchManager.INVALID_POSITION;
Vector2 result;
switch (id)
{
case CLUSTER1:
result = ClusterUtils.Get2DCenterPosition(cluster1);
break;
case CLUSTER2:
result = ClusterUtils.Get2DCenterPosition(cluster2);
break;
default:
return TouchManager.INVALID_POSITION;
}
return result;
}
/// <summary>Calculates previous center position of one of the clusters.</summary>
/// <param name="id">Cluster id. Either <see cref="CLUSTER1"/> or <see cref="CLUSTER2"/>.</param>
/// <returns>Cluster's centroid previous position or <see cref="TouchManager.INVALID_POSITION"/> if cluster contains no points.</returns>
public Vector2 GetPreviousCenterPosition(int id)
{
if (!HasClusters) return TouchManager.INVALID_POSITION;
Vector2 result;
switch (id)
{
case CLUSTER1:
result = ClusterUtils.GetPrevious2DCenterPosition(cluster1);
break;
case CLUSTER2:
result = ClusterUtils.GetPrevious2DCenterPosition(cluster2);
break;
default:
return TouchManager.INVALID_POSITION;
}
return result;
}
/// <summary>Adds a point to cluster.</summary>
/// <param name="point">A point.</param>
public void AddPoint(ITouch point)
{
if (points.Contains(point)) return;
points.Add(point);
markDirty();
}
/// <summary>Adds a list of points to cluster.</summary>
/// <param name="points">List of points.</param>
public void AddPoints(IList<ITouch> points)
{
foreach (var point in points) AddPoint(point);
}
/// <summary>Removes a point from cluster.</summary>
/// <param name="point">A point.</param>
public void RemovePoint(ITouch point)
{
if (!points.Contains(point)) return;
points.Remove(point);
markDirty();
}
/// <summary>Removes a list of points from cluster.</summary>
/// <param name="points">List of points.</param>
public void RemovePoints(IList<ITouch> points)
{
foreach (var point in points) RemovePoint(point);
}
/// <summary>Removes all points from cluster.</summary>
public void RemoveAllPoints()
{
points.Clear();
markDirty();
}
/// <summary>Invalidates cluster state. Call this method to recalculate cluster properties.</summary>
public void Invalidate()
{
markDirty();
}
#endregion
#region Private functions
private void distributePoints()
{
cluster1.Clear();
cluster2.Clear();
hasClusters = checkClusters();
if (!hasClusters) return;
cluster1.Add(points[0]);
cluster2.Add(points[1]);
var total = points.Count;
if (total == 2) return;
var oldHash1 = "";
var oldHash2 = "";
var hash1 = "#";
var hash2 = "#";
while (oldHash1 != hash1 || oldHash2 != hash2)
{
var center1 = ClusterUtils.Get2DCenterPosition(cluster1);
var center2 = ClusterUtils.Get2DCenterPosition(cluster2);
ITouch obj1 = null;
ITouch obj2 = null;
// Take most distant points from cluster1 and cluster2
var maxDist1 = -float.MaxValue;
var maxDist2 = -float.MaxValue;
for (var i = 0; i < total; i++)
{
var obj = points[i];
var dist = (center1 - obj.Position).sqrMagnitude;
if (dist > maxDist2)
{
maxDist2 = dist;
obj2 = obj;
}
dist = (center2 - obj.Position).sqrMagnitude;
if (dist > maxDist1)
{
maxDist1 = dist;
obj1 = obj;
}
}
// If it is the same point it means that this point is too far away from both clusters and has to be in a separate cluster
if (obj1 == obj2)
{
center1 = (center1 + center2) * .5f;
center2 = obj2.Position;
}
else
{
center1 = obj1.Position;
center2 = obj2.Position;
}
cluster1.Clear();
cluster2.Clear();
for (var i = 0; i < total; i++)
{
var obj = points[i];
if ((center1 - obj.Position).sqrMagnitude < (center2 - obj.Position).sqrMagnitude)
{
cluster1.Add(obj);
}
else
{
cluster2.Add(obj);
}
}
oldHash1 = hash1;
oldHash2 = hash2;
hash1 = ClusterUtils.GetPointsHash(cluster1);
hash2 = ClusterUtils.GetPointsHash(cluster2);
}
markClean();
}
private bool checkClusters()
{
var length = points.Count - 1;
if (length < 1) return false;
if (length == 1)
{
var p1 = points[0].Position;
var p2 = points[1].Position;
var dx = p1.x - p2.x;
var dy = p1.y - p2.y;
if (dx * dx + dy * dy >= minPointDistanceSqr) return true;
return false;
}
for (var i = 0; i < length; i++)
{
for (var j = i + 1; j <= length; j++)
{
var p1 = points[i].Position;
var p2 = points[j].Position;
var dx = p1.x - p2.x;
var dy = p1.y - p2.y;
if (dx * dx + dy * dy >= minPointDistanceSqr) return true;
}
}
return false;
}
private void markDirty()
{
dirty = true;
}
private void markClean()
{
dirty = false;
}
#endregion
}
}