root/branches/debug/extra/driving_distance/src/alpha_drivedist.cpp

Revision 128, 4.6 KB (checked in by anton, 3 years ago)

Debugging branch 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
38#include <vector>
39#include <list>
40
41#include "alpha.h"
42
43#include <CGAL/Delaunay_triangulation_2.h>
44#include <CGAL/Triangulation_2.h>
45#include <CGAL/Triangulation_hierarchy_vertex_base_2.h>
46#include <CGAL/Triangulation_hierarchy_2.h>
47#include <CGAL/Triangulation_face_base_2.h>
48#include <CGAL/Triangulation_euclidean_traits_2.h>
49#include <CGAL/Alpha_shape_2.h>
50#include <CGAL/Alpha_shape_face_base_2.h>
51#include <CGAL/Alpha_shape_2.h>
52#include <CGAL/Alpha_shape_vertex_base_2.h>
53
54typedef double coord_type;
55
56typedef CGAL::Simple_cartesian<coord_type>  SC;
57typedef CGAL::Filtered_kernel<SC> K;
58
59typedef K::Point_2  Point;
60typedef K::Segment_2  Segment;
61
62typedef CGAL::Alpha_shape_vertex_base_2<K> Avb;
63typedef CGAL::Triangulation_hierarchy_vertex_base_2<Avb> Av;
64
65typedef CGAL::Triangulation_face_base_2<K> Tf;
66typedef CGAL::Alpha_shape_face_base_2<K,Tf> Af;
67
68typedef CGAL::Triangulation_default_data_structure_2<K,Av,Af> Tds;
69typedef CGAL::Delaunay_triangulation_2<K,Tds> Dt;
70typedef CGAL::Triangulation_hierarchy_2<Dt> Ht;
71typedef CGAL::Alpha_shape_2<Ht> Alpha_shape_2;
72
73typedef Alpha_shape_2::Face_circulator  Face_circulator;
74typedef Alpha_shape_2::Vertex_circulator  Vertex_circulator;
75
76typedef Alpha_shape_2::Alpha_iterator Alpha_iterator;
77typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator;
78
79//---------------------------------------------------------------------
80
81void find_next_edge(Segment s, std::vector<Segment>& segments,
82                    std::vector<Segment>& res)
83{
84  if(res.size() == segments.size())
85    return;
86   
87  res.push_back(s);
88
89  Point end = s.target();
90 
91  for(int i=0;i < segments.size(); i++)
92    {
93      Point source = segments.at(i).source();
94      if(source == end)
95        {
96          find_next_edge(segments.at(i), segments, res);
97        }
98    }
99}
100
101template <class OutputIterator>
102void
103alpha_edges( const Alpha_shape_2&  A,
104             OutputIterator out)
105{
106
107  for(Alpha_shape_edges_iterator it =  A.alpha_shape_edges_begin();
108      it != A.alpha_shape_edges_end();
109      ++it){
110    *out++ = A.segment(*it);
111  }
112}
113
114
115int alpha_shape(vertex_t *vertices, unsigned int count,
116                vertex_t **res, int *res_count, char **err_msg)
117{
118  std::list<Point> points;
119
120  //std::copy(begin(vertices), end(vertices), std::back_inserter(points));
121 
122  for (std::size_t j = 0; j < count; ++j)
123    {
124      Point p(vertices[j].x, vertices[j].y);
125      points.push_back(p);
126    }
127 
128
129  Alpha_shape_2 A(points.begin(), points.end(),
130                  coord_type(10000),
131                  Alpha_shape_2::GENERAL);
132 
133  std::vector<Segment> segments;
134  std::vector<Segment> result;
135
136  Alpha_shape_2::Alpha_shape_vertices_iterator vit;
137  Alpha_shape_2::Vertex_handle vertex;
138  Alpha_shape_2::Alpha_shape_edges_iterator eit;
139  Alpha_shape_2::Edge edge;
140  Alpha_shape_2::Face_iterator fit;
141  Alpha_shape_2::Face_handle face;
142 
143  A.set_alpha(*A.find_optimal_alpha(1)*6);
144
145  alpha_edges( A, std::back_inserter(segments));
146
147  Segment s = segments.at(0);
148  find_next_edge(s, segments, result);
149
150  *res = (vertex_t *) malloc(sizeof(vertex_t) * (result.size() + 1));
151  *res_count = result.size();
152
153  for(int i=0;i < result.size(); i++)
154    {
155      (*res)[i].x = result.at(i).target().x();
156      (*res)[i].y = result.at(i).target().y();
157    }
158
159  return EXIT_SUCCESS;
160}
Note: See TracBrowser for help on using the browser.