Ejemplo n.º 1
0
static double findY(double* p1, double* p2)
{
 double a = p1[0];
 double B = p2[1];
 double D = p2[3];

 double x = findX(p1, p2);
 return -(a*x+D)/B;
}
 bool findX(int u) {
     for (int v = 0; v < nx; v++) {
         if (v != delX && !used[v] && g[v][u]) {//notice it's g[v][u]
             used[v] = 1;
             if (mx[v] == -1 || findX(mx[v]))
                 return 1;
         }
     }
     return 0;
 }
Ejemplo n.º 3
0
Archivo: OCR.cpp Proyecto: AAAyag/OCR-1
/// <summary>
///     Finds bounding-box of the data present in given image.
/// </summary>
/// <params name="imsSrc">
///     Source image for which bouding box has to be found.
/// </params>
/// <returns> Bounding box as CvRect. </returns>
CvRect OCR::findBB(IplImage* imgSrc)
{
	CvRect aux;
	int xmin, xmax, ymin, ymax;
	xmin=xmax=ymin=ymax=0;

	findX(imgSrc, &xmin, &xmax);
	findY(imgSrc, &ymin, &ymax);
	aux=cvRect(xmin, ymin, xmax-xmin, ymax-ymin);

	return aux;
}
Ejemplo n.º 4
0
line intervalRange(line I, vector a, vector b, line e) {
    point i1a = intersection(I.pt1, a, e);
    point i1b = intersection(I.pt1, b, e);
    point i2a = intersection(I.pt2, a, e);
    point i2b = intersection(I.pt2, b, e);
    double xmin = min(i1a.x, i1b.x, i2a.x, i2b.x);
    double ymin = min(i1a.y, i1b.y, i2a.y, i2b.y);
    if (i1a.x == INFINITY) {
        i1a.x = -INFINITY;
        i1a.y = -INFINITY;
    }
    if (i1b.x == INFINITY) {
        i1b.x = -INFINITY;
        i1b.y = -INFINITY;
    }
    if (i2a.x == INFINITY) {
        i2a.x = -INFINITY;
        i2a.y = -INFINITY;
    }
    if (i2b.x == INFINITY) {
        i2b.x = -INFINITY;
        i2b.y = -INFINITY;
    }
    double xmax = max(i1a.x, i1b.x, i2a.x, i2b.x);
    double ymax = max(i1a.y, i1b.y, i2a.y, i2b.y);
    line l;
    if (xmin < xmax) {
        l.pt1.x = xmin;
        l.pt1.y = findY(xmin, e);
        l.pt2.x = xmax;
        l.pt2.y = findY(xmax, e);
        return l;
    }
    l.pt1.x = findX(ymin, e);
    l.pt1.y = ymin;
    l.pt2.x = findX(ymax, e);
    l.pt2.y = ymax;
    return l;
}
Ejemplo n.º 5
0
void solve(Point p1, Point p2) {
    if (p1.y == p2.y) return;
    if (p1.y > p2.y) {
        Point tmp = p1;
        p1 = p2;
        p2 = tmp;
    }
    for (int i = upperBound(p1.y); i <= lowerBound(p2.y); i++) 
        if (i > 0 && i < 100) {
            double v = findX(p1,i,p2);
            if (maxX[i] < v) maxX[i] = v;
            if (minX[i] > v) minX[i] = v;
        }
}
Ejemplo n.º 6
0
CvRect findBB(IplImage* imgSrc)
{
	CvRect aux;
	int xmin, xmax, ymin, ymax;
	xmin=xmax=ymin=ymax=0;
	
	findX(imgSrc, &xmin, &xmax);
	findY(imgSrc, &ymin, &ymax);
	
	aux=cvRect(xmin, ymin, xmax-xmin, ymax-ymin);
	
	//printf("BB: %d,%d - %d,%d\n", aux.x, aux.y, aux.width, aux.height);
	
	return aux;

}
Ejemplo n.º 7
0
QuantisedFunctionPtr QuantisedFunction::inverse() const
{
    if(!isMonotonous(true)) return QuantisedFunctionPtr();
    std::vector<real_t> values(__sampling,0);
    real_t firsty = __values[0];
    real_t lasty = __values[__sampling-1];
    if (firsty > lasty) std::swap(firsty,lasty);
    real_t x = __firstx;
    real_t y = firsty ;
    real_t extent = lasty - firsty;
    real_t deltay = extent / (__sampling-1);
    bool found;
    for (uint_t i = 0; i < __sampling; ++i){
        x = findX(y,found,x);
        assert(found == true);
        values[i] = x;
        y += deltay;
    }
    return QuantisedFunctionPtr(new QuantisedFunction(values,firsty,lasty));
}
 //X[i]=1 or Y[i]=1 means it's not a key vertex!!!
 void solve() {
     memset(X, 0, sizeof(X));
     memset(Y, 0, sizeof(Y));
     for (int i = 0; i < nx; i++) {
         if (mx[i] == -1) {
             X[i] = 1;
         } else {
             delY = mx[i];
             memset(used, 0, sizeof(used));
             if (findY(i)) Y[delY] = 1;
         }
     }
     for (int j = 0; j < ny; j++) {
         if (my[j] == -1) {
             Y[j] = 1;
         } else {
             delX = my[j];
             memset(used, 0, sizeof(used));
             if (findX(j)) X[delX] = 1;
         }
     }
 }
