MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/dem/face.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raul Durand                   *
00004  * Copyright (C) 2009 Sergio Galindo                                    *
00005  *                                                                      *
00006  * This program is free software: you can redistribute it and/or modify *
00007  * it under the terms of the GNU General Public License as published by *
00008  * the Free Software Foundation, either version 3 of the License, or    *
00009  * any later version.                                                   *
00010  *                                                                      *
00011  * This program is distributed in the hope that it will be useful,      *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00014  * GNU General Public License for more details.                         *
00015  *                                                                      *
00016  * You should have received a copy of the GNU General Public License    *
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00018  ************************************************************************/
00019 
00020 #ifndef MECHSYS_DEM_FACE_H
00021 #define MECHSYS_DEM_FACE_H
00022 
00023 // MechSys
00024 #include <mechsys/dem/edge.h>
00025 #include <mechsys/dem/graph.h>
00026 #include <mechsys/dem/basic_functions.h>
00027 #include <mechsys/util/array.h>
00028 
00029 class Face
00030 {
00031 public:
00032     // Constructor
00033     Face (Array<Edge *> E); 
00034     Face (Array<Vec3_t> & V);
00035     Face (Array<Vec3_t*> & V);
00036     Face () {};
00037 
00038     // Destructor
00039     ~Face ();
00040 
00041 
00042     // Methods
00043     void UpdatedL ();               
00044     void Normal   (Vec3_t & N);     
00045     void Centroid (Vec3_t & C);     
00046     double Area   ();               
00047     void Draw      (std::ostream & os, double Radius=1.0, char const * Color="Blue", bool BPY=false);
00048 
00049     // Data
00050     Array<Edge*> Edges; 
00051     bool         Allocate; 
00052 };
00053 
00054 
00056 
00057 
00058 inline Face::Face (Array<Edge *> E)
00059 {
00060     Edges = E;
00061     Allocate = false;
00062 }
00063 
00064 inline Face::Face (Array<Vec3_t> & V)
00065 {
00066     for (size_t i = 0; i < V.Size() ; i++)
00067     {
00068         Edges.Push(new Edge(&V[i],&V[(i+1)%V.Size()]));
00069     }
00070     Allocate = true;
00071 }
00072 
00073 inline Face::Face(Array<Vec3_t*> & V)
00074 {
00075     for (size_t i = 0; i < V.Size() ; i++)
00076     {
00077         Edges.Push(new Edge(V[i],V[(i+1)%V.Size()]));
00078     }
00079     Allocate = true;
00080 }
00081 
00082 inline Face::~Face ()
00083 {
00084     if (Allocate) 
00085     {
00086         for (size_t i = 0; i<Edges.Size();i++)
00087         {
00088             delete Edges[i];
00089         }
00090     }
00091 }
00092 
00093 inline void Face::UpdatedL()
00094 {
00095     for (size_t i = 0; i<Edges.Size();i++)
00096     {
00097         Edges[i]->UpdatedL();
00098     }
00099 }
00100 
00101 inline void Face::Normal(Vec3_t & N)
00102 {
00103     N = cross(Edges[0]->dL, Edges[1]->dL);
00104     N = N/norm(N);
00105 }
00106 
00107 inline void Face::Centroid(Vec3_t & N)
00108 {
00109     N = Vec3_t(0.0,0.0,0.0);
00110     for (size_t i=0; i<Edges.Size(); i++)
00111     {
00112         N += *Edges[i]->X0;
00113     }
00114     N/=Edges.Size();
00115 }
00116 
00117 inline double Face::Area()
00118 {
00119     Vec3_t N;
00120     Normal(N);
00121     double area=0;
00122     for (size_t i=0; i<Edges.Size(); i++)
00123     {
00124         area += 0.5*dot(N,cross(*Edges[i]->X0,*Edges[(i+1)%Edges.Size()]->X0));
00125     }
00126     return area;
00127 }
00128 
00129 inline void Face::Draw (std::ostream & os, double Radius, char const * Color, bool BPY)
00130 {
00131     Array<Vec3_t> vi, vs; // two "sandwich" faces due to the spheroradius (i:inferior, s:superior)
00132     Vec3_t n = cross(Edges[0]->dL, Edges[1]->dL);
00133     n = n/norm(n);
00134     for (size_t i=0; i<Edges.Size(); i++)
00135     {
00136         vi.Push(*Edges[i]->X0 - Radius*n);
00137         vs.Push(*Edges[i]->X0 + Radius*n);
00138     }
00139     if (BPY)
00140     {
00141         BPYDrawPolygon (vi,os);
00142         BPYDrawPolygon (vs,os);
00143     }
00144     else
00145     {
00146         POVDrawPolygon (vi,os,Color);
00147         POVDrawPolygon (vs,os,Color);
00148     }
00149 }
00150 
00151 #endif // MECHSYS_DEM_FACE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines