VCG Library
trimesh_inertia.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
33 #include<vcg/complex/complex.h>
34 
35 #include<wrap/io_trimesh/import_off.h>
36 
37 #include<vcg/complex/algorithms/inertia.h>
38 #include<vcg/complex/algorithms/create/platonic.h>
39 
40 class MyEdge;
41 class MyFace;
42 class MyVertex;
43 struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex> ::AsVertexType,
44  vcg::Use<MyEdge> ::AsEdgeType,
45  vcg::Use<MyFace> ::AsFaceType>{};
46 
47 class MyVertex : public vcg::Vertex<MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
48 class MyFace : public vcg::Face< MyUsedTypes, vcg::face::FFAdj, vcg::face::Normal3f, vcg::face::VertexRef, vcg::face::BitFlags > {};
49 class MyEdge : public vcg::Edge<MyUsedTypes>{};
50 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge> > {};
51 
52 int main( int argc, char **argv )
53 {
54  MyMesh boxMesh,torusMesh;
55  vcg::Matrix33f IT;
56  vcg::Point3f ITv;
57 
58  vcg::tri::Hexahedron(boxMesh);
59  vcg::Matrix44f ScaleM,TransM;
60  ScaleM.SetScale(1.0f, 2.0f, 5.0f);
61  TransM.SetTranslate(2.0f,3.0f,4.0f);
64  vcg::tri::Inertia<MyMesh> Ib(boxMesh);
65  vcg::Point3f cc = Ib.CenterOfMass();
66  Ib.InertiaTensorEigen(IT,ITv);
67 
68  printf("Box of size 2,4,10, centered in (2,3,4)\n");
69  printf("Volume %f \n",Ib.Mass());
70  printf("CenterOfMass %f %f %f\n",cc[0],cc[1],cc[2]);
71  printf("InertiaTensor Values %6.3f %6.3f %6.3f\n",ITv[0],ITv[1],ITv[2]);
72  printf("InertiaTensor Matrix\n");
73 
74  printf(" %6.3f %6.3f %6.3f\n",IT[0][0],IT[0][1],IT[0][2]);
75  printf(" %6.3f %6.3f %6.3f\n",IT[1][0],IT[1][1],IT[1][2]);
76  printf(" %6.3f %6.3f %6.3f\n",IT[2][0],IT[2][1],IT[2][2]);
77 
78  // Now we have a box with sides (h,w,d) 2,4,10, centered in (2,3,4)
79  // Volume is 80
80  // inertia tensor should be:
81  // I_h = 1/12 m *(w^2+d^2) = 1/12 * 80 * (16+100) = 773.33
82  // I_w = 1/12 m *(h^2+d^2) = 1/12 * 80 * (4+100) = 693.33
83  // I_d = 1/12 m *(h^2+w^2) = 1/12 * 80 * (4+16) = 133.33
84 
85 
86  vcg::tri::Torus(torusMesh,2,1,1024,512);
87  vcg::tri::Inertia<MyMesh> It(torusMesh);
88  cc = It.CenterOfMass();
89  It.InertiaTensorEigen(IT,ITv);
90 
91  printf("\nTorus of radius 2,1\n");
92  printf("Mass %f \n",It.Mass());
93  printf("CenterOfMass %f %f %f\n",cc[0],cc[1],cc[2]);
94  printf("InertiaTensor Values %6.3f %6.3f %6.3f\n",ITv[0],ITv[1],ITv[2]);
95  printf("InertiaTensor Matrix\n");
96 
97  printf(" %6.3f %6.3f %6.3f\n",IT[0][0],IT[0][1],IT[0][2]);
98  printf(" %6.3f %6.3f %6.3f\n",IT[1][0],IT[1][1],IT[1][2]);
99  printf(" %6.3f %6.3f %6.3f\n",IT[2][0],IT[2][1],IT[2][2]);
100 
101  /*
102  Now we have a torus with c = 2, a = 1
103  c = radius of the ring
104  a = radius of the section
105 
106  Volume is:
107  V= 2 PI^2 * a^2 * c = ~39.478
108 
109  Inertia tensor should be:
110 
111  | ( 5/8 a^2 + 1/2 c^2 ) M 0 0 |
112  | 0 ( 5/8 a^2 + 1/2 c^2 ) M 0 | =
113  | 0 0 (3/4 a^2 + c^2) M |
114 
115  | ( 5/8+2 ) M 0 0 | | 103.630 0 0 |
116  = | 0 ( 5/8+2 ) M 0 | = | 0 103.630 0 |
117  | 0 0 (3/4+2) M | | 0 0 187.52 |
118 
119  */
120 
121  return 0;
122 }