void Shape::makeHollow(double wallthickness) { invertNormals(); const Vector3d wall(wallthickness,wallthickness,wallthickness); Matrix4d invT = transform3D.getInverse(); vector<Triangle> cubet = cube(invT*Min-wall, invT*Max+wall); triangles.insert(triangles.end(),cubet.begin(),cubet.end()); CalcBBox(); }
void Shape::setTriangles(const vector<Triangle> &triangles_) { triangles = triangles_; CalcBBox(); double vol = volume(); if (vol < 0) { invertNormals(); vol = -vol; } //PlaceOnPlatform(); cerr << _("Shape has volume ") << volume() << _(" mm^3 and ") << triangles.size() << _(" triangles") << endl; }
/* Loads an binary STL file by filename * Returns 0 on success and -1 on failure */ int Shape::loadBinarySTL(string filename) { // if(getFileType(filename) != BINARY_STL) { // return -1; // } triangles.clear(); Min.x = Min.y = Min.z = numeric_limits<double>::infinity(); Max.x = Max.y = Max.z = -numeric_limits<double>::infinity(); ifstream file; file.open(filename.c_str()); if(file.fail()) { cerr << _("Error: Unable to open stl file - ") << filename << endl; return -1; } /* Binary STL files have a meaningless 80 byte header * followed by the number of triangles */ file.seekg(80, ios_base::beg); unsigned int num_triangles; unsigned char buffer[4]; file.read(reinterpret_cast <char *> (buffer), 4); // Read platform independent 32-bit little-endian int. num_triangles = buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24; triangles.reserve(num_triangles); for(uint i = 0; i < num_triangles; i++) { double a,b,c; a = read_double (file); b = read_double (file); c = read_double (file); Vector3d N(a,b,c); a = read_double (file); b = read_double (file); c = read_double (file); Vector3d Ax(a,b,c); a = read_double (file); b = read_double (file); c = read_double (file); Vector3d Bx(a,b,c); a = read_double (file); b = read_double (file); c = read_double (file); Vector3d Cx(a,b,c); // done in Triangle /* Recalculate normal vector - can't trust an STL file! */ // Vector3d AA=Cx-Ax; // Vector3d BB=Cx-Bx; // N = AA.cross(BB).getNormalized(); /* attribute byte count - sometimes contains face color information but is useless for our purposes */ unsigned short byte_count; file.read(reinterpret_cast <char *> (buffer), 2); byte_count = buffer[0] | buffer[1] << 8; // Repress unused variable warning. (void)&byte_count; Triangle T(Ax,Bx,Cx); //cout << "bin triangle "<< N << ":\n\t" << Ax << "/\n\t"<<Bx << "/\n\t"<<Cx << endl; triangles.push_back(T); } file.close(); CenterAroundXY(); scale_factor = 1.0; scale_factor_x=scale_factor_y=scale_factor_z = 1.0; double vol = volume(); if (vol < 0) { invertNormals(); vol = -vol; } cout << _("Shape has volume ") << vol << " mm^3"<<endl; return 0; }