Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77329 - sandbox/gtl/boost/polygon
From: sydorchuk.andriy_at_[hidden]
Date: 2012-03-13 20:39:04


Author: asydorchuk
Date: 2012-03-13 20:39:03 EDT (Tue, 13 Mar 2012)
New Revision: 77329
URL: http://svn.boost.org/trac/boost/changeset/77329

Log:
Adding builder structure to the voronoi_diagram.
This allows to hide public construction methods the user should not deal with.
Text files modified:
   sandbox/gtl/boost/polygon/voronoi_builder.hpp | 12 +++---
   sandbox/gtl/boost/polygon/voronoi_diagram.hpp | 80 +++++++++++++++++++++++++++++++---------
   2 files changed, 68 insertions(+), 24 deletions(-)

Modified: sandbox/gtl/boost/polygon/voronoi_builder.hpp
==============================================================================
--- sandbox/gtl/boost/polygon/voronoi_builder.hpp (original)
+++ sandbox/gtl/boost/polygon/voronoi_builder.hpp 2012-03-13 20:39:03 EDT (Tue, 13 Mar 2012)
@@ -132,7 +132,7 @@
         template <typename OUTPUT>
         void construct(OUTPUT *output) {
             // Init structures.
- output->reserve(site_events_.size());
+ output->builder()->reserve(site_events_.size());
             init_sites_queue();
             if (!circle_events_.empty())
                 circle_events_.clear();
@@ -164,7 +164,7 @@
             beach_line_.clear();
 
             // Clean the output (remove zero-length edges).
- output->seal();
+ output->builder()->build();
         }
 
         void clear() {
@@ -237,7 +237,7 @@
                 return;
             if (site_events_.size() == 1) {
                 // Handle one input site case.
- output->process_single_site(site_events_[0]);
+ output->builder()->process_single_site(site_events_[0]);
                 ++site_event_iterator_;
             } else {
                 int skip = 0;
@@ -292,7 +292,7 @@
                  key_type new_node(*it_first, *it_second);
 
                  // Update the output.
- edge_type *edge = output->insert_new_edge(*it_first, *it_second).first;
+ edge_type *edge = output->builder()->insert_new_edge(*it_first, *it_second).first;
 
                  // Insert a new bisector into the beach line.
                  beach_line_.insert(beach_line_.end(),
@@ -453,7 +453,7 @@
             const_cast<key_type &>(it_first->first).right_site(site3);
 
             // Insert the new bisector into the beach line.
- it_first->second.edge(output->insert_new_edge(
+ it_first->second.edge(output->builder()->insert_new_edge(
                 site1, site3, circle_event, bisector1, bisector2).first);
 
             // Remove the (B, C) bisector node from the beach line.
@@ -500,7 +500,7 @@
 
             // Update the output.
             std::pair<edge_type*, edge_type*> edges =
- output->insert_new_edge(site_arc2, site_event);
+ output->builder()->insert_new_edge(site_arc2, site_event);
             position = beach_line_.insert(position,
                 typename beach_line_type::value_type(new_right_node, value_type(edges.second)));
 

Modified: sandbox/gtl/boost/polygon/voronoi_diagram.hpp
==============================================================================
--- sandbox/gtl/boost/polygon/voronoi_diagram.hpp (original)
+++ sandbox/gtl/boost/polygon/voronoi_diagram.hpp 2012-03-13 20:39:03 EDT (Tue, 13 Mar 2012)
@@ -277,19 +277,56 @@
   typedef typename edge_container_type::iterator edge_iterator;
   typedef typename edge_container_type::const_iterator const_edge_iterator;
 
- voronoi_diagram() : sealed_(false) {}
+ class voronoi_diagram_builder {
+ public:
+ void vd(voronoi_diagram *vd) {
+ vd_ = vd;
+ }
 
- void reserve(int num_sites) {
- cells_.reserve(num_sites);
- vertices_.reserve(num_sites << 1);
- edges_.reserve((num_sites << 2) + (num_sites << 1));
+ bool done() {
+ return vd_ == NULL;
+ }
+
+ void reserve(int num_sites) {
+ vd_->reserve(num_sites);
+ }
+
+ template <typename SEvent>
+ void process_single_site(const SEvent &site) {
+ vd_->process_single_site(site);
+ }
+
+ template <typename SEvent>
+ std::pair<void*, void*> insert_new_edge(
+ const SEvent &site1, const SEvent &site2) {
+ return vd_->insert_new_edge(site1, site2);
+ }
+
+ template <typename SEvent, typename CEvent>
+ std::pair<void *, void *> insert_new_edge(
+ const SEvent &site1, const SEvent &site3, const CEvent &circle,
+ void *data12, void *data23) {
+ return vd_->insert_new_edge(site1, site3, circle, data12, data23);
+ }
+
+ void build() {
+ vd_->build();
+ vd_ = NULL;
+ }
+
+ private:
+ voronoi_diagram *vd_;
+ };
+
+ voronoi_diagram() {
+ builder_.vd(&(*this));
   }
 
   void clear() {
     cells_.clear();
     vertices_.clear();
     edges_.clear();
- sealed_ = false;
+ builder_.vd(&(*this));
   }
 
   const cell_container_type &cells() const {
@@ -316,10 +353,26 @@
     return vertices_.size();
   }
 
+ voronoi_diagram_builder *builder() {
+ if (builder_.done()) {
+ return NULL;
+ } else {
+ return &builder_;
+ }
+ }
+
+private:
+ friend class voronoi_diagram_builder;
+
+ void reserve(int num_sites) {
+ cells_.reserve(num_sites);
+ vertices_.reserve(num_sites << 1);
+ edges_.reserve((num_sites << 2) + (num_sites << 1));
+ }
+
   // Update the voronoi output in case of a single point input.
   template <typename SEvent>
   void process_single_site(const SEvent &site) {
- if (sealed_) return;
     // Update cell records.
     point_type p = prepare_point(site.point0());
     cells_.push_back(cell_type(p, NULL));
@@ -331,8 +384,6 @@
   template <typename SEvent>
   std::pair<void*, void*> insert_new_edge(
       const SEvent &site1, const SEvent &site2) {
- if (sealed_) return std::pair<void*, void*>(NULL, NULL);
-
     // Get sites' indices.
     int site_index1 = site1.index();
     int site_index2 = site2.index();
@@ -378,8 +429,6 @@
   std::pair<void *, void *> insert_new_edge(
       const SEvent &site1, const SEvent &site3, const CEvent &circle,
       void *data12, void *data23) {
- if (sealed_) return std::pair<void*, void*>(NULL, NULL);
-
     edge_type *edge12 = static_cast<edge_type*>(data12);
     edge_type *edge23 = static_cast<edge_type*>(data23);
 
@@ -420,9 +469,7 @@
     return std::make_pair(&new_edge1, &new_edge2);
   }
 
- void seal() {
- if (sealed_) return;
-
+ void build() {
     // Remove degenerate edges.
     edge_iterator last_edge = edges_.begin();
     for (edge_iterator it = edges_.begin(); it != edges_.end(); it += 2) {
@@ -530,11 +577,8 @@
         right_edge->next(left_edge);
       }
     }
-
- sealed_ = true;
   }
 
-private:
   template <typename P>
   point_type prepare_point(const P& p) {
     coordinate_type nx = convert_(p.x());
@@ -571,8 +615,8 @@
   cell_container_type cells_;
   vertex_container_type vertices_;
   edge_container_type edges_;
- bool sealed_;
 
+ voronoi_diagram_builder builder_;
   ctype_converter_type convert_;
   vertex_equality_predicate_type vertex_equality_predicate_;
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk