PCA demo: fit plane to vertices, edges and facets.

This commit is contained in:
Pierre Alliez 2009-07-31 20:42:39 +00:00
parent 17e6fbfae2
commit c9b1327a19
13 changed files with 173 additions and 2297 deletions

8
.gitattributes vendored
View File

@ -2979,13 +2979,9 @@ Polytope_distance_d/test/Polytope_distance_d/zwick_bug.cpp -text
Principal_component_analysis/demo/Principal_component_analysis/MainWindow.ui -text
Principal_component_analysis/demo/Principal_component_analysis/PCA_demo.qrc -text
Principal_component_analysis/demo/Principal_component_analysis/cleanup.bat eol=crlf
Principal_component_analysis/demo/Principal_component_analysis/data/cylinder_iso1_locally_refined.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/cylinder_iso2.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/cylinder.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/cylinder_locally_refined.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/ellipsoid.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/mushroom-19v.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/mushroom-651v.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/mushroom-81v.off -text
Principal_component_analysis/demo/Principal_component_analysis/data/mushroom.off -text
Principal_component_analysis/demo/Principal_component_analysis/resources/about.html svneol=native#text/html
Principal_component_analysis/doc_tex/Principal_component_analysis/examples.tex -text
Principal_component_analysis/doc_tex/Principal_component_analysis/fit.eps -text svneol=unset#application/postscript

View File

@ -182,3 +182,27 @@ void MainWindow::on_actionCopy_snapshot_triggered()
qb->setImage(snapshot);
QApplication::restoreOverrideCursor();
}
void MainWindow::on_actionFit_triangles_triggered()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
m_pScene->fit_triangles();
m_pViewer->update();
QApplication::restoreOverrideCursor();
}
void MainWindow::on_actionFit_edges_triggered()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
m_pScene->fit_edges();
m_pViewer->update();
QApplication::restoreOverrideCursor();
}
void MainWindow::on_actionFit_vertices_triggered()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
m_pScene->fit_vertices();
m_pViewer->update();
QApplication::restoreOverrideCursor();
}

View File

@ -47,7 +47,9 @@ public:
// algorithm menu
void on_actionRefine_loop_triggered();
void on_actionFit_triangles_triggered();
void on_actionFit_edges_triggered();
void on_actionFit_vertices_triggered();
// view menu
void on_actionView_polyhedron_triggered();

View File

