/// <summary> /// Creates clusters for all points which are within an epsilon /// distance from each other. This algorithm uses a hash-grid and /// only works fast if the supplied epsilon is small enough that /// not too many points fall within each cluster. Thus it is ideal /// for merging vertices in meshes and point sets, that are different /// due to numerical inaccuracies. /// </summary> public PointEpsilonClustering(V3d[] pa, double epsilon, IRandomUniform rnd = null) { rnd = rnd ?? new RandomSystem(); var count = pa.Length; Alloc(count); var ca = m_indexArray; var dict = new IntDict <int>(count, stackDuplicateKeys: true); int rndBits = 0; int bit = 0; var ha = new int[8]; var eps2 = epsilon * epsilon; for (int i = 0; i < count; i++) { var p = pa[i]; p.HashCodes8(epsilon, ha); int ci = ca[i]; if (ca[ci] != ci) { do { ci = ca[ci]; } while (ca[ci] != ci); ca[i] = ci; } for (int hi = 0; hi < 8; hi++) { foreach (var j in dict.ValuesWithKey(ha[hi])) { int cj = ca[j]; if (ca[cj] != cj) { do { cj = ca[cj]; } while (ca[cj] != cj); ca[j] = cj; } if (ci == cj || V3d.DistanceSquared(p, pa[j]) >= eps2) { continue; } bit >>= 1; if (bit == 0) { rndBits = rnd.UniformInt(); bit = 1 << 30; } if ((rndBits & bit) != 0) { ca[ci] = cj; ca[i] = cj; ci = cj; } else { ca[cj] = ci; ca[j] = ci; } } } dict[ha[0]] = i; } Init(); }
/// <summary> /// Merge clusters of exactly equal points. The supplied epsilon is used to define a /// grid as acceleration data structure and the algorithm is only fast if not too many /// points fall within the supplied epsilon. /// </summary> public PointEqualClustering(V3d[] pa, double eps, IRandomUniform rnd = null) { rnd = rnd ?? new RandomSystem(); var count = pa.Length; Alloc(count); var ca = m_indexArray; var dict = new IntDict <int>(count, stackDuplicateKeys: true); int rndBits = 0; int bit = 0; for (int i = 0; i < count; i++) { var p = pa[i]; var hc = p.HashCode1(eps); int ci = ca[i]; if (ca[ci] != ci) { do { ci = ca[ci]; } while (ca[ci] != ci); ca[i] = ci; } foreach (var j in dict.ValuesWithKey(hc)) { int cj = ca[j]; if (ca[cj] != cj) { do { cj = ca[cj]; } while (ca[cj] != cj); ca[j] = cj; } if (ci == cj || p != pa[j]) { continue; } bit >>= 1; if (bit == 0) { rndBits = rnd.UniformInt(); bit = 1 << 30; } if ((rndBits & bit) != 0) { ca[ci] = cj; ca[i] = cj; ci = cj; } else { ca[cj] = ci; ca[j] = ci; } } dict[hc] = i; } Init(); }
/// <summary> /// Creates clusters of planes within a certain epsilon from each other /// (euclidean distance between normal vectors and between offsets). /// This algorithm uses a 4d hash-grid and only works fast if the supplied /// epsilons are small enough that not too many planes fall within each cluster. /// Thus it is ideal for merging planes with small variations in orientation and /// offset due to numerical inaccuracies. /// </summary> public PlaneEpsilonClustering( int count, TArray pa, Func <TArray, int, V3d> getNormal, Func <TArray, int, double> getDist, double epsNormal, double epsDist, double deltaEpsFactor = 0.25, IRandomUniform rnd = null) { rnd = rnd ?? new RandomSystem(); Alloc(count); var ca = m_indexArray; var dict = new IntDict <int>(count, stackDuplicateKeys: true); int rndBits = 0; int bit = 0; var ha = new int[16]; var ne2 = epsNormal * epsNormal; var de2 = epsDist * epsDist; var deps = (ne2 + de2) * deltaEpsFactor * deltaEpsFactor; for (int i = 0; i < count; i++) { var ni = getNormal(pa, i); var di = getDist(pa, i); ni.HashCodes16(di, epsNormal, epsDist, ha); int ci = ca[i]; if (ca[ci] != ci) { do { ci = ca[ci]; } while (ca[ci] != ci); ca[i] = ci; } double dmin = double.MaxValue; for (int hi = 0; hi < 16; hi++) { foreach (var j in dict.ValuesWithKey(ha[hi])) { int cj = ca[j]; if (ca[cj] != cj) { do { cj = ca[cj]; } while (ca[cj] != cj); ca[j] = cj; } if (ci == cj) { continue; } var dd = Fun.Square(di - getDist(pa, j)); if (dd >= de2) { continue; } var dn = V3d.DistanceSquared(ni, getNormal(pa, j)); if (dn >= ne2) { continue; } var d = dn + dd; if (d < dmin) { dmin = d; } bit >>= 1; if (bit == 0) { rnd.UniformInt(); bit = 1 << 30; } if ((rndBits & bit) != 0) { ca[ci] = cj; ca[i] = cj; ci = cj; } else { ca[cj] = ci; ca[j] = ci; } } } if (dmin > deps) { dict[ha[0]] = i; // only sparsely populate hashtable for performance reasons } } Init(); }