HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_Cosserat.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_Cosserat.h ( CE Library, C++)
7  *
8  * COMMENTS: Functions to perform stable cosserat rods simulation.
9  */
10 
11 #pragma once
12 
13 #include "GU_API.h"
14 #include <CE/CE_Array.h>
15 #include <UT/UT_Quaternion.h>
16 #include <UT/UT_Matrix3.h>
17 #include <variant>
18 
19 class CE_Context;
20 
21 namespace GU_Cosserat
22 {
23 
25 {
26 public:
27  GU_API void loadKernels(
28  CE_Context &context,
29  int n,
30  bool recompile);
31 
32  template <typename T>
33  class Buffer
34  {
35  public:
37  {
38  if (std::holds_alternative<CE_Array<T>>(myBuffer))
39  return std::get<CE_Array<T>>(myBuffer).init(size);
40  };
41 
43  {
44  if (std::holds_alternative<CE_Array<T>>(myBuffer))
45  return std::get<CE_Array<T>>(myBuffer).buffer();
46  else
47  return std::get<cl::Buffer>(myBuffer);
48  }
49 
51  {
52  myBuffer = buffer;
53  }
54 
55  void setInternal()
56  {
57  if (!std::holds_alternative<CE_Array<T>>(myBuffer))
58  myBuffer = CE_Array<T>();
59  }
60  bool isExternal()
61  {
62  return std::holds_alternative<cl::Buffer>(myBuffer);
63  }
64  private:
65  std::variant<CE_Array<T>, cl::Buffer> myBuffer;
66  };
67 
68  class Buffers
69  {
70  public:
71  GU_API void init(int n);
72 
85 
86  // Ping-pong buffers: alternate read/write sides each iteration instead
87  // of copying. myPosSwapped/myOrientSwapped track which side is current.
88  // GUcosseratFinalize normalises back to myPos/myOrient when needed.
92 
93  bool myPosSwapped = false;
94  bool myOrientSwapped = false;
95 
96  } myBuffers;
97 
98  struct Kernels
99  {
106  } myKernels;
107 
108 public:
110 
112 };
113 
114 enum class PointFlags
115 {
116  HASPREV = 1,
117  HASNEXT = 2,
118  NEXTORIENT = 4,
119  FIXORIENT = 8
120 };
121 
123 {
124  float myTimeStep = 1.0f;
125  float myDamping = 1.0f;
126  float myAccelerationRatio = 1.0f;
127  float myKStretch = 1.0f;
128  float myKBend = 1.0f;
129  float myDensity = 1.0f;
130  float myRootBias = 0.5f;
131  float myRefPosMult = 1.0f;
132  float myClumpStiffness = 1.0f;
133  float myClumpDamping = 1.0f;
134  bool myLockRoots = false;
135 };
136 
137 int computeInitFlags(int curveptindex, int ncurvepts)
138 {
139  int flags = 0;
140 
141  if (curveptindex > 0)
143 
144  if (curveptindex < ncurvepts-1)
145  {
147 
148  if (curveptindex < ncurvepts-2)
150  }
151 
152  return flags;
153 }
154 
156  const UT_Vector3 &lastpos,
157  const UT_Vector3 &pos,
158  const UT_Quaternion &lastorient)
159 {
160  UT_Vector3F seg = pos - lastpos;
161  if (seg.length2() < 1e-12f)
162  return lastorient;
163  UT_Matrix3F temp;
164  UT_Vector3F xaxis = {0,0,1};
165  temp.dihedral(xaxis, seg);
166  UT_Quaternion qq;
167  qq.updateFromRotationMatrix(temp);
168 
169  // minimize twist
170  UT_QuaternionF invqq(qq);
171  invqq.invert();
172  UT_QuaternionF t = invqq * lastorient;
173  t.x() = 0.0f;
174  t.y() = 0.0f;
175  t.normalize();
176 
177  return qq * t;
178 }
179 } // namespace GU_Cosserat
180 
181 // by default this uses internal CE_Arrays for every buffer.
182 // call setExternal beforehand to use external cl::Buffer
184  CE_Context &context,
185  int n,
186  GU_Cosserat::SimSettings &sim_settings,
187  GU_Cosserat::CachedItems &cached_items,
188  bool recompile);
189 
190 // by default this uses internal CE_Arrays for every buffer.
191 // call setExternal beforehand to use external cl::Buffer
193  CE_Context &context,
194  int n,
195  GU_Cosserat::CachedItems &cached_items);
196 
198  CE_Context &context,
199  int n,
200  GU_Cosserat::SimSettings &sim_settings,
201  GU_Cosserat::CachedItems &cached_items);
202 
204  CE_Context &context,
205  int n,
206  GU_Cosserat::SimSettings &sim_settings,
207  GU_Cosserat::CachedItems &cached_items);
208 
209 // Symmetrize a CSR clump-constraint graph so every directed edge (a→b) has a
210 // matching reverse edge (b→a). The initial build from a nearest-neighbour
211 // search is asymmetric (maxconstraints=3 by default), causing one-sided forces
212 // in the Jacobi solver. Call once after building the initial arrays.
214  UT_Array<int> &clumppts,
215  UT_Array<int> &clumpptsindex,
216  UT_Array<float> &clumpdists,
217  exint npts);
218 
219 // Must be called once after the iteration loop. For even iteration counts
220 // this is a no-op; for odd counts it copies the ping-pong buffer back to
221 // myPos/myOrient so callers always read results from the primary buffers.
223  CE_Context &context,
224  int n,
225  GU_Cosserat::CachedItems &cached_items);
226 
constexpr SYS_FORCE_INLINE T length2() const noexcept
Definition: UT_Vector3.h:358
GU_API void GUcosseratInit(CE_Context &context, int n, GU_Cosserat::SimSettings &sim_settings, GU_Cosserat::CachedItems &cached_items, bool recompile)
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GLbitfield flags
Definition: glcorearb.h:1596
A simple OpenCL-based array class.
Definition: CE_Array.h:28
GU_API Buffers & getBuffers()
Definition: GU_Cosserat.h:109
int64 exint
Definition: SYS_Types.h:125
cl::KernelFunctor myInitRestLength
Definition: GU_Cosserat.h:101
GU_API Kernels & getKernels()
Definition: GU_Cosserat.h:111
void setExternal(cl::Buffer buffer)
Definition: GU_Cosserat.h:50
GLuint buffer
Definition: glcorearb.h:660
void initIfInternal(exint size)
Definition: GU_Cosserat.h:36
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
struct GU_Cosserat::CachedItems::Kernels myKernels
class GU_Cosserat::CachedItems::Buffers myBuffers
GU_API void GUcosseratIter(CE_Context &context, int n, GU_Cosserat::SimSettings &sim_settings, GU_Cosserat::CachedItems &cached_items)
static UT_Matrix3T< T > dihedral(UT_Vector3T< S > &a, UT_Vector3T< S > &b, UT_Vector3T< S > &c, int norm=1)
GU_API void GUcosseratSymmetrizeClumpConstraints(UT_Array< int > &clumppts, UT_Array< int > &clumpptsindex, UT_Array< float > &clumpdists, exint npts)
GU_API void loadKernels(CE_Context &context, int n, bool recompile)
GU_API void GUcosseratUpdateOrient(CE_Context &context, int n, GU_Cosserat::SimSettings &sim_settings, GU_Cosserat::CachedItems &cached_items)
GU_API void GUcosseratFinalize(CE_Context &context, int n, GU_Cosserat::CachedItems &cached_items)
cl::KernelFunctor myUpdateOrient
Definition: GU_Cosserat.h:105
UT_Quaternion computeMinimalTwistOrient(const UT_Vector3 &lastpos, const UT_Vector3 &pos, const UT_Quaternion &lastorient)
Definition: GU_Cosserat.h:155
#define GU_API
Definition: GU_API.h:14
GLdouble t
Definition: glad.h:2397
GLsizeiptr size
Definition: glcorearb.h:664
Kernel functor interface.
Definition: cl.hpp:3587
Memory buffer interface.
Definition: cl.hpp:1867
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2544
int computeInitFlags(int curveptindex, int ncurvepts)
Definition: GU_Cosserat.h:137
GU_API void GUcosseratInitRefPos(CE_Context &context, int n, GU_Cosserat::CachedItems &cached_items)
void updateFromRotationMatrix(const UT_Matrix3 &)