OpenLB 1.7
Loading...
Searching...
No Matches
Public Member Functions | List of all members
olb::STLmesh< T > Class Template Reference

#include <stlReader.h>

+ Collaboration diagram for olb::STLmesh< T >:

Public Member Functions

 STLmesh (std::string, T stlSize=1.)
 Constructs a new STLmesh from a file.
 
 STLmesh (const std::vector< std::vector< T > > meshPoints, T stlSize=1.)
 Constructs a new STLmesh from a file.
 
STLtriangle< T > & getTri (unsigned int i)
 Returns reference to a triangle.
 
std::vector< STLtriangle< T > > & getTriangles ()
 Returns reference to all triangles.
 
unsigned int triangleSize () const
 Returns number of triangles.
 
Vector< T, 3 > & getMin ()
 Returns _min.
 
Vector< T, 3 > & getMax ()
 Returns _max.
 
float maxDist2 () const
 Returns maxDist squared.
 
void print (bool full=false)
 Prints console output.
 
void write (std::string fName)
 Writes STL mesh in Si units.
 
bool testRayIntersect (const std::set< unsigned int > &tris, const Vector< T, 3 > &pt, const Vector< T, 3 > &dir, Vector< T, 3 > &q, T &alpha)
 Compute intersection between Ray and set of triangles; returns true if intersection is found.
 

Detailed Description

template<typename T>
class olb::STLmesh< T >

Definition at line 145 of file stlReader.h.

Constructor & Destructor Documentation

◆ STLmesh() [1/2]

template<typename T >
olb::STLmesh< T >::STLmesh ( std::string fName,
T stlSize = 1. )

Constructs a new STLmesh from a file.

Parameters
Filename- Filename
stlSize- Conversion factor for STL (e.g. STL in mm stlSize=10^-3)

Definition at line 318 of file stlReader.hh.

