forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusters2.cs
More file actions
245 lines (209 loc) · 7.32 KB
/
Copy pathClusters2.cs
File metadata and controls
245 lines (209 loc) · 7.32 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
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System.Collections.Generic;
using UnityEngine;
namespace TouchScript.Clusters
{
/// <summary>
/// Represents a pool of points separated into two clusters.
/// </summary>
public class Clusters2 : Cluster
{
#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>
/// Minimum distance in pixels between clusters to treat them as two separate clusters.
/// Default: 0.
/// </summary>
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<TouchPoint> cluster1 = new List<TouchPoint>();
private List<TouchPoint> cluster2 = new List<TouchPoint>();
private float minPointDistance, minPointDistanceSqr;
private bool hasClusters = false;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="Clusters2"/> class.
/// </summary>
public Clusters2() : base()
{
MinPointsDistance = 0;
}
#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="TouchPoint.INVALID_POSITION"/> if cluster contains no points.</returns>
public Vector2 GetCenterPosition(int id)
{
if (!HasClusters) return TouchPoint.INVALID_POSITION;
Vector2 result;
switch (id)
{
case CLUSTER1:
result = Get2DCenterPosition(cluster1);
break;
case CLUSTER2:
result = Get2DCenterPosition(cluster2);
break;
default:
return TouchPoint.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="TouchPoint.INVALID_POSITION"/> if cluster contains no points.</returns>
public Vector2 GetPreviousCenterPosition(int id)
{
if (!HasClusters) return TouchPoint.INVALID_POSITION;
Vector2 result;
switch (id)
{
case CLUSTER1:
result = GetPrevious2DCenterPosition(cluster1);
break;
case CLUSTER2:
result = GetPrevious2DCenterPosition(cluster2);
break;
default:
return TouchPoint.INVALID_POSITION;
}
return result;
}
#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 = Get2DCenterPosition(cluster1);
var center2 = Get2DCenterPosition(cluster2);
TouchPoint obj1 = null;
TouchPoint 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 = GetPointsHash(cluster1);
hash2 = 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;
}
#endregion
}
}