double Trajectory::trajProb(const void* pars, int last) const { double w2; double dw = 1.0; EpiState x(curState); const TransitionType* tt; int ntrans = transitionCount(); if (ntrans <= last) return 0.0; // Events that were not included in the tree if (ntrans > 0) { for (int i = ntrans-1; i > last; --i) { tt = getTrans(i).getType(); w2 = tt->applyProb(x,pars); dw *= w2; x -= getTrans(i).getTrans(); // if (w2 <= 0.0) { // cerr << getTrans(i).realTime() // << "[" << i << "] >> " // << tt->getName() << " " // << tt->applyProb(curState,pars) << endl; // } } } return dw; }
bool AnalysisString(string str,tablenode* Table,charset* NotEnd) //字串语法分析 { int len=str.length(); string str2=str; int len3=str2.length(); char* sstr=(char*)str.c_str(); string storage="#S"; int len2=storage.length(); printf("\n分析栈 接收字串 \n"); //输出状态至控制台,可注释 printf("%s %s \n",storage.c_str(),sstr); int i=0; char* sto=(char*)storage.c_str(); while(getTrans(sto[len2-1],sstr[0],Table,NotEnd)!=NULL&&len3!=0) { charset* temp=getTrans(sto[len2-1],sstr[0],Table,NotEnd); string temp2=getTransto(temp); char* sto3=(char*)storage.c_str(); sss.str(""); for(int j=0;j<len2-1;j++) sss<<sto3[j]; storage=sss.str()+temp2; len2=storage.length(); sto3=(char*)storage.c_str(); char* str3=(char*)str2.c_str(); while(sto3[len2-1]==str3[0]) { printf("%s %s \n",storage.c_str(),str2.c_str()); sss.str(""); for(int k=1;k<len3;k++) sss<<str3[k]; str2=sss.str(); len3=str2.length(); sss.str(""); for(int l=0;l<len2-1;l++) sss<<sto3[l]; storage=sss.str(); len2=storage.length(); sss.str(""); if(len3!=1||len2!=1) printf("%s %s \n",storage.c_str(),str2.c_str()); sto3=(char*)storage.c_str(); str3=(char*)str2.c_str(); sstr=(char*)str2.c_str(); if(storage==""&&str2=="") return true; } sto=sto3; continue; } return false; }
void DFA::DFAState::addTransition(int stateD, int symbol) { if (useTable(symbol)) { lookup_.set(symbol - MIN_ASCII, (stateNum)(stateD+1)); } else { // store symbol in ordered list as an integer, // which will be sorted by symbol, so store symbol in // bits 16..31. int tblCode = constructOrdListVal(symbol,stateD); ordList_.add(tblCode); if (!lookupUsed_ && ordList_.length() > MAX_ORDLIST_LENGTH) { lookupUsed_ = true; lookup_.ensureCapacity(MAX_ASCII + 1 - MIN_ASCII); for (int i = MIN_ASCII; i <= MAX_ASCII; i++) lookup_.add(0); for (int i = 0; i < ordList_.length(); i++) { int n = ordList_.itemAt(i); int sym = getSym(n); // int sym = n >> 16; if (sym <= MAX_ASCII) { // store in lookup table, and remove from set. lookup_.set(sym - MIN_ASCII, (stateNum)(getTrans(n)+1) ); ordList_.remove(n); i--; } } } } }
SEXP Dsyrk(SEXP ENV, SEXP A, SEXP C, SEXP ALPHA, SEXP BETA, SEXP TRANSA, SEXP UPLO) { cl_env *env = get_env(ENV); int allocated = 0; MATRIXCHECK(A, REALSXP); SCALARCHECK(ALPHA, REALSXP); SCALARCHECK(BETA, REALSXP); clblasTranspose transA = getTrans(TRANSA); clblasUplo uplo = getUplo(UPLO); double alpha = SCALARREAL(ALPHA), beta = SCALARREAL(BETA); int ar = nrows(A), ac = ncols(A), n; if (isNull(C) || LENGTH(C) == 0) { n = transA == clblasNoTrans ? ar : ac; C = PROTECT(allocMatrix(REALSXP, n, n)); beta = 0; allocated = 1; } else { MATRIXCHECK(C, REALSXP); n = nrows(C); } int size_a = LENGTH(A) * sizeof(double); int size_c = LENGTH(C) * sizeof(double); double *ap = REAL(A), *cp = REAL(C); cl_int err = Dsyrk_internal( env, ap, cp, alpha, beta, transA, uplo, ar, ac, n, size_a, size_c); CHECK(err); UNPROTECT(allocated); return C; }
SEXP Dsyr2k(SEXP ENV, SEXP A, SEXP B, SEXP C, SEXP ALPHA, SEXP BETA, SEXP TRANSAB, SEXP UPLO) { cl_env *env = get_env(ENV); int allocated = 0; MATRIXCHECK(A, REALSXP); MATRIXCHECK(B, REALSXP); SCALARCHECK(ALPHA, REALSXP); SCALARCHECK(BETA, REALSXP); clblasTranspose transAB = getTrans(TRANSAB); clblasUplo uplo = getUplo(UPLO); double alpha = SCALARREAL(ALPHA), beta = SCALARREAL(BETA); int ar = nrows(A), ac = ncols(A), br = nrows(B), bc = ncols(B), cr, cc; if (isNull(C) || LENGTH(C) == 0) { cr = transAB == clblasNoTrans ? ar : ac; cc = transAB == clblasNoTrans ? br : bc; C = PROTECT(allocMatrix(REALSXP, cr, cc)); beta = 0; allocated = 1; } else { MATRIXCHECK(C, REALSXP); cr = nrows(C), cc = ncols(C); } int size_a = LENGTH(A) * sizeof(double); int size_b = LENGTH(B) * sizeof(double); int size_c = LENGTH(C) * sizeof(double); double *ap = REAL(A), *bp = REAL(B), *cp = REAL(C); cl_int err = Dsyr2k_internal( env, ap, bp, cp, alpha, beta, transAB, uplo, ar, ac, br, bc, cr, cc, size_a, size_b, size_c); CHECK(err); UNPROTECT(allocated); return C; }
// ---------------------------------------------------------------------------- void KartRewinder::computeError() { //btTransform error = getTrans() - m_saved_transform; Vec3 pos_error = getTrans().getOrigin() - m_saved_transform.getOrigin(); btQuaternion rot_error(0, 0, 0, 1); Kart::addError(pos_error, rot_error); } // computeError
void Flyable::getClosestKart(const AbstractKart **minKart, float *minDistSquared, Vec3 *minDelta, const AbstractKart* inFrontOf, const bool backwards) const { btTransform trans_projectile = (inFrontOf != NULL ? inFrontOf->getTrans() : getTrans()); *minDistSquared = 999999.9f; *minKart = NULL; World *world = World::getWorld(); for(unsigned int i=0 ; i<world->getNumKarts(); i++ ) { AbstractKart *kart = world->getKart(i); // If a kart has star effect shown, the kart is immune, so // it is not considered a target anymore. if(kart->isEliminated() || kart == m_owner || kart->isInvulnerable() || kart->getKartAnimation() ) continue; btTransform t=kart->getTrans(); Vec3 delta = t.getOrigin()-trans_projectile.getOrigin(); // the Y distance is added again because karts above or below should// // not be prioritized when aiming float distance2 = delta.length2() + std::abs(t.getOrigin().getY() - trans_projectile.getOrigin().getY())*2; if(inFrontOf != NULL) { // Ignore karts behind the current one Vec3 to_target = kart->getXYZ() - inFrontOf->getXYZ(); const float distance = to_target.length(); if(distance > 50) continue; // kart too far, don't aim at it btTransform trans = inFrontOf->getTrans(); // get heading=trans.getBasis*(0,0,1) ... so save the multiplication: Vec3 direction(trans.getBasis().getColumn(2)); // Originally it used angle = to_target.angle( backwards ? -direction : direction ); // but sometimes due to rounding errors we get an acos(x) with x>1, causing // an assertion failure. So we remove the whole acos() test here and copy the // code from to_target.angle(...) Vec3 v = backwards ? -direction : direction; float s = sqrt(v.length2() * to_target.length2()); float c = to_target.dot(v)/s; // Original test was: fabsf(acos(c))>1, which is the same as // c<cos(1) (acos returns values in [0, pi] anyway) if(c<0.54) continue; } if(distance2 < *minDistSquared) { *minDistSquared = distance2; *minKart = kart; *minDelta = delta; } } // for i<getNumKarts } // getClosestKart
/** Updates the current position and rotation. This function is also called * by ghost karts for getHeading() to work. */ void Moveable::updatePosition() { Vec3 forw_vec = m_transform.getBasis().getColumn(2); m_heading = atan2f(forw_vec.getX(), forw_vec.getZ()); // The pitch in hpr is in between -pi and pi. But for the camera it // must be restricted to -pi/2 and pi/2 - so recompute it by restricting // y to positive values, i.e. no pitch of more than pi/2. Vec3 up = getTrans().getBasis().getColumn(1); m_pitch = atan2(up.getZ(), fabsf(up.getY())); m_roll = atan2(up.getX(), up.getY()); } // updatePosition
void Trajectory::printFromLast(size_t last) const { EpiState curState(getState()); size_t ntrans = transitionCount(); // Events that were not included in the tree if (ntrans > 0) { for (size_t i = ntrans-1; i > last; --i) { cout << curState << endl; StateTransition st = getTrans(i); curState -= st.getTrans(); } } }
int DFA::DFAState::getTransitionState(int symbol) const { int out = -1; if (useTable(symbol)) { out = ((int)(lookup_.itemAt(symbol - MIN_ASCII)))-1; } else { for (int i = 0; i < ordList_.length(); i++) { int n = ordList_.itemAt(i); if (getSym(n) == symbol) { out = getTrans(n); break; } } } return out; }
GameObject::GameObject(std::string _name, osg::Vec3f _pos, float _colRad, int _hp, std::string _model, osg::ref_ptr<osg::MatrixTransform> _scene, int _id) { initTransform(); setVel(0.0); setDir(osg::Vec3f(0.0f, 0.0f, 1.0f)); setOrientation(osg::Quat(0.0f, 0.0f, 0.0f, 1.0f)); rigidBodyRadius = _colRad; translate(_pos); setName(_name); setHP(_hp); setDescr((std::string)("hej")); _scene->addChild(getTrans()); setID(_id); setModel(_model); }
void Flyable::setAnimation(AbstractKartAnimation *animation) { if (animation) { assert(m_animation == NULL); Physics::getInstance()->removeBody(getBody()); } else // animation = NULL { assert(m_animation != NULL); m_body->setWorldTransform(getTrans()); Physics::getInstance()->addBody(getBody()); } m_animation = animation; } // addAnimation
/** Updates the current position and rotation from the corresponding physics * body, and then calls updateGraphics to position the model correctly. * \param float dt Time step size. */ void Moveable::update(float dt) { if(m_body->getInvMass()!=0) m_motion_state->getWorldTransform(m_transform); m_velocityLC = getVelocity()*m_transform.getBasis(); Vec3 forw_vec = m_transform.getBasis().getColumn(0); m_heading = -atan2f(forw_vec.getZ(), forw_vec.getX()); // The pitch in hpr is in between -pi and pi. But for the camera it // must be restricted to -pi/2 and pi/2 - so recompute it by restricting // y to positive values, i.e. no pitch of more than pi/2. Vec3 up = getTrans().getBasis().getColumn(1); m_pitch = atan2(up.getZ(), fabsf(up.getY())); m_roll = atan2(up.getX(), up.getY()); updateGraphics(dt, Vec3(0,0,0), btQuaternion(0, 0, 0, 1)); } // update
/** The reset position must be set before calling reset */ void Moveable::reset() { if(m_body) { m_body->setLinearVelocity(btVector3(0.0, 0.0, 0.0)); m_body->setAngularVelocity(btVector3(0, 0, 0)); m_body->setCenterOfMassTransform(m_transform); } m_node->setVisible(true); // In case that the objects was eliminated Vec3 up = getTrans().getBasis().getColumn(1); m_pitch = atan2(up.getZ(), fabsf(up.getY())); m_roll = atan2(up.getX(), up.getY()); m_velocityLC = Vec3(0, 0, 0); Vec3 forw_vec = m_transform.getBasis().getColumn(0); m_heading = -atan2f(forw_vec.getZ(), forw_vec.getX()); } // reset
EnemyShip::EnemyShip(std::string _name, osg::Vec3f _pos, float _colRad, std::string _model, osg::ref_ptr<osg::MatrixTransform> _scene, int _hp, int _id) { initTransform(); setVel(0.0); setDir(osg::Vec3f(0.0f, 0.0f, 1.0f)); //Not used setOrientation(osg::Quat(0.0f, 0.0f, 1.0f, 0.0f)); setColRad(_colRad); setHP(_hp); translate(_pos); setName(_name); setDescr((std::string)("hej")); _scene->addChild(getTrans()); setID(_id); setModel(_model); attackCooldown = 10; homingMissileAttackCooldown = 40; }
SEXP Dtrmm(SEXP ENV, SEXP A, SEXP B, SEXP ALPHA, SEXP SIDE, SEXP TRANSA, SEXP UPLO, SEXP DIAG) { cl_env *env = get_env(ENV); MATRIXCHECK(A, REALSXP); MATRIXCHECK(B, REALSXP); SCALARCHECK(ALPHA, REALSXP); clblasSide side = getSide(SIDE); clblasTranspose transA = getTrans(TRANSA); clblasUplo uplo = getUplo(UPLO); clblasDiag diag = getDiag(DIAG); double alpha = SCALARREAL(ALPHA); int ar = nrows(A), ac = ncols(A), br = nrows(B), bc = ncols(B); int size_a = LENGTH(A) * sizeof(double); int size_b = LENGTH(B) * sizeof(double); double *ap = REAL(A), *bp = REAL(B); cl_int err = Dtrmm_internal( env, ap, bp, alpha, side, transA, uplo, diag, ar, ac, br, bc, size_a, size_b); CHECK(err); return B; }
/** Creates a bullet physics body for the flyable item. * \param forw_offset How far ahead of the kart the flyable should be * positioned. Necessary to avoid exploding a rocket inside of the * firing kart. * \param velocity Initial velocity of the flyable. * \param shape Collision shape of the flyable. * \param gravity Gravity to use for this flyable. * \param rotates True if the item should rotate, otherwise the angular factor * is set to 0 preventing rotations from happening. * \param turn_around True if the item is fired backwards. * \param custom_direction If defined the initial heading for this item, * otherwise the kart's heading will be used. */ void Flyable::createPhysics(float forw_offset, const Vec3 &velocity, btCollisionShape *shape, float restitution, const btVector3& gravity, const bool rotates, const bool turn_around, const btTransform* custom_direction) { // Get Kart heading direction btTransform trans = ( !custom_direction ? m_owner->getAlignedTransform() : *custom_direction ); // Apply offset btTransform offset_transform; offset_transform.setIdentity(); assert(!std::isnan(m_average_height)); assert(!std::isnan(forw_offset)); offset_transform.setOrigin(Vec3(0,m_average_height,forw_offset)); // turn around if(turn_around) { btTransform turn_around_trans; //turn_around_trans.setOrigin(trans.getOrigin()); turn_around_trans.setIdentity(); turn_around_trans.setRotation(btQuaternion(btVector3(0, 1, 0), M_PI)); trans *= turn_around_trans; } trans *= offset_transform; m_shape = shape; createBody(m_mass, trans, m_shape, restitution); m_user_pointer.set(this); Physics::getInstance()->addBody(getBody()); m_body->setGravity(gravity); // Rotate velocity to point in the right direction btVector3 v=trans.getBasis()*velocity; if(m_mass!=0.0f) // Don't set velocity for kinematic or static objects { #ifdef DEBUG // Just to get some additional information if the assert is triggered if(std::isnan(v.getX()) || std::isnan(v.getY()) || std::isnan(v.getZ())) { Log::debug("[Flyable]", "vel %f %f %f v %f %f %f", velocity.getX(),velocity.getY(),velocity.getZ(), v.getX(),v.getY(),v.getZ()); } #endif assert(!std::isnan(v.getX())); assert(!std::isnan(v.getY())); assert(!std::isnan(v.getZ())); m_body->setLinearVelocity(v); if(!rotates) m_body->setAngularFactor(0.0f); // prevent rotations } m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE); m_saved_transform = getTrans(); m_saved_lv = m_body->getLinearVelocity(); m_saved_av = m_body->getAngularVelocity(); m_saved_gravity = gravity; } // createPhysics
bool GStillEntity::checkIntersect ( const D3DXVECTOR4& vPos, /*世界坐标系中的点 */ const D3DXVECTOR4& vDir, /*世界坐标系中的向量 */ bool bInsectInfo /*是 裥枰鲎残畔?*/ ) { HRESULT hr = S_FALSE; //将Pos和Dir转换到物体本地坐标系中 D3DXMATRIX matWorld = getTrans()->getLocalD3D(); D3DXMatrixInverse ( &matWorld, NULL, &matWorld ); D3DXVec4Transform ( ( D3DXVECTOR4 * ) &vDir, ( D3DXVECTOR4 * ) &vDir, &matWorld ); D3DXVec3Normalize ( ( D3DXVECTOR3* ) &vDir, ( D3DXVECTOR3* ) &vDir ); D3DXVec4Transform ( ( D3DXVECTOR4 * ) &vPos, ( D3DXVECTOR4 * ) &vPos, &matWorld ); if ( mMeshForInsect == NULL ) { recreateInsectMesh(); } BOOL bHit = FALSE; hr = D3DXIntersect ( mMeshForInsect, ( D3DXVECTOR3* ) &vPos, ( D3DXVECTOR3* ) &vDir, &bHit, &m_InsectInfo.dwFaceIndex, &m_InsectInfo.u, &m_InsectInfo.v, &m_InsectInfo.fDist, NULL, NULL ); mNodeState.setBit ( eObjState_Picked, ( bool ) bHit ); dDebugMsgBox ( hr, "碰撞失败!" ); if ( FAILED ( hr ) ) { return false; } if ( bInsectInfo && mNodeState[eObjState_Picked] ) { D3DXVECTOR3 v[3]; DWORD dwIndex[3]; //先要获取索引缓冲区格式 LPDIRECT3DINDEXBUFFER9 pI = NULL; mMeshForInsect->GetIndexBuffer ( &pI ); D3DINDEXBUFFER_DESC indexDesc; dMemoryZero ( &indexDesc, sizeof ( D3DINDEXBUFFER_DESC ) ); if ( pI != NULL ) { pI->GetDesc ( &indexDesc ); } if ( indexDesc.Format== D3DFMT_INDEX16 ) { WORD *pIndexData16; hr = mMeshForInsect->LockIndexBuffer ( D3DLOCK_READONLY, ( void** ) &pIndexData16 ); dwIndex[0] = pIndexData16[m_InsectInfo.dwFaceIndex * 3 + 0]; dwIndex[1] = pIndexData16[m_InsectInfo.dwFaceIndex * 3 + 1]; dwIndex[2] = pIndexData16[m_InsectInfo.dwFaceIndex * 3 + 2]; } else { DWORD *pIndexData32; hr = mMeshForInsect->LockIndexBuffer ( D3DLOCK_READONLY, ( void** ) &pIndexData32 ); dwIndex[0] = pIndexData32[m_InsectInfo.dwFaceIndex * 3 + 0]; dwIndex[1] = pIndexData32[m_InsectInfo.dwFaceIndex * 3 + 1]; dwIndex[2] = pIndexData32[m_InsectInfo.dwFaceIndex * 3 + 2]; } mMeshForInsect->UnlockIndexBuffer(); D3DXVECTOR3 *pVertexData; hr = mMeshForInsect->LockVertexBuffer ( D3DLOCK_READONLY, ( void** ) &pVertexData ); v[0] = pVertexData[dwIndex[0]]; v[1] = pVertexData[dwIndex[1]]; v[2] = pVertexData[dwIndex[2]]; mMeshForInsect->UnlockVertexBuffer(); D3DXVECTOR4 vNormal ( ZEROFLOAT, ZEROFLOAT, ZEROFLOAT, ZEROFLOAT ); D3DXVECTOR4 vHitPos ( ZEROFLOAT, ZEROFLOAT, ZEROFLOAT, ZEROFLOAT ); D3DXVECTOR3 vTmp1, vTmp2; vTmp1 = v[1] - v[0]; vTmp2 = v[2] - v[0]; vHitPos = ( D3DXVECTOR4 ) v[0] + m_InsectInfo.u * ( D3DXVECTOR4 ) vTmp1 + m_InsectInfo.v * ( D3DXVECTOR4 ) vTmp2; vHitPos.w = 1; updateWorld (); D3DXVec4Transform ( &vHitPos, &vHitPos, &getTrans()->getLocalD3D() ); m_InsectInfo.vHitPos = D3DXVECTOR3 ( vHitPos.x, vHitPos.y, vHitPos.z ); D3DXVec3Cross ( ( D3DXVECTOR3* ) &vNormal, &vTmp1, &vTmp2 ); vNormal.w = 0; D3DXVec4Transform ( &vNormal, &vNormal, &matWorld ); D3DXVec3Normalize ( ( D3DXVECTOR3* ) &vNormal, ( D3DXVECTOR3* ) &vNormal ); m_InsectInfo.vNormal = D3DXVECTOR3 ( vNormal.x, vNormal.y, vNormal.z ); } return mNodeState[eObjState_Picked]; }
void blink(SDL_Surface *source) { SDL_SetAlpha(source, SDL_SRCALPHA, getTrans()); }
/** This function is called immediately before a rewind is done and saves * the current transform for the kart. The difference between this saved * transform and the new transform after rewind is the error that needs * (smoothly) be applied to the graphical position of the kart. */ void KartRewinder::saveTransform() { m_saved_transform = getTrans(); } // saveTransform