
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                        initialisation de l'interface                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = texture(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @texture_OpeningFcn, ...
                   'gui_OutputFcn',  @texture_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end


function texture_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
%initialisation de la colormap 
colormap(pink);

function varargout = texture_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      fonction de generation de textures                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function generer_Callback(hObject, eventdata, handles)
generer(handles);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                 Zoom avant                                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function zoom_avant_Callback(hObject, eventdata, handles)
zoom_avant(handles);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                 Zoom arriere                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function zoom_arriere_Callback(hObject, eventdata, handles)
zoom_arriere;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              la boite de message                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function messagebox_CreateFcn(hObject, eventdata, handles)
global HandleBox Nbligne
HandleBox=hObject;
Nbligne=100;

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

data=cell(Nbligne,1);
set(hObject,'string',data);
set(hObject,'ListBoxTop',Nbligne);
set(hObject,'Value',Nbligne);

function messagebox_Callback(hObject, eventdata, handles)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         Menu selection / insertion                          %                           
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function selection_insertion_Callback(hObject, eventdata, handles)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                    selectionner une partie d'une texture                    %                           
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function selectionner_image_Callback(hObject, eventdata, handles)
selection1(handles);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     insertion d'une image dans la figure                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function inserer_Image_Callback(hObject, eventdata, handles)
insertion1(handles);
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     selectionner une partie d'une texture                   %                           
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function selectionner2_Callback(hObject, eventdata, handles)
selection2(handles);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     insertion d'une image dans la figure                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function inserer2_Callback(hObject, eventdata, handles)
insertion2(handles);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                le menu option                               %                                                      
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function options_Callback(hObject, eventdata, handles)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                 sauvegarde de l'image dans differents formats               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Sous_Menu sauver l'image
function Sauve_image_Callback(hObject, eventdata, handles)

% En bmp
function sauver_bmp_Callback(hObject, eventdata, handles)
global NG
[filename,pathname ]= uiputfile( ...
                               {'*.bmp', 'fichier Bitmap (*.bmp)'; ...
                                '*.*',   'tous les fichiers (*.*)'}, ...
                                'Enregistrer sous');
if ~isequal(filename, 0)
  chemin=sprintf('%s%s.bmp',pathname,filename);
  % Appel de la foncion colorer permettant de crée une matrice conservant
  % la colormap utilisé
  f=colorer(NG);
  imwrite(f,chemin,'bmp');
  clear f;
end   


% En eps
function sauver_eps_Callback(hObject, eventdata, handles)

[filename,pathname ]= uiputfile( ...
                               {'*.ps', 'fichier postscipt (*.ps)'; ...
                                '*.*',   'tous les fichiers (*.*)'}, ...
                                'Enregistrer sous');
if ~isequal(filename, 0)
  chemin=sprintf('%s%s.ps',pathname,filename);
  %print(handles.axes1,'-depsc2',chemin);
  %print -fgca -dps 'Figure2.ps'
  print(gcf,'-depsc2',chemin); 
end   


% En jpg
function sauver_jpg_Callback(hObject, eventdata, handles)
global NG
[filename,pathname ]= uiputfile( ...
                               {'*.jpg', 'fichier Bitmap (*.jpg)'; ...
                                '*.*',   'tous les fichiers (*.*)'}, ...
                                'Enregistrer sous');
if ~isequal(filename, 0)
  chemin=sprintf('%s%s.jpg',pathname,filename);
  f=colorer(NG);
  imwrite(f,chemin,'jpg');
  clear f;
end   


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             sortie de l'interface                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function quitter_Callback(hObject, eventdata, handles)
clear Qr va zones x_max y_max r_min dx tailles choix_loi params motif angle polygone courbe_spline choix_mapping
clear  expression discretisation

if(exist('pile.dat','file'))
  delete pile.dat
end  
close(handles.figure1);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             Sauvegarde des donnees                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function sauve_donnee_Callback(hObject, eventdata, handles)

global Qr va zones x_max y_max r_min dx tailles choix_loi params motif angle polygone courbe_spline choix_mapping

[filename, pathname] = uiputfile( ...
      {'*.mat', 'fichier MATLAB (*.mat)'; ...
       '*.*',   'tous les fichiers (*.*)'}, ...
       'Sauver les données de la simulation  cours');
    
if ~isequal(filename,0),
   chemin=sprintf('%s%s',pathname,filename);
   save(chemin,'Qr','va','zones','x_max','y_max','r_min','dx','choix_loi','params',...
        'motif','angle','polygone','courbe_spline','choix_mapping');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%            Ouverture d'un fichier de donnees et restitution totale          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function Ouvrir_donnees_Callback(hObject, eventdata, handles)

global Qr va zones x_max y_max r_min dx tailles choix_loi params motif angle polygone courbe_spline choix_mapping


[filename,pathname ]= uigetfile( ...
                               {'*.mat', 'fichier MATLAB (*.mat)'; ...
                                '*.*',   'tous les fichiers (*.*)'}, ...
                                'Ouvrir dans');
        
if ~isequal(filename, 0)
   chemin=sprintf('%s%s',pathname,filename);
   load(chemin,'Qr','va','zones','x_max','y_max','r_min','dx','choix_loi','params',...
        'motif','angle','polygone','courbe_spline','choix_mapping');
   
   axes(handles.axes1);
   afficher(Qr);

   % Remise a jour des parametres
   set(handles.choix,'value',choix_loi); 
   set(handles.c1   ,'string',params(1));
   set(handles.c2   ,'string',params(2));
   set(handles.c3   ,'string',params(3));
   set(handles.motif,'value',motif);
   set(handles.rotation,'Value',angle);
   if get(handles.choix,'value')~=1,
      set(handles.c2,'visible','off');
      set(handles.text8,'visible','off');
   else
      set(handles.c2,'visible','on');
      set(handles.text8,'visible','on');
   end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                 Mapping (3d)                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function mappage_Callback(hObject, eventdata, handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Sphere  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sphere_Callback(hObject, eventdata, handles)
global choix_mapping Qr

choix_mapping=1;
afficher(Qr);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Cylindre  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cylindre_Callback(hObject, eventdata, handles)
global choix_mapping Qr

choix_mapping=2;
afficher(Qr);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Ellipsoide  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ellipsoide_Callback(hObject, eventdata, handles)
global choix_mapping Qr

choix_mapping=3;
afficher(Qr);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Plan (defaut)  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function defaut_Callback(hObject, eventdata, handles)
global choix_mapping Qr

choix_mapping=0;
afficher(Qr);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  3 dimensions  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function trois_dim_Callback(hObject, eventdata, handles)
global Qr choix_mapping

 messagebox('',...
            '**************************************************************************',...
            '* Selectionner le cadre que vous souhaitez visualiser en 3-D  *',...
            '**************************************************************************')
choix_mapping=0;            
afficher(Qr);
axes(handles.axes1);
cadre=floor(getrectangle);

if(cadre(3)*cadre(4)>18000)
  Reponse=questdlg('Le cadre selectionne est trop grand (le temps de calcul risque d''etre trop long)', ...
                      'Attention', ...
                      'Continuer','Annuler','Recommencer','Recommencer');
 
  switch Reponse,
    case 'Continuer', 
      surf(Qr(cadre(2):(cadre(2)+cadre(4)),cadre(1):(cadre(1)+cadre(3))));
      shading interp
    case 'Annuler',
    case 'Recommencer',
      trois_dim_Callback(hObject, eventdata, handles);
  end
else   
  surf(Qr(cadre(2):(cadre(2)+cadre(4)),cadre(1):(cadre(1)+cadre(3))));
  shading interp
  axis vis3d
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             saisie des parametres                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function xm_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function xm_Callback(hObject, eventdata, handles)
a=str2num(get(hObject,'string'));
if (a<0)
  uiwait(errordlg('Vous devez entrer une valeur positive','Problème')); 
else   
  Xmax=a;
end

function ym_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function ym_Callback(hObject, eventdata, handles)
a=str2num(get(hObject,'string'));
if (a<0)
  uiwait(errordlg('Vous devez entrer une valeur positive','Problème')); 
else   
  Ymax=a;
end

function rm_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function rm_Callback(hObject, eventdata, handles)
a=str2num(get(hObject,'string'));
if (a<=0)
  uiwait(errordlg('Vous devez entrer une valeur strictement positive','Problème')); 
else   
  rmin=a;
end

function dx_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function dx_Callback(hObject, eventdata, handles)
a=str2num(get(hObject,'string'));
if (a<0)
  uiwait(errordlg('Vous devez entrer une valeur positive','Problème')); 
else   
  dx=a;
end

function c1_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function c1_Callback(hObject, eventdata, handles)

function c2_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function c2_Callback(hObject, eventdata, handles)

function c3_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function c3_Callback(hObject, eventdata, handles)

function choix_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function choix_Callback(hObject, eventdata, handles)
if get(handles.choix,'value' )~=1,
   set(handles.c2,'visible','off');  set(handles.text8,'visible','off');
    set(handles.c1,'string','2/3');
else
   set(handles.c2,'visible','on');   set(handles.text8,'visible','on');
   set(handles.c1,'string','-.75');
end

function motif_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function rotation_Callback(hObject, eventdata, handles)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            saisie du type de motif                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function motif_Callback(hObject,eventdata,handles)
global polygone courbe_spline expression discretisation

   type_motif=get(handles.motif,'value');
   axes(handles.axes2);
   if (type_motif>=3 & type_motif<=6) | type_motif==8,
      set(handles.axes2,'visible','on');
      % Affichage du polygone ou de la spline courante
      if type_motif==3,
         axis([-.5 .5 0 1]);
         x=-.5:.01:.5; y=cos(pi*x).^2;
         plot(x,y);
      elseif type_motif==4,
         axis([-.5 .5 0 1]);
         x=-.5:.01:.5; y=exp(-1./(1-1e-15-(2*x).^4))/exp(-1);
         plot(x,y);
      elseif type_motif==5,
          % reintroduit le polygone precedent
         if size(polygone,2)==2 & size(polygone,1)>=3,
            axis([-.5 .5 -.5 .5]);
            plot(polygone(:,1),-polygone(:,2),'k');
            hold on;
            plot([polygone(end,1) polygone(1,1)],-([polygone(end,2) polygone(1,2)]),'k');
            hold off;
         else
            set(handles.axes2,'visible','off');
         end
      elseif type_motif==6,
          % reintroduit la spline  precedente
         if size(courbe_spline,2)==2 & size(courbe_spline,1)>=4,
            pts=points_spline(courbe_spline,200);
            axis([-.5 .5 -.5 .5]);
            plot(pts(:,1)',-pts(:,2)','k');
         else
            set(handles.axes2,'visible','off');
         end
      % On trace l'allure de la fonction shaper saisi par l'utilisateur
      elseif type_motif==8,
         expression='y=cos(pi*r).^2;';
         discretisation=1000;
         axis([-.5 .5 -1 1]);
         pas=.5/(discretisation-1);
         r=0:pas:.5;
         eval(expression);
         plot(-.5:pas:.5,[y(end:-1:2) y]);
      end
   else
      set(handles.axes2,'visible','off');
   end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     modification de l'echelle de couleur                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function couleurs_Callback(hObject, eventdata, handles)

function autumn_Callback(hObject, eventdata, handles)
colormap(autumn);

function bone_Callback(hObject, eventdata, handles)
colormap(bone);

function colorcube_Callback(hObject, eventdata, handles)
colormap(colorcube);

function cool_Callback(hObject, eventdata, handles)
colormap(cool);

function copper_Callback(hObject, eventdata, handles)
colormap(copper);

function flag_Callback(hObject, eventdata, handles)
colormap(flag);

function gray_Callback(hObject, eventdata, handles)
colormap(gray);

function hot_Callback(hObject, eventdata, handles)
colormap(hot);

function hsv_Callback(hObject, eventdata, handles)
colormap(hsv);

function jet_Callback(hObject, eventdata, handles)
colormap(jet);

function lines_Callback(hObject, eventdata, handles)
colormap(lines);

function pink_Callback(hObject, eventdata, handles)
colormap(pink);

function prism_Callback(hObject, eventdata, handles)
colormap(prism);

function spring_Callback(hObject, eventdata, handles)
colormap(spring);

function summer_Callback(hObject, eventdata, handles)
colormap(summer);

function vga_Callback(hObject, eventdata, handles)
colormap(vga);

function white_Callback(hObject, eventdata, handles)
colormap(white);

function winter_Callback(hObject, eventdata, handles)
colormap(winter);



