Esempio n. 1
0
main()
{
    ginit(500, 500, WHITE);
    GRAPH g;
    g.window(0, -N, 2 * N, N);
    g.line(0, 0, 2 * N, 0);
    g.line(0, N, 0, -N);
    double x, v, f, h, t, tend;
    int step;
    
    x = 1.0;
    v = 0.0;
    f = 0;
    tend = 100;
    h = 0.1;
    
    step = (int)(tend / h);
    
    for(int i = 0; i < step; i++)
    {
        euler(x, v, f, h);
        g.pset(t, x);
        t += h;
    }
    gend();
}
Esempio n. 2
0
main()
{
   int i, j;
   double t, p, x, y, z, r, R, N;
   ginit(500, 500, WHITE);
   GRAPH g;
   g.window(-PI, -PI, PI, PI);
   g.line(-PI, 0, PI, 0);  // x
   g.line(0, PI, 0, -PI);  // y
   g.setcolor(BLACK);
   r = 1.0;
   R = 2.0;
   i = 0;
   while(i < NUMBER)
   {
       t = i * (PI / (NUMBER / 2));
       j = 0;
       while(j < NUMBER)
       {
           p = j * (PI / (NUMBER / 2));
           
           x = R * cos(t) + r * cos(p) * cos(t); //External reference Wikipedia
           y = R * sin(t) + r * cos(p) * sin(t);
           z = r * sin(p);
           
           g.pset(x, y);
           j = j + 1;
       }
       i = i + 1;
   }
   gend();
}
Esempio n. 3
0
bool compute_point_in_convex_polyhedron(const GRAPH<POINT,int>& G, POINT& ins)
{
  // find four non-coplanar vertices ...
  // then compute a point inside the tetrahedron 
  // defined by these points ...
  
  POINT a,b,c,d;
  
  if (G.number_of_nodes() < 4) return false;
  
  const list<node>& vert = G.all_nodes();
  node v;
  int state = 0;
  
  forall(v,vert){
    switch(state){
      case 0: {
        a = G[v];
	state++;
        break;
      }
      case 1: {
        if (a != G[v]){
	  b = G[v];
	  state++;
	}
        break;
      }
      case 2: {
        // 3 points must be non-collinear ...
	if (! collinear(a,b,G[v])){
	  c = G[v];
	  state++;
	}
	break;
      }
      case 3: {
        // 4 points must be non-coplanar ...
	if (! coplanar(a,b,c,G[v])){
	  d = G[v];
	  state++;
	}	
	break;
      }
    }
    
    if (state == 4) break;
  }
  
  if (state == 4) { // we found 4 non-coplanar vertices ...
    ins = midpoint(d, midpoint(c,midpoint(a,b)));
  
    return true;
  }

  return false;
}
Esempio n. 4
0
/*
Dump dom set, pdom set, idom, ipdom.
'dump_dom_tree': set to be true to dump dominate 
	tree, and post dominate Tree.
*/
void DGRAPH::dump_dom(FILE * h, bool dump_dom_tree)
{
	if (h == NULL) return;
	fprintf(h, "\n\n\n\n========= DUMP DOM/PDOM/IDOM/IPDOM =======");
	INT c;
	for (VERTEX * v = m_vertexs.get_first(c); 
		 v != NULL; v = m_vertexs.get_next(c)) {
		INT vid = VERTEX_id(v);
		fprintf(h, "\nVERTEX(%d) dom: ", vid);
		BITSET * bs;
		if ((bs = m_dom_set.get(vid)) != NULL) {
			for (INT id = bs->get_first(); id != -1 ; id = bs->get_next(id)) {
				if (id != vid) {
					fprintf(h, "%d ", id);
				}
			}
		}
		fprintf(h, "\n     pdom: ");
		if ((bs = m_pdom_set.get(vid)) != NULL) {
			for (INT id = bs->get_first(); id != -1; id = bs->get_next(id)) {
				if (id != vid) {
					fprintf(h, "%d ", id);
				}
			}
		}
		if (m_idom_set.get(vid) != 0) {
			fprintf(h, "\n     idom: %d", m_idom_set.get(vid));
		} else {
			fprintf(h, "\n");
		}

		if (m_ipdom_set.get(vid) != 0) {
			fprintf(h, "\n     ipdom: %d", m_ipdom_set.get(vid));
		} else {
			fprintf(h, "\n");
		}
	} //end for each vertexs
	fprintf(h, "\n");
	fflush(h);

	if (dump_dom_tree) {
		GRAPH dom;
		get_dom_tree(dom);
		dom.dump_vcg("graph_dom_tree.vcg");
		dom.erasure();
		get_pdom_tree(dom);
		dom.dump_vcg("graph_pdom_tree.vcg");
	}
}
Esempio n. 5
0
bool GRAPH::is_equal(GRAPH & g)
{
	if (get_vertex_num() != g.get_vertex_num() ||
		get_edge_num() != g.get_edge_num()) {
		return false;
	}
	
	BITSET vs;
	INT c;
	for (VERTEX * v1 = get_first_vertex(c); 
		 v1 != NULL; v1 = get_next_vertex(c)) {
		VERTEX * v2 = g.get_vertex(VERTEX_id(v1));
		if (v2 == NULL) {
			return false; 
		}

		vs.clean();
		EDGE_C * el = VERTEX_out_list(v1);		
		EDGE * e = NULL;
		UINT v1_succ_n = 0;
		if (el == NULL) {
			if (VERTEX_out_list(v2) != NULL) {
				return false;
			}
			continue;
		}
		for (e = EC_edge(el); e != NULL; el = EC_next(el), 
			 e = el ? EC_edge(el) : NULL) {
			vs.bunion(VERTEX_id(EDGE_to(e)));
			v1_succ_n++;
		}

		UINT v2_succ_n = 0;
		el = VERTEX_out_list(v2);
		for (e = EC_edge(el); e != NULL; el = EC_next(el), 
			 e = el ? EC_edge(el) : NULL) {
			v2_succ_n++;
			if (!vs.is_contain(VERTEX_id(EDGE_to(e)))) {
				return false;
			}			
		}
		
		if (v1_succ_n != v2_succ_n) { 
			return false; 
		}
	}
	return true;
}
Esempio n. 6
0
        void operator()(const GRAPH& _graph, STRATEGY _strategy, OUTPUT& _out)
        {
          typedef typename GRAPH::slice_type slice_type; 
          for_each(_graph,[&](const slice_type& _slice)
          {
            using namespace gex;
            using gex::base::expand;
            auto& _holes = _slice.polygon().holes();

            Scalar _middle = _slice.range().middle();
            Axis _axis = _slice.axis();

            for (auto& _hole : _holes)
            {
              // Calc center point of slice polygon
              auto&& _a = expand(algorithm::centroid(_hole),_axis,_middle);
              auto&& _upperSlices = _graph.indexesToPointers(_slice.upper());
              for (auto& _connSlice : _upperSlices)
              {
                auto& _upperHoles = _connSlice->polygon().holes();
                Scalar _upperMiddle = _connSlice->range().middle();
                
                for (auto& _upperHole : _upperHoles)
                {
                  if (!boost::geometry::intersects(_hole,_upperHole)) continue;

                  auto&& _centroid = expand(algorithm::centroid(_upperHole),_axis,_upperMiddle);
                  _out.emplace_back(_a,_centroid);
                }
              }
            }
          });
        }
Esempio n. 7
0
bool GRAPH::clone(GRAPH & src)
{
	erasure();
	m_is_unique = src.m_is_unique; 
	m_is_direction = src.m_is_direction; 

	//Clone vertexs
	INT c;
	for (VERTEX * srcv = src.get_first_vertex(c); 
		 srcv != NULL; srcv = src.get_next_vertex(c)) {
		VERTEX * v = add_vertex(VERTEX_id(srcv));

		/*
		Calls inherited class method.
		Vertex info of memory should allocated by inherited class method
		*/
		if (VERTEX_info(srcv) != NULL) {
			VERTEX_info(v) = clone_vertex_info(srcv);
		}
	}

	//Clone edges
	for (EDGE * srce = src.get_first_edge(c); 
		 srce != NULL; srce = src.get_next_edge(c)) {
		EDGE * e = add_edge(VERTEX_id(EDGE_from(srce)), 
							VERTEX_id(EDGE_to(srce)));

		/*Calls inherited class method.
		 *Edge info of memory should allocated by inherited class method
		 * */
		if (EDGE_info(srce) != NULL) {
			EDGE_info(e) = clone_edge_info(srce);
		}
	}
	return true;
}
Esempio n. 8
0
 void operator()(const GRAPH& _graph, STRATEGY _strategy, OUTPUT& _out)
 {
   typedef typename GRAPH::slice_type slice_type; 
   for_each(_graph,[&](const slice_type& _slice)
   {
     // Calc center point of slice polygon
     auto&& _a = centroid(_slice); 
     auto&& _upperSlices = _graph.indexesToPointers(_slice.upper());
     auto&& _lowerSlices = _graph.indexesToPointers(_slice.lower());
     for (auto& _connSlice : _upperSlices)
     {
       _out.emplace_back(_a,centroid(*_connSlice));
     }
     for (auto& _connSlice : _lowerSlices)
     {
       _out.emplace_back(_a,centroid(*_connSlice));
     }
   });
 }
Esempio n. 9
0
ABS_NODE * CFS_MGR::construct_abs_tree(
						IN IR_BB * entry,
						IN ABS_NODE * parent,
						IN BITSET * cur_region,
						IN GRAPH & cur_graph,
						IN OUT BITSET & visited)
{
	IR_CFG * cfg = m_ru->get_cfg();
	ABS_NODE * lst = NULL;
	IR_BB * bb = entry;
	GRAPH g;
	g.clone(cur_graph);
	VERTEX * next = NULL;
	VERTEX * v;
	if (cur_region != NULL) {
		if (cur_region->get_elem_count() == 0) {
			visited.clean();
			return NULL;
		}
		INT c;
		for (v = g.get_first_vertex(c); v != NULL; v = next) {
			next = g.get_next_vertex(c);
			if (cur_region->is_contain(VERTEX_id(v))) {
				continue;
			}
			g.remove_vertex(v);
		}
	}
	BITSET loc_visited;
	while (bb != NULL &&
		   (cur_region == NULL ||
		    cur_region->is_contain(IR_BB_id(bb)))) {
		ABS_NODE * node = NULL;
		loc_visited.clean();
		LI<IR_BB> * li = cfg->map_bb2li(bb);
		if (li != NULL) {
			node = construct_abs_loop(bb, parent, LI_bb_set(li),
									  g, loc_visited);
		} else {
			IR * last_xr = cfg->get_last_xr(bb);
			if (last_xr != NULL && //'bb' is branching node of IF.
				last_xr->is_cond_br()) {
				IS_TRUE0(map_ir2cfsinfo(last_xr) != NULL);

				/* There might not exist ipdom.
				e.g:
					if (x) //BB1
						return 1;
					return 2;

					BB1 does not have a ipdom.
				*/
				UINT ipdom = ((DGRAPH*)cfg)->get_ipdom(IR_BB_id(bb));
				IS_TRUE(ipdom > 0, ("bb does not have ipdom"));
				node = construct_abs_if(bb, parent, g, loc_visited);
			} else {
				node = construct_abs_bb(bb, parent);
				loc_visited.bunion(IR_BB_id(bb));
			}
		}
		insertbefore_one(&lst, lst, node);

		visited.bunion(loc_visited);
		//Remove visited vertex.
		next = NULL;
		INT c;
		for (v = g.get_first_vertex(c); v != NULL; v = next) {
			next = g.get_next_vertex(c);
			if (!loc_visited.is_contain(VERTEX_id(v))) {
				continue;
			}
			g.remove_vertex(v);
		}

		IR_BB * cand = NULL;
		for (v = g.get_first_vertex(c); v != NULL; v = g.get_next_vertex(c)) {
			if (g.get_in_degree(v) == 0) {
				IS_TRUE(cand == NULL, ("multiple immediate-post-dominators"));
				cand = cfg->get_bb(VERTEX_id(v));
			}
		}

		if (cand == NULL) {
			//Cannot find leading BB, there might be exist cycle in graph.
			bb = cfg->get_ipdom(bb);
		} else {
			bb = cand;
		}

		if (parent != NULL && bb == ABS_NODE_bb(parent)) {
			//Here control-flow is cyclic.
			break;
		}
	}
	lst = reverse_list(lst);
	return lst;
}
//--------------------------------------------------------
//update graph "G" when delete edge "te" from graph
void change_graph_as_del_edge(leda_edge& te, GRAPH<int, int>& G)
{
     leda_node source_node=G.source(te);
     leda_node target_node=G.target(te);
     leda_edge e;
             
     //connect source node with sink if the node's outdegree ==1
     if(G.outdeg(source_node)==1){
         e=G.new_edge(source_node, G.last_node());
         G.assign(e, G[te]);
     }  

     //connect target node with source if the node's indegree ==1              
     if(G.indeg(target_node)==1){
         e=G.new_edge(G.first_node(), target_node);
         G.assign(e, 0);
     } 

     //delete original edge
     G.del_edge(te);
}
Esempio n. 11
0
//#####################################################################
// Function Add_Horizontal_Edges
//#####################################################################
template<class T_GRID> template<class T_FACE> void LAPLACE_RLE<T_GRID>::
Add_Horizontal_Edges::Apply(const LAPLACE_RLE<T_GRID>& laplace,GRAPH& graph)
{
    for(T_FACE face(laplace.grid,0);face;face++)if(!laplace.psi_N(face.Face())){int c1=face.cell1.Cell(),c2=face.cell2.Cell();
        for(int i1=0;i1<=(int)face.cell1.Long();i1++)for(int i2=0;i2<=(int)face.cell2.Long();i2++)graph.Add_Undirected_Edge(c1+i1,c2+i2);}
}
void Hv_Matrix::update_matrix(int i, int j, char x, 
                              GRAPH<int, int>& G, leda_edge& e)
{
   leda_edge old_e;
   if(i < j)
   { 
      if(x=='H')
      {   
          if(triangle_matrix[i][j-i-1].direction!=H){
            //delete old edge in the graph
            old_e=triangle_matrix[i][j-i-1].ed;
            G.del_edge(old_e);
 
            //update matrix
            triangle_matrix[i][j-i-1].direction=H;
            triangle_matrix[i][j-i-1].ed=e;
          }
          else{
            cout<<"update matrix error of h edge <"<<i<<"," 
                <<j<<">"<<endl;
            exit(0);
          }
      }
      else if(x=='V')
      {   
          if(triangle_matrix[i][j-i-1].direction!=V)
          {
            //delete old edge in the graph
            old_e=triangle_matrix[i][j-i-1].ed;
            G.del_edge(old_e);
 
            //update matrix
            triangle_matrix[i][j-i-1].direction=V;
            triangle_matrix[i][j-i-1].ed=e;
          }
          else{
            cout<<"update matrix error of v edge <"<<i<<"," 
                <<j<<">"<<endl;
            exit(0);
          }
      }
      else
         error_message("update matrix error (not H or V)!!!");        
   }
   else
   {
      if(x=='H')
      {
          if(triangle_matrix[j][i-j-1].direction!=h)
          {
            //delete old edge in the graph
            old_e=triangle_matrix[j][i-j-1].ed;
            G.del_edge(old_e);
 
            //update matrix
            triangle_matrix[j][i-j-1].ed=e;
            triangle_matrix[j][i-j-1].direction=h;
          }
          else{
            cout<<"update matrix error of h edge <"<<j<<"," 
                <<i<<">"<<endl;
            exit(0);
          }
      }
      else if(x=='V')
      {
          if(triangle_matrix[j][i-j-1].direction!=v)
          {
            //delete old edge in the graph
            old_e=triangle_matrix[j][i-j-1].ed;
            G.del_edge(old_e);
 
            //update matrix  
            triangle_matrix[j][i-j-1].direction=v; 
            triangle_matrix[j][i-j-1].ed=e;
          }
          else{
            cout<<"update matrix error of v edge <"<<j<<"," 
                <<i<<">"<<endl;
            exit(0);
          }
      }
      else
         error_message("update matrix error (not H or V)!!!");        
   }
}