mirror of https://github.com/CGAL/cgal
cleaning
This commit is contained in:
parent
2da405114c
commit
91e49fe995
|
|
@ -30,8 +30,8 @@
|
|||
namespace CGAL {
|
||||
namespace Optimal_bounding_box {
|
||||
|
||||
template<typename Matrix>
|
||||
const double compute_fitness(const Matrix& R, const Matrix& data)
|
||||
template<typename Vertex, typename Matrix>
|
||||
const double compute_fitness(const Vertex& R, const Matrix& data)
|
||||
{
|
||||
|
||||
// R: rotation matrix
|
||||
|
|
@ -42,8 +42,9 @@ const double compute_fitness(const Matrix& R, const Matrix& data)
|
|||
CGAL_assertion(data.rows() >= 3);
|
||||
|
||||
// rotate points
|
||||
Matrix RT = R.transpose();
|
||||
Matrix rotated_data = data * RT;
|
||||
Vertex RT = R.transpose();
|
||||
Matrix rotated_data;
|
||||
rotated_data = data * RT;
|
||||
CGAL_assertion(rotated_data.cols() == data.cols());
|
||||
CGAL_assertion(rotated_data.rows() == data.rows());
|
||||
|
||||
|
|
@ -64,13 +65,13 @@ const double compute_fitness(const Matrix& R, const Matrix& data)
|
|||
|
||||
}
|
||||
|
||||
template <typename Matrix>
|
||||
template <typename Vertex, typename Matrix>
|
||||
struct Fitness_map // -> a free function
|
||||
{
|
||||
Fitness_map(Population<Matrix>& p, Matrix& points) : pop(p), points(points)
|
||||
Fitness_map(Population<Vertex>& p, Matrix& points) : pop(p), points(points)
|
||||
{}
|
||||
|
||||
Matrix get_best()
|
||||
Vertex get_best()
|
||||
{
|
||||
std::size_t count_vertices = 0;
|
||||
|
||||
|
|
@ -79,10 +80,9 @@ struct Fitness_map // -> a free function
|
|||
double best_fitness = std::numeric_limits<int>::max();
|
||||
for(std::size_t i = 0; i < pop.size(); ++i)
|
||||
{
|
||||
//const std::vector<Matrix> simplex = pop[i];
|
||||
for(std::size_t j =0; j < 4; ++j)
|
||||
{
|
||||
const Matrix vertex = pop[i][j];
|
||||
const Vertex vertex = pop[i][j];
|
||||
//std::cout << "i= "<< i << " j=" << j<<"\n vertex= " << vertex << std::endl;
|
||||
++count_vertices;
|
||||
//std::cout << "vertex = " << vertex << std::endl;
|
||||
|
|
@ -98,7 +98,7 @@ struct Fitness_map // -> a free function
|
|||
}
|
||||
}
|
||||
|
||||
std::vector<Matrix> best_simplex = pop[simplex_id];
|
||||
std::vector<Vertex> best_simplex = pop[simplex_id];
|
||||
//Matrix temp = best_simplex[vertex_id];
|
||||
|
||||
return best_simplex[vertex_id];
|
||||
|
|
@ -107,13 +107,13 @@ struct Fitness_map // -> a free function
|
|||
|
||||
double get_best_fitness_value(const Matrix& data)
|
||||
{
|
||||
Matrix best_mat = get_best();
|
||||
Vertex best_mat = get_best();
|
||||
return compute_fitness(best_mat, data);
|
||||
}
|
||||
|
||||
|
||||
const Matrix points;
|
||||
Population<Matrix> pop;
|
||||
const Matrix points; // or just a reference?
|
||||
Population<Vertex> pop;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -42,16 +42,15 @@ namespace CGAL {
|
|||
namespace Optimal_bounding_box {
|
||||
|
||||
|
||||
template <typename Matrix>
|
||||
void evolution(Matrix& R, Matrix& points, std::size_t max_generations) // todo: points is const
|
||||
template <typename Vertex, typename Matrix>
|
||||
void evolution(Vertex& R, Matrix& points, std::size_t max_generations) // todo: points is const
|
||||
{
|
||||
|
||||
CGAL_assertion(points.rows() >= 3);
|
||||
CGAL_assertion(points.cols() == 3);
|
||||
CGAL_assertion(R.rows() == 3);
|
||||
CGAL_assertion(R.cols() == 3);
|
||||
|
||||
Population<Matrix> pop(50);
|
||||
Population<Vertex> pop(50);
|
||||
std::size_t nelder_mead_iterations = 20;
|
||||
|
||||
double prev_fit_value = 0;
|
||||
|
|
@ -89,7 +88,7 @@ void evolution(Matrix& R, Matrix& points, std::size_t max_generations) // todo:
|
|||
#endif
|
||||
|
||||
// stopping criteria
|
||||
Fitness_map<Matrix> fitness_map(pop, points);
|
||||
Fitness_map<Vertex, Matrix> fitness_map(pop, points);
|
||||
new_fit_value = fitness_map.get_best_fitness_value(points);
|
||||
|
||||
double difference = new_fit_value - prev_fit_value;
|
||||
|
|
@ -102,27 +101,30 @@ void evolution(Matrix& R, Matrix& points, std::size_t max_generations) // todo:
|
|||
prev_fit_value = new_fit_value;
|
||||
}
|
||||
|
||||
Fitness_map<Matrix> fitness_map(pop, points);
|
||||
Fitness_map<Vertex, Matrix> fitness_map(pop, points);
|
||||
R = fitness_map.get_best();
|
||||
}
|
||||
|
||||
|
||||
// works on matrices only
|
||||
template <typename Matrix>
|
||||
void post_processing(Matrix& points, Matrix& R, Matrix& obb)
|
||||
template <typename Vertex, typename Matrix_dynamic, typename Matrix_fixed>
|
||||
void post_processing(Matrix_dynamic& points, Vertex& R, Matrix_fixed& obb)
|
||||
{
|
||||
CGAL_assertion(points.cols() == 3);
|
||||
CGAL_assertion(R.rows() == 3);
|
||||
CGAL_assertion(R.cols() == 3);
|
||||
CGAL_assertion(obb.rows() == 8);
|
||||
CGAL_assertion(obb.cols() == 3);
|
||||
|
||||
// 1) rotate points with R
|
||||
|
||||
Matrix rotated_points(points.rows(), points.cols());
|
||||
Matrix_dynamic rotated_points(points.rows(), points.cols());
|
||||
rotated_points = points * R.transpose();
|
||||
|
||||
// 2) get AABB from rotated points
|
||||
|
||||
typedef CGAL::Simple_cartesian<double> K;
|
||||
typedef K::Point_3 Point;
|
||||
std::vector<Point> v_points; // Simplex -> std::vector
|
||||
for(int i = 0; i < rotated_points.rows(); ++i)
|
||||
for(std::size_t i = 0; i < rotated_points.rows(); ++i)
|
||||
{
|
||||
Point p(rotated_points(i, 0), rotated_points(i, 1), rotated_points(i, 2));
|
||||
v_points.push_back(p);
|
||||
|
|
@ -131,8 +133,12 @@ void post_processing(Matrix& points, Matrix& R, Matrix& obb)
|
|||
bbox = bbox_3(v_points.begin(), v_points.end());
|
||||
K::Iso_cuboid_3 ic(bbox);
|
||||
|
||||
Matrix aabb(8, 3);
|
||||
for(int i = 0; i < 8; ++i)
|
||||
Matrix_fixed aabb;
|
||||
// preallocate sanity: if Matrix is not preallocated in compile time
|
||||
if(aabb.cols() != 3 && aabb.rows() != 8)
|
||||
aabb.resize(8, 3);
|
||||
|
||||
for(std::size_t i = 0; i < 8; ++i)
|
||||
{
|
||||
aabb(i, 0) = ic[i].x();
|
||||
aabb(i, 1) = ic[i].y();
|
||||
|
|
@ -140,10 +146,7 @@ void post_processing(Matrix& points, Matrix& R, Matrix& obb)
|
|||
}
|
||||
|
||||
// 3) apply inverse rotation to rotated AABB
|
||||
|
||||
obb = aabb * R;
|
||||
|
||||
|
||||
}
|
||||
|
||||
template <typename Matrix, typename Point>
|
||||
|
|
@ -173,8 +176,9 @@ void find_obb(std::vector<Point>& points, std::vector<Point>& obb_points, bool u
|
|||
obb_points.resize(8);
|
||||
CGAL_assertion(obb_points.size() == 8);
|
||||
|
||||
|
||||
typedef Eigen::MatrixXd MatrixXd; // using eigen internally
|
||||
// using eigen internally
|
||||
typedef Eigen::MatrixXd MatrixXd; // for point data
|
||||
typedef Eigen::Matrix3d Matrix3d; // for matrices in simplices
|
||||
MatrixXd points_mat;
|
||||
|
||||
// get the ch3
|
||||
|
|
@ -195,11 +199,11 @@ void find_obb(std::vector<Point>& points, std::vector<Point>& obb_points, bool u
|
|||
fill_matrix(points, points_mat);
|
||||
}
|
||||
|
||||
MatrixXd R(3, 3);
|
||||
Matrix3d R;
|
||||
std::size_t max_generations = 100;
|
||||
CGAL::Optimal_bounding_box::evolution(R, points_mat, max_generations);
|
||||
|
||||
MatrixXd obb(8, 3);
|
||||
Eigen::Matrix<double, 8, 3> obb;
|
||||
CGAL::Optimal_bounding_box::post_processing(points_mat, R, obb);
|
||||
|
||||
// matrix -> vector
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@ namespace CGAL {
|
|||
namespace Optimal_bounding_box {
|
||||
|
||||
|
||||
template<typename Vertex>
|
||||
struct Comparator
|
||||
{
|
||||
Comparator(std::vector<double> in) : fitness(in) {}
|
||||
|
|
@ -54,8 +53,8 @@ struct Comparator
|
|||
|
||||
// points: point coords
|
||||
// simplex: 4 roation matrices are its vertices
|
||||
template<typename Matrix>
|
||||
void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_iterations)
|
||||
template<typename Vertex, typename Matrix>
|
||||
void nelder_mead(std::vector<Vertex>& simplex, Matrix& points, std::size_t nb_iterations)
|
||||
{
|
||||
|
||||
CGAL_assertion(simplex.size() == 4); // tetrahedron
|
||||
|
|
@ -68,7 +67,7 @@ void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_it
|
|||
for(std::size_t t = 0; t < nb_iterations; ++t)
|
||||
{
|
||||
|
||||
for(int i = 0; i < 4; ++i)
|
||||
for(std::size_t i = 0; i < 4; ++i)
|
||||
{
|
||||
fitness[i] = compute_fitness(simplex[i], points);
|
||||
}
|
||||
|
|
@ -82,7 +81,7 @@ void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_it
|
|||
|
||||
|
||||
// get indices of sorted sequence
|
||||
Comparator<Matrix> compare_indices(fitness);
|
||||
Comparator compare_indices(fitness);
|
||||
std::sort(indices.begin(), indices.end(), compare_indices);
|
||||
|
||||
|
||||
|
|
@ -98,7 +97,7 @@ void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_it
|
|||
|
||||
|
||||
// new sorted simplex & fitness
|
||||
std::vector<Matrix> s_simplex(4);
|
||||
std::vector<Vertex> s_simplex(4);
|
||||
std::vector<double> s_fitness(4);
|
||||
for(int i = 0; i < 4; ++i)
|
||||
{
|
||||
|
|
@ -110,11 +109,11 @@ void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_it
|
|||
fitness = s_fitness;
|
||||
|
||||
// centroid
|
||||
const Matrix v_centroid = centroid(simplex[0], simplex[1], simplex[2]);
|
||||
const Vertex v_centroid = centroid(simplex[0], simplex[1], simplex[2]);
|
||||
|
||||
// find worst's vertex reflection
|
||||
const Matrix v_worst = simplex[3];
|
||||
const Matrix v_refl = reflection(v_centroid, v_worst);
|
||||
const Vertex v_worst = simplex[3];
|
||||
const Vertex v_refl = reflection(v_centroid, v_worst);
|
||||
const double f_refl = compute_fitness(v_refl, points);
|
||||
|
||||
if(f_refl < fitness[2])
|
||||
|
|
@ -127,7 +126,7 @@ void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_it
|
|||
else
|
||||
{
|
||||
// expansion
|
||||
const Matrix v_expand = expansion(v_centroid, v_worst, v_refl);
|
||||
const Vertex v_expand = expansion(v_centroid, v_worst, v_refl);
|
||||
const double f_expand = compute_fitness(v_expand, points);
|
||||
if(f_expand < f_refl)
|
||||
simplex[3] = v_expand;
|
||||
|
|
@ -137,7 +136,7 @@ void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_it
|
|||
}
|
||||
else // if reflected vertex is not better
|
||||
{
|
||||
const Matrix v_mean = mean(v_centroid, v_worst);
|
||||
const Vertex v_mean = mean(v_centroid, v_worst);
|
||||
const double f_mean = compute_fitness(v_mean, points);
|
||||
if(f_mean <= fitness[3])
|
||||
// contraction of worst
|
||||
|
|
@ -145,9 +144,9 @@ void nelder_mead(std::vector<Matrix>& simplex, Matrix& points, std::size_t nb_it
|
|||
else
|
||||
{
|
||||
// reduction: move all vertices towards the best
|
||||
for(int i=1; i < 4; ++i)
|
||||
for(std::size_t i=1; i < 4; ++i)
|
||||
{
|
||||
simplex[i] = mean(simplex[i], simplex[0]); // todo: test order of addition
|
||||
simplex[i] = mean(simplex[i], simplex[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -172,8 +171,8 @@ struct Random_int_generator
|
|||
};
|
||||
|
||||
|
||||
template <typename Simplex>
|
||||
void genetic_algorithm(Population<Simplex>& pop, Simplex& points)
|
||||
template <typename Simplex, typename Matrix>
|
||||
void genetic_algorithm(Population<Simplex>& pop, Matrix& points)
|
||||
{
|
||||
// random permutations
|
||||
std::size_t m = pop.size();
|
||||
|
|
@ -206,7 +205,6 @@ void genetic_algorithm(Population<Simplex>& pop, Simplex& points)
|
|||
[&rng, &m] ()
|
||||
{ return rng.get_int(0, m); });
|
||||
|
||||
|
||||
Population<Simplex> group1(m/2), group2(m/2);
|
||||
Population<Simplex> group3(m - m/2), group4(m - m/2);
|
||||
|
||||
|
|
|
|||
|
|
@ -24,54 +24,47 @@
|
|||
|
||||
#include <vector>
|
||||
#include <CGAL/Random.h>
|
||||
|
||||
#include <CGAL/Optimal_bounding_box/fitness_function.h>
|
||||
#include <CGAL/Optimal_bounding_box/linear_algebra.h>
|
||||
|
||||
|
||||
namespace CGAL {
|
||||
|
||||
namespace Optimal_bounding_box {
|
||||
|
||||
|
||||
|
||||
template<typename Matrix>
|
||||
class Population
|
||||
{
|
||||
|
||||
typedef std::vector<Matrix> Individual;
|
||||
typedef std::vector<Matrix> Simplex;
|
||||
|
||||
public:
|
||||
Population(std::size_t size) : n(size), random_generator(CGAL::Random(1))
|
||||
Population(std::size_t size) : n(size), random_generator(CGAL::Random())
|
||||
{
|
||||
//std::cout << "seed= " << random_generator.get_seed() << std::endl;
|
||||
|
||||
// reserve pop space
|
||||
pop.reserve(n);
|
||||
|
||||
// create individuals
|
||||
// create simplices
|
||||
for(std::size_t i = 0 ; i < n; ++i)
|
||||
{
|
||||
Individual member(4);
|
||||
create_individual(member);
|
||||
CGAL_assertion(member.size() == 4);
|
||||
pop.push_back(member);
|
||||
Simplex simplex(4);
|
||||
create_simplex(simplex);
|
||||
CGAL_assertion(simplex.size() == 4);
|
||||
pop.push_back(simplex);
|
||||
}
|
||||
}
|
||||
|
||||
void show_population()
|
||||
{
|
||||
std::size_t id = 0;
|
||||
for(const Individual i : pop)
|
||||
for(const Simplex i : pop)
|
||||
{
|
||||
CGAL_assertion(i.size() == 4);
|
||||
std:: cout << "Individual: "<< id++ << std::endl;
|
||||
std:: cout << "Simplex: "<< id++ << std::endl;
|
||||
for(const Matrix R : i)
|
||||
{
|
||||
std::cout << R; // eigen out
|
||||
std::cout << "\n\n";
|
||||
}
|
||||
|
||||
std:: cout << std:: endl;
|
||||
}
|
||||
}
|
||||
|
|
@ -81,15 +74,14 @@ public:
|
|||
return n;
|
||||
}
|
||||
|
||||
|
||||
// access simplex
|
||||
Individual& operator[](std::size_t i)
|
||||
Simplex& operator[](std::size_t i)
|
||||
{
|
||||
CGAL_assertion(i < n);
|
||||
return pop[i];
|
||||
}
|
||||
|
||||
const Individual& operator[](std::size_t i) const
|
||||
const Simplex& operator[](std::size_t i) const
|
||||
{
|
||||
CGAL_assertion(i < n);
|
||||
return pop[i];
|
||||
|
|
@ -98,24 +90,26 @@ public:
|
|||
private:
|
||||
|
||||
// create random population
|
||||
void create_individual(Individual& member)
|
||||
void create_simplex(Simplex& simplex)
|
||||
{
|
||||
CGAL_assertion(member.size() == 4);
|
||||
|
||||
CGAL_assertion(simplex.size() == 4);
|
||||
for(std::size_t i = 0; i < 4; ++i)
|
||||
{
|
||||
// create matrix
|
||||
Matrix R(3,3);
|
||||
create_simplex(R);
|
||||
Matrix Q;
|
||||
qr_factorization(R, Q);
|
||||
member[i] = Q;
|
||||
}
|
||||
Matrix R;
|
||||
// R may be preallocated, if Vertex is Matrix3d,
|
||||
// but Vertex may not, if Vertex is MatrixXd and R is constructed with MatrixXd R(3,3)
|
||||
if(R.cols() == 0 || R.rows() == 0)
|
||||
R.resize(3, 3);
|
||||
|
||||
CGAL_assertion(member.size() == 4);
|
||||
create_vertex(R);
|
||||
Matrix Q; // no allocation
|
||||
qr_factorization(R, Q);
|
||||
simplex[i] = Q;
|
||||
}
|
||||
CGAL_assertion(simplex.size() == 4);
|
||||
}
|
||||
|
||||
void create_simplex(Matrix& R)
|
||||
void create_vertex(Matrix& R)
|
||||
{
|
||||
CGAL_assertion(R.rows() == 3);
|
||||
CGAL_assertion(R.cols() == 3);
|
||||
|
|
@ -131,8 +125,7 @@ private:
|
|||
|
||||
CGAL::Random random_generator;
|
||||
std::size_t n;
|
||||
std::vector<Individual> pop;
|
||||
|
||||
std::vector<Simplex> pop;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,36 +1,26 @@
|
|||
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
|
||||
#include <CGAL/Surface_mesh.h>
|
||||
|
||||
#include <CGAL/Optimal_bounding_box/linear_algebra.h>
|
||||
#include <CGAL/Optimal_bounding_box/fitness_function.h>
|
||||
#include <CGAL/Optimal_bounding_box/population.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <Eigen/Dense>
|
||||
|
||||
//typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
|
||||
//typedef CGAL::Surface_mesh<K::Point_3> Surface_mesh;
|
||||
|
||||
typedef Eigen::MatrixXf MatrixXf;
|
||||
|
||||
typedef Eigen::Matrix3d Matrix3d;
|
||||
|
||||
bool assert_doubles(double d1, double d2, double epsilon)
|
||||
{
|
||||
return (d1 < d2 + epsilon && d1 > d2 - epsilon) ? true : false;
|
||||
}
|
||||
|
||||
|
||||
void test_qr_factorization()
|
||||
{
|
||||
|
||||
MatrixXf A(3, 3);
|
||||
Matrix3d A(3, 3);
|
||||
A << 0.3011944, 0.9932761, 0.5483701,
|
||||
0.5149142, 0.5973263, 0.5162336,
|
||||
0.0039213, 0.0202949, 0.9240308;
|
||||
|
||||
MatrixXf Q;
|
||||
Matrix3d Q;
|
||||
CGAL::Optimal_bounding_box::qr_factorization(A, Q);
|
||||
|
||||
double epsilon = 1e-6;
|
||||
|
|
@ -43,20 +33,17 @@ void test_qr_factorization()
|
|||
CGAL_assertion(assert_doubles(Q(2,0), -0.006573, epsilon));
|
||||
CGAL_assertion(assert_doubles(Q(2,1), 0.024478, epsilon));
|
||||
CGAL_assertion(assert_doubles(Q(2,2), 0.999679, epsilon));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void test_fitness_function()
|
||||
{
|
||||
MatrixXf data_points(4, 3);
|
||||
Eigen::Matrix<double, 4, 3> data_points(4, 3);
|
||||
data_points << 0.866802, 0.740808, 0.895304,
|
||||
0.912651, 0.761565, 0.160330,
|
||||
0.093661, 0.892578, 0.737412,
|
||||
0.166461, 0.149912, 0.364944;
|
||||
|
||||
MatrixXf rotation(3, 3);
|
||||
Matrix3d rotation(3, 3);
|
||||
rotation << -0.809204, 0.124296, 0.574230,
|
||||
-0.574694, 0.035719, -0.817589,
|
||||
-0.122134, -0.991602, 0.042528;
|
||||
|
|
@ -66,20 +53,19 @@ void test_fitness_function()
|
|||
|
||||
}
|
||||
|
||||
|
||||
void test_simplex_operations()
|
||||
{
|
||||
MatrixXf Sc(3, 3);
|
||||
Matrix3d Sc(3, 3);
|
||||
Sc << -0.809204, 0.124296, 0.574230,
|
||||
-0.574694, 0.035719, -0.817589,
|
||||
-0.122134, -0.991602, 0.042528;
|
||||
|
||||
MatrixXf S_worst(3, 3);
|
||||
Matrix3d S_worst(3, 3);
|
||||
S_worst << -0.45070, -0.32769, -0.83035,
|
||||
-0.13619, -0.89406, 0.42675,
|
||||
-0.88222, 0.30543, 0.35833;
|
||||
|
||||
MatrixXf Sr = CGAL::Optimal_bounding_box::reflection(Sc, S_worst);
|
||||
Matrix3d Sr = CGAL::Optimal_bounding_box::reflection(Sc, S_worst);
|
||||
double epsilon = 1e-5;
|
||||
CGAL_assertion(assert_doubles(Sr(0,0), -0.13359, epsilon));
|
||||
CGAL_assertion(assert_doubles(Sr(0,1), -0.95986, epsilon));
|
||||
|
|
@ -91,7 +77,7 @@ void test_simplex_operations()
|
|||
CGAL_assertion(assert_doubles(Sr(2,1), 0.25411, epsilon));
|
||||
CGAL_assertion(assert_doubles(Sr(2,2), -0.56300, epsilon));
|
||||
|
||||
MatrixXf Se = CGAL::Optimal_bounding_box::expansion(Sc, S_worst, Sr);
|
||||
Matrix3d Se = CGAL::Optimal_bounding_box::expansion(Sc, S_worst, Sr);
|
||||
CGAL_assertion(assert_doubles(Se(0,0), -0.87991, epsilon));
|
||||
CGAL_assertion(assert_doubles(Se(0,1), 0.36105, epsilon));
|
||||
CGAL_assertion(assert_doubles(Se(0,2), -0.30888, epsilon));
|
||||
|
|
@ -102,17 +88,17 @@ void test_simplex_operations()
|
|||
CGAL_assertion(assert_doubles(Se(2,1), -0.48595, epsilon));
|
||||
CGAL_assertion(assert_doubles(Se(2,2), 0.74300, epsilon));
|
||||
|
||||
MatrixXf S_a(3, 3);
|
||||
Matrix3d S_a(3, 3);
|
||||
S_a << -0.277970, 0.953559, 0.116010,
|
||||
-0.567497, -0.065576, -0.820760,
|
||||
-0.775035, -0.293982, 0.559370;
|
||||
|
||||
MatrixXf S_b(3, 3);
|
||||
Matrix3d S_b(3, 3);
|
||||
S_b << -0.419979, 0.301765, -0.855894,
|
||||
-0.653011, -0.755415, 0.054087,
|
||||
-0.630234, 0.581624, 0.514314;
|
||||
|
||||
MatrixXf S_c = CGAL::Optimal_bounding_box::mean(S_a, S_b);
|
||||
Matrix3d S_c = CGAL::Optimal_bounding_box::mean(S_a, S_b);
|
||||
CGAL_assertion(assert_doubles(S_c(0,0), -0.35111, epsilon));
|
||||
CGAL_assertion(assert_doubles(S_c(0,1), 0.79308, epsilon));
|
||||
CGAL_assertion(assert_doubles(S_c(0,2), -0.49774, epsilon));
|
||||
|
|
@ -122,28 +108,26 @@ void test_simplex_operations()
|
|||
CGAL_assertion(assert_doubles(S_c(2,0), -0.70693, epsilon));
|
||||
CGAL_assertion(assert_doubles(S_c(2,1), 0.12405, epsilon));
|
||||
CGAL_assertion(assert_doubles(S_c(2,2), 0.69632, epsilon));
|
||||
|
||||
}
|
||||
|
||||
void test_centroid()
|
||||
{
|
||||
MatrixXf S_a(3, 3);
|
||||
Matrix3d S_a(3, 3);
|
||||
S_a << -0.588443, 0.807140, -0.047542,
|
||||
-0.786228, -0.584933, -0.199246,
|
||||
-0.188629, -0.079867, 0.978795;
|
||||
|
||||
MatrixXf S_b(3, 3);
|
||||
Matrix3d S_b(3, 3);
|
||||
S_b << -0.2192721, 0.2792986, -0.9348326,
|
||||
-0.7772152, -0.6292092, -0.0056861,
|
||||
-0.5897934, 0.7253193, 0.3550431;
|
||||
|
||||
MatrixXf S_c(3, 3);
|
||||
Matrix3d S_c(3, 3);
|
||||
S_c << -0.32657, -0.60013, -0.73020,
|
||||
-0.20022, -0.71110, 0.67398,
|
||||
-0.92372, 0.36630, 0.11207;
|
||||
|
||||
|
||||
MatrixXf S_centroid = CGAL::Optimal_bounding_box::centroid(S_a, S_b, S_c);
|
||||
Matrix3d S_centroid = CGAL::Optimal_bounding_box::centroid(S_a, S_b, S_c);
|
||||
double epsilon = 1e-5;
|
||||
CGAL_assertion(assert_doubles(S_centroid(0,0), -0.419979, epsilon));
|
||||
CGAL_assertion(assert_doubles(S_centroid(0,1), 0.301765, epsilon));
|
||||
|
|
@ -154,23 +138,14 @@ void test_centroid()
|
|||
CGAL_assertion(assert_doubles(S_centroid(2,0), -0.630234, epsilon));
|
||||
CGAL_assertion(assert_doubles(S_centroid(2,1), 0.581624, epsilon));
|
||||
CGAL_assertion(assert_doubles(S_centroid(2,2), 0.514314, epsilon));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
test_qr_factorization();
|
||||
test_fitness_function();
|
||||
test_simplex_operations();
|
||||
test_centroid();
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,15 @@
|
|||
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
|
||||
#include <CGAL/Surface_mesh.h>
|
||||
|
||||
#include <CGAL/Optimal_bounding_box/optimization_algorithms.h>
|
||||
#include <CGAL/Optimal_bounding_box/population.h>
|
||||
#include <CGAL/Optimal_bounding_box/obb.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <Eigen/Dense>
|
||||
|
||||
|
||||
typedef Eigen::MatrixXd MatrixXd;
|
||||
typedef Eigen::Matrix3d Matrix3d;
|
||||
|
||||
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
|
||||
|
||||
|
|
@ -62,38 +60,38 @@ double calculate_volume(std::vector<Point> points)
|
|||
|
||||
void test_population()
|
||||
{
|
||||
CGAL::Optimal_bounding_box::Population<MatrixXd> pop(5);
|
||||
CGAL::Optimal_bounding_box::Population<Matrix3d> pop(5);
|
||||
//pop.show_population();
|
||||
CGAL_assertion(pop.size() == 5);
|
||||
}
|
||||
|
||||
void test_nelder_mead()
|
||||
{
|
||||
MatrixXd data_points(4, 3);
|
||||
Eigen::Matrix<double, 4, 3> data_points(4, 3);
|
||||
data_points << 0.866802, 0.740808, 0.895304,
|
||||
0.912651, 0.761565, 0.160330,
|
||||
0.093661, 0.892578, 0.737412,
|
||||
0.166461, 0.149912, 0.364944;
|
||||
|
||||
// one simplex
|
||||
std::vector<MatrixXd> simplex(4);
|
||||
std::vector<Matrix3d> simplex(4);
|
||||
|
||||
MatrixXd v0(3, 3);
|
||||
Matrix3d v0;
|
||||
v0 << -0.2192721, 0.2792986, -0.9348326,
|
||||
-0.7772152, -0.6292092, -0.0056861,
|
||||
-0.5897934, 0.7253193, 0.3550431;
|
||||
|
||||
MatrixXd v1(3, 3);
|
||||
Matrix3d v1;
|
||||
v1 << -0.588443, 0.807140, -0.047542,
|
||||
-0.786228, -0.584933, -0.199246,
|
||||
-0.188629, -0.079867, 0.978795;
|
||||
|
||||
MatrixXd v2(3, 3);
|
||||
Matrix3d v2;
|
||||
v2 << -0.277970, 0.953559, 0.116010,
|
||||
-0.567497, -0.065576, -0.820760,
|
||||
-0.775035, -0.293982, 0.559370;
|
||||
|
||||
MatrixXd v3(3, 3);
|
||||
Matrix3d v3;
|
||||
v3 << -0.32657, -0.60013, -0.73020,
|
||||
-0.20022, -0.71110, 0.67398,
|
||||
-0.92372, 0.36630, 0.11207;
|
||||
|
|
@ -108,7 +106,7 @@ void test_nelder_mead()
|
|||
|
||||
double epsilon = 1e-5;
|
||||
|
||||
MatrixXd v0_new = simplex[0];
|
||||
Matrix3d v0_new = simplex[0];
|
||||
CGAL_assertion(assert_doubles(v0_new(0,0), -0.288975, epsilon));
|
||||
CGAL_assertion(assert_doubles(v0_new(0,1), 0.7897657, epsilon));
|
||||
CGAL_assertion(assert_doubles(v0_new(0,2), -0.541076, epsilon));
|
||||
|
|
@ -119,7 +117,7 @@ void test_nelder_mead()
|
|||
CGAL_assertion(assert_doubles(v0_new(2,1), 0.5111260, epsilon));
|
||||
CGAL_assertion(assert_doubles(v0_new(2,2), 0.84094, epsilon));
|
||||
|
||||
MatrixXd v1_new = simplex[1];
|
||||
Matrix3d v1_new = simplex[1];
|
||||
CGAL_assertion(assert_doubles(v1_new(0,0), -0.458749, epsilon));
|
||||
CGAL_assertion(assert_doubles(v1_new(0,1), 0.823283, epsilon));
|
||||
CGAL_assertion(assert_doubles(v1_new(0,2), -0.334296, epsilon));
|
||||
|
|
@ -130,7 +128,7 @@ void test_nelder_mead()
|
|||
CGAL_assertion(assert_doubles(v1_new(2,1), 0.338040, epsilon));
|
||||
CGAL_assertion(assert_doubles(v1_new(2,2), 0.937987, epsilon));
|
||||
|
||||
MatrixXd v2_new = simplex[2];
|
||||
Matrix3d v2_new = simplex[2];
|
||||
CGAL_assertion(assert_doubles(v2_new(0,0), -0.346582, epsilon));
|
||||
CGAL_assertion(assert_doubles(v2_new(0,1), 0.878534, epsilon));
|
||||
CGAL_assertion(assert_doubles(v2_new(0,2), -0.328724, epsilon));
|
||||
|
|
@ -141,7 +139,7 @@ void test_nelder_mead()
|
|||
CGAL_assertion(assert_doubles(v2_new(2,1), 0.334057, epsilon));
|
||||
CGAL_assertion(assert_doubles(v2_new(2,2), 0.941423, epsilon));
|
||||
|
||||
MatrixXd v3_new = simplex[3];
|
||||
Matrix3d v3_new = simplex[3];
|
||||
CGAL_assertion(assert_doubles(v3_new(0,0), -0.394713, epsilon));
|
||||
CGAL_assertion(assert_doubles(v3_new(0,1), 0.791782, epsilon));
|
||||
CGAL_assertion(assert_doubles(v3_new(0,2), -0.466136, epsilon));
|
||||
|
|
@ -155,20 +153,20 @@ void test_nelder_mead()
|
|||
|
||||
void test_genetic_algorithm()
|
||||
{
|
||||
MatrixXd data_points(4, 3);
|
||||
Eigen::Matrix<double, 4, 3> data_points(4, 3);
|
||||
data_points << 0.866802, 0.740808, 0.895304,
|
||||
0.912651, 0.761565, 0.160330,
|
||||
0.093661, 0.892578, 0.737412,
|
||||
0.166461, 0.149912, 0.364944;
|
||||
|
||||
CGAL::Optimal_bounding_box::Population<MatrixXd> pop(5);
|
||||
CGAL::Optimal_bounding_box::Population<Matrix3d> pop(5);
|
||||
CGAL::Optimal_bounding_box::genetic_algorithm(pop, data_points);
|
||||
CGAL_assertion(pop.size() == 5);
|
||||
}
|
||||
|
||||
void test_random_unit_tetra()
|
||||
{
|
||||
MatrixXd data_points(4, 3); // points on their convex hull
|
||||
Eigen::Matrix<double, 4, 3> data_points(4, 3); // points on their convex hull
|
||||
data_points << 0.866802, 0.740808, 0.895304,
|
||||
0.912651, 0.761565, 0.160330,
|
||||
0.093661, 0.892578, 0.737412,
|
||||
|
|
@ -192,7 +190,7 @@ void test_random_unit_tetra()
|
|||
out.close();
|
||||
#endif
|
||||
|
||||
MatrixXd R(3, 3);
|
||||
Matrix3d R;
|
||||
std::size_t generations = 10;
|
||||
CGAL::Optimal_bounding_box::evolution(R, data_points, generations);
|
||||
|
||||
|
|
@ -229,7 +227,7 @@ void test_reference_tetrahedron(const char* fname)
|
|||
MatrixXd points;
|
||||
sm_to_matrix(mesh, points);
|
||||
|
||||
MatrixXd R(3, 3);
|
||||
Matrix3d R(3, 3);
|
||||
std::size_t generations = 10;
|
||||
CGAL::Optimal_bounding_box::evolution(R, points, generations);
|
||||
double epsilon = 1e-5;
|
||||
|
|
@ -256,7 +254,7 @@ void test_long_tetrahedron(std::string fname)
|
|||
MatrixXd points;
|
||||
sm_to_matrix(mesh, points);
|
||||
|
||||
MatrixXd R(3, 3);
|
||||
MatrixXd R(3, 3); // test with dynamic size
|
||||
std::size_t generations = 10;
|
||||
CGAL::Optimal_bounding_box::evolution(R, points, generations);
|
||||
double epsilon = 1e-3;
|
||||
|
|
@ -348,7 +346,6 @@ bench(const char* fname)
|
|||
{
|
||||
std::vector<K::Point_3> sm_points, obb_points;
|
||||
std::ifstream in(fname);
|
||||
//double x,y,z;
|
||||
K::Point_3 p;
|
||||
int i = 0;
|
||||
while(in >> p){
|
||||
|
|
@ -365,20 +362,20 @@ bench(const char* fname)
|
|||
std::cout << "number of points= " << sm_points.size() << std::endl;
|
||||
|
||||
|
||||
std::cout.precision(17);
|
||||
CGAL::Optimal_bounding_box::find_obb(sm_points, obb_points, true);
|
||||
|
||||
/*
|
||||
/*
|
||||
std::cout.precision(17);
|
||||
for(int i =0; i < obb_points.size(); i ++){
|
||||
std::cout << obb_points[i] << std::endl;
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
||||
CGAL::Surface_mesh<K::Point_3> mesh;
|
||||
CGAL::make_hexahedron(obb_points[0], obb_points[1], obb_points[2], obb_points[3], obb_points[4], obb_points[5],
|
||||
obb_points[6], obb_points[7], mesh);
|
||||
|
||||
std::ofstream out("/tmp/octopus_result.off");
|
||||
std::ofstream out("/tmp/result_obb.off");
|
||||
out << mesh;
|
||||
out.close();
|
||||
|
||||
|
|
@ -388,6 +385,7 @@ bench(const char* fname)
|
|||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
/*
|
||||
test_population();
|
||||
test_nelder_mead();
|
||||
|
|
@ -398,6 +396,7 @@ int main(int argc, char* argv[])
|
|||
test_find_obb("data/random_unit_tetra.off");
|
||||
*/
|
||||
|
||||
|
||||
bench(argv[1]);
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
#include <QtCore/qglobal.h>
|
||||
|
||||
#include <CGAL/Three/Scene_item.h>
|
||||
#include <CGAL/Three/Scene_interface.h>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue