trimesh_inertia.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004-2012                                           \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00033 #include<vcg/complex/complex.h>
00034 
00035 #include<wrap/io_trimesh/import_off.h>
00036 
00037 #include<vcg/complex/algorithms/inertia.h>
00038 #include<vcg/complex/algorithms/create/platonic.h>
00039 
00040 class MyEdge;
00041 class MyFace;
00042 class MyVertex;
00043 struct MyUsedTypes : public vcg::UsedTypes<     vcg::Use<MyVertex>   ::AsVertexType,
00044                                             vcg::Use<MyEdge>     ::AsEdgeType,
00045                                             vcg::Use<MyFace>     ::AsFaceType>{};
00046 
00047 class MyVertex  : public vcg::Vertex<MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags  >{};
00048 class MyFace    : public vcg::Face< MyUsedTypes, vcg::face::FFAdj, vcg::face::Normal3f, vcg::face::VertexRef, vcg::face::BitFlags > {};
00049 class MyEdge    : public vcg::Edge<MyUsedTypes>{};
00050 class MyMesh    : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge>  > {};
00051 
00052 int main( int argc, char **argv )
00053 {
00054   MyMesh boxMesh,torusMesh;
00055   vcg::Matrix33f IT;
00056   vcg::Point3f ITv;
00057 
00058   vcg::tri::Hexahedron(boxMesh);
00059   vcg::Matrix44f ScaleM,TransM;
00060   ScaleM.SetScale(1.0f, 2.0f, 5.0f);
00061   TransM.SetTranslate(2.0f,3.0f,4.0f);
00062   vcg::tri::UpdatePosition<MyMesh>::Matrix(boxMesh,ScaleM);
00063   vcg::tri::UpdatePosition<MyMesh>::Matrix(boxMesh,TransM);
00064   vcg::tri::Inertia<MyMesh> Ib(boxMesh);
00065   vcg::Point3f cc = Ib.CenterOfMass();
00066   Ib.InertiaTensorEigen(IT,ITv);
00067 
00068   printf("Box of size 2,4,10, centered in (2,3,4)\n");
00069   printf("Volume %f \n",Ib.Mass());
00070   printf("CenterOfMass %f %f %f\n",cc[0],cc[1],cc[2]);
00071   printf("InertiaTensor Values  %6.3f %6.3f %6.3f\n",ITv[0],ITv[1],ITv[2]);
00072   printf("InertiaTensor Matrix\n");
00073 
00074   printf(" %6.3f %6.3f %6.3f\n",IT[0][0],IT[0][1],IT[0][2]);
00075   printf(" %6.3f %6.3f %6.3f\n",IT[1][0],IT[1][1],IT[1][2]);
00076   printf(" %6.3f %6.3f %6.3f\n",IT[2][0],IT[2][1],IT[2][2]);
00077 
00078   // Now we have a box with sides (h,w,d) 2,4,10, centered in (2,3,4)
00079   // Volume is 80
00080   // inertia tensor should be:
00081   // I_h = 1/12 m *(w^2+d^2)  = 1/12 * 80 * (16+100) = 773.33
00082   // I_w = 1/12 m *(h^2+d^2)  = 1/12 * 80 * (4+100)  = 693.33
00083   // I_d = 1/12 m *(h^2+w^2)  = 1/12 * 80 * (4+16)   = 133.33
00084 
00085 
00086   vcg::tri::Torus(torusMesh,2,1,1024,512);
00087   vcg::tri::Inertia<MyMesh> It(torusMesh);
00088   cc = It.CenterOfMass();
00089   It.InertiaTensorEigen(IT,ITv);
00090 
00091   printf("\nTorus of radius 2,1\n");
00092   printf("Mass %f \n",It.Mass());
00093   printf("CenterOfMass %f %f %f\n",cc[0],cc[1],cc[2]);
00094   printf("InertiaTensor Values  %6.3f %6.3f %6.3f\n",ITv[0],ITv[1],ITv[2]);
00095   printf("InertiaTensor Matrix\n");
00096 
00097   printf(" %6.3f %6.3f %6.3f\n",IT[0][0],IT[0][1],IT[0][2]);
00098   printf(" %6.3f %6.3f %6.3f\n",IT[1][0],IT[1][1],IT[1][2]);
00099   printf(" %6.3f %6.3f %6.3f\n",IT[2][0],IT[2][1],IT[2][2]);
00100 
00101   /*
00102      Now we have a torus with c = 2, a = 1
00103      c = radius of the ring
00104      a = radius of the section
00105 
00106     Volume is:
00107     V= 2 PI^2 * a^2 * c = ~39.478
00108 
00109     Inertia tensor should be:
00110 
00111     | ( 5/8 a^2 + 1/2 c^2 ) M             0                         0     |
00112     |            0             ( 5/8 a^2 + 1/2 c^2 ) M              0     | =
00113     |            0                        0             (3/4 a^2 + c^2) M |
00114 
00115     | ( 5/8+2 ) M        0           0     |   | 103.630    0        0     |
00116   = |      0         ( 5/8+2 ) M     0     | = |    0    103.630     0     |
00117     |      0             0       (3/4+2) M |   |    0       0      187.52  |
00118 
00119   */
00120 
00121   return 0;
00122 }


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:20