Skip to content

Instantly share code, notes, and snippets.

@ToruNiina
Last active August 8, 2016 06:41
Show Gist options
  • Save ToruNiina/089c5b25e7356faa58b90116efec9a69 to your computer and use it in GitHub Desktop.
Save ToruNiina/089c5b25e7356faa58b90116efec9a69 to your computer and use it in GitHub Desktop.
generates vmd script (tcl) that draw polygon on vmd from STL binary format file.
/* Description: this program generates vmd script file (.tcl file) that draws *
* polygon structure on vmd from STL binary-format file. *
* *
* Licensing Terms : This code is licensed under the MIT License. *
* copyright (c) 2016 Toru Niina (niina.toru.68u@gmail.com) *
* all rights reserved.   *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
* */
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
constexpr static std::size_t sizeof_uint = sizeof(unsigned int);
constexpr static std::size_t sizeof_float = sizeof(float);
struct position
{
float x, y, z;
};
struct triangle
{
position a, b, c;
};
std::ostream& operator<<(std::ostream& os, const position& pos)
{
os << pos.x << " " << pos.y << " " << pos.z;
return os;
}
std::ostream& operator<<(std::ostream& os, const triangle& tri)
{
os << "draw triangle {" << tri.a << "} {"
<< tri.b << "} {" << tri.c << "}" << std::endl;
return os;
}
position read_vector(std::ifstream& ifs)
{
char *float0 = new char [sizeof_float];
char *float1 = new char [sizeof_float];
char *float2 = new char [sizeof_float];
ifs.read(float0, sizeof_float);
ifs.read(float1, sizeof_float);
ifs.read(float2, sizeof_float);
const float x = *reinterpret_cast<float*>(float0);
const float y = *reinterpret_cast<float*>(float1);
const float z = *reinterpret_cast<float*>(float2);
delete [] float0;
delete [] float1;
delete [] float2;
return position{x, y, z};
}
void read_triangle(std::ifstream& ifs)
{
const position normal = read_vector(ifs);
std::cout << "# normal " << normal << std::endl;
const position A = read_vector(ifs);
const position B = read_vector(ifs);
const position C = read_vector(ifs);
triangle tri = {A, B, C};
std::cout << tri << std::endl;
char *unused_data = new char [2];
ifs.read(unused_data, 2);
delete [] unused_data;
return;
}
int main(int argc, char **argv)
{
try
{
if(argc != 2)
{
std::cerr << "usage: ./stl2vmd [filename].stl (binary only) > vmd.tcl" << std::endl;
return 1;
}
const std::string filename = argv[1];
std::ifstream stlfile(filename, std::ios::in | std::ios::binary);
if(!stlfile.good())
throw std::runtime_error("file open error");
std::cerr << "file reading start." << std::endl;
std::cout << "draw delete all" << std::endl;
stlfile.seekg(0, stlfile.end);
const std::size_t size_of_file = stlfile.tellg();
stlfile.seekg(0, stlfile.beg);
std::cerr << "file size: " << size_of_file << std::endl;
char *ch_header = new char[81];
stlfile.read(ch_header, 80);
ch_header[80] = '\0';
const std::string header(ch_header);
delete [] ch_header;
std::cerr << "header : " << header << std::endl;
char *ch_numTriangle = new char [sizeof_uint];
stlfile.read(ch_numTriangle, sizeof_uint);
const std::size_t num_Triangle = *reinterpret_cast<unsigned int*>(ch_numTriangle);
delete [] ch_numTriangle;
std::cerr << "# of face: " << num_Triangle << std::endl;
if(50 * num_Triangle + 80 + sizeof_uint != size_of_file)
{
std::cerr << "file size must be 50 * number of triangle + 80 + 4" << std::endl;
std::cerr << " = " << 50 * num_Triangle + 80 + sizeof_uint << std::endl;
throw std::runtime_error("invalid filesize");
}
for(std::size_t i=0; i < num_Triangle; ++i)
{
read_triangle(stlfile);
}
return 0;
}
catch(std::exception& except)
{
std::cerr << "exception thrown. what: " << except.what() << std::endl;
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment