int main( int argc, char * argv[] ) { QApplication app( argc, argv ); QWidget w; QGridLayout glay( &w ); KDHorizontalLine hl1( "Foo", &w ); glay.addWidget( &hl1, 0, 0, 1, 2 ); QLabel lb1( "Foo 1:", &w ); glay.addWidget( &lb1, 1, 0 ); QLineEdit le1( &w ); glay.addWidget( &le1, 1, 1 ); glay.setColumnStretch( 1, 1 ); glay.setRowStretch( 2, 1 ); w.show(); return app.exec(); }
ExecStatus SuperOfInter<View0,View1,View2>::propagate(Space& home, const ModEventDelta& med) { bool allassigned = x0.assigned() && x1.assigned() && x2.assigned(); ModEvent me0 = View0::me(med); ModEvent me1 = View1::me(med); ModEvent me2 = View2::me(med); bool modified = false; do { // glb(x2) >= glb(x0) ^ glb(x1) if ( modified || Rel::testSetEventLB(me0,me1)) { GlbRanges<View0> lb0(x0); GlbRanges<View1> lb1(x1); Iter::Ranges::Inter<GlbRanges<View0>,GlbRanges<View1> > is(lb0, lb1); GECODE_ME_CHECK_MODIFIED(modified,x2.includeI(home,is)); } // lub(x0) -= glb(x1)-lub(x2) // lub(x1) -= glb(x0)-lub(x2) if ( modified || Rel::testSetEventAnyB(me0,me1,me2)) { modified = false; GlbRanges<View1> lb12(x1); LubRanges<View2> ub22(x2); Iter::Ranges::Diff<GlbRanges<View1>, LubRanges<View2> > diff1(lb12, ub22); GECODE_ME_CHECK_MODIFIED(modified, x0.excludeI(home,diff1)); GlbRanges<View0> lb01(x0); LubRanges<View2> ub23(x2); Iter::Ranges::Diff<GlbRanges<View0>, LubRanges<View2> > diff2(lb01, ub23); GECODE_ME_CHECK_MODIFIED(modified, x1.excludeI(home,diff2)); } else { modified = false; } // Cardinality propagation if ( modified || Rel::testSetEventCard(me0,me1,me2) || Rel::testSetEventUB(me0,me1) ) { LubRanges<View0> ub0(x0); LubRanges<View1> ub1(x1); Iter::Ranges::Union<LubRanges<View0>, LubRanges<View1> > u(ub0,ub1); unsigned int m = Iter::Ranges::size(u); if (m < x0.cardMin() + x1.cardMin()) { GECODE_ME_CHECK_MODIFIED(modified, x2.cardMin( home, x0.cardMin()+x1.cardMin() - m ) ); } if (m + x2.cardMax() > x1.cardMin()) { GECODE_ME_CHECK_MODIFIED(modified, x0.cardMax( home, m+x2.cardMax()-x1.cardMin() ) ); } if (m + x2.cardMax() > x0.cardMin()) { GECODE_ME_CHECK_MODIFIED(modified, x1.cardMax( home, m+x2.cardMax()-x0.cardMin() ) ); } } } while (modified); if (shared(x0,x1,x2)) { if (allassigned) { return home.ES_SUBSUMED(*this); } else { return ES_NOFIX; } } else { if (x0.assigned() + x1.assigned() + x2.assigned() >= 2) { return home.ES_SUBSUMED(*this); } else { return ES_FIX; } } }
bool hitboxCollision(int a_x,int a_y,int a_width,int a_height,float a_angle, int b_x,int b_y,int b_width,int b_height,float b_angle) { Point pa1(a_x, a_y); Point pa2(a_x + cos (a_angle*PI/180) * a_width ,a_y - sin (a_angle*PI/180) * a_width); Point pa3(a_x + cos (a_angle*PI/180) * a_width + sin (a_angle*PI/180) * a_height, a_y - sin (a_angle*PI/180) * a_width + cos (a_angle*PI/180) * a_height); Point pa4(a_x + sin (a_angle*PI/180) * a_height, a_y + cos (a_angle*PI/180) * a_height); Point pb1(b_x, b_y); Point pb2(b_x + cos (b_angle*PI/180) * b_width ,b_y - sin (b_angle*PI/180) * b_width); Point pb3(b_x + cos (b_angle*PI/180) * b_width + sin (b_angle*PI/180) * b_height, b_y - sin (b_angle*PI/180) * b_width + cos (b_angle*PI/180) * b_height); Point pb4(b_x + sin (b_angle*PI/180) * b_height, b_y + cos (b_angle*PI/180) * b_height); Line la1(pa1,pa2); Line la2(pa2,pa3); Line la3(pa3,pa4); Line la4(pa4,pa1); Line lb1(pb1,pb2); Line lb2(pb2,pb3); Line lb3(pb3,pb4); Line lb4(pb4,pb1); if(segmentIntersection(la1,lb1)) return true; if(segmentIntersection(la1,lb2)) return true; if(segmentIntersection(la1,lb3)) return true; if(segmentIntersection(la1,lb4)) return true; if(segmentIntersection(la2,lb1)) return true; if(segmentIntersection(la2,lb2)) return true; if(segmentIntersection(la2,lb3)) return true; if(segmentIntersection(la2,lb4)) return true; if(segmentIntersection(la3,lb1)) return true; if(segmentIntersection(la3,lb2)) return true; if(segmentIntersection(la3,lb3)) return true; if(segmentIntersection(la3,lb4)) return true; if(segmentIntersection(la4,lb1)) return true; if(segmentIntersection(la4,lb2)) return true; if(segmentIntersection(la4,lb3)) return true; if(segmentIntersection(la4,lb4)) return true; return false; /* vector<Point*>intersections; intersections.push_back(lineIntersection(la1,lb1)); intersections.push_back(lineIntersection(la1,lb2)); intersections.push_back(lineIntersection(la1,lb3)); intersections.push_back(lineIntersection(la1,lb4)); intersections.push_back(lineIntersection(la2,lb1)); intersections.push_back(lineIntersection(la2,lb2)); intersections.push_back(lineIntersection(la2,lb3)); intersections.push_back(lineIntersection(la2,lb4)); intersections.push_back(lineIntersection(la3,lb1)); intersections.push_back(lineIntersection(la3,lb2)); intersections.push_back(lineIntersection(la3,lb3)); intersections.push_back(lineIntersection(la3,lb4)); intersections.push_back(lineIntersection(la4,lb1)); intersections.push_back(lineIntersection(la4,lb2)); intersections.push_back(lineIntersection(la4,lb3)); intersections.push_back(lineIntersection(la4,lb4)); int x_min=0;int x_max=0; int y_max=0;int y_min=0; if(a_width*a_height>b_width*b_height) { x_min = pa1.x; x_min=min(x_min,pa2.x); x_min=min(x_min,pa3.x); x_min=min(x_min,pa4.x); x_max = pa1.x; x_max=max(x_max,pa2.x); x_max=max(x_max,pa3.x); x_max=max(x_max,pa4.x); y_min = pa1.y; y_min=min(y_min,pa2.y); y_min=min(y_min,pa3.y); y_min=min(y_min,pa4.y); y_max = pa1.y; y_max=max(y_max,pa2.y); y_max=max(y_max,pa3.y); y_max=max(y_max,pa4.y); }else { x_min = pb1.x; x_min=min(x_min,pb2.x); x_min=min(x_min,pb3.x); x_min=min(x_min,pb4.x); x_max = pb1.x; x_max=max(x_max,pb2.x); x_max=max(x_max,pb3.x); x_max=max(x_max,pb4.x); y_min = pb1.y; y_min=min(y_min,pb2.y); y_min=min(y_min,pb3.y); y_min=min(y_min,pb4.y); y_max = pb1.y; y_max=max(y_max,pb2.y); y_max=max(y_max,pb3.y); y_max=max(y_max,pb4.y); } int cont=0; for(int i=0;i<(int)intersections.size();i++) { Point* point=intersections[i]; if(point!=NULL) { if(point->x > x_min && point->x < x_max && point->y > y_min && point->y < y_max) { cont++; } } } vector<Point*>::iterator i; for ( i = intersections.begin() ; i < intersections.end(); i++ ) { delete * i; } if(cont>=8) return true; */ return false; }
//Construct the kdTree kdTree::kdTree(vector<vector<double>> points, const string& method, int cutdimension): splitMethod(method){ if (points.empty()) { kdTreeRoot = nullptr; return; } // Select intial splitting axis int dimension = (int)(points[0].size()); int axis = cutdimension % dimension; size_t lb1(0),lb2(0),ub1(0),ub2(0); size_t pivotLocation; size_t numpoints = points.size(); if(numpoints == 1) { kdTreeRoot = shared_ptr<kdTreeNode>(new kdTreeNode(points[0])); } else { sort(points.begin(), points.end(), sorter(axis)); if (method == "median") { pivotLocation = (size_t)(numpoints/2); } else if (method == "mean") { double mean(0.0); for (size_t i=0;i<numpoints;i++) { mean = mean + points[i][axis]; } mean =mean/(double)numpoints; for (size_t i=0;i<numpoints;i++) { if (points[i][axis]>= mean) { pivotLocation = i; break; } } } /* lb1 = 0; */ ub1 = pivotLocation; lb2 = pivotLocation +1; ub2 = (size_t)points.size(); // Recursively create Left SubTree unique_ptr<kdTree> left_child = move(unique_ptr<kdTree> (new kdTree( slice<vector<double>>(points,lb1,ub1), method,axis+1))); // Recursively create Right SubTree unique_ptr<kdTree> right_child = move(unique_ptr<kdTree> (new kdTree( slice<vector<double>>(points,lb2,ub2), method,axis+1))); kdTreeRoot = shared_ptr<kdTreeNode>( new kdTreeNode( points[pivotLocation], axis, right_child->kdTreeRoot, left_child->kdTreeRoot)); } }
//Compute continuity with brep2. int MGSBRep::continuity( // Reuturn value is the continuity. const MGSBRep& brep2, // Input second SBRep int is_u1, // Input if u-direction of this. int is_u2, // Input if u-direction of brep2. int opposite, // Input if parameter direction of which2 is equal or not. int& which1, // Outputs which perimeter(which1) of this is int& which2, // connected to which(which2) of brep2. // These are valid only when continuity>=0. double& ratio // Ratio of 1st derivatives of the two surfaces will // be returned. // ratio= d2/d1, where d1=1st deriv of this and d2=of brep2 ) const // Function's return value is: // -1: G(-1) continuity, i.e. two surfaces are discontinuous. // 0: G0 continuity, i.e. two surfaces are connected, // but tangents are discontinuous // 1: G1 continuity, i.e. two surfaces are connected, // and tangents are also continuous. // 2: G2 continuity, i.e. two surfaces are connected, // and tangents and curvatures are also continuous. { size_t i,j,k,i2; int incrmnt; double ratio2; int which; int cont, contold; size_t dim1=sdim(), dim2=brep2.sdim(); size_t ns1,nt1, ns2,nt2; MGLBRep p1a, p1b, p2a, p2b; // Test if perimeter of this is continuous to perimeter brep2. const MGKnotVector *s1,*s2,*t1,*t2; if(is_u1){ which1=0; s1=&knot_vector_u(); t1=&knot_vector_v(); p1a=perimeter(0); p1b=perimeter(2); } else{ which1=1; s1=&knot_vector_v(); t1=&knot_vector_u(); p1a=perimeter(1); p1b=perimeter(3); } if(is_u2){ which2=0; s2=&(brep2.knot_vector_u()); t2=&(brep2.knot_vector_v()); p2a=brep2.perimeter(0); p2b=brep2.perimeter(2); } else{ which2=1; s2=&(brep2.knot_vector_v()); t2=&(brep2.knot_vector_u()); p2a=brep2.perimeter(1); p2b=brep2.perimeter(3); } ns1=(*s1).bdim(); ns2=(*s2).bdim(); nt1=(*t1).bdim(); nt2=(*t2).bdim(); cont=0; // 1. Test if positional data of two perimeters are equal. if(opposite){ p2a.negate(); p2b.negate();} if (p1a.line_bcoef()==p2a.line_bcoef()){ cont=1;} else if(p1a.line_bcoef()==p2b.line_bcoef()){ which2+=2; cont=1;} else if(p1b.line_bcoef()==p2a.line_bcoef()){ which1+=2; cont=1;} else if(p1b.line_bcoef()==p2b.line_bcoef()){ which1+=2; which2+=2; cont=1;} if(cont==0) return -1; // There exists a possibility of continuity 1. // 2. Test if derivatives along v direction are equal. i2=0; incrmnt=1; if(opposite) {i2=ns2-1; incrmnt=-1;} MGBPointSeq b1(nt1,dim1), b2(nt2,dim2); contold=0; for(i=0; i<ns1; i++){ if(is_u1){ for(j=0; j<nt1; j++) for(k=0; k<dim1; k++) b1(j,k)=coef(i,j,k); } else{ for(j=0; j<nt1; j++) for(k=0; k<dim1; k++) b1(j,k)=coef(j,i,k); } if(is_u2){ for(j=0; j<nt2; j++) for(k=0; k<dim2; k++) b2(j,k)=brep2.coef(i2,j,k); } else{ for(j=0; j<nt2; j++) for(k=0; k<dim2; k++) b2(j,k)=brep2.coef(j,i2,k); } i2=i2+incrmnt; MGLBRep lb1(*t1,b1), lb2(*t2, b2); cont=lb1.continuity(lb2,which,ratio2); if(cont<=0) return 0; //Continuity is C0. if(contold==0) {contold=cont; ratio=ratio2;} else{ if(!MGREqual2(ratio2,ratio)) return 0; //Continuity is C0. else if(contold>cont) contold=cont; } } return contold; }
void CLBFGSCPP::lbfgs ( int n , int m , double x[] , double f , double g[] , bool diagco , double diag[] , int iprint[] , double eps , double xtol , int iflag[] ) //throw (ExceptionWithIflag* e) { bool execute_entire_while_loop = false; if ( w == NULL ) { w = (double*)malloc( sizeof(double) * (n*(2*m+1)+2*m) ); } if ( solution_cache == NULL ) { solution_cache = (double*)malloc(sizeof(double) * n); } if ( iflag[0] == 0 ) // Initialize. { //// changed by p-jzhu, for an instance of LBFGS is used multi-times with different n and m //if ( w != NULL ) { // free(w); // w = NULL; //} //w = (double*)malloc( sizeof(double) * (n*(2*m+1)+2*m) ); //if(solution_cache != NULL){ // free( solution_cache ); // solution_cache = NULL; //} //solution_cache = (double*)malloc(sizeof(double) * n); // replaced by p-jzhu for (int i=0; i<n; i++) solution_cache[i] = x[i]; //System.arraycopy( x, 0, solution_cache, 0, n ); iter = 0; if ( n <= 0 || m <= 0 ) { iflag[0]= -3; //return false; throw new ExceptionWithIflag( iflag[0], L"Improper input parameters (n or m are not positive.)" ); } if ( gtol <= 0.0001 ) { wcerr<< L"LBFGS.lbfgs: gtol is less than or equal to 0.0001. It has been reset to 0.9." << endl; gtol= 0.9; } nfun= 1; point= 0; finish= false; if ( diagco ) { for ( int i = 1 ; i <= n ; i += 1 ) { if ( diag [ i -1] <= 0 ) { iflag[0]=-2; wostringstream oss; oss << L"The " << i << L"-th diagonal element of the inverse hessian approximation is not positive."; throw new ExceptionWithIflag( iflag[0], oss.str()); } } } else { for ( int i = 1 ; i <= n ; i += 1 ) { diag [ i -1] = 1; } } ispt= n+2*m; iypt= ispt+n*m; for ( int i = 1 ; i <= n ; i += 1 ) { w [ ispt + i -1] = - g [ i -1] * diag [ i -1]; } gnorm = sqrt( ddot ( n , g , 0, 1 , g , 0, 1 ) ); stp1= 1/gnorm; ftol= 0.000001; maxfev= 20; if ( iprint [ 1 -1] >= 0 ) lb1 ( iprint , iter , nfun , gnorm , n , m , x , f , g , stp , finish ); execute_entire_while_loop = true; } while ( true ) { if ( execute_entire_while_loop ) { iter= iter+1; info[0]=0; bound=iter-1; if ( iter != 1 ) { if ( iter > m ) bound = m; ys = ddot ( n , w , iypt + npt , 1 , w , ispt + npt , 1 ); if ( ! diagco ) { yy = ddot ( n , w , iypt + npt , 1 , w , iypt + npt , 1 ); for ( int i = 1 ; i <= n ; i += 1 ) { diag [ i -1] = ys / yy; } } else { iflag[0]=2; return; } } } if ( execute_entire_while_loop || iflag[0] == 2 ) { if ( iter != 1 ) { if ( diagco ) { for ( int i = 1 ; i <= n ; i += 1 ) { if ( diag [ i -1] <= 0 ) { iflag[0]=-2; wostringstream oss; oss << L"The " << i << L"-th diagonal element of the inverse hessian approximation is not positive."; throw new ExceptionWithIflag( iflag[0], oss.str() ); } } } cp= point; if ( point == 0 ) cp = m; w [ n + cp -1] = 1 / ys; for ( i = 1 ; i <= n ; i += 1 ) { w [ i -1] = - g [ i -1]; } cp= point; for (int i = 1 ; i <= bound ; i += 1 ) { cp=cp-1; if ( cp == - 1 ) cp = m - 1; sq = ddot ( n , w , ispt + cp * n , 1 , w , 0 , 1 ); inmc=n+m+cp+1; iycn=iypt+cp*n; w [ inmc -1] = w [ n + cp + 1 -1] * sq; daxpy ( n , - w [ inmc -1] , w , iycn , 1 , w , 0 , 1 ); } for (int i = 1 ; i <= n ; i += 1 ) { w [ i -1] = diag [ i -1] * w [ i -1]; } for (int i = 1 ; i <= bound ; i += 1 ) { yr = ddot ( n , w , iypt + cp * n , 1 , w , 0 , 1 ); beta = w [ n + cp + 1 -1] * yr; inmc=n+m+cp+1; beta = w [ inmc -1] - beta; iscn=ispt+cp*n; daxpy ( n , beta , w , iscn , 1 , w , 0 , 1 ); cp=cp+1; if ( cp == m ) cp = 0; } for (int i = 1 ; i <= n ; i += 1 ) { w [ ispt + point * n + i -1] = w [ i -1]; } } nfev[0]=0; stp[0]=1; if ( iter == 1 ) stp[0] = stp1; for (int i = 1 ; i <= n ; i += 1 ) { w [ i -1] = g [ i -1]; } } m_mcsrch.mcsrch ( n , x , f , g , w , ispt + point * n , stp , ftol , xtol , maxfev , info , nfev , diag ); if ( info[0] == - 1 ) { iflag[0]=1; return; } if ( info[0] != 1 ) { iflag[0]=-1; wostringstream oss; oss << L"Line search failed. See documentation of routine mcsrch. Error return of line search: info = " << info[0] << L" Possible causes: function or gradient are incorrect, or incorrect tolerances."; throw new ExceptionWithIflag( iflag[0], oss.str() ); } nfun= nfun + nfev[0]; npt=point*n; for (int i = 1 ; i <= n ; i += 1 ) { w [ ispt + npt + i -1] = stp[0] * w [ ispt + npt + i -1]; w [ iypt + npt + i -1] = g [ i -1] - w [ i -1]; } point=point+1; if ( point == m ) point = 0; gnorm = sqrt ( ddot ( n , g , 0 , 1 , g , 0 , 1 ) ); xnorm = sqrt ( ddot ( n , x , 0 , 1 , x , 0 , 1 ) ); xnorm = max ( 1.0 , xnorm ); if ( gnorm / xnorm <= eps ) finish = true; if ( iprint [ 1 -1] >= 0 ) lb1 ( iprint , iter , nfun , gnorm , n , m , x , f , g , stp , finish ); // Cache the current solution vector. Due to the spaghetti-like // nature of this code, it's not possible to quit here and return; // we need to go back to the top of the loop, and eventually call // mcsrch one more time -- but that will modify the solution vector. // So we need to keep a copy of the solution vector as it was at // the completion (info[0]==1) of the most recent line search. // replaced by p-jzhu for (int i=0; i<n; i++) solution_cache[i] = x[i]; //System.arraycopy( x, 0, solution_cache, 0, n ); if ( finish ) { iflag[0]=0; return; } execute_entire_while_loop = true; // from now on, execute whole loop } //return true; }