root/tags/release-1.0-beta/alpha_drivedist.cpp

Revision 39, 5.0 KB (checked in by anton, 3 years ago)

1.0.0b tag added

Line 
1/*
2 * Alpha-Shapes for PostgreSQL
3 *
4 * Copyright (c) 2006 Anton A. Patrushev, Orkney, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 *
21 * As a special exception, you have permission to link this program
22 * with the CGAL library and distribute executables, as long as you
23 * follow the requirements of the GNU GPL in regard to all of the
24 * software in the executable aside from CGAL.
25 *
26 */
27
28
29/***********************************************************************
30Takes a list of points and returns a list of segments
31corresponding to the Alpha shape.
32************************************************************************/
33
34#include <CGAL/Simple_cartesian.h>
35#include <CGAL/Filtered_kernel.h>
36#include <CGAL/algorithm.h>
37#include <iostream>
38#include <fstream>
39#include <vector>
40#include <list>
41
42#include "alpha.h"
43
44#include <CGAL/Delaunay_triangulation_2.h>
45#include <CGAL/Triangulation_2.h>
46#include <CGAL/Triangulation_hierarchy_vertex_base_2.h>
47#include <CGAL/Triangulation_hierarchy_2.h>
48#include <CGAL/Triangulation_face_base_2.h>
49#include <CGAL/Triangulation_euclidean_traits_2.h>
50#include <CGAL/Alpha_shape_2.h>
51#include <CGAL/Alpha_shape_face_base_2.h>
52#include <CGAL/Alpha_shape_2.h>
53#include <CGAL/Alpha_shape_vertex_base_2.h>
54
55typedef double coord_type;
56
57typedef CGAL::Simple_cartesian<coord_type>  SC;
58typedef CGAL::Filtered_kernel<SC> K;
59
60typedef K::Point_2  Point;
61typedef K::Segment_2  Segment;
62
63typedef CGAL::Alpha_shape_vertex_base_2<K> Avb;
64typedef CGAL::Triangulation_hierarchy_vertex_base_2<Avb> Av;
65
66typedef CGAL::Triangulation_face_base_2<K> Tf;
67typedef CGAL::Alpha_shape_face_base_2<K,Tf> Af;
68
69typedef CGAL::Triangulation_default_data_structure_2<K,Av,Af> Tds;
70typedef CGAL::Delaunay_triangulation_2<K,Tds> Dt;
71typedef CGAL::Triangulation_hierarchy_2<Dt> Ht;
72typedef CGAL::Alpha_shape_2<Ht> Alpha_shape_2;
73
74typedef Alpha_shape_2::Face_circulator  Face_circulator;
75typedef Alpha_shape_2::Vertex_circulator  Vertex_circulator;
76
77typedef Alpha_shape_2::Alpha_iterator Alpha_iterator;
78typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator;
79
80//---------------------------------------------------------------------
81
82void find_next_edge(Segment s, std::vector<Segment>& segments,
83                    std::vector<Segment>& res)
84{
85  if(res.size() == segments.size())
86    return;
87   
88  res.push_back(s);
89
90  Point end = s.target();
91 
92  for(int i=0;i < segments.size(); i++)
93    {
94      Point source = segments.at(i).source();
95      if(source == end)
96        {
97          find_next_edge(segments.at(i), segments, res);
98        }
99    }
100}
101
102template <class OutputIterator>
103void
104alpha_edges( const Alpha_shape_2&  A,
105             OutputIterator out)
106{
107
108  for(Alpha_shape_edges_iterator it =  A.alpha_shape_edges_begin();
109      it != A.alpha_shape_edges_end();
110      ++it){
111    *out++ = A.segment(*it);
112  }
113}
114
115template <class OutputIterator>
116bool
117file_input(OutputIterator out)
118{
119  std::ifstream is("./data/set", std::ios::in);
120
121  if(is.fail()){
122    std::cerr << "unable to open file for input" << std::endl;
123    return false;
124  }
125
126  int n;
127  is >> n;
128
129  CGAL::copy_n(std::istream_iterator<Point>(is), n, out);
130
131  return true;
132}
133   
134int alpha_shape(vertex_t *vertices, unsigned int count,
135                vertex_t **res, int *res_count, char **err_msg)
136{
137  std::list<Point> points;
138
139  //std::copy(begin(vertices), end(vertices), std::back_inserter(points));
140 
141  for (std::size_t j = 0; j < count; ++j)
142    {
143      Point p(vertices[j].x, vertices[j].y);
144      points.push_back(p);
145    }
146 
147
148  Alpha_shape_2 A(points.begin(), points.end(),
149                  coord_type(10000),
150                  Alpha_shape_2::GENERAL);
151 
152  std::vector<Segment> segments;
153  std::vector<Segment> result;
154
155  Alpha_shape_2::Alpha_shape_vertices_iterator vit;
156  Alpha_shape_2::Vertex_handle vertex;
157  Alpha_shape_2::Alpha_shape_edges_iterator eit;
158  Alpha_shape_2::Edge edge;
159  Alpha_shape_2::Face_iterator fit;
160  Alpha_shape_2::Face_handle face;
161 
162  A.set_alpha(*A.find_optimal_alpha(1)*6);
163
164  alpha_edges( A, std::back_inserter(segments));
165
166  Segment s = segments.at(0);
167  find_next_edge(s, segments, result);
168
169  *res = (vertex_t *) malloc(sizeof(vertex_t) * (result.size() + 1));
170  *res_count = result.size();
171
172  for(int i=0;i < result.size(); i++)
173    {
174      (*res)[i].x = result.at(i).target().x();
175      (*res)[i].y = result.at(i).target().y();
176    }
177
178  return EXIT_SUCCESS;
179}
Note: See TracBrowser for help on using the browser.