void MeshDenoisingBase::updateVertexPosition(TriMesh &mesh, std::vector<TriMesh::Normal> &filtered_normals, int iteration_number, bool fixed_boundary)
{
    std::vector<TriMesh::Point> new_points(mesh.n_vertices());

    std::vector<TriMesh::Point> centroid;

    for(int iter = 0; iter < iteration_number; iter++)
    {
        getFaceCentroid(mesh, centroid);
        for(TriMesh::VertexIter v_it = mesh.vertices_begin(); v_it != mesh.vertices_end(); v_it++)
        {
            TriMesh::Point p = mesh.point(*v_it);
            if(fixed_boundary && mesh.is_boundary(*v_it))
            {
                new_points.at(v_it->idx()) = p;
            }
            else
            {
                double face_num = 0.0;
                TriMesh::Point temp_point(0.0, 0.0, 0.0);
                for(TriMesh::VertexFaceIter vf_it = mesh.vf_iter(*v_it); vf_it.is_valid(); vf_it++)
                {
                    TriMesh::Normal temp_normal = filtered_normals[vf_it->idx()];
                    TriMesh::Point temp_centroid = centroid[vf_it->idx()];
                    temp_point += temp_normal * (temp_normal | (temp_centroid - p));
                    face_num++;
                }
                p += temp_point / face_num;

                new_points.at(v_it->idx()) = p;
            }
        }

        for(TriMesh::VertexIter v_it = mesh.vertices_begin(); v_it != mesh.vertices_end(); v_it++)
            mesh.set_point(*v_it, new_points[v_it->idx()]);
    }
}
Beispiel #2
0
void SilhouetteExtractor::computeVisibleFrontFacingStatus()
{
    int terrain_width = terrain_->width();
    int terrain_height = terrain_->height();

    delete front_facing_;
    front_facing_ = new FacingMode [(terrain_width-1)*(terrain_height-1)];

    bool use_intersections = true ;

    if (!use_intersections)
    {
        setupPixelBuffer();
        pixelbuffer_->makeCurrent();
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHTING);
        GLfloat lightpos[] = {.5, 1., 1., 0.};
        glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        drawTerrain();
        updateMatrices();

        saveBuffer("test_dpth.png");
        pixelbuffer_->makeCurrent();
    }

    double begin = omp_get_wtime();

    int i = 0;
#pragma omp parallel for private(i)
    for (int j = 0; j < terrain_->height()-1; ++j)
        for (i = 0; i < terrain_->width()-1; ++i)
        {
            front_facing_[j*(terrain_width-1)+i] = kInvisible;

            Eigen::Vector3f center = getFaceCentroid(i, j);
            Eigen::Vector3f projector = center - camera_info_.position;
            //projector = camera_info_.direction;

            float theta = acos(camera_info_.direction.normalized().dot(projector.normalized()));
            if (theta > camera_info_.fov_in_rads/2)
                continue;

            front_facing_[j*(terrain_width-1)+i] = kBackFacing;

            if (terrain_->getGridNormal(i, j).dot(projector) <= -FLT_EPSILON)
            {
                if (use_intersections)
                {
                    if (checkVisibility(center))
                        front_facing_[j*(terrain_width-1)+i] = kFrontFacing;
                } else
                {
                    Eigen::Vector3d window_coords;
                    gluProject(center[0], center[1], center[2],
                               modelview_matrix_, projection_matrix_, viewport_,
                               &window_coords[0], &window_coords[1], &window_coords[2]);

                    if (window_coords[0] < 0 || window_coords[1] < 0 || window_coords[0] >= width() || window_coords[1] >= height())
                        continue;
                    float depth = 0.0;
                    glReadPixels(window_coords[0], window_coords[1], 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);

                    if (std::abs(depth-window_coords[2]) < 1e-3)
                        front_facing_[j*(terrain_width-1)+i] = kFrontFacing;
                }
            }
        }

    double end = omp_get_wtime();
    double elapsed_secs = double(end - begin);
    fprintf(stdout, "Elapsed time for checking front/back facing: %.2f secs\n", elapsed_secs);
    fprintf(stdout, "Num of threads: %d threads\n", omp_get_thread_num());
    fflush(stdout);

    if (pixelbuffer_)
    {
        pixelbuffer_->doneCurrent();
        cleanupPixelBuffer();
    }
}