Ejemplo n.º 1
0
        void minimum_area_enclosing_rectangle(
            const Polygon2d& PP, 
            vec2& S, vec2& T
        ) {

            // Note: this implementation has O(n2) complexity :-(
            // (where n is the number of vertices in the convex hull)
            // If this appears to be a bottleneck, use a smarter
            // implementation with better complexity.

            Polygon2d P ;
            convex_hull(PP, P) ;

            int N = P.size() ;
            
            // Add the first vertex at the end of P
            P.push_back(P[0]) ;

            double min_area = Numeric::big_double ;

            for(int i=1; i<=N; i++) {
                vec2 Si = P[i] - P[i-1] ;

                if( ( Si.length2() ) < 1e-20) {
                    continue ;
                }

                vec2 Ti(-Si.y, Si.x) ;
                normalize(Si) ;
                normalize(Ti) ;
                double s0 =  Numeric::big_double ;
                double s1 = -Numeric::big_double ;
                double t0 =  Numeric::big_double ;
                double t1 = -Numeric::big_double ; 
                for(int j=1; j<N; j++) {
                    vec2 D = P[j] - P[0] ;
                    double s = dot(Si, D) ;
                    s0 = gx_min(s0, s) ;
                    s1 = gx_max(s1, s) ;
                    double t = dot(Ti, D) ;
                    t0 = gx_min(t0, t) ;
                    t1 = gx_max(t1, t) ;
                }
                double area = (s1 - s0) * (t1 - t0) ;
                if(area < min_area) {
                    min_area = area ;
                    if((s1 - s0) < (t1 - t0)) {
                        S = Si ;
                        T = Ti ;
                    } else {
                        S = Ti ;
                        T = Si ;
                    }
                }
            }
        }
Ejemplo n.º 2
0
 size_t SparseMatrixPatternCRS::bandwidth() const {
     size_t result = 0 ;
     for(index_t i=0; i<m(); i++) {
         for(index_t jj=rowptr[i]; jj < rowptr[i+1]; jj++) {
             index_t j = colind[jj] ;
             result = gx_max(result, (size_t)gx_abs(signed_index_t(i) - signed_index_t(j))) ;
         }
     }
     return result ;
 }
Ejemplo n.º 3
0
    signed_index_t SparseMatrixPatternCRS::find_furthest_node(index_t x, IndexArray& dist) {

        std::deque<index_t> Q ;
        for(index_t i=0; i<m(); i++) {  dist[i] = NO_INDEX ; }
        index_t r = x ; index_t ex = 0 ; size_t dr = row_nnz(r) ;
            
        dist[x] = 0 ;
        Q.push_back(x) ;
        
        while(!Q.empty()) {
            index_t cur = Q.front() ;
            Q.pop_front() ;
            
            if(dist[cur] > ex) {
                r = cur ; dr = row_nnz(r) ; ex = dist[r] ;
            } else if(dist[cur] == ex) {
                size_t d = row_nnz(cur) ;
                if(d < dr) {   r = cur ; dr = d ;  }
            }
            
            for(index_t jj=rowptr[cur]; jj < rowptr[cur+1]; jj++) {
                index_t j = colind[jj] ;
                if(dist[j] == NO_INDEX) {
                    dist[j] = dist[cur] + 1 ;
                    Q.push_back(j) ;
                }
            }
        }
             
        size_t er = 0 ;
        for(index_t i=0; i<m(); i++) {  dist[i] = NO_INDEX ; }
        dist[r] = 0 ;
        Q.push_back(r) ;
        while(!Q.empty()) {
            index_t cur = Q.front() ;
            Q.pop_front() ;
            for(index_t jj=rowptr[cur]; jj < rowptr[cur+1]; jj++) {
                index_t j = colind[jj] ;
                if(dist[j] == NO_INDEX) {
                    dist[j] = dist[cur] + 1 ;
                    er = gx_max(er, dist[j]) ;
                    Q.push_back(j) ;
                }
            }
        }
        std::cerr << "   ex = " << ex << "   er = " << er << std::endl ;
        return (er > ex) ? signed_index_t(r) : -1 ;
    }
Ejemplo n.º 4
0
void begin_spheres() {

    if(sphere_vertex_program_id == 0 && sphere_fragment_program_id == 0) {
        create_sphere_programs() ;
    }

    glDisable(GL_NORMALIZE) ;
    glEnable(GL_VERTEX_PROGRAM_ARB) ;
    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB) ;
    glBindProgramARB(GL_VERTEX_PROGRAM_ARB, sphere_vertex_program_id) ;
    glEnable(GL_FRAGMENT_PROGRAM_ARB) ;
    glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, sphere_fragment_program_id) ;

    // For the moment, this is the only way we have to find the
    // dimension of the OpenGL window.
    float w=float(screen_w), h=float(screen_h), r=gx_max(w,h) ;

    glProgramLocalParameter4dARB(GL_VERTEX_PROGRAM_ARB, 0, 1.0/r, 0, 0, 0.2) ;
    glProgramLocalParameter4dARB(GL_FRAGMENT_PROGRAM_ARB, 0, -w/2.0, -h/2.0, 0, 0) ;
    glProgramLocalParameter4dARB(GL_FRAGMENT_PROGRAM_ARB, 1, -2.0/r, -2.0/r, 0, 0) ;

    glBegin(GL_POINTS) ;
}