我在做什么错了?
在第一个打印中,值很模糊,在第二个打印中都是零。
第一次打印-q:0.965926 0.000000 0.258819 0.000000
第二次打印-q:0.000000 0.000000 0.000000 0.000000
float *AnglesToQaternion(float roll, float pitch, float yaw) { // Convert Euler angles in degrees to quaternions
static float q[4];
roll = roll * DEG_TO_RAD;
pitch = pitch * DEG_TO_RAD;
yaw = yaw * DEG_TO_RAD;
float t0 = cosf(yaw * 0.5);
float t1 = sinf(yaw * 0.5);
float t2 = cosf(roll * 0.5);
float t3 = sinf(roll * 0.5);
float t4 = cosf(pitch * 0.5);
float t5 = sinf(pitch * 0.5);
q[0] = t0 * t2 * t4 + t1 * t3 * t5;
q[1] = t0 * t3 * t4 - t1 * t2 * t5;
q[2] = t0 * t2 * t5 + t1 * t3 * t4;
q[3] = t1 * t2 * t4 - t0 * t3 * t5;
printf(">> q-1: %f %f %f %f \n", q[0], q[1], q[2], q[3]);
return q;
}
float q[4] = *AnglesToQaternion(0, 30.0, 0);
printf(">> q-2: %f %f %f %f \n", q[0], q[1], q[2], q[3]);
答案 0 :(得分:5)
根本不返回数组。
using degree = float;
struct Angles {
degree roll;
degree pitch;
degree yaw;
};
struct Quaternion {
float i;
float j;
float k;
float l;
};
Quaternion angles_to_quaternion(Angles angles)
{
float yaw = angles.yaw * DEG_TO_RAD;
float pitch = angles.pitch * DEG_TO_RAD;
float roll = angles.roll * DEG_TO_RAD;
float t0 = cosf(yaw * 0.5);
float t1 = sinf(yaw * 0.5);
float t2 = cosf(roll * 0.5);
float t3 = sinf(roll * 0.5);
float t4 = cosf(pitch * 0.5);
float t5 = sinf(pitch * 0.5);
return {
t0 * t2 * t4 + t1 * t3 * t5,
t0 * t3 * t4 - t1 * t2 * t5,
t0 * t2 * t5 + t1 * t3 * t4,
t1 * t2 * t4 - t0 * t3 * t5,
};
}
答案 1 :(得分:1)
在代码更改最少的情况下,函数和q应该如下所示:
#include <array>
std::array<float, 4> AnglesToQaternion(float roll, float pitch, float yaw)
{
std::array<float, 4> q{ 0,0,0,0 };
&使用如下功能:
std::array<float, 4> q{ 0,0,0,0 };
q = AnglesToQaternion(0, 30.0, 0);
答案 2 :(得分:0)
解决方案1:如果要能够从外部访问数组,则必须在函数内部动态创建数组。然后,您必须考虑删除它,以避免内存泄漏。
float *AnglesToQaternion(float roll, float pitch, float yaw) { // Convert Euler angles in degrees to quaternions
float *q = new float[4];
// Or C-style: float *q = malloc(sizeof(float)*4);
roll = roll * DEG_TO_RAD;
pitch = pitch * DEG_TO_RAD;
yaw = yaw * DEG_TO_RAD;
float t0 = cosf(yaw * 0.5);
float t1 = sinf(yaw * 0.5);
float t2 = cosf(roll * 0.5);
float t3 = sinf(roll * 0.5);
float t4 = cosf(pitch * 0.5);
float t5 = sinf(pitch * 0.5);
q[0] = t0 * t2 * t4 + t1 * t3 * t5;
q[1] = t0 * t3 * t4 - t1 * t2 * t5;
q[2] = t0 * t2 * t5 + t1 * t3 * t4;
q[3] = t1 * t2 * t4 - t0 * t3 * t5;
printf(">> q-1: %f %f %f %f \n", q[0], q[1], q[2], q[3]);
return q;
}
int main() {
float *q = AnglesToQaternion(0, 30.0, 0);
printf(">> q-2: %f %f %f %f \n", q[0], q[1], q[2], q[3]);
delete[] q;
// Or C-style: free(q);
}
解决方案2:您可以创建静态数组,然后将其第一个元素的地址传递给函数。
void AnglesToQaternion(float *q, float roll, float pitch, float yaw) { // Convert Euler angles in degrees to quaternions
roll = roll * DEG_TO_RAD;
pitch = pitch * DEG_TO_RAD;
yaw = yaw * DEG_TO_RAD;
float t0 = cosf(yaw * 0.5);
float t1 = sinf(yaw * 0.5);
float t2 = cosf(roll * 0.5);
float t3 = sinf(roll * 0.5);
float t4 = cosf(pitch * 0.5);
float t5 = sinf(pitch * 0.5);
q[0] = t0 * t2 * t4 + t1 * t3 * t5;
q[1] = t0 * t3 * t4 - t1 * t2 * t5;
q[2] = t0 * t2 * t5 + t1 * t3 * t4;
q[3] = t1 * t2 * t4 - t0 * t3 * t5;
printf(">> q-1: %f %f %f %f \n", q[0], q[1], q[2], q[3]);
}
int main() {
float q[4];
AnglesToQaternion(&q[0], 0, 30.0, 0);
printf(">> q-2: %f %f %f %f \n", q[0], q[1], q[2], q[3]);
}
第二种解决方案在性能上要好得多(如果可能,应尽可能避免使用new / delete)。
答案 3 :(得分:0)
正如您在问题下的注释中提到的那样,您不能真正返回数组,而是返回指针。如果您确实希望将其用作返回值,请确保在使用该函数之前为其分配内存。像这样:
float *f_array = malloc(sizeof(float)*lengthArray);
另一种方法是将数组作为参数传递给函数,然后将其填充到函数中。
void AnglesToQaternion(float roll, float pitch, float yaw, float resultArray[])
答案 4 :(得分:0)
将main()中本地q的地址传递给AnglesToQaternion以便填充。
void AnglesToQaternion(浮点滚动,浮点俯仰,浮点偏航,浮点*四元数)
答案 5 :(得分:0)
Caleth已经写出了答案的答案。但是我认为您需要开始考虑使用C ++而不是C。这是一个完整的示例:
Android.Support.V4.App.Fragment fragment = null;
fragment = Fragment3.NewInstance();
if (fragment == null)
return;
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();