Newton Dynamics  4.00
ndScene.h
1 /* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __ND_SCENE_H__
23 #define __ND_SCENE_H__
24 
25 #include "ndCollisionStdafx.h"
26 #include "ndBvhNode.h"
27 #include "ndBodyListView.h"
28 #include "ndContactArray.h"
29 #include "ndPolygonMeshDesc.h"
30 
31 #define D_SCENE_MAX_STACK_DEPTH 256
32 
33 class ndWorld;
34 class ndScene;
35 class ndContact;
36 class ndRayCastNotify;
37 class ndContactNotify;
38 class ndConvexCastNotify;
41 
42 D_MSV_NEWTON_ALIGN_32
44 {
45  public:
47  {
48  }
49 
50  virtual ~ndSceneTreeNotiFy()
51  {
52  }
53 
54  virtual void OnDebugNode(const ndBvhNode* const node) = 0;
55 } D_GCC_NEWTON_ALIGN_32;
56 
57 D_MSV_NEWTON_ALIGN_32
58 class ndScene : public ndThreadPool
59 {
60  protected:
62  {
63  public:
64  ndContactPairs(ndUnsigned32 body0, ndUnsigned32 body1)
65  :m_body0(ndMin(body0, body1))
66  ,m_body1(ndMax(body0, body1))
67  {
68  }
69 
70  ndUnsigned32 m_body0;
71  ndUnsigned32 m_body1;
72  };
73 
74  public:
75  D_COLLISION_API virtual ~ndScene();
76  D_COLLISION_API virtual bool AddBody(ndSharedPtr<ndBody>& body);
77  D_COLLISION_API virtual bool RemoveBody(ndBodyKinematic* const body);
78 
79  D_COLLISION_API virtual void Begin();
80  D_COLLISION_API virtual void End();
81  D_COLLISION_API virtual void Sync();
82  D_COLLISION_API virtual bool IsGPU() const;
83  D_COLLISION_API virtual bool IsValid() const;
84  D_COLLISION_API virtual double GetGPUTime() const;
85 
86  D_COLLISION_API virtual void Cleanup();
87 
88  D_COLLISION_API ndContactNotify* GetContactNotify() const;
89  D_COLLISION_API void SetContactNotify(ndContactNotify* const notify);
90 
91  D_COLLISION_API virtual void DebugScene(ndSceneTreeNotiFy* const notify);
92 
93  D_COLLISION_API virtual void BodiesInAabb(ndBodiesInAabbNotify& callback, const ndVector& minBox, const ndVector& maxBox) const;
94  D_COLLISION_API virtual bool RayCast(ndRayCastNotify& callback, const ndVector& globalOrigin, const ndVector& globalDest) const;
95  D_COLLISION_API virtual bool ConvexCast(ndConvexCastNotify& callback, const ndShapeInstance& convexShape, const ndMatrix& globalOrigin, const ndVector& globalDest) const;
96 
97  D_COLLISION_API void SendBackgroundTask(ndBackgroundTask* const job);
98 
99  ndInt32 GetThreadCount() const;
100 
101  virtual ndWorld* GetWorld() const;
102  const ndBodyListView& GetBodyList() const;
103 
104  ndArray<ndBodyKinematic*>& GetActiveBodyArray();
105  const ndArray<ndBodyKinematic*>& GetActiveBodyArray() const;
106 
107  ndArray<ndConstraint*>& GetActiveContactArray();
108  const ndArray<ndConstraint*>& GetActiveContactArray() const;
109 
110  ndArray<ndUnsigned8>& GetScratchBuffer();
111 
112  ndFloat32 GetTimestep() const;
113  void SetTimestep(ndFloat32 timestep);
114  ndBodyKinematic* GetSentinelBody() const;
115 
116  protected:
117  D_COLLISION_API ndScene();
118  D_COLLISION_API ndScene(const ndScene& src);
119  bool ValidateContactCache(ndContact* const contact, const ndVector& timestep) const;
120 
121  const ndContactArray& GetContactArray() const;
122  void FindCollidingPairs(ndBodyKinematic* const body, ndInt32 threadId);
123  void FindCollidingPairsForward(ndBodyKinematic* const body, ndInt32 threadId);
124  void FindCollidingPairsBackward(ndBodyKinematic* const body, ndInt32 threadId);
125  void AddPair(ndBodyKinematic* const body0, ndBodyKinematic* const body1, ndInt32 threadId);
126  void SubmitPairs(ndBvhLeafNode* const bodyNode, ndBvhNode* const node, bool forward, ndInt32 threadId);
127 
128  void CalculateJointContacts(ndInt32 threadIndex, ndContact* const contact);
129  void ProcessContacts(ndInt32 threadIndex, ndInt32 contactCount, ndContactSolver* const contactSolver);
130 
131  ndJointBilateralConstraint* FindBilateralJoint(ndBodyKinematic* const body0, ndBodyKinematic* const body1) const;
132  bool RayCast(ndRayCastNotify& callback, const ndBvhNode** stackPool, ndFloat32* const distance, ndInt32 stack, const ndFastRay& ray) const;
133  bool ConvexCast(ndConvexCastNotify& callback, const ndBvhNode** stackPool, ndFloat32* const distance, ndInt32 stack, const ndFastRay& ray, const ndShapeInstance& convexShape, const ndMatrix& globalOrigin, const ndVector& globalDest) const;
134 
135  // call from sub steps update
136  D_COLLISION_API virtual void ApplyExtForce();
137  D_COLLISION_API virtual void BalanceScene();
138  D_COLLISION_API virtual void InitBodyArray();
139  D_COLLISION_API virtual void UpdateSpecial();
140  D_COLLISION_API virtual void UpdateBodyList();
141  D_COLLISION_API virtual void UpdateTransform();
142  D_COLLISION_API virtual void CreateNewContacts();
143  D_COLLISION_API virtual void CalculateContacts();
144  D_COLLISION_API virtual void FindCollidingPairs();
145  D_COLLISION_API virtual void DeleteDeadContacts();
146 
147  D_COLLISION_API virtual void CalculateContacts(ndInt32 threadIndex, ndContact* const contact);
148  D_COLLISION_API virtual void UpdateTransformNotify(ndInt32 threadIndex, ndBodyKinematic* const body);
149 
150  ndBodyListView m_bodyList;
151  ndContactArray m_contactArray;
152  ndBvhSceneManager m_bvhSceneManager;
153  ndArray<ndUnsigned8> m_scratchBuffer;
154  ndArray<ndBodyKinematic*> m_sceneBodyArray;
155  ndArray<ndConstraint*> m_activeConstraintArray;
156  ndSpecialList<ndBodyKinematic> m_specialUpdateList;
157  ndThreadBackgroundWorker m_backgroundThread;
158  ndArray<ndContactPairs> m_newPairs;
159  ndArray<ndContactPairs> m_partialNewPairs[D_MAX_THREADS_COUNT];
160  ndPolygonMeshDesc::ndStaticMeshFaceQuery m_staticMeshQuery[D_MAX_THREADS_COUNT];
161  ndPolygonMeshDesc::ndProceduralStaticMeshFaceQuery m_proceduralStaticMeshQuery[D_MAX_THREADS_COUNT];
162 
163  ndSpinLock m_lock;
164  ndBvhNode* m_rootNode;
165  ndBodyKinematic* m_sentinelBody;
166  ndContactNotify* m_contactNotifyCallback;
167 
168  ndFloat32 m_timestep;
169  ndUnsigned32 m_lru;
170  ndUnsigned32 m_frameNumber;
171  ndUnsigned32 m_subStepNumber;
172  ndUnsigned32 m_forceBalanceSceneCounter;
173 
174  static ndVector m_velocTol;
175  static ndVector m_linearContactError2;
176  static ndVector m_angularContactError2;
177 
178  friend class ndWorld;
179  friend class ndBodyKinematic;
180  friend class ndRayCastNotify;
181  friend class ndPolygonMeshDesc;
182  friend class ndConvexCastNotify;
183  friend class ndSkeletonContainer;
184 } D_GCC_NEWTON_ALIGN_32 ;
185 
186 inline bool ndScene::IsValid() const
187 {
188  return true;
189 }
190 
191 inline bool ndScene::IsGPU() const
192 {
193  return false;
194 }
195 
196 inline double ndScene::GetGPUTime() const
197 {
198  return 0.0;
199 }
200 
201 inline ndWorld* ndScene::GetWorld() const
202 {
203  return nullptr;
204 }
205 
206 inline ndInt32 ndScene::GetThreadCount() const
207 {
208  const ndThreadPool& pool = *this;
209  return pool.GetThreadCount();
210 }
211 
212 inline ndArray<ndUnsigned8>& ndScene::GetScratchBuffer()
213 {
214  return m_scratchBuffer;
215 }
216 
217 inline const ndBodyListView& ndScene::GetBodyList() const
218 {
219  return m_bodyList;
220 }
221 
222 inline ndArray<ndConstraint*>& ndScene::GetActiveContactArray()
223 {
224  return m_activeConstraintArray;
225 }
226 
227 inline const ndContactArray& ndScene::GetContactArray() const
228 {
229  return m_contactArray;
230 }
231 
232 inline const ndArray<ndConstraint*>& ndScene::GetActiveContactArray() const
233 {
234  return m_activeConstraintArray;
235 }
236 
237 inline ndArray<ndBodyKinematic*>& ndScene::GetActiveBodyArray()
238 {
239  return m_bodyList.GetView();
240 }
241 
242 inline const ndArray<ndBodyKinematic*>& ndScene::GetActiveBodyArray() const
243 {
244  return m_bodyList.GetView();
245 }
246 
247 inline ndFloat32 ndScene::GetTimestep() const
248 {
249  return m_timestep;
250 }
251 
252 inline void ndScene::SetTimestep(ndFloat32 timestep)
253 {
254  m_timestep = timestep;
255 }
256 
257 inline ndBodyKinematic* ndScene::GetSentinelBody() const
258 {
259  return m_sentinelBody;
260 }
261 
262 #endif
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndSpinLock
Simple spin lock for synchronizing threads for very short period of time.
Definition: ndUtils.h:212
ndPolygonMeshDesc
Definition: ndPolygonMeshDesc.h:35
ndSharedPtr
Definition: ndSharedPtr.h:17
ndSkeletonContainer
Definition: ndSkeletonContainer.h:31
ndScene
Definition: ndScene.h:59
ndArray< ndBodyKinematic * >
ndThreadBackgroundWorker
Definition: ndThreadBackgroundWorker.h:66
ndContactNotify
Definition: ndContactNotify.h:62
ndContact
Definition: ndContact.h:97
ndSceneTreeNotiFy
Definition: ndScene.h:44
ndContactSolver
Definition: ndContactSolver.h:60
ndRayCastNotify
Definition: ndRayCastNotify.h:31
ndPolygonMeshDesc::ndStaticMeshFaceQuery
Definition: ndPolygonMeshDesc.h:38
ndBodiesInAabbNotify
Definition: ndBodiesInAabbNotify.h:30
ndJointBilateralConstraint
Definition: ndJointBilateralConstraint.h:53
ndBodyKinematic
Definition: ndBodyKinematic.h:40
ndContactArray
Definition: ndContactArray.h:29
ndBodyListView
Definition: ndBodyListView.h:47
ndBvhLeafNode
Definition: ndBvhNode.h:93
ndMatrix
Definition: ndMatrix.h:42
ndSpecialList< ndBodyKinematic >
ndBvhSceneManager
Definition: ndBvhNode.h:172
ndBvhNode
Definition: ndBvhNode.h:35
ndBackgroundTask
Definition: ndThreadBackgroundWorker.h:35
ndFastRay
Definition: ndFastRay.h:48
ndPolygonMeshDesc::ndProceduralStaticMeshFaceQuery
Definition: ndPolygonMeshDesc.h:63
ndScene::ndContactPairs
Definition: ndScene.h:62
ndThreadPool
Definition: ndThreadPool.h:65
ndConvexCastNotify
Definition: ndConvexCastNotify.h:34
ndShapeInstance
Definition: ndShapeInstance.h:62
ndVector
Definition: ndVectorArmNeon.h:41
ndWorld
Definition: ndWorld.h:47