File: generator.cpp

package info (click to toggle)
cgal 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (181 lines) | stat: -rw-r--r-- 5,465 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2005-2009  INRIA Sophia-Antipolis (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL$
// $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Sebastien Loriot, Sylvain Pion

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <boost/format.hpp>
#include <CGAL/CGAL_Ipelet_base.h>
#include <algorithm>
#include <CGAL/point_generators_2.h>
#include <CGAL/copy_n.h>
#include <CGAL/random_selection.h>
#include <CGAL/random_convex_set_2.h>
#include <CGAL/random_polygon_2.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Join_input_iterator.h>
#include <CGAL/function_objects.h>
#include <CGAL/copy_n.h>


namespace CGAL_generator{


const std::string sublabel[] ={
  "Points in a disk","Points on a grid","Points in a square","Points on a convex hull","Polygon","Segments in a square", "Circles (center in a square)","Help"
};

const std::string hlpmsg[] ={
"Generate random inputs. You have to specify the size of the bounding box and the number of elements"};

typedef CGAL::Exact_predicates_inexact_constructions_kernel               Kernel;

struct generator
  : CGAL::Ipelet_base<Kernel,8>
{
  typedef CGAL::Creator_uniform_2<Kernel::FT,Point_2>                     Creator;
  typedef CGAL::Random_points_in_square_2<Point_2,Creator>                Point_generator;
  typedef CGAL::Creator_uniform_2<Kernel::FT,Point_2>                     Pt_creator;

  generator()
    : CGAL::Ipelet_base<Kernel,8>("Generators",sublabel, hlpmsg){};
  void protected_run(int);
};


void generator::protected_run(int fn)
{
  if (fn==7) {
    show_help(false);
    return;
  }

  std::list<Point_2> pt_list;
  std::list<Circle_2> cir_list;

  Iso_rectangle_2 bbox=
  read_active_objects(
                      CGAL::dispatch_or_drop_output<Point_2,Circle_2>(
      std::back_inserter(pt_list),
      std::back_inserter(cir_list)
    )
  );

  Kernel::Vector_2 origin;
  double size=200;
  int ret_val;


  if (fn==0){
    if (cir_list.size()==0) { print_error_message(("Selection must be a circle")); return;}
    Circle_2  circ=*cir_list.begin();
    size =  sqrt(circ.squared_radius());
    origin= circ.center()-CGAL::ORIGIN;
  }else{
    size = (bbox.xmax()-bbox.xmin())/2;
    origin= Kernel::Vector_2((bbox.xmin()+bbox.xmax())/2,(bbox.ymin()+bbox.ymax())/2);
    if (size<1){
      size=200;
      //std::tie(ret_val,size)=request_value_from_user<int>((boost::format("Size (default : %1%)") % size).str());
      //if (ret_val == -1) return;
      //if (ret_val == 0) size=200;
      origin =  Kernel::Vector_2(200,200);
    }
  }

  int nbelements=30;

  std::tie(ret_val,nbelements)=request_value_from_user<int>((boost::format("Number of elements (default : %1%)") % nbelements).str() );
  if (ret_val == -1) return;
  if (ret_val == 0) nbelements=30;


  if(nbelements < 3){
    print_error_message("Not a good value");
    return;
  }

  std::vector<Point_2> points;
  std::vector<Segment_2> segments;

  if (fn==5)
    points.reserve(nbelements);
  else
    segments.reserve(nbelements);

  get_IpePage()->deselectAll();

  switch(fn){
    case 0:{//random point in a circle
      CGAL::Random_points_in_disc_2<Point_2,Creator> gs( size);
      std::copy_n( gs, nbelements, std::back_inserter(points));
      }
    break;

    case 1://random point on a grid
    points_on_square_grid_2( size, nbelements, std::back_inserter(points),Creator());
    break;

    case 6:
    case 2://points in a square : side =
    {CGAL::Random_points_in_square_2<Point_2, Creator> gc (size);
    std::copy_n( gc, nbelements, std::back_inserter(points));
    }
    break;

    case 3:{//draw random set of point on a convex hull
       CGAL::random_convex_set_2(nbelements, std::back_inserter(points),
        Point_generator( size));
    }
    break;


    case 4:
      // create k-gon and write it into a window:
      CGAL::random_polygon_2(nbelements, std::back_inserter(points),Point_generator(size));
      for ( std::vector<Point_2>::iterator it=points.begin(); it!=points.end(); ++it) *it = *it + origin;
      draw_polyline_in_ipe(points.begin(),points.end(),true);
      return;

    case 5://Random segments
    typedef CGAL::Random_points_in_square_2<Point_2, Creator> P1;
    typedef CGAL::Random_points_in_square_2<Point_2, Creator> P2;

    P1 p1 (size);
    P2 p2 (size);
    typedef CGAL::Creator_uniform_2< Point_2, Segment_2> Seg_creator;
    typedef CGAL::Join_input_iterator_2< P1, P2, Seg_creator> Seg_iterator;
    Seg_iterator g( p1, p2);
    std::copy_n( g, nbelements, std::back_inserter(segments) );
    break;
  };

  if (fn==6){
    CGAL::Random random;
    for (std::vector<Point_2>::iterator it_pt=points.begin();it_pt!=points.end();++it_pt)
      draw_in_ipe(Circle_2(*it_pt+origin,pow(random.get_double(size/20.,size/2.),2) ));
    group_selected_objects_();
  }
  else
    if (!points.empty()){// Translate and draw points
      for ( std::vector<Point_2>::iterator it=points.begin(); it!=points.end(); ++it) *it = *it + origin;
      draw_in_ipe(points.begin(),points.end());
    }
    else
      if (!segments.empty()){// Translate and draw segments
        for ( std::vector<Segment_2>::iterator it=segments.begin(); it!=segments.end(); ++it)
          *it = Segment_2( it->source() + origin, it->target() + origin);
        draw_in_ipe(segments.begin(),segments.end());
      }
}

}

CGAL_IPELET(CGAL_generator::generator)