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 <iostream>
#include <cassert>
#include <array>
#ifdef CGAL_USE_GMP
#include <CGAL/Gmpzf.h>
@ -25,10 +26,10 @@ typedef CGAL::Min_annulus_d<Traits> Min_annulus;
int main()
{
// 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),
Point(-2,-2), Point(-2,2), Point(2,-2), Point(2,2)};
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)};
Min_annulus ma(P, P+8);
Min_annulus ma(P.begin(), P.end());
assert (ma.is_valid());
// get center of annulus

View File

@ -11,6 +11,7 @@ typedef CGAL::Exact_integer ET;
#include <iostream>
#include <cassert>
#include <array>
// use an inexact kernel...
typedef CGAL::Homogeneous<double> K;
@ -24,10 +25,10 @@ typedef CGAL::Min_annulus_d<Traits> Min_annulus;
int main()
{
// 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),
Point(-2,-2), Point(-2,2), Point(2,-2), Point(2,2)};
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)};
Min_annulus ma(P, P+8);
Min_annulus ma(P.begin(), P.end());
assert (ma.is_valid());
// get center of annulus

View File

@ -4,6 +4,7 @@
#include <CGAL/Random.h>
#include <iostream>
#include <array>
typedef CGAL::Simple_cartesian<double> K;
typedef CGAL::Min_sphere_of_points_d_traits_2<K,double> Traits;
@ -14,14 +15,14 @@ int
main( int, char**)
{
const int n = 100;
Point P[n];
std::array<Point, n> P;
CGAL::Random r; // random number generator
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();
std::cout << "center:";

View File

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

View File

@ -4,6 +4,7 @@
#include <CGAL/Exact_rational.h>
#include <cassert>
#include <array>
typedef CGAL::Exact_rational NT;
typedef CGAL::Cartesian<NT> K;
@ -16,20 +17,20 @@ int
main( int, char**)
{
const int n = 200;
Point P[n];
std::array<Point, n> P;
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)
std::cout << "Computing ellipse (without randomization)...";
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 << "Computing ellipse (with randomization)...";
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;
// because all input points are collinear, the ellipse is

View File

@ -5,6 +5,7 @@
#include <iostream>
#include <cstdlib>
#include <array>
typedef CGAL::Simple_cartesian<double> K;
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 ()
{
Point P[n]; // n points
std::array<Point, n> P; // n points
CGAL::Random r; // random number generator
for (int i=0; i<n; ++i) {
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();
std::cout << "center:";

View File

@ -6,6 +6,7 @@
#include <iostream>
#include <cstdlib>
#include <array>
typedef CGAL::Homogeneous<CGAL::Exact_integer> K;
typedef CGAL::Min_sphere_annulus_d_traits_3<K> Traits;
@ -16,14 +17,14 @@ int
main ()
{
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
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);
std::cout << ms; // output the sphere