RavEngine
Loading...
Searching...
No Matches
CmTransformUtils.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef CM_TRANSFORM_UTILS_H
30#define CM_TRANSFORM_UTILS_H
31
32#include "foundation/PxVecMath.h"
33
34namespace
35{
36
37using namespace physx::aos;
38
39// V3PrepareCross would help here, but it's not on all platforms yet...
40
41PX_FORCE_INLINE void transformFast(const FloatVArg wa, const Vec3VArg va, const Vec3VArg pa,
42 const FloatVArg wb, const Vec3VArg vb, const Vec3VArg pb,
43 FloatV& wo, Vec3V& vo, Vec3V& po)
44{
45 wo = FSub(FMul(wa, wb), V3Dot(va, vb));
46 vo = V3ScaleAdd(va, wb, V3ScaleAdd(vb, wa, V3Cross(va, vb)));
47
48 const Vec3V t1 = V3Scale(pb, FScaleAdd(wa, wa, FLoad(-0.5f)));
49 const Vec3V t2 = V3ScaleAdd(V3Cross(va, pb), wa, t1);
50 const Vec3V t3 = V3ScaleAdd(va, V3Dot(va, pb), t2);
51
52 po = V3ScaleAdd(t3, FLoad(2.f), pa);
53}
54
55PX_FORCE_INLINE void transformInvFast(const FloatVArg wa, const Vec3VArg va, const Vec3VArg pa,
56 const FloatVArg wb, const Vec3VArg vb, const Vec3VArg pb,
57 FloatV& wo, Vec3V& vo, Vec3V& po)
58{
59 wo = FScaleAdd(wa, wb, V3Dot(va, vb));
60 vo = V3NegScaleSub(va, wb, V3ScaleAdd(vb, wa, V3Cross(vb, va)));
61
62 const Vec3V pt = V3Sub(pb, pa);
63 const Vec3V t1 = V3Scale(pt, FScaleAdd(wa, wa, FLoad(-0.5f)));
64 const Vec3V t2 = V3ScaleAdd(V3Cross(pt, va), wa, t1);
65 const Vec3V t3 = V3ScaleAdd(va, V3Dot(va, pt), t2);
66 po = V3Add(t3,t3);
67}
68
69}
70
71namespace physx
72{
73namespace Cm
74{
75
76PX_FORCE_INLINE void getStaticGlobalPoseAligned(const PxTransform& actor2World, const PxTransform& shape2Actor, PxTransform& outTransform)
77{
78 using namespace aos;
79
80 PX_ASSERT((size_t(&actor2World)&15) == 0);
81 PX_ASSERT((size_t(&shape2Actor)&15) == 0);
82 PX_ASSERT((size_t(&outTransform)&15) == 0);
83
84 const Vec3V actor2WorldPos = V3LoadA(actor2World.p);
85 const QuatV actor2WorldRot = QuatVLoadA(&actor2World.q.x);
86
87 const Vec3V shape2ActorPos = V3LoadA(shape2Actor.p);
88 const QuatV shape2ActorRot = QuatVLoadA(&shape2Actor.q.x);
89
90 Vec3V v,p;
91 FloatV w;
92
93 transformFast(V4GetW(actor2WorldRot), Vec3V_From_Vec4V(actor2WorldRot), actor2WorldPos,
94 V4GetW(shape2ActorRot), Vec3V_From_Vec4V(shape2ActorRot), shape2ActorPos,
95 w, v, p);
96
97 V3StoreA(p, outTransform.p);
98 V4StoreA(V4SetW(v,w), &outTransform.q.x);
99}
100
101PX_FORCE_INLINE void getDynamicGlobalPoseAligned(const PxTransform& body2World, const PxTransform& shape2Actor, const PxTransform& body2Actor, PxTransform& outTransform)
102{
103 PX_ASSERT((size_t(&body2World)&15) == 0);
104 PX_ASSERT((size_t(&shape2Actor)&15) == 0);
105 PX_ASSERT((size_t(&body2Actor)&15) == 0);
106 PX_ASSERT((size_t(&outTransform)&15) == 0);
107
108 using namespace aos;
109
110 const Vec3V shape2ActorPos = V3LoadA(shape2Actor.p);
111 const QuatV shape2ActorRot = QuatVLoadA(&shape2Actor.q.x);
112
113 const Vec3V body2ActorPos = V3LoadA(body2Actor.p);
114 const QuatV body2ActorRot = QuatVLoadA(&body2Actor.q.x);
115
116 const Vec3V body2WorldPos = V3LoadA(body2World.p);
117 const QuatV body2WorldRot = QuatVLoadA(&body2World.q.x);
118
119 Vec3V v1, p1, v2, p2;
120 FloatV w1, w2;
121
122 transformInvFast(V4GetW(body2ActorRot), Vec3V_From_Vec4V(body2ActorRot), body2ActorPos,
123 V4GetW(shape2ActorRot), Vec3V_From_Vec4V(shape2ActorRot), shape2ActorPos,
124 w1, v1, p1);
125
126 transformFast(V4GetW(body2WorldRot), Vec3V_From_Vec4V(body2WorldRot), body2WorldPos,
127 w1, v1, p1,
128 w2, v2, p2);
129
130 V3StoreA(p2, outTransform.p);
131 V4StoreA(V4SetW(v2, w2), &outTransform.q.x);
132}
133
134}
135}
136
137#endif
float x
Definition PxQuat.h:395
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxVecMathAoSScalar.h:52
Definition PxVecMathAoSScalar.h:77
Definition PxVecMathAoSScalar.h:65