diff --git a/.gitattributes b/.gitattributes index 24524ac53d7..b150ef240fb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2099,6 +2099,13 @@ Principal_component_analysis/include/CGAL/linear_least_squares_fitting_triangles Principal_component_analysis/include/CGAL/util.h -text Principal_component_analysis/test/Principal_component_analysis/fitting_rectangles_2.cpp -text Principal_component_analysis/test/Principal_component_analysis/fitting_segments_2.cpp -text +Principal_component_analysis/test/Principal_component_analysis/fitting_segments_3.cpp -text +Principal_component_analysis/test/Principal_component_analysis/fitting_triangles_2.cpp -text +Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_circles_2.cpp -text +Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_cuboids_3.cpp -text +Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp -text +Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_tetrahedrons_3.cpp -text +Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_triangles_3.cpp -text QP_solver/doc_tex/QP_solver/PkgDescription.tex -text QP_solver/doc_tex/QP_solver/closest_point.eps -text svneol=unset#application/postscript QP_solver/doc_tex/QP_solver/closest_point.fig -text svneol=unset#application/octet-stream diff --git a/Principal_component_analysis/examples/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp b/Principal_component_analysis/examples/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp index 3b1c7231862..38a2fb4f248 100644 --- a/Principal_component_analysis/examples/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp +++ b/Principal_component_analysis/examples/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp @@ -1,4 +1,4 @@ -// Example program for the linear_least_square_fitting function +// Example program for the linear_least_square_fitting function on set of spheres in 3D #include #include diff --git a/Principal_component_analysis/include/CGAL/centroid.h b/Principal_component_analysis/include/CGAL/centroid.h index 68194691bba..730c3c68197 100644 --- a/Principal_component_analysis/include/CGAL/centroid.h +++ b/Principal_component_analysis/include/CGAL/centroid.h @@ -444,8 +444,9 @@ centroid(InputIterator begin, it++) { const Segment& s = *it; - FT length = std::sqrt(std::abs(s.squared_length())); - Point c = K().construct_midpoint_3_object()(s[0],s[1]); + FT length = std::sqrt(s.squared_length()); + // Point c = K().construct_midpoint_3_object()(s[0],s[1]); + Point c = Point((s[0][0] + s[1][0])/2.0, (s[0][1] + s[1][1])/2.0, (s[0][2] + s[1][2])/2.0); v = v + length * (c - ORIGIN); sum_lengths += length; } diff --git a/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_2.h b/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_2.h index 54def822d72..4f3ec979a37 100644 --- a/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_2.h +++ b/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_2.h @@ -71,13 +71,13 @@ linear_least_squares_fitting_2(InputIterator first, // deduces the kernel from the points in container. template < typename InputIterator, typename Line, - typename Point, typename tag> + typename tag> inline typename Kernel_traits::Kernel::FT linear_least_squares_fitting_2(InputIterator first, InputIterator beyond, Line& line, - Point& centroid, + typename Kernel_traits::Kernel::Point_2& centroid, const tag& t) { typedef typename std::iterator_traits::value_type Value_type; diff --git a/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_3.h b/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_3.h index 1ce6e2a8de5..1ff9bcac4b7 100644 --- a/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_3.h +++ b/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_3.h @@ -108,13 +108,13 @@ linear_least_squares_fitting_3(InputIterator first, // deduces the kernel from the points in container. template < typename InputIterator, typename Object, - typename Point, typename tag> + typename tag> inline typename Kernel_traits::Kernel::FT linear_least_squares_fitting_3(InputIterator first, InputIterator beyond, Object& object, - Point& centroid, + typename Kernel_traits::Kernel::Point_3& centroid, const tag& t) { typedef typename std::iterator_traits::value_type Value_type; diff --git a/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_circles_2.h b/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_circles_2.h index 7134d121f48..3d3f2175628 100644 --- a/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_circles_2.h +++ b/Principal_component_analysis/include/CGAL/linear_least_squares_fitting_circles_2.h @@ -120,9 +120,6 @@ linear_least_squares_fitting_2(InputIterator first, covariance[1] += mass * (-1.0 * c.x() * c.y()); covariance[2] += mass * (-1.0 * c.y() * c.y()); - // to remove later - std::cout<(3,delta); - FT volume = t.volume(); - CGAL_assertion(volume != 0.0); + FT area = pow(delta[0]*delta[0] + delta[3]*delta[3] + delta[6]*delta[6],1/3.0)*pow(delta[1]*delta[1] + delta[4]*delta[4] + delta[7]*delta[7],1/3.0)*2 + pow(delta[0]*delta[0] + delta[3]*delta[3] + delta[6]*delta[6],1/3.0)*pow(delta[2]*delta[2] + delta[5]*delta[5] + delta[8]*delta[8],1/3.0)*2 + pow(delta[1]*delta[1] + delta[4]*delta[4] + delta[7]*delta[7],1/3.0)*pow(delta[2]*delta[2] + delta[5]*delta[5] + delta[8]*delta[8],1/3.0)*2; + CGAL_assertion(area != 0.0); // Find the 2nd order moment for the cuboid wrt to the origin by an affine transformation. // Transform the standard 2nd order moment using the transformation matrix - transformation = volume * transformation * moment * LA::transpose(transformation); + transformation = area * transformation * moment * LA::transpose(transformation); // Translate the 2nd order moment to the minimum corner (x0,y0,z0) of the cuboid. FT xav0 = (delta[0] + delta[1] + delta[2])/4.0; @@ -306,14 +306,14 @@ assemble_covariance_matrix_3(InputIterator first, FT zav0 = (delta[6] + delta[7] + delta[8])/4.0; // and add to covariance matrix - covariance[0] += transformation[0][0] + volume * (2*x0*xav0 + x0*x0); - covariance[1] += transformation[1][0] + volume * (xav0*y0 + yav0*x0 + x0*y0); - covariance[2] += transformation[1][1] + volume * (2*y0*yav0 + y0*y0); - covariance[3] += transformation[2][0] + volume * (x0*zav0 + xav0*z0 + x0*z0); - covariance[4] += transformation[2][1] + volume * (yav0*z0 + y0*zav0 + z0*y0); - covariance[5] += transformation[2][2] + volume * (2*zav0*z0 + z0*z0); + covariance[0] += transformation[0][0] + area * (2*x0*xav0 + x0*x0); + covariance[1] += transformation[1][0] + area * (xav0*y0 + yav0*x0 + x0*y0); + covariance[2] += transformation[1][1] + area * (2*y0*yav0 + y0*y0); + covariance[3] += transformation[2][0] + area * (x0*zav0 + xav0*z0 + x0*z0); + covariance[4] += transformation[2][1] + area * (yav0*z0 + y0*zav0 + z0*y0); + covariance[5] += transformation[2][2] + area * (2*zav0*z0 + z0*z0); - mass += volume; + mass += area; } // Translate the 2nd order moment calculated about the origin to @@ -375,13 +375,95 @@ assemble_covariance_matrix_3(InputIterator first, 0.0, radius, 0.0, 0.0, 0.0, radius}; Matrix transformation = init_Matrix(3,delta); - FT area = 4/3.0 * radius*t.squared_radius(); + FT volume = 4/3.0 * radius*t.squared_radius(); + CGAL_assertion(volume != 0.0); + + // Find the 2nd order moment for the sphere wrt to the origin by an affine transformation. + + // Transform the standard 2nd order moment using the transformation matrix + transformation = (3.0/4.0) * volume * transformation * moment * LA::transpose(transformation); + + // Translate the 2nd order moment to the center of the sphere. + FT x0 = t.center().x(); + FT y0 = t.center().y(); + FT z0 = t.center().z(); + + // and add to covariance matrix + covariance[0] += transformation[0][0] + volume * x0*x0; + covariance[1] += transformation[1][0] + volume * x0*y0; + covariance[2] += transformation[1][1] + volume * y0*y0; + covariance[3] += transformation[2][0] + volume * x0*z0; + covariance[4] += transformation[2][1] + volume * z0*y0; + covariance[5] += transformation[2][2] + volume * z0*z0; + + mass += volume; + } + + // Translate the 2nd order moment calculated about the origin to + // the center of mass to get the covariance. + covariance[0] += mass * (-1.0 * c.x() * c.x()); + covariance[1] += mass * (-1.0 * c.x() * c.y()); + covariance[2] += mass * (-1.0 * c.y() * c.y()); + covariance[3] += mass * (-1.0 * c.z() * c.x()); + covariance[4] += mass * (-1.0 * c.z() * c.y()); + covariance[5] += mass * (-1.0 * c.z() * c.z()); + +} +// assemble covariance matrix from a sphere set +template < typename InputIterator, + typename K > +void +assemble_covariance_matrix_3(InputIterator first, + InputIterator beyond, + typename K::FT covariance[6], // covariance matrix + const typename K::Point_3& c, // centroid + const K& , // kernel + const typename K::Sphere_3*,// used for indirection + const CGAL::PCA_dimension_2_tag& tag) +{ + typedef typename K::FT FT; + typedef typename K::Point_3 Point; + typedef typename K::Vector_3 Vector; + typedef typename K::Sphere_3 Sphere; + typedef typename CGAL::Linear_algebraCd LA; + typedef typename LA::Matrix Matrix; + + // assemble covariance matrix as a semi-definite matrix. + // Matrix numbering: + // 0 + // 1 2 + // 3 4 5 + //Final combined covariance matrix for all spheres and their combined mass + FT mass = 0.0; + + // assemble 2nd order moment about the origin. + FT temp[9] = {4.0/3.0, 0.0, 0.0, + 0.0, 4.0/3.0, 0.0, + 0.0, 0.0, 4.0/3.0}; + Matrix moment = init_Matrix(3,temp); + + for(InputIterator it = first; + it != beyond; + it++) + { + // Now for each sphere, construct the 2nd order moment about the origin. + // assemble the transformation matrix. + const Sphere& t = *it; + + // defined for convenience. + // FT example = CGAL::to_double(t[0].x()); + FT radius = std::sqrt(t.squared_radius()); + FT delta[9] = {radius, 0.0, 0.0, + 0.0, radius, 0.0, + 0.0, 0.0, radius}; + Matrix transformation = init_Matrix(3,delta); + FT area = 4 * t.squared_radius(); CGAL_assertion(area != 0.0); // Find the 2nd order moment for the sphere wrt to the origin by an affine transformation. // Transform the standard 2nd order moment using the transformation matrix - transformation = (3.0/4.0) * area * transformation * moment * LA::transpose(transformation); + transformation = (1.0/4.0) * area * transformation * moment * LA::transpose(transformation); // Translate the 2nd order moment to the center of the sphere. FT x0 = t.center().x(); @@ -550,7 +632,7 @@ assemble_covariance_matrix_3(InputIterator first, // Transform the standard 2nd order moment using the transformation matrix transformation = length * transformation * moment * LA::transpose(transformation); - std::cout< Iso_rectangles; - Iso_rectangles.push_back(Iso_rectangle_2(Point_2(0.0,0.0),Point_2(4.0,4.0))); + Iso_rectangles.push_back(Iso_rectangle_2(Point_2(0.0,0.0),Point_2(5.0,4.0))); // Iso_rectangles.push_back(Iso_rectangle_2(Point_2(4.0,0.0),Point_2(5.0,1.0))); // fit a line @@ -51,14 +51,14 @@ void test_1() } } -// case with only one square vs two rectangles that it is composed of +// case with only one rectangle vs two rectangles that it is composed of // in container // the fitting line must be the same void test_2() { std::cout<<"Test 2"< Iso_rectangles; - Iso_rectangles.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(5.6,19.2))); + Iso_rectangles.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(11.6,19.2))); // Iso_rectangles.push_back(Iso_rectangle_2(Point_2(4.0,0.0),Point_2(5.0,1.0))); // fit a line @@ -77,8 +77,8 @@ void test_2() // fit a line // call all versions of the function std::list Iso_rectangles1; - Iso_rectangles1.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(3.6,19.2))); - Iso_rectangles1.push_back(Iso_rectangle_2(Point_2(3.6,15.2),Point_2(5.6,19.2))); + Iso_rectangles1.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(6.6,19.2))); + Iso_rectangles1.push_back(Iso_rectangle_2(Point_2(6.6,15.2),Point_2(11.6,19.2))); std::cout << "fit 2D line to two small rectangles..."; Kernel k1; Line_2 line1; @@ -97,68 +97,58 @@ void test_2() } } -/* -// case with a point set on a segment vs the segment -// the fitting line must be the same segment -void test_5(const unsigned int nb_points) +// case with only one rectangle vs two rectangles horizontally splitting the previous one in half +// the fitting line must be the same +void test_3() { - std::cout<<"Test 5"< points; - points_on_segment_2(p,q,100,std::back_inserter(points)); - std::cout << "done " << std::endl; + std::cout<<"Test 3"< Iso_rectangles; + Iso_rectangles.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(11.6,19.2))); + // Iso_rectangles.push_back(Iso_rectangle_2(Point_2(4.0,0.0),Point_2(5.0,1.0))); // fit a line - std::cout << "fit 2D line to points..."; + // call all versions of the function + std::cout << "fit 2D line to bigger rectangle..."; + Kernel k; Line_2 line; Point_2 centroid; - - // call all versions of the function FT quality; - Kernel k; - quality = linear_least_squares_fitting_2(points.begin(),points.end(),line); - quality = linear_least_squares_fitting_2(points.begin(),points.end(),line,centroid); - quality = linear_least_squares_fitting_2(points.begin(),points.end(),line,centroid,k); - - std::cout << "done (quality: " << quality << ")" <<" line: "< segments; - segments.push_back(Segment_2(p,q)); + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,CGAL::PCA_dimension_2_tag()); + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,centroid,CGAL::PCA_dimension_2_tag()); + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,centroid,k,CGAL::PCA_dimension_2_tag()); + std::cout << "done (quality: " << quality << ") Line: " << line<<" centroid: "< Iso_rectangles1; + Iso_rectangles1.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(11.6,17.2))); + Iso_rectangles1.push_back(Iso_rectangle_2(Point_2(1.6,17.2),Point_2(11.6,19.2))); + std::cout << "fit 2D line to two small rectangles..."; Kernel k1; Line_2 line1; Point_2 centroid1; FT quality1; - quality1 = linear_least_squares_fitting_2(segments.begin(),segments.end(),line1); - quality1 = linear_least_squares_fitting_2(segments.begin(),segments.end(),line1,centroid1); - quality1 = linear_least_squares_fitting_2(segments.begin(),segments.end(),line1,centroid1,k1); - quality1 = linear_least_squares_fitting_2(segments.begin(),segments.end(),line1,centroid1,k1,false); - std::cout << "done (quality: " << quality1 << ")" <<" line: "< +#include +#include +#include + +#include +#include +#include +#define THRESHOLD 0.001 +// types + +typedef CGAL::Cartesian Kernel; +typedef Kernel::FT FT; + +typedef Kernel::Line_3 Line_3; +typedef Kernel::Plane_3 Plane_3; +typedef Kernel::Point_3 Point_3; +typedef Kernel::Segment_3 Segment_3; +typedef Kernel::Vector_3 Vector_3; +typedef Kernel::Direction_3 Direction_3; + +// case with only one segment in container +// the fitting line must be the same +void test_1() +{ + std::list segments; + segments.push_back(Segment_3(Point_3(3.0,3.0,3.0),Point_3(1.0,1.0,1.0))); + + + // fit a line + // call all versions of the function + std::cout<<"Test 1"< segments; + segments.push_back(Segment_3(Point_3(2.0,2.0,2.0),Point_3(1.5,1.5,1.5))); + segments.push_back(Segment_3(Point_3(1.0,1.0,1.0),Point_3(1.5,1.5,1.5))); + + + // fit a line + // call all versions of the function + std::cout << "fit 3D line..."; + Kernel k; + Line_3 line; + Point_3 centroid; + FT quality; + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,CGAL::PCA_dimension_1_tag()); + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,centroid,CGAL::PCA_dimension_1_tag()); + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,centroid,k,CGAL::PCA_dimension_1_tag()); + std::cout << "done (quality: " << quality << ")" << std::endl; + + if(!(line.has_on(Point_3(2.0,2.0,2.0)) && (double)line.to_vector().x() - 1/std::sqrt(3.0) <=THRESHOLD && (double)line.to_vector().y() - 1.0/std::sqrt(3.0) <= THRESHOLD && (double)line.to_vector().z() - 1.0/std::sqrt(3.0) <= THRESHOLD)) + { + std::cout << "failure" << std::endl; + exit(1); // failure + } +} + +// case with four segments cutting the one segment in four parts in +// container the fitting line must be the same +void test_3() +{ + std::cout<<"Test 3"< segments; + segments.push_back(Segment_3(Point_3(2.0,2.0,2.0),Point_3(1.75,1.75,1.75))); + segments.push_back(Segment_3(Point_3(1.75,1.75,1.75),Point_3(1.50,1.50,1.50))); + segments.push_back(Segment_3(Point_3(1.50,1.50,1.50),Point_3(1.25,1.25,1.25))); + segments.push_back(Segment_3(Point_3(1.25,1.25,1.25),Point_3(1.0,1.0,1.0))); + + // fit a line + // call all versions of the function + std::cout << "fit 3D line..."; + Kernel k; + Line_3 line; + Point_3 centroid; + FT quality; + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,CGAL::PCA_dimension_1_tag()); + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,centroid,CGAL::PCA_dimension_1_tag()); + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,centroid,k,CGAL::PCA_dimension_1_tag()); + std::cout.precision(20); + std::cout << "done (quality: " << quality << ")" <<" line: "< segments; + segments.push_back(Segment_3(p,q)); + + + // fit a line + // call all versions of the function + std::cout << "fit 3D line to segment..."; + Kernel k; + Line_3 line; + Point_3 centroid; + FT quality; + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,CGAL::PCA_dimension_1_tag()); + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,centroid,CGAL::PCA_dimension_1_tag()); + quality = linear_least_squares_fitting_3(segments.begin(),segments.end(),line,centroid,k,CGAL::PCA_dimension_1_tag()); + std::cout << "done (quality: " << quality << ")" <<" line: "< points; + points.push_back(p); + points.push_back(q); + + // fit a line + // call all versions of the function + std::cout << "fit 3D line to end points..."; + Kernel k1; + Line_3 line1; + Point_3 centroid1; + FT quality1; + quality1 = linear_least_squares_fitting_3(points.begin(),points.end(),line1,CGAL::PCA_dimension_0_tag()); + quality1 = linear_least_squares_fitting_3(points.begin(),points.end(),line1,centroid1,CGAL::PCA_dimension_0_tag()); + quality1 = linear_least_squares_fitting_3(points.begin(),points.end(),line1,centroid1,k1,CGAL::PCA_dimension_0_tag()); + std::cout << "done (quality: " << quality1 << ")" <<" line: "< segments; + segments.push_back(Segment_3(Point_3(3.0,3.0,3.0),Point_3(1.0,1.0,1.0))); + + + // fit a plane + // call all versions of the function + std::cout<<"Test 5"< segments; + segments.push_back(Segment_3(Point_3(3.0,3.0,3.0),Point_3(1.0,1.0,1.0))); + + + // call all versions of the function + std::cout<<"Test 6"< +#include +#include +#include + +#include +#include +#include +#define THRESHOLD 0.001 +// types + +typedef CGAL::Cartesian Kernel; +typedef Kernel::FT FT; + +typedef Kernel::Line_2 Line_2; +typedef Kernel::Point_2 Point_2; +typedef Kernel::Triangle_2 Triangle_2; +typedef Kernel::Iso_rectangle_2 Iso_rectangle_2; +typedef Kernel::Segment_2 Segment_2; +typedef Kernel::Vector_2 Vector_2; + +// case with one rectangle in container vs it split into two triangles +// the fitting line must be the same. +void test_1() +{ + std::cout<<"Test 1"< Iso_rectangles; + Iso_rectangles.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(11.6,19.2))); + + // fit a line + // call all versions of the function + std::cout << "fit 2D line to bigger rectangle..."; + Kernel k; + Line_2 line; + Point_2 centroid; + FT quality; + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,CGAL::PCA_dimension_2_tag()); + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,centroid,CGAL::PCA_dimension_2_tag()); + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,centroid,k,CGAL::PCA_dimension_2_tag()); + std::cout << "done (quality: " << quality << ") Line: " << line<<" centroid: "< Triangles1; + Triangles1.push_back(Triangle_2(Point_2(1.6,15.2),Point_2(1.6,19.2),Point_2(11.6,19.2))); + Triangles1.push_back(Triangle_2(Point_2(1.6,15.2),Point_2(11.6,19.2),Point_2(11.6,15.2))); + + // fit a line + // call all versions of the function + std::cout << "fit 2D line to two small triangles..."; + Kernel k1; + Line_2 line1; + Point_2 centroid1; + FT quality1; + quality1 = linear_least_squares_fitting_2(Triangles1.begin(),Triangles1.end(),line1,CGAL::PCA_dimension_2_tag()); + quality1 = linear_least_squares_fitting_2(Triangles1.begin(),Triangles1.end(),line1,centroid1,CGAL::PCA_dimension_2_tag()); + quality1 = linear_least_squares_fitting_2(Triangles1.begin(),Triangles1.end(),line1,centroid1,k1,CGAL::PCA_dimension_2_tag()); + std::cout << "done (quality: " << quality1 << ") Line: " << line1<<" centroid: "< Iso_rectangles; + Iso_rectangles.push_back(Iso_rectangle_2(Point_2(1.6,15.2),Point_2(11.6,19.2))); + + // fit a line + // call all versions of the function + std::cout << "fit 2D line to bigger rectangle..."; + Kernel k; + Line_2 line; + Point_2 centroid; + FT quality; + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,CGAL::PCA_dimension_2_tag()); + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,centroid,CGAL::PCA_dimension_2_tag()); + quality = linear_least_squares_fitting_2(Iso_rectangles.begin(),Iso_rectangles.end(),line,centroid,k,CGAL::PCA_dimension_2_tag()); + std::cout << "done (quality: " << quality << ") Line: " << line<<" centroid: "< Triangles1; + Triangles1.push_back(Triangle_2(Point_2(1.6,15.2),Point_2(1.6,19.2),Point_2(6.6,17.2))); + Triangles1.push_back(Triangle_2(Point_2(11.6,19.2),Point_2(11.6,15.2),Point_2(6.6,17.2))); + Triangles1.push_back(Triangle_2(Point_2(1.6,19.2),Point_2(11.6,19.2),Point_2(6.6,17.2))); + Triangles1.push_back(Triangle_2(Point_2(1.6,15.2),Point_2(11.6,15.2),Point_2(6.6,17.2))); + + // fit a line + // call all versions of the function + std::cout << "fit 2D line to four small triangles..."; + Kernel k1; + Line_2 line1; + Point_2 centroid1; + FT quality1; + quality1 = linear_least_squares_fitting_2(Triangles1.begin(),Triangles1.end(),line1,CGAL::PCA_dimension_2_tag()); + quality1 = linear_least_squares_fitting_2(Triangles1.begin(),Triangles1.end(),line1,centroid1,CGAL::PCA_dimension_2_tag()); + quality1 = linear_least_squares_fitting_2(Triangles1.begin(),Triangles1.end(),line1,centroid1,k1,CGAL::PCA_dimension_2_tag()); + std::cout << "done (quality: " << quality1 << ") Line: " << line1<<" centroid: "< +#include + +#include + +typedef double FT; +typedef CGAL::Cartesian K; +typedef K::Line_2 Line_2; +typedef K::Point_2 Point_2; +typedef K::Circle_2 Circle_2; + +int main() +{ + std::list circles; + circles.push_back(Circle_2(Point_2(0.0,0.0),9)); + circles.push_back(Circle_2(Point_2(0.0,10.0),49)); + circles.push_back(Circle_2(Point_2(10.0,0.0),49)); + + Line_2 line; + Point_2 c; + K k; + + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,CGAL::PCA_dimension_1_tag()); + + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,c,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,c,CGAL::PCA_dimension_1_tag()); + + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,k,CGAL::PCA_dimension_1_tag()); + + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,c,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_2(circles.begin(),circles.end(),line,c,k,CGAL::PCA_dimension_1_tag()); + + return 0; +} diff --git a/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_cuboids_3.cpp b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_cuboids_3.cpp new file mode 100644 index 00000000000..47125d0f341 --- /dev/null +++ b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_cuboids_3.cpp @@ -0,0 +1,68 @@ +// Example program for the linear_least_square_fitting function on set of cuboids in 3D + +#include +#include + +#include + +typedef double FT; +typedef CGAL::Cartesian K; +typedef K::Line_3 Line_3; +typedef K::Plane_3 Plane_3; +typedef K::Point_3 Point_3; +typedef K::Iso_cuboid_3 Iso_cuboid_3; + +int main() +{ + std::list Iso_cuboids; + Iso_cuboids.push_back(Iso_cuboid_3(Point_3(0.0,0.0,0.0),Point_3(1.0,1.0,1.0))); + Iso_cuboids.push_back(Iso_cuboid_3(Point_3(1.0,1.0,1.0),Point_3(6.0,6.0,6.0))); + + Line_3 line; + K k; + Point_3 c; + + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,k,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),line,c,k,CGAL::PCA_dimension_0_tag()); + + Plane_3 plane; + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,k,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(Iso_cuboids.begin(),Iso_cuboids.end(),plane,c,k,CGAL::PCA_dimension_0_tag()); + + + return 0; +} diff --git a/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp new file mode 100644 index 00000000000..a6f68ec0290 --- /dev/null +++ b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_spheres_3.cpp @@ -0,0 +1,52 @@ +// Example program for the linear_least_square_fitting function + +#include +#include + +#include + +typedef double FT; +typedef CGAL::Cartesian K; +typedef K::Line_3 Line_3; +typedef K::Plane_3 Plane_3; +typedef K::Point_3 Point_3; +typedef K::Sphere_3 Sphere_3; + +int main() +{ + std::list spheres; + spheres.push_back(Sphere_3(Point_3(0.0,0.0,0.0),9)); + spheres.push_back(Sphere_3(Point_3(0.0,10.0,0.0),25)); + + Line_3 line; + Plane_3 plane; + K k; + Point_3 c; + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,CGAL::PCA_dimension_2_tag()); + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,c,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,c,CGAL::PCA_dimension_2_tag()); + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,k,CGAL::PCA_dimension_2_tag()); + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,c,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),line,c,k,CGAL::PCA_dimension_2_tag()); + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,CGAL::PCA_dimension_2_tag()); + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,c,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,c,CGAL::PCA_dimension_2_tag()); + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,k,CGAL::PCA_dimension_2_tag()); + + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,c,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(spheres.begin(),spheres.end(),plane,c,k,CGAL::PCA_dimension_2_tag()); + + + return 0; +} diff --git a/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_tetrahedrons_3.cpp b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_tetrahedrons_3.cpp new file mode 100644 index 00000000000..026131b44ea --- /dev/null +++ b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_tetrahedrons_3.cpp @@ -0,0 +1,68 @@ +// Example program for the linear_least_square_fitting function on set of tetrahedrons in 3D + +#include +#include + +#include + +typedef double FT; +typedef CGAL::Cartesian K; +typedef K::Line_3 Line_3; +typedef K::Plane_3 Plane_3; +typedef K::Point_3 Point_3; +typedef K::Tetrahedron_3 Tetrahedron_3; + +int main() +{ + std::list tetrahedrons; + tetrahedrons.push_back(Tetrahedron_3(Point_3(0.0,0.0,0.0),Point_3(1.0,0.0,0.0),Point_3(0.0,1.0,0.0),Point_3(0.0,0.0,1.0))); + tetrahedrons.push_back(Tetrahedron_3(Point_3(0.0,0.0,0.0),Point_3(-1.0,0.0,0.0),Point_3(0.0,-1.0,0.0),Point_3(0.0,0.0,1.0))); + + Line_3 line; + Point_3 c; + K k; + + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,k,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),line,c,k,CGAL::PCA_dimension_0_tag()); + + Plane_3 plane; + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,k,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,k,CGAL::PCA_dimension_3_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(tetrahedrons.begin(),tetrahedrons.end(),plane,c,k,CGAL::PCA_dimension_0_tag()); + + + return 0; +} diff --git a/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_triangles_3.cpp b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_triangles_3.cpp new file mode 100644 index 00000000000..9f0fe8a65fb --- /dev/null +++ b/Principal_component_analysis/test/Principal_component_analysis/linear_least_squares_fitting_triangles_3.cpp @@ -0,0 +1,57 @@ +// Example program for the linear_least_square_fitting function on set of triangles in 3D + +#include +#include + +#include + +typedef double FT; +typedef CGAL::Cartesian K; +typedef K::Line_3 Line_3; +typedef K::Plane_3 Plane_3; +typedef K::Point_3 Point_3; +typedef K::Triangle_3 Triangle_3; + +int main() +{ + std::list triangles; + triangles.push_back(Triangle_3(Point_3(1.0,0.0,0.0),Point_3(0.0,1.0,0.0),Point_3(0.0,0.0,0.0))); + triangles.push_back(Triangle_3(Point_3(-1.0,0.0,0.0),Point_3(0.0,-1.0,0.0),Point_3(0.0,0.0,0.0))); + + Line_3 line; + Point_3 c; + K k; + + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,k,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,c,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,c,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,c,k,CGAL::PCA_dimension_0_tag()); + + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,c,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,c,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),line,c,CGAL::PCA_dimension_0_tag()); + + Plane_3 plane; + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,CGAL::PCA_dimension_0_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,k,CGAL::PCA_dimension_0_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,c,k,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,c,k,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,c,k,CGAL::PCA_dimension_0_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,c,CGAL::PCA_dimension_2_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,c,CGAL::PCA_dimension_1_tag()); + linear_least_squares_fitting_3(triangles.begin(),triangles.end(),plane,c,CGAL::PCA_dimension_0_tag()); + + + return 0; +}