cgma
MedialTool3D.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 #include "MedialTool3D.hpp"
00017 #include "CubitMessage.hpp"
00018 
00019 #ifdef USING_MEDIAL
00020 
00021 
00022 
00023 #include "medial/medial_util/MedialDefines.hpp"
00024 #include "medial/medial_util/Triangle3D.hpp"
00025 #include "medial/medial_util/Vec3D.hpp"
00026 #include "medial/medial3D/Medial3DAPI.hpp"
00027 #include "medial/medial3D/MedialVertex3D.hpp"
00028 #include "medial/medial3D/MedialEdge3D.hpp"
00029 #include "medial/medial3D/MedialFace3D.hpp"
00030 
00034 #include "GfxDebug.hpp"
00035 #include "CubitVector.hpp"
00036 #include "RefVertex.hpp"
00037 #include "RefFace.hpp"
00038 #include "RefVolume.hpp"
00039 #include "CubitPoint.hpp"
00040 #include "CubitFacet.hpp"
00041 #endif //USING_MEDIAL
00042 
00043 
00050 MedialTool3D::MedialTool3D(RefVolume *ref_volume_ptr)
00051 {
00052   myRefVolume = ref_volume_ptr;
00053 }
00054 
00061 MedialTool3D::~MedialTool3D()
00062 {
00063 
00064 }
00065 
00076 #ifndef USING_MEDIAL
00077 CubitStatus MedialTool3D::create_medial_axis_3d( 
00078                   DLIList <RefVertex*> &medial_verts,
00079                   DLIList <RefEdge*> &medial_edges, 
00080                   DLIList <RefFace*> &medial_faces,
00081                   bool full_search,
00082                   double cube_size,
00083                   double accuracy,
00084                   double min_ang_strong,
00085                   double min_angle_diff_components)
00086 {
00087   PRINT_ERROR("Medial Axis Code is not included with this version of CGM.\n");
00088   return CUBIT_FAILURE;
00089 }
00090 #endif
00091 
00093 // Functions that gets compiled only when compiling the medial stuff
00095 #ifdef USING_MEDIAL
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 CubitStatus MedialTool3D::create_medial_axis_3d(
00107   DLIList <RefVertex*> &medial_verts,
00108   DLIList <RefEdge*> &medial_edges,
00109   DLIList <RefFace*> &medial_faces,
00110   bool full_search,
00111   double cube_size,
00112   double accuracy,
00113   double min_ang_strong,
00114   double min_angle_diff_components)
00115 {
00116   // create the array of vertices and triangles to represent objectdata3d
00117   Vec3DPArray my_vertices;
00118   Triangle3DPArray my_facets;
00119 
00120   MedialVertex3DPArray my_output_verts;
00121   MedialEdge3DPArray my_output_edges;
00122   MedialFace3DPArray my_output_faces;
00123 
00124   // get the boundary facets
00125   CubitStatus boundaries = get_boundary_facets(my_vertices, my_facets);
00126 
00127   // create the medial axis by calling the Medial3DAPI
00128   int results = Medial3DAPI::create_medial3d(my_vertices, my_facets,
00129                    full_search, cube_size, accuracy, min_ang_strong,
00130                    min_angle_diff_components, my_output_verts,
00131                    my_output_edges, my_output_faces);
00132 
00133   if ( results != 1 )
00134   {
00135     PRINT_ERROR("Medial Extraction Failed.\n");
00136     return CUBIT_FAILURE;
00137   }
00138   draw_medial_results( my_output_verts, my_output_edges, my_output_faces,
00139               CUBIT_RED_INDEX, CUBIT_CYAN_INDEX, CUBIT_RED_INDEX);
00140 
00141   // translate the data from Medial Extractor format back to Cubit Format
00142 //?***** Not implemented as of now.
00143     return CUBIT_SUCCESS;
00144 }
00145 
00153 void MedialTool3D::convert_vec3d_to_cubitvector( Vec3D &my_vec3d,
00154                                                  CubitVector &my_cubitvec)
00155 {
00156   my_cubitvec.set(my_vec3d.x, my_vec3d.y, my_vec3d.z);
00157 }
00158 
00166 void MedialTool3D::convert_cubitvector_to_vec3d( CubitVector &my_cubitvec,
00167                                                  Vec3D &my_vec3d)
00168 {
00169   my_vec3d.x = my_cubitvec.x();
00170   my_vec3d.y = my_cubitvec.y();
00171   my_vec3d.z = my_cubitvec.z();
00172 }
00173 
00180 void MedialTool3D::draw_medial_results(
00181   MedialVertex3DPArray &output_vertices,
00182   MedialEdge3DPArray &output_edges,
00183   MedialFace3DPArray &output_faces,
00184   int vert_color, int edge_color, int face_color)
00185 {
00186   int ii;
00187   Vec3D curr_vec3d_1, curr_vec3d_2;
00188 
00189   int n1 = output_vertices.size();
00190   int n2 = output_edges.size();
00191 
00192   printf("Before drawing the results to screen\n");
00193   printf("Number of output vertices = %d\n",output_vertices.size());
00194   printf("Number of output segments = %d\n",output_edges.size());
00195   printf("Number of output triangles= %d\n",output_faces.size());
00196 /*  
00197   // draw the vertices
00198   for(ii =0; ii < output_vertices.size(); ii++)
00199     {
00200       curr_vec3d_1 = output_vertices[ii]->coordinates();
00201       GfxDebug::draw_point(curr_vec3d_1.x, curr_vec3d_1.y, curr_vec3d_1.z, vert_color);
00202     }
00203     GfxDebug::flush();
00204     
00205   // draw the edges
00206   for(ii=0; ii < output_edges.size(); ii++)
00207   {
00208     // get the start, end vertices and convert them to CubitVector format
00209     curr_vec3d_1 = output_edges[ii]->get_start_coordinates();
00210     curr_vec3d_2 = output_edges[ii]->get_end_coordinates();
00211     GfxDebug::draw_line(curr_vec3d_1.x, curr_vec3d_1.y,
00212         curr_vec3d_1.z, curr_vec3d_2.x, curr_vec3d_2.y, curr_vec3d_2.z,
00213         edge_color);
00214   }
00215   GfxDebug::flush();
00216 */
00217   //draw faces
00218   for(ii=0; ii<output_faces.size(); ii++)
00219   {
00220     // Points are ordered clockwise around the tri.
00221     draw_medial_face(output_faces[ii], face_color);
00222 //  void draw_tri   (GPoint p[], int color);
00223   }
00224   GfxDebug::flush();
00225 }
00226 
00234 void MedialTool3D::draw_medial_face(MedialFace3D* my_face, int tri_color)
00235 {
00236   // array of three points
00237   GPoint my_points[3];
00238   Vec3D v1, v2, v3;
00239   v1 = my_face->get_vertex1_coordinates();
00240   v2 = my_face->get_vertex2_coordinates();
00241   v3 = my_face->get_vertex3_coordinates();
00242   my_points[0].x = v1.x; my_points[0].y = v1.y; my_points[0].z = v1.z;
00243   my_points[1].x = v2.x; my_points[1].y = v2.y; my_points[1].z = v2.z;
00244   my_points[2].x = v3.x; my_points[2].y = v3.y; my_points[2].z = v3.z;
00245     //GfxDebug::draw_tri(my_points, tri_color);
00246   GfxDebug::draw_polygon(my_points, 3, 8/*CUBIT_GREEN_INDEX*/, CUBIT_GREEN_INDEX);
00247 }
00248 
00249 
00259 CubitStatus MedialTool3D::get_boundary_facets(
00260   Vec3DPArray &boundary_verts,
00261   Triangle3DPArray &boundary_facets)
00262 {
00263   // get all the ref faces of the volume
00264   DLIList<RefFace*> ref_faces_list;
00265   CubitStatus get_faces_status = myRefVolume->ref_faces(ref_faces_list);
00266   if(get_faces_status != CUBIT_SUCCESS)
00267     {
00268       PRINT_ERROR("Problem in getting the faces of volume geometry\n"
00269                   "Check volume %d\n", myRefVolume->id());
00270       return CUBIT_FAILURE;
00271     }
00272 
00273   // loop through each RefFace and get its facets
00274   int ii;
00275   RefFace *my_face;
00276   CubitStatus face_facet_stat;
00277   DLIList<CubitFacet*> my_facet_list;  // delete these after done
00278   DLIList<CubitPoint*> my_point_list;  // delete after done
00279 
00280   for(ii = ref_faces_list.size(); ii > 0; ii--)
00281   {
00282     my_face = ref_faces_list.get_and_step();
00283 
00284     // get the facets for the current face
00285     // get_facets appends the new vertices and facets to the exusting list
00286     face_facet_stat = my_face->get_facets( my_facet_list, my_point_list);
00287 
00288     if(face_facet_stat != CUBIT_SUCCESS)
00289     {
00290       PRINT_ERROR("Problem in getting the facets of face geometry\n"
00291                   "Check surface %d in volume %d\n",
00292                   my_face->id(), myRefVolume->id());
00293       return CUBIT_FAILURE;
00294     }
00295   }
00296 
00297   // translate the points to Vec3D format and flag the my_point_list
00298   CubitPoint *my_point;
00299   Vec3D *new_vec3d;
00300 
00301   // resize the size of "boundary_verts" and "boundary_facets" to equal that
00302   // of "my_point_list" and "my_facet_list"
00303   boundary_verts.resize(my_point_list.size());
00304   boundary_facets.resize(my_facet_list.size());
00305 
00306   // set all the pointers in vector list to NULL for safety
00307   for(ii=0; ii < boundary_verts.size(); ii++)
00308     boundary_verts[ii] = NULL;
00309   for(ii=0; ii < boundary_facets.size(); ii++)
00310     boundary_facets[ii] = NULL;
00311 
00312   // location of the current point
00313   CubitVector pt_coord;
00314 
00315   // go to each cubit point; set a flag and create corresponding new Vec3D
00316   for(ii = my_point_list.size(); ii > 0; ii--)
00317   {
00318     my_point = my_point_list.get_and_step();
00319     // set the mark number
00320     my_point->marked(ii-1);
00321     // create new Vec3D
00322     pt_coord = my_point->coordinates();
00323     new_vec3d = new Vec3D(pt_coord.x(), pt_coord.y(), pt_coord.z());
00324     boundary_verts[ii-1] = new_vec3d;
00325   }
00326 
00327   // translate the facets to Triangle3D format
00328   CubitFacet *my_facet;
00329   CubitPoint *vert0, *vert1, *vert2;
00330   Vec3D my_vert0, my_vert1, my_vert2;
00331   Triangle3D *my_tri;
00332 
00333   // go through each facet and create new Triangle3D
00334   for(ii = my_facet_list.size(); ii > 0; ii--)
00335   {
00336     my_facet = my_facet_list.get_and_step();
00337     // get the three points that form the facet and also their id's
00338     vert0 = my_facet->point(0);
00339     vert1 = my_facet->point(1);
00340     vert2 = my_facet->point(2);
00341 
00342     // create new Triangle3D
00343     my_tri = new Triangle3D(boundary_verts,
00344             vert0->marked(), vert1->marked(), vert2->marked());
00345     // assign the Triangle3D to its position in vector list
00346     boundary_facets[ii-1] = my_tri;
00347   }
00348 
00349   // delete the facet entities
00350   for(ii=my_facet_list.size(); ii >0; ii--)
00351     delete my_facet_list.pop();
00352   my_facet_list.clean_out();
00353 
00354   // delete the vertex entities
00355   for(ii=my_point_list.size(); ii>0; ii--)
00356     delete my_point_list.pop();
00357   my_point_list.clean_out();
00358 
00359   return CUBIT_SUCCESS;
00360 }
00361 #endif // USING_MEDIAL
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines