Used std::vector instead of c-style arrays for point storage. (#8709)

Updated two CGAL examples to use ``std::vector`` instead of plain
C-style arrays for storing points.

Few more examples can be improved the same way to use ``std::vector``
and avoid usage of c-style arrays.
- Min_annulus_d
- Min_circle_2
- Min_ellipse
- Min_sphere_d
This commit is contained in:
Sebastien Loriot 2025-03-31 09:04:00 +02:00 committed by GitHub
commit 14dd2cb7be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 23 deletions

View File

@ -6,6 +6,7 @@
#include <CGAL/Homogeneous.h> #include <CGAL/Homogeneous.h>
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include <array>
#ifdef CGAL_USE_GMP #ifdef CGAL_USE_GMP
#include <CGAL/Gmpzf.h> #include <CGAL/Gmpzf.h>
@ -25,10 +26,10 @@ typedef CGAL::Min_annulus_d<Traits> Min_annulus;
int main() int main()
{ {
// points on the squares [-1,1]^2 and [-2,2]^2 // points on the squares [-1,1]^2 and [-2,2]^2
Point P[8] = { Point(-1,-1), Point(-1,1), Point(1,-1), Point(1,1), std::array<Point, 8> P = { Point(-1,-1), Point(-1,1), Point(1,-1), Point(1,1),
Point(-2,-2), Point(-2,2), Point(2,-2), Point(2,2)}; Point(-2,-2), Point(-2,2), Point(2,-2), Point(2,2)};
Min_annulus ma(P, P+8); Min_annulus ma(P.begin(), P.end());
assert (ma.is_valid()); assert (ma.is_valid());
// get center of annulus // get center of annulus

View File

@ -11,6 +11,7 @@ typedef CGAL::Exact_integer ET;
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include <array>
// use an inexact kernel... // use an inexact kernel...
typedef CGAL::Homogeneous<double> K; typedef CGAL::Homogeneous<double> K;
@ -24,10 +25,10 @@ typedef CGAL::Min_annulus_d<Traits> Min_annulus;
int main() int main()
{ {
// points on the squares [-1,1]^2 and [-2,2]^2 // points on the squares [-1,1]^2 and [-2,2]^2
Point P[8] = { Point(-1,-1), Point(-1,1), Point(1,-1), Point(1,1), std::array<Point, 8> P = { Point(-1,-1), Point(-1,1), Point(1,-1), Point(1,1),
Point(-2,-2), Point(-2,2), Point(2,-2), Point(2,2)}; Point(-2,-2), Point(-2,2), Point(2,-2), Point(2,2)};
Min_annulus ma(P, P+8); Min_annulus ma(P.begin(), P.end());
assert (ma.is_valid()); assert (ma.is_valid());
// get center of annulus // get center of annulus

View File

@ -4,6 +4,7 @@
#include <CGAL/Random.h> #include <CGAL/Random.h>
#include <iostream> #include <iostream>
#include <array>
typedef CGAL::Simple_cartesian<double> K; typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Min_sphere_of_points_d_traits_2<K,double> Traits; typedef CGAL::Min_sphere_of_points_d_traits_2<K,double> Traits;
@ -14,14 +15,14 @@ int
main( int, char**) main( int, char**)
{ {
const int n = 100; const int n = 100;
Point P[n]; std::array<Point, n> P;
CGAL::Random r; // random number generator CGAL::Random r; // random number generator
for ( int i = 0; i < n; ++i){ for ( int i = 0; i < n; ++i){
P[ i] = Point(r.get_double(), r.get_double()); P.at(i) = Point(r.get_double(), r.get_double());
} }
Min_circle mc( P, P+n); Min_circle mc( P.begin(), P.end());
Min_circle::Cartesian_const_iterator ccib = mc.center_cartesian_begin(), ccie = mc.center_cartesian_end(); Min_circle::Cartesian_const_iterator ccib = mc.center_cartesian_begin(), ccie = mc.center_cartesian_end();
std::cout << "center:"; std::cout << "center:";

View File

@ -3,6 +3,7 @@
#include <CGAL/Min_circle_2.h> #include <CGAL/Min_circle_2.h>
#include <CGAL/Min_circle_2_traits_2.h> #include <CGAL/Min_circle_2_traits_2.h>
#include <array>
#include <iostream> #include <iostream>
// typedefs // typedefs
@ -16,15 +17,15 @@ int
main( int, char**) main( int, char**)
{ {
const int n = 100; const int n = 100;
Point P[n]; std::array<Point, n> P;
for ( int i = 0; i < n; ++i){ for ( int i = 0; i < n; ++i){
P[i] = Point( (i%2 == 0 ? i : -i), 0, 1); P.at(i) = Point( (i%2 == 0 ? i : -i), 0, 1);
// (0,0), (-1,0), (2,0), (-3,0), ... // (0,0), (-1,0), (2,0), (-3,0), ...
} }
Min_circle mc1( P, P+n, false); // very slow Min_circle mc1( P.begin(), P.end(), false); // very slow
Min_circle mc2( P, P+n, true); // fast Min_circle mc2( P.begin(), P.end(), true); // fast
CGAL::IO::set_pretty_mode( std::cout); CGAL::IO::set_pretty_mode( std::cout);
std::cout << mc2; std::cout << mc2;

View File

@ -4,6 +4,7 @@
#include <CGAL/Exact_rational.h> #include <CGAL/Exact_rational.h>
#include <cassert> #include <cassert>
#include <array>
typedef CGAL::Exact_rational NT; typedef CGAL::Exact_rational NT;
typedef CGAL::Cartesian<NT> K; typedef CGAL::Cartesian<NT> K;
@ -16,20 +17,20 @@ int
main( int, char**) main( int, char**)
{ {
const int n = 200; const int n = 200;
Point P[n]; std::array<Point, n> P;
for ( int i = 0; i < n; ++i) for ( int i = 0; i < n; ++i)
P[ i] = Point( i % 2 ? i : -i , 0); P.at(i) = Point( i % 2 ? i : -i , 0);
// (0,0), (-1,0), (2,0), (-3,0) // (0,0), (-1,0), (2,0), (-3,0)
std::cout << "Computing ellipse (without randomization)..."; std::cout << "Computing ellipse (without randomization)...";
std::cout.flush(); std::cout.flush();
Min_ellipse me1( P, P+n, false); // very slow Min_ellipse me1( P.begin(), P.end(), false); // very slow
std::cout << "done." << std::endl; std::cout << "done." << std::endl;
std::cout << "Computing ellipse (with randomization)..."; std::cout << "Computing ellipse (with randomization)...";
std::cout.flush(); std::cout.flush();
Min_ellipse me2( P, P+n, true); // fast Min_ellipse me2( P.begin(), P.end(), true); // fast
std::cout << "done." << std::endl; std::cout << "done." << std::endl;
// because all input points are collinear, the ellipse is // because all input points are collinear, the ellipse is

View File

@ -5,6 +5,7 @@
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <array>
typedef CGAL::Simple_cartesian<double> K; typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Min_sphere_of_points_d_traits_3<K,double> Traits; typedef CGAL::Min_sphere_of_points_d_traits_3<K,double> Traits;
@ -16,16 +17,16 @@ const int d = 3; // dimension of points
int main () int main ()
{ {
Point P[n]; // n points std::array<Point, n> P; // n points
CGAL::Random r; // random number generator CGAL::Random r; // random number generator
for (int i=0; i<n; ++i) { for (int i=0; i<n; ++i) {
for (int j = 0; j < d; ++j) { for (int j = 0; j < d; ++j) {
P[i] = Point(r.get_double(), r.get_double(), r.get_double()); // random point P.at(i) = Point(r.get_double(), r.get_double(), r.get_double()); // random point
} }
} }
Min_sphere ms(P, P+n); // smallest enclosing sphere Min_sphere ms(P.begin(), P.end()); // smallest enclosing sphere
Min_sphere::Cartesian_const_iterator ccib = ms.center_cartesian_begin(), ccie = ms.center_cartesian_end(); Min_sphere::Cartesian_const_iterator ccib = ms.center_cartesian_begin(), ccie = ms.center_cartesian_end();
std::cout << "center:"; std::cout << "center:";

View File

@ -6,6 +6,7 @@
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <array>
typedef CGAL::Homogeneous<CGAL::Exact_integer> K; typedef CGAL::Homogeneous<CGAL::Exact_integer> K;
typedef CGAL::Min_sphere_annulus_d_traits_3<K> Traits; typedef CGAL::Min_sphere_annulus_d_traits_3<K> Traits;
@ -16,14 +17,14 @@ int
main () main ()
{ {
const int n = 10; // number of points const int n = 10; // number of points
Point P[n]; // n points std::array<Point, n> P; // n points
CGAL::Random r; // random number generator CGAL::Random r; // random number generator
for (int i=0; i<n; ++i) { for (int i=0; i<n; ++i) {
P[i] = Point(r.get_int(0, 1000),r.get_int(0, 1000), r.get_int(0, 1000), 1 ); P.at(i) = Point(r.get_int(0, 1000),r.get_int(0, 1000), r.get_int(0, 1000), 1 );
} }
Min_sphere ms (P, P+n); // smallest enclosing sphere Min_sphere ms (P.begin(), P.end()); // smallest enclosing sphere
CGAL::IO::set_pretty_mode (std::cout); CGAL::IO::set_pretty_mode (std::cout);
std::cout << ms; // output the sphere std::cout << ms; // output the sphere