13 #ifndef MESH_H_INCLUDED
14 #define MESH_H_INCLUDED
21 #define TEST_ARRAYSTRUCT
38 #define _CRTDBG_MAP_ALLOC
53 #include <unordered_map>
82 typedef std::array<std::array<double, 2>, 3> limits;
87 typedef std::vector<const std::vector<double> *>
coordlist;
94 static const auto __issetlength = [](
double l) ->
bool {
return l >= -0.0; };
95 static const double __unsetlength = -1.0;
98 static const int ordered = 0;
99 static const int truncated = 1;
100 static const int open = -1;
101 static const int error = 2;
102 static const auto __isordered = [](
int l) ->
bool {
return l == ordered; };
116 std::vector<double> elems;
123 double GetNorm()
const;
124 void PrepareForUse();
126 double Unit(
const int a)
const;
128 void assign(
double a,
double b,
double c);
129 double &operator[](
int a);
130 double operator()(
int a)
const;
134 return (
bool(isuptodate));
136 const std::vector<double> &usedata()
const
140 const std::vector<double> *retPtr()
const
146 void max(
const std::vector<double> &vecin);
147 void min(
const std::vector<double> &vecin);
148 void add(
const std::vector<double> &vecin);
149 void substract(
const std::vector<double> &vecin);
150 void substractfrom(
const std::vector<double> &vecin);
151 void div(
const std::vector<double> &vecin);
152 void div(
double scalin);
153 void mult(
const std::vector<double> &vecin);
154 void mult(
double scalin);
155 void swap(std::vector<double> &vecin);
157 std::vector<double> cross(
const std::vector<double> &vecin)
const;
158 double dot(
const std::vector<double> &vecin)
const;
159 double angle(
const coordvec &coordin)
const;
167 #ifdef TEST_SNAKSTRUCT
168 std::cout <<
"constructor called for coordvec" << std::endl;
171 void operator=(
const std::vector<double> &a)
173 if (
int(a.size()) != 3)
176 "std::vector other than 3 long");
189 virtual void disptree(
const mesh &meshin,
int n)
const = 0;
190 virtual double value(
const mesh &meshin)
const = 0;
199 double fill, target, error, volume;
200 std::vector<int> surfind;
202 std::vector<int>
vertind(
const mesh &meshin)
const;
203 void ChangeIndices(
int nVert,
int nEdge,
int nSurf,
int nVolu);
205 void disptree(
const mesh &meshin,
int n)
const;
206 double value(
const mesh &meshin)
const;
207 void PrepareForUse(){};
208 #pragma GCC diagnostic push
209 #pragma GCC diagnostic ignored "-Wunused-parameter"
210 bool isready(
bool isInMesh)
const
214 #pragma GCC diagnostic pop
215 void read(FILE *fid);
216 void write(FILE *fid)
const;
217 void TightenConnectivity()
231 #ifdef TEST_ARRAYSTRUCT
232 std::cout <<
"volu #" << index <<
" Was created " << surfind.size() << std::endl;
239 #ifdef TEST_ARRAYSTRUCT
240 std::cout <<
"volu #" << index <<
" Was deleted " << surfind.size() << std::endl;
243 void operator=(
const volu *other)
245 index = other->index;
247 target = other->target;
248 error = other->error;
249 surfind = other->surfind;
251 #ifdef TEST_ARRAYSTRUCT
252 std::cout <<
"OTHER: ";
278 double fill, target, error, area;
279 std::vector<int> edgeind;
280 std::vector<int> voluind;
283 std::vector<int> vertind(
const mesh &meshin)
const;
285 double value(
const mesh &meshin)
const;
286 void disptree(
const mesh &meshin,
int n)
const;
287 void ChangeIndices(
int nVert,
int nEdge,
int nSurf,
int nVolu);
288 void PrepareForUse(){};
289 bool isready(
bool isInMesh)
const
291 return (isInMesh ? isordered :
true);
293 bool IsOrdered()
const
297 void read(FILE *fid);
298 void write(FILE *fid)
const;
299 int OrderEdges(
mesh *meshin);
300 int SplitSurface(
mesh &meshin,
const std::vector<int> &fullEdgeInd);
301 void OrderedVerts(
const mesh *meshin, std::vector<int> &vertList)
const;
302 std::vector<int> OrderedVerts(
const mesh *meshin)
const;
303 void TightenConnectivity()
312 bool edgeconneq(
const surf &other,
bool recurse =
true)
const;
323 voluind.assign(2, 0);
332 void operator=(
const surf *other)
334 index = other->index;
336 error = other->error;
337 target = other->target;
338 edgeind = other->edgeind;
339 voluind = other->voluind;
340 isordered = other->isordered;
355 double length = rsvs3d::constants::__unsetlength;
361 std::vector<int> vertind;
362 std::vector<int> surfind;
365 void ChangeIndices(
int nVert,
int nEdge,
int nSurf,
int nVolu);
367 void disptree(
const mesh &meshin,
int n)
const;
368 double value(
const mesh &meshin)
const;
369 void PrepareForUse(){};
370 #pragma GCC diagnostic push
371 #pragma GCC diagnostic ignored "-Wunused-parameter"
372 bool isready(
bool isInMesh)
const
376 #pragma GCC diagnostic pop
377 void read(FILE *fid);
378 void write(FILE *fid)
const;
379 void TightenConnectivity()
381 if (vertind.size() > 2)
390 double SetLength(
const mesh &meshin)
392 this->length = this->
Length(meshin);
395 double GetLength(
bool warn =
true)
const
397 if (!rsvs3d::constants::__issetlength(this->length) && warn)
400 "Run SetEdgeLengths before call.");
404 void InvalidateLength()
406 this->length = rsvs3d::constants::__unsetlength;
409 bool IsLength0(
const mesh &meshin,
double eps = __DBL_EPSILON__)
const;
410 bool vertconneq(
const edge &other)
const;
415 vertind.assign(2, 0);
419 index = oldEdge.index;
420 vertind = oldEdge.vertind;
421 surfind = oldEdge.surfind;
423 edge &operator=(
const edge &other) =
default;
430 void operator=(
const edge *other)
432 index = other->index;
434 vertind = other->vertind;
435 surfind = other->surfind;
450 std::vector<int> edgeind;
451 std::vector<double> coord;
453 std::vector<int> elmind(
const mesh &meshin,
int dimOveride = -1)
const;
456 void disptree(
const mesh &meshin,
int n)
const;
457 double value(
const mesh &meshin)
const;
458 void ChangeIndices(
int nVert,
int nEdge,
int nSurf,
int nVolu);
459 void PrepareForUse(){};
460 #pragma GCC diagnostic push
461 #pragma GCC diagnostic ignored "-Wunused-parameter"
462 bool isready(
bool isInMesh)
const
466 #pragma GCC diagnostic pop
467 void read(FILE *fid);
468 void write(FILE *fid)
const;
469 void TightenConnectivity()
474 int OrderEdges(
const mesh *meshin);
475 std::pair<std::vector<int>,
int> OrderEdges(
const mesh *meshin)
const;
476 int OrderEdges(
const mesh *meshin, std::vector<int> &edgeIndOut)
const;
477 int SurroundingCoords(
const mesh *meshin,
grid::coordlist &coordout,
bool isOrdered =
false,
478 std::vector<int> *edgeIndOutPtr = NULL)
const;
495 void operator=(
const vert *other)
497 index = other->index;
499 edgeind = other->edgeind;
500 coord = other->coord;
518 std::vector<int> rmvind;
519 std::vector<int> scopeind;
548 int AddParent(
mesh *meshin);
549 int AddChild(
mesh *meshin);
550 void AddParent(
mesh *meshin, std::vector<int> &parentind);
551 void RemoveChild(
mesh *meshin);
552 void RemoveParent(
mesh *meshin);
555 int NumberOfParents()
const
559 const mesh *ParentPointer(
int a)
const
561 return this->parentmesh.at(a);
563 std::vector<int> ChildIndices(
int parent,
int parentVoluIndex)
const
565 return this->parentconn.at(parent).findall(parentVoluIndex);
569 this->elemind.clear();
570 this->parentconn.clear();
571 for (
auto parent : this->parentmesh)
573 this->RemoveParent(parent);
575 for (
auto child : this->childmesh)
577 this->RemoveChild(child);
594 bool borderIsSet =
false;
595 bool meshDepIsSet =
false;
596 bool facesAreOriented =
false;
597 bool edgesLengthsAreSet =
false;
601 void OrientSurfaceVolume();
602 void OrientEdgeSurface();
603 int OrientRelativeSurfaceVolume(std::vector<int> &surfOrient);
604 void ArraysAreHashed();
622 this->meshtree.clear();
623 this->borderIsSet =
false;
624 this->meshDepIsSet =
false;
625 this->facesAreOriented =
false;
626 this->edgesLengthsAreSet =
false;
629 void RemoveFromFamily();
630 void AddChild(
mesh *meshin);
631 void AddParent(
mesh *meshin);
632 void AddParent(
mesh *meshin, std::vector<int> &parentind);
633 void AddChild(
mesh *meshin, std::vector<int> &parentind);
634 void SetMeshDepElm();
636 void MaintainLineage();
637 int CountParents()
const;
638 int SurfInParent(
int surfind)
const;
639 void SurfInParent(std::vector<int> &listInParent)
const;
640 void ElmOnParentBound(std::vector<int> &listInParent, std::vector<int> &voluInd,
bool isBorderBound =
true,
641 bool outerVolume =
true)
const;
642 void SurfOnParentBound(std::vector<int> &listInParent, std::vector<int> &voluInd,
bool isBorderBound,
643 bool outerVolume)
const;
644 void EdgeOnParentBound(std::vector<int> &listInParent, std::vector<int> &voluInd,
bool isBorderBound,
645 bool outerVolume)
const;
646 int CountVoluParent()
const;
647 void ReturnParentMap(std::vector<int> &currind, std::vector<int> &parentpos,
648 std::vector<std::pair<int, int>> &parentcases, std::vector<double> &voluVals)
const;
649 void MapVolu2Parent(
const std::vector<double> &fillIn,
const std::vector<std::pair<int, int>> &parentcases,
650 double volu::*mp = &volu::fill);
651 void MapVolu2Self(
const std::vector<double> &fillIn,
const std::vector<int> &elms,
double volu::*mp = &volu::fill);
652 void VoluValuesofParents(
int elmInd, std::vector<double> &vals,
int volType = 0)
const;
653 void VoluValuesofParents(
int elmInd, std::vector<double> &vals,
double volu::*mp)
const;
654 void SurfValuesofParents(
int elmInd, std::vector<double> &vals,
int volType = 0)
const;
655 void SurfValuesofParents(
int elmInd, std::vector<double> &vals,
double surf::*mp)
const;
656 int ParentElementIndex(
int childElmInd,
int parentInd = 0)
const;
665 void GetMaxIndex(
int *nVert,
int *nEdge,
int *nSurf,
int *nVolu)
const;
666 void Init(
int nVe,
int nE,
int nS,
int nVo);
667 void size(
int &nVe,
int &nE,
int &nS,
int &nVo)
const;
668 void reserve(
int nVe,
int nE,
int nS,
int nVo);
669 void PrepareForUse(
bool needOrder =
true);
670 void SetEdgeLengths();
671 void InvalidateEdgeLength(
int iEdge);
673 void displight()
const;
674 void Concatenate(
const mesh &other);
675 bool isready()
const;
676 void PopulateIndices();
677 void TightenConnectivity();
678 int TestConnectivity(
const char *strRoot =
"")
const;
679 int TestConnectivityBiDir(
const char *strRoot =
"",
bool emptyIsErr =
true)
const;
681 void write(FILE *fid)
const;
682 void read(FILE *fid);
683 int write(
const char *str)
const;
684 int read(
const char *str);
686 void MakeCompatible_inplace(
mesh &other)
const;
687 mesh MakeCompatible(
mesh other)
const;
688 void ChangeIndices(
int nVert,
int nEdge,
int nSurf,
int nVolu);
689 void SwitchIndex(
int typeInd,
int oldInd,
int newInd,
const std::vector<int> &scopeInd = {0});
690 void RemoveIndex(
int typeInd,
int oldInd);
692 int ConnectedVolumes(std::vector<int> &volBlock,
const std::vector<bool> &boundaryFaces = {})
const;
693 void ForceCloseContainers();
694 void RemoveSingularConnectors(
const std::vector<int> &rmvVertInds = {},
bool voidError =
true);
697 std::vector<int> OrderEdges();
700 int OrderVertexEdges(
int vertIndex);
703 bool CompareVerts(
const vert &in1,
const vert &in2)
const;
704 bool CompareEdges(
const edge &in1,
const edge &in2)
const;
705 bool CompareSurfs(
const surf &in1,
const surf &in2)
const;
706 bool CompareVolus(
const volu &in1,
const volu &in2)
const;
708 void GetOffBorderVert(std::vector<int> &vertList, std::vector<int> &voluInd,
int outerVolume = -1);
709 void GetOffBorderVert(std::vector<int> &vertList, std::vector<int> &voluInd,
int outerVolume = -1)
const;
710 void GetOffBorderVert3D(std::vector<int> &vertList, std::vector<int> &voluInd,
int outerVolume = -1)
const;
711 void GetOffBorderVert2D(std::vector<int> &vertInd, std::vector<int> &surfind,
int outerVolume = -1)
const;
713 coordvec CalcCentreVolu(
int ind)
const;
714 coordvec CalcPseudoNormalSurf(
int ind)
const;
715 std::vector<int>
VertexInVolume(
const std::vector<double> testVertices,
int sizeVert = 3)
const;
721 void LoadTargetFill(
const std::string &fileName);
722 grid::limits BoundingBox()
const;
723 void ReturnBoundingBox(std::array<double, 3> &lowerB, std::array<double, 3> &upperB)
const;
725 void Crop(std::vector<int> indList,
int indType = 1);
726 std::vector<int>
AddBoundary(
const std::vector<double> &lb,
const std::vector<double> &ub);
727 void CropAtBoundary(
const std::vector<double> &lb,
const std::vector<double> &ub);
729 int EdgeFromVerts(
int v1,
int v2)
const;
730 int SurfFromEdges(
int e1,
int e2,
int repetitionBehaviour = -1)
const;
732 void VerticesVector(
int v1,
int v2,
coordvec &vec)
const;
733 void EdgeVector(
int e,
coordvec &vec)
const;
743 void ConnVertFromConnEdge(
const mesh &meshin,
const std::vector<int> &edgeind, std::vector<int> &vertind);
744 std::pair<int, int> OrderMatchLists(
const std::vector<int> &vec1,
int p1,
int p2);
745 int OrderMatchLists(
const std::vector<int> &vec1,
const std::vector<int> &vec2,
int p1,
int p2);
746 void CropMeshGreedy(
mesh &meshin,
const std::vector<double> &lb,
const std::vector<double> &ub);
747 int OrderEdgeList(std::vector<int> &edgeind,
const mesh &meshin,
bool warn =
true,
bool errout =
true,
748 const std::vector<int> *edgeIndOrigPtr = NULL,
const surf *surfin = NULL);
749 int OrderList(std::vector<int> &edgeind,
const std::vector<int> &edge2Vert,
bool warn =
true,
bool errout =
true,
750 const std::vector<int> *edgeIndOrigPtr = NULL);
751 void DiffPointsFromCentre(
const std::vector<double> ¢re,
const std::vector<double> &planeVert2,
753 void DiffPoints(
const std::vector<double> &vert1,
const std::vector<double> &vert2,
coordvec &diffVerts);
754 double Angle3Points(
const std::vector<double> ¢re,
const std::vector<double> &planeVert2,
756 double VertexDistanceToPlane(
const std::vector<double> &planeVert1,
const std::vector<double> &planeVert2,
757 const std::vector<double> &planeVert3,
const std::vector<double> &testVertex,
760 const std::vector<double> &planeVert2,
761 const std::vector<double> &planeVert3,
763 double VertexDistanceToPlane(
const std::vector<double> &planeVert1,
const std::vector<double> &planeVert2,
764 const std::vector<double> &planeVert3,
const std::vector<double> &testVertex);
766 const std::vector<double> &planeVert2,
767 const std::vector<double> &planeVert3,
768 const std::vector<double> &testVertices);
770 double PlanesDotProduct(
const std::vector<double> &planeVert1,
const std::vector<double> &planeVert2,
771 const std::vector<double> &planeVert3,
const std::vector<double> &planeVert4,
772 const std::vector<double> &planeVert5,
const std::vector<double> &planeVert6,
773 bool normalize =
true);
774 void PlaneNormal(
const std::vector<double> &planeVert1,
const std::vector<double> &planeVert2,
776 double PlaneNormalAndAngle(
const std::vector<double> &planeVert1,
const std::vector<double> &planeVert2,
782 template <
class T,
class V,
class W>
783 double ProjectRay(
int count,
const W &&boundBox,
const T &dir,
const V &orig,
double minDist = 0.0);
785 void PlaceBorderVertex(
const std::vector<double> &coordIn,
const std::vector<double> &coordOut,
786 const std::vector<double> &lb,
const std::vector<double> &ub, std::vector<double> &coordTarg);
788 void SplitBorderSurfaceEdgeind(
const mesh &meshin,
const std::vector<bool> &edgeOut, std::vector<int> &vecconnIn,
789 std::vector<int> &vecconnOut);
790 void SplitBorderVolumeSurfind(
const mesh &meshin,
const std::vector<bool> &edgeOut, std::vector<int> &vecconnIn,
791 std::vector<int> &vecconnOut);
792 void HandleMultiSurfaceSplit(
mesh &meshin, std::vector<int> &edgeindOld, std::vector<int> &edgeindNew,
793 std::vector<int> &vertindNew);
794 std::vector<int> FindVertInFromEdgeOut(
const mesh &meshin,
const std::vector<bool> &vertOut,
795 const std::vector<int> &edgeList,
const std::vector<int> &edgeListCheck);
796 std::vector<int> FindEdgeInFromSurfOut(
const mesh &meshin,
const std::vector<bool> &edgeOut, std::vector<int> surfList);
797 double VerticesDistanceSquared(
const mesh &meshin,
const std::vector<int> &vertind);
798 double VerticesDistance(
const mesh &meshin,
const std::vector<int> &vertind);
799 bool IsVerticesDistance0(
const mesh &meshin,
const std::vector<int> &vertind,
double eps = __DBL_EPSILON__);
800 int VertexInVolume(
const mesh &meshin,
const std::vector<double> testCoord,
bool needFlip =
false);
801 int Get3PointsInSurface(
const mesh &meshin,
int surfCurr, std::array<int, 3> &surfacePoints);
802 int NormalShouldFlip(
const std::vector<int> orderedList,
int elm1,
int elm2,
const std::vector<int> &voluind,
803 bool innerComparison);
806 int Test_ArrayStructures();
814 template <
class T,
class V,
class W>
815 double meshhelp::ProjectRay(
int count,
const W &&boundBox,
const T &dir,
const V &orig,
double minDist)
826 double l = -INFINITY;
828 for (
int i = 0; i < count; ++i)
830 l = std::max(l, std::min((boundBox[0][i] - orig[i]) / dir[i], (boundBox[1][i] - orig[i]) / dir[i]));
832 return std::min(l, minDist);
Provide std::vector container with hashed index mapping.
Class containing the information needed to trim objects from a mesh.
Handles the use and norm of a vector for which the norm and the unit value might be needed.
Class for an edge object in a mesh.
double Length(const mesh &meshin) const
Calculate the edge length.
double LengthSquared(const mesh &meshin) const
Calculate squared edge length.
bool IsLength0(const mesh &meshin, double eps=__DBL_EPSILON__) const
Returns.
void GeometricProperties(const mesh *meshin, coordvec ¢re, double &length) const
Math operations in mesh.
std::vector< int > AddBoundary(const std::vector< double > &lb, const std::vector< double > &ub)
Adds boundaries alond max and min xyz planes.
void LinearTransform(const grid::transformation &transform)
Applies a linear transformation to the points on a grid.
void LinearTransformFamily(const grid::transformation &transform)
Applies a linear transform to child and parent meshes.
void _LinearTransformGeneration(const grid::transformation &transform, std::vector< mesh * > meshdependence::*mp)
Applies reccursively linear transforms to a tree of meshes.
std::vector< int > VertexInVolume(const std::vector< double > testVertices, int sizeVert=3) const
Finds for each vertex, the volume object containing it.
int VertFromVertEdge(int v, int e) const
Returns the vertex in edges.isearch(e)->vertind which does not match v.
int ConnectedVertex(std::vector< int > &vertBlock) const
Return in a vector for each vertex a block number which it is part of.
int SurfFromEdges(int e1, int e2, int repetitionBehaviour=-1) const
Returns the index of the surface connecting two edges.
void OrientFaces()
Orients either surfaces or edges depending on the dimensionality of the object.
Class for connecting meshes.
std::vector< HashedVectorSafe< int, int > > parentconn
parent/to self connectivity, 1 vector element per parent.
std::vector< int > elemind
Indices of the active elements of the owning mesh.
std::vector< mesh * > parentmesh
Vector of pointers to the mesh which are coarser (parents).
std::vector< mesh * > childmesh
Vector of pointers to the mesh which are finer (children).
int nParents
Number of parent meshes.
/Abstract class to ensure mesh interfaces are correct.
Class for surface object in a mesh.
coordvec PseudoCentroid(const mesh &meshin) const
Calculates the length weighted pseudo-centroid of a surface.
Class for a vertex in a mesh.
Class for volume cell objects in a mesh.
std::vector< int > vertind(const mesh &meshin) const
Get all the vertices a volume is connected to.
coordvec PseudoCentroid(const mesh &meshin) const
Calculates the length weighted pseudo-centroid of a volume.
std::vector< const std::vector< double > * > coordlist
Defines a list of coordinates.
std::tuple< coordvec, double > VertexNormal(const std::vector< double > ¢re, const grid::coordlist &vecPts)
Calculates the vertex normal weighted by surface angle partitions.
mesh Points2Mesh(const std::vector< double > &vecPts, int nProp=3)
Takes in a set of points and returns a mesh of points ready for voronisation.
double PlaneNormalAndAngle(const std::vector< double > &planeVert1, const std::vector< double > &planeVert2, const std::vector< double > &planeVert3, coordvec &normal, coordvec &temp1)
Calculates a plane's normal vector.
void PlaneNormal(const std::vector< double > &planeVert1, const std::vector< double > &planeVert2, const std::vector< double > &planeVert3, coordvec &normal, coordvec &temp1)
Calculates a plane's normal vector.
double Angle3Points(const std::vector< double > ¢re, const std::vector< double > &planeVert2, const std::vector< double > &planeVert3, coordvec &vec1, coordvec &vec2)
Calculates a plane's normal vector.
void DiffPoints(const std::vector< double > &vert1, const std::vector< double > &vert2, coordvec &diffVerts)
Computes vector between vertices to then compute angles and plane normals.
std::array< std::array< double, 3 >, 3 > transformation
Defines a linear transformation to the mesh where for each dimension: {new minimum,...
std::vector< double > VerticesDistanceToPlane(const std::vector< double > &planeVert1, const std::vector< double > &planeVert2, const std::vector< double > &planeVert3, const std::vector< double > &testVertices, coordvec &temp1, coordvec &temp2)
Calculates the distance from a set of vertices to a plane.
int OrderEdgeList(std::vector< int > &edgeind, const mesh &meshin, bool warn=true, bool errout=true, const std::vector< int > *edgeIndOrigPtr=NULL, const surf *surfin=NULL)
Orders a list of edge to be connected.
int OrderList(std::vector< int > &edgeind, const std::vector< int > &edge2Vert, bool warn=true, bool errout=true, const std::vector< int > *edgeIndOrigPtr=NULL)
Orders a list of elements defined by pairs of indices.
void CropMeshGreedy(mesh &meshin, const std::vector< double > &lb, const std::vector< double > &ub)
Crops a mesh to only the elements inside the cropBox.
void DiffPointsFromCentre(const std::vector< double > ¢re, const std::vector< double > &planeVert2, const std::vector< double > &planeVert3, coordvec &normal, coordvec &temp1)
Computes vector between vertices to then compute angles and plane normals.
double VertexDistanceToPlane(const std::vector< double > &planeVert1, const std::vector< double > &planeVert2, const std::vector< double > &planeVert3, const std::vector< double > &testVertex, coordvec &temp1, coordvec &temp2)
Calculates the distance from a vertex to a plane.
Namespace for general purpose tools of the RSVS project.
void error(const char *message="", const char *caller="", const char *file="", int line=0, bool throwError=true)
Custom error function.
Provides the error and warning system used by the RSVS3D project.
#define RSVS3D_ERROR_NOTHROW(M)
Generic rsvs warning.
#define RSVS3D_ERROR_ARGUMENT(M)
Throw a invalid_argument.