Also habe ich eine Liste von 3D-Punkten und ich möchte Punkte zusammenfassen, die innerhalb 1 Einheit oder weniger voneinander sind. Also hier ist ein Beispiel von dem, worüber ich spreche (ich verwende 2D Punkte für das Beispiel),So gruppieren Sie 3D-Punkte, die alle in einem Abstand voneinander liegen
Sagen wir haben Punkt 1: (0,0) und Punkt 2: (0,1) welche 1 sind Abstand voneinander. Das Programm speichert beide in einem Vektor. Jetzt ist hier ein dritter Punkt (0,2). Dieser Punkt ist 1 Abstand von Punkt 2, aber nicht von Punkt 1, aber das Programm speichert ihn immer noch, da er sich in einem Abstand von mindestens 1 Punkt im Vektor befindet.
Deshalb mag ich 3D-Punkte in „Blobs“ und alles sammeln, die 1 Einheit oder weniger von diesem „Blob“ wird auf den „Blob“
Ich habe versucht, so diese Vergangenheit viele verschiedene Funktionen hinzugefügt werden ein paar Tage, versuchte Rekursion, aber stürzte immer und verschachtelte Tonnen Forloops, aber ich kann das nicht machen.
Hier ist mein Code
void combinePoints(vector<Point>& allPoints, vector< vector<Cavity> >& allPointBlobs, vector<Point>& tempBlob)
{
float check;
if(allPoints.size() != 0) //if statement to stop recursion once all the points from "allPoints" vector is checked and removed
{
for(int i = 0; i < allPoints.size(); i++) //3d distance formula checking first point with all other points
{
check = sqrt((allPoints[0].getX() - allPoints[i].getX()) * (allPoints[0].getX() - allPoints[i].getX()) +
(allPoints[0].getY() - allPoints[i].getY()) * (allPoints[0].getY() - allPoints[i].getY()) +
(allPoints[0].getZ() - allPoints[i].getZ()) * (allPoints[0].getZ() - allPoints[i].getZ()));
if ((check <= 1.000) && (check != 0)) //once a point is found that is 1 distance or less, it is added to tempBlob vector and removed from allPoints
{
tempBlob.push_back(allPoints[0]);
tempBlob.push_back(allPoints[i]);
allPoints.erase(allPoints.begin() + i);
allPoints.erase(connollyPoints.begin());
break;
}
}
if(check > 1.000) //However, if no points are nearby, then tempBlob is finished finding all nearby points and is added to a vector and cleared so it can start finding another blob.
{
allPointBlobs.push_back(tempBlob);
tempBlob.clear();
cout << "Blob Done" << endl;
combinePoints(allPoints, allPointBlobs, tempBlob);
}
else
{
combinePoints2(allPoints, allPointBlobs, tempBlob);
}
}
}
void combinePoints2(vector<Point>& allPoints, vector< vector<Point> >& allPointBlobs, vector<Point>& tempBlob) //combinePoints2 is almost the same as the first one, except I changed the first part where it doesnt have to initiate a vector with first two points. This function will then check all points in the temporary blob against all other points and find ones that are 1 distance or less
{
cout << tempBlob.size() << endl; //I use this just to check if function is working
float check = 0;
if(allPoints.size() != 0)
{
for(int j = 0; j < tempBlob.size(); j++)
{
for(int k = 0; k < allPoints.size(); k++)
{
check = sqrt((tempBlob[j].getX() - allPoints[k].getX()) * (tempBlob[j].getX() - allPoints[k].getX()) +
(tempBlob[j].getY() - allPoints[k].getY()) * (tempBlob[j].getY() - allPoints[k].getY()) +
(tempBlob[j].getZ() - allPoints[k].getZ()) * (tempBlob[j].getZ() - allPoints[k].getZ()));
if ((check <= 1.000) && (check != 0))
{
tempBlob.push_back(allPoints[k]);
allPoints.erase(allPoints.begin() + k);
break;
}
}
if ((check <= 1.000) && (check != 0))
{
break;
}
}
if(check > 1.000)
{
allPointBlobs.push_back(tempBlob);
tempBlob.clear();
cout << "Blob Done" << endl;
combinePoints(allPoints, allPointBlobs, tempBlob);
}
else
{
combinePoints2(allPoints, allPointBlobs, tempBlob);
}
}
}
Ich benutze alle Pausen, weil, wenn ein Punkt von allPoints gelöscht, es vermasselt die forloops (I in den Kommentaren neben dem Code leichter verständlich zu machen hinzugefügt) seit Ich verwende .size() für die Anzahl der Male, die es ausgeführt wird. Dies macht das Programm wirklich langsam, da es die Funktion erneut initialisieren muss, wenn es einen Punkt findet. Ich hoffe, dass jemand mir helfen kann, einen einfacheren Weg zu finden.
Ich habe viele andere Funktionen gemacht, aber sie stürzen ab, das ist das einzige, das so weit funktioniert (oder zumindest hoffe ich, dass es funktioniert lol, es stürzt einfach nicht ab, was ein gutes Zeichen ist).
Bitte versuchen Sie, einen Codebeispiel hinzuzufügen, was Sie erreichen möchten. – oronbz
Ich habe meinen Code hinzugefügt –