HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_EdgeMesh.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: GU_EdgeMesh.h ( GU Library, C++)
7  *
8  * COMMENTS:
9  * GU_EdgeMesh represents a mesh with edge connectivity information. There
10  * is support for solving the Eikonal equation (propagates from source
11  * points with the given speed, along edges), the Poisson equation (finds
12  * a smooth field whose gradient aligns best with gradient of a target
13  * field and satisfies the given boundary conditions), and a timestep of
14  * the diffusion equation (models propagation of heat at the vertices along
15  * the edges).
16  */
17 
18 #ifndef __GU_EdgeMesh_h__
19 #define __GU_EdgeMesh_h__
20 
21 #include "GU_API.h"
22 #include "GU_Detail.h"
23 
24 #include <UT/UT_Array.h>
25 #include <UT/UT_Map.h>
26 #include <UT/UT_Set.h>
27 
28 /// Forward-declaration of the internal cache class.
29 template<typename T>
31 
32 /// This base class implements the low-level solve routines for GU_EdgeMesh.
33 template<typename T>
35 {
36 public:
37  /// The constructor does nothing... The programmer should interface with a
38  /// concrete subclass which implements buildMesh() (like GU_EdgeMesh).
40  virtual ~GU_EdgeMeshBase();
41  virtual void buildMesh(const GU_Detail* gdp) = 0;
42 
43  /// Set the minimum distance between adjacent nodes used by the solver.
44  void setMinimumDistance(T min_dist);
45 
46  /// This function calculates arrival times for vertices given the starting
47  /// vertices and propagation speeds.
48  /// If USE_SPEEDS is false, the speeds array is unused; in this case, each
49  /// vertex is assumed to have a speed of 1.
50  /// times and speeds should come in pre-initialized. times(x) should be set
51  /// to the respective arrival time for starters; for non-starters, it should
52  /// be set to a large value (preferrably infinity).
53  template<bool USE_SPEEDS>
54  void solveEikonal(UT_Array<T>& times, const UT_Array<T>& speeds,
55  const UT_Set<int>& starters) const;
56 
57  /// Computes the Laplacian of the function defined in vals.
58  void laplacian(UT_Array<T>& result, const UT_Array<T>& vals) const;
59  /// Computes the weighted Laplacian (divergence of product of weights with
60  /// the gradient of vals).
61  void weightedLaplacian(UT_Array<T>& result, const UT_Array<T>& vals,
62  const UT_Array<T>& weights) const;
63 
64  /// Solves the boundary value problem for the Poisson equation. Vertices in
65  /// pinned are the boundary conditions, with their values given by the
66  /// respective entry in sol.
67  /// def is the default value to give to unpinned components.
68  void solvePoisson(UT_Array<T>& sol, const UT_Array<T>& rhs,
69  const UT_Set<int>& pinned, T def = 0) const;
70 
71  /// Solves the boundary value problem for the heat equation. Vertices in
72  /// pineed are the fixed Dirichlet boundary conditions, with their values
73  /// given by the respective entry in sol. dt is size of the time step: its
74  /// value must be positive. If provided, the rates array is assumed to
75  /// contain conductivities at the vertices.
76  void solveDiffusion(UT_Array<T>& sol, const UT_Array<T>* rates,
77  const UT_Set<int>& pinned, T dt) const;
78 
79 protected:
80  /// Internal function to signal that the diffusion rates will be used for
81  /// the next diffusion solve. This is for caching optimization.
82  void setDiffusionRatesCacheIds(bool used, GA_DataId id, float min_rate)
83  const;
84 
85  /// Resets the internal cache. Should only be called if the graph changed.
86  void resetCache() const;
87 
88 protected:
89  /// Number of vertices in this graph.
91  /// Adjacency list for each vertex.
93  /// Array of point locations in space.
95  /// Our solve cache to speed up subsequent similar solves.
97 
98  /// Minimum squared distance between nodes; maximum edge weight is the
99  /// reciprocal of this value.
101 };
102 
103 template<typename T>
105 {
106 public:
108 
109  GU_EdgeMesh();
110  ~GU_EdgeMesh() override {}
111 
112  /// Constructs the edge mesh from its detail. This function must be called
113  /// before the solve routines can be invoked.
114  /// NOTE: the graph will only be rebuilt if the topology or point positions
115  /// changed.
116  void buildMesh(const GU_Detail* gdp) override;
117 
118  /// This function calculates arrival times for all points in the mesh, given
119  /// the starting vertices (the ones in the pinned group, which are arrived
120  /// at the current value of the dst attribute) and propagation speeds (read
121  /// from the speeds point attribute).
122  /// Results are written in the destination point attribute. Points that are
123  /// not reachable from the starters get the value of unreachable.
124  /// The attributes and the group must be defined for points and be for the
125  /// same detail as the one this mesh was built from. If speeds are not
126  /// provided, speed of 1 is assumed for every point.
127  void solveEikonalAttrib(GA_Attribute* dst, const GA_Attribute* speeds,
128  const GA_PointGroup* pinned, T unreachable) const;
129 
130  /// This function solves the Poisson boundary value problem. Points in the
131  /// pinned group are the boundary conditions, and the field will be as close
132  /// as possible to the values defined in the source attribute; if a source
133  /// attribute is not given, it will be assumed to be constant.
134  /// Matching weights can also be provided; if given, they control how
135  /// closely the source field is to be matched at each point (weight values
136  /// should be between 0 and 1). Alternatively, the actual field to match is
137  /// a convex combination of the incoming source field and a linear function;
138  /// the weight then controls the contribution of the source field.
139  /// Results are written in the destination point attribute.
140  /// The attributes and the group must be defined for points and be for the
141  /// same detail as the one this mesh was built from.
142  void solvePoissonAttrib(GA_Attribute* dst, const GA_Attribute* src,
143  const GA_Attribute* weights,
144  const GA_PointGroup* pinned) const;
145 
146  /// This function solves a single time step of the heat equation. Points
147  /// in the pinned group are assumed to be fixed boundary conditions (this
148  /// can be NULL if there are no fixed boundaries). The conductivity rates
149  /// can vary per attribute (specified in rates, values are clamped from
150  /// below at min_rate).
151  void solveDiffusionAttrib(GA_Attribute* dst, const GA_Attribute* rates,
152  const GA_PointGroup* pinned, T dt, T min_rate)
153  const;
154 
155 protected:
156  /// Writes the contents of the array (indexed within this mesh) to the
157  /// given attribute. The attribute must live on points of this mesh's
158  /// detail.
159  /// If WRITE_TO_ALL is true, points not in the mesh will write the value of
160  /// def.
161  /// If CHECK_EQUALITY is true, def is written for points whose value in src
162  /// is the same as equal. If CHECK_EQUALITY is false, this parameter is not
163  /// used.
164  template<bool WRITE_TO_ALL, bool CHECK_EQUALITY = false>
165  void writeArrayToAttribute(GA_Attribute* dst, const UT_Array<T>& src,
166  int comp, T def, T equal) const;
167 
168  /// Reads values from the given attribute and writes them to the destination
169  /// array. Also adds every point in the given source group to the
170  /// destination set.
171  void copyArrayFromAttributeAndSetPinned(UT_Array<T>& dst_a,
172  UT_Set<int>& dst_s,
173  const GA_Attribute* src_a,
174  const GA_PointGroup* src_s) const;
175 
176  /// Reads values from the given attributes and writes them to the
177  /// destination arrays. Also adds every point in the given source group to
178  /// the destination set.
179  /// The second attribute and point group must be non-null. a1 is left
180  /// untouched if ar1 is null. a2 is set to the value of the attribute for
181  /// each pinned point; it is set to def for points not in the group.
182  void copyArraysFromAttributesAndSetPinned(UT_Array<T>& a1,
183  const GA_Attribute* ar1,
184  UT_Array<T>& a2,
185  const GA_Attribute* ar2,
186  T def,
187  UT_Set<int>& dst_s,
188  const GA_PointGroup* src_s) const;
189 
190  /// Adds every point in the given source group to the destination set.
191  void setPinnedFromGroup(UT_Set<int>& dst, const GA_PointGroup* src) const;
192 
193  /// Reads values from the given attribute and writes them to the destination
194  /// array.
195  /// If CLAMP_* is true, values will be clamped beyond that endpoint (given
196  /// by below and above function arguments).
197  template<bool CLAMP_BELOW = false, bool CLAMP_ABOVE = false>
198  void copyArrayFromAttribute(UT_Array<T>& dst, const GA_Attribute* src,
199  int comp, T below = 0, T above = 1) const;
200 
201 protected:
202  /// Pointer to the detail this is to be built from.
204  /// Data ID for mesh topology of the last built graph.
206  /// Data ID for the position attribute of the last built graph.
208  /// Mapping of point offsets in the detail to indices in the mesh.
210 };
211 
216 
217 #endif
218 
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
virtual void buildMesh(const GU_Detail *gdp)=0
gu_EMSolveCache< T > * mySolveCache
Our solve cache to speed up subsequent similar solves.
Definition: GU_EdgeMesh.h:96
int64 GA_DataId
Definition: GA_Types.h:696
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
**But if you need a result
Definition: thread.h:613
Forward-declaration of the internal cache class.
Definition: GU_EdgeMesh.h:30
ImageBuf OIIO_API laplacian(const ImageBuf &src, ROI roi={}, int nthreads=0)
GA_DataId myTopologyId
Data ID for mesh topology of the last built graph.
Definition: GU_EdgeMesh.h:205
const GU_Detail * myDetail
Pointer to the detail this is to be built from.
Definition: GU_EdgeMesh.h:203
UT_Array< UT_Set< int > > myVertexConnections
Adjacency list for each vertex.
Definition: GU_EdgeMesh.h:92
GU_EXTERN_TEMPLATE(GU_EdgeMeshBase< fpreal32 >)
#define GU_API
Definition: GU_API.h:14
UT_Array< UT_Vector3T< T > > myVertexPositions
Array of point locations in space.
Definition: GU_EdgeMesh.h:94
UT_Map< GA_Offset, int > myIndexMap
Mapping of point offsets in the detail to indices in the mesh.
Definition: GU_EdgeMesh.h:209
GLenum GLenum dst
Definition: glcorearb.h:1793
This base class implements the low-level solve routines for GU_EdgeMesh.
Definition: GU_EdgeMesh.h:34
GA_DataId myPositionId
Data ID for the position attribute of the last built graph.
Definition: GU_EdgeMesh.h:207
int myNumVertices
Number of vertices in this graph.
Definition: GU_EdgeMesh.h:90
GLenum src
Definition: glcorearb.h:1793
~GU_EdgeMesh() override
Definition: GU_EdgeMesh.h:110