HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
capsuleMeshGenerator.h
Go to the documentation of this file.
1 //
2 // Copyright 2022 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_GEOM_UTIL_CAPSULE_MESH_GENERATOR_H
8 #define PXR_IMAGING_GEOM_UTIL_CAPSULE_MESH_GENERATOR_H
9 
13 
14 #include "pxr/pxr.h"
15 
17 
18 class GfMatrix4d;
19 class PxOsdMeshTopology;
20 
21 /// This class provides an implementation for generating topology, point
22 /// positions and surface normals on a capsule. The simplest form takes a radius
23 /// and height and is a cylinder capped by two hemispheres that is centered at
24 /// the origin. The generated capsule is made up of circular cross-sections in
25 /// the XY plane. Each cross-section has numRadial segments. Successive cross-
26 /// sections for each of the hemispheres are generated at numCapAxial locations
27 /// along the Z and -Z axes respectively. The height is aligned with the Z axis
28 /// and represents the height of just the cylindrical portion.
29 ///
30 /// An optional transform may be provided to GeneratePoints and GenerateNormals
31 /// to orient the capsule as necessary (e.g., whose height is aligned with the
32 /// Y axis).
33 ///
34 /// An additional overload of GeneratePoints is provided to specify different
35 /// radii and heights for the bottom and top caps, as well as the sweep angle
36 /// for the capsule about the +Z axis. When the sweep is less than 360 degrees,
37 /// the generated geometry is not closed.
38 ///
39 /// When the radii are different, the numCapAxial parameter is doubled and the
40 /// number of cross-sections will be divided between the top and bottom
41 /// hemispheres relative to the angle that each portion uses. The topology will
42 /// remain the same while the density of the mesh is more even than if the
43 /// bottom and top caps used the same number of cross-sections.
44 ///
45 /// Usage:
46 /// \code{.cpp}
47 ///
48 /// const size_t numRadial = 4, numCapAxial = 4;
49 /// const size_t numPoints =
50 /// GeomUtilCapsuleMeshGenerator::ComputeNumPoints(numRadial, numCapAxial);
51 /// const float radius = 1, height = 2;
52 ///
53 /// MyPointContainer<GfVec3f> points(numPoints);
54 ///
55 /// GeomUtilCapsuleMeshGenerator::GeneratePoints(
56 /// points.begin(), numRadial, numCapAxial, radius, height);
57 ///
58 /// const size_t numNormals =
59 /// GeomUtilCapsuleMeshGenerator::ComputeNumNormals(numRadial, numCapAxial);
60 ///
61 /// MyPointContainer<GfVec3f> normals(numNormals);
62 ///
63 /// GeomUtilCapsuleMeshGenerator::GenerateNormals(
64 /// normals.begin(), numRadial, numCapAxial, radius, height);
65 ///
66 /// \endcode
67 ///
70 {
71 public:
72  static constexpr size_t minNumRadial = 3;
73  static constexpr size_t minNumCapAxial = 1;
74 
76  static size_t ComputeNumPoints(
77  const size_t numRadial,
78  const size_t numCapAxial,
79  const bool closedSweep = true);
80 
81  static size_t ComputeNumNormals(
82  const size_t numRadial,
83  const size_t numCapAxial,
84  const bool closedSweep = true)
85  {
86  // Normals are per point.
87  return ComputeNumPoints(numRadial, numCapAxial, closedSweep);
88  }
89 
91  {
92  // Normals are per point.
93  return GeomUtilInterpolationTokens->vertex;
94  }
95 
98  const size_t numRadial,
99  const size_t numCapAxial,
100  const bool closedSweep = true);
101 
102  template<typename PointIterType,
103  typename ScalarType,
104  typename Enabled =
106  static void GeneratePoints(
107  PointIterType iter,
108  const size_t numRadial,
109  const size_t numCapAxial,
110  const ScalarType radius,
111  const ScalarType height,
112  const GfMatrix4d* framePtr = nullptr)
113  {
114  GeneratePoints(iter, numRadial, numCapAxial,
115  /* bottomRadius = */ radius,
116  /* topRadius = */ radius,
117  height, framePtr);
118  }
119 
120  template<typename PointIterType,
121  typename ScalarType,
122  typename Enabled =
124  static void GeneratePoints(
125  PointIterType iter,
126  const size_t numRadial,
127  const size_t numCapAxial,
128  const ScalarType bottomRadius,
129  const ScalarType topRadius,
130  const ScalarType height,
131  const GfMatrix4d* framePtr = nullptr)
132  {
133  constexpr ScalarType sweep = 360;
134 
135  GeneratePoints(iter, numRadial, numCapAxial,
136  bottomRadius, topRadius,
137  height, sweep, framePtr);
138  }
139 
140  template<typename PointIterType,
141  typename ScalarType,
142  typename Enabled =
144  static void GeneratePoints(
145  PointIterType iter,
146  const size_t numRadial,
147  const size_t numCapAxial,
148  const ScalarType bottomRadius,
149  const ScalarType topRadius,
150  const ScalarType height,
151  const ScalarType sweepDegrees,
152  const GfMatrix4d* framePtr = nullptr)
153  {
154  using PointType =
156 
157  _GeneratePointsImpl(numRadial, numCapAxial, bottomRadius, topRadius,
158  height, sweepDegrees,
159  framePtr ? _PointWriter<PointType>(iter, framePtr)
160  : _PointWriter<PointType>(iter));
161  }
162 
164 
165 
166  template<typename PointIterType,
167  typename ScalarType,
168  typename Enabled =
170  static void GenerateNormals(
171  PointIterType iter,
172  const size_t numRadial,
173  const size_t numCapAxial,
174  const ScalarType radius,
175  const ScalarType height,
176  const GfMatrix4d* framePtr = nullptr)
177  {
178  GenerateNormals(iter, numRadial, numCapAxial,
179  /* bottomRadius = */ radius,
180  /* topRadius = */ radius,
181  height, framePtr);
182  }
183 
184  template<typename PointIterType,
185  typename ScalarType,
186  typename Enabled =
188  static void GenerateNormals(
189  PointIterType iter,
190  const size_t numRadial,
191  const size_t numCapAxial,
192  const ScalarType bottomRadius,
193  const ScalarType topRadius,
194  const ScalarType height,
195  const GfMatrix4d* framePtr = nullptr)
196  {
197  constexpr ScalarType sweep = 360;
198 
199  GenerateNormals(iter, numRadial, numCapAxial,
200  bottomRadius, topRadius,
201  height, sweep, framePtr);
202  }
203 
204  template<typename PointIterType,
205  typename ScalarType,
206  typename Enabled =
208  static void GenerateNormals(
209  PointIterType iter,
210  const size_t numRadial,
211  const size_t numCapAxial,
212  const ScalarType bottomRadius,
213  const ScalarType topRadius,
214  const ScalarType height,
215  const ScalarType sweepDegrees,
216  const GfMatrix4d* framePtr = nullptr)
217  {
218  using PointType =
220 
221  _GenerateNormalsImpl(numRadial, numCapAxial, bottomRadius, topRadius,
222  height, sweepDegrees,
223  framePtr ? _PointWriter<PointType>(iter, framePtr)
224  : _PointWriter<PointType>(iter));
225  }
226 
228 
229 private:
230 
231  template<typename ScalarType>
232  static size_t _ComputeNumBottomCapAxial(
233  const size_t numCapAxial,
234  const ScalarType latitudeRange);
235 
236  static size_t _ComputeNumTopCapAxial(
237  const size_t numCapAxial,
238  const size_t numBottomCapAxial);
239 
240  template<typename PointType>
241  static void _GeneratePointsImpl(
242  const size_t numRadial,
243  const size_t numCapAxial,
244  const typename PointType::ScalarType bottomRadius,
245  const typename PointType::ScalarType topRadius,
246  const typename PointType::ScalarType height,
247  const typename PointType::ScalarType sweep,
248  const _PointWriter<PointType>& ptWriter);
249 
250  template<typename PointType>
251  static void _GenerateNormalsImpl(
252  const size_t numRadial,
253  const size_t numCapAxial,
254  const typename PointType::ScalarType bottomRadius,
255  const typename PointType::ScalarType topRadius,
256  const typename PointType::ScalarType height,
257  const typename PointType::ScalarType sweep,
258  const _PointWriter<PointType>& ptWriter);
259 };
260 
262 
263 #endif // PXR_IMAGING_GEOM_UTIL_CAPSULE_MESH_GENERATOR_H
type
Definition: core.h:556
static GEOMUTIL_API size_t ComputeNumPoints(const size_t numRadial, const size_t numCapAxial, const bool closedSweep=true)
static void GeneratePoints(PointIterType iter, const size_t numRadial, const size_t numCapAxial, const ScalarType radius, const ScalarType height, const GfMatrix4d *framePtr=nullptr)
static void GenerateNormals(PointIterType iter, const size_t numRadial, const size_t numCapAxial, const ScalarType bottomRadius, const ScalarType topRadius, const ScalarType height, const ScalarType sweepDegrees, const GfMatrix4d *framePtr=nullptr)
static void GeneratePoints(PointIterType iter, const size_t numRadial, const size_t numCapAxial, const ScalarType bottomRadius, const ScalarType topRadius, const ScalarType height, const ScalarType sweepDegrees, const GfMatrix4d *framePtr=nullptr)
static void GenerateNormals(PointIterType iter, const size_t numRadial, const size_t numCapAxial, const ScalarType bottomRadius, const ScalarType topRadius, const ScalarType height, const GfMatrix4d *framePtr=nullptr)
static GEOMUTIL_API PxOsdMeshTopology GenerateTopology(const size_t numRadial, const size_t numCapAxial, const bool closedSweep=true)
uint64 value_type
Definition: GA_PrimCompat.h:29
static void GeneratePoints(PointIterType iter, const size_t numRadial, const size_t numCapAxial, const ScalarType bottomRadius, const ScalarType topRadius, const ScalarType height, const GfMatrix4d *framePtr=nullptr)
static void GenerateNormals(PointIterType iter, const size_t numRadial, const size_t numCapAxial, const ScalarType radius, const ScalarType height, const GfMatrix4d *framePtr=nullptr)
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
static size_t ComputeNumNormals(const size_t numRadial, const size_t numCapAxial, const bool closedSweep=true)
Definition: token.h:70
#define GEOMUTIL_API
Definition: api.h:23
static constexpr size_t minNumCapAxial
static TfToken GetNormalsInterpolation()
static constexpr size_t minNumRadial
static void GeneratePoints(PointIterType iter,...)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
static void GenerateNormals(PointIterType iter,...)