int HEG_system::enforcePbc(Array1 <doublevar> & pos, Array1 <int> & nshifted) { assert(pos.GetDim(0) >=3); int shifted=0; nshifted.Resize(3); nshifted=0; int nshift=0; for(int i=0; i< 3; i++) { int shouldcontinue=1; while(shouldcontinue) { //Whether we're past the origin side doublevar tooshort=0; for(int j=0; j<3;j++) tooshort+=normVec(i,j)*(pos(j)-origin(j)); //Whether we're past the lattice vector doublevar toofar=0; for(int j=0; j< 3; j++) toofar+=normVec(i,j)*(pos(j)-corners(i,j)); //the 1e-12 seems to help avoid numerical problems, esp //when integrating over a grid(which tends to hit the edges) if(tooshort < -1e-12) { //cout <<"tooshort " << tooshort << endl; for(int j=0; j< 3; j++) pos(j)+=latVec(i,j); shifted=1; nshifted(i)+=1; nshift++; } else if(toofar > 1e-12) { //cout << "toofar " << toofar << endl; for(int j=0; j< 3; j++) pos(j)-=latVec(i,j); shifted=1; // JK: here was +1, which works fine for real k-points that // correspond to standing waves. For general (complex) k-points the // wavefunction is "directional", however. nshifted(i)-=1; nshift++; } else { shouldcontinue=0; } if(nshift > 1000) error("Did over 1000 shifts and we're still out of the simulation cell." " There's probably something wrong. Position : ", pos(i)); } } return shifted; }
std::vector< float > Saliency::uniqueness( const std::vector< SuperpixelStatistic >& stat ) const { const int N = stat.size(); std::vector< float > r( N ); const float sp = 0.5 / (settings_.sigma_p_ * settings_.sigma_p_); for( int i=0; i<N; i++ ) { float u = 0, norm = 1e-10; Vec3f c = stat[i].mean_color_; Vec2f p = stat[i].mean_position_; // Evaluate the score, for now without filtering for( int j=0; j<N; j++ ) { Vec3f dc = stat[j].mean_color_ - c; Vec2f dp = stat[j].mean_position_ - p; float w = fast_exp( - sp * dp.dot(dp) ); u += w*dc.dot(dc); norm += w; } // Let's not normalize here, must have been a typo in the paper // r[i] = u / norm; r[i] = u; } normVec( r ); return r; }
std::vector< float > Saliency::distributionFilter( const std::vector< SuperpixelStatistic >& stat ) const { const int N = stat.size(); // Setup the data and features std::vector< Vec3f > features( stat.size() ); Mat_<float> data( stat.size(), 4 ); for( int i=0; i<N; i++ ) { //features[i] = stat[i].mean_color_ / settings_.sigma_c_; { Vec3f x = stat[i].mean_color_; x[0] = x[0]/settings_.sigma_c_; x[1] = x[1]/settings_.sigma_c_; x[2] = x[2]/settings_.sigma_c_; features[i] = x; } Vec2f p = stat[i].mean_position_; data(i,0) = 1; data(i,1) = p[0]; data(i,2) = p[1]; data(i,3) = p.dot(p); } // Filter Filter filter( (const float*)features.data(), N, 3 ); filter.filter( data.ptr<float>(), data.ptr<float>(), 4 ); // Compute the uniqueness std::vector< float > r( N ); for( int i=0; i<N; i++ ) r[i] = data(i,3) / data(i,0) - ( data(i,1) * data(i,1) + data(i,2) * data(i,2) ) / ( data(i,0) * data(i,0) ); normVec( r ); return r; }
std::vector< float > Saliency::distribution( const std::vector< SuperpixelStatistic >& stat ) const { const int N = stat.size(); std::vector< float > r( N ); const float sc = 0.5 / (settings_.sigma_c_*settings_.sigma_c_); for( int i=0; i<N; i++ ) { float u = 0, norm = 1e-10; Vec3f c = stat[i].mean_color_; Vec2f p(0.f, 0.f); // Find the mean position for( int j=0; j<N; j++ ) { Vec3f dc = stat[j].mean_color_ - c; float w = fast_exp( - sc * dc.dot(dc) ); p += w*stat[j].mean_position_; norm += w; } p *= 1.0 / norm; // Compute the variance for( int j=0; j<N; j++ ) { Vec3f dc = stat[j].mean_color_ - c; Vec2f dp = stat[j].mean_position_ - p; float w = fast_exp( - sc * dc.dot(dc) ); u += w*dp.dot(dp); } r[i] = u / norm; } normVec( r ); return r; }
float Enemy::getPlayerAngle(sf::Vector2f &playerCoord, sf::RenderWindow *window, Bullet *bullet) { float alphaX = playerCoord.x - getCoordinateX();//вектор , колинеарный прямой, которая пересекает спрайт и курсор float alphaY = playerCoord.y - getCoordinateY();//он же, координата y float angle = (atan2(alphaY, alphaX)) * 180 / PI;//получаем угол в радианах и переводим его в градусы float vecLength = sqrt(pow(alphaX, 2) + pow(alphaY, 2)); sf::Vector2f normVec(alphaX / vecLength, alphaY / vecLength); //sf::Vector2f winSize(window->getSize().x, window->getSize().y); //if (vecLength < visibleRadius) isPlayerVisible = true; //видимость в радиусе = visibleRadius; return angle; }
Leaf::Leaf(double m, double a, double dens, double air, const btVector3& pos, const btVector3& flu, const btVector3& angleVel) { mass = m; area = a; density = dens; airCoeff = air; position = pos; flutter = flu; angVel = angleVel; rotation = btVector3(rand() % 720 - 360, rand() % 720 - 360, rand() % 720 - 360); rotation = normVec(rotation); fallShape = new btSphereShape(1); fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(rotation, 1), btVector3(position))); fallInertia = btVector3(0, 0, 0); fallShape->calculateLocalInertia(mass, fallInertia); btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia); leafBody = new btRigidBody(fallRigidBodyCI); leafBody->setLinearVelocity(btVector3(0, 0, 0)); }
std::vector< float > Saliency::uniquenessFilter( const std::vector< SuperpixelStatistic >& stat ) const { const int N = stat.size(); // Setup the data and features std::vector< Vec2f > features( stat.size() ); Mat_<float> data( stat.size(), 5 ); for( int i=0; i<N; i++ ) { //features[i] = stat[i].mean_position_ / (double)settings_.sigma_p_; { //due to compile error, rewrite above line explicitly Vec2f x = stat[i].mean_position_; x[0] = x[0]/settings_.sigma_p_; x[1] = x[1]/settings_.sigma_p_; features[i] = x; } Vec3f c = stat[i].mean_color_; data(i,0) = 1; data(i,1) = c[0]; data(i,2) = c[1]; data(i,3) = c[2]; data(i,4) = c.dot(c); } // Filter Filter filter( (const float*)features.data(), N, 2 ); filter.filter( data.ptr<float>(), data.ptr<float>(), 5 ); // Compute the uniqueness std::vector< float > r( N ); for( int i=0; i<N; i++ ) { Vec3f c = stat[i].mean_color_; float u = 0, norm = 1e-10; Vec2f p = stat[i].mean_position_; r[i] = data(i,0)*c.dot(c) + data(i,4) - 2*( c[0]*data(i,1) + c[1]*data(i,2) + c[2]*data(i,3) ); } normVec( r ); return r; }
/*! */ int HEG_system::read(vector <string> & words, unsigned int & pos) { const int ndim=3; int startpos=pos; vector <string> latvectxt; vector <string> spintxt; if(!readsection(words, pos, spintxt, "NSPIN")) { error("Need NSPIN in HEG system"); } nspin.Resize(2); nspin(0)=atoi(spintxt[0].c_str()); nspin(1)=atoi(spintxt[1].c_str()); totnelectrons=nspin(0)+nspin(1); vector <string> ktxt; if(readsection(words, pos=0, ktxt, "KPOINT")) { if(ktxt.size()!=3) error("KPOINT must be a section of size 3"); kpt.Resize(3); for(int i=0; i< 3; i++) kpt(i)=atof(ktxt[i].c_str()); } else { kpt.Resize(3); kpt=0; } pos=startpos; if(!readsection(words, pos, latvectxt, "BOXSIZE")) error("BOXSIZE is required in HEG"); if(latvectxt.size() != ndim) error("BOXSIZE must have exactly ",ndim, " values"); vector< vector<string> > perturb_sec; vector <string> dumstring; pos=startpos; while(readsection(words, pos,dumstring, "PERTURB")) perturb_sec.push_back(dumstring); nperturb=perturb_sec.size(); perturb_pos.Resize(nperturb, ndim); perturb_strength.Resize(nperturb); perturb_alpha.Resize(nperturb); perturb_spin.Resize(nperturb); for(int i=0; i< nperturb; i++) { if(!readsection(perturb_sec[i], pos=0,dumstring,"POS")) error("Need POS in PERTURB"); if(dumstring.size()!=ndim) error("wrong dimension in POS in PERTURB"); for(int d=0; d< ndim; d++) perturb_pos(i,d)=atof(dumstring[d].c_str()); if(!readvalue(perturb_sec[i], pos=0,perturb_strength(i),"STRENGTH")) error("Need STRENGTH in PERTURB"); if(!readvalue(perturb_sec[i], pos=0,perturb_alpha(i),"SCALE")) error("Need SCALE in PERTURB"); if(!readvalue(perturb_sec[i], pos=0,perturb_spin(i),"SPIN")) error("Need SPIN in PERTURB"); } latVec.Resize(ndim, ndim); latVec=0.0; for(int i=0; i< ndim; i++) latVec(i,i)=atof(latvectxt[i].c_str()); /* for(int i=0; i< ndim; i++) { for(int j=0; j< ndim; j++) { latVec(i,j)=atof(latvectxt[i*ndim+j].c_str()); } } */ origin.Resize(3); vector <string> origintxt; if(readsection(words, pos=0, origintxt, "ORIGIN")) { if(origintxt.size() < 3) error("ORIGIN section must have at least 3 elements."); for(int i=0; i< 3; i++) origin(i)=atof(origintxt[i].c_str()); } else { origin=0; //defaulting the origin to zero } vector <string> interactiontxt; // default is truncated Coulomb calcLocChoice=&HEG_system::calcLocTrunc; eeModel=2; same_spin_int=true; diff_spin_int=true; if(readsection(words, pos=0, interactiontxt, "INTERACTION")) { if( haskeyword(interactiontxt, pos=0, "EWALD") ) { calcLocChoice=&HEG_system::calcLocEwald; eeModel=1; } if( haskeyword(interactiontxt, pos=0, "TRUNCCOUL") ) { calcLocChoice=&HEG_system::calcLocTrunc; eeModel=2; } if( haskeyword(interactiontxt, pos=0, "GAUSS") ) { calcLocChoice=&HEG_system::calcLocGauss; eeModel=3; if(!readvalue(interactiontxt,pos=0,Gauss_a, "AMP")) error("Gauss interaction model requested, but no AMP given."); if(!readvalue(interactiontxt,pos=0,Gauss_s, "STDEV")) error("Gauss interaction model requested, but no STDEV given."); Gauss_s2=Gauss_s*Gauss_s; } if( haskeyword(interactiontxt, pos=0, "NO_SAME_SPIN") ) same_spin_int=false; if( haskeyword(interactiontxt, pos=0, "NO_DIFF_SPIN") ) diff_spin_int=false; } if ( eeModel==1 && ( !same_spin_int || !diff_spin_int ) ) error("Spin-dependent Ewald interaction not supported."); //-------------cross products //cross product: 0->1x2, 1->2x0, 2->0x1 Array2 <doublevar> crossProduct(ndim, ndim); crossProduct(0,0)=(latVec(1,1)*latVec(2,2)-latVec(1,2)*latVec(2,1)); crossProduct(0,1)=(latVec(1,2)*latVec(2,0)-latVec(1,0)*latVec(2,2)); crossProduct(0,2)=(latVec(1,0)*latVec(2,1)-latVec(1,1)*latVec(2,0)); crossProduct(1,0)=(latVec(2,1)*latVec(0,2)-latVec(2,2)*latVec(0,1)); crossProduct(1,1)=(latVec(2,2)*latVec(0,0)-latVec(2,0)*latVec(0,2)); crossProduct(1,2)=(latVec(2,0)*latVec(0,1)-latVec(2,1)*latVec(0,0)); crossProduct(2,0)=(latVec(0,1)*latVec(1,2)-latVec(0,2)*latVec(1,1)); crossProduct(2,1)=(latVec(0,2)*latVec(1,0)-latVec(0,0)*latVec(1,2)); crossProduct(2,2)=(latVec(0,0)*latVec(1,1)-latVec(0,1)*latVec(1,0)); //------------ reciprocal cell (used in setupEwald and showinfo) recipLatVec.Resize(ndim, ndim); doublevar det=Determinant(latVec, ndim); debug_write(cout, "cell volume ", det,"\n"); cellVolume=det; for(int i=0; i< ndim; i++) { for(int j=0; j< ndim; j++) { recipLatVec(i,j)=crossProduct(i,j)/det; } } //------------ normal vectors (needed in enforcePbc) normVec.Resize(ndim, ndim); for(int i=0; i< ndim; i++) { for(int j=0; j < ndim; j++) { normVec(i,j)=crossProduct(i,j); } //Check to make sure the direction is facing out doublevar dotprod=0; for(int j=0; j < ndim; j++) { dotprod+=normVec(i,j)*latVec(i,j); } if(dotprod < 0) { for(int j=0; j< ndim; j++) { normVec(i,j)= -normVec(i,j); } } } corners.Resize(ndim, ndim); for(int i=0; i< ndim; i++) { for(int j=0; j< ndim; j++) { corners(i,j)=origin(j)+latVec(i,j); } } //------------ setup for interaction calculators (reciprocal part of Ewald sum etc.) if ( eeModel == 1 ) setupEwald(crossProduct); if ( eeModel == 2 ) setupTruncCoulomb(); return 1; }