Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlignT.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 #include <Eigen/Dense>
44 
45 namespace align{
46 
47 
48 template< class MeshT >
49 void moveToCOG(MeshT& _mesh) {
50 
51  ACG::Vec3d mean(0.0);
52  for (typename MeshT::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it) {
53  mean += _mesh.point(*v_it);
54  }
55 
56  mean /= (double)_mesh.n_vertices();
57 
58  for (TriMesh::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it) {
59  _mesh.set_point(*v_it, _mesh.point(*v_it) - mean);
60  }
61 }
62 
63 
64 template< class MeshT >
65 void rotate(MeshT& _mesh) {
66  using namespace Eigen;
67 
68  Matrix3Xd data = Matrix3Xd::Zero(3, _mesh.n_vertices());
69  size_t i(0);
70  for (typename MeshT::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it, ++i) {
71  const ACG::Vec3d tmp = _mesh.point(*v_it);
72  data.col(i) = Vector3d(tmp[0], tmp[1], tmp[2]);
73  }
74 
75  Matrix3d covar = (data * data.transpose()) / (double)_mesh.n_vertices();
76 
77  JacobiSVD<Matrix3d> svd(covar, ComputeFullU | ComputeFullV);
78 
79  const Matrix3d& u = svd.matrixU();
80 
81  Eigen::Vector3d v0 = u.col(0);
82  Eigen::Vector3d v1 = u.col(1);
83  Eigen::Vector3d v2 = v0.cross(v1);
84 
85  v0.normalize();
86  v1.normalize();
87  v2.normalize();
88 
89  Matrix3d trans;
90  trans.col(0) = v0;
91  trans.col(1) = v1;
92  trans.col(2) = v2;
93 
94  Matrix3d invTrans = trans.inverse();
95 
96  ACG::Matrix4x4d mat;
97  mat.identity();
98  for (i = 0; i < 3; ++i) {
99  for (size_t j = 0; j < 3; ++j) {
100  mat(i,j) = invTrans(i,j);
101  }
102  }
103 
104  for (typename MeshT::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it, ++i) {
105  const ACG::Vec4d tmp(_mesh.point(*v_it)[0], _mesh.point(*v_it)[1], _mesh.point(*v_it)[2], 1.0);
106  const ACG::Vec4d res = mat * tmp;
107 
108  _mesh.set_point(*v_it, ACG::Vec3d(res[0], res[1],res[2]));
109  }
110 }
111 
112 template< class MeshT >
113 void moveCenterOfBBToOrigin(MeshT& _mesh) {
114 
115  ACG::Vec3d min(DBL_MAX);
116  ACG::Vec3d max(-DBL_MAX);
117  for (typename MeshT::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it) {
118  min.minimize(_mesh.point(*v_it));
119  max.maximize(_mesh.point(*v_it));
120  }
121 
122  const ACG::Vec3d diag = max - min;
123  const ACG::Vec3d center = min + 0.5*diag;
124 
125  for (typename MeshT::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it) {
126  _mesh.point(*v_it) -= center;
127  }
128 }
129 
130 } // namespace align
131 
132 #define ALIGNT_CC
void rotate(const ACG::Vec3d &_axis, const double _angle, const ACG::Vec3d &_center, int _viewer)
Rotate Scene around axis.
void identity()
setup an identity matrix
Definition: Matrix4x4T.cc:256
Definition: AlignT.cc:45