-
Notifications
You must be signed in to change notification settings - Fork 31
/
geometry.hpp
279 lines (204 loc) · 6.45 KB
/
geometry.hpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifndef MCNP2CAD_GEOMETRY_H
#define MCNP2CAD_GEOMETRY_H
#include <vector>
#include <iosfwd>
#include <cmath>
#include <cstdlib>
#include "dataref.hpp"
// under visual studio, the following macro must be defined
// in order to get M_PI and friends from the cmath header.
#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#endif
#include <math.h>
class Vector3d{
public:
double v[3];
Vector3d(){
v[2] = v[1] = v[0] = 0;
}
Vector3d( const double p[3] ){
v[0] = p[0];
v[1] = p[1];
v[2] = p[2];
}
Vector3d( double x, double y, double z ){
v[0] = x;
v[1] = y;
v[2] = z;
}
Vector3d( const std::vector<double>& p, int idx = 0 ){
v[0] = p.at(idx+0);
v[1] = p.at(idx+1);
v[2] = p.at(idx+2);
}
Vector3d( const Vector3d& other ) {
v[0] = other.v[0];
v[1] = other.v[1];
v[2] = other.v[2];
}
double length() const{
return sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
}
Vector3d normalize() const {
double length = this->length();
return Vector3d( v[0]/length, v[1]/length, v[2]/length );
}
Vector3d& operator=( const Vector3d& other) {
v[0] = other.v[0];
v[1] = other.v[1];
v[2] = other.v[2];
return *this;
}
Vector3d operator-() const {
return Vector3d(-v[0], -v[1], -v[2]);
}
Vector3d reverse() const {
return -(*this);
}
Vector3d scale( double d ) const {
return Vector3d(v[0]*d, v[1]*d, v[2]*d);
}
Vector3d operator*( double d ) const {
return scale(d);
}
Vector3d add( const Vector3d& alt ) const {
return Vector3d( v[0]+alt.v[0], v[1]+alt.v[1], v[2]+alt.v[2] );
}
Vector3d operator+( const Vector3d& alt ) const {
return add(alt);
}
double dot( const Vector3d& alt ) const {
return v[0]*alt.v[0] + v[1]*alt.v[1] + v[2]*alt.v[2];
}
Vector3d cross( const Vector3d& alt ) const {
Vector3d c;
c.v[0] = v[1]*alt.v[2] - v[2]*alt.v[1];
c.v[1] = v[2]*alt.v[0] - v[0]*alt.v[2];
c.v[2] = v[0]*alt.v[1] - v[1]*alt.v[0];
return c;
}
Vector3d rotate_about( const Vector3d& v_p, double theta_p, bool degrees = true ) const {
Vector3d v = v_p.normalize();
double theta = theta_p;
if( degrees ) { theta *= M_PI / 180.0; }
double cos_t = cos( theta );
Vector3d ret = scale(cos_t) + v.scale((1.0-cos_t)*(dot(v))) + cross(v).scale( sin(theta) );
return ret;
}
/// project v onto this vector, return the result
Vector3d projection( const Vector3d& v ) const {
return scale( this->dot(v) / this->dot(*this) );
}
};
std::ostream& operator<<( std::ostream& str, const Vector3d& v );
//std::string to_string( const Vector3d& v );
// determinant of 3x3 matrix (C-style matrix ordering)
double matrix_det( double mat[9] );
class Transform{
public:
enum mat_format{ C_STYLE, FORTRAN_STYLE };
protected:
Vector3d translation;
bool has_rot;
double theta; Vector3d axis;
bool invert;
void set_rots_from_matrix( double raw_matrix[9], enum mat_format );
public:
Transform():translation(),has_rot(false),invert(false){}
Transform( const Vector3d& v ):translation(v),has_rot(false),invert(false){}
Transform( const std::vector< double >& inputs, bool degree_format_p = false, enum mat_format = FORTRAN_STYLE );
Transform( double rot[9], const Vector3d& trans, enum mat_format = C_STYLE );
const Vector3d& getTranslation() const { return translation; }
void modify_translation( const Vector3d& translation_addition );
void modify_translation( const Vector3d& translation_addition ) const;
bool hasRot() const{ return has_rot; }
bool hasInversion() const{ return invert; }
double getTheta() const { return theta; }
const Vector3d& getAxis() const { return axis; }
void print( std::ostream& str ) const;
Transform reverse() const;
};
// generate a full transformation matrix
std::vector<double> get_matrix(const Transform& t);
Transform combine(const Transform& a, const Transform& b);
std::ostream& operator<<(std::ostream& str, const Transform& t );
/** A universe and a transformation */
class FillNode {
protected:
int universe;
DataRef<Transform>* tr;
public:
FillNode():
universe(0), tr(new NullRef<Transform>())
{}
FillNode( int universe_p ):
universe(universe_p), tr(new NullRef<Transform>())
{}
FillNode( int universe_p, DataRef<Transform>* tr_p ):
universe(universe_p), tr(tr_p)
{}
FillNode( const FillNode& node_p ):
universe(node_p.universe), tr(node_p.tr->clone())
{}
FillNode& operator=( const FillNode& node_p ){
if( this != &node_p ){
universe = node_p.universe;
tr = node_p.tr->clone();
}
return *this;
}
~FillNode(){
delete tr;
}
int getFillingUniverse() const { return universe; }
bool hasTransform() const{ return tr->hasData();}
const Transform& getTransform() const { return tr->getData(); }
void setTransform( DataRef<Transform>* tr_p ){
delete tr;
tr = tr_p;
}
};
typedef std::pair<int,int> irange;
/** A fill is a 3-dimensional grid of fill nodes */
class Fill{
protected:
std::vector<FillNode> nodes;
bool has_grid;
irange xrange, yrange, zrange;
size_t indicesToSerialIndex( int x, int y, int z ) const ;
public:
Fill():
nodes(1,FillNode()),has_grid(false)
{}
Fill( const FillNode& origin_p ):
nodes(1,origin_p), has_grid(false)
{}
Fill( irange x, irange y, irange z, std::vector<FillNode> nodes_p ):
nodes(nodes_p), has_grid(true), xrange(x), yrange(y), zrange(z)
{}
const FillNode& getOriginNode() const;
const FillNode& getNode( int x, int y, int z ) const;
friend class Lattice;
};
class Lattice{
protected:
int num_finite_dims;
Vector3d v1, v2, v3;
DataRef<Fill> *fill;
public:
Lattice() : fill(new NullRef<Fill>()){}
Lattice( int dims, const Vector3d& v1_p, const Vector3d& v2_p, const Vector3d& v3_p, const FillNode& singleton_fill );
Lattice( int dims, const Vector3d& v1_p, const Vector3d& v2_p, const Vector3d& v3_p, const Fill& full_fill );
Lattice( const Lattice& l );
Lattice& operator=( const Lattice& l );
~Lattice(){ delete fill; }
int numFiniteDirections() const { return num_finite_dims; }
Transform getTxForNode( int x, int y, int z ) const ;
const FillNode& getFillForNode( int x, int y, int z ) const ;
bool isFixedSize() const { return fill->getData().has_grid; }
irange getXRange() const { return fill->getData().xrange; }
irange getYRange() const { return fill->getData().yrange; }
irange getZRange() const { return fill->getData().zrange; }
};
#endif /* MCNP2CAD_GEOMETRY_H */