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 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <random>
#include <cmath>
#include <ctime>
using namespace std;
const double PI = 4.0 * atan( 1.0 );
using Mat = vector< vector<double> >;
mt19937 gen( time( 0 ) );
uniform_real_distribution<double> rnd( 0.0, 1.0 );
//====
struct Pt
{
double x, y, z;
Pt( double x = 0, double y = 0, double z = 0 ) : x( x ), y( y ), z( z ) {}
};
struct Triangle
{
Pt v1, v2, v3;
Triangle( const Pt &v1, const Pt &v2, const Pt &v3 ) : v1( v1 ), v2( v2 ), v3( v3 ) {}
};
struct Polymer
{
double side;
Pt start;
vector< pair<Pt,Pt> > cubes;
Polymer( double side, const Pt &start, const vector< pair<Pt,Pt> > &cubes ) : side( side ), start( start ), cubes( cubes ) {}
};
//====
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 ) ); }
Pt unit ( const Pt &a ){ return a / len( 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 ) // Edge case sin(theta)=0: painful!
{
// Find largest diagonal element and use that column
int i = 0;
if ( R[1][1] > R[i][i] ) i = 1;
if ( R[2][2] > R[i][i] ) i = 2;
axis = Pt( R[0][i], R[1][i], R[2][i] );
if ( i == 0 ) axis.x += 1;
if ( i == 1 ) axis.y += 1;
if ( i == 2 ) axis.z += 1;
// Note: axis must be normalised below; it currently has length 2n_i
}
else
{
// Antisymmetric part should determine axis without problems
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 ); // Normalise to length 1
}
//====
//====
class Stl;
class Shape
{
public:
virtual void addToPlot( Stl &stl ) = 0;
};
//====
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 );
}
//====
void plot( const string &filename, const Polymer &P )
{
Stl stl;
Pt start = P.start;
for ( auto &C : P.cubes )
{
Pt side1 = C.first; // axis
Pt side2 = C.second; // perpendicular edge
Pt side3 = cross( side1, side2 ); // other perpendicular edge
side1 = P.side * unit( side1 );
side2 = P.side * unit( side2 );
side3 = P.side * unit( side3 );
Pt centre = start + 0.5 * side1;
Cuboid cub( centre, side1, side2, side3 );
stl.add( &cub );
start = start + side1;
}
stl.draw( filename );
}
//====
int main()
{
const int Ncubes = 100;
const double delta = ( 180 - 110 ) * PI / 180.0;
Pt ex = { 1, 0, 0 }, ey = { 0, 1, 0 };
Polymer P( 1.0, { 0, 0, 0 }, vector< pair<Pt,Pt> >{ { ex, ey } } );
for ( int i = 1; i < Ncubes; i++ )
{
Pt old = P.cubes.back().first; // previous axis
Pt perp = cross( old, ex ); // random perpendicular axis
if ( len( perp ) < 0.01 ) perp = cross( old, ey );
Mat R = rotationMatrix( old, rnd( gen ) * 2 * PI );
perp = R * perp;
old = old / len( old ); perp = perp / len( perp ); // normalise
Pt axis = cos( delta ) * old + sin( delta ) * perp; // new axis
Pt edge = sin( delta ) * old - cos( delta ) * perp;
R = rotationMatrix( axis, rnd( gen ) * 2 * PI );
edge = R * edge; // new edge
P.cubes.push_back( { axis, edge } );
}
plot( "stl.stl", P );
}
|