1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
using Mat = vector< vector<double> >;
const double PI = 4.0 * atan( 1.0 );
//======================================================================
class Stl;
//----------------------------
class Shape
{
public:
virtual void addToPlot( Stl &stl ) = 0;
};
//======================================================================
struct Vec
{
double x, y, z;
Vec( double x = 0, double y = 0, double z = 0 ) : x( x ), y( y ), z( z ) {}
};
//----------------------------
Vec operator + ( const Vec &a, const Vec &b ){ return { a.x + b.x, a.y + b.y, a.z + b.z }; }
Vec operator - ( const Vec &a, const Vec &b ){ return { a.x - b.x, a.y - b.y, a.z - b.z }; }
Vec operator / ( const Vec &a, double d ){ return { a.x / d, a.y / d, a.z / d }; }
Vec operator * ( double d , const Vec &a ){ return { d * a.x, d * a.y, d * a.z }; }
Vec cross ( const Vec &a, const Vec &b ){ return { a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x }; }
double dot ( const Vec &a, const Vec &b ){ return a.x * b.x + a.y * b.y + a.z * b.z; }
double normsq ( const Vec &a ){ return dot( a, a ); }
double len ( const Vec &a ){ return sqrt( normsq( a ) ); }
ostream & operator << ( ostream &out, const Vec a ){ return out << a.x << " " << a.y << " " << a.z << " "; }
Vec operator * ( const Mat &M, const Vec &v )
{
return { M[0][0] * v.x + M[0][1] * v.y + M[0][2] * v.z,
M[1][0] * v.x + M[1][1] * v.y + M[1][2] * v.z,
M[2][0] * v.x + M[2][1] * v.y + M[2][2] * v.z };
}
Mat rotationMatrix( const Vec &axis, double radians )
{
Mat result( 3, vector<double>( 3, 0.0 ) );
Vec n = axis / len( axis );
double cosA = cos( radians ), sinA = sin( radians ), cosA1 = 1.0 - cosA;
result[0][0] = cosA + n.x * n.x * cosA1;
result[0][1] = + n.x * n.y * cosA1 - n.z * sinA;
result[0][2] = + n.x * n.z * cosA1 + n.y * sinA;
result[1][0] = + n.y * n.x * cosA1 + n.z * sinA;
result[1][1] = cosA + n.y * n.y * cosA1;
result[1][2] = + n.y * n.z * cosA1 - n.x * sinA;
result[2][0] = + n.z * n.x * cosA1 - n.y * sinA;
result[2][1] = + n.z * n.y * cosA1 + n.x * sinA;
result[2][2] = cosA + n.z * n.z * cosA1;
return result;
}
//======================================================================
struct Triangle
{
Vec v1, v2, v3;
Triangle( const Vec &v1, const Vec &v2, const Vec &v3 ) : v1( v1 ), v2( v2 ), v3( v3 ) {}
};
//======================================================================
class Stl
{
vector<Triangle> triangles;
public:
void add( Shape *S ) { S->addToPlot( *this ); }
void addTriangle( const Vec &v1, const Vec &v2, const Vec &v3 ) { triangles.push_back( Triangle( v1, v2, v3 ) ); }
void addRectangle( const Vec &v1, const Vec &v2, const Vec &v3, const Vec &v4 ) { addTriangle( v1, v2, v3 ); addTriangle( v1, v3, v4 ); }
void insert( const Stl &stl ) { triangles.insert( triangles.end(), stl.triangles.begin(), stl.triangles.end() ); }
void translate( const Vec &v ) { for ( Triangle &t : triangles ) t = Triangle( t.v1 + v, t.v2 + v, t.v3 + v ); }
void scale( double s ) { for ( Triangle &t : triangles ) t = Triangle( s * t.v1, s * t.v2, s * t.v3 ); }
void scale( double s, const Vec ¢re ) { translate( -1 * centre ); scale( s ); translate( centre ); }
void rotate( const Mat &M ) { for ( Triangle &t : triangles ) t = Triangle( M * t.v1, M * t.v2, M * t.v3 ); }
void rotate( const Mat &M, const Vec ¢re ) { translate( -1 * centre ); rotate( M ); translate( centre ); }
void draw( const string &filename );
};
//----------------------------
void Stl::draw( const string &filename )
{
ofstream out( filename );
out << "solid\n";
for ( Triangle t : triangles )
{
Vec n = cross( t.v2 - t.v1, t.v3 - t.v1 );
n = n / len( n ); // unit normal vector;
out << " facet normal " << n << '\n';
out << " outer loop\n";
out << " vertex " << t.v1 << '\n';
out << " vertex " << t.v2 << '\n';
out << " vertex " << t.v3 << '\n';
out << " endloop\n";
out << " endfacet\n";
}
out << "endsolid\n";
}
//======================================================================
class Cuboid : public Shape
{
Vec centre; // centre
Vec side1, side2, side3; // side vectors
public:
Cuboid( const Vec &c, double L ) : centre( c ), side1( Vec( L, 0, 0 ) ), side2( Vec( 0, L, 0 ) ), side3( Vec( 0, 0, L ) ) {}
Cuboid( const Vec &c, const Vec &s1, const Vec &s2, const Vec &s3 ) : centre( c ), side1( s1 ), side2( s2 ), side3( s3 ) {}
void addToPlot( Stl &stl );
};
//----------------------------
void Cuboid::addToPlot( Stl &stl )
{
Vec v1 = centre - 0.5 * ( side1 + side2 + side3 );
Vec v2 = v1 + side1, v3 = v2 + side2, v4 = v3 - side1;
Vec v5 = v1 + side3, v6 = v2 + side3, v7 = v3 + side3, v8 = v4 + side3;
stl.addRectangle( v1, v2, v6, v5 );
stl.addRectangle( v2, v3, v7, v6 );
stl.addRectangle( v3, v4, v8, v7 );
stl.addRectangle( v4, v1, v5, v8 );
stl.addRectangle( v1, v4, v3, v2 );
stl.addRectangle( v5, v6, v7, v8 );
}
//======================================================================
class Cylinder : public Shape
{
Vec centre; // centre (NOT base centre)
Vec side1, side2, side3; // vectors along radius, along radius perpendicular, along axis
int nface; // number of rectangular faces
public:
Cylinder( const Vec &c, double r, double h, int n = 30 )
: centre( c ), side1( Vec( r, 0, 0 ) ), side2( Vec( 0, r, 0 ) ), side3( Vec( 0, 0, h ) ), nface( n ) {}
Cylinder( const Vec &c, const Vec &s1, const Vec &s3, int n = 30 )
: centre( c ), side1( s1 ), side2( cross( s3, s1 ) / len( s3 ) ), side3( s3 ), nface( n ) {}
void addToPlot( Stl &stl );
};
//----------------------------
void Cylinder::addToPlot( Stl &stl )
{
Vec bottom = centre - 0.5 * side3; // centre of base
Vec top = bottom + side3; // centre of top
double dtheta = 2.0 * PI / nface;
Vec v1, v2, v3, v4;
v2 = bottom + side1;
v3 = v2 + side3;
for ( int n = 1; n <= nface; n++ )
{
double theta = n * dtheta;
v1 = v2;
v4 = v3;
v2 = bottom + cos( theta ) * side1 + sin( theta ) * side2;
v3 = v2 + side3;
stl.addRectangle( v1, v2, v3, v4 ); // add sides as a series of rectangles
stl.addTriangle( v2, v1, bottom ); // add triangles for bottom
stl.addTriangle( v4, v3, top ); // add triangles for top
}
}
//======================================================================
int main()
{
Stl stl;
for ( int i = 1; i <= 4; i++ ) // Create a line of cylinders and cubes
{
Cylinder cyl( 2 * i, 0.4, 0.6 );
Cuboid cub( Vec( 2 * i - 1, 0, 0 ), 0.6 );
stl.add( &cyl );
stl.add( &cub );
}
// Duplicate and rotate
Stl extra = stl;
Vec axis( 0.0, 0.0, 1.0 );
double degrees = 135.0;
Mat R = rotationMatrix( axis, degrees * PI / 180.0 );
extra.rotate( R );
// Add to original
stl.insert( extra );
// Output to file
stl.draw( "stl.stl" );
}
|