% -------  New function --------------

function ddata = D12data(e)
global all_edges;

edge = all_edges(e(1));

ddata = edge.data{mod4(e(2)+2)};
% -------  End of function --------------



% -------  New function --------------
 
function dnext = Dnext(e)

dnext = Symet(SymDnext(e));

% -------  End of function --------------



% -------  New function --------------
 
function dprev = Dprev(e)

dprev = Tor(TorLnext(e));
% -------  End of function --------------



% -------  New function --------------
 
function ldata = Ldata(e)
global all_edges;

edge = all_edges(e(1));

ldata = edge.data{mod4(e(2)+3)};
% -------  End of function --------------



% -------  New function --------------
 
function lnext = Lnext(e)

lnext = Rot(TorLnext(e));
% -------  End of function --------------



% -------  New function --------------
 
function lprev = Lprev(e)

lprev = Symet(Onext(e));
% -------  End of function --------------



% -------  New function --------------
 
function mark = Mark(e)
global all_edges;

mark = all_edges(e(1)).mark;
% -------  End of function --------------



% -------  New function --------------
 
function odata = Odata(e)
global all_edges;

edge = all_edges(e(1));

odata = edge.data{e(2)};
% -------  End of function --------------



% -------  New function --------------
 
function eOnext = Onext(edge_ref)
global all_edges;

e = all_edges( edge_ref(1) );

eOnext = e.next{edge_ref(2)};


% -------  End of function --------------



% -------  New function --------------
 
function oprev = Oprev(e)

oprev = Rot(RotRnext(e));
% -------  End of function --------------



% -------  New function --------------
 
function rdata = Rdata(e)
global all_edges;

edge = all_edges(e(1));

rdata = edge.data{mod4(e(2)+1)};
% -------  End of function --------------



% -------  New function --------------
 
function rnext = Rnext(e)

rnext = Tor(RotRnext(e));
% -------  End of function --------------



% -------  New function --------------
 
function eRot = Rot(edge_ref)
% Pair (e,r) where a edge is e.Rot^r

eRot(1) = edge_ref(1); 
eRot(2) = mod4(edge_ref(2) + 1);

% -------  End of function --------------



% -------  New function --------------
 
function rotrnext = RotRnext(edge_ref)
global all_edges;

e = all_edges( edge_ref(1) );

rotrnext = e.next{mod4(edge_ref(2)+1)};


% -------  End of function --------------



% -------  New function --------------
 
function rprev = Rprev(e)

rprev = SymDnext(e);
% -------  End of function --------------



% -------  New function --------------
 
function symdnext = SymDnext(edge_ref)
global all_edges;

e = all_edges( edge_ref(1) );

symdnext = e.next{ mod4(edge_ref(2)+2) };


% -------  End of function --------------



% -------  New function --------------
 
function [eSym] = Symet(edge_ref)

eSym(1) = edge_ref(1);
eSym(2) = mod4(edge_ref(2)+2);

% -------  End of function --------------



% -------  New function --------------
 
function [eTor] = Tor(edge_ref)

eTor(1) = edge_ref(1);
eTor(2) = mod4(edge_ref(2)+3);

% -------  End of function --------------



% -------  New function --------------
 
function torlnext = TorLnext(edge_ref)
global all_edges;
e = all_edges( edge_ref(1) );

torlnext = e.next{ mod4(edge_ref(2)+3) };


% -------  End of function --------------



% -------  New function --------------
 
function destroy_edge(e)
global all_edges;

f = Symet(e);

if(any(Onext(e) ~= e))
   splice(e, Oprev(e));
end
if(any(Onext(f) ~= f))
   splice(f, Oprev(f));
end

set_Destroyed(e);
%void destroy_edge(edge_ref e)
%  {
%    edge_ref f = SYM(e);
%    if (ONEXT(e) != e) splice(e, OPREV(e));
%    if (ONEXT(f) != f) splice(f, OPREV(f));  
%    free((char *) ((e) & 0xfffffffcu));
%  }

% -------  End of function --------------



% -------  New function --------------
 
function [res] = is_Active(e)
global all_edges;

res = abs(all_edges(e(1)).destroyed - 1);
% -------  End of function --------------



% -------  New function --------------
 
function e = make_edge
global all_edges;

ref = length(all_edges)+1;

e = [ref 1];

set_Onext(e,e);
set_SymDnext(e,Symet(e));
set_RotRnext(e,Tor(e));
set_TorLnext(e,Rot(e));
set_Mark(e,0);

set_Active(e);
%edge_ref make_edge(void)
%  {
%    edge_ref e;
%
%    e = (edge_ref) malloc(sizeof(edge_struct));
%    ONEXT(e) = e;
%    SYMDNEXT(e) = SYM(e);
%    ROTRNEXT(e) = TOR(e);
%    TORLNEXT(e) = ROT(e);
%    MARK(e) = 0;
%    return e;
%  }


% -------  End of function --------------



% -------  New function --------------
 
function res = mod4(n)

res = mod(n,4);
if(res==0)
   res=4;
end;

% -------  End of function --------------



% -------  New function --------------
 
function [closure] = quad_do_enum(e,visit_proc,mark)
%global closure;
global saida;
closure=[];

mk_e = Mark(e);
while((mk_e ~= mark))
   %eval([visit_proc '(e,closure);']); % execute visit_proc(e,closure)
   %visit_proc(e);
   
   o = Org(e);
	d = Dest(e);
	x = [o.x,d.x];
	y = [o.y,d.y];
   %z = [o.z,d.z];
   
	aresta.x = x;
	aresta.y = y;

	%saida(length(saida)+1) = aresta
   
   if(is_Active(e))
		plot(x,y,'b*-');
      hold on;
   else
      break;
   end;
   
   
   set_Mark(e,mark);
   [closure] = quad_do_enum(Onext(Symet(e)),visit_proc,mark);
   e = Onext(e);
   mk_e = Mark(e);
end;


%void quad_do_enum (
%    edge_ref e, 
%    void visit_proc(edge_ref e, void *closure), 
%    void *closure,
%    unsigned mark
%  )
%  {
%    while (MARK(e) != mark)
%      {
%        visit_proc(e, closure);
%        MARK(e) = mark;
%        quad_do_enum (ONEXT(SYM(e)), visit_proc, closure, mark);
%        e = ONEXT(e);
%      }
%  }

% -------  End of function --------------



% -------  New function --------------
 
function [closure] = quad_enum(a,visit_proc)
% next_mark has to begin being 1
global next_mark;
%global closure;

next_mark=1;

mark = next_mark;
next_mark = next_mark +1;


if(next_mark == 0)
   next_mark = 1;
end;

[closure] = quad_do_enum(a,visit_proc,mark);


%void quad_enum(
%    edge_ref a, 
%    void visit_proc(edge_ref e, void *closure), 
%    void *closure
%  )
%  {
%    unsigned mark = next_mark;
%    next_mark++;
%    if (next_mark == 0) next_mark = 1;
%    quad_do_enum(a, visit_proc, closure, mark); 
%  }

% -------  End of function --------------



% -------  New function --------------
 
function set_Active(e)
global all_edges;

all_edges(e(1)).destroyed = 0;
% -------  End of function --------------



% -------  New function --------------
 
function set_Ddata(e,value)
global all_edges;

all_edges(e(1)).data{mod4(e(2)+2)} = value;
% -------  End of function --------------



% -------  New function --------------
 
function set_Destroyed(e)
global all_edges;

all_edges(e(1)).destroyed = 1;
% -------  End of function --------------



% -------  New function --------------
 
function set_Ldata(e,value)
global all_edges;

all_edges(e(1)).data{mod4(e(2)+3)} = value;
% -------  End of function --------------



% -------  New function --------------
 
function set_Mark(e,value)
global all_edges;

all_edges(e(1)).mark = value;
% -------  End of function --------------



% -------  New function --------------
 
function set_Odata(e,value)
global all_edges;

all_edges(e(1)).data{e(2)} = value;
% -------  End of function --------------



% -------  New function --------------
 
function set_eOnext(e,value)
global all_edges;

all_edges( e(1) ).next{e(2)} = value;

% -------  End of function --------------



% -------  New function --------------
 
function set_Rdata(e,value)
global all_edges;

all_edges(e(1)).data{mod4(e(2)+1)} = value;
% -------  End of function --------------



% -------  New function --------------
 
function set_RotRnext(edge_ref,value)
global all_edges;

all_edges( edge_ref(1) ).next{mod4(edge_ref(2)+1)} = value;


% -------  End of function --------------



% -------  New function --------------
 
function set_SymDnext(edge_ref,value)
global all_edges;

all_edges( edge_ref(1) ).next{mod4(edge_ref(2)+2)} = value;


% -------  End of function --------------



% -------  New function --------------
 
function set_TorLnext(edge_ref,value)
global all_edges;

all_edges( edge_ref(1) ).next{ mod4(edge_ref(2)+3) } = value;


% -------  End of function --------------



% -------  New function --------------
 
function splice(a,b)

alpha = Rot(Onext(a));
beta = Rot(Onext(b));

ta = Onext(a);
tb = Onext(b);

set_Onext(a,tb);
set_Onext(b,ta);

ta = Onext(alpha);
tb = Onext(beta);

set_Onext(alpha,tb);
set_Onext(beta,ta);


%void splice(edge_ref a, edge_ref b)
%  {
%    edge_ref ta, tb;
%    edge_ref alpha = ROT(ONEXT(a));
%    edge_ref beta = ROT(ONEXT(b));

%    ta = ONEXT(a);
%    tb = ONEXT(b);
%    ONEXT(a) = tb;
%    ONEXT(b) = ta;
%    ta = ONEXT(alpha);
%    tb = ONEXT(beta);
%    ONEXT(alpha) = tb;
%    ONEXT(beta) = ta;    
%  }

% -------  End of function --------------



% -------  New function --------------
 
