c++ - Printing to a STL(stereolithography) file in binary mode -


i trying print stl file , can't print correctly. there lot of samples hex format printing in c++ no sample program binary format. program follow. wrong program?

       string name = "create stlwrite.m ";        name = name + currentdatetime();        pfile.setf(ios::left);        pfile.width(sizeof(unsigned char)*80);        //header        pfile << name;              unsigned int size = faces.rows;        //size        pfile.write((char*)&size,sizeof(size));            int height = 25;        unsigned short ** data= new unsigned short *[height];        for(int = 0; < height; i++)        {            data[i] = new unsigned short[142000];        }        for(int j = 0; j < 142000; j++)        {                int = 0;                  //for 1 facets                for(int k = 0; k < (*facets[j]).cols; k++)                {                    for(int l = 0; l < (*facets[j]).rows; l++)                    {                        float f = (*facets[j]).at<float>(l,k);                        data[i][j] = *reinterpret_cast<unsigned int *>(&f);                        data[i+1][j] = *reinterpret_cast<unsigned int *>(&f)>>16;                                             = + 2;                    }                 }             //then last row            data[height-1][j] = (unsigned short)0;        }         (int = 0; < height; i++)          (int j = 0; j < faces.rows; j++)             pfile.write ((char*)&data[i][j], sizeof(unsigned short) );        pfile.close(); 

edit1: follow idea of matlab stlwrite.mat program.link

this c code print in binary mode. calling function is:

    stl_write_binary(x, y, path) 

where x 3xm vertices matrix , y 3xn faces matrix. both in opencv's mat structure.
path path store stl file can modify according requirement.
whole code is:

int stl_write_binary(mat &vertemp, mat &faces, pathtofolders &path) {     mat vertices(vertemp.cols,vertemp.rows,vertemp.type());     vertices = vertemp.t();     cv::mat** facets = new cv::mat*[faces.rows];         for(int = 0; < faces.rows; i++)     {         facets[i] = new cv::mat(3,4,cv_32f);          for(int j = 0; j < 3; j++)         {             vertices(range::all(),range(faces.at<int>(i,j),faces.at<int>(i,j)+1)).convertto((*(facets[i]))(range::all(),range(j+1,j+2)), cv_32f);          }        }     //compute normals     mat v1(3,faces.rows,cv_32f);     mat v2(3,faces.rows,cv_32f);     for(int = 0; < faces.rows; i++)     {         v1(range::all(),range(i,i+1)) = ((*(facets[i]))(range::all(),range(2,3))) - ((*(facets[i]))(range::all(),range(1,2)));         v2(range::all(),range(i,i+1)) = ((*(facets[i]))(range::all(),range(3,4))) - ((*(facets[i]))(range::all(),range(1,2)));     }         mat v3(3,faces.rows,cv_32f);     mat v4(3,faces.rows,cv_32f);     mat v5(3,faces.rows,cv_32f);     mat v6(3,faces.rows,cv_32f);     v1(range(1,2),range::all()).copyto(v3(range(0,1),range::all()));     v1(range(2,3),range::all()).copyto(v3(range(1,2),range::all()));     v1(range(0,1),range::all()).copyto(v3(range(2,3),range::all()));      v2(range(2,3),range::all()).copyto(v4(range(0,1),range::all()));     v2(range(0,1),range::all()).copyto(v4(range(1,2),range::all()));     v2(range(1,2),range::all()).copyto(v4(range(2,3),range::all()));      v2(range(1,2),range::all()).copyto(v5(range(0,1),range::all()));     v2(range(2,3),range::all()).copyto(v5(range(1,2),range::all()));     v2(range(0,1),range::all()).copyto(v5(range(2,3),range::all()));      v1(range(2,3),range::all()).copyto(v6(range(0,1),range::all()));     v1(range(0,1),range::all()).copyto(v6(range(1,2),range::all()));     v1(range(1,2),range::all()).copyto(v6(range(2,3),range::all()));      mat normals(3,faces.rows,cv_32f);     normals = v3.mul(v4) - v5.mul(v6);      v1.release();     v2.release();     v3.release();     v4.release();     v5.release();     v6.release();      mat normalsqu(3,faces.rows,cv_32f);     mat normalsqu_colm_summed(1,faces.rows,cv_32f);     normalsqu = normals.mul(normals);      cv::reduce(normalsqu, normalsqu_colm_summed, 0, cv_reduce_sum, cv_32f);     cv::sqrt(normalsqu_colm_summed,normalsqu_colm_summed);//check mem leak     normalsqu.release();     normalsqu_colm_summed = 1/normalsqu_colm_summed;      for(int = 0; < faces.rows; i++)     {         normals(range::all(),range(i,i+1)) = normals(range::all(),range(i,i+1)) * normalsqu_colm_summed.at<float>(i);     }      normalsqu_colm_summed.release();     for(int = 0; < faces.rows; i++)     {         normals(range::all(),range(i,i+1)).copyto(((*(facets[i]))(range::all(),range(0,1))));      }     normals.release();     //write stl file     //get file paths     std::vector<std::string> masktoken;     split(path.pathtoimages, '/', masktoken);     path.pathtostl = path.pathtostl+masktoken[0];     for(int = 1; < masktoken.size()-1; i++)     {         path.pathtostl = path.pathtostl+"/"+masktoken[i];     }     path.pathtostl = path.pathtostl+"/"+"mesh.stl";     cout<<"saving file "<<path.pathtostl<<endl;     const char * c = path.pathtostl.c_str();     ofstream pfile;     pfile.open (c, ios::out | ios::binary);      if (pfile.is_open())     {        string name = "create stlwrite.m ";        name = name + currentdatetime();        pfile.setf(ios::left);        pfile.width(sizeof(unsigned char)*80);        //header        pfile << name;              unsigned int size = faces.rows;        //size        pfile.write((char*)&size,sizeof(size));            int height = (*facets[0]).rows * (*facets[0]).cols * 2 + 1;        unsigned short ** data= new unsigned short *[height];        for(int = 0; < height; i++)        {            data[i] = new unsigned short[faces.rows];        }        for(int j = 0; j < faces.rows; j++)        {                int = 0;                  //for 1 facets                for(int k = 0; k < (*facets[j]).cols; k++)                {                    for(int l = 0; l < (*facets[j]).rows; l++)                    {                        float f = (*facets[j]).at<float>(l,k);                        data[i][j] = *reinterpret_cast<unsigned int *>(&f);                        data[i+1][j] = *reinterpret_cast<unsigned int *>(&f)>>16;                                             = + 2;                    }                 }             //then last row            data[height-1][j] = (unsigned short)0;        }         (int = 0; < faces.rows; i++)          (int j = 0; j < height; j++)             pfile.write ((char*)&data[j][i], sizeof(unsigned short) );        pfile.close();        //fclose (pfile);        //delete table        for(int = 0; < height; i++)        {            delete[] data[i];        }        delete[] data;     }else{         cout<<"stlwrite:cannotwritefile"<<" "<<"unable write to"<<" "<<path.pathtostl<<endl;     }     for(int = 0; < faces.rows; i++)     {         (*facets[i]).release();     }     delete[] facets;      return 1; } 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -