Пример #1
0
static void subdiv( union vert *v0, 
		    union vert *v1,
		    union vert *v2,
		    GLuint depth )
{
   if (depth == 0) {
      glColor3fv(v0->v.color);
      glVertex3fv(v0->v.pos);
      glColor3fv(v1->v.color); 
      glVertex3fv(v1->v.pos);
      glColor3fv(v2->v.color); 
      glVertex3fv(v2->v.pos);
   }
   else {
      union vert m[3];

      make_midpoint(&m[0], v0, v1);
      make_midpoint(&m[1], v1, v2);
      make_midpoint(&m[2], v2, v0);
      
      subdiv(&m[0], &m[2], v0, depth-1);
      subdiv(&m[1], &m[0], v1, depth-1);
      subdiv(&m[2], &m[1], v2, depth-1);
      subdiv(&m[0], &m[1], &m[2], depth-1);
   }
}
Пример #2
0
void zzn3_div2(_MIPD_ zzn3 *w)
{
#ifdef MR_OS_THREADS
    miracl *mr_mip=get_mip();
#endif
    if (mr_mip->ERNUM) return;
    MR_IN(188)
    copy(w->a,mr_mip->w1);
    if (remain(_MIPP_ mr_mip->w1,2)!=0)
        add(_MIPP_ mr_mip->w1,mr_mip->modulus,mr_mip->w1);
    subdiv(_MIPP_ mr_mip->w1,2,mr_mip->w1);
    copy(mr_mip->w1,w->a);

    copy(w->b,mr_mip->w1);
    if (remain(_MIPP_ mr_mip->w1,2)!=0)
        add(_MIPP_ mr_mip->w1,mr_mip->modulus,mr_mip->w1);
    subdiv(_MIPP_ mr_mip->w1,2,mr_mip->w1);
    copy(mr_mip->w1,w->b);

    copy(w->c,mr_mip->w1);
    if (remain(_MIPP_ mr_mip->w1,2)!=0)
        add(_MIPP_ mr_mip->w1,mr_mip->modulus,mr_mip->w1);
    subdiv(_MIPP_ mr_mip->w1,2,mr_mip->w1);
    copy(mr_mip->w1,w->c);

    MR_OUT
}
Пример #3
0
int invmodp(_MIPD_ big a,big p,big z)
{
#ifdef MR_OS_THREADS
    miracl *mr_mip=get_mip();
#endif
    big u,v,x1,x2;

    MR_IN(213);

    u=mr_mip->w1; v=mr_mip->w2; x1=mr_mip->w3; x2=mr_mip->w4;
    copy(a,u);    
    copy(p,v);    
    convert(_MIPP_ 1,x1); 
    zero(x2);      
   
    while (size(u)!=1 && size(v)!=1)
    {
        while (remain(_MIPP_ u,2)==0)
        {
            subdiv(_MIPP_ u,2,u);
            if (remain(_MIPP_ x1,2)!=0) add(_MIPP_ x1,p,x1);
            subdiv(_MIPP_ x1,2,x1);
        }
        while (remain(_MIPP_ v,2)==0)
        {
            subdiv(_MIPP_ v,2,v);
            if (remain(_MIPP_ x2,2)!=0) add(_MIPP_ x2,p,x2);
            subdiv(_MIPP_ x2,2,x2);
        }
        if (mr_compare(u,v)>=0)
        {
            mr_psub(_MIPP_ u,v,u);
            subtract(_MIPP_ x1,x2,x1);
        }
        else
        {
            mr_psub(_MIPP_ v,u,v);
            subtract(_MIPP_ x2,x1,x2);
        }
    }
    if (size(u)==1) copy(x1,z);
    else            copy(x2,z);

    if (size(z)<0) add(_MIPP_ z,p,z);

    MR_OUT
    return 1; /* note - no checking that gcd=1 */
}
Пример #4
0
int main ()
{  /*  hailstone numbers  */

    int iter,r;
    big x,y,mx;
    mirsys(400,10);
    x=mirvar(0);
    y=mirvar(0);
    mx=mirvar(0);
    iter=0;
    printf("number = \n");
    innum(x,stdin);
    do
    { /* main loop */
        if (compare(x,mx)>0) copy(x,mx);
        r=subdiv(x,2,y);
        if (r!=0)
        { /* what goes up ... */
            premult(x,3,x);
            incr(x,1,x);
        }
        /* ... must come down */
        else copy(y,x);
        otnum(x,stdout);
        iter++;
    } while (size(x)!=1);
    printf("path length = %d \n",iter);
    printf("maximum = \n");
    otnum(mx,stdout);
    return 0;
}
Пример #5
0
t_get	*subdivide(char *s, t_get *prev, char **bad_sintax)
{
  size_t l;
  t_get *link;

  if (empty(s))
    return (prev);
  if (s[0] == '\\' && prev)
    if ((!S_IN(prev->word, "\"'|;<>()`")))
      return (echappment(s, prev, bad_sintax, 0));
  if ((link = xmalloc(sizeof(*prev))) == NULL)
    return (NULL);
  link->word = (void*)(link->next = NULL);
  link->prev = prev;
  if (prev)
    prev->next = link;
  s += hempty(s);
  if (s[(link->inter = 0)] == '\\')
    return (echappment(s, link, bad_sintax, 1));
  l = subdiv(s, bad_sintax);
  if (*bad_sintax)
    return (nullify_link(link, 0, 1));
  if ((link->word = my_strndup(s, l)) == NULL)
    return (nullify_link(link, 0, 1));
  if (subdivide(s + my_strlen(link->word), link, bad_sintax) == NULL)
    return (nullify_link(link, 1, 1));
  return (link);
}
Пример #6
0
static void Display( void )
{
   glClearColor(0.3, 0.3, 0.3, 1);
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
   glPointSize(psz);

   enable( GL_POINT_SMOOTH, pointsmooth );
   enable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB, program_point_size );

   glBegin(prim);


   {
      union vert v[3];

      ASSIGN_3V(v[0].v.color, 0,0,1); 
      ASSIGN_3V(v[0].v.pos,  0.9, -0.9, 0.0);
      ASSIGN_3V(v[1].v.color, 1,0,0); 
      ASSIGN_3V(v[1].v.pos, 0.9, 0.9, 0.0);
      ASSIGN_3V(v[2].v.color, 0,1,0); 
      ASSIGN_3V(v[2].v.pos, -0.9, 0, 0.0);

      subdiv(&v[0], &v[1], &v[2], nr_steps);
   }

   glEnd();


   glFlush();
   if (show_fps) {
      ++frame_cnt;
      glutPostRedisplay();
   }
}
Пример #7
0
static void Display( void )
{
   glClearColor(0.3, 0.3, 0.3, 1);
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   glUseProgram(program);

   glBegin(GL_TRIANGLES);


   {
      union vert v[3];

      ASSIGN_3V(v[0].v.color, 0,0,1);
      ASSIGN_3V(v[0].v.pos,  0.9, -0.9, 0.0);
      ASSIGN_3V(v[1].v.color, 1,0,0);
      ASSIGN_3V(v[1].v.pos, 0.9, 0.9, 0.0);
      ASSIGN_3V(v[2].v.color, 0,1,0);
      ASSIGN_3V(v[2].v.pos, -0.9, 0, 0.0);

      subdiv(&v[0], &v[1], &v[2], nr_steps);
   }

   glEnd();


   glFlush();
}
ofMesh ofxFaceTracker2Landmarks::getMesh(vector<T> points) const {
    cv::Rect rect(0, 0, info.inputWidth, info.inputHeight);
    cv::Subdiv2D subdiv(rect);
    
    for(int i=0;i<points.size();i++){
        if( rect.contains(points[i]) ){
            subdiv.insert(points[i]);
        }
    }
    
    vector<cv::Vec6f> triangleList;
    subdiv.getTriangleList(triangleList);
    
    ofMesh mesh;
    mesh.setMode(OF_PRIMITIVE_TRIANGLES);
    
    for( size_t i = 0; i < triangleList.size(); i++ )
    {
        cv::Vec6f t = triangleList[i];
        
        cv::Point2f pt1 = cv::Point(cvRound(t[0]), cvRound(t[1]));
        cv::Point2f pt2 = cv::Point(cvRound(t[2]), cvRound(t[3]));
        cv::Point2f pt3 = cv::Point(cvRound(t[4]), cvRound(t[5]));
        
        // Draw rectangles completely inside the image.
        if ( rect.contains(pt1) && rect.contains(pt2) && rect.contains(pt3))
        {
            mesh.addVertex(ofxCv::toOf(pt1));
            mesh.addVertex(ofxCv::toOf(pt2));
            mesh.addVertex(ofxCv::toOf(pt3));
        }
    }
    return mesh;
    
}
Пример #9
0
void SoCapsule :: subdiv(std::vector<GsVec>& Q, int degree) {
	if (degree == 0) 
	{
		P.push_back(Q[0]);
		P.push_back(Q[1]);
		P.push_back(Q[2]);
		C.push_back(GsColor::blue);
		C.push_back(GsColor::blue);
		C.push_back(GsColor::blue);
	}
	else {
		std::vector<GsVec>   A;
		std::vector<GsVec>   R;
		std::vector<GsVec>   D;
		std::vector<GsVec>   S;
		GsVec mid_1 = (Q[0] + Q[1]) / 2;
		GsVec mid_2 = (Q[1] + Q[2]) / 2;
		GsVec mid_3 = (Q[2] + Q[0]) / 2;
		mid_1.normalize();
		mid_2.normalize();
		mid_3.normalize();

		A.push_back(Q[0]);
		A.push_back(mid_1);
		A.push_back(mid_3);
		subdiv(A, degree - 1);

		R.push_back(Q[1]);
		R.push_back(mid_1);
		R.push_back(mid_2);
		subdiv(R, degree - 1);

		D.push_back(Q[2]);
		D.push_back(mid_2);
		D.push_back(mid_3);
		subdiv(D, degree - 1);

		S.push_back(mid_1);
		S.push_back(mid_2);
		S.push_back(mid_3);
		subdiv(S, degree - 1);

	}


}
Пример #10
0
    RowVectorXi findDelaunayTriangulation(Eigen::Ref<const RowVectorX> ileavedPoints)
    {
        auto points = toSeparatedViewConst<Scalar>(ileavedPoints);
        eigen_assert(points.cols() == 2);

        // Find min max.
        RowVector2 minC = points.colwise().minCoeff();
        RowVector2 maxC = points.colwise().maxCoeff();

        // Don't make the bounds too tight.
        cv::Rect_<float> bounds(
            std::floor(minC.x() - aam::Scalar(1)), 
            std::floor(minC.y() - aam::Scalar(1)), 
            std::ceil(maxC.x() - minC.x() + aam::Scalar(2)), 
            std::ceil(maxC.y() - minC.y() + aam::Scalar(2)));

        cv::Subdiv2D subdiv(bounds);

        std::vector<cv::Point2f> controlPoints;

        for (MatrixX::Index i = 0; i < points.rows(); ++i) {
            cv::Point2f c(points(i, 0), points(i, 1));
            subdiv.insert(c);
            controlPoints.push_back(c);
        }

        std::vector<cv::Vec6f> triangleList;
        subdiv.getTriangleList(triangleList);
        
        RowVectorXi triangleIds(triangleList.size() * 3);

        int validTris = 0;
        for (size_t i = 0; i < triangleList.size(); i++)
        {
            cv::Vec6f t = triangleList[i];

            cv::Point2f p0(t[0], t[1]);
            cv::Point2f p1(t[2], t[3]);
            cv::Point2f p2(t[4], t[5]);

            if (bounds.contains(p0) && bounds.contains(p1) && bounds.contains(p2)) {

                auto iter0 = std::find(controlPoints.begin(), controlPoints.end(), p0);
                auto iter1 = std::find(controlPoints.begin(), controlPoints.end(), p1);
                auto iter2 = std::find(controlPoints.begin(), controlPoints.end(), p2);

                triangleIds(validTris * 3 + 0) = (int)std::distance(controlPoints.begin(), iter0);
                triangleIds(validTris * 3 + 1) = (int)std::distance(controlPoints.begin(), iter1);
                triangleIds(validTris * 3 + 2) = (int)std::distance(controlPoints.begin(), iter2);

                ++validTris;
            }
        }

        return triangleIds.leftCols(validTris * 3);
    }
