15 unsigned int num_vertices;
18 Matrix<T> norm_x, norm_y, norm_z, norm_mod;
20 std::vector<Matrix<T>> GSI_prop;
21 std::vector<Matrix<T>> surface_prop;
22 std::vector<const char*> GSI_prop_names;
23 std::vector<const char*> surface_prop_names;
61 void shift(T* offset);
83 void rotate(
int axis, T angle);
164 Matrix<T>& get_p1_x() {
return this->p1[0]; }
165 Matrix<T>& get_p1_y() {
return this->p1[1]; }
166 Matrix<T>& get_p1_z() {
return this->p1[2]; }
167 Matrix<T>& get_p2_x() {
return this->p2[0]; }
168 Matrix<T>& get_p2_y() {
return this->p2[1]; }
169 Matrix<T>& get_p2_z() {
return this->p2[2]; }
170 Matrix<T>& get_p3_x() {
return this->p3[0]; }
171 Matrix<T>& get_p3_y() {
return this->p3[1]; }
172 Matrix<T>& get_p3_z() {
return this->p3[2]; }
173 Matrix<T>& get_norm_x() {
return this->norm_x; }
174 Matrix<T>& get_norm_y() {
return this->norm_y; }
175 Matrix<T>& get_norm_z() {
return this->norm_z; }
242std::ifstream open_binary_file(
const char* filename) {
243 return std::ifstream{filename, std::ifstream::in | std::ifstream::binary};
254char* as_char_ptr(T* pointer) {
255 return reinterpret_cast<char*
>(pointer);
266void read_binary_value(std::ifstream& in, T* dst) {
267 in.read(as_char_ptr(dst),
sizeof(T));
279void read_binary_array(std::ifstream& in, T* dst,
size_t array_length) {
280 size_t n_bytes = array_length *
sizeof(T);
281 in.read(as_char_ptr(dst), n_bytes);
303 float p1_local[4], p2_local[4], p3_local[4], normal[4];
307 auto in = open_binary_file(filename.c_str());
310 read_binary_array<char>(in, header, 80);
312 read_binary_value<unsigned int>(in, &num_vertices);
331 for (
auto i = 0; i < num_vertices; i++) {
332 read_binary_array<float>(in, normal, 3);
333 read_binary_array<float>(in, p1_local, 3);
334 read_binary_array<float>(in, p2_local, 3);
335 read_binary_array<float>(in, p3_local, 3);
339 p1[0](0, i) =
static_cast<T
>(p1_local[0]);
340 p1[1](0, i) =
static_cast<T
>(p1_local[1]);
341 p1[2](0, i) =
static_cast<T
>(p1_local[2]);
342 p2[0](0, i) =
static_cast<T
>(p2_local[0]);
343 p2[1](0, i) =
static_cast<T
>(p2_local[1]);
344 p2[2](0, i) =
static_cast<T
>(p2_local[2]);
345 p3[0](0, i) =
static_cast<T
>(p3_local[0]);
346 p3[1](0, i) =
static_cast<T
>(p3_local[1]);
347 p3[2](0, i) =
static_cast<T
>(p3_local[2]);
349 norm_x(0, i) =
static_cast<T
>(normal[0]);
350 norm_y(0, i) =
static_cast<T
>(normal[1]);
351 norm_z(0, i) =
static_cast<T
>(normal[2]);
354 norm_mod(0, i) = std::sqrt(norm_x(0, i) * norm_x(0, i) + norm_y(0, i) * norm_y(0, i) + norm_z(0, i) * norm_z(0, i));
355 norm_x(0, i) /= norm_mod(0, i);
356 norm_y(0, i) /= norm_mod(0, i);
357 norm_z(0, i) /= norm_mod(0, i);
383 T bb_x_1, bb_x_2, bb_y_1, bb_y_2, bb_z_1, bb_z_2;
386 bb_x_1 = std::min(p1[0].min(), std::min(p2[0].min(), p3[0].min()));
387 bb_x_2 = std::max(p1[0].max(), std::max(p2[0].max(), p3[0].max()));
388 bb_y_1 = std::min(p1[1].min(), std::min(p2[1].min(), p3[1].min()));
389 bb_y_2 = std::max(p1[1].max(), std::max(p2[1].max(), p3[1].max()));
390 bb_z_1 = std::min(p1[2].min(), std::min(p2[2].min(), p3[2].min()));
391 bb_z_2 = std::max(p1[2].max(), std::max(p2[2].max(), p3[2].max()));
394 bounding_box(0, 0) = bb_x_1;
395 bounding_box(1, 0) = bb_x_2;
396 bounding_box(2, 0) = bb_y_1;
397 bounding_box(3, 0) = bb_y_2;
398 bounding_box(4, 0) = bb_z_1;
399 bounding_box(5, 0) = bb_z_2;
412 center(0) = (bounding_box(0, 0) + bounding_box(1, 0)) / 2.0;
413 center(1) = (bounding_box(3, 0) + bounding_box(2, 0)) / 2.0;
414 center(2) = (bounding_box(5, 0) + bounding_box(4, 0)) / 2.0;
443 bounding_box(0, 0) += offset[0];
444 bounding_box(1, 0) += offset[0];
445 bounding_box(2, 0) += offset[1];
446 bounding_box(3, 0) += offset[1];
447 bounding_box(4, 0) += offset[2];
448 bounding_box(5, 0) += offset[2];
475 bounding_box(0, 0) += offset(0);
476 bounding_box(1, 0) += offset(0);
477 bounding_box(2, 0) += offset(1);
478 bounding_box(3, 0) += offset(1);
479 bounding_box(4, 0) += offset(2);
480 bounding_box(5, 0) += offset(2);
506 bounding_box *= factor;
522 Matrix<T> p1_y_new = (p1[1] * cos(angle) + p1[2] * sin(angle));
523 Matrix<T> p1_z_new = (p1[2] * cos(angle) - p1[1] * sin(angle));
524 Matrix<T> p2_y_new = (p2[1] * cos(angle) + p2[2] * sin(angle));
525 Matrix<T> p2_z_new = (p2[2] * cos(angle) - p2[1] * sin(angle));
526 Matrix<T> p3_y_new = (p3[1] * cos(angle) + p3[2] * sin(angle));
527 Matrix<T> p3_z_new = (p3[2] * cos(angle) - p3[1] * sin(angle));
529 Matrix<T> norm_y_new = norm_y * cos(angle) + norm_z * sin(angle);
530 Matrix<T> norm_z_new = norm_z * cos(angle) - norm_y * sin(angle);
533 p1[1] = p1_y_new; p1[2] = p1_z_new;
534 p2[1] = p2_y_new; p2[2] = p2_z_new;
535 p3[1] = p3_y_new; p3[2] = p3_z_new;
536 norm_y = norm_y_new; norm_z = norm_z_new;
538 compute_bounding_box();
539 }
else if (axis == 1) {
541 Matrix<T> p1_x_new = p1[0] * cos(angle) - p1[2] * sin(angle);
542 Matrix<T> p1_z_new = p1[0] * sin(angle) + p1[2] * cos(angle);
543 Matrix<T> p2_x_new = p2[0] * cos(angle) - p2[2] * sin(angle);
544 Matrix<T> p2_z_new = p2[0] * sin(angle) + p2[2] * cos(angle);
545 Matrix<T> p3_x_new = p3[0] * cos(angle) - p3[2] * sin(angle);
546 Matrix<T> p3_z_new = p3[0] * sin(angle) + p3[2] * cos(angle);
548 Matrix<T> norm_x_new = (norm_x * cos(angle) - norm_z * sin(angle));
549 Matrix<T> norm_z_new = (norm_x * sin(angle) + norm_z * cos(angle));
552 p1[0] = p1_x_new; p1[2] = p1_z_new;
553 p2[0] = p2_x_new; p2[2] = p2_z_new;
554 p3[0] = p3_x_new; p3[2] = p3_z_new;
555 norm_x = norm_x_new; norm_z = norm_z_new;
557 compute_bounding_box();
558 }
else if (axis == 2) {
560 Matrix<T> p1_x_new = p1[0] * cos(angle) + p1[1] * sin(angle);
561 Matrix<T> p1_y_new = p1[1] * cos(angle) - p1[0] * sin(angle);
562 Matrix<T> p2_x_new = p2[0] * cos(angle) + p2[1] * sin(angle);
563 Matrix<T> p2_y_new = p2[1] * cos(angle) - p2[0] * sin(angle);
564 Matrix<T> p3_x_new = p3[0] * cos(angle) + p3[1] * sin(angle);
565 Matrix<T> p3_y_new = p3[1] * cos(angle) - p3[0] * sin(angle);
567 Matrix<T> norm_x_new = norm_x * cos(angle) + norm_y * sin(angle);
568 Matrix<T> norm_y_new = norm_y * cos(angle) - norm_x * sin(angle);
571 p1[0] = p1_x_new; p1[1] = p1_y_new;
572 p2[0] = p2_x_new; p2[1] = p2_y_new;
573 p3[0] = p3_x_new; p3[1] = p3_y_new;
574 norm_x = norm_x_new; norm_y = norm_y_new;
576 compute_bounding_box();
578 std::cout <<
"The axis must be either 0, 1, or 2!\n";
592 return 0.5 * fabs(p1[0](index) * (p2[1](index) * p3[2](index) - p3[1](index) * p2[2](index)) +
593 p1[1](index) * (p3[0](index) * p2[2](index) - p2[0](index) * p3[2](index)) +
594 p1[2](index) * (p2[0](index) * p3[1](index) - p3[0](index) * p2[1](index)));
609 GSI_prop.push_back(property);
610 GSI_prop_names.push_back(property_name);
623 GSI_prop.push_back(values);
624 GSI_prop_names.push_back(property_name);
639 surface_prop.push_back(property);
640 surface_prop_names.push_back(property_name);
653 surface_prop.push_back(values);
654 surface_prop_names.push_back(property_name);
665 std::cout <<
"STL file with " << num_vertices <<
" triangles:\n\n";
668 for (
auto i = 0; i < num_vertices; i++) {
669 std::cout <<
"Triangle " << i <<
":\n";
670 std::cout <<
"p1: [" << p1[0](i) <<
" " << p1[1](i) <<
" " << p1[2](i) <<
"]\n";
671 std::cout <<
"p2: [" << p2[0](i) <<
" " << p2[1](i) <<
" " << p2[2](i) <<
"]\n";
672 std::cout <<
"p3: [" << p3[0](i) <<
" " << p3[1](i) <<
" " << p3[2](i) <<
"]\n";
675 std::cout <<
"normal: [" << norm_x(i) <<
" " << norm_y(i) <<
" " << norm_z(i) <<
"]\n";
678 std::cout <<
"GSI properties names: ";
679 for (
auto j = 0; j < GSI_prop_names.size(); j++) {
680 std::cout << GSI_prop_names[j] <<
" ";
684 std::cout <<
"\nGSI properties values: ";
685 for (
auto j = 0; j < GSI_prop.size(); j++) {
686 std::cout << GSI_prop[j](i) <<
" ";
690 std::cout <<
"\nSurface properties names: ";
691 for (
auto j = 0; j < surface_prop_names.size(); j++) {
692 std::cout << surface_prop_names[j] <<
" ";
696 std::cout <<
"\nSurface properties values: ";
697 for (
auto j = 0; j < surface_prop.size(); j++) {
698 std::cout << surface_prop[j](i) <<
" ";
Class representing a geometric structure composed of vertices, normals, and properties.
Definition Geometry.h:11
std::vector< Matrix< T > > & get_GSI_properties()
Get all GSI properties.
Definition Geometry.h:212
Matrix< T > get_center()
Get the center of the bounding box of the geometry.
Definition Geometry.h:408
void print()
Print the geometry details, including vertices, normals, and properties.
Definition Geometry.h:664
void rotate(int axis, T angle)
Rotate the geometry around a specific axis.
Definition Geometry.h:519
void shift(T *offset)
Shift the geometry by an offset.
Definition Geometry.h:428
void add_surface_property(const char *property_name, T value)
Add a scalar surface property to the geometry.
Definition Geometry.h:636
void compute_bounding_box()
Compute the bounding box for the geometry.
Definition Geometry.h:381
std::vector< const char * > & get_surface_property_names()
Get all surface property names.
Definition Geometry.h:233
void set_c_ref(T c_ref)
Set the reference speed for the geometry.
Definition Geometry.h:129
Geometry()
Default constructor for Geometry class.
Definition Geometry.h:290
void add_GSI_property(const char *property_name, T value)
Add a scalar GSI property to the geometry.
Definition Geometry.h:606
Matrix< T > & get_GSI_property(int index)
Get a GSI property by index.
Definition Geometry.h:183
T get_c_ref()
Get the reference speed.
Definition Geometry.h:148
std::vector< Matrix< T > > & get_surface_properties()
Get all surface properties.
Definition Geometry.h:226
Matrix< T > get_bounding_box()
Get the bounding box of the geometry.
Definition Geometry.h:712
char * get_GSI_property_name(int index)
Get the name of a GSI property by index.
Definition Geometry.h:191
T get_A_ref()
Get the reference area.
Definition Geometry.h:141
T compute_area(unsigned int index)
Compute the area of a specific triangle in the geometry.
Definition Geometry.h:591
void set_A_ref(T A_ref)
Set the reference area for the geometry.
Definition Geometry.h:122
void scale(T factor)
Scale the geometry by a factor.
Definition Geometry.h:491
std::vector< const char * > & get_GSI_property_names()
Get all GSI property names.
Definition Geometry.h:219
~Geometry()
Destructor for Geometry class.
Definition Geometry.h:370
unsigned int get_num_vertices() const
Get the number of vertices (triangles) in the geometry.
Definition Geometry.h:54
This class implements a matrix object used for linear algebra and vectorized operations.
Definition Matrix.h:25