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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
#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 Pt
{
double x, y, z;
Pt( double x = 0, double y = 0, double z = 0 ) : x( x ), y( y ), z( z ) {}
};
//---
Pt operator + ( const Pt &a, const Pt &b ){ return { a.x + b.x, a.y + b.y, a.z + b.z }; }
Pt operator - ( const Pt &a, const Pt &b ){ return { a.x - b.x, a.y - b.y, a.z - b.z }; }
Pt operator / ( const Pt &a, double d ){ return { a.x / d, a.y / d, a.z / d }; }
Pt operator * ( double d , const Pt &a ){ return { d * a.x, d * a.y, d * a.z }; }
Pt cross ( const Pt &a, const Pt &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 Pt &a, const Pt &b ){ return a.x * b.x + a.y * b.y + a.z * b.z; }
double normsq ( const Pt &a ){ return dot( a, a ); }
double len ( const Pt &a ){ return sqrt( normsq( a ) ); }
ostream & operator << ( ostream &out, const Pt a ){ return out << a.x << " " << a.y << " " << a.z << " "; }
Pt operator * ( const Mat &M, const Pt &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 operator * ( const Mat &M, const Mat &N )
{
Mat R( M.size(), vector<double>( N[0].size(), 0.0 ) );
for ( int i = 0; i < M.size(); i++ )
{
for ( int j = 0; j < N[0].size(); j++ )
{
for ( int k = 0; k < N.size(); k++ ) R[i][j] += M[i][k] * N[k][j];
}
}
return R;
}
//====
Mat rotationMatrix( const Pt &axis, double radians )
{
Mat result( 3, vector<double>( 3, 0.0 ) );
Pt 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;
}
//---
void axisAngle( const Mat &R, Pt &axis, double &angle )
{
angle = acos( 0.5 * ( R[0][0] + R[1][1] + R[2][2] - 1 ) );
double s = sin( angle );
if ( abs( s ) < 1.0e-6 ) axis = Pt( 1.0, 0.0, 0.0 );
else axis = ( 0.5 / s ) * Pt( R[2][1] - R[1][2], R[0][2] - R[2][0], R[1][0] - R[0][1] );
axis = axis / len( axis );
}
//====
struct Triangle
{
Pt v1, v2, v3;
Triangle( const Pt &v1, const Pt &v2, const Pt &v3 ) : v1( v1 ), v2( v2 ), v3( v3 ) {}
};
//====
class Stl
{
vector<Triangle> triangles;
public:
void add( Shape *S ) { S->addToPlot( *this ); }
void addTriangle( const Pt &v1, const Pt &v2, const Pt &v3 ) { triangles.push_back( Triangle( v1, v2, v3 ) ); }
void addRectangle( const Pt &v1, const Pt &v2, const Pt &v3, const Pt &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 Pt &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 Pt ¢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 Pt ¢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 )
{
Pt n = cross( t.v2 - t.v1, t.v3 - t.v1 );
n = n / len( n );
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
{
Pt centre;
Pt side1, side2, side3;
public:
Cuboid( const Pt &c, double L ) : centre( c ), side1( Pt( L, 0, 0 ) ), side2( Pt( 0, L, 0 ) ), side3( Pt( 0, 0, L ) ) {}
Cuboid( const Pt &c, const Pt &s1, const Pt &s2, const Pt &s3 ) : centre( c ), side1( s1 ), side2( s2 ), side3( s3 ) {}
void addToPlot( Stl &stl );
};
//---
void Cuboid::addToPlot( Stl &stl )
{
Pt v1 = centre - 0.5 * ( side1 + side2 + side3 );
Pt v2 = v1 + side1, v3 = v2 + side2, v4 = v3 - side1;
Pt 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 Polymer
{
public:
double side;
Pt start;
vector< pair<Pt,double> > cubes;
Polymer( double side, const Pt &start, const vector< pair<Pt,double> > &cubes ) : side( side ), start( start ), cubes( cubes ) {}
void rotate( const Pt ¢re, const Pt &axis, double angle );
};
//---
void Polymer::rotate( const Pt ¢re, const Pt &axis, double angle )
{
Mat M = rotationMatrix( axis, angle );
start = centre + M * ( start - centre );
for ( auto &C : cubes )
{
Mat P = rotationMatrix( C.first, C.second );
Mat R = M * P;
axisAngle( R, C.first, C.second );
}
}
//====
int main()
{
Polymer pb1( 1.0,
{ 10.0, 15.0, 5.0 },
{ { { -0.79948 , 0.504813 , 0.325568 }, 0.0888429 },
{ { -0.941624 , 0.0857279, 0.325568 }, 6.06663 },
{ { -0.940697 , -0.0953601, 0.325568 }, 0.454249 },
{ { 0.945361 , -0.017274 , 0.325568 }, 0.932644 },
{ { 0.936006 , -0.133785 , 0.325568 }, 0.585938 },
{ { 0.0287705, 0.945081 , 0.325568 }, 6.16575 },
{ { -0.190289 , -0.926173 , 0.325568 }, 1.48524 },
{ { -0.922358 , -0.207991 , 0.325568 }, 2.83318 },
{ { -0.0299494, 0.945044 , 0.325568 }, 2.60525 } } );
Polymer pb2 = pb1;
Pt reference = { 0.0, 0.0, 0.0 };
Pt axis = { 1.0, 0.0, 0.0 };
double angle = PI / 2.0;
pb2.rotate( reference, axis, angle );
// Add both polymers to plot
Stl stl;
Pt ex = { 1, 0, 0 }, ey = { 0, 1, 0 }, ez = { 0, 0, 1 };
for ( Polymer P : { pb1, pb2 } )
{
Pt start = P.start;
for ( auto &C : P.cubes )
{
Mat R = rotationMatrix( C.first, C.second );
Pt side1 = P.side * ( R * ex );
Pt side2 = P.side * ( R * ey );
Pt side3 = P.side * ( R * ez );
Pt centre = start + 0.5 * side3;
Cuboid cub( centre, side1, side2, side3 );
stl.add( &cub );
start = start + side3;
}
}
stl.draw( "stl.stl" );
}
|