@ -10,7 +10,7 @@
</rect>
</property>
<property name="windowTitle" >
<string>CGAL - PCA tree demo</string>
<string>CGAL - PCA demo</string>
</property>
<property name="windowIcon" >
<iconset resource="PCA_demo.qrc" >
@ -57,14 +57,11 @@
<property name="title" >
<string>Algorithms</string>
</property>
<widget class="QMenu" name="menuRefine" >
<property name="title" >
<string>Refine</string>
</property>
<addaction name="actionRefine_bisection" />
<addaction name="actionRefine_loop" />
</widget>
<addaction name="menuRefine" />
<addaction name="actionFit_edges" />
<addaction name="actionFit_vertices" />
<addaction name="actionFit_triangles" />
<addaction name="separator" />
<addaction name="actionRefine_loop" />
</widget>
<widget class="QMenu" name="menuEdit" >
<property name="title" >
@ -76,7 +73,6 @@
<addaction name="menuFile" />
<addaction name="menuEdit" />
<addaction name="menuAlgorithms" />
<addaction name="menuBenchmarks" />
<addaction name="menuView" />
</widget>
<action name="actionQuit" >
@ -225,6 +221,21 @@
<string>Loop subdivision</string>
</property>
</action>
<action name="actionFit_vertices" >
<property name="text" >
<string>Fit vertices</string>
</property>
</action>
<action name="actionFit_edges" >
<property name="text" >
<string>Fit edges</string>
</property>
</action>
<action name="actionFit_triangles" >
<property name="text" >
<string>Fit triangles</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -12,6 +12,10 @@
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Subdivision_method_3.h>
#include <CGAL/centroid.h>
#include <CGAL/linear_least_squares_fitting_3.h>
#include "render_edges.h"
Scene::Scene()
@ -93,6 +97,26 @@ void Scene::draw()
{
if(m_view_polyhedron)
draw_polyhedron();
// draw plane
::glColor3ub(255,0,0);
::glBegin(GL_QUADS);
Point o = m_plane.projection(m_centroid);
Point a = o + normalize(m_plane.base1()) + normalize(m_plane.base2());
Point b = o + normalize(m_plane.base1()) - normalize(m_plane.base2());
Point c = o - normalize(m_plane.base1()) - normalize(m_plane.base2());
Point d = o - normalize(m_plane.base1()) + normalize(m_plane.base2());
::glVertex3d(a.x(),a.y(),a.z());
::glVertex3d(b.x(),b.y(),b.z());
::glVertex3d(c.x(),c.y(),c.z());
::glVertex3d(d.x(),d.y(),d.z());
::glEnd();
}
Vector Scene::normalize(const Vector& v)
{
return v / std::sqrt(v*v);
}
void Scene::draw_polyhedron()
@ -124,3 +148,82 @@ void Scene::toggle_view_poyhedron()
m_view_polyhedron = !m_view_polyhedron;
}
void Scene::fit_triangles()
{
std::cout << "Fit triangles...";
std::list<Triangle> triangles;
Polyhedron::Facet_iterator it;
for(it = m_pPolyhedron->facets_begin();
it != m_pPolyhedron->facets_end();
it++)
{
Polyhedron::Halfedge_handle he = it->halfedge();
const Point& a = he->vertex()->point();
const Point& b = he->next()->vertex()->point();
const Point& c = he->next()->next()->vertex()->point();
Triangle triangle(a,b,c);
triangles.push_back(triangle);
}
m_centroid = CGAL::centroid(triangles.begin(),triangles.end());
CGAL::linear_least_squares_fitting_3(triangles.begin(),
triangles.end(), m_line, CGAL::Dimension_tag<2>());
CGAL::linear_least_squares_fitting_3(triangles.begin(),
triangles.end(), m_plane, CGAL::Dimension_tag<2>());
std::cout << "done" << std::endl;
}
void Scene::fit_edges()
{
std::cout << "Fit edges...";
std::list<Segment> segments;
Polyhedron::Edge_iterator he;
for(he = m_pPolyhedron->edges_begin();
he != m_pPolyhedron->edges_end();
he++)
{
const Point& a = he->vertex()->point();
const Point& b = he->opposite()->vertex()->point();
Segment segment(a,b);
segments.push_back(segment);
}
m_centroid = CGAL::centroid(segments.begin(),segments.end());
CGAL::linear_least_squares_fitting_3(segments.begin(),
segments.end(), m_line, CGAL::Dimension_tag<1>());
CGAL::linear_least_squares_fitting_3(segments.begin(),
segments.end(), m_plane, CGAL::Dimension_tag<1>());
std::cout << "done" << std::endl;
}
void Scene::fit_vertices()
{
std::cout << "Fit vertices...";
std::list<Point> points;
Polyhedron::Vertex_iterator v;
for(v = m_pPolyhedron->vertices_begin();
v != m_pPolyhedron->vertices_end();
v++)
{
const Point& p = v->point();
points.push_back(p);
}
m_centroid = CGAL::centroid(points.begin(),points.end());
CGAL::linear_least_squares_fitting_3(points.begin(),
points.end(), m_line, CGAL::Dimension_tag<0>());
CGAL::linear_least_squares_fitting_3(points.begin(),
points.end(), m_plane, CGAL::Dimension_tag<0>());
std::cout << "done" << std::endl;
}

View File

@ -7,7 +7,6 @@
#include "types.h"
class Scene
{
public:
@ -25,9 +24,13 @@ public:
private:
// member data
Bbox m_bbox;
Line m_line;
Plane m_plane;
Point m_centroid;
Polyhedron *m_pPolyhedron;
private:
// view options
bool m_view_polyhedron;
public:
// file menu
@ -36,14 +39,22 @@ public:
// toggle view options
void toggle_view_poyhedron();
// view options
bool m_view_polyhedron;
// algorithms
Vector normalize(const Vector& v);
// refinement
void refine_loop();
void fit_edges();
void fit_vertices();
void fit_triangles();
// drawing
// rendering
void draw_polyhedron();
private:
}; // end class Scene

View File

@ -1,54 +0,0 @@
OFF
19 31 0
0.454199 0.0916024 -2.10744e-018
-5e-008 0.0916024 -0.454199
-5e-008 1 -2.10744e-018
-5e-008 0.0916024 0.454199
-0.454199 0.0916024 -2.10744e-018
0.0749192 0.0916024 -0.230578
-0.196141 0.0916024 -0.142505
-0.196141 0.0916024 0.142505
0.0749192 0.0916024 0.230578
0.242444 0.0916024 -2.10744e-018
0.0749192 -0.30407 -0.230578
0.242444 -0.30407 -2.10744e-018
0.0749192 -0.30407 0.230578
-0.196141 -0.30407 0.142505
-0.196141 -0.30407 -0.142505
0.227099 0.545801 -2.10744e-018
-5e-008 0.545801 0.2271
-5e-008 0.545801 -0.2271
-0.2271 0.545801 -2.10744e-018
3 15 1 17
3 15 2 16
3 16 2 18
3 17 1 4
3 4 7 3
3 7 8 3
3 3 8 9
3 0 9 5
3 7 4 6
3 4 1 6
3 5 6 1
3 5 9 11
3 9 8 12
3 8 7 13
3 7 6 14
3 6 5 10
3 9 0 3
3 5 1 0
3 11 10 5
3 12 11 9
3 13 12 8
3 14 13 7
3 10 14 6
3 15 0 1
3 3 0 15
3 16 3 15
3 4 3 16
3 17 2 15
3 18 2 17
3 18 4 16
3 17 4 18