Пример #11
0
void fpmul(_MIPD_ flash x,int n,int d,flash y)
{ /* multiply x by small fraction n/d - y=x*n/d */
    int r,g;
#ifdef MR_OS_THREADS
    miracl *mr_mip=get_mip();
#endif
    if (mr_mip->ERNUM) return;
    if (n==0 || size(x)==0)
    {
        zero(y);
        return;
    }
    if (n==d)
    {
        copy(x,y);
        return;
    }

    MR_IN(42)

    if (d<0)
    {
        d=(-d);
        n=(-n);
    }
    numer(_MIPP_ x,mr_mip->w1);
    denom(_MIPP_ x,mr_mip->w2);
    r=subdiv(_MIPP_ mr_mip->w1,d,mr_mip->w3);
    g=igcd(d,r);
    r=subdiv(_MIPP_ mr_mip->w2,n,mr_mip->w3);
    g*=igcd(n,r);
    mr_mip->check=OFF;
    premult(_MIPP_ mr_mip->w1,n,mr_mip->w5);
    premult(_MIPP_ mr_mip->w2,d,mr_mip->w6);
    subdiv(_MIPP_ mr_mip->w5,g,mr_mip->w5);
    subdiv(_MIPP_ mr_mip->w6,g,mr_mip->w6);
    mr_mip->check=ON;
    if (fit(mr_mip->w5,mr_mip->w6,mr_mip->nib))
        fpack(_MIPP_ mr_mip->w5,mr_mip->w6,y);
    else
        mround(_MIPP_ mr_mip->w5,mr_mip->w6,y);
    MR_OUT
}
Пример #12
0
Geometry& subdivide(Geometry& geo, int amount)
{
	while (amount--) {
		Geometry subdivided;
		subdivided.positions.reserve(geo.positions.size() * 4);
		subdivided.texcoords.reserve(geo.texcoords.size() * 4);
		subdivided.normals.reserve(geo.normals.size() * 4);
		subdivided.quads.reserve(geo.quads.size() * 4);
		for (const auto& quad : geo.quads) {
			uint base = subdivided.positions.size();
			subdiv(quad, subdivided.positions, geo.positions);
			subdiv(quad, subdivided.texcoords, geo.texcoords);
			subdiv(quad, subdivided.normals, geo.normals);
			subdivided.quads.emplace_back(base + 0, base + 1, base + 8, base + 7);
			subdivided.quads.emplace_back(base + 1, base + 2, base + 3, base + 8);
			subdivided.quads.emplace_back(base + 8, base + 3, base + 4, base + 5);
			subdivided.quads.emplace_back(base + 7, base + 8, base + 5, base + 6);
		}
		geo = std::move(subdivided);
		normalizeNormals(geo);
	}
	return geo;
}
Пример #13
0
void SoCapsule::build(float len, float rt, float rb, int degree) {
	P.clear(); C.clear();
    P.reserve(18); C.reserve(18); // reserve space to avoid re-allocations from the calls below
	// circle radius rt, vetex at cos, sin, -len/2
	
	double thetaInc = M_PI /2;

	for (double p = 0; p < 2* M_PI ; p += thetaInc) {
		for (double d = 0; d < 2 * M_PI; d += thetaInc)
		{
			std::vector<GsVec>   Q;
			Q.push_back(GsVec(cos(d)*sin(p), sin(d)*sin(p), cos(p)));
			Q.push_back(GsVec(cos(d+ thetaInc)*sin(p), sin(d+ thetaInc)*sin(p), cos(p)));
			Q.push_back(GsVec(cos(d)*sin(p + thetaInc), sin(d)*sin(p + thetaInc), cos(p + thetaInc)));
			subdiv(Q, degree);
			
			std::vector<GsVec>   S;
			S.push_back(GsVec(cos(d + thetaInc)*sin(p+thetaInc), sin(d + thetaInc)*sin(p+thetaInc), cos(p+thetaInc)));
			S.push_back(GsVec(cos(d + thetaInc)*sin(p), sin(d + thetaInc)*sin(p), cos(p)));
			S.push_back(GsVec(cos(d)*sin(p + thetaInc), sin(d)*sin(p + thetaInc), cos(p + thetaInc)));
			subdiv(S, degree);

		}
	}
		
	// circle radius rb, vertex at cos, sin, len/2

	// send data to OpenGL buffers:
	glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
	glBufferData(GL_ARRAY_BUFFER, P.size() * 3 * sizeof(float), &P[0], GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
	glBufferData(GL_ARRAY_BUFFER, C.size() * 4 * sizeof(gsbyte), &C[0], GL_STATIC_DRAW);

	_numpoints = P.size();
	P.resize(0); C.resize(0);
}
Пример #14
0
	int delaunay2( int argc, char** argv )
	{
	    cv::CommandLineParser parser(argc, argv, "{help h||}");
	    if (parser.has("help"))
	    {
	        help();
	        return 0;
	    }
	
	    Scalar active_facet_color(0, 0, 255), delaunay_color(255,255,255);
	    Rect rect(0, 0, 600, 600);
	
	    Subdiv2D subdiv(rect);
	    Mat img(rect.size(), CV_8UC3);
	
	    img = Scalar::all(0);
	    string win = "Delaunay Demo";
	    imshow(win, img);
	
	    for( int i = 0; i < 200; i++ )
	    {
	        Point2f fp( (float)(rand()%(rect.width-10)+5),
	                    (float)(rand()%(rect.height-10)+5));
	
	        locate_point( img, subdiv, fp, active_facet_color );
	        imshow( win, img );
	
	        if( waitKey( 100 ) >= 0 )
	            break;
	
	        subdiv.insert(fp);
	
	        img = Scalar::all(0);
	        draw_subdiv( img, subdiv, delaunay_color );
	        imshow( win, img );
	
	        if( waitKey( 100 ) >= 0 )
	            break;
	    }
	
	    img = Scalar::all(0);
	    paint_voronoi( img, subdiv );
	    imshow( win, img );
	
	    waitKey(0);
	
	    return 0;
	}
Пример #15
0
ZZn& ZZn::operator/=(int i)
{
 if (i==1) return *this; 
 if (i==2)
 { // make a special effort... modulus is odd
    copy(fn,get_mip()->w1);
    if (remain(get_mip()->w1,2)!=0)
        add(get_mip()->w1,get_mip()->modulus,get_mip()->w1);
    subdiv(get_mip()->w1,2,get_mip()->w1);
    copy(get_mip()->w1,fn);

    return *this;
 }
 ZZn x=i;
 nres_moddiv(fn,x.fn,fn);
 return *this;
}
Пример #16
0
void getprime(char *fname)
{ /* get prime details from file */
    FILE *fp;
    int i;
    fp=fopen(fname,"r");
    fscanf(fp,"%d\n",&np);
    for (i=0;i<np;i++) cinnum(pp[i],fp);
    fclose(fp);
    convert(1,p1);
    for (i=0;i<np;i++) multiply(p1,pp[i],p1);
    incr(p1,1,p);
    if (!isprime(p))
    {
        printf("Huh - modulus is not prime!");
        exit(0);
    }
    subdiv(p,3,lim1);
    premult(lim1,2,lim2);
}
Пример #17
0
BOOL factored(long lptr,big T)
{ /* factor quadratic residue */
    BOOL facted;
    int i,j,r,st;  
    partial=FALSE;
    facted=FALSE;
    for (j=1;j<=mm;j++)
    { /* now attempt complete factorisation of T */
        r=(int)(lptr%epr[j]);
        if (r<0) r+=epr[j];
        if (r!=r1[j] && r!=r2[j]) continue;
        while (subdiv(T,epr[j],XX)==0)
        { /* cast out epr[j] */
            e[j]++;
            copy(XX,T);
        }
        st=size(T);
        if (st==1)
        {
           facted=TRUE;
           break;
        }
        if (size(XX)<=epr[j])
        { /* st is prime < epr[mm]^2 */
            if (st>=MR_TOOBIG || (st/epr[mm])>(1+mlf/50)) break;
            if (st<=epr[mm])
                for (i=j;i<=mm;i++)
                if (st==epr[i])
                {
                    e[i]++;
                    facted=TRUE;
                    break;
                }
            if (facted) break;
            lp=st;  /* factored with large prime */
            partial=TRUE;
            facted=TRUE;
            break;
        }
    }
    return facted;
}
Пример #18
0
int main()
{
    FILE *fp;
    big q,p,p1,h,t,g,low,high;
    big pool[POOL_SIZE];
    BOOL fail;
    int i,j,p1bits,np;
    long seed,m,permutation;
    miracl *mip=mirsys(100,0);
    q=mirvar(0);
    p=mirvar(0);
    h=mirvar(0);
    t=mirvar(0);
    g=mirvar(0);
    p1=mirvar(0);
    low=mirvar(0);
    high=mirvar(0);
    gprime(10000);

/* randomise */
    printf("Enter 9 digit random number seed  = ");
    scanf("%ld",&seed);
    getchar();
    irand(seed);
    
    p1bits=PBITS-QBITS-1;

/* find number of primes pa, pb, pc etc., that will be needed */

    np=1;
    while (p1bits/np >= OBITS) np++;
    np--;

/* find the high/low limits for these primes, so that 
   the generated prime p will be exactly PBITS in length */

    expb2(p1bits-1,t);
    nroot(t,np,low);      /* np-th integer root */
    incr(low,1,low);

    premult(t,2,t);
    decr(t,1,t);
    nroot(t,np,high);

    subtract(high,low,t);   /* raise low limit up to half-way...  */
    subdiv(t,2,t);
    subtract(high,t,low);

/* generate q  */
    forever
    { /* make sure leading two bits of q 11... */
        expb2(QBITS,q);
        bigbits(QBITS-2,t);
        subtract(q,t,q);
        nxprime(q,q);
        if (logb2(q)>QBITS) continue;
        break;
    }
    printf("q= (%d bits)\n",logb2(q));
    cotnum(q,stdout);

/* generate prime pool from which permutations of np 
   primes will be picked until a Lim-Lee prime is found */

    for (i=0;i<POOL_SIZE;i++)
    { /* generate the primes pa, pb, pc etc.. */
        pool[i]=mirvar(0);
        forever
        { 
            bigrand(high,p1);
            if (mr_compare(p1,low)<0) continue;
            nxprime(p1,p1);
            if (mr_compare(p1,high)>0) continue;
            copy(p1,pool[i]);  
            break;
        }
    }

/* The '1' bits in the permutation indicate which primes are 
   picked from the pool. If np=5, start at 11111, then 101111 etc */

    permutation=1L;
    for (i=0;i<np;i++) permutation<<=1;
    permutation-=1;     /* permuation = 2^np-1 */

/* generate p   */
    fail=FALSE;
    forever 
    {
        convert(1,p1);
        for (i=j=0,m=1L;j<np;i++,m<<=1)
        {
            if (i>=POOL_SIZE) 
            { /* ran out of primes... */
                fail=TRUE;
                break;
            }
            if (m&permutation) 
            {
                multiply(p1,pool[i],p1); 
                j++;
            }
        } 
        if (fail) break;
        printf(".");   
        premult(q,2,p);
        multiply(p,p1,p);
        incr(p,1,p);
        permutation=increment(permutation);
        if (logb2(p)!=PBITS) continue;
        if (isprime(p)) break; 
    } 

    if (fail)
    {
        printf("\nFailed - very unlikely! - try increasing POOL_SIZE\n");
        return 0;
    }

    printf("\np= (%d bits)\n",logb2(p));
    cotnum(p,stdout);

/* finally find g */
    do {
        decr(p,1,t);
        bigrand(t,h);
        divide(t,q,t);
        powmod(h,t,p,g);
    } while(size(g)==1);    
   
    printf("g= (%d bits)\n",logb2(g));
    cotnum(g,stdout);

    fp=fopen("common.dss","wt");
    fprintf(fp,"%d\n",PBITS);
    mip->IOBASE=16;
    cotnum(p,fp);
    cotnum(q,fp);
    cotnum(g,fp);
    fclose(fp);
    return 0;
}
Пример #19
0
int main()
{  /*  decode using private key  */
    int i;
    big e,ep[NP],m,ke,kd,p[NP],kp[NP],mn,mx;
    FILE *ifile;
    FILE *ofile;
    char ifname[13],ofname[13];
    BOOL flo;
    big_chinese ch;
    mip=mirsys(100,0);
    for (i=0;i<NP;i++)
    {
        p[i]=mirvar(0);
        ep[i]=mirvar(0);
        kp[i]=mirvar(0);
    }
    e=mirvar(0);
    m=mirvar(0);
    kd=mirvar(0);
    ke=mirvar(0);
    mn=mirvar(0);
    mx=mirvar(0);
    mip->IOBASE=60;
    if ((ifile=fopen("private.key","r"))==NULL)
    {
        printf("Unable to open file private.key\n");
        return 0;
    }
    for (i=0;i<NP;i++)
    {
        cinnum(p[i],ifile);
    }
    fclose(ifile);
 /* generate public and private keys */
    convert(1,ke);
    for (i=0;i<NP;i++)
    {
        multiply(ke,p[i],ke);
    }
    for (i=0;i<NP;i++)
    { /* kp[i]=(2*(p[i]-1)+1)/3 = 1/3 mod p[i]-1 */
        decr(p[i],1,kd);
        premult(kd,2,kd);
        incr(kd,1,kd);
        subdiv(kd,3,kp[i]);
    }
    crt_init(&ch,NP,p);
    nroot(ke,3,mn);
    multiply(mn,mn,m);
    multiply(mn,m,mx);
    subtract(mx,m,mx);
    do
    { /* get input file */
        printf("file to be decoded = ");
        gets(ifname);
    } while (strlen(ifname)==0);
    strip(ifname);
    strcat(ifname,".rsa");
    printf("output filename = ");
    gets(ofname);
    flo=FALSE;
    if (strlen(ofname)>0) 
    { /* set up output file */
        flo=TRUE;
        ofile=fopen(ofname,"w");
    }
    printf("decoding message\n");
    if ((ifile=fopen(ifname,"r"))==NULL)
    {
        printf("Unable to open file %s\n",ifname);
        return 0;
    }
    forever
    { /* decode line by line */
        mip->IOBASE=60;
        cinnum(m,ifile);
        if (size(m)==0) break;
        for (i=0;i<NP;i++)
            powmod(m,kp[i],p[i],ep[i]);
        crt(&ch,ep,e);    /* Chinese remainder thereom */
        if (compare(e,mx)>=0) divide(e,mn,mn);
        mip->IOBASE=128;
        if (flo) cotnum(e,ofile);
        cotnum(e,stdout);
    }
    crt_end(&ch);
    fclose(ifile);
    if (flo) fclose(ofile);
    printf("message ends\n");
    return 0;
}
Пример #20
0
int main()
{
    int i;
    FILE *fp;
    big K,rid,id,w,a,b,n,q1;
    miracl *mip=mirsys(200,256);
    for (i=0;i<NPRIMES;i++)
    {
        pp[i]=mirvar(0);
        rem[i]=mirvar(0);
    }
    w=mirvar(0);
    n=mirvar(0);
    a=mirvar(0);
    b=mirvar(0);
    p=mirvar(0);
    p1=mirvar(0);     
    q1=mirvar(0);
    K=mirvar(0);
    lim1=mirvar(0);
    lim2=mirvar(0);
    id=mirvar(0);
    rid=mirvar(0);
    order=mirvar(0);

    printf("Enter ID= ");
    innum(rid,stdin);
    getprime("trap1.dat");
    copy(p,n);
    getprime("trap2.dat");
   
    multiply(n,p,n);
    printf("\ncomposite =\n");
    cotnum(n,stdout);

    premult(rid,256,id);   
    while (jack(id,n)!=1)
    { /* bad identity - id=256*rid+i */
        printf("No Discrete Log. for this ID -- incrementing\n");
        incr(id,1,id);
    }

    getprime("trap1.dat");
    copy(p1,q1);
    pollard(id,b);
    getprime("trap2.dat");
    pollard(id,a);

    xgcd(p1,q1,K,K,K); 
    subtract(b,a,w);
    mad(w,K,w,q1,q1,w);
    if(size(w)<0) add_r(w,q1,w);
    subdiv(w,2,w);
    multiply(w,p1,w);
    add_r(w,a,w);

    fp=fopen("secret.dat","w");
    otnum(rid,fp);
    cotnum(w,fp);
    cotnum(n,fp);
    fclose(fp);
    printf("\nDiscrete log (secret key) \n");
    cotnum(w,stdout);
    powltr(PROOT,w,n,id);
    subdiv(id,256,id);
    otstr(id,mip->IOBUFF);
    printf("Check Identity= %s\n",mip->IOBUFF);
    return 0;
}
Пример #21
0
double Triangulate::performDelaunay(int x, int y, int w, int h, const vector<point_2d_t> &points, vector<triangle_t> *triangles)
{
    //cout << "performDelaunay" << endl;
    triangles->clear();
    
    Rect rect(x,y,w,h);
    Subdiv2D subdiv(rect);
    
    for( int i = 0; i < points.size(); i++ )
    {
        //cout << i << " : [" << points[i].pos[0] << ", " << points[i].pos[1] << "]" << endl;
        Point2f fp(points[i].pos[0], points[i].pos[1]);
        subdiv.insert(fp);
    }
    
    vector<Vec6f> triangleList;
    subdiv.getTriangleList(triangleList);
    vector<Point> pt(3);
    
    vector<triangle_t> triangleIndList;
    
    float tol = 0.000001;
    double area = 0.;
    for( size_t i = 0; i < triangleList.size(); i++ )
    {
        bool f1 = false;
        bool f2 = false;
        bool f3 = false;
        triangle_t tri;
        
        Vec6f t = triangleList[i];
        pt[0] = Point(cvRound(t[0]), cvRound(t[1]));
        pt[1] = Point(cvRound(t[2]), cvRound(t[3]));
        pt[2] = Point(cvRound(t[4]), cvRound(t[5]));

        for(int j = 0; j < points.size(); ++j)
        {
            //cout << "x = " << points[j].pos[0] << ", y = " << points[j].pos[1] << endl;
        
            if(abs(points[j].pos[0] - pt[0].x) < tol && 
               abs(points[j].pos[1] - pt[0].y) < tol) {
                tri.ind[0] = j;
                f1 = true;
            }
            else if(abs(points[j].pos[0] - pt[1].x) < tol && 
                    abs(points[j].pos[1] - pt[1].y) < tol) {
                tri.ind[1] = j;
                f2 = true;
            }
            else if(abs(points[j].pos[0] - pt[2].x) < tol && 
                    abs(points[j].pos[1] - pt[2].y) < tol) {
                tri.ind[2] = j;
                f3 = true;
            }
        }
        
        
        if(f1 && f2 && f3) {
            triangles->push_back(tri);
            area += abs(t[0]*(t[3] - t[5]) + t[2]*(t[5] - t[1]) + t[4]*(t[1] - t[3]))/2;
        }
        
    }
    
    return area;
    
}
Пример #22
0
int main(int argc, char **argv) {
  char *infile, *outfile;
  struct model *or_model, *sub_model=NULL;
  int lev, nlev=1, rcode, nonopt_argc=1;
  int sub_method=-1, use_binary=0;
  struct subdiv_methods sm={BUTTERFLY_SUBDIV_FUNCTIONS, 
			    LOOP_SUBDIV_FUNCTIONS,
                            SPHERICAL_OR_SUBDIV_FUNCTIONS, 
			    SPHERICAL_ALT_SUBDIV_FUNCTIONS,
                            KOBBELTSQRT3_SUBDIV_FUNCTIONS};
  

  struct subdiv_functions *tmp_func=NULL;
  
  if (argc < 4 || argc > 6) {
    fprintf(stderr, 
	    "Usage: subdiv [-sph_or, -sph_alt, -but, -loop, -ksqrt3][-bin]"
            " infile outfile n_lev\n");
    exit(1);
  }
  
  for (nonopt_argc=1; nonopt_argc<=2; nonopt_argc++) {
#ifdef DEBUG
    printf("argv[%d] = %s\n", nonopt_argc, argv[nonopt_argc]);
#endif
    if (strcmp(argv[nonopt_argc], "-sph_or") == 0) {
      tmp_func = &(sm.spherical_or);
      sub_method = SUBDIV_SPH_OR;
    }
    else if (strcmp(argv[nonopt_argc], "-sph_alt") == 0) {
      tmp_func = &(sm.spherical_alt);
      sub_method = SUBDIV_SPH_ALT;
    }
    else if (strcmp(argv[nonopt_argc], "-but") == 0) {
      tmp_func = &(sm.butterfly);
      sub_method = SUBDIV_BUTTERFLY;
    }
    else if (strcmp(argv[nonopt_argc], "-loop") == 0) {
      tmp_func = &(sm.loop);
      sub_method = SUBDIV_LOOP;
    }
    else if (strcmp(argv[nonopt_argc], "-ksqrt3") == 0) 
      sub_method = SUBDIV_KOB_SQRT3;
    else if (strcmp(argv[nonopt_argc], "-bin") == 0)
      use_binary = 1;
    else
      break;
      
  }
  if (sub_method == -1) {
    fprintf(stderr, "Invalid subdivision method\n");
    fprintf(stderr, 
	    "Usage: subdiv [-sph_or, -sph_alt, -but, -loop, -ksqrt3][-bin]"
            " infile outfile n_lev\n");
    exit(1);
  }

  infile = argv[nonopt_argc];
  outfile = argv[++nonopt_argc];

  if (argc == nonopt_argc+2)
    nlev = atoi(argv[++nonopt_argc]);
  
  if (nlev < 1)
    nlev = 1;

  rcode = read_fmodel(&or_model, infile, MESH_FF_AUTO, 0);

  if (rcode < 0) {
    fprintf(stderr, "Unable to read model - error code %d\n", rcode);
    exit(-1);
  }  


  for (lev=0; lev<nlev; lev++) {

    /* performs the subdivision */
    switch (sub_method) {
    case SUBDIV_KOB_SQRT3: /* handle sqrt3 stuff separately */
      sub_model = subdiv_sqrt3(or_model, &(sm.kob_sqrt3));
      break;
    default: /* 4-to-1 split */
      sub_model = subdiv(or_model, tmp_func);     
      break;
    }

    
    __free_raw_model(or_model);
    
    or_model = sub_model;
  }
  write_raw_model(sub_model, outfile, use_binary);

  __free_raw_model(sub_model);
  return 0;
}
Пример #23
0
int main()
{  /*  encode using public key  */
    big e,m,y,ke,mn,mx;
    FILE *ifile;
    FILE *ofile;
    static char line[500];
    static char buff[256];
    char ifname[13],ofname[13];
    BOOL fli,last;
    int i,ipt,klen;
    mip=mirsys(100,0);
    e=mirvar(0);
    m=mirvar(0);
    y=mirvar(0);
    ke=mirvar(0);
    mn=mirvar(0);
    mx=mirvar(0);
    if ((ifile=fopen("public.key","rt"))==NULL)
    {
        printf("Unable to open file public.key\n");
        return 0;
    }
    mip->IOBASE=16;
    cinnum(ke,ifile);
    fclose(ifile);
    nroot(ke,3,mn);
    multiply(mn,mn,m);
    multiply(mn,m,mx);
    subtract(mx,m,mx);
    klen=0;
    copy(mx,m);
    while (size(m)>0)
    { /* find key length in characters */
        klen++;
        subdiv(m,128,m);
    }
    klen--;
    printf("file to be encoded = ");
    gets(ifname);
    fli=FALSE;
    if (strlen(ifname)>0) fli=TRUE;
    if (fli)
    { /* set up input file */
        strcpy(ofname,ifname);
        strip(ofname);
        strcat(ofname,".rsa");
        if ((ifile=fopen(ifname,"rt"))==NULL)
        {
            printf("Unable to open file %s\n",ifname);
            return 0;
        }
        printf("encoding message\n");
    }
    else
    { /* accept input from keyboard */
        ifile=stdin;
        do
        {
            printf("output filename = ");
            gets(ofname); 
        } while (strlen(ofname)==0);
        strip(ofname);    
        strcat(ofname,".rsa");
        printf("input message - finish with cntrl z\n");
    }
    ofile=fopen(ofname,"wt");
    ipt=0;
    last=FALSE;
    while (!last)
    { /* encode line by line */
        if (fgets(&line[ipt],132,ifile)==NULL) last=TRUE;
        if (line[ipt]==EOF) last=TRUE;
        ipt=strlen(line);
        if (ipt<klen && !last) continue;
        while (ipt>=klen)
        { /* chop up into klen-sized chunks and encode */
            for (i=0;i<klen;i++)
                buff[i]=line[i];
            buff[klen]='\0';
            for (i=klen;i<=ipt;i++)
                line[i-klen]=line[i];
            ipt-=klen;
            mip->IOBASE=128;
            cinstr(m,buff);
            power(m,3,ke,e);
            mip->IOBASE=16;
            cotnum(e,ofile);
        }
        if (last && ipt>0)
        { /* now deal with left overs */
            mip->IOBASE=128;
            cinstr(m,line);
            if (compare(m,mn)<0)
            { /* pad out with random number if necessary */
                bigrand(mn,y);
                multiply(mn,mn,e);
                subtract(e,y,e);
                multiply(mn,e,y);
                add(m,y,m);
            }
            power(m,3,ke,e);
            mip->IOBASE=16;
            cotnum(e,ofile);
        }
    }
    fclose(ofile);
    if (fli) fclose(ifile);
    return 0;
}   
Пример #24
0
int MakeBoxDomains(const Box &box,int nproc, int nx, int ny, int nz, inp_it result){
  int ndir[3]={nx,ny,nz}, nspl[3]={nx,ny,nz};
  int np=(nproc>0 ? nproc : -nproc);
  int startdir=2;
  int dd, curdir=-1;
  // getting minimal splits
  int nauto=0, autod[3]={-1, -1, -1}, ndef=0;
  for(dd=0;dd<3;dd++){
    int dir=(dd+startdir)%3;
    if(ndir[dir]>0 && ndir[dir]>curdir)curdir=dir;
    else if(ndir[dir]<0){
      autod[nauto++]=dir;
    }
    if(ndir[dir]==0)ndef++;
  }
  if(curdir<0 && nauto>0){
    if(nauto==1){
      ndir[autod[0]]=np;
      curdir=autod[0];
    }
    else{  // auto-finding direction and number of slices to split
      int i;
      vec_type lprod=1.;
      for(i=0;i<nauto;i++){
        lprod*=box.GetSize()[autod[i]];
      }
      vec_type k=pow((vec_type)np/lprod,vec_type(1)/vec_type(nauto));
      int mnp=0, mdir=0;
      for(i=0;i<nauto;i++){
        int tnp=(int)(box.GetSize()[autod[i]]*k);
        if(mnp<tnp){
          mnp=tnp;
          mdir=i;
        }
      }
      if(mnp>0){
        ndir[autod[mdir]]=mnp;
        curdir=autod[mdir];
      }
    }
  }
  if(curdir>=0){ // splitting by minimal dir, auto split is ignored
    // number of domains
    int ndom=0;
    // determining sub-nps
    // THIS IS NP SWITCH : !!!
    if(np<ndir[curdir])ndir[curdir]=np;
    if(np>ndir[curdir] && ndef==2){ // the last axis
      ndir[curdir]=np;
    }

    int snp=(np>ndir[curdir] ? np : ndir[curdir]);
    int mnp=snp/ndir[curdir]; // minimal np
    int dntot=snp%ndir[curdir]; // difference
    vec_type c0=(vec_type)mnp/snp, c1=(vec_type)(mnp+1)/snp;
    Vector_3 p1=box.get_p1();
    Vector_3 p2=box.get_p2();
    
    vec_type l=box.GetSize()[curdir];
    int i;
    for(i=0;i<ndir[curdir];i++){
      vec_type dl=(dntot>0 ? l*c1 : l*c0);
      int cnp=(dntot>0 ? mnp+1 : mnp);
      if(cnp<=0)break;
      dntot--;
      p2[curdir]=p1[curdir]+dl;
      Box subdiv(p1,p2);
      nspl[curdir]=0; // this direction is processed
      int ddom=MakeBoxDomains(subdiv,(nproc>0 ? cnp: -cnp),nspl[0],nspl[1],nspl[2],result);
      if(nproc>0)result+=ddom;
      ndom+=ddom;
      p1[curdir]=p2[curdir];
    }
    return ndom;
  }
  /// called with  (0,0,0) split: put this box into the set
  if(nproc>0)
    *result=box;
  return 1;   
}
Пример #25
0
int operator%(const Big& b, int i)
{Big mdbi; return(subdiv(b.fn,i, mdbi.fn));}
Пример #26
0
Big operator/(const Big& b, int i)
{Big dbi; subdiv(b.fn, i, dbi.fn); return dbi;}
Пример #27
0
int main()
{ /* Pollard's lambda algorithm for finding discrete logs  *
   * which are known to be less than a certain limit LIMIT */
    big x,n,t,trap,table[32];
    int i,j,m;
    long dm,dn,s,distance[32];
    miracl *mip=mirsys(50,0);
    x=mirvar(0);
    n=mirvar(0);
    t=mirvar(0);
    trap=mirvar(0);
    for (s=1L,m=1;;m++)
    { /* find table size */
        distance[m-1]=s;
        s*=2;     
        if ((2*s/m)>(LEAPS/4)) break;
    }
    mip->IOBASE=60;    /* get large modulus */
    cinstr(n,modulus);
    mip->IOBASE=10;
    printf("solve discrete logarithm problem - using Pollard's kangaroos\n");
    printf("finds x in y=%d^x mod n, given y, for fixed n and small x\n",ALPHA);
    printf("known to be less than %ld\n",LIMIT);
    printf("n= ");
    cotnum(n,stdout);
    for (i=0;i<m;i++) 
    { /* create table */
        lgconv(distance[i],t);
        table[i]=mirvar(0);
        powltr(ALPHA,t,n,table[i]);
    }       
    lgconv(LIMIT,t);
    powltr(ALPHA,t,n,x);
    printf("setting trap .... \n");
    for (dn=0L,j=0;j<LEAPS;j++)
    { /* set traps beyond LIMIT using tame kangaroo */
        i=subdiv(x,m,t);    /* random function */
        mad(x,table[i],x,n,n,x);
        dn+=distance[i];
    }
    printf("trap set!\n");
    copy(x,trap);
    forever
    { /* ready to solve */
        printf("Enter x= ");
        cinnum(x,stdin);
        if (size(x)<=0) break;
        powltr(ALPHA,x,n,t);
        printf("y= ");
        cotnum(t,stdout);
        copy(t,x);
        for (dm=0L;;)
        { /* unlease wild kangaroo - boing - boing ... */
            i=subdiv(x,m,t);
            mad(x,table[i],x,n,n,x);
            dm+=distance[i];
            if (compare(x,trap)==0 || dm>LIMIT+dn) break;
        }
        if (dm>LIMIT+dn)
        { /* trap stepped over */
            printf("trap failed\n");
            continue;
        }
        printf("Gotcha!\n");
        printf("Discrete log of y= %ld\n",LIMIT+dn-dm);
    }
    return 0;
}