MeshInterface.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 Werner Mayer <wmayer@users.sourceforge.net>        *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  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 Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #ifndef MESH_INTERFACE_H
00025 #define MESH_INTERFACE_H
00026 
00027 #include <Mod/Mesh/App/Core/Elements.h>
00028 #include <Mod/Mesh/App/Core/Iterator.h>
00029 #include <Mod/Mesh/App/Core/MeshKernel.h>
00030 #include <Base/Vector3D.h>
00031 
00032 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
00033 #error
00034 namespace Mesh
00035 {
00036 
00041 template <class Kernel>
00042 class Interface : public Kernel
00043 {
00044 public:
00045     Interface()
00046     {
00047     }
00048 
00049     Interface(const MeshCore::MeshKernel& kernel)
00050     {
00051         copy(kernel);
00052     }
00053 
00054     ~Interface()
00055     {
00056     }
00057 
00058     void copy(const MeshCore::MeshKernel& kernel_In)
00059     {
00060         this->clear();
00061         std::vector<Kernel::VertexHandle>  vertex_handles;
00062         vertex_handles.reserve(kernel_In.CountPoints());
00063         MeshCore::MeshPointIterator it(kernel_In);
00064         for (it.Init(); it.More(); it.Next()) {
00065             vertex_handles.push_back(this->add_vertex(Kernel::Point(it->x, it->y, it->z)));
00066         }
00067 
00068         const MeshCore::MeshFacetArray& ary = kernel_In.GetFacets();
00069         for (MeshCore::MeshFacetArray::_TConstIterator it = ary.begin(); it != ary.end(); ++it) {
00070             this->add_face(vertex_handles[it->_aulPoints[0]], vertex_handles[it->_aulPoints[1]], vertex_handles[it->_aulPoints[2]]);
00071         }
00072         vertex_handles.clear();
00073     }
00074 
00075     void release(MeshCore::MeshKernel& kernel_Out)
00076     {
00077         MeshCore::MeshFacetArray facets;
00078         MeshCore::MeshPointArray points;
00079 
00080         facets.reserve(this->n_faces());
00081         points.reserve(this->n_vertices());
00082 
00083         // get the points
00084         Kernel::ConstVertexIter v_it, v_end(this->vertices_end());
00085         for (v_it=this->vertices_begin(); v_it!=v_end; ++v_it) {
00086             Kernel::Point p = this->point(v_it);
00087             points.push_back(Base::Vector3f(p[0], p[1], p[2]));
00088         }
00089 
00090         // get the facets
00091         Kernel::ConstFaceIter f_it, f_end(this->faces_end());
00092         for (f_it=this->faces_begin(); f_it!=f_end; ++f_it) {
00093             MeshCore::MeshFacet face;
00094             int i=0;
00095             for (Kernel::ConstFaceVertexIter fv_it=this->cfv_iter(f_it); fv_it; ++fv_it) {
00096                 face._aulPoints[i++] = fv_it.handle().idx();
00097             }
00098 
00099             facets.push_back(face);
00100         }
00101         // get the neighbourhood
00102         //
00103         // iterate over all faces
00104         for (f_it=this->faces_begin(); f_it!=f_end; ++f_it) {
00105             // iterate over the half edges to which belong the current face 
00106             for (Kernel::ConstFaceHalfedgeIter fh_it=this->cfh_iter(f_it); fh_it; ++fh_it) {
00107                 // get the opposite half edge of the current half edge
00108                 Kernel::HalfedgeHandle hh = this->opposite_halfedge_handle(fh_it);
00109                 if (hh.is_valid()) {
00110                     // if the opposite half edge is valid a neighbour face must exist
00111                     Kernel::FaceHandle fh = this->face_handle(hh);
00112                     Kernel::VertexHandle vh = this->to_vertex_handle(hh);
00113                     for (int j=0; j<3; j++) {
00114                         // find the appropriate vertex and set the neighbour face
00115                         if (facets[f_it.handle().idx()]._aulPoints[j] == vh.idx()) {
00116                             facets[f_it.handle().idx()]._aulNeighbours[j] = fh.idx();
00117                             break;
00118                         }
00119                     }
00120                 }
00121             }
00122         }
00123 
00124         this->clear();
00125         kernel_Out.Adopt(points, facets, false);
00126     }
00127 };
00128 
00129 } // namespace Mesh
00130 
00131 
00132 #endif  // MESH_INTERFACE_H 

Generated on Wed Nov 23 19:00:22 2011 for FreeCAD by  doxygen 1.6.1