GTSAlgos.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Juergen Riegel         <juergen.riegel@web.de>          *
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 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # ifdef FC_OS_LINUX
00027 #         include <unistd.h>
00028 # endif
00029 #endif
00030 
00031 
00032 #include "GTSAlgos.h"
00033 #include "Mesh.h"
00034 
00035 #include "Core/MeshIO.h"
00036 #include "Core/MeshKernel.h"
00037 #include "Core/Iterator.h"
00038 #include "Core/Algorithm.h"
00039 #include "Core/TopoAlgorithm.h"
00040 #include "Core/Evaluation.h"
00041 
00042 #include <Base/Exception.h>
00043 #include <Base/FileInfo.h>
00044 #include <Base/Console.h>
00045 
00046 using namespace Mesh;
00047 using namespace MeshCore;
00048 
00049 
00050 
00051 void GTSAlgos::coarsen(float f)
00052 {
00053   GtsSurface * surface;
00054 
00055   // create a GTS surface
00056   surface = GTSAlgos::createGTSSurface(_Mesh);
00057 
00058 
00059   guint stop_number=100000;
00060   gdouble fold = 3.1415 / 180.;
00061 
00062   try{
00063     gts_surface_coarsen (surface, 
00064                          NULL, NULL, 
00065                          NULL, NULL, 
00066                          (GtsStopFunc)gts_coarsen_stop_number,
00067                          &stop_number, fold);
00068   } catch (...)
00069   {
00070     gts_object_destroy (GTS_OBJECT (surface));
00071     throw Base::Exception("Unknown error in GTSAlgos::coarsen()");
00072   }
00073 
00074   // get the standard mesh
00075   _Mesh.getKernel().Clear();
00076   fillMeshFromGTSSurface(_Mesh,surface);
00077   gts_object_destroy (GTS_OBJECT (surface));
00078 }
00079 
00080 
00081 void GTSAlgos::boolean(const Mesh::MeshObject& ToolMesh, int Type)
00082 {
00083   GtsSurface * s1, * s2, * s3;
00084   GtsSurfaceInter * si;
00085   GNode * tree1, * tree2;
00086   gboolean check_self_intersection = FALSE;
00087   gboolean closed = TRUE, is_open1, is_open2;
00088 
00089 
00090   // create a GTS surface
00091   s1 = GTSAlgos::createGTSSurface(_Mesh);
00092   s2 = GTSAlgos::createGTSSurface(ToolMesh);
00093 
00094   // clear the mesh (mermory)
00095   //Mesh1.clear();
00096   //Mesh2.clear();
00097 
00098   // check that the surfaces are orientable manifolds 
00099   if (!gts_surface_is_orientable (s1)) {
00100     gts_object_destroy (GTS_OBJECT (s1));
00101     gts_object_destroy (GTS_OBJECT (s2));
00102     throw Base::Exception("surface 1 is not an orientable manifold\n");
00103   }
00104   if (!gts_surface_is_orientable (s2)) {
00105     gts_object_destroy (GTS_OBJECT (s1));
00106     gts_object_destroy (GTS_OBJECT (s2));
00107     throw Base::Exception("surface 2 is not an orientable manifold\n");
00108   }
00109 
00110   // check that the surfaces are not self-intersecting 
00111   if (check_self_intersection) {
00112     GtsSurface * self_intersects;
00113 
00114     self_intersects = gts_surface_is_self_intersecting (s1);
00115     if (self_intersects != NULL) {
00116 //      if (verbose)
00117 //            gts_surface_print_stats (self_intersects, stderr);
00118 //      gts_surface_write (self_intersects, stdout);
00119       gts_object_destroy (GTS_OBJECT (self_intersects));
00120       gts_object_destroy (GTS_OBJECT (s1));
00121       gts_object_destroy (GTS_OBJECT (s2));
00122       throw Base::Exception("surface is self-intersecting\n");
00123     }
00124     self_intersects = gts_surface_is_self_intersecting (s2);
00125     if (self_intersects != NULL) {
00126 //      if (verbose)
00127 //            gts_surface_print_stats (self_intersects, stderr);
00128 //      gts_surface_write (self_intersects, stdout);
00129       gts_object_destroy (GTS_OBJECT (self_intersects));
00130       gts_object_destroy (GTS_OBJECT (s1));
00131       gts_object_destroy (GTS_OBJECT (s2));
00132       throw Base::Exception("surface is self-intersecting\n");
00133     }
00134   }
00135 
00136   // build bounding box tree for first surface 
00137   tree1 = gts_bb_tree_surface (s1);
00138   is_open1 = gts_surface_volume (s1) < 0. ? TRUE : FALSE;
00139 
00140   // build bounding box tree for second surface 
00141   tree2 = gts_bb_tree_surface (s2);
00142   is_open2 = gts_surface_volume (s2) < 0. ? TRUE : FALSE;
00143 
00144   si = gts_surface_inter_new (gts_surface_inter_class (),
00145                               s1, s2, tree1, tree2, is_open1, is_open2);
00146   g_assert (gts_surface_inter_check (si, &closed));
00147   if (!closed) {
00148     gts_object_destroy (GTS_OBJECT (s1));
00149     gts_object_destroy (GTS_OBJECT (s2));
00150     gts_bb_tree_destroy (tree1, TRUE);
00151     gts_bb_tree_destroy (tree2, TRUE);  
00152     throw Base::Exception("the intersection of 1 and  2 is not a closed curve\n");
00153   }
00154 
00155   s3 = gts_surface_new (gts_surface_class (),
00156                         gts_face_class (),
00157                         gts_edge_class (),
00158                         gts_vertex_class ());  
00159   if (Type==0) { // union
00160     gts_surface_inter_boolean (si, s3, GTS_1_OUT_2);
00161     gts_surface_inter_boolean (si, s3, GTS_2_OUT_1);
00162   }
00163   else if (Type==1) { // inter
00164     gts_surface_inter_boolean (si, s3, GTS_1_IN_2);
00165     gts_surface_inter_boolean (si, s3, GTS_2_IN_1);
00166   }
00167   else if (Type==2) { //diff
00168     gts_surface_inter_boolean (si, s3, GTS_1_OUT_2);
00169     gts_surface_inter_boolean (si, s3, GTS_2_IN_1);
00170     gts_surface_foreach_face (si->s2, (GtsFunc) gts_triangle_revert, NULL);
00171     gts_surface_foreach_face (s2, (GtsFunc) gts_triangle_revert, NULL);
00172   }
00173   else if (Type==3) { // cut inner
00174     gts_surface_inter_boolean (si, s3, GTS_1_IN_2);
00175   }
00176   else if (Type==4) { // cut outer
00177     gts_surface_inter_boolean (si, s3, GTS_1_OUT_2);
00178   }
00179    
00180   // check that the resulting surface is not self-intersecting 
00181   if (check_self_intersection) {
00182     GtsSurface * self_intersects;
00183 
00184     self_intersects = gts_surface_is_self_intersecting (s3);
00185     if (self_intersects != NULL) {
00186  //      if (verbose)
00187  //           gts_surface_print_stats (self_intersects, stderr);
00188  //     gts_surface_write (self_intersects, stdout);
00189       gts_object_destroy (GTS_OBJECT (self_intersects));
00190       gts_object_destroy (GTS_OBJECT (s1));
00191       gts_object_destroy (GTS_OBJECT (s2));
00192       gts_object_destroy (GTS_OBJECT (s3));
00193       gts_object_destroy (GTS_OBJECT (si));
00194       gts_bb_tree_destroy (tree1, TRUE);
00195       gts_bb_tree_destroy (tree2, TRUE);  
00196       throw Base::Exception("the resulting surface is self-intersecting\n");
00197     }
00198   }
00199   // display summary information about the resulting surface 
00200   //  if (verbose)
00201   //    gts_surface_print_stats (s3, stderr);
00202   // write resulting surface to standard output 
00203 
00204   // get the standard mesh
00205   _Mesh.clear();
00206   fillMeshFromGTSSurface(_Mesh,s3);
00207 
00208 
00209   // destroy surfaces 
00210   gts_object_destroy (GTS_OBJECT (s1));
00211   gts_object_destroy (GTS_OBJECT (s2));
00212   gts_object_destroy (GTS_OBJECT (s3));
00213   gts_object_destroy (GTS_OBJECT (si));
00214 
00215   // destroy bounding box trees (including bounding boxes) 
00216   gts_bb_tree_destroy (tree1, TRUE);
00217   gts_bb_tree_destroy (tree2, TRUE);  
00218   
00219 
00220 }
00221 
00222 
00223 
00224 
00225 
00227 static GtsEdge * new_edge (GtsVertex * v1, GtsVertex * v2)
00228 {
00229   GtsSegment * s = gts_vertices_are_connected (v1, v2);
00230   if( s == NULL ) 
00231     return gts_edge_new (gts_edge_class (), v1, v2);
00232   else
00233     return GTS_EDGE (s);
00234 }
00235 
00236 
00237 GtsSurface* GTSAlgos::createGTSSurface(const Mesh::MeshObject& Mesh)
00238 {
00239   GtsSurface* Surf = gts_surface_new (gts_surface_class (),
00240                                       gts_face_class (),
00241                                       gts_edge_class (),
00242                                       gts_vertex_class () );
00243 
00244   unsigned long p1,p2,p3;
00245   Base::Vector3f Vertex;
00246 
00247 
00248   // Geting all the points
00249   GtsVertex ** aVertex = (GtsVertex **) malloc(Mesh.getKernel().CountPoints() * sizeof (GtsVertex *));
00250   for (unsigned int PIter = 0;PIter < Mesh.getKernel().CountPoints(); PIter++)
00251   {
00252     // Transform the point in the global coordinate system
00253     Vertex = Mesh.getTransform() * Mesh.getKernel().GetPoint(PIter);
00254 
00255     aVertex[PIter] = gts_vertex_new (gts_vertex_class (), Vertex.x, Vertex.y, Vertex.z);
00256   }
00257 
00258     // cycling through the facets
00259   for (unsigned int pFIter = 0;pFIter < Mesh.getKernel().CountFacets(); pFIter++)
00260   {
00261     // geting the three points of the facet
00262     Mesh.getKernel().GetFacetPoints(pFIter,p1,p2,p3);
00263     
00264     // creating the edges and add the face to the surface
00265     gts_surface_add_face (Surf, 
00266             gts_face_new (Surf->face_class,
00267           new_edge (aVertex[p1],aVertex[p2]),
00268           new_edge (aVertex[p2],aVertex[p3]),
00269           new_edge (aVertex[p3],aVertex[p1])));
00270   }
00271 
00272   Base::Console().Log("GTS [%d faces, %d Points, %d Edges,%s ,%s]\n",gts_surface_face_number(Surf),
00273                                                                      gts_surface_vertex_number(Surf),
00274                                                                      gts_surface_edge_number(Surf),
00275                                                                      gts_surface_is_orientable (Surf)?"orientable":"not orientable",
00276                                                                      gts_surface_is_self_intersecting(Surf)?"self-intersections":"no self-intersection" ); 
00277 
00278   return Surf;
00279 }
00280 
00282 static void onFaces (GtsTriangle * t,  std::vector<MeshGeomFacet> *VAry )
00283 {
00284   GtsVertex *mv0,*mv1,*mv2;
00285 
00286   gts_triangle_vertices (t,&mv0,&mv1,&mv2);
00287 
00288   VAry->push_back(MeshGeomFacet(Base::Vector3f(mv0->p.x,mv0->p.y,mv0->p.z),
00289                                 Base::Vector3f(mv1->p.x,mv1->p.y,mv1->p.z),
00290                                 Base::Vector3f(mv2->p.x,mv2->p.y,mv2->p.z)));
00291 
00292 }
00293 
00294 
00295 void GTSAlgos::fillMeshFromGTSSurface(Mesh::MeshObject& Mesh, GtsSurface* pSurface)
00296 {
00297   std::vector<MeshGeomFacet> VAry;
00298 
00299   // remove old mesh
00300   Mesh.getKernel().Clear();
00301 
00302 //  gts_surface_foreach_vertex(pSurface,(GtsFunc) onVertices,&MeshK);
00303   gts_surface_foreach_face (pSurface, (GtsFunc) onFaces,&VAry);
00304 
00305   // destroy surfaces 
00306   //gts_object_destroy (GTS_OBJECT (pSurface));
00307 
00308   // put the facets the simple way in the mesh, totp is recalculated!
00309   Mesh.getKernel() = VAry;
00310 
00311 }
00312 

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