Esempio n. 1
0
double dynamics_mul::get_curvature(dsc_obj *obj, HMesh::Walker hew0){
    // Find next edge on the boundary
    auto hew1 = hew0.next().opp();
    while (1) {
        if (s_dsc->is_interface(hew1.halfedge())) {
            hew1 = hew1.opp();
            break;
        }
        
        hew1 = hew1.next().opp();
    }
    
    Vec2 p0 = obj->get_pos(hew0.vertex()) - obj->get_pos(hew0.opp().vertex());
    Vec2 p1 = obj->get_pos(hew1.vertex()) - obj->get_pos(hew1.opp().vertex());
    
    Vec2 norm0(p0[1], -p0[0]); norm0.normalize();
    Vec2 norm1(p1[1], -p1[0]); norm1.normalize();
    Vec2 norm = norm0 + norm1; norm.normalize();
    
    
    double l0 = p0.length();
    double l1 = p1.length();
    double angle = std::atan2(CGLA::cross(p0, p1), DSC2D::Util::dot(p0, p1));
    double curvature = angle / (l0/2.0 + l1/2.0);
    
    return curvature;
}
Esempio n. 2
0
double dynamics_mul::u_gradient(dsc_obj *obj, std::map<int, double>  intensity_map)
{
    double dEu = 0.0;
    HMesh::HalfEdgeAttributeVector<int> touch(obj->get_no_halfedges(), 0);
    for (auto eid = obj->halfedges_begin(); eid != obj->halfedges_end(); eid++) {
        auto hew = obj->walker(*eid);
        if (!touch[*eid] and obj->is_interface(*eid)) {
            double c0 = mean_inten_[s_dsc->get_label(hew.face())];
            double c1 = mean_inten_[s_dsc->get_label(hew.opp().face())];
            
            // Loop on the edge
            auto p0 = s_dsc->get_pos(hew.opp().vertex());
            auto p1 = s_dsc->get_pos(hew.vertex());
            
            int length = (int)(p1 - p0).length();
            for (int i = 0; i <= length; i++) {
                auto p = p0 + (p1 - p0)*(double(i)/(double)length);
                double I = s_img->get_intensity(p[0], p[1]);
                
                dEu += std::pow(g_param.beta*(2*I - c0 - c1)*(c0 - c1), 2);
            }
        }
        
        touch[*eid] = 1;
        touch[hew.opp().halfedge()] = 1;
    }
    
    return dEu;
}
Esempio n. 3
0
ElliPoint *star(ElliPoint *p,ElliPoint *q,int a,int base) {
	int l,k;
	if (isInfty(p) == 0) return opp(q,base);
	if (isInfty(q) == 0) return opp(p,base);
	if (Elli_equal(p,q,base) == 0) {
		l = divide(3 * (p->x) * (p->x) + a,2 * (p->y),base);
		k = l * l - 2 * (p->x);
		return ElliPt(k,l * (k - (p->x)) + (p->y), base);
	}
	if (mod(p->x,base) == mod(q->x,base)) {
		return ElliInfty();
	}
	l = divide((q->y) - (p->y),(q->x) - (p->x),base);
	k = l * l - (p->x) - (q->x);
	return ElliPt(k,l * (k - (p->x)) + (p->y), base);
}
Esempio n. 4
0
double dynamics_mul::gradient_length(dsc_obj *obj){
    double dEl = 0.0;
    
    for (auto eid = obj->halfedges_begin(); eid != obj->halfedges_end(); eid++) {
        auto hew = obj->walker(*eid);
        if (obj->is_interface(*eid)) {
            
            // Loop on the edge
            auto p0 = s_dsc->get_pos(hew.opp().vertex());
            auto p1 = s_dsc->get_pos(hew.vertex());
            
            int length = (int)(p1 - p0).length();
            for (int i = 0; i <= length; i++) {
                auto p = p0 + (p1 - p0)*(double(i)/(double)length);
                
                double K1, K2;
                get_curvature(obj, hew, K1, K2);
                
                double K = K1*(p0-p).length()/(p0-p1).length()
                            + K2*(p1-p).length()/(p0-p1).length();
                
                dEl += std::pow(g_param.alpha*K, 2);
            }
        }
    }
    return dEl;
}
Esempio n. 5
0
void getmovesn(int f, int r, int c)
{
    int i, nf, nr;
    int o;
    chesspiece p;
    square s1;

    s1 = SQ(f,r);
    o = opp(c);

    for (i=0;i<8;i++)
    {
	nf = f+df[i];
	nr = r+dr[i];

	if (offboardp(nf,nr))
	    continue;

	p = getpiece(nf, nr);
	if (!p || (c != chesspiececolor(p)))
	{
	    pushmove(MV(s1, SQ(nf, nr)));
	}
    }
}
Esempio n. 6
0
ElliPoint *dif(ElliPoint *p,ElliPoint *q,int a,int base) {
	ElliPoint *o,*r;
	o = opp(q,base);
	r = sum(p,o,a,base);
	free(o);
	return r;
}
Esempio n. 7
0
matrix<T> matrix<T>::operator-() const
{
    matrix<T> opp(m_rows, m_cols);
    for(int i = nElements()-1; i >= 0; i--)
        opp.p[i] = -p[i];
    return opp;
}
Esempio n. 8
0
void cmd_edit(char *s)
{
#if 0
    char str[80];
    int color = WHITE;

    board->flags = 0;

    for(;;)
    {
	fgets(str, 80, stdin);
	if (!strcmp(str,"#\n"))
	    clearboard();
	else if (!strcmp(str, ".\n"))
	    break;
	else if (!strcmp(str, "c\n"))
	    color=opp(color);
	else if (!strcmp(str, "C\n"))
	    color=opp(color);
	else
	{
	    int f, r;
	    chesspiece p;
	    f = str[1]-'a';
	    r = str[2]-'1';
	    p = lookup(str[0]);
	    if (color==WHITE) p &= ~(1);
	    else p |= 1;
	    setpiece(f,r,p);
	}
    }

    countmaterial();

    /* set up castling rights */
    if ((board->kings[WHITE]==E1)&&(getpiece__(H1)==WROOK))
	board->flags |= wkc;
    if ((board->kings[WHITE]==E1)&&(getpiece__(A1)==WROOK))
	board->flags |= wqc;
    if ((board->kings[BLACK]==E8)&&(getpiece__(H8)==BROOK))
	board->flags |= bkc;
    if ((board->kings[BLACK]==E8)&&(getpiece__(A8)==BROOK))
	board->flags |= bqc;

    compute_hash();
#endif
}
Esempio n. 9
0
File: eval.c Progetto: cwbowron/BCE
int score_knight(int f, int r,int c)
{
    int o = opp(c);
    int df = abs(F(board->kings[o])-f);
    int dr = abs(R(board->kings[o])-r);
    int bonus=(14-(dr+df))*weights[KNIGHT_TROPISM];

    verbose_print(f,r,bonus,"knight/king tropism");
    return bonus;
}
Esempio n. 10
0
File: eval.c Progetto: cwbowron/BCE
int score_queen(int f, int r, int c)
{
    int o = opp(c);
    int df = abs(F(board->kings[o])-f);
    int dr = abs(R(board->kings[o])-r);
    int bonus=(7-min(df,dr))*weights[QUEEN_TROPISM];

    verbose_print(f,r,bonus,"queen/king tropism");
    return bonus;
}
Esempio n. 11
0
void dynamics_mul::compute_curvature_force(){
    for (auto eid = s_dsc->halfedges_begin(); eid != s_dsc->halfedges_end(); eid++) {

        if (s_dsc->is_interface(*eid)) {
            auto hew0 = s_dsc->walker(*eid);
            
            // Find next edge on the boundary
            auto hew1 = hew0.next().opp();
            while (1) {
                if (s_dsc->is_interface(hew1.halfedge())) {
                    hew1 = hew1.opp();
                    break;
                }
                
                hew1 = hew1.next().opp();
            }
            
            assert(hew0.halfedge() != hew1.halfedge());
            
            Vec2 p0 = s_dsc->get_pos(hew0.vertex()) - s_dsc->get_pos(hew0.opp().vertex());
            Vec2 p1 = s_dsc->get_pos(hew1.vertex()) - s_dsc->get_pos(hew1.opp().vertex());
            
            Vec2 norm0(p0[1], -p0[0]); norm0.normalize();
            Vec2 norm1(p1[1], -p1[0]); norm1.normalize();
            Vec2 norm = norm0 + norm1; norm.normalize();

#ifdef DEBUG
            assert(norm.length() < 1.1 and norm.length() > 0.9);
#endif
            
            
            double l0 = p0.length();
            double l1 = p1.length();
            double angle = std::atan2(CGLA::cross(p0, p1), DSC2D::Util::dot(p0, p1));
            double curvature = angle / (l0/2.0 + l1/2.0);
            
            s_dsc->add_node_internal_force(
                    hew0.vertex(), -norm*curvature*s_dsc->get_avg_edge_length()*g_param.alpha);
        }
    }
}
Esempio n. 12
0
void Edges::endpoint(Edge *e, EdgeEnd lr, Site *s) {
    e -> ep[lr] = s;
    sites.ref(s);
    if(e -> ep[opp(lr)] == (Site *) 0) return;
    clip_line (e);
#ifdef STANDALONE
    out_ep(e);
#endif
    sites.deref(e->reg[le]);
    sites.deref(e->reg[re]);
    fedges.free(e);
}
Esempio n. 13
0
int main(int argc, char *argv[]) {
	char cards[15][10] = {
		{"I"},
		{"zero"},
		{"succ"},
		{"dbl"},
		{"get"},
		{"put"},
		{"S"},
		{"K"},
		{"inc"},
		{"dec"},
		{"attack"},
		{"help"},
		{"copy"},
		{"revive"},
		{"zombie"}
	};

	struct cmd lastopp_cmd;
	lastopp_cmd.lr = 1;
	strcpy(lastopp_cmd.card, cards[0]);
	lastopp_cmd.slot = 0;
		
	if (argc>1 && !strcmp(argv[1], "1"))
		opp(&lastopp_cmd);
	
	while (1) {
		if (lastopp_cmd.lr == 1)
			fprintf (stdout, "%d\n%s\n%d\n", lastopp_cmd.lr, lastopp_cmd.card, lastopp_cmd.slot);
		else
			fprintf (stdout, "%d\n%d\n%s\n", lastopp_cmd.lr, lastopp_cmd.slot, lastopp_cmd.card);

		fflush(stdout);

		opp(&lastopp_cmd);
		
	}
	return 0;
}
Esempio n. 14
0
File: eval.c Progetto: cwbowron/BCE
int score_rook(int f, int r, int c)
{
    int o = opp(c);
    int df = abs(F(board->kings[o])-f);
    int dr = abs(R(board->kings[o])-r);
    int bonus=(7-min(df,dr))*weights[ROOK_TROPISM];
    
    verbose_print(f,r,bonus,"rook/king tropism");

    if (c == WHITE)
    {
	if (r == 6)
	{
	    bonus += weights[SEVENTH_RANK_ROOK];
	    verbose_print(f,r,weights[SEVENTH_RANK_ROOK],"seventh rank rook");
	}
    }
    else if (r == 1)
    {
	bonus += weights[SEVENTH_RANK_ROOK];
	verbose_print(f,r,weights[SEVENTH_RANK_ROOK],"seventh rank rook");
    }
    
    if (rooks[c] == -1)
    {
	rooks[c] = SQ(f,r);
    }
    else
    {
	if ((f==F(rooks[c]))||(r==R(rooks[c])))
	{
	    bonus += weights[DOUBLEDROOKS];
	    verbose_print(f,r,weights[DOUBLEDROOKS], "doubled rooks");
	}
    }
    
    if (!board->pawns[c][f+1])
    {
	bonus += weights[SEMIOPEN];
	if (!board->pawns[o][f+1])
	{
	    bonus += weights[OPENFILE];
	    verbose_print(f,r,weights[OPENFILE]+weights[SEMIOPEN],
			  "rook on open file");
	}
	else verbose_print(f,r,weights[SEMIOPEN], "rook on semi-open file");
    }
    
    return bonus;
}
Esempio n. 15
0
ElliPoint *mul(ElliPoint *p,int n,int a,int base) {
	ElliPoint *q,*r;
	if (n == 0) return ElliInfty();
	if (n < 0) {
		q = opp(p,base);
		r = mul(q,-n,a,base);
		free(q);
		return r;
	}
	q = mul(p,n-1,a,base);
	r = sum(p,q,a,base);
	free(q);
	return r;
}
Esempio n. 16
0
void dynamics_mul::get_curvature(dsc_obj *obj, HMesh::Walker hew, double &Kcur, double &Kpre){
    // Find preveous edge
    auto hew_pre = hew.prev().opp();
    while (1) {
        if (obj->is_interface(hew_pre.halfedge())) {
            hew_pre = hew_pre.opp();
            break;
        }
        
        hew_pre = hew_pre.prev().opp();
    }
    
    Kcur = get_curvature(obj, hew);
    Kpre = get_curvature(obj, hew_pre);
}
Esempio n. 17
0
void getmovesk_nocastles(int f, int r, int c)
{
    int df, dr, nf, nr;
    int o;
    chesspiece p;
    
    square s1 = SQ(f,r);
    o = opp(c);
    
    for (df=-1;df<2;df++)
    for (dr=-1;dr<2;dr++)
    {
	if (!df && !dr) continue;
	
	nf = f+df;
	nr = r+dr;
	p = getpiece(nf, nr);
	if (!offboardp(nf, nr) && (!p || (c != chesspiececolor(p))))
	    pushmove(MV(s1, SQ(nf, nr)));
    }
}
Esempio n. 18
0
/*-----------------------------------------------------------------------------*/
Color Notation::getWinPlayer(const Move& move) const
{
	Color player = NO_COLOR;

	switch (move.move_type())
	{
	case MoveType::RESIGN: // 投了
	case MoveType::TIMEOUT: // 時間切れ
	case MoveType::LOSE_FOUL: // 反則負け
		player = opp(move.side()); // 相手が勝ち
		break;
	case MoveType::STOP: // 中断
	case MoveType::REPETITION: // 千日手
	case MoveType::DRAW: // 持将棋
		break;
	case MoveType::WIN_FOUL: // 反則勝ち
		player = move.side(); // 自分が勝ち
		break;
	}

	return player;
}
Esempio n. 19
0
double dynamics_mul::get_total_energy(dsc_obj *obj, std::map<int, double>  intensity_map){
    double total_length = 0.0;
    HMesh::HalfEdgeAttributeVector<int> touch(obj->get_no_halfedges(), 0);
    for (auto eid = obj->halfedges_begin(); eid != obj->halfedges_end(); eid++) {
        auto hew = obj->walker(*eid);
        if (!touch[*eid] and obj->is_interface(*eid)) {
            total_length += obj->length(*eid);
        }
        
        touch[*eid] = 1;
        touch[hew.opp().halfedge()] = 1;
    }
    
    double E = 0.0;
    for (auto fid = obj->faces_begin(); fid != obj->faces_end(); fid++) {
        double ci = intensity_map[obj->get_label(*fid)];
        auto tris = obj->get_pos(*fid);
        
        Vec2 min(INFINITY, INFINITY), max(-INFINITY, -INFINITY);
        for (auto p: tris){
            min[0] = std::min(min[0], p[0]);
            min[1] = std::min(min[1], p[1]);
            max[0] = std::max(max[0], p[0]);
            max[1] = std::max(max[1], p[1]);
        }
        
        for (int i = floor(min[0]); i < ceil(max[0]); i++) {
            for (int j = floor(min[1]); j < ceil(max[1]); j++) {
                if (helper_t::is_point_in_tri(Vec2(i,j), tris)) {
                    double I = s_img->get_intensity(i, j);
                    E += (I-ci)*(I-ci);
                }
            }
        }
    }
    
    return g_param.alpha*total_length + g_param.beta*E;
}
Esempio n. 20
0
/* returns -1 for stalemate or winner's color */
int playchess()
{
    int use_pondering = 0;
    
    printboard_and_time();
     
    for (;;)
    {
	long starttime, endtime;
	move m;
	int g;

	g = gameoverp(tomove());

	if (g)
	{
	    switch (g)
	    {
		case END_CHECKMATE:
		    if (tomove() == BLACK)
		    {
			return end(WHITE, "white mates");
		    }
		    else
		    {
			return end(BLACK, "black mates");
		    }
		case END_STALEMATE:
		    return end(-1, "stalemate");
		case NON_MATERIAL:
		    return end(-1, "insufficient material");
		case REP_DRAW:
		    if (!robo_mode)
		    {
			printf("drawable position\n");
		    }
		    if (computer[WHITE]||computer[BLACK])
		    {
			if (robo_mode)
			    tellics("draw\n");
			return end(-1, "draw by repetition of moves");
		    }
		    break;
	    }
	}
    
	starttime = get_ms();
	
	if (computer[tomove()])
	{
	    m = bce();

	    if ((m!=dummymove)&&(validmove(m)==1))
	    {
		printf("move %s\n", movestring(m));
	    }
	    else
	    {
		if (robo_mode)
		{
		    tellics("mailmoves\n");
		    tellics(
			"message madhacker valid? = %d, move = %s, wouldbeincheckp() = %d, wouldbeinfullcheckp() = %d, pv = %s\n",
			validmove(m),
			movestring(m),
			wouldbeincheckp(m),
			wouldbeincheckfullp(m),
			thoughts);
		    tellics("abort\n");
		}
		else
		{
		    printf("BCE returned invalid move: %s\n", movestring(m));
		    printf("valid? = %d\n", validmove(m));

		    fprintf(stdout, "random seed = %ld\n", seed);
		    fprintf(stdout, "hash = %lld\n", board->hash);
		    fprintf(stdout, "draw? = %d g = %d\n",
			    draw_by_rep(), g);
		    computer[1] = computer[0] = 0;
		}
	    }
	    use_pondering = 1;
	}
	else 
	{
	    if ((ponder_mode && computer[opp(tomove())])&&
		use_pondering)
	    {
		ponder();
		use_pondering = 0;
	    }
	    
	    m = usermove();
	    use_pondering = 0;
	}
    
	endtime = get_ms();
	chessclock[tomove()] -= (endtime-starttime);
	chessclock[tomove()] += clockinc;
    
	if (m)
	{
	    domove(m);
	    update_state(m);
	    printboard_and_time();
	}
    }
}
Esempio n. 21
0
void cmd_go(char *s)
{
    computer[opp(tomove())]=0;
    computer[tomove()]=1;
}
Esempio n. 22
0
/*
 * @return The abscissa of the intersection point between two parabolas
 */
static long Sep(int i, int u, long gi2, long gu2)
{
	return intdivint(sum(sum((long)(u * u - i * i), gu2), opp(gi2)), 2 * (u - i));
}
Esempio n. 23
0
void getmovesk(int f, int r, int c)
{
    int df, dr, nf, nr;
    int o;
    chesspiece p;

    square s1 = SQ(f,r);
    o = opp(c);

    for (df=-1; df<2; df++)
        for (dr=-1; dr<2; dr++)
        {
            if (!df && !dr) continue;

            nf = f+df;
            nr = r+dr;
            p = getpiece(nf, nr);
            if (!offboardp(nf, nr) && (!p || (c != chesspiececolor(p))))
                pushmove(MV(s1, SQ(nf, nr)));
        }

    if (c==WHITE)
    {
        if (wqcastlep()&&
                !getpiece__(D1)&&
                !getpiece__(C1)&&
                !getpiece__(B1)&&
                !incheckp(c)&&
                !wouldbeincheckp(THRUWQ))
        {
            pushmove(WQC);
        }
        if (wkcastlep()&&
                !getpiece__(F1)&&
                !getpiece__(G1)&&
                !incheckp(c)&&
                !wouldbeincheckp(THRUWK))
        {
            pushmove(WKC);
        }
    }
    else
    {
        if (bqcastlep()&&
                !getpiece__(D8)&&
                !getpiece__(C8)&&
                !getpiece__(B8)&&
                !incheckp(c)&&
                !wouldbeincheckp(THRUBQ))
        {
            pushmove(BQC);
        }
        if (bkcastlep()&&
                !getpiece__(F8)&&
                !getpiece__(G8)&&
                !incheckp(c)&&
                !wouldbeincheckp(THRUBK))
        {
            pushmove(BKC);
        }
    }
}
Esempio n. 24
0
void dynamics_mul::compute_intensity_force(){
    HMesh::HalfEdgeAttributeVector<int> touched(s_dsc->get_no_halfedges(), 0);
    
    for (auto eit = s_dsc->halfedges_begin(); eit != s_dsc->halfedges_end(); eit++) {
        if(s_dsc->is_interface(*eit) and !touched[*eit]){
            auto hew = s_dsc->walker(*eit);
            
            double c0 = mean_inten_[s_dsc->get_label(hew.face())];
            double c1 = mean_inten_[s_dsc->get_label(hew.opp().face())];
            
            // Loop on the edge
            auto p0 = s_dsc->get_pos(hew.opp().vertex());
            auto p1 = s_dsc->get_pos(hew.vertex());
            
            Vec2 L01 = p1 - p0;
            L01.normalize();
            Vec2 N01(L01[1], -L01[0]); // Outward pointing normal
            
            int length = (int)(p1 - p0).length();
            double f0 = 0.0, f1 = 0.0;
            Vec2 fg0(0.0), fg1(0.0);
            for (int i = 0; i <= length; i++) {
                auto p = p0 + (p1 - p0)*(double(i)/(double)length);
                double I = s_img->get_intensity(p[0], p[1]);
                
                // Normalize force
                int normalizedF = 1;
                double f ;
                switch (normalizedF) {
                    case 1:
                        f = ( (c0-c1)*(2*I - c0 - c1)) / ((c0-c1)*(c0-c1));
                        break;
                    case 2:
                        f = ( (c0-c1)*(2*I - c0 - c1)) / std::abs((c0 - c1));
                        break;
                    case 3:
                        f = (c0-c1)*(2*I - c0 - c1);
                        break;
                    default:
                        f = 0.0;
                        break;
                }
                
                Vec2 fu = N01*(c0-c1)*(2*I - c0 - c1);
                
                // Image gradient force
                int lengthM = 10;
                Vec2 gm(0.0); double max_grad = 0;
                for (int l = 0; l < lengthM; l++) {
                    Vec2 curPt = p + fu*(l/(double)(lengthM));
                    Vec2 gg = s_img->grad((int)curPt[0], (int)curPt[1]);
                    if (max_grad < gg.length()) {
                        max_grad = gg.length();
                        gm = gg*(lengthM - l)/(double)lengthM;
                    }
                }
                

                Vec2 fg = gm*(2*I - c0 - c1);
                
                Vec2 fv = (fu + fg)/((c0-c1)*(c0-c1));
                
                fg0 += fv*(p-p1).length() / (double)length;
                fg1 += fv*(p-p0).length() / (double)length;
                
                // Barry Centric coordinate
                f0 += f*(p-p1).length() / (double)length;
                f1 += f*(p-p0).length() / (double)length;
                
                // Image gradient force
//                Vec2 fg = s_img->grad((int)p[0], (int)p[1]) * (2*I - c0 - c1) / ((c0-c1)*(c0-c1));
//                fg0 += fg*(p-p1).length() / (double)length;
//                fg1 += fg*(p-p0).length() / (double)length;
            }
            
            // Set force
            
            Vec2 f_x0 = fg0; // N01*f0;
            Vec2 f_x1 = fg1; // N01*f1;
            
            s_dsc->add_node_external_force(hew.opp().vertex(), f_x0*g_param.beta);
            s_dsc->add_node_external_force(hew.vertex(), f_x1*g_param.beta);
            
            // Avoid retouch the edge
            touched[*eit] = 1;
            touched[hew.opp().halfedge()] = 1;
        }
    }
}
Esempio n. 25
0
void cmd_go(char *s)
{
    computer[opp(tomove())]=0;
    computer[tomove()]=searchdepth;
}
Esempio n. 26
0
void cmd_time(char *s){int v;sscanf(s, "%d", &v);chessclock[opp(tomove())] = v*10;}