Start a new topic

Problem with fltXmRotate

Original Post by: jrhea Wed Jun 29 19:27:58 2011


Hello,


I am having issues using fltXmRotate. I can set the fltXmRotateCenter and fltXmRotateAngle, but the fltXmRotateAxis doesn't take. Here is my code:



struct Point

{

double x;

double y;

double z;

};


void AddXForms (mgrec *node,Point translate,Vector3* axis, Float angle )

{

cout<<mgGetName(node)<<endl;

node = mgGetChild(node);

cout<<mgGetName(node)<<endl;


//Rotate

mgrec* xformRotate = mgNewRec (fltXmRotate);

mgSetCoord3d (xformRotate, fltXmRotateCenter, 0, 0, 0);

mgSetAttList (xformRotate,

fltVectorI, axis->x,

fltVectorJ, axis->y,

fltVectorK, axis->z,

mgNULL);

mgSetAttList (xformRotate, fltXmRotateAngle, angle, mgNULL);

mgAttach (node, xformRotate);


//Test

float i, j, k;

double x, y, z;

mgrec* xrec = mgGetXform(node);

mgGetCoord3d (xrec, fltXmRotateCenter, &x, &y, &z);

printf ("RotateCenter: %f, %f, %f\n", x, y, z);

mgGetAttList (xrec,

fltVectorI, &i,

fltVectorJ, &j,

fltVectorK, &k,

mgNULL);

printf ("RotateAxis: %f, %f, %f\n", i, j, k);

printf ("RotateAxis should be this: %f, %f, %f\n", axis->x, axis->y, axis->z);

mgGetAttList (xrec, fltXmRotateAngle, &angle;, mgNULL);

printf ("RotateAngle: %f\n", angle);


//Translate

mgrec* xformTranslate = mgNewRec(fltXmTranslate);

mgSetCoord3d(xformTranslate,fltXmTranslateFrom,0,0,0);

mgSetCoord3d(xformTranslate,fltXmTranslateDelta,translate.x,translate.y,translate.z);

//Puts the translation xform before the rotation xform

mgAttach(node,xformTranslate);

//Test

xrec = mgGetXform(node);

mgGetCoord3d (xrec, fltXmTranslateFrom, &x, &y, &z);

printf ("TranslateFrom xyz: %f, %f, %f\n", x, y, z);

mgGetCoord3d (xrec, fltXmTranslateDelta, &x, &y, &z);

printf ("TranslateDelta xyz: %f, %f, %f\n", x, y, z);


}


int main(int argc, char* argv[])

{

float u = 30*PI/180;

float i = 84*PI/180;


Matrix3x3* m = new Matrix3x3();

m->row[0].set(-sin(u)*cos(i),-sin(u)*-sin(i),cos(u));

m->row[1].set(-sin(i),cos(i),0);

m->row[2].set(-cos(u)*cos(i),-cos(u)*sin(i),-sin(u));

Quat* q = new Quat((Matrix3x3*) m);

//look at w,v

Vector3* axis = new Vector3();

Float angle = 0;

m->getAxisAngle(axis,angle);


//Quat* q1 = new Quat();

//q1->set(axis,angle);

//look at w,v


Point result;

mgrec* db;

char idname[80];

char *id = idname;


mgInit(&argc;, argv);


if (!(db = mgOpenDb(argv[1])))

{

printf("\nError in mgOpenDb(%s)\n", argv[1]);

exit(1);

}

result = GeodeticToGeocentric(u,i,0);

cout<<"Calculated coordinates (x,y,z): " << result.x << "," << result.y << "," << result.z << endl;

AddXForms(db,result,axis,angle);


return 0;

}


This is the output I get:


db

g1

RotateCenter: 0.000000, 0.000000, 0.000000

RotateAxis: 0.000000, 0.000000, 0.000000

RotateAxis should be this: -0.208057, 0.693214, -0.360366

RotateAngle: 3.902997

TranslateFrom xyz: 0.000000, 0.000000, 0.000000

TranslateDelta xyz: 577859.941915, 5497972.248549, 3170373.815528


As you can see my RotateAxis gets set to (0,0,0) and it should be (-0.208047,0.693214,-0.360366). What am I doing wrong? I am following the code from the User's Guide. Thanks in advance.


Original Post by: TedLai Wed Jun 29 21:00:52 2011


Alternatively, it may be more convenient to use mgSetVector and mgGetVector to set and get the record fltXmRotateAxis.

Original Post by: jrhea Wed Jun 29 20:33:33 2011


Ok I figured it out. I am posting the code for posterity:


void AddXForms (mgrec *node,Point translate,Vector3* axis, Float angle )

{

cout<<mgGetName(node)<<endl;

node = mgGetChild(node);

cout<<mgGetName(node)<<endl;


//Rotate

mgrec* xformRotate = mgNewRec (fltXmRotate);

mgSetCoord3d (xformRotate, fltXmRotateCenter, 0, 0, 0);

mgSetAttList (xformRotate, fltXmRotateAngle, angle, mgNULL);

mgrec* nestedAttrib = mgGetAttRec(xformRotate,fltXmRotateAxis,MG_NULL);

mgSetAttList (nestedAttrib,

fltVectorI, axis->x,

fltVectorJ, axis->y,

fltVectorK, axis->z,

mgNULL);

mgAttach (node, xformRotate);


//Test

float i, j, k;

double x, y, z;

mgrec* xrec = mgGetXform(node);

mgGetCoord3d (xrec, fltXmRotateCenter, &x, &y, &z);

printf ("RotateCenter: %f, %f, %f\n", x, y, z);

mgGetAttList (xrec,

fltVectorI, &i,

fltVectorJ, &j,

fltVectorK, &k,

mgNULL);

printf ("RotateAxis: %f, %f, %f\n", i, j, k);

printf ("RotateAxis should be this: %f, %f, %f\n", axis->x, axis->y, axis->z);

mgGetAttList (xrec, fltXmRotateAngle, &angle;, mgNULL);

printf ("RotateAngle: %f\n", angle);


//Translate

mgrec* xformTranslate = mgNewRec(fltXmTranslate);

mgSetCoord3d(xformTranslate,fltXmTranslateFrom,0,0,0);

mgSetCoord3d(xformTranslate,fltXmTranslateDelta,translate.x,translate.y,translate.z);

//Puts the translation xform before the rotation xform

mgAttach(node,xformTranslate);

//Test

xrec = mgGetXform(node);

mgGetCoord3d (xrec, fltXmTranslateFrom, &x, &y, &z);

printf ("TranslateFrom xyz: %f, %f, %f\n", x, y, z);

mgGetCoord3d (xrec, fltXmTranslateDelta, &x, &y, &z);

printf ("TranslateDelta xyz: %f, %f, %f\n", x, y, z);

}


FYI, I found the answer in the API Ref manual in the FAQ section. "How do I access a value in a record nested inside another record?"

Login to post a comment