Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_MEL_WRITE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_T4MESH_IO_T4MESH_MEL_WRITE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <iostream>
00013 #include <fstream>
00014 #include <sstream>
00015
00016 namespace OpenTissue
00017 {
00018 namespace t4mesh
00019 {
00020
00021 namespace detail
00022 {
00023
00037 template<typename vector3_type,typename real_type>
00038 inline std::string mel_tetrahedron(
00039 size_t const & idx
00040 , vector3_type const & p0
00041 , vector3_type const & p1
00042 , vector3_type const & p2
00043 , vector3_type const & p3
00044 , real_type scale
00045 )
00046 {
00047 std::stringstream mel;
00048
00049 vector3_type c = (p0+p1+p2+p3)/real_type(4.0);
00050 vector3_type v0 = scale*(p0 - c) + c;
00051 vector3_type v1 = scale*(p1 - c) + c;
00052 vector3_type v2 = scale*(p2 - c) + c;
00053 vector3_type v3 = scale*(p3 - c) + c;
00054
00055
00056 mel << "polyCreateFacet -ch off"
00057 << " -p " << v0[0] << " " << v0[1] << " " << v0[2]
00058 << " -p " << v1[0] << " " << v1[1] << " " << v1[2]
00059 << " -p " << v2[0] << " " << v2[1] << " " << v2[2]
00060 << " -n idx" << idx << ";"
00061 << std::endl;
00062
00063 mel << "polyAppendVertex -ch off -v 0 -v 1"
00064 << " -p "
00065 << v3[0] << " " << v3[1] << " " << v3[2]
00066 << ";"
00067 << std::endl;
00068
00069 mel << "polyAppendVertex -ch off -v 1 -v 2 -v 3;" << std::endl;
00070 mel << "polyAppendVertex -ch off -v 2 -v 0 -v 3;" << std::endl;
00071 return mel.str();
00072 }
00073
00074 }
00075
00092 template<typename point_container,typename t4mesh_type,typename real_type2>
00093 inline bool mel_write(std::string const & filename, t4mesh_type const & mesh, point_container & points, real_type2 scale)
00094 {
00095 typedef typename t4mesh_type::node_iterator node_iterator;
00096 typedef typename t4mesh_type::node_iterator const_node_iterator;
00097 typedef typename t4mesh_type::tetrahedron_iterator tetrahedron_iterator;
00098 typedef typename t4mesh_type::tetrahedron_iterator const_tetrahedron_iterator;
00099
00100 std::ofstream mel_file(filename.c_str(), std::ios::out);
00101 if (!mel_file)
00102 {
00103 std::cerr << "t4mesh::mel_write(): Error unable to create file: "<< filename << std::endl;
00104 return false;
00105 }
00106
00107 for(const_tetrahedron_iterator tetrahedron=mesh.tetrahedron_begin();tetrahedron!=mesh.tetrahedron_end();++tetrahedron)
00108 {
00109 std::string mel_string = detail::mel_tetrahedron(
00110 tetrahedron->idx()
00111 , points[tetrahedron->i()->idx()]
00112 , points[tetrahedron->j()->idx()]
00113 , points[tetrahedron->k()->idx()]
00114 , points[tetrahedron->m()->idx()]
00115 , scale
00116 );
00117 mel_file << mel_string << std::endl;
00118 }
00119 mel_file << "select -r \"idx*\" ;" << std::endl;
00120 mel_file << "group; xform -os -piv 0 0 0;" << std::endl;
00121
00122 mel_file.flush();
00123 mel_file.close();
00124
00125 std::cout << "done writting mel file.." << std::endl;
00126
00127 return true;
00128 }
00129
00145 template<typename t4mesh_type, typename real_type2>
00146 inline bool mel_write(std::string const & filename, t4mesh_type const & mesh, real_type2 scale)
00147 {
00148 default_point_container<t4mesh_type> points(const_cast<t4mesh_type*>(&mesh));
00149 return mel_write(filename,mesh,points,scale);
00150 }
00151
00152 }
00153 }
00154
00155
00156 #endif