Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve particles density from imported geom #361

Open
luchete80 opened this issue Nov 28, 2024 · 1 comment
Open

Improve particles density from imported geom #361

luchete80 opened this issue Nov 28, 2024 · 1 comment

Comments

@luchete80
Copy link
Owner

Add Radius Based on K-Nearest Neighbors (KNN):
Definition: Compute the average distance to the
𝑘
k-nearest neighbors.
Pros:
A balance between local and global influences.
Adjustable parameter (
𝑘
k) allows for tuning based on cloud density.
Cons:
More computationally expensive than nearest-neighbor or fixed-radius methods.

@luchete80
Copy link
Owner Author

luchete80 commented Nov 28, 2024


#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <limits>
#include <algorithm>

// Define a 3D point structure
struct Point3D {
    double x, y, z;
};

// Function to calculate Euclidean distance between two points
double euclideanDistance(const Point3D& p1, const Point3D& p2) {
    return std::sqrt(
        (p1.x - p2.x) * (p1.x - p2.x) +
        (p1.y - p2.y) * (p1.y - p2.y) +
        (p1.z - p2.z) * (p1.z - p2.z)
    );
}

// Function to calculate KNN-based radius
double calculateKNNRadius(const Point3D& center, const std::vector<Point3D>& points, int k) {
    std::priority_queue<double> maxHeap;

    for (const auto& point : points) {
        if (&center != &point) { // Avoid self-distance
            double dist = euclideanDistance(center, point);
            maxHeap.push(dist);

            if (maxHeap.size() > k) {
                maxHeap.pop(); // Keep only the k smallest distances
            }
        }
    }

    // Calculate the average of the k smallest distances
    double total = 0.0;
    while (!maxHeap.empty()) {
        total += maxHeap.top();
        maxHeap.pop();
    }
    return total / k;
}

// Function to calculate the volume of a sphere given its radius
double sphereVolume(double radius) {
    return (4.0 / 3.0) * M_PI * std::pow(radius, 3);
}

// Function to calculate the average sphere volume for all points in the cloud
double averageSphereVolume(const std::vector<Point3D>& points, int k) {
    double totalVolume = 0.0;

    for (const auto& point : points) {
        double radius = calculateKNNRadius(point, points, k); // Compute KNN radius
        totalVolume += sphereVolume(radius);                 // Compute sphere volume and add to total
    }

    return points.size() > 0 ? totalVolume / points.size() : 0.0;
}

int main() {
    // Example point cloud
    std::vector<Point3D> pointCloud = {
        {0.0, 0.0, 0.0},
        {1.0, 0.0, 0.0},
        {0.0, 1.0, 0.0},
        {0.0, 0.0, 1.0},
        {1.0, 1.0, 1.0}
    };

    int k = 2; // Number of nearest neighbors to consider
    double avgVolume = averageSphereVolume(pointCloud, k);

    std::cout << "Average sphere volume (KNN): " << avgVolume << std::endl;

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant