Blender  V3.3
btDeformableMousePickingForce.h
Go to the documentation of this file.
1 /*
2  Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
3 
4  Bullet Continuous Collision Detection and Physics Library
5  Copyright (c) 2019 Google Inc. http://bulletphysics.org
6  This software is provided 'as-is', without any express or implied warranty.
7  In no event will the authors be held liable for any damages arising from the use of this software.
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it freely,
10  subject to the following restrictions:
11  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13  3. This notice may not be removed or altered from any source distribution.
14  */
15 
16 #ifndef BT_MOUSE_PICKING_FORCE_H
17 #define BT_MOUSE_PICKING_FORCE_H
18 
20 
22 {
23  // If true, the damping force will be in the direction of the spring
24  // If false, the damping force will be in the direction of the velocity
25  btScalar m_elasticStiffness, m_dampingStiffness;
26  const btSoftBody::Face& m_face;
27  btVector3 m_mouse_pos;
28  btScalar m_maxForce;
29 
30 public:
32  btDeformableMousePickingForce(btScalar k, btScalar d, const btSoftBody::Face& face, btVector3 mouse_pos, btScalar maxForce = 0.3) : m_elasticStiffness(k), m_dampingStiffness(d), m_face(face), m_mouse_pos(mouse_pos), m_maxForce(maxForce)
33  {
34  }
35 
36  virtual void addScaledForces(btScalar scale, TVStack& force)
37  {
38  addScaledDampingForce(scale, force);
39  addScaledElasticForce(scale, force);
40  }
41 
42  virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
43  {
44  addScaledElasticForce(scale, force);
45  }
46 
47  virtual void addScaledDampingForce(btScalar scale, TVStack& force)
48  {
49  for (int i = 0; i < 3; ++i)
50  {
51  btVector3 v_diff = m_face.m_n[i]->m_v;
52  btVector3 scaled_force = scale * m_dampingStiffness * v_diff;
53  if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
54  {
55  btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
56  scaled_force = scale * m_dampingStiffness * v_diff.dot(dir) * dir;
57  }
58  force[m_face.m_n[i]->index] -= scaled_force;
59  }
60  }
61 
62  virtual void addScaledElasticForce(btScalar scale, TVStack& force)
63  {
64  btScalar scaled_stiffness = scale * m_elasticStiffness;
65  for (int i = 0; i < 3; ++i)
66  {
67  btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
68  btVector3 scaled_force = scaled_stiffness * dir;
69  if (scaled_force.safeNorm() > m_maxForce)
70  {
71  scaled_force.safeNormalize();
72  scaled_force *= m_maxForce;
73  }
74  force[m_face.m_n[i]->index] -= scaled_force;
75  }
76  }
77 
78  virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
79  {
80  btScalar scaled_k_damp = m_dampingStiffness * scale;
81  for (int i = 0; i < 3; ++i)
82  {
83  btVector3 local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index];
84  if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
85  {
86  btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
87  local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index].dot(dir) * dir;
88  }
89  df[m_face.m_n[i]->index] -= local_scaled_df;
90  }
91  }
92 
94 
95  virtual double totalElasticEnergy(btScalar dt)
96  {
97  double energy = 0;
98  for (int i = 0; i < 3; ++i)
99  {
100  btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
101  btVector3 scaled_force = m_elasticStiffness * dir;
102  if (scaled_force.safeNorm() > m_maxForce)
103  {
104  scaled_force.safeNormalize();
105  scaled_force *= m_maxForce;
106  }
107  energy += 0.5 * scaled_force.dot(dir);
108  }
109  return energy;
110  }
111 
112  virtual double totalDampingEnergy(btScalar dt)
113  {
114  double energy = 0;
115  for (int i = 0; i < 3; ++i)
116  {
117  btVector3 v_diff = m_face.m_n[i]->m_v;
118  btVector3 scaled_force = m_dampingStiffness * v_diff;
119  if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
120  {
121  btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
122  scaled_force = m_dampingStiffness * v_diff.dot(dir) * dir;
123  }
124  energy -= scaled_force.dot(m_face.m_n[i]->m_v) / dt;
125  }
126  return energy;
127  }
128 
129  virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
130  {
131  btScalar scaled_stiffness = scale * m_elasticStiffness;
132  for (int i = 0; i < 3; ++i)
133  {
134  btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
135  btScalar dir_norm = dir.norm();
136  btVector3 dir_normalized = (dir_norm > SIMD_EPSILON) ? dir.normalized() : btVector3(0, 0, 0);
137  int id = m_face.m_n[i]->index;
138  btVector3 dx_diff = dx[id];
139  btScalar r = 0; // rest length is 0 for picking spring
140  btVector3 scaled_df = btVector3(0, 0, 0);
141  if (dir_norm > SIMD_EPSILON)
142  {
143  scaled_df -= scaled_stiffness * dir_normalized.dot(dx_diff) * dir_normalized;
144  scaled_df += scaled_stiffness * dir_normalized.dot(dx_diff) * ((dir_norm - r) / dir_norm) * dir_normalized;
145  scaled_df -= scaled_stiffness * ((dir_norm - r) / dir_norm) * dx_diff;
146  }
147  df[id] += scaled_df;
148  }
149  }
150 
151  void setMousePos(const btVector3& p)
152  {
153  m_mouse_pos = p;
154  }
155 
157  {
158  return BT_MOUSE_PICKING_FORCE;
159  }
160 };
161 
162 #endif /* btMassSpring_h */
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
btDeformableLagrangianForceType
@ BT_MOUSE_PICKING_FORCE
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define SIMD_EPSILON
Definition: btScalar.h:543
btVector3
btVector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16-by...
Definition: btVector3.h:82
SIMD_FORCE_INLINE btVector3 normalized() const
Return a normalized version of this vector.
virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack &dv, TVStack &df)
virtual void addScaledDampingForce(btScalar scale, TVStack &force)
virtual double totalElasticEnergy(btScalar dt)
btDeformableMousePickingForce(btScalar k, btScalar d, const btSoftBody::Face &face, btVector3 mouse_pos, btScalar maxForce=0.3)
virtual btDeformableLagrangianForceType getForceType()
virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack &diagA)
virtual double totalDampingEnergy(btScalar dt)
virtual void addScaledExplicitForce(btScalar scale, TVStack &force)
btAlignedObjectArray< btVector3 > TVStack
virtual void addScaledForces(btScalar scale, TVStack &force)
virtual void addScaledElasticForce(btScalar scale, TVStack &force)
virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack &dx, TVStack &df)
Node * m_n[3]
Definition: btSoftBody.h:296
btVector3 m_x
Definition: btSoftBody.h:263
btVector3 m_v
Definition: btSoftBody.h:265
btVector3 m_q
Definition: btSoftBody.h:264