Exemple #1
0
static void relaxPoints(FillContext& context, Size iterations) {
    auto chunkSize = context.chunkSize;
    auto cx = context.chunkX;
    auto cy = context.chunkY;
    auto& points = context.points;

    for(int i = 0; i < iterations; ++i) {
        Diagram diagram;
        construct_voronoi(points.begin(), points.end(), &diagram);

        for(const DiagramCell& it : diagram.cells()) {
            auto edge = it.incident_edge();
            CoordinateType x = 0;
            CoordinateType y = 0;
            int count = 0;
            do {
                edge = edge->next();
                auto edgePoints = getEdgePoints(*edge, points);
                x += edgePoints.first.x() - cx * chunkSize;
                y += edgePoints.first.y() - cy * chunkSize;
                ++count;
            } while (edge != it.incident_edge());
            points[it.source_index()].x(x / count + cx * chunkSize);
            points[it.source_index()].y(y / count + cy * chunkSize);
        }
    }
}
Exemple #2
0
void
MedialAxis::build(Polylines* polylines)
{
    /*
    // build bounding box (we use it for clipping infinite segments)
    // --> we have no infinite segments
    this->bb = BoundingBox(this->lines);
    */
    
    construct_voronoi(this->lines.begin(), this->lines.end(), &this->vd);
    
    /*
    // DEBUG: dump all Voronoi edges
    {
        for (VD::const_edge_iterator edge = this->vd.edges().begin(); edge != this->vd.edges().end(); ++edge) {
            if (edge->is_infinite()) continue;
            
            Polyline polyline;
            polyline.points.push_back(Point( edge->vertex0()->x(), edge->vertex0()->y() ));
            polyline.points.push_back(Point( edge->vertex1()->x(), edge->vertex1()->y() ));
            polylines->push_back(polyline);
        }
        return;
    }
    */
    
    // collect valid edges (i.e. prune those not belonging to MAT)
    // note: this keeps twins, so it contains twice the number of the valid edges
    this->edges.clear();
    for (VD::const_edge_iterator edge = this->vd.edges().begin(); edge != this->vd.edges().end(); ++edge) {
        // if we only process segments representing closed loops, none if the
        // infinite edges (if any) would be part of our MAT anyway
        if (edge->is_secondary() || edge->is_infinite()) continue;
        this->edges.insert(&*edge);
    }
    
    // count valid segments for each vertex
    std::map< const VD::vertex_type*,std::set<const VD::edge_type*> > vertex_edges;
    std::set<const VD::vertex_type*> entry_nodes;
    for (VD::const_vertex_iterator vertex = this->vd.vertices().begin(); vertex != this->vd.vertices().end(); ++vertex) {
        // get a reference to the list of valid edges originating from this vertex
        std::set<const VD::edge_type*>& edges = vertex_edges[&*vertex];
        
        // get one random edge originating from this vertex
        const VD::edge_type* edge = vertex->incident_edge();
        do {
            if (this->edges.count(edge) > 0)    // only count valid edges
                edges.insert(edge);
            edge = edge->rot_next();            // next edge originating from this vertex
        } while (edge != vertex->incident_edge());
        
        // if there's only one edge starting at this vertex then it's a leaf
        size_t edge_count = edges.size();
        if (edge_count == 1) {
            entry_nodes.insert(&*vertex);
        }
    }
    
    // prune recursively
    while (!entry_nodes.empty()) {
        // get a random entry node
        const VD::vertex_type* v = *entry_nodes.begin();
    
        // get edge starting from v
        assert(!vertex_edges[v].empty());
        const VD::edge_type* edge = *vertex_edges[v].begin();
        
        if (!this->is_valid_edge(*edge)) {
            // if edge is not valid, erase it from edge list
            (void)this->edges.erase(edge);
            (void)this->edges.erase(edge->twin());
            
            // decrement edge counters for the affected nodes
            const VD::vertex_type* v1 = edge->vertex1();
            (void)vertex_edges[v].erase(edge);
            (void)vertex_edges[v1].erase(edge->twin());
            
            // also, check whether the end vertex is a new leaf
            if (vertex_edges[v1].size() == 1) {
                entry_nodes.insert(v1);
            } else if (vertex_edges[v1].empty()) {
                entry_nodes.erase(v1);
            }
        }
        
        // remove node from the set to prevent it from being visited again
        entry_nodes.erase(v);
    }
    
    // iterate through the valid edges to build polylines
    while (!this->edges.empty()) {
        const VD::edge_type& edge = **this->edges.begin();
        
        // start a polyline
        Polyline polyline;
        polyline.points.push_back(Point( edge.vertex0()->x(), edge.vertex0()->y() ));
        polyline.points.push_back(Point( edge.vertex1()->x(), edge.vertex1()->y() ));
        
        // remove this edge and its twin from the available edges
        (void)this->edges.erase(&edge);
        (void)this->edges.erase(edge.twin());
        
        // get next points
        this->process_edge_neighbors(edge, &polyline.points);
        
        // get previous points
        Points pp;
        this->process_edge_neighbors(*edge.twin(), &pp);
        polyline.points.insert(polyline.points.begin(), pp.rbegin(), pp.rend());
        
        // append polyline to result if it's not too small
        if (polyline.length() > this->max_width)
            polylines->push_back(polyline);
    }
}
Exemple #3
0
bool VoronoiMap::update_diagram(const World* world)
{
    std::vector<Point> points;
    for (int x = 0; x < world->width(); ++x)
    {
        for (int y = 0; y < world->height(); ++y)
        {
            Node* node = world->at(x,y);
            if (node->is_wall())
            {
                bool success = true;
                for (auto nei : world->neighbours(node))
                {
                    if (!nei->is_wall())
                    {
                        success = false;
                        break;
                    }
                }

                if (success)
                {
                    points.push_back(Point(x,y));
                }
            }
        }
    }

    // Keep this empty for now
    std::vector<Segment> segments;

    construct_voronoi(points.begin(), points.end(), segments.begin(), segments.end(), m_map);

    // Filter edges and vertexes
    m_filtered_edges = m_map->edges();

    auto edge_filter = [world](const voronoi_diagram<double>::edge_type& edge)
    {
        bool remove = false;

        if(edge.is_finite())
        {
            std::pair<int,int> p0 = std::make_pair(std::round(edge.vertex0()->x()), std::round(edge.vertex0()->y()));
            std::pair<int,int> p1 = std::make_pair(std::round(edge.vertex1()->x()), std::round(edge.vertex1()->y()));

            if (world->is_valid(p0) && world->is_valid(p1))
            {
                remove = remove || world->at(p0)->is_wall() || world->at(p1)->is_wall();
            }
            else
            {
                remove = true;
            }
        }
        else
        {
            remove = true;
        }

        return remove;
    };

    m_filtered_edges.erase(std::remove_if(m_filtered_edges.begin(), m_filtered_edges.end(), edge_filter), m_filtered_edges.end());

    return true;
};