1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | |
31 | #ifndef PX_PHYSICS_NP_ARTICULATION_JOINT |
32 | #define PX_PHYSICS_NP_ARTICULATION_JOINT |
33 | |
34 | #include "PxArticulationJoint.h" |
35 | #include "ScbArticulationJoint.h" |
36 | #include "NpArticulationTemplate.h" |
37 | |
38 | #if PX_ENABLE_DEBUG_VISUALIZATION |
39 | #include "CmRenderOutput.h" |
40 | #endif |
41 | |
42 | namespace physx |
43 | { |
44 | |
45 | class NpScene; |
46 | class NpArticulationLink; |
47 | |
48 | class PxArticulationJointImpl |
49 | { |
50 | //= ATTENTION! ===================================================================================== |
51 | // Changing the data layout of this class breaks the binary serialization format. See comments for |
52 | // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData |
53 | // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION |
54 | // accordingly. |
55 | //================================================================================================== |
56 | public: |
57 | Scb::ArticulationJoint mJoint; |
58 | NpArticulationLink* mParent; |
59 | NpArticulationLink* mChild; |
60 | |
61 | PX_INLINE PxArticulationJointImpl(NpArticulationLink& parent, |
62 | const PxTransform& parentFrame, |
63 | NpArticulationLink& child, |
64 | const PxTransform& childFrame, |
65 | bool reducedCoordinate); |
66 | |
67 | // PX_SERIALIZATION |
68 | PX_INLINE PxArticulationJointImpl(const PxEMPTY) : mJoint(PxEmpty) {} |
69 | PX_INLINE void resolveReferences(PxDeserializationContext& context, PxArticulationJointBase& pxArticulationJoint); |
70 | static void getBinaryMetaData(PxOutputStream& stream); |
71 | //~PX_SERIALIZATION |
72 | |
73 | PX_INLINE NpScene* getOwnerScene() const; // the scene the user thinks the actor is in, or from which the actor is pending removal |
74 | |
75 | PX_INLINE void release(); |
76 | |
77 | PX_INLINE const Scb::ArticulationJoint& getScbArticulationJoint() const { return mJoint; } |
78 | PX_INLINE Scb::ArticulationJoint& getScbArticulationJoint() { return mJoint; } |
79 | |
80 | PX_INLINE PxArticulationLink& getParentArticulationLink() const; |
81 | PX_INLINE PxArticulationLink& getChildArticulationLink() const; |
82 | |
83 | PX_INLINE PxTransform getParentPose() const; |
84 | PX_INLINE void setParentPose(const PxTransform&); |
85 | |
86 | PX_INLINE PxTransform getChildPose() const; |
87 | PX_INLINE void setChildPose(const PxTransform&); |
88 | |
89 | PX_INLINE const NpArticulationLink& getParent() const { return *mParent; } |
90 | PX_INLINE NpArticulationLink& getParent() { return *mParent; } |
91 | |
92 | PX_INLINE const NpArticulationLink& getChild() const { return *mChild; } |
93 | PX_INLINE NpArticulationLink& getChild() { return *mChild; } |
94 | }; |
95 | |
96 | template <typename APIClass> |
97 | class NpArticulationJointTemplate : public APIClass, public Ps::UserAllocated |
98 | { |
99 | //= ATTENTION! ===================================================================================== |
100 | // Changing the data layout of this class breaks the binary serialization format. See comments for |
101 | // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData |
102 | // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION |
103 | // accordingly. |
104 | //================================================================================================== |
105 | public: |
106 | |
107 | // PX_SERIALIZATION |
108 | NpArticulationJointTemplate(PxBaseFlags baseFlags) : APIClass(baseFlags), mImpl(PxEmpty) {} |
109 | void preExportDataReset() { mImpl.mJoint.preExportDataReset(); } |
110 | //~PX_SERIALIZATION |
111 | |
112 | NpArticulationJointTemplate(PxType concreteType, NpArticulationLink& parent, const PxTransform& parentFrame, NpArticulationLink& child, const PxTransform& childFrame); |
113 | virtual ~NpArticulationJointTemplate() {} |
114 | |
115 | //--------------------------------------------------------------------------------- |
116 | // Miscellaneous |
117 | //--------------------------------------------------------------------------------- |
118 | public: |
119 | void release(); |
120 | |
121 | PX_INLINE const Scb::ArticulationJoint& getScbArticulationJoint() const { return mImpl.getScbArticulationJoint(); } |
122 | PX_INLINE Scb::ArticulationJoint& getScbArticulationJoint() { return mImpl.getScbArticulationJoint(); } |
123 | |
124 | virtual PxArticulationLink& getParentArticulationLink() const { return mImpl.getParentArticulationLink(); } |
125 | virtual PxArticulationLink& getChildArticulationLink() const { return mImpl.getChildArticulationLink(); } |
126 | |
127 | virtual PxTransform getParentPose() const { return mImpl.getParentPose(); } |
128 | virtual void setParentPose(const PxTransform& t) { mImpl.setParentPose(t); } |
129 | |
130 | virtual PxTransform getChildPose() const { return mImpl.getChildPose(); } |
131 | virtual void setChildPose(const PxTransform& t) { mImpl.setChildPose(t); } |
132 | |
133 | PX_INLINE const NpArticulationLink& getParent() const { return mImpl.getParent(); } |
134 | PX_INLINE NpArticulationLink& getParent() { return mImpl.getParent(); } |
135 | |
136 | PX_INLINE const NpArticulationLink& getChild() const { return mImpl.getChild(); } |
137 | PX_INLINE NpArticulationLink& getChild() { return mImpl.getChild(); } |
138 | |
139 | virtual PxArticulationJointImpl* getImpl() { return &mImpl; } |
140 | virtual const PxArticulationJointImpl* getImpl() const { return &mImpl; } |
141 | |
142 | public: |
143 | NpScene* getOwnerScene() const { return mImpl.getOwnerScene(); } |
144 | |
145 | PxArticulationJointImpl mImpl; |
146 | }; |
147 | |
148 | class NpArticulationJoint : public NpArticulationJointTemplate<PxArticulationJoint> |
149 | { |
150 | //= ATTENTION! ===================================================================================== |
151 | // Changing the data layout of this class breaks the binary serialization format. See comments for |
152 | // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData |
153 | // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION |
154 | // accordingly. |
155 | //================================================================================================== |
156 | public: |
157 | // PX_SERIALIZATION |
158 | NpArticulationJoint(PxBaseFlags baseFlags) : NpArticulationJointTemplate(baseFlags) {} |
159 | virtual void resolveReferences(PxDeserializationContext& context); |
160 | static NpArticulationJoint* createObject(PxU8*& address, PxDeserializationContext& context); |
161 | static void getBinaryMetaData(PxOutputStream& stream); |
162 | void (PxSerializationContext&) {} |
163 | void (PxDeserializationContext&) {} |
164 | virtual void requiresObjects(PxProcessPxBaseCallback&){} |
165 | virtual bool isSubordinate() const { return true; } |
166 | //~PX_SERIALIZATION |
167 | NpArticulationJoint(NpArticulationLink& parent, const PxTransform& parentFrame, NpArticulationLink& child, const PxTransform& childFrame); |
168 | virtual ~NpArticulationJoint(); |
169 | |
170 | //--------------------------------------------------------------------------------- |
171 | // PxArticulationJoint implementation |
172 | //--------------------------------------------------------------------------------- |
173 | |
174 | virtual void setTargetOrientation(const PxQuat&); |
175 | virtual PxQuat getTargetOrientation() const; |
176 | |
177 | virtual void setTargetVelocity(const PxVec3&); |
178 | virtual PxVec3 getTargetVelocity() const; |
179 | |
180 | virtual void setDriveType(PxArticulationJointDriveType::Enum driveType); |
181 | virtual PxArticulationJointDriveType::Enum getDriveType() const; |
182 | |
183 | virtual void setStiffness(PxReal); |
184 | virtual PxReal getStiffness() const; |
185 | |
186 | virtual void setDamping(PxReal); |
187 | virtual PxReal getDamping() const; |
188 | |
189 | virtual void setInternalCompliance(PxReal); |
190 | virtual PxReal getInternalCompliance() const; |
191 | |
192 | virtual void setExternalCompliance(PxReal); |
193 | virtual PxReal getExternalCompliance() const; |
194 | |
195 | virtual void setSwingLimit(PxReal yLimit, PxReal zLimit); |
196 | virtual void getSwingLimit(PxReal &yLimit, PxReal &zLimit) const; |
197 | |
198 | virtual void setTangentialStiffness(PxReal spring); |
199 | virtual PxReal getTangentialStiffness() const; |
200 | |
201 | virtual void setTangentialDamping(PxReal damping); |
202 | virtual PxReal getTangentialDamping() const; |
203 | |
204 | virtual void setSwingLimitEnabled(bool); |
205 | virtual bool getSwingLimitEnabled() const; |
206 | |
207 | virtual void setSwingLimitContactDistance(PxReal contactDistance); |
208 | virtual PxReal getSwingLimitContactDistance() const; |
209 | |
210 | virtual void setTwistLimit(PxReal lower, PxReal upper); |
211 | virtual void getTwistLimit(PxReal &lower, PxReal &upper) const; |
212 | |
213 | virtual void setTwistLimitEnabled(bool); |
214 | virtual bool getTwistLimitEnabled() const; |
215 | |
216 | virtual void setTwistLimitContactDistance(PxReal contactDistance); |
217 | virtual PxReal getTwistLimitContactDistance() const; |
218 | |
219 | virtual void setJointType(PxArticulationJointType::Enum jointType); |
220 | virtual PxArticulationJointType::Enum getJointType() const; |
221 | |
222 | virtual void setMotion(PxArticulationAxis::Enum axis, PxArticulationMotion::Enum motion); |
223 | virtual PxArticulationMotion::Enum getMotion(PxArticulationAxis::Enum axis) const; |
224 | |
225 | virtual void setFrictionCoefficient(const PxReal coefficient); |
226 | virtual PxReal getFrictionCoefficient() const ; |
227 | |
228 | //--------------------------------------------------------------------------------- |
229 | // Miscellaneous |
230 | //--------------------------------------------------------------------------------- |
231 | static PX_INLINE size_t getScbArticulationJointOffset() |
232 | { |
233 | return PX_OFFSET_OF_RT(NpArticulationJoint, mImpl.mJoint); |
234 | } |
235 | }; |
236 | |
237 | PX_INLINE PxArticulationLink& PxArticulationJointImpl::getParentArticulationLink() const |
238 | { |
239 | return *mParent; |
240 | } |
241 | |
242 | PX_INLINE PxArticulationLink& PxArticulationJointImpl::getChildArticulationLink() const |
243 | { |
244 | return *mChild; |
245 | } |
246 | |
247 | PX_INLINE PxTransform PxArticulationJointImpl::getParentPose() const |
248 | { |
249 | NP_READ_CHECK(getOwnerScene()); |
250 | return mParent->getCMassLocalPose().transform(src: mJoint.getParentPose()); |
251 | } |
252 | |
253 | PX_INLINE void PxArticulationJointImpl::setParentPose(const PxTransform& t) |
254 | { |
255 | PX_CHECK_AND_RETURN(t.isSane(), "NpArticulationJoint::setParentPose t is not valid." ); |
256 | |
257 | NP_WRITE_CHECK(getOwnerScene()); |
258 | if (mParent == NULL) |
259 | return; |
260 | |
261 | mJoint.setParentPose(mParent->getCMassLocalPose().transformInv(src: t.getNormalized())); |
262 | } |
263 | |
264 | PX_INLINE PxTransform PxArticulationJointImpl::getChildPose() const |
265 | { |
266 | NP_READ_CHECK(getOwnerScene()); |
267 | |
268 | return mChild->getCMassLocalPose().transform(src: mJoint.getChildPose()); |
269 | } |
270 | |
271 | PX_INLINE void PxArticulationJointImpl::setChildPose(const PxTransform& t) |
272 | { |
273 | PX_CHECK_AND_RETURN(t.isSane(), "NpArticulationJoint::setChildPose t is not valid." ); |
274 | |
275 | NP_WRITE_CHECK(getOwnerScene()); |
276 | |
277 | mJoint.setChildPose(mChild->getCMassLocalPose().transformInv(src: t.getNormalized())); |
278 | } |
279 | |
280 | PX_INLINE NpScene* PxArticulationJointImpl::getOwnerScene() const |
281 | { |
282 | return mJoint.getScbScene() ? static_cast<NpScene *>(mJoint.getScbScene()->getPxScene()) : NULL; |
283 | } |
284 | |
285 | PX_INLINE PxArticulationJointImpl::PxArticulationJointImpl(NpArticulationLink& parent, |
286 | const PxTransform& parentFrame, |
287 | NpArticulationLink& child, |
288 | const PxTransform& childFrame, |
289 | bool reducedCoordinate) |
290 | : mJoint(parentFrame, childFrame, reducedCoordinate) |
291 | , mParent(&parent) |
292 | , mChild(&child) |
293 | { |
294 | PxArticulationImpl* impl = parent.getRoot().getImpl(); |
295 | mJoint.setScArticulation(&impl->getScbArticulation()); |
296 | } |
297 | |
298 | PX_INLINE void PxArticulationJointImpl::release() |
299 | { |
300 | if (mJoint.getScbSceneForAPI()) |
301 | mJoint.getScbSceneForAPI()->removeArticulationJoint(mJoint); |
302 | |
303 | mJoint.destroy(); |
304 | } |
305 | |
306 | PX_INLINE void PxArticulationJointImpl::resolveReferences(PxDeserializationContext& context, PxArticulationJointBase& pxArticulationJoint) |
307 | { |
308 | context.translatePxBase(base&: mParent); |
309 | context.translatePxBase(base&: mChild); |
310 | getScbArticulationJoint().getScArticulationJoint().setRoot(&pxArticulationJoint); |
311 | //articulation backlink can't be set yet, because articulation wasn't constructed yet |
312 | //see NpArticulationTemplate<APIClass>::resolveReferences |
313 | } |
314 | |
315 | template <typename APIClass> |
316 | void NpArticulationJointTemplate<APIClass>::release() |
317 | { |
318 | NpPhysics::getInstance().notifyDeletionListenersUserRelease(b: this, NULL); |
319 | mImpl.release(); |
320 | } |
321 | |
322 | template <typename APIClass> |
323 | NpArticulationJointTemplate<APIClass>::NpArticulationJointTemplate( |
324 | PxType concreteType, |
325 | NpArticulationLink& parent, |
326 | const PxTransform& parentFrame, |
327 | NpArticulationLink& child, |
328 | const PxTransform& childFrame) |
329 | : APIClass(concreteType, PxBaseFlag::eOWNS_MEMORY), |
330 | mImpl(parent, parentFrame, child, childFrame, concreteType == PxConcreteType::eARTICULATION_JOINT_REDUCED_COORDINATE) |
331 | { |
332 | mImpl.getScbArticulationJoint().getScArticulationJoint().setRoot(this); |
333 | } |
334 | |
335 | } |
336 | |
337 | #endif |
338 | |