319 : _fName(fName),
320 _min(T()),
321 _max(T()),
322 _maxDist2(0),
323 clout(std::cout, "STLmesh")
324{
325 std::ifstream f(fName.c_str(), std::ios::in);
326 _triangles.reserve(10000);
327 if (!f.good()) {
328 throw std::runtime_error("STL File not valid.");
329 }
330 char buf[6];
331 buf[5] = 0;
332 f.read(buf, 5);
333 const std::string asciiHeader = "solid";
334 if (std::string(buf) == asciiHeader) {
335 f.seekg(0, std::ios::beg);
336 if (f.good()) {
337 std::string s0, s1;
338 int i = 0;
339 while (!f.eof()) {
340 f >> s0;
341 if (s0 == "facet") {
342 STLtriangle<T> tri;
343 f >> s1 >> tri.normal[0] >> tri.normal[1] >> tri.normal[2];
344 f >> s0 >> s1;
345 f >> s0 >> tri.point[0].coords[0] >> tri.point[0].coords[1]
346 >> tri.point[0].coords[2];
347 f >> s0 >> tri.point[1].coords[0] >> tri.point[1].coords[1]
348 >> tri.point[1].coords[2];
349 f >> s0 >> tri.point[2].coords[0] >> tri.point[2].coords[1]
350 >> tri.point[2].coords[2];
351 f >> s0;
352 f >> s0;
353 for (int k = 0; k < 3; k++) {
354 tri.point[0].coords[k] *= stlSize;
355 tri.point[1].coords[k] *= stlSize;
356 tri.point[2].coords[k] *= stlSize;
357 }
358 if (i == 0) {
359 _min = T();
360 _max = T();
361
362 _min[0] = tri.point[0].coords[0];
363 _min[1] = tri.point[0].coords[1];
364 _min[2] = tri.point[0].coords[2];
365
366 _max[0] = tri.point[0].coords[0];
367 _max[1] = tri.point[0].coords[1];
368 _max[2] = tri.point[0].coords[2];
369
370 _min[0] = util::min(_min[0], tri.point[1].coords[0]);
371 _min[1] = util::min(_min[1], tri.point[1].coords[1]);
372 _min[2] = util::min(_min[2], tri.point[1].coords[2]);
373
374 _max[0] = util::max(_max[0], tri.point[1].coords[0]);
375 _max[1] = util::max(_max[1], tri.point[1].coords[1]);
376 _max[2] = util::max(_max[2], tri.point[1].coords[2]);
377
378 _min[0] = util::min(_min[0], tri.point[2].coords[0]);
379 _min[1] = util::min(_min[1], tri.point[2].coords[1]);
380 _min[2] = util::min(_min[2], tri.point[2].coords[2]);
381
382 _max[0] = util::max(_max[0], tri.point[2].coords[0]);
383 _max[1] = util::max(_max[1], tri.point[2].coords[1]);
384 _max[2] = util::max(_max[2], tri.point[2].coords[2]);
385
386 }
387 else {
388 _min[0] = util::min(_min[0], tri.point[0].coords[0]);
389 _min[1] = util::min(_min[1], tri.point[0].coords[1]);
390 _min[2] = util::min(_min[2], tri.point[0].coords[2]);
391
392 _max[0] = util::max(_max[0], tri.point[0].coords[0]);
393 _max[1] = util::max(_max[1], tri.point[0].coords[1]);
394 _max[2] = util::max(_max[2], tri.point[0].coords[2]);
395
396 _min[0] = util::min(_min[0], tri.point[1].coords[0]);
397 _min[1] = util::min(_min[1], tri.point[1].coords[1]);
398 _min[2] = util::min(_min[2], tri.point[1].coords[2]);
399
400 _max[0] = util::max(_max[0], tri.point[1].coords[0]);
401 _max[1] = util::max(_max[1], tri.point[1].coords[1]);
402 _max[2] = util::max(_max[2], tri.point[1].coords[2]);
403
404 _min[0] = util::min(_min[0], tri.point[2].coords[0]);
405 _min[1] = util::min(_min[1], tri.point[2].coords[1]);
406 _min[2] = util::min(_min[2], tri.point[2].coords[2]);
407
408 _max[0] = util::max(_max[0], tri.point[2].coords[0]);
409 _max[1] = util::max(_max[1], tri.point[2].coords[1]);
410 _max[2] = util::max(_max[2], tri.point[2].coords[2]);
411 }
412
413 i++;
414 tri.init();
415 _triangles.push_back(tri);
416
417 _maxDist2 = util::max(distPoints(tri.point[0], tri.point[1]),
418 _maxDist2);
419 _maxDist2 = util::max(distPoints(tri.point[2], tri.point[1]),
420 _maxDist2);
421 _maxDist2 = util::max(distPoints(tri.point[0], tri.point[2]),
422 _maxDist2);
423 }
424 else if (s0 == "endsolid") {
425 break;
426 }
427 }
428 }
429 }
430 else {
431 f.close();
432 f.open(fName.c_str(), std::ios::in | std::ios::binary);
433 char comment[80];
434 f.read(comment, 80);
435
436 if (!f.good()) {
437 throw std::runtime_error("STL File not valid.");
438 }
439
440 comment[79] = 0;
441 int32_t nFacets;
442 f.read(reinterpret_cast<char *>(&nFacets), sizeof(int32_t));
443
444 if (!f.good()) {
445 throw std::runtime_error("STL File not valid.");
446 }
447
448 float v[12];
449 std::uint16_t uint16;
450 for (int32_t i = 0; i < nFacets; ++i) {
451 for (unsigned int j = 0; j < 12; ++j) {
452 f.read(reinterpret_cast<char *>(&v[j]), sizeof(float));
453 }
454 f.read(reinterpret_cast<char *>(&uint16), sizeof(std::uint16_t));
455 STLtriangle<T> tri;
456 tri.normal[0] = v[0];
457 tri.normal[1] = v[1];
458 tri.normal[2] = v[2];
459 tri.point[0].coords[0] = v[3];
460 tri.point[0].coords[1] = v[4];
461 tri.point[0].coords[2] = v[5];
462 tri.point[1].coords[0] = v[6];
463 tri.point[1].coords[1] = v[7];
464 tri.point[1].coords[2] = v[8];
465 tri.point[2].coords[0] = v[9];
466 tri.point[2].coords[1] = v[10];
467 tri.point[2].coords[2] = v[11];
468
469 for (int k = 0; k < 3; k++) {
470 tri.point[0].coords[k] *= stlSize;
471 tri.point[1].coords[k] *= stlSize;
472 tri.point[2].coords[k] *= stlSize;
473 }
474 if (i == 0) {
475 _min[0] = tri.point[0].coords[0];
476 _min[1] = tri.point[0].coords[1];
477 _min[2] = tri.point[0].coords[2];
478
479 _max[0] = tri.point[0].coords[0];
480 _max[1] = tri.point[0].coords[1];
481 _max[2] = tri.point[0].coords[2];
482
483 _min[0] = util::min(_min[0], (T) tri.point[1].coords[0]);
484 _min[1] = util::min(_min[1], (T) tri.point[1].coords[1]);
485 _min[2] = util::min(_min[2], (T) tri.point[1].coords[2]);
486
487 _max[0] = util::max(_max[0], (T) tri.point[1].coords[0]);
488 _max[1] = util::max(_max[1], (T) tri.point[1].coords[1]);
489 _max[2] = util::max(_max[2], (T) tri.point[1].coords[2]);
490
491 _min[0] = util::min(_min[0], (T) tri.point[2].coords[0]);
492 _min[1] = util::min(_min[1], (T) tri.point[2].coords[1]);
493 _min[2] = util::min(_min[2], (T) tri.point[2].coords[2]);
494
495 _max[0] = util::max(_max[0], (T) tri.point[2].coords[0]);
496 _max[1] = util::max(_max[1], (T) tri.point[2].coords[1]);
497 _max[2] = util::max(_max[2], (T) tri.point[2].coords[2]);
498
499 }
500 else {
501 _min[0] = util::min(_min[0], (T) tri.point[0].coords[0]);
502 _min[1] = util::min(_min[1], (T) tri.point[0].coords[1]);
503 _min[2] = util::min(_min[2], (T) tri.point[0].coords[2]);
504
505 _max[0] = util::max(_max[0], (T) tri.point[0].coords[0]);
506 _max[1] = util::max(_max[1], (T) tri.point[0].coords[1]);
507 _max[2] = util::max(_max[2], (T) tri.point[0].coords[2]);
508
509 _min[0] = util::min(_min[0], (T) tri.point[1].coords[0]);
510 _min[1] = util::min(_min[1], (T) tri.point[1].coords[1]);
511 _min[2] = util::min(_min[2], (T) tri.point[1].coords[2]);
512
513 _max[0] = util::max(_max[0], (T) tri.point[1].coords[0]);
514 _max[1] = util::max(_max[1], (T) tri.point[1].coords[1]);
515 _max[2] = util::max(_max[2], (T) tri.point[1].coords[2]);
516
517 _min[0] = util::min(_min[0], (T) tri.point[2].coords[0]);
518 _min[1] = util::min(_min[1], (T) tri.point[2].coords[1]);
519 _min[2] = util::min(_min[2], (T) tri.point[2].coords[2]);
520
521 _max[0] = util::max(_max[0], (T) tri.point[2].coords[0]);
522 _max[1] = util::max(_max[1], (T) tri.point[2].coords[1]);
523 _max[2] = util::max(_max[2], (T) tri.point[2].coords[2]);
524 }
525 tri.init();
526 _triangles.push_back(tri);
527
528 _maxDist2 = util::max(distPoints(tri.point[0], tri.point[1]), _maxDist2);
529 _maxDist2 = util::max(distPoints(tri.point[2], tri.point[1]), _maxDist2);
530 _maxDist2 = util::max(distPoints(tri.point[0], tri.point[2]), _maxDist2);
531 }
532 }
533 f.close();
534}
cpu::simd::Pack< T > min(cpu::simd::Pack< T > rhs, cpu::simd::Pack< T > lhs)
Definition pack.h:124
cpu::simd::Pack< T > max(cpu::simd::Pack< T > rhs, cpu::simd::Pack< T > lhs)
Definition pack.h:130

References olb::STLtriangle< T >::init(), olb::util::max(), olb::util::min(), olb::STLtriangle< T >::normal, and olb::STLtriangle< T >::point.

+ Here is the call graph for this function:

◆ STLmesh() [2/2]

template<typename T >
olb::STLmesh< T >::STLmesh ( const std::vector< std::vector< T > > meshPoints,
T stlSize = 1. )

Constructs a new STLmesh from a file.

Parameters
Filename- Filename
stlSize- Conversion factor for STL (e.g. STL in mm stlSize=10^-3)

Definition at line 537 of file stlReader.hh.

538 : _fName("meshPoints.stl"),
539 _min(T()),
540 _max(T()),
541 _maxDist2(0),
542 clout(std::cout, "STLmesh")
543{
544 _triangles.reserve(10000);
545 for (size_t i = 0; i < meshPoints.size() / 3; i++) {
546 STLtriangle<T> tri;
547 tri.point[0].coords[0] = meshPoints[i*3 + 0][0];
548 tri.point[0].coords[1] = meshPoints[i*3 + 0][1];
549 tri.point[0].coords[2] = meshPoints[i*3 + 0][2];
550
551 tri.point[1].coords[0] = meshPoints[i*3 + 1][0];
552 tri.point[1].coords[1] = meshPoints[i*3 + 1][1];
553 tri.point[1].coords[2] = meshPoints[i*3 + 1][2];
554
555 tri.point[2].coords[0] = meshPoints[i*3 + 2][0];
556 tri.point[2].coords[1] = meshPoints[i*3 + 2][1];
557 tri.point[2].coords[2] = meshPoints[i*3 + 2][2];
558 for (int k = 0; k < 3; k++) {
559 tri.point[0].coords[k] *= stlSize;
560 tri.point[1].coords[k] *= stlSize;
561 tri.point[2].coords[k] *= stlSize;
562 }
563 if (i == 0) {
564 _min*=T();
565 _max*=T();
566
567 _min[0] = tri.point[0].coords[0];
568 _min[1] = tri.point[0].coords[1];
569 _min[2] = tri.point[0].coords[2];
570
571 _max[0] = tri.point[0].coords[0];
572 _max[1] = tri.point[0].coords[1];
573 _max[2] = tri.point[0].coords[2];
574
575 _min[0] = util::min(_min[0], (T) tri.point[1].coords[0]);
576 _min[1] = util::min(_min[1], (T) tri.point[1].coords[1]);
577 _min[2] = util::min(_min[2], (T) tri.point[1].coords[2]);
578
579 _max[0] = util::max(_max[0], (T) tri.point[1].coords[0]);
580 _max[1] = util::max(_max[1], (T) tri.point[1].coords[1]);
581 _max[2] = util::max(_max[2], (T) tri.point[1].coords[2]);
582
583 _min[0] = util::min(_min[0], (T) tri.point[2].coords[0]);
584 _min[1] = util::min(_min[1], (T) tri.point[2].coords[1]);
585 _min[2] = util::min(_min[2], (T) tri.point[2].coords[2]);
586
587 _max[0] = util::max(_max[0], (T) tri.point[2].coords[0]);
588 _max[1] = util::max(_max[1], (T) tri.point[2].coords[1]);
589 _max[2] = util::max(_max[2], (T) tri.point[2].coords[2]);
590
591 }
592 else {
593 _min[0] = util::min(_min[0], (T) tri.point[0].coords[0]);
594 _min[1] = util::min(_min[1], (T) tri.point[0].coords[1]);
595 _min[2] = util::min(_min[2], (T) tri.point[0].coords[2]);
596
597 _max[0] = util::max(_max[0], (T) tri.point[0].coords[0]);
598 _max[1] = util::max(_max[1], (T) tri.point[0].coords[1]);
599 _max[2] = util::max(_max[2], (T) tri.point[0].coords[2]);
600
601 _min[0] = util::min(_min[0], (T) tri.point[1].coords[0]);
602 _min[1] = util::min(_min[1], (T) tri.point[1].coords[1]);
603 _min[2] = util::min(_min[2], (T) tri.point[1].coords[2]);
604
605 _max[0] = util::max(_max[0], (T) tri.point[1].coords[0]);
606 _max[1] = util::max(_max[1], (T) tri.point[1].coords[1]);
607 _max[2] = util::max(_max[2], (T) tri.point[1].coords[2]);
608
609 _min[0] = util::min(_min[0], (T) tri.point[2].coords[0]);
610 _min[1] = util::min(_min[1], (T) tri.point[2].coords[1]);
611 _min[2] = util::min(_min[2], (T) tri.point[2].coords[2]);
612
613 _max[0] = util::max(_max[0], (T) tri.point[2].coords[0]);
614 _max[1] = util::max(_max[1], (T) tri.point[2].coords[1]);
615 _max[2] = util::max(_max[2], (T) tri.point[2].coords[2]);
616 }
617
618 tri.init();
619 _triangles.push_back(tri);
620
621 _maxDist2 = util::max(distPoints(tri.point[0], tri.point[1]),
622 _maxDist2);
623 _maxDist2 = util::max(distPoints(tri.point[2], tri.point[1]),
624 _maxDist2);
625 _maxDist2 = util::max(distPoints(tri.point[0], tri.point[2]),
626 _maxDist2);
627 }
628}

References olb::STLtriangle< T >::init(), olb::util::max(), olb::util::min(), and olb::STLtriangle< T >::point.

+ Here is the call graph for this function:

Member Function Documentation

◆ getMax()

template<typename T >
Vector< T, 3 > & olb::STLmesh< T >::getMax ( )
inline

Returns _max.

Definition at line 195 of file stlReader.h.

196 {
197 return _max;
198 };

◆ getMin()

template<typename T >
Vector< T, 3 > & olb::STLmesh< T >::getMin ( )
inline

Returns _min.

Definition at line 190 of file stlReader.h.

191 {
192 return _min;
193 };

◆ getTri()

template<typename T >
STLtriangle< T > & olb::STLmesh< T >::getTri ( unsigned int i)
inline

Returns reference to a triangle.

Definition at line 175 of file stlReader.h.

176 {
177 return _triangles[i];
178 }

◆ getTriangles()

template<typename T >
std::vector< STLtriangle< T > > & olb::STLmesh< T >::getTriangles ( )
inline

Returns reference to all triangles.

Definition at line 180 of file stlReader.h.

181 {
182 return _triangles;
183 }

◆ maxDist2()

template<typename T >
float olb::STLmesh< T >::maxDist2 ( ) const
inline

Returns maxDist squared.

Definition at line 200 of file stlReader.h.

201 {
202 return _maxDist2;
203 }

◆ print()

template<typename T >
void olb::STLmesh< T >::print ( bool full = false)

Prints console output.

Definition at line 639 of file stlReader.hh.

640{
641 if (full) {
642 int i = 0;
643 clout << "Triangles: " << std::endl;
644 typename std::vector<STLtriangle<T> >::iterator it = _triangles.begin();
645
646 for (; it != _triangles.end(); ++it) {
647 clout << i++ << ": " << it->point[0].coords[0] << " " << it->point[0].coords[1]
648 << " " << it->point[0].coords[2] << " | " << it->point[1].coords[0] << " "
649 << it->point[1].coords[1] << " " << it->point[1].coords[2] << " | "
650 << it->point[2].coords[0] << " " << it->point[2].coords[1] << " "
651 << it->point[2].coords[2] << std::endl;
652 }
653 }
654 clout << "nTriangles=" << _triangles.size() << "; maxDist2=" << _maxDist2
655 << std::endl;
656 clout << "minPhysR(StlMesh)=(" << getMin()[0] << "," << getMin()[1] << ","
657 << getMin()[2] << ")";
658 clout << "; maxPhysR(StlMesh)=(" << getMax()[0] << "," << getMax()[1] << ","
659 << getMax()[2] << ")" << std::endl;
660}
Vector< T, 3 > & getMin()
Returns _min.
Definition stlReader.h:190
Vector< T, 3 > & getMax()
Returns _max.
Definition stlReader.h:195

◆ testRayIntersect()

template<typename T >
bool olb::STLmesh< T >::testRayIntersect ( const std::set< unsigned int > & tris,
const Vector< T, 3 > & pt,
const Vector< T, 3 > & dir,
Vector< T, 3 > & q,
T & alpha )

Compute intersection between Ray and set of triangles; returns true if intersection is found.

Definition at line 698 of file stlReader.hh.

699{
700 std::set<unsigned int>::iterator it = tris.begin();
701 for (; it != tris.end(); ++it) {
702 if (_triangles[*it].testRayIntersect(pt, dir, q, alpha) && alpha < 1) {
703 return true;
704 }
705 }
706 return false;
707}
bool testRayIntersect(const std::set< unsigned int > &tris, const Vector< T, 3 > &pt, const Vector< T, 3 > &dir, Vector< T, 3 > &q, T &alpha)
Compute intersection between Ray and set of triangles; returns true if intersection is found.
Definition stlReader.hh:698

◆ triangleSize()

template<typename T >
unsigned int olb::STLmesh< T >::triangleSize ( ) const
inline

Returns number of triangles.

Definition at line 185 of file stlReader.h.

186 {
187 return _triangles.size();
188 }

◆ write()

template<typename T >
void olb::STLmesh< T >::write ( std::string fName)

Writes STL mesh in Si units.

Definition at line 663 of file stlReader.hh.

664{
665 int rank = 0;
666#ifdef PARALLEL_MODE_MPI
667 rank = singleton::mpi().getRank();
668#endif
669 if (rank == 0) {
670 std::string fullName = singleton::directories().getVtkOutDir() + fName
671 + ".stl";
672 std::ofstream f(fullName.c_str());
673 f << "solid ascii " << fullName << "\n";
674
675 for (unsigned int i = 0; i < _triangles.size(); i++) {
676 f << "facet normal " << _triangles[i].normal[0] << " "
677 << _triangles[i].normal[1] << " " << _triangles[i].normal[2] << "\n";
678 f << " outer loop\n";
679 f << " vertex " << _triangles[i].point[0].coords[0] << " "
680 << _triangles[i].point[0].coords[1] << " " << _triangles[i].point[0].coords[2]
681 << "\n";
682 f << " vertex " << _triangles[i].point[1].coords[0] << " "
683 << _triangles[i].point[1].coords[1] << " " << _triangles[i].point[1].coords[2]
684 << "\n";
685 f << " vertex " << _triangles[i].point[2].coords[0] << " "
686 << _triangles[i].point[2].coords[1] << " " << _triangles[i].point[2].coords[2]
687 << "\n";
688 f << " endloop\n";
689 f << "endfacet\n";
690 }
691 f << "endsolid\n";
692 f.close();
693 }
694 /*if (_verbose)*/clout << "Write ... OK" << std::endl;
695}
std::string getVtkOutDir() const
Definition singleton.h:97
int getRank() const
Returns the process ID.
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150

References olb::singleton::directories(), olb::singleton::MpiManager::getRank(), olb::singleton::Directories::getVtkOutDir(), and olb::singleton::mpi().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: