%
%    This file is meant to be used together with hf.m
%
%    It computes the Maslov index for the domain between the generators pone and ptwo
%    such that the multiplicity of the basepoint is equal to the variable power.	
%
%    To compute the index, we use Corollary 4.3 in R. Lipshitz, 
%    "A cylindrical reformulation of Heegaard Floer homology"
%
%    When power = 0, this gives the difference in gradings between pone and ptwo.
%
%    Ciprian Manolescu - May 18, 2005
%
%

loop;

Corr = round (Corr);


LG = LG - (BG' * Corr);

% the new LG describes how many copies of an alpha curve need to be added
% Corr describes how many copies of a beta curves need to be added

% assign multiplicities to each edge; the result is the array Mult

Mult = zeros(nume, 1);
for i=1:g
	for j=1:(length(Path(i).beta) - 1)
		k=Path(i).betaind(j);
		Mult(EE{2,i}(k)) = -1;
	end
	for j=1:Sizeofbeta(i)
		Mult(EE{2,i}(j)) =  Mult(EE{2,i}(j)) + Corr (i);
	end
	for j=1:(length(Path(i).alpha) - 1)
                k=Path(i).alphaind(j);
                Mult(EE{1,i}(k)) = 1;
        end
        for j=1:Alpha(i)
                Mult(EE{1,i}(j)) =  Mult(EE{1,i}(j)) + LG (i);
        end
end




% assign multiplicities to each domain; the result is the array MD
% Counted is 1 initially for all domains; it becomes 0 when the domain is
% assigned a multiplicity

% MDred is MD with the zeros removed

MD = zeros (numd, 1);
Counted = ones (numd, 1);
MDred = [];

MD(basept) = power;
Counted (basept) = 0;
if (power > 0)
	MDred = [basept, power];
end

while (any(Counted))
	% for each uncounted domain, try to assign a multiplicity
	for i=1:numd
		if Counted(i) == 1
			% check the neighbors
			for j=1:Typedom(i)
				aedg = abs(Dom{i}(j));
				semn = sign(Dom{i}(j));
				if ((semn == 1) & (Counted(ED(aedg,2)) == 0))
					Counted(i) = 0;
					MD (i) = MD(ED(aedg,2)) + Mult(aedg);
				elseif ((semn == -1) & (Counted(ED(aedg,1)) == 0))	
					Counted(i) = 0;
					MD (i) = MD(ED(aedg,1)) - Mult(aedg);
				end
				if (Counted(i) == 0)
					if ((MD(i)) ~= 0) 
						MDred = [MDred; i, MD(i)];		
					end
					break
				end
			end
		end
	end
end

% sort MDred nicely

if (size(MDred) > 0)
	[Whocares, ix] = sort(MDred(:,1));
	iy = ix(:, 1);
	MDred(:, :) = MDred(iy(:), :);
end

% calculate the Euler measure euler of the domain, and the quantities n 

euler = 0;
triv = 0;
n=0;
N = zeros (numv, 1);
for i=1:size(MDred)
	domain = MDred(i,1);
	multipl = MDred(i,2);
	% contribution from the euler class
	euler = euler + ((1 - (Typedom(domain))/4) * multipl);
	for j=1:Typedom(domain)
		verte=Dov{domain}(j);
		% contribution from the endpoint x	
		if any(G(pone, :) == verte)
			n = n + (multipl/4);
			N (verte) = N(verte) + 1;
		end
		% contribution from the endpoint y
		if any(G(ptwo, :) == verte)
                        n = n + (multipl/4);
			N (verte) = N(verte) + 1;
                end
	end
end

% the Maslov index
masl = n + euler;

% the number triv of trivial disks
triv = 0;
for i=1:g
	if ((G(pone, i) == G(ptwo, i)) & (N(G(pone, i)) == 0))
		triv = triv + 1;
	end
end 

% the number of cycles in the permutation corresponding to (pone, ptwo)

permone = V(G(pone, :), 3);
permtwo = V(G(ptwo, :), 3);

for i=1:g
	permtwoinv(permtwo(i)) = i;
end

for i=1:g
	permnew (i) = permtwoinv(permone(i));
end

cycles = 0;
marked = zeros (1,g);  
for i=1:g
	if (marked(i) == 0)
		cycles = cycles +1;
		j=i;
		while 1
			marked (j) =1;	
			j = permnew (j);
			if (j == i) break, end
		end
	end
end

% the number of boundary components of the connected surface S, if it exists

cycles = cycles - triv;

% the genus of S

genus = (2 - cycles + triv - euler + n - g)/2;
 
