#include "main.h"

//Maillage de subdivision

int dicho_sub_inf(double theta_inf)
{
	int ind1 = 0, ind2 = nb_sommets-1, inf_ind = 0, indice, ind_m;
	double valeur_test;
	
	while (ind1<ind2)
	{
		ind_m = (int)((ind1+ind2)/2.0);
		indice = liste_triee_x[ind_m];
		
		valeur_test = liste_triee_y[indice];			
		if (valeur_test==theta_inf)
			inf_ind = ind_m;
		else if (valeur_test>theta_inf)
			ind2 = ind_m-1;
		else if (valeur_test<theta_inf)
			ind1 = ind_m+1;
	}	
	while (ind2>1 && ind2<nb_sommets && liste_triee_y[liste_triee_x[ind2]]==liste_triee_y[liste_triee_x[ind2-1]])
		ind2 = ind2-1;
	if (inf_ind == 0)	inf_ind = ind2;
	
	return (int) inf_ind;
}
	
int dicho_sub_sup(double theta_sup)
{
	int ind1 = 0, ind2 = nb_sommets-1, sup_ind = 0, indice, ind_m;	
	double valeur_test;
		
	while (ind1<ind2)
	{
		ind_m = (int)((ind1+ind2)/2.0);
		indice = liste_triee_x[ind_m];

		valeur_test = liste_triee_y[indice];				
		if (valeur_test==theta_sup)
			sup_ind = ind_m;
		else if (valeur_test>theta_sup)
			ind2 = ind_m-1;
		else if (valeur_test<theta_sup)
			ind1 = ind_m+1;
	}
	while (ind1>0 && ind1<(nb_sommets-1) && liste_triee_y[liste_triee_x[ind1]]==liste_triee_y[liste_triee_x[ind1+1]])
		ind1 = ind1+1;
	if (sup_ind == 0)	sup_ind = ind1;
	
	return (int) sup_ind;
}


//Maillage ˇquiangulaire

int dicho_equi_inf(double theta_inf)
{
	int ind1 = 0, ind2 = nb_sommets-1, inf_ind = -10, ind_m;
	double valeur_test;
	
	while (ind1<ind2)
	{
		ind_m = (int)((ind1+ind2)/2.0);
		valeur_test = liste_triee_y[ind_m];			
		if (valeur_test==theta_inf)
			inf_ind = ind_m;
		else if (valeur_test>theta_inf)
			ind2 = ind_m-1;
		else if (valeur_test<theta_inf)
			ind1 = ind_m+1;
	}	
	while (ind2>1 && ind2<nb_sommets && liste_triee_y[ind2]==liste_triee_y[ind2-1])
		ind2 = ind2-1;
	if (inf_ind == -10)	inf_ind = ind2;
	
	return (int) inf_ind;
}

int dicho_equi_sup(double theta_sup)
{
	int ind1 = 0, ind2 = nb_sommets-1, sup_ind = -10, ind_m;	
	double valeur_test;
		
	while (ind1<ind2)
	{
		ind_m = (int)((ind1+ind2)/2.0);
		valeur_test = liste_triee_y[ind_m];				
		if (valeur_test==theta_sup)
			sup_ind = ind_m;
		else if (valeur_test>theta_sup)
			ind2 = ind_m-1;
		else if (valeur_test<theta_sup)
			ind1 = ind_m+1;
	}
	while (ind1>0 && ind1<(nb_sommets-1) && liste_triee_y[ind1]==liste_triee_y[ind1+1])
		ind1 = ind1+1;
	if (sup_ind == -10)	sup_ind = ind1;
	
	return (int) sup_ind;
}


//Maillage HEALPix

int dicho_healpix_inf(double theta_inf)
{
	int ind1 = 0, ind2 = nb_sommets-1, inf_ind = -10, ind_m;
	double valeur_test;
	
	while (ind1<ind2)
	{
		ind_m = (int)((ind1+ind2)/2.0);
		valeur_test = liste_triee_y[ind_m];			
		if (valeur_test==theta_inf)
			inf_ind = ind_m;
		else if (valeur_test>theta_inf)
			ind2 = ind_m-1;
		else if (valeur_test<theta_inf)
			ind1 = ind_m+1;
	}	
	while (ind2>1 && ind2<nb_sommets && liste_triee_y[ind2]==liste_triee_y[ind2-1])
		ind2 = ind2-1;
	if (inf_ind == -10)	inf_ind = ind2;
	
	return (int) inf_ind;
}

int dicho_healpix_sup(double theta_sup)
{
	int ind1 = 0, ind2 = nb_sommets-1, sup_ind = -10, ind_m;
	double valeur_test;
			
	while (ind1<ind2)
	{
		ind_m = (int)((ind1+ind2)/2.0);
		valeur_test = liste_triee_y[ind_m];				
		if (valeur_test==theta_sup)
			sup_ind = ind_m;
		else if (valeur_test>theta_sup)
			ind2 = ind_m-1;
		else if (valeur_test<theta_sup)
			ind1 = ind_m+1;
	}
	while (ind1>0 && ind1<(nb_sommets-1) && liste_triee_y[ind1]==liste_triee_y[ind1+1])
		ind1 = ind1+1;
	if (sup_ind == -10)	sup_ind = ind1;
	
	return (int) sup_ind;
}