View File

@ -1,240 +0,0 @@
OFF
81 155 0
0.454199 0.0916024 -2.10744e-018
-5e-008 0.0916024 -0.454199
-5e-008 1 -2.10744e-018
-5e-008 0.0916024 0.454199
-0.454199 0.0916024 -2.10744e-018
0.0749192 0.0916024 -0.230578
-0.196141 0.0916024 -0.142505
-0.196141 0.0916024 0.142505
0.0749192 0.0916024 0.230578
0.242444 0.0916024 -2.10744e-018
0.0749192 -0.30407 -0.230578
0.242444 -0.30407 -2.10744e-018
0.0749192 -0.30407 0.230578
-0.196141 -0.30407 0.142505
-0.196141 -0.30407 -0.142505
0.227099 0.545801 -2.10744e-018
-5e-008 0.545801 0.2271
-5e-008 0.545801 -0.2271
-0.2271 0.545801 -2.10744e-018
0.11355 0.318702 -0.2271
0.11355 0.318702 0.2271
-0.2271 0.318702 0.11355
-0.2271 0.318702 -0.11355
0.227099 0.0916024 -0.2271
0.227099 0.0916024 0.2271
-0.2271 0.0916024 0.2271
-0.2271 0.0916024 -0.2271
0.121222 0.0916024 0.2271
0.11355 0.772901 -2.10744e-018
0.340649 0.318702 -2.10744e-018
-5e-008 0.318702 -0.340649
-5e-008 0.772901 0.11355
-5e-008 0.318702 0.340649
-5e-008 0.772901 -0.11355
-0.340649 0.318702 -2.10744e-018
-0.11355 0.772901 -2.10744e-018
0.158682 -0.106234 -0.115289
0.158682 -0.106234 0.115289
-0.196141 -0.106234 0
-0.0606109 -0.106234 -0.186542
-0.0606109 -0.106234 0.186542
0.283874 0.205152 0.11355
0.283874 0.205152 -0.11355
-0.11355 0.205152 -0.283874
-0.11355 0.205152 0.283874
0.264559 0.0916024 -0.115289
0.0749192 -0.106234 0.230578
0.242444 -0.106234 -2.10744e-018
0.0749192 -0.106234 -0.230578
-0.196141 -0.106234 -0.142505
-0.196141 -0.106234 0.142505
-0.0980705 0.0916024 0.298352
-0.0980705 0.0916024 -0.298352
0.0567748 0.659351 0.11355
0.0567748 0.659351 -0.11355
0.170325 0.432252 0.11355
0.0567748 0.205152 0.340649
0.0567748 0.205152 -0.340649
0.170325 0.432252 -0.11355
-0.11355 0.659351 -0.0567749
-0.11355 0.659351 0.0567749
-0.340649 0.205152 -0.0567749
-0.340649 0.205152 0.0567749
-0.11355 0.432252 -0.170325
-0.11355 0.432252 0.170325
0.11355 0.545801 -0.11355
0.227099 0.318702 0.11355
0.11355 0.545801 0.11355
0.11355 0.0916024 0.340649
0.11355 0.0916024 -0.340649
0.227099 0.318702 -0.11355
0.340649 0.0916024 0.11355
0.340649 0.0916024 -0.11355
-0.340649 0.0916024 -0.11355
-0.340649 0.0916024 0.11355
-0.11355 0.545801 -0.11355
-0.11355 0.318702 0.2271
-0.11355 0.0916024 -0.340649
-0.11355 0.0916024 0.340649
-0.11355 0.318702 -0.2271
-0.11355 0.545801 0.11355
3 58 17 65
3 15 28 53
3 16 31 60
3 26 22 43
3 74 4 7
3 51 7 8
3 8 9 27
3 9 5 45
3 7 4 6
3 77 1 52
3 52 1 5
3 36 9 47
3 37 8 46
3 40 7 50
3 38 6 49
3 39 5 48
3 27 9 24
3 45 5 23
3 36 11 10
3 37 12 11
3 40 13 12
3 38 14 13
3 39 10 14
3 58 15 29
3 56 3 68
3 20 16 32
3 62 4 74
3 65 17 54
3 59 17 75
3 21 18 34
3 34 18 22
3 30 17 19
3 42 0 72
3 16 20 55
3 55 20 66
3 18 21 64
3 64 21 76
3 63 30 79
3 63 22 18
3 57 19 23
3 69 23 5
3 41 24 71
3 71 24 9
3 44 25 78
3 25 7 51
3 61 26 73
3 73 26 6
3 27 3 8
3 68 3 27
3 53 28 31
3 54 17 33
3 29 0 42
3 41 0 29
3 79 30 43
3 30 19 57
3 60 31 35
3 28 2 31
3 32 3 56
3 44 3 32
3 33 2 28
3 59 18 35
3 34 4 62
3 61 4 34
3 35 2 33
3 31 2 35
3 36 5 9
3 48 5 36
3 37 9 8
3 47 9 37
3 38 7 6
3 50 7 38
3 39 6 5
3 49 6 39
3 40 8 7
3 46 8 40
3 41 20 24
3 66 20 41
3 70 29 42
3 23 19 42
3 43 1 77
3 30 1 43
3 44 21 25
3 76 21 44
3 45 0 9
3 72 0 45
3 46 12 37
3 40 12 46
3 47 11 36
3 37 11 47
3 48 10 39
3 36 10 48
3 49 14 38
3 39 14 49
3 50 13 40
3 38 13 50
3 78 25 51
3 8 3 51
3 52 6 26
3 5 6 52
3 53 16 67
3 31 16 53
3 54 28 15
3 33 28 54
3 67 16 55
3 29 15 55
3 56 20 32
3 24 20 56
3 57 1 30
3 57 23 69
3 58 19 17
3 58 29 70
3 59 33 17
3 35 33 59
3 60 18 80
3 35 18 60
3 61 22 26
3 34 22 61
3 62 21 34
3 25 21 62
3 63 17 30
3 75 17 63
3 80 18 64
3 32 16 64
3 65 15 58
3 54 15 65
3 66 29 55
3 41 29 66
3 67 15 53
3 55 15 67
3 68 24 56
3 27 24 68
3 69 1 57
3 5 1 69
3 70 19 58
3 42 19 70
3 71 0 41
3 9 0 71
3 72 23 42
3 45 23 72
3 73 4 61
3 6 4 73
3 74 25 62
3 7 25 74
3 75 18 59
3 63 18 75
3 76 32 64
3 44 32 76
3 77 26 43
3 52 26 77
3 78 3 44
3 51 3 78
3 79 22 63
3 43 22 79
3 80 16 60
3 64 16 80

