00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
00091 s1 = GTSAlgos::createGTSSurface(_Mesh);
00092 s2 = GTSAlgos::createGTSSurface(ToolMesh);
00093
00094
00095
00096
00097
00098
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
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
00117
00118
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
00127
00128
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
00137 tree1 = gts_bb_tree_surface (s1);
00138 is_open1 = gts_surface_volume (s1) < 0. ? TRUE : FALSE;
00139
00140
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) {
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) {
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) {
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) {
00174 gts_surface_inter_boolean (si, s3, GTS_1_IN_2);
00175 }
00176 else if (Type==4) {
00177 gts_surface_inter_boolean (si, s3, GTS_1_OUT_2);
00178 }
00179
00180
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
00187
00188
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
00200
00201
00202
00203
00204
00205 _Mesh.clear();
00206 fillMeshFromGTSSurface(_Mesh,s3);
00207
00208
00209
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
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
00249 GtsVertex ** aVertex = (GtsVertex **) malloc(Mesh.getKernel().CountPoints() * sizeof (GtsVertex *));
00250 for (unsigned int PIter = 0;PIter < Mesh.getKernel().CountPoints(); PIter++)
00251 {
00252
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
00259 for (unsigned int pFIter = 0;pFIter < Mesh.getKernel().CountFacets(); pFIter++)
00260 {
00261
00262 Mesh.getKernel().GetFacetPoints(pFIter,p1,p2,p3);
00263
00264
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
00300 Mesh.getKernel().Clear();
00301
00302
00303 gts_surface_foreach_face (pSurface, (GtsFunc) onFaces,&VAry);
00304
00305
00306
00307
00308
00309 Mesh.getKernel() = VAry;
00310
00311 }
00312