示例#1
0
void
Spring::paintEvent(QPaintEvent *ev)
{
    if (!designMode())
        return;

    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing, true);
//! @todo p.setPen(QPen(Qt::white, 1));
//! @todo p.setCompositionMode(QPainter::CompositionMode_Xor);
    if (m_orient == Qt::Vertical) {
        uint part = (height() + 16) / 16;
        if (part < 3)
            part = 3;
        uint w = width() - 1;
        uint offset = 0;
        for (uint i = 0; i < 4; i++, offset += (part * 4)) {
            QPolygon poly1(4);
            poly1.putPoints(0, 4,
                         w / 2, offset, w, offset + part, w, offset + part, w / 2, offset + part*2);
            QPainterPath ppath1;
            ppath1.addPolygon(poly1);
            p.strokePath(ppath1, p.pen());
            QPolygon poly2(4);
            poly2.putPoints(0, 4,
                         w / 2, offset + part*2, 0, offset + part*3, 0, offset + part*3, w / 2, offset + part*4);
            QPainterPath ppath2;
            ppath2.addPolygon(poly2);
            p.strokePath(ppath2, p.pen());
        }
    } else {
        uint part = (width() + 16) / 16;
        if (part < 3)
            part = 3;
        uint h = height() - 1;
        uint offset = 0;
        for (uint i = 0; i < 4; i++, offset += (part * 4)) {
            QPolygon poly1(4);
            poly1.putPoints(0, 4,
                         offset, h / 2, offset + part, 0, offset + part, 0, offset + part*2, h / 2);
            QPainterPath ppath1;
            ppath1.addPolygon(poly1);
            p.strokePath(ppath1, p.pen());
            QPolygon poly2(4);
            poly2.putPoints(0, 4,
                         offset + part*2, h / 2, offset + part*3, h, offset + part*3, h, offset + part*4, h / 2);
            QPainterPath ppath2;
            ppath2.addPolygon(poly2);
            p.strokePath(ppath2, p.pen());
        }
    }
}
int main()
{
	Poly<int> poly1(2);
	poly1[0] = 1;
	poly1[1] = 2;
	poly1[2] = 10;

	Poly<int> poly2(3);
	poly2[0] = 1;
	poly2[1] = 2;
	poly2[2] = 10;
	poly2[3] = 5;

	poly1.view();
	poly2.view();

	auto result1 = poly1 + poly2;
	auto result2 = poly1 * poly2;
	result1.view();
	result2.view();


	std::cout << std::endl << std::endl;
	return 0;
}
示例#3
0
文件: main.cpp 项目: EBirch/polymult
int main(int argc, char **argv){
	if(argc < 3){
		std::cout<<"Not enough arguments"<<std::endl;
		return -1;
	}
	int POLYTERMS = atoi(argv[2]);
	if(POLYTERMS < 2 || (POLYTERMS & (POLYTERMS - 1))){
		std::cout<<"Problem size must be a power of 2\n";
		return -1;
	}
	std::mt19937 rng(time(NULL));
	std::uniform_real_distribution<> dist(-1, 1);
	std::vector<float> times;
	for(int i = 0; i < 10; ++i){
		std::vector<float> poly1(POLYTERMS), poly2(POLYTERMS);
		std::generate(poly1.begin(), poly1.end(), [&](){return dist(rng);});
		std::generate(poly2.begin(), poly2.end(), [&](){return dist(rng);});
		std::vector<float> temp;
		auto start = std::chrono::steady_clock::now();
		switch(argv[1][1]){
			case 'd': temp = dumbMult(poly1, poly2); break;
			case '4': temp = fourMult(poly1, poly2); break;
			case '3': temp = threeMult(poly1, poly2); break;
			default: "Invalid algorithm argument\n"; return -1;
		}
		times.push_back(std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - start).count());
		// std::cout<<"Ran in "<<(std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - start).count())<<" seconds"<<std::endl;
	}
	std::cout<<"Mean time: "<<(std::accumulate(times.begin(), times.end(), 0.0) / times.size())<<" seconds"<<std::endl;
	// std::cout<<"Resulting coefficients: ";
	// for(auto &term : temp){
	// 	std::cout<<term<<" ";
	// }
	// std::cout<<std::endl;
}
示例#4
0
int point_proche(int16 table[][2]) {
	int x1, y1, i, x, y, p;
	int d1 = 1000;

	_vm->_polyStructs = &_vm->_polyStructNorm;

	if (nclick_noeud == 1) {
		x = x_mouse;
		y = y_mouse;
		x1 = table_ptselect[0][0];
		y1 = table_ptselect[0][1];

		_vm->_polyStructs = &_vm->_polyStructExp;

		getPixel(x, y);

		if (!flag_obstacle) {
			_vm->_polyStructs = &_vm->_polyStructNorm;

			getPixel(x, y);

			if (flag_obstacle) {
				polydroite(x1, y1, x, y);
			}
			_vm->_polyStructs = &_vm->_polyStructExp;
		}
		if (!flag_obstacle) {	/* dans flag_obstacle --> couleur du point */
			x1 = table_ptselect[0][0];
			y1 = table_ptselect[0][1];

			poly2(x, y, x1, y1);

			x_mouse = X;
			y_mouse = Y;
		}
	}
	_vm->_polyStructs = &_vm->_polyStructNorm;

	p = -1;
	for (i = 0; i < ctp_routeCoordCount; i++) {
		x = table[i][0];
		y = table[i][1];

		int pointDistance = computeDistance(x_mouse, y_mouse, x, y);
		if (pointDistance < d1) {
			polydroite(x_mouse, y_mouse, x, y);

			if (!flag_obstacle && ctp_routes[i][0] > 0) {
				d1 = pointDistance;
				p = i;
			}
		}
	}

	return (p);
}
示例#5
0
TESSLINE* ApproximateOutline(bool allow_detailed_fx, C_OUTLINE* c_outline) {
  TBOX loop_box;                  // bounding box
  inT32 area;                    // loop area
  EDGEPT stack_edgepts[FASTEDGELENGTH];  // converted path
  EDGEPT* edgepts = stack_edgepts;

  // Use heap memory if the stack buffer is not big enough.
  if (c_outline->pathlength() > FASTEDGELENGTH)
    edgepts = new EDGEPT[c_outline->pathlength()];

  loop_box = c_outline->bounding_box();
  area = loop_box.height();
  if (!poly_wide_objects_better && loop_box.width() > area)
    area = loop_box.width();
  area *= area;
  edgesteps_to_edgepts(c_outline, edgepts);
  fix2(edgepts, area);
  EDGEPT* edgept = poly2(edgepts, area);  // 2nd approximation.
  EDGEPT* startpt = edgept;
  EDGEPT* result = NULL;
  EDGEPT* prev_result = NULL;
  do {
    EDGEPT* new_pt = new EDGEPT;
    new_pt->pos = edgept->pos;
    new_pt->prev = prev_result;
    if (prev_result == NULL) {
      result = new_pt;
    } else {
      prev_result->next = new_pt;
      new_pt->prev = prev_result;
    }
    if (allow_detailed_fx) {
      new_pt->src_outline = edgept->src_outline;
      new_pt->start_step = edgept->start_step;
      new_pt->step_count = edgept->step_count;
    }
    prev_result = new_pt;
    edgept = edgept->next;
  }
  while (edgept != startpt);
  prev_result->next = result;
  result->prev = prev_result;
  if (edgepts != stack_edgepts)
    delete [] edgepts;
  return TESSLINE::BuildFromOutlineList(result);
}
示例#6
0
// This computes Chen and Han's magic polynomial G, which 
// has the property that G(x) = (x mod p) (mod p^e).
// Here, (x mod p) is in the interval [0,1] if p == 2,
// and otherwise, is in the interval (-p/2, p/2).
static
void compute_magic_poly(ZZX& poly1, long p, long e)
{
   FHE_TIMER_START;

   Vec<ZZ> a;

   compute_a_vals(a, p, e);

   ZZ p_to_e = power_ZZ(p, e);
   long len = (e-1)*(p-1)+2;

   ZZ_pPush push(p_to_e);

   ZZ_pX poly(0);
   ZZ_pX term(1);
   ZZ_pX X(INIT_MONO, 1);

   poly = 0;
   term = 1;
   
   for (long m = 0; m < p; m++) {
      term *= (X-m);
   }

   for (long m = p; m < len; m++) {
      poly += term * conv<ZZ_p>(a[m]);
      term *= (X-m);
   }

   // replace poly by poly(X+(p-1)/2) for odd p
   if (p % 2 == 1) {
      ZZ_pX poly2(0);

      for (long i = deg(poly); i >= 0; i--) 
         poly2 = poly2*(X+(p-1)/2) + poly[i];

      poly = poly2;
   }

   poly = X - poly;
   poly1 = conv<ZZX>(poly);
}
示例#7
0
void valide_noeud(int16 table[], int16 p, int *nclick, int16 solution0[20 + 3][2]) {
	int a, b, d, i, p1, x1, x2, y1, y2;
	//int   y=30;

	table[*nclick] = p;
	table[(*nclick) + 1] = -1;
	table_ptselect[*nclick][0] = x_mouse;
	table_ptselect[*nclick][1] = y_mouse;
	(*nclick)++;
	_vm->_polyStructs = &_vm->_polyStructNorm;

	if (*nclick == 2) {	// second point
		x1 = table_ptselect[0][0];
		y1 = table_ptselect[0][1];
		x2 = table_ptselect[1][0];
		y2 = table_ptselect[1][1];
		if ((x1 == x2) && (y1 == y2)) {
			return;
		}
		flag_aff_chemin = 1;
		_vm->_polyStructs = &_vm->_polyStructExp;

		// can we go there directly ?
		polydroite(x1, y1, x2, y2);

		if (!flag_obstacle) {
			solution0[0][0] = x1;
			solution0[0][1] = y1;
			_vm->_polyStructs = &_vm->_polyStructExp;

			poly2(x2, y2, ctp_routeCoords[select_noeud[1]][0],
			      ctp_routeCoords[select_noeud[1]][1]);

			solution0[1][0] = table_ptselect[1][0] = X;
			solution0[1][1] = table_ptselect[1][1] = Y;
			solution0[2][0] = -1;

			if ((x1 == X) && (y1 == Y)) {
				flag_aff_chemin = 0;
				return;
			}
		} else {
			// no, we take the fastest way
			solution[0] = -1;
			if (ctp_routes[select_noeud[0]][0] > 0)
				chemin0(table[0], table[1]);

			if (solution[0] == -1) {
				x1 = table_ptselect[0][0];
				y1 = table_ptselect[0][1];
				polydroite(x1, y1, x_mouse, y_mouse);
				solution0[0][0] = x1;
				solution0[0][1] = y1;
				solution0[1][0] = X;
				solution0[1][1] = Y;

				solution0[2][0] = -1;
				if ((x1 == X) && (y1 == Y)) {
					flag_aff_chemin = 0;
					return;
				}
			} else {
				solution0[0][0] = x1;
				solution0[0][1] = y1;
				i = 0;
				while (solution[i] != -1) {
					p1 = solution[i];
					solution0[i + 1][0] =
					    ctp_routeCoords[p1][0];
					solution0[++i][1] =
					    ctp_routeCoords[p1][1];
				}
				_vm->_polyStructs = &_vm->_polyStructExp;
				poly2(x2, y2,
				      ctp_routeCoords[select_noeud[1]][0],
				      ctp_routeCoords[select_noeud[1]][1]);
				solution0[i + 1][0] = table_ptselect[1][0] = X;
				solution0[i + 1][1] = table_ptselect[1][1] = Y;
				solution0[i + 2][0] = -1;
				if ((x1 == X) && (y1 == Y)) {
					flag_aff_chemin = 0;
					return;
				}

				/****** Trim down any un-necessary walk points ******/

				i++;
				d = 0;
				a = i;
				flag_obstacle = 1;
				while (d != a) {
					x1 = solution0[d][0];
					y1 = solution0[d][1];

					while (flag_obstacle && i != d) {
						x2 = solution0[i][0];
						y2 = solution0[i][1];
						_vm->_polyStructs = &_vm->_polyStructExp;
						polydroite(x1, y1, x2, y2);
						i--;
					}
					flag_obstacle = 1;
					if (d != i) {
						i++;
						for (b = d + 1; b < i; b++) {
							solution0[b][0] = -2;
						}
					} else
						i++;
					d = i;
					i = a;
				}
				flag_obstacle = 0;
			}
		}
	}
}
示例#8
0
  virtual void on_draw() {
    typedef agg::renderer_base<pixfmt> ren_base;

    pixfmt pixf(rbuf_window());
    ren_base renb(pixf);
    renb.clear(agg::rgba(1, 1, 1));

    agg::rasterizer_scanline_aa<> ras;
    agg::scanline_p8 sl;

    agg::path_storage path;

    path.move_to(m_x[0], m_y[0]);
    path.line_to((m_x[0] + m_x[1]) / 2,
                 (m_y[0] + m_y[1]) / 2);  // This point is added only to check
                                          // for numerical stability
    path.line_to(m_x[1], m_y[1]);
    path.line_to(m_x[2], m_y[2]);
    path.line_to(m_x[2], m_y[2]);  // This point is added only to check for
                                   // numerical stability

    path.move_to((m_x[0] + m_x[1]) / 2, (m_y[0] + m_y[1]) / 2);
    path.line_to((m_x[1] + m_x[2]) / 2, (m_y[1] + m_y[2]) / 2);
    path.line_to((m_x[2] + m_x[0]) / 2, (m_y[2] + m_y[0]) / 2);
    path.close_polygon();

    agg::line_cap_e cap = agg::butt_cap;
    if (m_cap.cur_item() == 1) cap = agg::square_cap;
    if (m_cap.cur_item() == 2) cap = agg::round_cap;

    agg::line_join_e join = agg::miter_join;
    if (m_join.cur_item() == 1) join = agg::miter_join_revert;
    if (m_join.cur_item() == 2) join = agg::round_join;
    if (m_join.cur_item() == 3) join = agg::bevel_join;

    // (1)
    agg::conv_stroke<agg::path_storage> stroke(path);
    stroke.line_join(join);
    stroke.line_cap(cap);
    stroke.miter_limit(m_miter_limit.value());
    stroke.width(m_width.value());
    ras.add_path(stroke);
    agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0.8, 0.7, 0.6));
    // (1)

    // (2)
    agg::conv_stroke<agg::path_storage> poly1(path);
    poly1.width(1.5);
    ras.add_path(poly1);
    agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0, 0, 0));
    // (2)

    // (3)
    agg::conv_dash<agg::conv_stroke<agg::path_storage> > poly2_dash(stroke);
    agg::conv_stroke<agg::conv_dash<agg::conv_stroke<agg::path_storage> > >
        poly2(poly2_dash);
    poly2.miter_limit(4.0);
    poly2.width(m_width.value() / 5.0);
    poly2.line_cap(cap);
    poly2.line_join(join);
    poly2_dash.add_dash(20.0, m_width.value() / 2.5);
    ras.add_path(poly2);
    agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0, 0, 0.3));
    // (3)

    // (4)
    ras.add_path(path);
    agg::render_scanlines_aa_solid(ras, sl, renb,
                                   agg::rgba(0.0, 0.0, 0.0, 0.2));
    // (4)

    agg::render_ctrl(ras, sl, renb, m_join);
    agg::render_ctrl(ras, sl, renb, m_cap);
    agg::render_ctrl(ras, sl, renb, m_width);
    agg::render_ctrl(ras, sl, renb, m_miter_limit);
  }
void skeleton_mul::smoothEachBranch() {
	smoothedBrchPts.clear() ;
	smoothedBrchPts.resize(brchPts.size()) ;
	for( int i=0; i<brchPts.size(); ++i ){
		if( brchPts[i].size()< 4 ){
			while( brchPts[i].size() < 4 ){
				std::vector<Point3f> tmp ;
				for( int j=0; j< brchPts[i].size()-1; ++j ){
					tmp.push_back( brchPts[i][j] ) ;
					tmp.push_back( (brchPts[i][j]+brchPts[i][j+1])/2 ) ;

				}
				tmp.push_back( brchPts[i].back() ) ;

				brchPts[i] = tmp ;
			}
		}

		std::vector<double> x, y, z, t ;
		for( int j=0; j<brchPts[i].size(); ++j){
			x.push_back(brchPts[i][j].X() );
			y.push_back(brchPts[i][j].Y() );
			z.push_back(brchPts[i][j].Z() );
			t.push_back( (double)j/(brchPts[i].size()-1) ) ;
		}

		Spline3Interp poly1( t, x);
		poly1.calcCoefs();
		std::vector<double> px,py, pz ;
		for( double t=0.0; t<1.0; t+=0.0005 )
			px.push_back(poly1.evaluate(t)) ;
		px.push_back(poly1.evaluate(1.0)) ;


		Spline3Interp poly2( t, y);
		poly2.calcCoefs();
		for( double t=0.0; t<1.0; t+=0.0005 )
			py.push_back(poly2.evaluate(t)) ;
		py.push_back(poly2.evaluate(1.0)) ;

		Spline3Interp poly3( t, z);
		poly3.calcCoefs();
		for( double t=0.0; t<1.0; t+=0.0005 )
			pz.push_back(poly3.evaluate(t)) ;
		pz.push_back(poly3.evaluate(1.0)) ;

		for( int id=0; id<px.size(); ++id )
			smoothedBrchPts[i].push_back( Point3f(px[id], py[id], pz[id]) ) ;

	}

	for( int i=0; i<smoothedBrchPts.size() ; ++i ){
		for( int j=1; j<smoothedBrchPts[i].size()-1; ++j ){
			if( (smoothedBrchPts[i][j-1] - smoothedBrchPts[i][j]).Norm() < ReconstructorPara::branchSampleStep ){
				smoothedBrchPts[i].erase( smoothedBrchPts[i].begin()+j) ;
				j-- ;
			}

			if( (smoothedBrchPts[i][smoothedBrchPts[i].size()-1] - smoothedBrchPts[i][smoothedBrchPts[i].size()-2]).Norm() <   ReconstructorPara::branchSampleStep * 0.5)
					smoothedBrchPts[i].erase( smoothedBrchPts[i].begin()+smoothedBrchPts[i].size()-2) ;
		}
	}
}
示例#10
0
int main(int argc, char *argv[])
{
    //check fit
    printf("Polynomial fit test:\n");
    std::vector<float> x, y;

    for(int i = 0; i < 10; i++) {
        float p_x = float(i);
        float p_y = 3.0f * p_x * p_x + 2.0f * p_x + 1.0f;
        x.push_back(p_x);
        y.push_back(p_y);
    }

    pic::Polynomial poly;
    poly.fit(x, y, 2);

    poly.print();

    printf("p(4.0f) = %f\n", poly.eval(4.0f));

    float roots[2];
    bool bReal = poly.getAllRoots(roots);

    if(bReal) {
        printf("Roots: %f %f\n", roots[0], roots[1]);
    } else {
        printf("No Real roots!\n");
    }

    printf("\n\n");

    printf("Second order polynomial test:\n");
    float tmp[] = {1.0f, -3.0f, 0.5f};
    pic::Polynomial poly2(tmp, 3);
    poly2.print();

    printf("p(0.0f) = %f\n", poly2.eval(0.0f));
    printf("dp(1.0f) = %f\n", poly2.dEval(1.0f));

    bReal = poly2.getRoots(roots);

    if(bReal) {
        printf("Roots: %f %f\n", roots[0], roots[1]);
    } else {
        printf("No Real roots!\n");
    }

    printf("\n\n");

    printf("Third order polynomial test:\n");
    float tmp3[] = {-6.0f, 11.0f, -6.0f, 1.0f};
    pic::Polynomial poly3(tmp3, 4);
    poly3.print();

    float r;
    auto poly3_2 = poly3.horner(3.0f, r);    
    printf("H: %s R: %f\n", poly3_2.toString().c_str(), r);;

    printf("p(1.0f) = %f\n", poly3.eval(1.0f));
    printf("dp(1.0f) = %f\n", poly3.dEval(1.0f));

    float roots3[3];
    bReal = poly3.getAllRoots(roots3);

    if(bReal) {
        printf("Roots: %f %f %f\n", roots3[0], roots3[1], roots3[2]);
    } else {
        printf("No Real roots!\n");
    }

    printf("\n\n");

    printf("Foruth order polynomial test:\n");
    float tmp4[] = {24, -50.0f, 35.0f, -10.0f, 1.0f};
    pic::Polynomial poly4(tmp4, 5);
    poly4.print();

    auto poly4_2 = poly4.horner(3.0f, r);
    printf("H: %s R: %f\n", poly4_2.toString().c_str(), r);;

    printf("p(1.0f) = %f\n", poly4.eval(1.0f));
    printf("dp(1.0f) = %f\n", poly4.dEval(1.0f));

    float roots4[4];
    bReal = poly4.getAllRoots(roots4);

    if(bReal) {
        printf("Roots: %f %f %f %f\n", roots4[0], roots4[1], roots4[2], roots4[3]);
    } else {
        printf("No Real roots!\n");
    }

    return 0;
}