View File

@ -1,40 +0,0 @@
OFF
15 23 37
0.454199 0.0916024 -2.10744e-018
-5e-008 0.0916024 -0.454199
-5e-008 1 -2.10744e-018
-5e-008 0.0916024 0.454199
-0.454199 0.0916024 -2.10744e-018
0.0749192 0.0916024 -0.230578
-0.196141 0.0916024 -0.142505
-0.196141 0.0916024 0.142505
0.0749192 0.0916024 0.230578
0.242444 0.0916024 -2.10744e-018
0.0749192 -0.30407 -0.230578
0.242444 -0.30407 -2.10744e-018
0.0749192 -0.30407 0.230578
-0.196141 -0.30407 0.142505
-0.196141 -0.30407 -0.142505
3 0 1 2
3 3 0 2
3 4 3 2
3 1 4 2
3 4 7 3
3 7 8 3
3 3 8 9
3 0 9 5
3 7 4 6
3 4 1 6
3 5 6 1
3 5 9 11
3 9 8 12
3 8 7 13
3 7 6 14
3 6 5 10
3 9 0 3
3 5 1 0
3 11 10 5
3 12 11 9
3 13 12 8
3 14 13 7
3 10 14 6

View File

@ -2,19 +2,17 @@
#define PCA_DEMO_TYPES_H
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <boost/iterator/transform_iterator.hpp>
typedef CGAL::Simple_cartesian<double> Kernel; // fastest in experiments
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::FT FT;
typedef Kernel::Ray_3 Ray;
typedef Kernel::Line_3 Line;
typedef Kernel::Point_3 Point;
typedef Kernel::Plane_3 Plane;
typedef Kernel::Vector_3 Vector;
typedef Kernel::Segment_3 Segment;
typedef Kernel::Triangle_3 Triangle;
#include <CGAL/Polyhedron_3.h>
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
#endif // PCA_DEMO_TYPES_H