HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_SurfaceDistance.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_SurfaceDistance.h (GU Library, C++)
7  *
8  * COMMENTS:
9  * Approximates geodesic distance.
10  * This code is related to the soft falloff code in GEO_Transform, but is
11  * mostly a replacement for it as of Houdini 16.5.
12  */
13 
14 #ifndef __GU_SurfaceDistance_h__
15 #define __GU_SurfaceDistance_h__
16 
17 #include "GU_API.h"
18 
19 #include <GA/GA_Types.h>
20 #include <GA/GA_Handle.h>
21 #include <GEO/GEO_Detail.h>
22 
23 
24 // keep this synced with the parameter in $PRM/PRM_Shared.C
26 {
33 };
34 
35 // Utility class for measuring distances along a surface (or along edges).
36 // Call findDistances() to discover distances to points within 'max_dist' from
37 // the seed points/position.
38 //
39 // The geometry given to the constructor will be converted to polygons (or
40 // polylines if measuring along edges). The converted geometry can be explored
41 // with methods on this class.
43 {
44 public:
45  GU_SurfaceDistance(const GEO_Detail &gdp, bool as_polylines);
46 
47  // Returns true if the converted geometry representation is polylines.
48  bool getAsPolylines() const { return myAsPolylines; }
49 
50  // Returns number of polygons in the converted geometry.
51  exint getNumPrimitives() const { return myPrimEnd.entries(); }
52 
53  // Returns range of vertices for the given converted polygon.
54  void getVertexRange(exint pr, exint &start, exint &end) const
55  {
56  end = myPrimEnd(pr);
57  start = pr ? myPrimEnd(pr - 1) : 0;
58  }
59 
60  // Returns the first vertex referencing the given point.
61  exint firstVertex(GA_Offset pt) const { return myPtFirstVtx(exint(pt)); }
62 
63  // Returns the next vertex referencing the same point.
64  exint nextVertex(exint vtx) const { return myVtxNextVtx(vtx); }
65 
66  // Returns the point the given vertex references.
67  GA_Offset vertexPoint(exint vtx) const { return myVtxPt(vtx); }
68 
69  // Returns the primitive to which the given vertex belongs.
70  exint vertexPrimitive(exint vtx) const { return myVtxPrim(vtx); }
71 
72  // Seeds known distances for a set of points. All found distances within
73  // 'max_dist' are reported by calling 'func'.
74  void findDistances(const GA_ROHandleV3 &p_attr,
75  fpreal max_dist, // negative means no max_dist
76  const GA_OffsetArray &pts,
77  const UT_FloatArray &dists,
78  const std::function<void(GA_Offset, fpreal)> &func) const;
79 
80  // Seeds known distances for a set of points. All found distances within
81  // 'max_dist' are reported by calling 'func'.
82  void findDistances(const GA_ROHandleV3 &p_attr,
83  fpreal max_dist, // negative means no max_dist
84  const GA_OffsetArray &pts,
85  const UT_FloatArray &dists,
86  const UT_Array<exint> &src,
87  const std::function<void(GA_Offset, fpreal, exint)> &func) const;
88 
89 private:
90  void internalVertex(GA_Offset pt);
91  void internalEndPrimitive() { myPrimEnd.append(myVtxPt.entries()); }
92 
93  // The following are used by convertPrimitive() to record polygon
94  // representation of geometry. They will discard invalid vertices
95  // and convex the results before passing the data along to the internal*
96  // methods.
97  void polyVertex(GA_OffsetArray &pts, GA_Offset pt)
98  {
99  if(pt != GA_INVALID_OFFSET)
100  pts.append(pt);
101  }
102  void endPolygon(GA_OffsetArray &pts);
103  void endPolyline(GA_OffsetArray &pts);
104 
105  bool myAsPolylines;
106 
107  // points
108  UT_Array<exint> myPtFirstVtx; // first vertex referencing the point
109 
110  // converted prim data
111  UT_Array<exint> myPrimEnd; // end of vertex list for a given polygon
112 
113  // converted vertex data
114  UT_Array<GA_Offset> myVtxPt; // vertexPoint
115  UT_Array<exint> myVtxPrim; // vertexPrimitive
116  UT_Array<exint> myVtxNextVtx; // next vertex referencing the point or -1
117 };
118 
119 // This class contains all the data needed to efficiently compute surface
120 // distances. It will automatically invalidate cache items when inputs to the
121 // query are incompatible with the cached data. This can help SOPs that want
122 // to save time on repeated calls.
124 {
125 public:
126  GU_SurfaceDistanceCache() { clear(); }
128 
129  void clear();
130 
131  // utility method to support multithreading of the surfdist VEX function
132  bool canReuse(const GEO_Detail &gdp, const char *p_attr, fpreal radius,
133  GU_SoftDistanceMetric metric) const;
134 
135  // group of points with valid distances (and lead points)
137  { return myAffectedGroup.get(); }
138 
139  // handle to detached attribute describing measured distances
141  { return GA_ROHandleF(myDistances.get()); }
142 
143  // handle to detached attribute describing closest lead point
145  { return GA_ROHandleID(mySourcePoints.get()); }
146 
147  // compute distances for points within the radius
148  void updateDistances(const GEO_Detail &gdp,
149  const GA_Group *srcgroup,
150  fpreal radius,
151  GU_SoftDistanceMetric metric,
152  const char *p_attr = "P",
153  const GEO_Detail::SoftSymmetryParms *symmetry = nullptr,
154  const GEO_Rolloff *rolloff = nullptr);
155 
156 private:
157  bool hasSameTopology(const GEO_Detail &gdp) const;
158 
159  void updateSurfAndEdgeDistances(const GEO_Detail &gdp,
160  const GA_PointGroup *srcgroup,
161  fpreal radius,
162  GU_SoftDistanceMetric metric,
163  const GA_ROHandleV3 &p_attrib,
164  const GEO_Detail::SoftSymmetryParms *symmetry);
165 
167 
168  // set of points with computed distances (and lead points)
169  GA_PointGroupUPtr myAffectedGroup;
170  // distance to the closest point in the input group
171  GA_AttributeUPtr myDistances;
172  // the point in the input group that the current point is closest to
173  GA_AttributeUPtr mySourcePoints;
174 
175  // values used to determine when to invalidate cached data
176  GU_SoftDistanceMetric myMetric;
177  fpreal myRadius;
178  GA_DataId myGdpPrimitiveId;
179  GA_DataId myGdpTopologyId;
180 
181  bool myHasSymmetryParms;
182  bool myHasPtGroup;
183  GEO_Detail::SoftSymmetryParms mySymmetryParms;
184  UT_String myPAttrib;
185  GA_OffsetArray myPts;
186 };
187 
188 // Utility class for measuring the distances along a surface from a point on the mesh
189 // to the point groups for the same mesh. Allows to compute distances to multiple point groups
190 // at the same time.
192  const GEO_Detail &gdp,
193  const GA_ROHandleV3 &p_attrib,
194  const GA_Offset &pt, // starting point
195  const UT_Array<GA_PointGroupUPtr> &psubset, // points we want to measure the distance to
196  int k, // stop when we find the k closest points for each group in the psubset
197  UT_Array<GA_PointGroupUPtr> &affectedgroups, // set of k closest points to pt with computed distances
198  GA_RWHandleF &h_dists); // distances from the pt to the points in the affectedgroups
199 #endif
GA_ROHandleF getDistances() const
void GU_API GU_FindDistancesToPointGroups(const GU_SurfaceDistance &sdist, const GEO_Detail &gdp, const GA_ROHandleV3 &p_attrib, const GA_Offset &pt, const UT_Array< GA_PointGroupUPtr > &psubset, int k, UT_Array< GA_PointGroupUPtr > &affectedgroups, GA_RWHandleF &h_dists)
void getVertexRange(exint pr, exint &start, exint &end) const
GLenum src
Definition: glew.h:2410
int64 GA_DataId
Definition: GA_Types.h:685
int64 exint
Definition: SYS_Types.h:125
exint nextVertex(exint vtx) const
const GA_PointGroup * getAffectedGroup() const
GA_ROHandleT< int64 > GA_ROHandleID
Definition: GA_Handle.h:1298
exint vertexPrimitive(exint vtx) const
#define GA_INVALID_OFFSET
Definition: GA_Types.h:676
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
GA_Size GA_Offset
Definition: GA_Types.h:639
GA_ROHandleID getSourcePoints() const
exint firstVertex(GA_Offset pt) const
GA_ROHandleT< fpreal32 > GA_ROHandleF
Definition: GA_Handle.h:1268
GLuint GLuint end
Definition: glew.h:1253
GU_SoftDistanceMetric
bool getAsPolylines() const
GA_Offset vertexPoint(exint vtx) const
#define GU_API
Definition: GU_API.h:14
GLuint start
Definition: glew.h:1253
exint append()
Definition: UT_Array.h:95
UT_UniquePtr< GA_Attribute > GA_AttributeUPtr
Definition: GA_Attribute.h:913
GLenum func
Definition: glcorearb.h:782
fpreal64 fpreal
Definition: SYS_Types.h:277
UT_UniquePtr< GA_PointGroup > GA_PointGroupUPtr
exint getNumPrimitives() const