MorphoGraphX  2.0-1-227
GraphUtils.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef GRAPH_UTILS_HPP
12 #define GRAPH_UTILS_HPP
13 
14 #include <Config.hpp>
15 #include <Misc.hpp>
16 #include <Subdivide.hpp>
17 #include <Attributes.hpp>
18 
19 namespace mgx
20 {
21  typedef std::pair<int, int> IntInt;
22  typedef std::pair<IntInt, double> IntIntDouPair;
23  typedef std::vector<Triangle> triVector;
24 
37  mgx_EXPORT bool subdivideBisect(vvGraph &S, const vertex &v1, const vertex &v2,
38  const vertex &v3, VtxVec* vs = 0, Subdivide *sDiv = 0);
39 
50  template<typename GraphT>
51  bool spliceNhbs(GraphT &S, typename GraphT::vertex_t a, typename GraphT::vertex_t b)
52  {
53  typename GraphT::vertex_t n = S.nextTo(b, a);
54  typename GraphT::vertex_t pv = b;
55 
56  if(!S.edge(a,b) or !S.edge(b,a))
57  return false;
58 
59  while(n != a) {
60  if(!S.edge(a, n)) {
61  S.spliceAfter(a, pv, n);
62  S.spliceAfter(n, b, a);
63  }
64  pv = n;
65  n = S.nextTo(b, n);
66  }
67  return true;
68  }
69 
79  mgx_EXPORT int getLabel(const vertex &v1, const vertex &v2, const vertex &v3);
80 
89  mgx_EXPORT bool getLabelCount(const vvGraph &S, VtxIntMap &labCount);
90 
101  mgx_EXPORT bool splitEdge(vvGraph &S, vertex v1, vertex v2, Subdivide *sDiv = 0);
102 
111  mgx_EXPORT int mergeVertices(vvGraph &S, const VtxVec &vs);
112 
121  mgx_EXPORT bool checkBorderTris(const vvGraph &S, bool select = true);
122 
131  mgx_EXPORT bool deleteCells(vvGraph &S, const VtxVec &V);
132 
140  mgx_EXPORT int fixBorderTris(vvGraph &S);
141 
151  mgx_EXPORT bool mgxmToMgxc(const vvGraph &srcS, vvGraph &S, double wallMax = 0);
152 
163  mgx_EXPORT void markMargins(vvGraph &M, vvGraph &S, bool remborders = true);
164 
174  mgx_EXPORT void markMargins(vvGraph &S, bool remborders = true);
175 
186  mgx_EXPORT void markMargin(vvGraph &S, vertex v, bool remborders = true);
187 
195  mgx_EXPORT int setNormals(const vvGraph &S);
196 
204  mgx_EXPORT void correctNormals(vvGraph &S);
205 
214  mgx_EXPORT bool setNormal(const vvGraph &S, const vertex &v);
215 
225  mgx_EXPORT bool calcNormal(const vvGraph &S, const vertex &v, Point3d &nrml);
226 
227 
237  typedef std::map<Point3i, std::set<int> > Point3iIntSetMap;
238  typedef std::pair<Point3i, std::set<int> > Point3iIntSetPair;
239 
240  // object to return output information of neighborhoodGraph
241  // currently used by cellmaker (label walls) and cellatlas (wall areas)
243 
244  // cell label -> wall area of cell
245  std::map<int,double> cellWallArea;
246 
247  // cell label -> wall area of cell that is not shared with other cells
248  std::map<int,double> outsideWallArea;
249 
250  // cell label 1, cell label 2 -> shared wall area between the cells
251  std::map<IntInt, double> sharedWallArea;
252 
253  // cell label 1, cell label 2 -> set of tri pairs that are identical
254  std::map<IntInt, std::set<std::pair<Triangle,Triangle> > > sharedTris;
255 
256  // vertex -> unique wall label
257  std::map<vertex, int> vtxNewLabelMap;
258  // vertex -> number of cells this vtx belongs to
259  std::map<vertex, int> vtxCellCounterMap;
260 
261  // map from two labels to their wall label (0 for second label if outside)
262  std::map<IntInt, int> allWalls;
263  };
264  mgx_EXPORT bool neighborhoodGraph(const vvGraph& S, double tolerance, NhbdGraphInfo& info, bool throwError = true);
265 
274  mgx_EXPORT std::vector<int> findAllLabels(const vvGraph& S);
275  mgx_EXPORT std::set<int> findAllLabelsSet(const vvGraph& S);
276 
285  mgx_EXPORT std::set<int> findAllSelectedLabels(const vvGraph& S);
286 
287 
295  mgx_EXPORT void neighborhood2D(vvGraph& S, std::map<IntInt, double>& neighMap, QString weightType = "", bool ignoreOutside = true);
296  mgx_EXPORT vertex nearestVertexOnMesh(const Point3d& targetPoint, const vvGraph& S);
297  mgx_EXPORT Point3d nearestPointOnMesh(const Point3d& targetPoint, const vvGraph& S);
298 
299 
300 
301  // dijkstra on vvGraph with selected cells
302  mgx_EXPORT std::map<int, double> dijkstra
303  (vvGraph& S, std::map<IntInt, double>& wallAreas, std::set<int>& selectedCells,
304  bool equalWeights, double cutOffDistance, double wallThreshold);
305 
306  // dijkstra with selected cells
307  mgx_EXPORT std::map<int, double> dijkstra
308  (std::set<int>& allCellLabels, std::map<IntInt, double>& wallAreas, std::set<int>& selectedCells,
309  bool equalWeights, double cutOffDistance, double wallThreshold);
310 
311  // dijkstra with initial values
312  mgx_EXPORT std::map<int, double> dijkstra
313  (std::set<int>& allCellLabels, std::map<IntInt, double>& wallAreas, std::map<int, double>& initialCells,
314  bool equalWeights, double cutOffDistance, double wallThreshold);
315 
316  mgx_EXPORT std::set<int> aStar
317  (std::vector<int>& uniqueLabels, std::map<int, Point3d>& cellCentroids, std::map<IntInt, double>& wallAreas,
318  int label1, int label2, double wallThreshold, bool equalWeights);
319 
320 
321 
322 
323 
332  mgx_EXPORT bool findIntersectPoint(Point3d coordCellCentroid, Point3d& dirVec, std::vector<Triangle>& cellTriangles, int& stopCounter, Point3d& intersectP);
333 
341  mgx_EXPORT double estimateCellLength(int currentLabel, Point3d coordCellCentroid, Point3d& dirVec, triVector& cellTriangles);
342 
343 
349  mgx_EXPORT void generateLabelTriangleMap(const vvGraph& S, std::map<int, triVector>& cellTriangles);
350 
356  mgx_EXPORT std::map<int, std::vector<vertex> > generateLabelVertexMap(const vvGraph& S);
357 
358  // return the border vtxs of a 2D cell
359  mgx_EXPORT void findBorderVerticesOfCell(vvGraph& S, int cellLabel, std::set<vertex>& verticesCell, std::vector<vertex>& borderVerticesOrdered);
360 
361  mgx_EXPORT void findBorderVerticesOfCellsCombined(vvGraph& S, std::set<int> cellLabels, std::set<vertex>& verticesCells, std::vector<vertex>& borderVerticesOrdered);
362 
363  mgx_EXPORT Point3i triIndex(Point3i t);
364  mgx_EXPORT int vIndex(const Point3d &pos, Point3dIntMap &vMap, float tolerance);
365 
366  // used for circular histogram processes
367  mgx_EXPORT int getKeyOfMaxValue(const std::map<int, double>& mapToTest);
368  mgx_EXPORT std::map<int,int> shiftMap(std::map<int, double>& mapToShift, int shift);
369 
370 }
371 #endif
Attributes.hpp
mgx::NhbdGraphInfo
Definition: GraphUtils.hpp:242
mgx::vertex
vvGraph::vertex_t vertex
Type of a vertex.
Definition: Misc.hpp:50
mgx::deleteCells
mgx_EXPORT bool deleteCells(vvGraph &S, const VtxVec &V)
Check for unlabeled vertices, border triangles and islands of label in preparation to make a cell mes...
mgx::VtxVec
std::vector< vertex > VtxVec
Vector of vertices.
Definition: Types.hpp:135
mgx::IntInt
std::pair< int, int > IntInt
Definition: GraphAlgorithms.hpp:27
mgx::NhbdGraphInfo::outsideWallArea
std::map< int, double > outsideWallArea
Definition: GraphUtils.hpp:248
n
#define n
Definition: Eigenvalues.hpp:36
mgx::findIntersectPoint
mgx_EXPORT bool findIntersectPoint(Point3d coordCellCentroid, Point3d &dirVec, std::vector< Triangle > &cellTriangles, int &stopCounter, Point3d &intersectP)
shoots rays from the cell centroid along a direction and checks if it hits any of the cell triangles ...
mgx::mgxmToMgxc
mgx_EXPORT bool mgxmToMgxc(const vvGraph &srcS, vvGraph &S, double wallMax=0)
Convert a normal (MGXM) mesh to a cell (MGXC) mesh.
mgx::NhbdGraphInfo::sharedTris
std::map< IntInt, std::set< std::pair< Triangle, Triangle > > > sharedTris
Definition: GraphUtils.hpp:254
mgx::findBorderVerticesOfCell
mgx_EXPORT void findBorderVerticesOfCell(vvGraph &S, int cellLabel, std::set< vertex > &verticesCell, std::vector< vertex > &borderVerticesOrdered)
mgx::findAllLabelsSet
mgx_EXPORT std::set< int > findAllLabelsSet(const vvGraph &S)
mgx::spliceNhbs
bool spliceNhbs(GraphT &S, typename GraphT::vertex_t a, typename GraphT::vertex_t b)
Splice the neighborhood of b into a (so we can delete b)
Definition: GraphUtils.hpp:51
mgx::correctNormals
mgx_EXPORT void correctNormals(vvGraph &S)
Correct MGXC normals (happens for non-star shaped cells)
mgx::findAllLabels
mgx_EXPORT std::vector< int > findAllLabels(const vvGraph &S)
return ALL labels of a vvGraph originally in CellAtlas, now also used in other AddOns
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::neighborhoodGraph
mgx_EXPORT bool neighborhoodGraph(const vvGraph &S, double tolerance, NhbdGraphInfo &info, bool throwError=true)
mgx::vIndex
mgx_EXPORT int vIndex(const Point3d &pos, Point3dIntMap &vMap, float tolerance)
mgx::generateLabelVertexMap
mgx_EXPORT std::map< int, std::vector< vertex > > generateLabelVertexMap(const vvGraph &S)
goes through all vertices of the vvGraph and builds up a map from label to vertex vector
Subdivide.hpp
mgx::vMap
mgx_EXPORT IntVtxMap * vMap
Maps for saveId for vertices and cells, created when file is read Globals required for overloads of r...
mgx::getLabel
mgx_EXPORT int getLabel(const vertex &v1, const vertex &v2, const vertex &v3)
Return the label for a triangle.
mgx::triVector
std::vector< Triangle > triVector
Definition: GraphUtils.hpp:23
mgx::markMargin
mgx_EXPORT void markMargin(vvGraph &S, vertex v, bool remborders=true)
Mark the cells and tissue margin vertices.
mgx::nrml
nrml
Definition: Geometry.hpp:240
mgx::NhbdGraphInfo::vtxNewLabelMap
std::map< vertex, int > vtxNewLabelMap
Definition: GraphUtils.hpp:257
mgx::subdivideBisect
mgx_EXPORT bool subdivideBisect(vvGraph &S, const vertex &v1, const vertex &v2, const vertex &v3, VtxVec *vs=0, Subdivide *sDiv=0)
Subdivide triangle with a propagating bisection that gives good triangles.
mgx::triIndex
mgx_EXPORT Point3i triIndex(Point3i t)
mgx::Point3d
Vector< 3, double > Point3d
Definition: Geometry.hpp:56
mgx::findBorderVerticesOfCellsCombined
mgx_EXPORT void findBorderVerticesOfCellsCombined(vvGraph &S, std::set< int > cellLabels, std::set< vertex > &verticesCells, std::vector< vertex > &borderVerticesOrdered)
mgx::vvGraph
VVGraph< VertexData, EdgeData > vvGraph
Simpler names for the various containers and iterators related to vertices and VV.
Definition: Misc.hpp:47
mgx::setNormals
mgx_EXPORT int setNormals(const vvGraph &S)
Set the normals in a graph.
mgx::dijkstra
mgx_EXPORT std::map< int, double > dijkstra(vvGraph &S, std::map< IntInt, double > &wallAreas, std::set< int > &selectedCells, bool equalWeights, double cutOffDistance, double wallThreshold)
mgx::NhbdGraphInfo::sharedWallArea
std::map< IntInt, double > sharedWallArea
Definition: GraphUtils.hpp:251
mgx::NhbdGraphInfo::allWalls
std::map< IntInt, int > allWalls
Definition: GraphUtils.hpp:262
mgx::nearestPointOnMesh
mgx_EXPORT Point3d nearestPointOnMesh(const Point3d &targetPoint, const vvGraph &S)
mgx::checkBorderTris
mgx_EXPORT bool checkBorderTris(const vvGraph &S, bool select=true)
Check for unlabeled vertices, border triangles and islands of label in preparation to make a cell mes...
mgx::NhbdGraphInfo::cellWallArea
std::map< int, double > cellWallArea
Definition: GraphUtils.hpp:245
Misc.hpp
mgx::IntIntDouPair
std::pair< IntInt, double > IntIntDouPair
Definition: GraphUtils.hpp:22
mgx::shiftMap
mgx_EXPORT std::map< int, int > shiftMap(std::map< int, double > &mapToShift, int shift)
mgx::Point3iIntSetPair
std::pair< Point3i, std::set< int > > Point3iIntSetPair
Definition: GraphUtils.hpp:238
mgx::Vector< 3, double >
mgx::nearestVertexOnMesh
mgx_EXPORT vertex nearestVertexOnMesh(const Point3d &targetPoint, const vvGraph &S)
mgx::setNormal
mgx_EXPORT bool setNormal(const vvGraph &S, const vertex &v)
Set the normal in a single vertex.
mgx::VtxIntMap
std::unordered_map< vertex, int > VtxIntMap
Map of vertex to int.
Definition: Types.hpp:154
mgx::generateLabelTriangleMap
mgx_EXPORT void generateLabelTriangleMap(const vvGraph &S, std::map< int, triVector > &cellTriangles)
goes through all triangles of the vvGraph and builds up a map from label to triangle vector
mgx::fixBorderTris
mgx_EXPORT int fixBorderTris(vvGraph &S)
Check for border triangles with all labels -1 and merge their vertices together.
mgx::markMargins
mgx_EXPORT void markMargins(vvGraph &M, vvGraph &S, bool remborders=true)
Mark the cells and tissue margin vertices.
mgx::findAllSelectedLabels
mgx_EXPORT std::set< int > findAllSelectedLabels(const vvGraph &S)
return ALL labels of selected vertices of a vvGraph originally in CellAtlas, now also used in other A...
mgx::VVGraph< VertexData, EdgeData >
mgx::mergeVertices
mgx_EXPORT int mergeVertices(vvGraph &S, const VtxVec &vs)
Merge vertices, returns number of vertices deleted.
mgx::NhbdGraphInfo::vtxCellCounterMap
std::map< vertex, int > vtxCellCounterMap
Definition: GraphUtils.hpp:259
mgx::Subdivide
Definition: Subdivide.hpp:25
mgx::Point3iIntSetMap
std::map< Point3i, std::set< int > > Point3iIntSetMap
calculate properties of the neighborhood of a vvGraph such as: cell wall area, shared wall areas,...
Definition: GraphUtils.hpp:237
mgx::getLabelCount
mgx_EXPORT bool getLabelCount(const vvGraph &S, VtxIntMap &labCount)
Count labels for each vertex in the graph.
mgx::calcNormal
mgx_EXPORT bool calcNormal(const vvGraph &S, const vertex &v, Point3d &nrml)
Set the normal in a single vertex.
mgx::estimateCellLength
mgx_EXPORT double estimateCellLength(int currentLabel, Point3d coordCellCentroid, Point3d &dirVec, triVector &cellTriangles)
estimates the size of the cell in a given direction by shooting two rays in a specified direction and...
mgx::getKeyOfMaxValue
mgx_EXPORT int getKeyOfMaxValue(const std::map< int, double > &mapToTest)
mgx::Point3dIntMap
std::map< Point3d, int > Point3dIntMap
Map of Point3d to int.
Definition: Types.hpp:50
mgx::splitEdge
mgx_EXPORT bool splitEdge(vvGraph &S, vertex v1, vertex v2, Subdivide *sDiv=0)
Split an edge in the S graph.
mgx::neighborhood2D
mgx_EXPORT void neighborhood2D(vvGraph &S, std::map< IntInt, double > &neighMap, QString weightType="", bool ignoreOutside=true)
creates a wall map with the neighboring cells as key (pair) and the wall length as value
mgx::aStar
mgx_EXPORT std::set< int > aStar(std::vector< int > &uniqueLabels, std::map< int, Point3d > &cellCentroids, std::map< IntInt, double > &wallAreas, int label1, int label2, double wallThreshold, bool equalWeights)
mgx::Vertex
Definition: Vertex.hpp:58