Skip to content

Commit a3d0845

Browse files
authored
Fix azimuth and elevation fields values for AWSIM (#278)
* Fix azimuth and elevation fields values for AWSIM * Review changes
1 parent c1f0a38 commit a3d0845

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

include/rgl/api/core.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,18 @@ typedef enum : int32_t
309309
RGL_FIELD_RAY_IDX_U32,
310310
RGL_FIELD_ENTITY_ID_I32,
311311
RGL_FIELD_DISTANCE_F32,
312-
RGL_FIELD_AZIMUTH_F32, // In radians. Assuming up vector is Y, forward vector is Z.
313-
RGL_FIELD_ELEVATION_F32, // In radians. Assuming up vector is Y, forward vector is Z.
312+
/**
313+
* Azimuth angle of the hit point in radians.
314+
* Currently only compatible with engines that generate rays as follows:
315+
* uses a left-handed coordinate system, rotation applies in ZXY order, up vector is Y, forward vector is Z
316+
*/
317+
RGL_FIELD_AZIMUTH_F32,
318+
/**
319+
* Elevation angle of the hit point in radians.
320+
* Currently only compatible with engines that generate rays as follows:
321+
* uses a left-handed coordinate system, rotation applies in ZXY order, up vector is Y, forward vector is Z
322+
*/
323+
RGL_FIELD_ELEVATION_F32,
314324
RGL_FIELD_RING_ID_U16,
315325
RGL_FIELD_RETURN_TYPE_U8,
316326
RGL_FIELD_TIME_STAMP_F64,

src/gpu/optixPrograms.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ extern "C" __global__ void __raygen__()
128128

129129
// Assuming up vector is Y, forward vector is Z (true for Unity).
130130
// TODO(msz-rai): allow to define up and forward vectors in RGL
131+
// Assuming rays are generated in left-handed coordinate system with the rotation applied in ZXY order.
132+
// TODO(msz-rai): move ray generation to RGL to unify rotations
131133
if (ctx.azimuth != nullptr) {
132-
ctx.azimuth[rayIdx] = rayLocal.toRotationYRad();
134+
ctx.azimuth[rayIdx] = rayLocal.toRotationYOrderZXYLeftHandRad();
133135
}
134136
if (ctx.elevation != nullptr) {
135-
ctx.elevation[rayIdx] = rayLocal.toRotationXRad();
137+
ctx.elevation[rayIdx] = rayLocal.toRotationXOrderZXYLeftHandRad();
136138
}
137139

138140
if (ctx.doApplyDistortion) {

src/math/Mat3x4f.hpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,36 @@ struct Mat3x4f
143143
return {rc[0][0], rc[0][1], rc[0][2], 0.0f, rc[1][0], rc[1][1], rc[1][2], 0.0f, rc[2][0], rc[2][1], rc[2][2], 0.0f};
144144
}
145145

146-
HostDevFn inline float toRotationXRad() const { return atan2f(rc[2][1], rc[2][2]); }
147-
148-
HostDevFn inline float toRotationYRad() const { return atan2f(-rc[2][0], sqrtf(powf(rc[2][1], 2) + powf(rc[2][2], 2))); }
149-
150-
HostDevFn inline float toRotationZRad() const { return atan2f(rc[1][0], rc[0][0]); }
146+
HostDevFn inline float toRotationXOrderZXYLeftHandRad() const
147+
{
148+
// Assuming rotation has been applied in the order: z x y in left-handed coordinate system
149+
// Based on: https://www.geometrictools.com/Documentation/EulerAngles.pdf
150+
// Taking transpose of the rotation matrix, because X axis facing opposite direction in left-handed coordinate system
151+
if (rc[1][2] < 1) {
152+
if (rc[1][2] > -1) {
153+
return asinf(rc[1][2]);
154+
} else {
155+
return -static_cast<float>(M_PI) / 2.0f;
156+
}
157+
} else {
158+
return static_cast<float>(M_PI) / 2.0f;
159+
}
160+
}
151161

152-
HostDevFn inline Vec3f toRotationRad() const { return {toRotationXRad(), toRotationYRad(), toRotationZRad()}; }
162+
HostDevFn inline float toRotationYOrderZXYLeftHandRad() const
163+
{
164+
// Assuming rotation has been applied in the order: z x y in left-handed coordinate system
165+
// Based on: https://www.geometrictools.com/Documentation/EulerAngles.pdf
166+
if (rc[2][1] < 1) {
167+
if (rc[2][1] > -1) {
168+
return atan2f(-rc[2][0], rc[2][2]);
169+
} else {
170+
return 0.0f;
171+
}
172+
} else {
173+
return 0.0f;
174+
}
175+
}
153176

154177
HostDevFn inline Vec3f scaleVec() const
155178
{

0 commit comments

Comments
 (0)