HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_Watershed.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SOP library (C++)
7  *
8  * COMMENTS:
9  *
10  * The GU_Watershed class implements a Morse-Smale complex on a polygonal
11  * mesh from a geometry detail, by constructing a discrete gradient field
12  * using a scalar function defined on points, primitives, or both. The
13  * definition of the complex and discrete gradient field follow the work
14  * of Robin Forman in discrete Morse theory.
15  *
16  * In addition, we implement a persistence-based simplification of the complex
17  * (Cancellation of critical pairs based on the persistent homology of sub- or
18  * super-level sets). This is done using the algorithm of Ulrich Bauer and
19  * Max Wardetzky. Note that this simplification does not alter function values
20  * and merely merges complex cells.
21  */
22 
23 #ifndef __GU_Watershed_h__
24 #define __GU_Watershed_h__
25 
26 #include "GU_API.h"
27 
28 #include "GU_Detail.h"
29 #include <UT/UT_UniquePtr.h>
30 #include <UT/UT_Map.h>
31 #include <UT/UT_Set.h>
32 #include <UT/UT_Classifier.h>
33 
34 
36 {
37 public:
40  using GetFunctor = std::function<fpreal(GA_Offset)>;
41 
42  GU_Watershed(const GU_Detail *gdp,
43  const GA_PrimitiveGroup *prims,
44  const GetFunctor &point_fn,
45  GetFunctor face_fn = nullptr,
46  const HedgeInterface *hip = nullptr);
47 
48  ~GU_Watershed();
49 
50  const
51  GA_OffsetArray &minima() const { return myMinima; }
52  int numMinima() const { return int(myMinima.size()); }
53 
54  const
55  GA_OffsetArray &maxima() const { return myMaxima; }
56  int numMaxima() const { return int(myMaxima.size()); }
57 
58  const HedgeArray &saddles() const { return mySaddles; }
59  int numSaddles() const { return int(mySaddles.size()); }
60 
62  { return compMax(poly); }
63 
65  { return compMin(pt); }
66 
67  GA_Offset faceMin(GA_Offset poly) const;
68  GA_Offset pointMax(GA_Offset pt) const;
69 
70 
72  { return GA_Offset(myPointSucc.get(pt)); }
73 
75  { return GA_Offset(myFaceSucc.get(poly)); }
76 
77  bool isInPrimalTree(GEO_Hedge h) const;
78  bool isInDualTree(GEO_Hedge h) const;
79 
80  bool isSaddle(GEO_Hedge h) const
81  { return !(isInPrimalTree(h) || isInDualTree(h)); }
82 
83  bool isRidgeEdge(GEO_Hedge h) const;
84  bool isValleyEdge(GEO_Hedge h) const;
85 
86 
87 private:
89  GA_OffsetListRef vertexList(GA_Offset poly) const
90  { return myGdp->getPrimitiveVertexList(poly); }
91 
93  GA_Offset vertexPoint(GA_Offset vtx) const
94  { return myGdp->vertexPoint(vtx); }
95 
97  GA_Offset vertexPoly(GA_Offset vtx) const
98  { return myGdp->vertexPrimitive(vtx); }
99 
101  GA_Offset hedgePoly(GEO_Hedge h) const
102  { return vertexPoly(myHip->srcVertex(h)); }
103 
105  GA_Offset srcVertex(GEO_Hedge h) const
106  { return myHip->srcVertex(h); }
107 
109  GA_Offset srcPoint(GEO_Hedge h) const
110  { return myHip->srcPoint(h); }
111 
113  GA_Offset dstPoint(GEO_Hedge h) const
114  { return myHip->dstPoint(h); }
115 
117  GEO_Hedge sym(GEO_Hedge h) const
118  { return myHip->sym(h); }
119 
120  using ExtremumMap = UT_Map<GA_Offset, GA_Offset>;
121 
123  void setCompExtremum(ExtremumMap &map,
124  const UT_Classifier &classes,
125  GA_Offset rep_elem, GA_Offset extremum)
126  {
127  map[GA_Offset(classes.classRoot(rep_elem))] = extremum;
128  };
129 
130  GA_Offset compExtremum(const ExtremumMap &map,
131  const UT_Classifier &classes,
132  GA_Offset rep_elem) const
133  {
134  auto it = map.find(GA_Offset(classes.classRoot(rep_elem)));
135  if (it == map.end())
136  return GA_INVALID_OFFSET;
137  return it->second;
138  }
139 
140  ExtremumMap myCompMax, myCompMin;
141  UT_Classifier myFaceComps, myPointComps;
142 
144  void setCompMax(GA_Offset rep_poly, GA_Offset max_poly)
145  {
146  return setCompExtremum(myCompMax, myFaceComps, rep_poly, max_poly);
147  };
148 
150  GA_Offset compMax(GA_Offset rep_poly) const
151  {
152  return compExtremum(myCompMax, myFaceComps, rep_poly);
153  };
154 
156  void setCompMin(GA_Offset rep_pt, GA_Offset min_pt)
157  {
158  return setCompExtremum(myCompMin, myPointComps, rep_pt, min_pt);
159  };
160 
162  GA_Offset compMin(GA_Offset rep_pt) const
163  {
164  return compExtremum(myCompMin, myPointComps, rep_pt);
165  };
166 
167  using HedgeInterfaceUptr = UT_UniquePtr<HedgeInterface>;
168  using HedgeSet = UT_Set<GEO_Hedge>;
169 
170  HedgeInterfaceUptr myOwnHip;
171  const GU_Detail *myGdp;
172 
173  const
174  HedgeInterface *myHip;
175 
176  const GetFunctor myPointFn;
177  GetFunctor myFaceFn;
178 
179  GA_RWHandleI myPointSucc;
180  GA_RWHandleI myFaceSucc;
181 
182  HedgeSet myPosSaddles;
183  HedgeSet myNegSaddles;
184 
185  GA_OffsetArray myMinima;
186  GA_OffsetArray myMaxima;
187  HedgeArray mySaddles;
188 };
189 #endif
int numSaddles() const
Definition: GU_Watershed.h:59
SYS_FORCE_INLINE GA_Offset srcPoint(const GA_Detail *gdp, GEO_Hedge h)
Definition: GEO_Hedge.h:186
OPENVDB_STATIC_SPECIALIZATION void compMin(GridOrTreeT &a, GridOrTreeT &b)
Given grids A and B, compute min(a, b) per voxel (using sparse traversal). Store the result in the A ...
Definition: Composite.h:773
GA_Offset pointSucc(GA_Offset pt) const
Definition: GU_Watershed.h:71
std::function< T(int)> GetFunctor
Definition: GU_UVUtils.h:28
GA_Offset faceSucc(GA_Offset poly) const
Definition: GU_Watershed.h:74
GA_Offset pointMin(GA_Offset pt) const
Definition: GU_Watershed.h:64
GA_Offset srcVertex(GEO_Hedge)
Definition: GEO_Hedge.h:179
#define GA_INVALID_OFFSET
Definition: GA_Types.h:674
GA_Size GA_Offset
Definition: GA_Types.h:637
GEO_Hedge encapsulates a half-edge (hedge) which is the restriction of.
Definition: GEO_Hedge.h:47
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
const GA_OffsetArray & maxima() const
Definition: GU_Watershed.h:55
#define GU_API
Definition: GU_API.h:14
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
bool isSaddle(GEO_Hedge h) const
Definition: GU_Watershed.h:80
std::function< fpreal(GA_Offset)> GetFunctor
Definition: GU_Watershed.h:40
int numMinima() const
Definition: GU_Watershed.h:52
UT_Array< GEO_Hedge > HedgeArray
Definition: GU_Decompose.h:55
OPENVDB_STATIC_SPECIALIZATION void compMax(GridOrTreeT &a, GridOrTreeT &b)
Given grids A and B, compute max(a, b) per voxel (using sparse traversal). Store the result in the A ...
Definition: Composite.h:757
int numMaxima() const
Definition: GU_Watershed.h:56
const HedgeArray & saddles() const
Definition: GU_Watershed.h:58
const GA_OffsetArray & minima() const
Definition: GU_Watershed.h:51
const GEO_DetachedHedgeInterface HedgeInterface
Definition: GU_Decompose.h:54
SYS_FORCE_INLINE exint classRoot(exint elem) const
GA_Offset faceMax(GA_Offset poly) const
Definition: GU_Watershed.h:61
SYS_FORCE_INLINE GA_Offset dstPoint(T &iface, GEO_Hedge h)
Definition: GEO_Hedge.h:244