Ejemplo n.º 9
0
int findX(int a[], int left, int right, int x)
{
	int mid=(left+right)/2;
	if(a[mid]==x)
		return mid;
	if(left>right)
		return -1;
	if(a[left]<a[mid]) //left is in order
	{
		if(x>=a[left]&&x<a[mid]) //x in left half
			return findX(a, left, mid-1, x);
		else
			return findX(a, mid+1, right, x);
	}
	else if(a[left]>a[mid])  //right is in order
	{
		if(x>a[mid]&&x<=a[right])
			return findX(a, mid+1, right, x);
		else
			return findX(a, left, mid-1, x);
	}else if(a[left]==a[mid]) 
	{
		if(a[mid]!=a[right]) // left half is all repeated, right half is different
			return findX(a, mid+1, right, x);
		else
		{
			int result=findX(a, left, mid-1, x);
			if(result==-1)
				return findX(a, mid+1, right, x);
			else 
				return result;
		}
	}
    return -1;

}
Ejemplo n.º 10
0
void main()
{
	int a[N]={3, 0, 1, 2, 3, 3, 3};
	printf("The index is %d\n", findX(a, 0, N-1, 3));
}
Ejemplo n.º 11
0
int main (int argc, char **argv)
{
	Visual *vis;
	Colormap cm;
	Display *_display;
	Imlib_Context context;
	Imlib_Image image;
	Pixmap pixmap;
	Imlib_Color_Modifier modifier = NULL;
	_display = XOpenDisplay (NULL);
	int width, height, depth, i, alpha;

	
	char str1[40];
	char str2[40];
	char str3[40];
	char str4[40];
	char str5[40];
		
	int ck0;
	int w, h;
	w = 0;
	h = 0;
			
			
	char strA1[30] = "hwe";
	char strA2[30] = "hwer";
	const char jpg[15] = "jpg"; //1
	const char png[15] = "png"; //2
	
	char *A1; 
	char *A2; 
	
	strcpy(strA1, argv[argc-1]);
	strcpy(strA2, strA1);
	A1 = strstr(strA1, jpg);
	A2 = strstr(strA2, png);
	
		//check to be sure image format is written right or abort
	 	checkForNull(A1, A2);
		
		
			
		
	for (screen = 0; screen < ScreenCount (_display); screen++)
	{
		display = XOpenDisplay (NULL);

		context = imlib_context_new ();
		imlib_context_push (context);

		imlib_context_set_display (display);
		vis = DefaultVisual (display, screen);
		cm = DefaultColormap (display, screen);

		width = DisplayWidth (display, screen);
		height = DisplayHeight (display, screen);

		depth = DefaultDepth (display, screen);

		pixmap =
			XCreatePixmap (display, RootWindow (display, screen),
							width, height, depth);

		imlib_context_set_visual (vis);
		imlib_context_set_colormap (cm);
		imlib_context_set_drawable (pixmap);
		imlib_context_set_color_range (imlib_create_color_range ());
		
		image = imlib_create_image (width, height);
		imlib_context_set_image (image);
				printf("1\n");
		imlib_context_set_color (0, 0, 0, 255);
		imlib_image_fill_rectangle (0, 0, width, height);

		imlib_context_set_dither (1);
		imlib_context_set_blend (1);
		printf("2\n");
		alpha = 255;


	for (i = 1; i < argc; i++)
	{
		if (modifier != NULL)
		{
			imlib_apply_color_modifier ();
			imlib_free_color_modifier ();
		}

	modifier = imlib_create_color_modifier ();
	imlib_context_set_color_modifier (modifier);

		if (strcmp (argv[i], "-alpha") == 0)
		{
			if ((++i) >= argc)
			{
				fprintf (stderr, "Missing alpha\n");
				continue;
			}
				if (sscanf (argv[i], "%i", &alpha) == 0)
				{
					fprintf (stderr, "Bad alpha (%s)\n", argv[i]);
					continue;
				}
		}
	else if (strcmp (argv[i], "-solid") == 0)
	{
		Color c;
		
		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if (parse_color (argv[i], &c, alpha) == 1)
			{
				fprintf (stderr, "Bad color (%s)\n", argv[i]);
				continue;
			}

		imlib_context_set_color (c.r, c.g, c.b, c.a);
		imlib_image_fill_rectangle (0, 0, width, height);
	}
		else if (strcmp (argv[i], "-clear") == 0)
		{
			imlib_free_color_range ();
			imlib_context_set_color_range (imlib_create_color_range ());
		}
	else if (strcmp (argv[i], "-add") == 0)
	{
		Color c;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if (parse_color (argv[i], &c, alpha) == 1)
			{
				fprintf (stderr, "Bad color (%s)\n", argv[i - 1]);
				continue;
			}

		imlib_context_set_color (c.r, c.g, c.b, c.a);
		imlib_add_color_to_color_range (1);
	}
	else if (strcmp (argv[i], "-addd") == 0)
	{
		Color c;
		int distance;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if ((++i) >= argc)
			{
				fprintf (stderr, "Missing distance\n");
				continue;
			}
				if (parse_color (argv[i - 1], &c, alpha) == 1)
				{
					fprintf (stderr, "Bad color (%s)\n", argv[i - 1]);
					continue;
				}
					if (sscanf (argv[i], "%i", &distance) == 1)
					{
						fprintf (stderr, "Bad distance (%s)\n", argv[i]);
						continue;
					}

				imlib_context_set_color (c.r, c.g, c.b, c.a);
				imlib_add_color_to_color_range (distance);
	}
	else if (strcmp (argv[i], "-gradient") == 0)
	{
		int angle;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing angle\n");
			continue;
		}
			if (sscanf (argv[i], "%i", &angle) == 1)
			{
				fprintf (stderr, "Bad angle (%s)\n", argv[i]);
				continue;
			}

		imlib_image_fill_color_range_rectangle (0, 0, width, height,
												angle);
	}

	 else if (strcmp (argv[i], "-fill") == 0)
	 {
		if ((++i) >= argc)
		{
		  fprintf (stderr, "Missing image\n");
		  continue;
		}
		if ( load_Mod_image(Fill, argv[i], width, height, alpha, image, ck0) == 1)
		{
		  fprintf (stderr, "Bad image (%s)\n", argv[i]);
		  continue;
		}
	 }
	else if (strcmp (argv[i], "-dia") == 0)
	{
		if((++i) >= argc)
		{
			fprintf(stderr, "missing Dia, and Image\n");
			continue;
		}
			strcpy (str1, argv[i]);
			strcpy (str2, str1);
				
			if ( findX(str1, &w, &h) == 1 )
			{
				fprintf(stderr, " Bad Format\n");
				continue;
			}
			else if (findX(str2, &w, &h) == 0 && ((++i) >= argc))
			{
				fprintf(stderr, "Missing Image\n");
				continue;
			}
			else
			{
				//if format is correct then assign a number for
				//load_Mod_Image to check
				ck0 = -2;
				w = w;
				h = h;
			}
			if( load_Mod_image(Dia, argv[i], w, h, alpha, image, ck0) == 1 )
			{
			fprintf(stderr, "Bad Image or Bad Image Dimensions \n");
			}
	} 
	else if (strcmp (argv[i], "-tile") == 0)
	{
		if ((++i) >= argc)

			{
			fprintf(stderr, "format 0 missing \n");
			continue;
			}
				strcpy (str1, argv[i]);
				strcpy (str2, str1);
				strcpy (str3, str2);
				strcpy (str4, str3);
				strcpy (str5, str4);


			if ( findX(str1, &w, &h) == 3 &&  ((++i) >= argc))
			{ 
				fprintf(stderr, "missing Image\n");
				continue;
			} //check to see if format is -tile 0 
			else if (findX(str2, &w, &h) == 3)
			{
				ck0 = 3;
				if( load_Mod_image(Tile, argv[i], width, height, alpha, image, ck0) == 1 )
				{
					fprintf(stderr, "Bad Image or Bad Image Dimensions \n");
					continue;
				}
			}

			if (findX(str3, &w, &h) == 1)
			{
				fprintf(stderr, "bad format\n");
				continue;
			}
			 if (findX(str4, &w, &h) == 0 && ((++i) >= argc))
			{
				fprintf(stderr, "missing something again\n");
				continue;
			}
			if (findX (str5, &w, &h) == 0 )
			{
				ck0 = 2;
				w = w;
				h = h;
				
			}
			if( load_Mod_image(Tile, argv[i], w, h, alpha, image, ck0) == 1 )
			{
			fprintf(stderr, "Bad Image or Bad Image Dimension\n");
			}

	}

	else if (strcmp (argv[i], "-center") == 0)
	{
		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing image\n");
			continue;
		}
			if (load_Mod_image (Center, argv[i], width, height, alpha, image, ck0) == 1)
			{
				fprintf (stderr, "Bad image (%s)\n", argv[i]);
				continue;
			}
	}
	else if (strcmp (argv[i], "-tint") == 0)
	{
		Color c;
		DATA8 r[256], g[256], b[256], a[256];
		int j;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing color\n");
			continue;
		}
			if (parse_color (argv[i], &c, 255) == 1)
			{
				fprintf (stderr, "Bad color\n");
				continue;
			}

		imlib_get_color_modifier_tables (r, g, b, a);

			for (j = 0; j < 256; j++)
			{
				r[j] = (DATA8) (((double) r[j] / 255.0) * (double) c.r);
				g[j] = (DATA8) (((double) g[j] / 255.0) * (double) c.g);
				b[j] = (DATA8) (((double) b[j] / 255.0) * (double) c.b);
			}

		imlib_set_color_modifier_tables (r, g, b, a);
	}
	else if (strcmp (argv[i], "-blur") == 0)
	{
		int intval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%i", &intval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_image_blur (intval);
	}
	else if (strcmp (argv[i], "-sharpen") == 0)
	{
		int intval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%i", &intval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_image_sharpen (intval);
	}
	else if (strcmp (argv[i], "-contrast") == 0)
	{
		double dblval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%lf", &dblval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_modify_color_modifier_contrast (dblval);
	}
	else if (strcmp (argv[i], "-brightness") == 0)
	{
		double dblval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%lf", &dblval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_modify_color_modifier_brightness (dblval);
	}
	else if (strcmp (argv[i], "-gamma") == 0)
	{
		double dblval;

		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing value\n");
			continue;
		}
			if (sscanf (argv[i], "%lf", &dblval) == 1)
			{
				fprintf (stderr, "Bad value (%s)\n", argv[i]);
				continue;
			}
		imlib_modify_color_modifier_gamma (dblval);
	}
	else if (strcmp (argv[i], "-flipv") == 0)
	{
		imlib_image_flip_vertical ();
	}
	else if (strcmp (argv[i], "-fliph") == 0)
	{
		imlib_image_flip_horizontal ();
	}
	else if (strcmp (argv[i], "-flipd") == 0)
	{
		imlib_image_flip_diagonal ();
	}
	else if (strcmp (argv[i], "-write") == 0)
	{
		if ((++i) >= argc)
		{
			fprintf (stderr, "Missing filename\n");
			continue;
		}
      imlib_save_image (argv[i]);
	}
	else
	{
		usage (argv[0]);
		imlib_free_image ();
		imlib_free_color_range ();

			if (modifier != NULL)
			{
				imlib_context_set_color_modifier (modifier);
				imlib_free_color_modifier ();
				modifier = NULL;
			}
				XFreePixmap (display, pixmap);
				exit (1);
	} // end else
} // end loop off of argc

	if (modifier != NULL)
	{
		imlib_context_set_color_modifier (modifier);
        imlib_apply_color_modifier ();
        imlib_free_color_modifier ();
        modifier = NULL;
	}

		imlib_render_image_on_drawable (0, 0);
		imlib_free_image ();
		imlib_free_color_range ();

		if (setRootAtoms (pixmap) == 0)
			fprintf (stderr, "Couldn't create atoms...\n");

		XKillClient (display, AllTemporary);
		XSetCloseDownMode (display, RetainTemporary);

		XSetWindowBackgroundPixmap (display, RootWindow (display, screen),
									pixmap);

		XClearWindow (display, RootWindow (display, screen));

		XFlush (display);
		XSync (display, False);

		imlib_context_pop ();
		imlib_context_free (context);

	} // end for loop off screen
                   //   } //  frist if statment at start of main
  return 0;
}
/* project onto intersection of (alpha)z-L1-ball and 1-LInf-ball */
static void project(double* x, double* v, double z, int N)
{
  int i;
  // first, check in v \in C
  double norm1=0; double normInf=0; double absvi;
  for (i=0;i<N;i++){
    absvi=fabs(v[i]); norm1+=absvi;
    if (absvi>normInf) {normInf=absvi;}
  }
  if ((norm1<=z)&(normInf<=1)){
    for (i=0;i<N;i++){x[i]=v[i];}
    return;
  }
  
  /* now, if v \not\in C */
  /* transform to the non-negative orthant */
  int* signv = new int[N]; double sumx=0;
  for (i=0;i<N;i++){ 
    signv[i]=sign(v[i]); v[i]=v[i]*signv[i]; 
    if (v[i]>1){x[i]=1;} else {x[i]=v[i];} /* this x for case theta=0 */
    sumx+=x[i];
  }
  /* check if theta=0 */
  if (sumx<=z){
    for (i=0;i<N;i++){x[i]=x[i]*signv[i];}
    delete[] signv;
    return;
  }
  
  /* case theta > 0 */
  
  /* construct vvb array & find its min(thetaL) and max(thetaR) */
  /* only non-negative values included (dual feasibility: theta>=0) */
  int counter=0;
  for (i=0;i<N;i++){if(v[i]>=1){counter++;}}
  int K=N+counter; /* the length of vvb array */
  double* vvb = new double[K];
  double thetaL=DBL_MAX; double thetaR=-DBL_MAX; /* requires <float.h> */
  for (i=0;i<N;i++){
    vvb[i]=v[i]; 
    if(v[i]>thetaR){thetaR=v[i];} /* max(thetaR) is among v[i]'s */
  } 
  counter=N; for (i=0;i<N;i++){
    if(v[i]>=1){vvb[counter]=v[i]-1;counter++;} 
    if(v[i]-1<thetaL){thetaL=v[i]-1;} /* min(thetaL) is among (v[i]-1)'s */
  }
  
  /* vvbadd is an array of adresses of vvb[left]...vvb[right] */
  int left=0; int right=K-1;
  double** vvbadd = new double*[K];
  for (i=0;i<K;i++) {vvbadd[i]=&vvb[i];}
  
  /* vadd - addresses of v[leftv]...v[rightv] */
  int leftv=0; int rightv=N-1;
  double** vadd = new double*[K];
  for (i=0;i<N;i++) {vadd[i]=&v[i];}
  
  double S=0; for(i=0;i<N;i++) {S=S+v[i];}
  double SL=0; double SR=0; int nL=0; int nR=0;
  double SLTemp, SRTemp; int nLTemp, nRTemp;
  int cardL, cardM, cardU; 
  double sumvM, thetaP, zP;
  int *medInds = new int[2];
  int medLInd, medRInd;
  double theta = -1;
  int iter = 0;
  int il,ic,ir;
  while (1>0){
    median(vvbadd,left,right,medInds);
    medLInd=medInds[0]; medRInd=medInds[1];
    thetaP = *vvbadd[medLInd];
    
    SLTemp=0; SRTemp=0; nLTemp=0; nRTemp=0;
    il=leftv-1; ic=leftv; ir=rightv+1;
    while (ic<ir){
      if (*vadd[ic]<thetaP){il++; swap(vadd,ic,il); SLTemp=SLTemp+*vadd[il]; nLTemp++; ic++;}
      else if (*vadd[ic]>thetaP+1){ir--; swap(vadd,ic,ir); SRTemp=SRTemp+*vadd[ir]; nRTemp++;}
      else {ic++;}
    }
    
    cardU = nR+nRTemp; cardL = nL+nLTemp; cardM = N-cardU-cardL;
    sumvM = S-SLTemp-SRTemp;
    zP = cardU+sumvM-thetaP*cardM;
    
    if (zP>z)
    {SL=SL+SLTemp; S=S-SLTemp; nL=nL+nLTemp; thetaL=thetaP; left=medRInd; leftv=il+1;}
    if (zP < z)
    {SR=SR+SRTemp; S=S-SRTemp; nR=nR+nRTemp; thetaR=thetaP; right=medLInd; rightv=ir-1;}
    
    if (zP == z){theta = thetaP; break;}
    if (right-left<=1) {break;}
    if ((right==medRInd)&(left==medLInd)){break;} /* median(thetaP) is not unique */
    iter++;if (iter == 100){printf("PROJ MAXITER: %d (error)\n",iter); break;}
  }
  
  if (theta==-1) /* theta != to one of the vvb[i] */
  {  
    double zL,zR;
    double thetaL=*vvbadd[left]; double thetaR=*vvbadd[right];
    
    double SLTempL=0; double SRTempL=0; int nLTempL=0; int nRTempL=0;
    il=leftv-1; ic=leftv; ir=rightv+1;
    while (ic<ir){
      if (*vadd[ic]<=thetaL){il++; swap(vadd,ic,il); SLTempL=SLTempL+*vadd[il]; nLTempL++; ic++;}
      else if (*vadd[ic]>=thetaL+1){ir--; swap(vadd,ic,ir); SRTempL=SRTempL+*vadd[ir]; nRTempL++;}
      else {ic++;}
    }
    cardU = nR+nRTempL; cardL = nL+nLTempL; cardM = N-cardU-cardL;
    sumvM = S-SLTempL-SRTempL;
    zL = cardU+sumvM-thetaL*cardM;
    
    double SLTempR=0; double SRTempR=0; int nLTempR=0; int nRTempR=0;
    il=leftv-1; ic=leftv; ir=rightv+1;
    while (ic<ir){
      if (*vadd[ic]<=thetaR){il++; swap(vadd,ic,il); SLTempR=SLTempR+*vadd[il]; nLTempR++; ic++;}
      else if (*vadd[ic]>=thetaR+1){ir--; swap(vadd,ic,ir); SRTempR=SRTempR+*vadd[ir]; nRTempR++;}
      else {ic++;}
    }
    cardU = nR+nRTempR; cardL = nL+nLTempR; cardM = N-cardU-cardL;
    sumvM = S-SLTempR-SRTempR;
    zR = cardU+sumvM-thetaR*cardM;
    
    if (z>zL){nR+=nRTempL+nRTempR; SR+=SRTempL+SRTempR; S-=(SRTempL+SRTempR);} 
    if (z<zR){nL+=nLTempL+nLTempR; SL+=SLTempL+SLTempR; S-=(SLTempL+SLTempR);}
    if ((z>zR)&(z<zL)){nL+=nLTempL; SL+=SLTempL; nR+=nRTempR; SR+=SRTempR; S-=(SLTempL+SRTempR);}
    
    theta=(nR+S-z)/(N-nR-nL); /* S is sumvM by construction */
  }
  
  findX(x,v,N,theta,signv);
  delete[] vvbadd; delete[] vadd; delete[] medInds; delete[] vvb; delete[] signv;
}
Ejemplo n.º 13
0
void mdFindFrustum(
    double* topLeftX,
    double* bottomLeftX,
    double* topLeftY,
    double* bottomLeftY)
{
   float   proj[16];
   float   modl[16];
   float   clip[16];
   float   t;

   /* Get the current PROJECTION matrix from OpenGL */
   glGetFloatv( GL_PROJECTION_MATRIX, proj );

   /* Get the current MODELVIEW matrix from OpenGL */
   glGetFloatv( GL_MODELVIEW_MATRIX, modl );

   /* Combine the two matrices (multiply projection by modelview) */
   clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
   clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
   clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
   clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];

   clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
   clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
   clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14];
   clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15];

   clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12];
   clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13];
   clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14];
   clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15];

   clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12];
   clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13];
   clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14];
   clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15];

   /* Extract the numbers for the RIGHT plane */
   frustum[0][0] = clip[ 3] - clip[ 0];
   frustum[0][1] = clip[ 7] - clip[ 4];
   frustum[0][2] = clip[11] - clip[ 8];
   frustum[0][3] = clip[15] - clip[12];

   /* Normalize the result */
   t = sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * frustum[0][1] + frustum[0][2] * frustum[0][2] );
   frustum[0][0] /= t;
   frustum[0][1] /= t;
   frustum[0][2] /= t;
   frustum[0][3] /= t;

   /* Extract the numbers for the LEFT plane */
   frustum[1][0] = clip[ 3] + clip[ 0];
   frustum[1][1] = clip[ 7] + clip[ 4];
   frustum[1][2] = clip[11] + clip[ 8];
   frustum[1][3] = clip[15] + clip[12];

   /* Normalize the result */
   t = sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * frustum[1][1] + frustum[1][2] * frustum[1][2] );
   frustum[1][0] /= t;
   frustum[1][1] /= t;
   frustum[1][2] /= t;
   frustum[1][3] /= t;

   /* Extract the BOTTOM plane */
   frustum[2][0] = clip[ 3] + clip[ 1];
   frustum[2][1] = clip[ 7] + clip[ 5];
   frustum[2][2] = clip[11] + clip[ 9];
   frustum[2][3] = clip[15] + clip[13];

   /* Normalize the result */
   t = sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * frustum[2][1] + frustum[2][2] * frustum[2][2] );
   frustum[2][0] /= t;
   frustum[2][1] /= t;
   frustum[2][2] /= t;
   frustum[2][3] /= t;

   /* Extract the TOP plane */
   frustum[3][0] = clip[ 3] - clip[ 1];
   frustum[3][1] = clip[ 7] - clip[ 5];
   frustum[3][2] = clip[11] - clip[ 9];
   frustum[3][3] = clip[15] - clip[13];

   /* Normalize the result */
   t = sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * frustum[3][1] + frustum[3][2] * frustum[3][2] );
   frustum[3][0] /= t;
   frustum[3][1] /= t;
   frustum[3][2] /= t;
   frustum[3][3] /= t;

   /* Extract the FAR plane */
   frustum[4][0] = clip[ 3] - clip[ 2];
   frustum[4][1] = clip[ 7] - clip[ 6];
   frustum[4][2] = clip[11] - clip[10];
   frustum[4][3] = clip[15] - clip[14];

   /* Normalize the result */
   t = sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * frustum[4][1] + frustum[4][2] * frustum[4][2] );
   frustum[4][0] /= t;
   frustum[4][1] /= t;
   frustum[4][2] /= t;
   frustum[4][3] /= t;

   /* Extract the NEAR plane */
   frustum[5][0] = clip[ 3] + clip[ 2];
   frustum[5][1] = clip[ 7] + clip[ 6];
   frustum[5][2] = clip[11] + clip[10];
   frustum[5][3] = clip[15] + clip[14];

   /* Normalize the result */
   t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2] );
   frustum[5][0] /= t;
   frustum[5][1] /= t;
   frustum[5][2] /= t;
   frustum[5][3] /= t;


 //Top X and Y: intersect LEFT and TOP
 *topLeftX = findX(frustum[1],frustum[3]);
 *topLeftY = findY(frustum[1],frustum[3]);

 //Bottom X and Y: intersect LEFT and BOTTOM
 *bottomLeftX = findX(frustum[1],frustum[2]);
 *bottomLeftY = findY(frustum[1],frustum[2]);
}