Ejemplo n.º 1
0
int find_displacement(struct cars *car1, struct cars *car2, float *rx, float *ry)
{
        if (car1 == NULL || car2 == NULL || car1 == car2) {
                return -1;
        }

        float x, y, w, h;
        x = car1->my_car.car_x - (car1->my_car.car->w/2) - (car2->my_car.car_x + (car2->my_car.car->w/2));
        y = car1->my_car.car_y - (car1->my_car.car->h/2) - (car2->my_car.car_y + (car2->my_car.car->h/2));
        w = car1->my_car.car->w + car2->my_car.car->w;
        h = car1->my_car.car->h + car2->my_car.car->h;
        
        struct points *p1 = NULL;
        struct points *p2 = NULL;
        p1 = get_coords(&car1->my_car);
        p2 = get_coords(&car2->my_car);
        struct points *diff = NULL;
        diff = minkowski_difference(p1, p2);
        
        struct points *hull = NULL;
        hull = convex_hull(diff);

        mk_minimum_displacement(hull, rx, ry);

        free_points(&p1);
        free_points(&p2);
        free_points(&diff);
        free_points(&hull);
        
        return 0;
}
Ejemplo n.º 2
0
int main(void)
{
  Point *polygon, *hull;
  int n, hull_size;
  int i;

  while (scanf("%d", &n) == 1 && n > 0) {
    polygon = (Point *)malloc(n * sizeof(Point));
    hull = (Point *)malloc(n * sizeof(Point));
    assert(polygon && hull);

    for (i = 0; i < n; i++) {
      scanf("%lf %lf", &polygon[i].x, &polygon[i].y);
    }

    hull_size = convex_hull(polygon, n, hull);

    printf("Sorted:\n");
    for (i = 0; i < n; i++) {
      printf("(%4.2f, %4.2f) ", polygon[i].x, polygon[i].y);
    }
    printf("\n");

    printf("Hull size = %d\n", hull_size);
    for (i = 0; i < hull_size; i++) {
      printf("(%4.2f, %4.2f) ", hull[i].x, hull[i].y);
    }
    printf("\n");

    free(polygon);
    free(hull);
  }
  return 0;
}
vector<Pt> minkowski(vector<Pt> p, vector<Pt> q){
  int n = p.size() , m = q.size();
  Pt c = Pt(0, 0);
  for( int i = 0; i < m; i ++) c = c + q[i];
  c = c / m;
  for( int i = 0; i < m; i ++) q[i] = q[i] - c;
  int cur = -1;
  for( int i = 0; i < m; i ++)
    if( (q[i] ^ (p[0] - p[n-1])) > -eps)
      if( cur == -1 || (q[i] ^ (p[0] - p[n-1])) >
                       (q[cur] ^ (p[0] - p[n-1])) )
        cur = i;
  vector<Pt> h;
  p.push_back(p[0]);
  for( int i = 0; i < n; i ++)
    while( true ){
      h.push_back(p[i] + q[cur]);
      int nxt = (cur + 1 == m ? 0 : cur + 1);
      if((q[cur] ^ (p[i+1] - p[i])) < -eps) cur = nxt;
      else if( (q[nxt] ^ (p[i+1] - p[i])) >
               (q[cur] ^ (p[i+1] - p[i])) ) cur = nxt;
      else break;
    }
  for(auto &&i : h) i = i + c;
  return convex_hull(h);
}
Ejemplo n.º 4
0
        void fillconvex_ring(Mat& mask, Point2f* points, int count, int inner, int outer) {

            Point2f* hull;
            int size = convex_hull(points, count, &hull);

            // apparently we have to convert Point2f array to integers ...
            Point* hulli = new Point[size];

            for (int i = 0; i < size; i++) {
                hulli[i].x = (int) hull[i].x;
                hulli[i].y = (int) hull[i].y;
            }

            free(hull);

            fillConvexPoly(mask, hulli, size, Scalar(1), 1);

            delete [] hulli;

            Mat mask2;

            dilate(mask, mask2, Mat::ones(outer + inner, outer + inner, CV_8U));

            fillConvexPoly(mask, hulli, size, Scalar(0), 1);

            erode(mask2, mask, Mat::ones(inner, inner, CV_8U));

        }
typename mpfr_bin_ieee754_flavor<T>::representation_dec
mpfr_bin_ieee754_flavor<T>::convex_hull(mpfr_bin_ieee754_flavor<T>::representation_dec const& x,
                                 mpfr_bin_ieee754_flavor<T>::representation_dec const& y)
{
    if (!is_valid(x) || !is_valid(y) || is_nai(x) || is_nai(y))
        return nai();

    return representation_dec(convex_hull(x.first, y.first), p1788::decoration::decoration::trv);
}
Ejemplo n.º 6
0
        void minimum_area_enclosing_rectangle(
            const Polygon2d& PP, 
            vec2& S, vec2& T
        ) {

            // Note: this implementation has O(n2) complexity :-(
            // (where n is the number of vertices in the convex hull)
            // If this appears to be a bottleneck, use a smarter
            // implementation with better complexity.

            Polygon2d P ;
            convex_hull(PP, P) ;

            int N = P.size() ;
            
            // Add the first vertex at the end of P
            P.push_back(P[0]) ;

            double min_area = Numeric::big_double ;

            for(int i=1; i<=N; i++) {
                vec2 Si = P[i] - P[i-1] ;

                if( ( Si.length2() ) < 1e-20) {
                    continue ;
                }

                vec2 Ti(-Si.y, Si.x) ;
                normalize(Si) ;
                normalize(Ti) ;
                double s0 =  Numeric::big_double ;
                double s1 = -Numeric::big_double ;
                double t0 =  Numeric::big_double ;
                double t1 = -Numeric::big_double ; 
                for(int j=1; j<N; j++) {
                    vec2 D = P[j] - P[0] ;
                    double s = dot(Si, D) ;
                    s0 = gx_min(s0, s) ;
                    s1 = gx_max(s1, s) ;
                    double t = dot(Ti, D) ;
                    t0 = gx_min(t0, t) ;
                    t1 = gx_max(t1, t) ;
                }
                double area = (s1 - s0) * (t1 - t0) ;
                if(area < min_area) {
                    min_area = area ;
                    if((s1 - s0) < (t1 - t0)) {
                        S = Si ;
                        T = Ti ;
                    } else {
                        S = Ti ;
                        T = Si ;
                    }
                }
            }
        }
Ejemplo n.º 7
0
// ps からなる凸多角形の面積
Real convex_area(vector<P> ps) {
    ps = convex_hull(ps);
    int n = ps.size();
    if (n <= 2) return 0;
    Real ans = 0;
    for (int i = 0; i < n; i++) {
        ans += ps[i].det(ps[(i+1)%n]);
    }
    return ans/2;
}
Ejemplo n.º 8
0
ld max_distance(const vector<Point> &ps) {
  assert (ps.size() > 1);
  Polygon g = convex_hull(ps);
  int a = 0, b = 1;
  ld res = abs(g[0] - g[1]);
  while (a < (int)g.size()) {
    if (arg((at(g,a+1)-at(g,a)) / (at(g,b)-at(g,b+1))) > 0) ++b; else ++a;
    res = max(res, abs(at(g,a) - at(g,b)));
  }
  return res;
}
int intersect_polygon(Point *poly1, int n1, Point *poly2, int n2,
		      Point **out){
  Point *newpoly, p; char *used;
  int new_n = n1 + n2 + n1*n2;
  int count, i, j, new_count, n;
  
  newpoly = (Point *)malloc(new_n * sizeof(Point));
  *out    = (Point *)malloc(new_n * sizeof(Point));
  used    =          malloc(new_n * sizeof(Point));
  /* assert(newpoly && *out && used); */

  for (count = i = 0; i < new_n; i++) used[i] = 0;

  for (i = 0; i < n1; i++)
    if (point_in_poly(poly2, n2, poly1[i]))
      newpoly[count++] = poly1[i];
  
  for (i = 0; i < n2; i++) 
    if (point_in_poly(poly1, n1, poly2[i]))
      newpoly[count++] = poly2[i];

  for (i = 0; i < n1; i++) for (j = 0; j < n2; j++)
    if (intersect_line(poly1[i], poly1[(i+1)%n1],
		       poly2[j], poly2[(j+1)%n2], &p) == 1)
      newpoly[count++] = p;
  
  if (count >= 3) {
    n = convex_hull(newpoly, count, *out);
    if (n < 3) {
      free(*out);
      n = 0;
    }
  } else {
    free(*out);
    n = 0;
  }
  
  /* eliminate duplicates */
  for (i = 0; i < n-1; i++)
    for (j = i+1; j < n; j++)
      if ((*out)[i].x == (*out)[j].x &&
	  (*out)[i].y == (*out)[j].y)
        used[j] = 1;
  
  new_count = 0;
  for (i = 0; i < n; i++)
    if (!used[i]) (*out)[new_count++] = (*out)[i];
  n = new_count;
  free(used);
  free(newpoly);
  return n;
}
Ejemplo n.º 10
0
inline void convex_hull(Geometry const& geometry,
            OutputGeometry& hull)
{
    concept::check_concepts_and_equal_dimensions
        <
            const Geometry,
            OutputGeometry
        >();

    typedef typename detail::convex_hull::default_strategy<Geometry>::type strategy_type;

    convex_hull(geometry, hull, strategy_type());
}
Ejemplo n.º 11
0
int main(int argc, char const *argv[]) {
	int rows;
	while(scanf("%d", &rows) != EOF) {
		std::vector<Point> points;
		points.reserve(rows * sizeof(points));
		for(int i = 1; i <= rows; ++i) {
			long long x, y;
			scanf("%lld %lld", &x, &y);
			points.push_back(Point(x, y, i));
		}
		convex_hull(points);
	}
	return 0;
}
int main() {
	Polygon p;

	while (...) {
		double x = ...;
		double y = ...;
		p.push_back(Point(x,y));
	}

	Polygon hull = convex_hull(p);

	for (int i=0; i < hull.size(); i++) {
		...
	}
	return 0;
}
Ejemplo n.º 13
0
int main(void) {
    point in[MAXPOLY];		/* input points */
    polygon hull;			/* convex hull */
    int n;				/* number of points */
    int i;				/* counter */

    printf("enter # points: ");
    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        printf("enter point #%d (x, y): ", i);
        scanf("%lf %lf", &in[i][X], &in[i][Y]);
    }

    convex_hull(in, n, &hull);
    print_polygon(&hull);

    return 0;
}
Ejemplo n.º 14
0
inline void convex_hull(Geometry1 const& geometry,
            Geometry2& hull)
{
    concept::check_concepts_and_equal_dimensions
        <
            const Geometry1,
            Geometry2
        >();

    typedef typename point_type<Geometry2>::type point_type;

    typedef typename strategy_convex_hull
        <
            typename cs_tag<point_type>::type,
            Geometry1,
            point_type
        >::type strategy_type;

    convex_hull(geometry, hull, strategy_type());
}
Ejemplo n.º 15
0
void fillconvex(Mat& image, Point2f* points, int count, Scalar color) {

    Point2f* hull;
    int size = convex_hull(points, count, &hull);

    // apparently we have to convert Point2f array to integers ...
    Point* hulli = new Point[size];

    for (int i = 0; i < size; i++) {
        hulli[i].x = (int) hull[i].x;
        hulli[i].y = (int) hull[i].y;
    }

    free(hull);

    fillConvexPoly(image, hulli, size, color, 1);

    delete [] hulli;

}
Ejemplo n.º 16
0
int main() {
  int n, lixo_nao_importa;
  while(true) {
    if (scanf(" %d %d", &n, &lixo_nao_importa)!=2)
      break;
    for(int i=0; i<n; ++i) {
      scanf(" %d %d", &tree[i].x, &tree[i].y);
    }
    int sul = 0;
    for(int i=1; i<n; ++i) {
      if (tree[sul].y > tree[i].y) {
        sul = i;
      }
      if (tree[sul].y == tree[i].y &&
          tree[sul].x > tree[i].x) {
        sul = i;
      }
    }
    printf("%.2f\n", convex_hull(n, sul));
  }
  return 0;
}
Ejemplo n.º 17
0
void solve()
{
	int i, j, pt;
	on = n;
	for (i = 0; i < n; i++) a[i] = i;
	for (j = 1; n > 2; j++)
	{
		hn = n;
		memset(s, 0, sizeof(s));
		pt = getmin();
		calc_panta(pt);
		sort_pts();
		convex_hull(pt);
		out();
		for (i = 1; i < s[0]; i++) lev[s[i]] = j;
	}
	for (i = 0; i < n; i++) lev[a[i]] = j;
	pt = 0;
	for (i = 0; i < on; i++)
		if (lev[i] > pt)
			pt = lev[i];

	printf("%d\n", pt);
}
Ejemplo n.º 18
0
static nlopt_result divide_good_rects(params *p)
{
     const int n = p->n;
     double **hull;
     int nhull, i, xtol_reached = 1, divided_some = 0;
     double magic_eps = p->magic_eps;

     if (p->hull_len < p->rtree.N) {
	  p->hull_len += p->rtree.N;
	  p->hull = (double **) realloc(p->hull, sizeof(double*)*p->hull_len);
	  if (!p->hull) return NLOPT_OUT_OF_MEMORY;
     }
     nhull = convex_hull(&p->rtree, hull = p->hull, p->which_opt != 1);
 divisions:
     for (i = 0; i < nhull; ++i) {
	  double K1 = -HUGE_VAL, K2 = -HUGE_VAL, K;
	  int im, ip;

	  /* find unequal points before (im) and after (ip) to get slope */
	  for (im = i-1; im >= 0 && hull[im][0] == hull[i][0]; --im) ;
	  for (ip = i+1; ip < nhull && hull[ip][0] == hull[i][0]; ++ip) ;

	  if (im >= 0)
	       K1 = (hull[i][1] - hull[im][1]) / (hull[i][0] - hull[im][0]);
	  if (ip < nhull)
	       K2 = (hull[i][1] - hull[ip][1]) / (hull[i][0] - hull[ip][0]);
	  K = MAX(K1, K2);
	  if (hull[i][1] - K * hull[i][0]
	      <= p->minf - magic_eps * fabs(p->minf) || ip == nhull) {
	       /* "potentially optimal" rectangle, so subdivide */
	       nlopt_result ret = divide_rect(hull[i], p);
	       divided_some = 1;
	       if (ret != NLOPT_SUCCESS) return ret;
	       xtol_reached = xtol_reached && small(hull[i] + 3+n, p);
	  }

	  /* for the DIRECT-L variant, we only divide one rectangle out
	     of all points with equal diameter and function values
	     ... note that for p->which_opt == 1, i == ip-1 should be a no-op
	         anyway, since we set allow_dups=0 in convex_hull above */
	  if (p->which_opt == 1)
	       i = ip - 1; /* skip to next unequal point for next iteration */
	  else if (p->which_opt == 2) /* like DIRECT-L but randomized */
	       i += nlopt_iurand(ip - i); /* possibly do another equal pt */
     }
     if (!divided_some) {
	  if (magic_eps != 0) {
	       magic_eps = 0;
	       goto divisions; /* try again */
	  }
	  else { /* WTF? divide largest rectangle with smallest f */
	       /* (note that this code actually gets called from time
		  to time, and the heuristic here seems to work well,
		  but I don't recall this situation being discussed in
		  the references?) */
	       rb_node *max = rb_tree_max(&p->rtree);
	       rb_node *pred = max;
	       double wmax = max->k[0];
	       do { /* note: this loop is O(N) worst-case time */
		    max = pred;
		    pred = rb_tree_pred(max);
	       } while (pred && pred->k[0] == wmax);
	       return divide_rect(max->k, p);
	  }
     }
     return xtol_reached ? NLOPT_XTOL_REACHED : NLOPT_SUCCESS;
}
Ejemplo n.º 19
0
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
	QWidget container;
	container.resize(640, 680);
	container.setFocusPolicy ( Qt::NoFocus );
	Engine engine(NULL);

	QMenuBar open_menu_bar(&container);
	QMenu open_menu("&File", &container);

	QAction open("&Open", &container);
	open.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
	open_menu.addAction(&open);

	QAction reset("&Reset", &container);
	reset.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
	open_menu.addAction(&reset);

	QAction save("&Save", &container);
	save.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
	open_menu.addAction(&save);

	QAction close("&Close", &container);
	close.setShortcut(QKeySequence(Qt::ALT + Qt::Key_F4));
	open_menu.addAction(&close);

	QMenu draw_menu("&Drawmodes", &container);

	QAction points("&Points", &container);
	points.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
	draw_menu.addAction(&points);

	QAction wire("&Wireframe", &container);
	wire.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
	draw_menu.addAction(&wire);

	QAction flat("&Flat shaded", &container);
	flat.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F));
	draw_menu.addAction(&flat);

	QAction smooth("&Smooth shaded", &container);
	smooth.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_M));
	draw_menu.addAction(&smooth);

	/**** MENU CONVEX HULL ****/
	QMenu convex_hull("Convex Hull", &container);

	QAction calc("&Calculate CH", &container);
	calc.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C));
	convex_hull.addAction(&calc);

	/**** MENU HELP ****/
	QMenu help_menu("&?", &container);

	QAction help("&Help", &container);
	help.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_H));
	help_menu.addAction(&help);

	QAction about("&About", &container);
	about.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_A));
	help_menu.addAction(&about);
	
	open_menu_bar.addMenu(&open_menu);
	open_menu_bar.addMenu(&draw_menu);
	open_menu_bar.addMenu(&convex_hull);
	open_menu_bar.addMenu(&help_menu);
	
	Window_gl window(&container);
	window.setGeometry(0,22,640, 680);

	container.show();
	
	QDialog instructions( NULL );
	instructions.setFixedSize(300,180);
	instructions.setWindowTitle("Help");
	QLabel instr_text("\nUp - Sposta l'osservatore verso l'alto\nDown - Sposta l'osservatore verso il basso\nLeft - Ruota verso sinistra\nRight - Ruota verso destra\nShift+Up - Zoom In\nShift+Down - Zoom out\n\nSi ricorda che il programma e' in grado di gestire\nsolo files di tipo .OFF.\nAltri formati non sono attualmente supportati.", &instructions);
	instr_text.setTextFormat(Qt::AutoText);
	instr_text.setWordWrap(true);
	instructions.hide();

	QDialog credits( NULL );
	credits.setFixedSize(300,100);
	credits.setWindowTitle("Credits");
	QLabel cred_text("\tCGVew v.1.7\n\nA cura di Fabio Guggeri ([email protected])\ne Stefano Marras ([email protected]).\n", &credits);
	cred_text.setTextFormat(Qt::AutoText);
	cred_text.setWordWrap(true);
	credits.hide();

	QObject::connect( &open, SIGNAL(triggered()), &engine, SLOT(open_file()) );
	QObject::connect( &reset, SIGNAL(triggered()), &window, SLOT(reset()) );
	QObject::connect( &reset, SIGNAL(triggered()), &engine, SLOT(reset()) );
	QObject::connect( &save, SIGNAL(triggered()), &engine, SLOT(save_file()) );
	QObject::connect( &points, SIGNAL(triggered()), &window, SLOT(set_p_drawmode()) );
	QObject::connect( &wire, SIGNAL(triggered()), &window, SLOT(set_w_drawmode()) );
	QObject::connect( &flat, SIGNAL(triggered()), &window, SLOT(set_f_drawmode()) );
	QObject::connect( &smooth, SIGNAL(triggered()), &window, SLOT(set_s_drawmode()) );
	QObject::connect( &help, SIGNAL(triggered()), &instructions, SLOT(show()) );
	QObject::connect( &about, SIGNAL(triggered()), &credits, SLOT(show()) );
	QObject::connect( &close, SIGNAL(triggered()), &app, SLOT(quit()) );
	QObject::connect( &engine, SIGNAL(send_dcel(QVector<DCEL>&)), &window, SLOT(add_dcel(QVector<DCEL>&)) );
	QObject::connect( &calc, SIGNAL(triggered()), &engine, SLOT(calculate_ch()) );

	window.setFocus();

	return app.exec();
}
Ejemplo n.º 20
0
void genConvex4Error(vector<vector<VECTOR3> > &flowfieldAry, int start, int sampling)
{
    println ("Compute convex4 Error");
    vector<float> errProximateRatioField(W*H*D);
    int x, y, z, i, dim;
    //d=2; //!!!!!!!!!!
    for (z = 0; z < D; z++)
    {
        if (y%10==0)
            println("z=%d", z);
        for (y = 0; y < H; y++)
            for (x = 0; x < W; x++)
            {
                int id = POS_ID(x,y,z);
                // interp
                VECTOR3 dist = flowfieldAry[sampling][id]
                        - flowfieldAry[0][id];
                vector<VECTOR3> interpAry(sampling+1);
                for (i = 0; i <= sampling; i++) {
                    VECTOR3 off = dist * (float(i)/sampling);
                    VECTOR3 &x0 = flowfieldAry[0][id];
                    VECTOR3 interp(x0[0] + off[0], x0[1] + off[1],
                            x0[2] + off[2]);
                    interpAry[i] = interp;
                }

                vector<float> errProximateRatioAry;
                for (dim=0; dim<3; dim++)
                {
                    vector<VECTOR2> convex;
                    convex.push_back(VECTOR2(0, flowfieldAry[0][id][dim]));
                    convex.push_back(VECTOR2(1, flowfieldAry[1][id][dim]));
                    int lower_hull_size, upper_hull_size;
                    for (i=2; i<=sampling; i++)
                    {
                        convex.push_back(VECTOR2(i, flowfieldAry[i][id][dim]));

                        convex = convex_hull(convex, &lower_hull_size);
                        upper_hull_size = convex.size()-lower_hull_size+1;
#ifdef DEBUG_CONVEX
                        printf("lower=%d upper=%d\n", lower_hull_size, upper_hull_size);
#endif

                        if (upper_hull_size >= 5)
                        {
                            //println("upper cut");
                            int s = convex.size();
                            //   merge last 4 points -> 3 points
                            //println("%f %f ", convex[s-4][0], convex[s-4][1]);
                            //println("%f %f ", convex[s-3][0], convex[s-3][1]);
                            //println("%f %f ", convex[s-2][0], convex[s-2][1]);
                            //println("%f %f ", convex[s-1][0], convex[s-1][1]);
                            convex[s-3] = intersect(convex[s-4], convex[s-3], convex[s-2], convex[s-1]);;
                            //println("->%f %f ", convex[s-3][0], convex[s-3][1]);
                            convex.erase(convex.begin()+(s-2));
                            upper_hull_size--;
                        }

                        if (lower_hull_size >= 5)
                        {
                            //println("lower cut");
                            // merge first 4 points -> 3 points
                            convex[1] = intersect(convex[0], convex[1], convex[2], convex[3]);;
                            convex.erase(convex.begin()+2);
                            lower_hull_size --;
                        }

                    }
#ifdef DEBUG_CONVEX
                    printf("convex:\n");
                    for (i=0; i<convex.size(); i++)
                        println("%f %f", convex[i][0], convex[i][1]);
                    for (i=0; i<=sampling; i++)
                        println("%f", flowfieldAry[i][id][dim]);
#endif
                    // convex: <lower> <upper>
                    vector<float> valueLowAry(sampling+1), valueHighAry(sampling+1);
                    for (i=1; i<convex.size(); i++)
                    {
                        for (int k=convex[i-1][0]; k<=convex[i][0]; k++)
                        {
                            VECTOR2 &left = convex[i-1];
                            VECTOR2 &right = convex[i];
                            float y = left[1]+(k-left[0])/(right[0]-left[0])*(right[1]-left[1]);
                            valueLowAry[k] =  y ;
                        }
                        if (convex[i][0]==sampling)
                            break;
                    }
                    for (i--; i<convex.size(); i++)
                    {
                        for (int k=convex[i-1][0]; k>=convex[i][0]; k--)
                        {
                            VECTOR2 &left = convex[i-1];
                            VECTOR2 &right = convex[i];
                            float y = left[1]+(k-left[0])/(right[0]-left[0])*(right[1]-left[1]);
                            valueHighAry[k] = y;
                        }
                    }

#ifdef DEBUG_CONVEX
                    printf("reconstruct low:\n");
                    for (i=0; i<=sampling; i++)
                        println("%f", valueLowAry[i]);
                    printf("reconstruct high:\n");
                    for (i=0; i<=sampling; i++)
                        println("%f", valueHighAry[i]);
#endif
                    for (i=1; i<sampling; i++) {
                        float real_err = fabs(flowfieldAry[i][id][dim]-interpAry[i][dim]);
                        float approximate = fabs(valueHighAry[i]-valueLowAry[i]);
                        float ratio = real_err/approximate;
                        if (!isinf(ratio))
                            errProximateRatioAry.push_back(real_err/approximate);
                    }

                }
                errProximateRatioField[id] = getMean(errProximateRatioAry.begin(), errProximateRatioAry.end());
            }
    }


    string fname;
    fname = strprintf("meanProximateRatio%d-%d", sampling, start).c_str();
    FILE *fp = fopen( ("err/"+fname+".raw").c_str(), "wb");
    assert(fp);
    fwrite(&errProximateRatioField[0], sizeof(float), W*H*D, fp);
    fclose(fp);

    write_nrrd_3d( ("err/"+fname+".nhdr").c_str(), (fname+".raw").c_str(), W, H, D, "float");

}
Ejemplo n.º 21
0
static int
sourcepoly(			/* compute image polygon for source */
	int	sn,
	RREAL	sp[MAXVERT][2]
)
{
	static short	cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2},
					 {0,1,3,7,6,4},{0,1,5,7,6,2},
					 {0,2,6,7,5,1},{0,4,6,7,3,1},
					 {0,2,3,7,5,4},{1,5,4,6,2,3}};
	register SRCREC	*s = source + sn;
	FVECT	ap, ip;
	RREAL	pt[6][2];
	int	dir;
	register int	i, j;

	if (s->sflags & (SDISTANT|SFLAT)) {
		if (s->sflags & SDISTANT) {
			if (ourview.type == VT_PAR)
				return(0);	/* all or nothing case */
			if (s->srad >= 0.05)
				return(0);	/* should never be a problem */
		}
		if (s->sflags & SFLAT) {
			for (i = 0; i < 3; i++)
				ap[i] = s->sloc[i] - ourview.vp[i];
			if (DOT(ap, s->snorm) >= 0.)
				return(0);	/* source faces away */
		}
		for (j = 0; j < 4; j++) {	/* four corners */
			for (i = 0; i < 3; i++) {
				ap[i] = s->sloc[i];
				if ((j==1)|(j==2)) ap[i] += s->ss[SU][i];
				else ap[i] -= s->ss[SU][i];
				if ((j==2)|(j==3)) ap[i] += s->ss[SV][i];
				else ap[i] -= s->ss[SV][i];
				if (s->sflags & SDISTANT) {
					ap[i] *= 1. + ourview.vfore;
					ap[i] += ourview.vp[i];
				}
			}
			viewloc(ip, &ourview, ap);	/* find image point */
			if (ip[2] <= 0.)
				return(0);		/* in front of view */
			sp[j][0] = ip[0]; sp[j][1] = ip[1];
		}
		return(4);
	}
					/* identify furthest corner */
	for (i = 0; i < 3; i++)
		ap[i] = s->sloc[i] - ourview.vp[i];
	dir =	(DOT(ap,s->ss[SU])>0.) |
		(DOT(ap,s->ss[SV])>0.)<<1 |
		(DOT(ap,s->ss[SW])>0.)<<2 ;
					/* order vertices based on this */
	for (j = 0; j < 6; j++) {
		for (i = 0; i < 3; i++) {
			ap[i] = s->sloc[i];
			if (cubeord[dir][j] & 1) ap[i] += s->ss[SU][i];
			else ap[i] -= s->ss[SU][i];
			if (cubeord[dir][j] & 2) ap[i] += s->ss[SV][i];
			else ap[i] -= s->ss[SV][i];
			if (cubeord[dir][j] & 4) ap[i] += s->ss[SW][i];
			else ap[i] -= s->ss[SW][i];
		}
		viewloc(ip, &ourview, ap);	/* find image point */
		if (ip[2] <= 0.)
			return(0);		/* in front of view */
		pt[j][0] = ip[0]; pt[j][1] = ip[1];
	}
	return(convex_hull(pt, 6, sp));		/* make sure it's convex */
}
Ejemplo n.º 22
0
void Subproblem::SolveSubproblemConvexHull(int iteration, int index) {
    if (constraints_.size() < 1) {
        return;
    }

    //if (iteration == 6 && index == 715) {
    if (iteration == 2 && index == 715) {
        int y = 0;
        y++;
        cout << y;
    }

    // Eliminate all superfluous constraints.
    std::vector<Constraint> active_constraints;

    vector<Point> points;
    for (int p = 0;  p < constraints_.size(); ++p) {
        points.push_back(Point());
        points[p].x = (-1.0) * constraints_[p].coefficient_;
        points[p].y = (-1.0) * constraints_[p].price_;
        points[p].weight = constraints_[p].weight_;
        points[p].constraint_id = p;
        constraints_[p].is_active_ = false;
    }
    points.push_back(Point());
    points[constraints_.size()].x = 0.0;
    points[constraints_.size()].y = 0.0;
    points[constraints_.size()].weight = -1;
    points[constraints_.size()].constraint_id = -1;

    vector<Point> convex_hull_points = convex_hull(points);

    for (int p = 0;  p < convex_hull_points.size(); ++p) {
        if ((convex_hull_points[p].x != 0.0) || (convex_hull_points[p].y != 0.0)) {
            active_constraints.push_back(Constraint(convex_hull_points[p].y * (-1.0),
                                                    convex_hull_points[p].x * (-1.0),
                                                    convex_hull_points[p].x / convex_hull_points[p].y,
                                                    -1));
            constraints_[convex_hull_points[p].constraint_id].is_active_ = true;
        }
    }

    // Sort list of active constraints. Note that the current implementation creates a clone of the
    // constraints vector. This isn't necessary, should be fixed.
    std::sort(active_constraints.begin(), active_constraints.end(), compare_Constraint_by_weight());

    // Go through active constraints, create envelope points.
    envelope_points_.clear();
    // First create intersection with x axis
    envelope_points_.push_back(std::make_pair((active_constraints[0].price_ / active_constraints[0].coefficient_),
                               0.0));
    for (int k = 0; (k < (active_constraints.size() - 1)); ++k) {
        long double u_value = ((active_constraints[k].price_ - active_constraints[k + 1].price_) /
                               (active_constraints[k].coefficient_ - active_constraints[k + 1].coefficient_));
        envelope_points_.push_back(std::make_pair(u_value,
                                   (active_constraints[k].price_ -
                                    active_constraints[k].coefficient_ * u_value)));
    }
    // Last, create intersection with y axis
    envelope_points_.push_back(std::make_pair(0.0, active_constraints[active_constraints.size() - 1].price_));
    // Find the budget ranges where there is a change of basis.
    budget_cutoffs_.clear();
    // Start by addding 0 for convenience.
    budget_cutoffs_.push_back(0.0);
    for (int k = 0; k < envelope_points_.size() - 1; ++k) {
        if ((envelope_points_[k].first - envelope_points_[k + 1].first) > 0.00000000000001) {
            budget_cutoffs_.push_back((envelope_points_[k + 1].second - envelope_points_[k].second) /
                                      (envelope_points_[k].first - envelope_points_[k + 1].first));
        } else {
            budget_cutoffs_.push_back(budget_cutoffs_[k]);
        }
    }
    // Add +\infty for convenience.
    budget_cutoffs_.push_back(DBL_MAX);
    active_constraints.clear();
}
Ejemplo n.º 23
0
        void ModalityConvex::update(Image& image, PatchSet* patchSet, Rect bounds) {

            Ptr<PatchSet> patches = Ptr<PatchSet>(reliablePatchesFilter.empty() ? patchSet : patchSet->filter(*reliablePatchesFilter));

            if (patches->size() < 3) {
                //flush();
                return;
            }

            Mat temp = image.get_float_mask();

            temp.setTo(0);

            if (history.empty()) {
                history.create(image.height(), image.width(), CV_32F);
                history.setTo(0);
            }

            Point2f* points = new Point2f[patches->size()];
            Point2f* hull = NULL;

            Point2f offset = Point2f(image.width() / 2, image.height() / 2) - patchSet->mean_position();

            Point2f mean(0, 0);

            for (int i = 0; i < patches->size(); i++) {
                points[i] = patches->get_position(i) + offset;
            }

            int size = convex_hull(points, patches->size(), &hull);

            for (int i = 0; i < size; i++) {
                mean.x += hull[i].x;
                mean.y += hull[i].y;
            }

            mean.x /= size;
            mean.y /= size;

            Point* hulli = new Point[size];
            Point* hullei = new Point[size];

            for (int i = 0; i < size; i++) {
                hulli[i].x = (int) hull[i].x;
                hulli[i].y = (int) hull[i].y;
            }

            expand(hull, size, mean, margin);

            for (int i = 0; i < size; i++) {
                hullei[i].x = (int) hull[i].x;
                hullei[i].y = (int) hull[i].y;
            }

            fillConvexPoly(temp, hullei, size, Scalar(1 - margin_diminish));
            fillConvexPoly(temp, hulli, size, Scalar(1));

            delete [] points;
            free(hull);
            delete [] hulli;
            delete [] hullei;

            history = history * persistence + temp * (1.0f - persistence);


        }