Transform* SceneParser::parseTransform() { char token[MAX_PARSER_TOKEN_LENGTH]; Matrix matrix; matrix.SetToIdentity(); Object3D *object = NULL; getToken(token); assert(!strcmp(token, "{")); // read in transformations: // apply to the LEFT side of the current matrix (so the first // transform in the list is the last applied to the object) getToken(token); while (1) { if (!strcmp(token, "Scale")) { matrix *= Matrix::MakeScale(readVec3f()); } else if (!strcmp(token, "UniformScale")) { float s = readFloat(); matrix *= Matrix::MakeScale(Vec3f(s, s, s)); } else if (!strcmp(token, "Translate")) { matrix *= Matrix::MakeTranslation(readVec3f()); } else if (!strcmp(token, "XRotate")) { matrix *= Matrix::MakeXRotation(DegreesToRadians(readFloat())); } else if (!strcmp(token, "YRotate")) { matrix *= Matrix::MakeYRotation(DegreesToRadians(readFloat())); } else if (!strcmp(token, "ZRotate")) { matrix *= Matrix::MakeZRotation(DegreesToRadians(readFloat())); } else if (!strcmp(token, "Rotate")) { getToken(token); assert(!strcmp(token, "{")); Vec3f axis = readVec3f(); float degrees = readFloat(); matrix *= Matrix::MakeAxisRotation(axis, DegreesToRadians(degrees)); getToken(token); assert(!strcmp(token, "}")); } else if (!strcmp(token, "Matrix")) { Matrix matrix2; matrix2.SetToIdentity(); getToken(token); assert(!strcmp(token, "{")); for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { float v = readFloat(); matrix2.Set(i, j, v); } } getToken(token); assert(!strcmp(token, "}")); matrix = matrix2 * matrix; } else { // otherwise this must be an object, // and there are no more transformations object = parseObject(token); break; } getToken(token); } assert(object != NULL); getToken(token); assert(!strcmp(token, "}")); return new Transform(matrix, object); }
Matrix Matrix::MakeTranslation(const Vec3f &v) { Matrix t; t.SetToIdentity(); t.data[0][3] = v.x(); t.data[1][3] = v.y(); t.data[2][3] = v.z(); return t; }
Matrix Matrix::MakeZRotation(float theta) { Matrix rz; rz.SetToIdentity(); rz.data[0][0]= (float)cos((float)theta); rz.data[0][1]=-(float)sin((float)theta); rz.data[1][0]= (float)sin((float)theta); rz.data[1][1]= (float)cos((float)theta); return rz; }
Matrix Matrix::MakeYRotation(float theta) { Matrix ry; ry.SetToIdentity(); ry.data[0][0]= (float)cos((float)theta); ry.data[0][2]= (float)sin((float)theta); ry.data[2][0]=-(float)sin((float)theta); ry.data[2][2]= (float)cos((float)theta); return ry; }
Matrix Matrix::MakeXRotation(float theta) { Matrix rx; rx.SetToIdentity(); rx.data[1][1]= (float)cos((float)theta); rx.data[1][2]=-(float)sin((float)theta); rx.data[2][1]= (float)sin((float)theta); rx.data[2][2]= (float)cos((float)theta); return rx; }
Matrix Matrix::MakeScale(const Vec3f &v) { Matrix s; s.SetToIdentity(); s.data[0][0] = v.x(); s.data[1][1] = v.y();; s.data[2][2] = v.z(); s.data[3][3] = 1; return s; }
Matrix Matrix::MakeAxisRotation(const Vec3f &v, float theta) { Matrix r; r.SetToIdentity(); float x = v.x(); float y = v.y(); float z = v.z(); float c = cosf(theta); float s = sinf(theta); float xx = x*x; float xy = x*y; float xz = x*z; float yy = y*y; float yz = y*z; float zz = z*z; r.Set(0, 0, (1-c)*xx + c); r.Set(0, 1, (1-c)*xy + z*s); r.Set(0, 2, (1-c)*xz - y*s); r.Set(0, 3, 0); r.Set(1, 0, (1-c)*xy - z*s); r.Set(1, 1, (1-c)*yy + c); r.Set(1, 2, (1-c)*yz + x*s); r.Set(1, 3, 0); r.Set(2, 0, (1-c)*xz + y*s); r.Set(2, 1, (1-c)*yz - x*s); r.Set(2, 2, (1-c)*zz + c); r.Set(2, 3, 0); r.Set(3,0, 0); r.Set(3,1, 0); r.Set(3,2, 0); r.Set(3,3, 1); return r; }
//用了彭老师书上介绍的求交方法,但是transform之后就有问题 bool Triangle::intersect(const Ray &r, Hit &h, float tmin) { Matrix A; A.SetToIdentity(); A.Set(0,0,a.x()-b.x()); A.Set(1,0,a.x()-c.x()); A.Set(2,0,r.getDirection().x()); A.Set(0,1,a.y()-b.y()); A.Set(1,1,a.y()-c.y()); A.Set(2,1,r.getDirection().y()); A.Set(0,2,a.z()-b.z()); A.Set(1,2,a.z()-c.z()); A.Set(2,2,r.getDirection().z()); Vec4f vr(a.x()-r.getOrigin().x(),a.y()-r.getOrigin().y(),a.z()-r.getOrigin().z(),1); if(fabs(r.getDirection().Dot3(normal))>0.0f) { A.Inverse(); A.Transform(vr); if((vr.x()+vr.y())<=1 && vr.x()>=0 && vr.y()>=0 && vr.z()>tmin && vr.z()<h.getT()) { h.set(vr.z(),material,normal,r); return 1; } } return 0; //为什么当使用transform之后,这里的求交函数就不能用了? 用没有加速的程序检测了一下,也无法求交,应该是这里的求交写错掉了 //Vec3f origin = r.getOrigin(); //Vec3f direct = r.getDirection(); //float isParallel = normal.Dot3(direct); //Vec3f tempbeta0; //Vec3f tempbeta1; //Vec3f tempbeta2; //float beta0; //float beta1; //float beta2; //if(fabs(isParallel)>0.0f) //{ // float dist = -(normal.Dot3(origin)+d)/isParallel; // Vec3f q = origin + direct*dist; // Vec3f::Cross3(tempbeta0,(c-b),(q-b)); // Vec3f::Cross3(tempbeta1,(a-c),(q-c)); // Vec3f::Cross3(tempbeta2,(b-a),(q-a)); // if(i0 == 0) // { // beta0 = tempbeta0.x()/normal.x(); // beta1 = tempbeta1.x()/normal.x(); // beta2 = tempbeta2.x()/normal.x(); // } // else if(i0 == 1) // { // beta0 = tempbeta0.y()/normal.y(); // beta1 = tempbeta1.y()/normal.y(); // beta2 = tempbeta2.y()/normal.y(); // } // else // { // beta0 = tempbeta0.z()/normal.z(); // beta1 = tempbeta1.z()/normal.z(); // beta2 = tempbeta2.z()/normal.z(); // } // if(beta0>=0 && beta0<=1 && beta1>=0 && beta1<=1 && beta2>=0 && beta2<=1) // { // if(dist > tmin && dist < h.getT()) // { // h.set(dist,material,normal,r); // return true; // } // } //} //return false; }
void RigidBodyFit( int natx, const Vector * const *xref, const Vector * const *xvar, const double *weights, Vector& cgref, Vector& cgvar, Matrix& rotMat) { Vector t, phi, cosin, sine; double rot[3][3], corlnmatrx[3][3]; int flag1, flag2, flag3; int i, j, iatv, ix; double an2, xx, f, fz=0.0, sgn, del, phix, phibes; double tol = .0001; // compute centroids cgref *= 0; cgvar *= 0; an2 = 0.; for (iatv=0; iatv<natx; ++iatv) { if (weights[iatv] <= 0.) continue; an2 += weights[iatv]; cgref += *(xref[iatv]) * weights[iatv]; cgvar += *(xvar[iatv]) * weights[iatv]; } cgref /= an2; cgvar /= an2; // compute correlation matrix for (i=0; i<3; ++i) { cosin[i] = 1.; for (j=0; j<3; ++j) { corlnmatrx[i][j] = 0.; } } for (iatv=0; iatv<natx; ++iatv) { for (i=0; i<3; ++i) { if (weights[iatv] <= 0.) continue; xx = ((*(xvar[iatv]))[i] - cgvar[i]) * weights[iatv]; for (j=0; j<3; ++j) { corlnmatrx[i][j] += xx * ((*(xref[iatv]))[j] - cgref[j]); } } } // evaluate rotation matrix iteratively flag1 = flag2 = flag3 = false; ix = 0; del = .5; sgn = 1.; phibes = phix = 0.; while (true) { cosin[ix] = cos(phix); sine[ix] = sin(phix); rot[0][0] = cosin[1] * cosin[2]; rot[1][0] = cosin[1] * sine[2]; rot[0][1] = sine[0] * sine[1] * cosin[2] - cosin[0] * sine[2]; rot[1][1] = sine[0] * sine[1] * sine[2] + cosin[0] * cosin[2]; rot[2][0] = -sine[1]; rot[2][1] = sine[0] * cosin[1]; rot[2][2] = cosin[0] * cosin[1]; rot[0][2] = cosin[0] * sine[1] * cosin[2] + sine[0] * sine[2]; rot[1][2] = cosin[0] * sine[1] * sine[2] - sine[0] * cosin[2]; // compute the trace of (rot x corlnmatrix) f = 0.; for (i=0; i<3; ++i) { for (j=0; j<3; ++j) { f += rot[i][j] * corlnmatrx[i][j]; } } if (!flag3) { fz = f; flag3 = true; phix = phibes + sgn * del; phi[ix] = phix; continue; } if (f > fz) { // f went down, try again with same difference. fz = f; flag1 = true; phibes = phi[ix]; flag2 = false; phix = phibes + sgn * del; phi[ix] = phix; continue; } if (!flag1) { // try in the opposite direction sgn = -sgn; flag1 = true; phix = phibes + sgn * del; phi[ix] = phix; continue; } phi[ix] = phibes; cosin[ix] = cos(phibes); sine[ix] = sin(phibes); flag1 = false; if (ix < 2) { // apply the same del to the next Euler angle ++ix; phibes = phi[ix]; phi[ix] = phix = phibes + sgn * del; continue; } if (flag2) { // cut del in half flag2 = false; del *= .5; if (del < tol) break; } else { flag2 = true; } ix = 0; phibes = phi[ix]; phix = phibes + sgn * del; phi[ix] = phix; } // end iterative evaluation of rotation matrix // store computed rotation matrix in rotMat rotMat.SetToIdentity(); for (i=0; i<3; ++i) { for (j=0; j<3; ++j) { rotMat[4*i + j] = rot[j][i]; } } } // end RigidBodyFit()