// TODO: using CGAL::pca_estimate_normals()
void NormalEstimator::apply(Object* object, unsigned int K)
{
    if (object == NULL) {
        std::cout << title() << "no data exists" << std::endl;
        return;
    }

    std::cout << title() << "estimating point cloud normal..." << std::endl;
    Stopwatch clock;

    PrimitiveFitting fit;
    std::vector<Vertex*> &vertices = object->vertices();

    unsigned int num = vertices.size() / 30;  // for each part
    unsigned int count = 0;

    for (unsigned int i=0; i<vertices.size(); ++i) {
        Vertex* v = vertices[i];

        // TODO: process larger number once a time
        std::vector<Vertex*> neighbors;
        object->get_K_nearest_points(v->point(), K, neighbors);

        neighbors.push_back(v);  // do NOT forget itself
        fit.set_points(neighbors);

        Plane3d plane;
        fit.fit_plane(plane);
        Vector3d n = global::normalOf(plane);
        v->set_normal(n);
    }

    std::cout << title() << "normal estimation done. Time: " << clock.elapsed_user_time() << " second" << std::endl;

    object->set_normals_exist(true);
}