source: trunk/src/plot_object.m @ 622

Last change on this file since 622 was 622, checked in by sommeria, 11 years ago

mouse system for uvmat imrpoved

File size: 17.8 KB
RevLine 
[8]1%'plot_object': draws a projection object (points, line, plane...)
2%-------------------------------------------------------------------
[432]3% function hh=plot_object(ObjectData,ProjObject,hplot,col)
[8]4%
5%OUTPUT
6%             hh: handles of the graphic object (core part)
7%
8%INPUT:
9%
[432]10% ObjectData: structure representing the object properties:
[379]11%        .Type : style of projection object
[8]12%        .Coord: set of coordinates defining the object position;
13%        .ProjMode=type of projection ;
14%       .ProjAngle=angle of projection;
15%       .DX,.DY,.DZ=increments;
16%       .YMax,YMin: min and max Y
17% ProjObject: projection object corresponding to the current plot (e. g. plane)
18% hplot: handle of the object plot to modify or if it is an axis, the axis
19%            where the object must be plotted, or if it is a figure the plotting figure
20% col: color of the plot, e;g; 'm', 'b' ..;
21
22%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
23%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
24%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
25%     This file is part of the toolbox UVMAT.
26%
27%     UVMAT is free software; you can redistribute it and/or modify
28%     it under the terms of the GNU General Public License as published by
29%     the Free Software Foundation; either version 2 of the License, or
30%     (at your option) any later version.
31%
32%     UVMAT is distributed in the hope that it will be useful,
33%     but WITHOUT ANY WARRANTY; without even the implied warranty of
34%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
36%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
37
[432]38function [hh]=plot_object(ObjectData,ProjObject,hplot,col)
39
[156]40%% default output
[8]41hh=[];%default output
[432]42% object representation is canceled if the field is not projected on a plane or is the same as the represented object
[534]43if ~isfield(ObjectData,'Type')|| isequal(ProjObject,ObjectData)||~isfield(ProjObject,'Type')|| ~strcmp(ProjObject.Type,'plane')
[432]44    if ~isempty(hplot) && ishandle(hplot) && ~strcmp(get(hplot,'Type'),'axes')
45        ObjectPlotData=get(hplot,'UserData');
46        if isfield(ObjectPlotData,'SubObject') & ishandle(ObjectPlotData.SubObject)
47            delete(ObjectPlotData.SubObject);
48        end
49        if isfield(ObjectPlotData,'DeformPoint') & ishandle(ObjectPlotData.DeformPoint)
50            delete(ObjectPlotData.DeformPoint);
51        end
52        delete(hplot)
53    end
54    return
[8]55end
56XMin=0;%default
57XMax=0;
58YMin=0;
59YMax=0;
60ZMin=0;
61ZMax=0;
62
[156]63%% determine the plotting axes (with handle 'haxes')
[8]64test_newobj=1;
65if ishandle(hplot)
[156]66    if isequal(get(hplot,'Tag'),'proj_object')% hplot is the handle of an object representation 
[8]67        test_newobj=0;
68        haxes=get(hplot,'parent');
[231]69        currentfig=get(haxes,'parent');
[156]70    elseif isequal(get(hplot,'Type'),'axes')% hplot is the handle of an axis
[231]71        haxes=hplot;
[156]72        currentfig=get(hplot,'parent');
73    elseif isequal(get(hplot,'Type'),'figure')% hplot is the handle of a figure
74        set(0,'CurrentFigure',hplot);%set the input figure as the current one
[8]75        haxes=findobj(hplot,'Type','axes');%look for axes in the figure
76        haxes=haxes(1);
[231]77        currentfig=hplot;
[8]78    else
[231]79        currentfig=figure; %create new figure
[8]80        hplot=axes;%create new axes
81        haxes=hplot;
82    end
83else
[231]84    currentfig=figure; %create new figure
[8]85    hplot=axes;%create new axes
86    haxes=hplot;
87end
[231]88set(0,'CurrentFigure',currentfig)%set the currentfigure as the current one
89set(currentfig,'CurrentAxes',haxes);%set the current axes in the current figure
[8]90
[156]91%% default input parameters
[177]92if ~isfield(ObjectData,'ProjMode')||isempty(ObjectData.ProjMode)
[8]93     ObjectData.ProjMode='projection';%default
94end
[177]95if ~isfield(ObjectData,'Coord')||isempty(ObjectData.Coord)
[8]96     ObjectData.Coord=[0 0 0];%default
97end
[379]98if isfield(ObjectData,'RangeX') && ~isempty(ObjectData.RangeX)
99    XMax=max(ObjectData.RangeX);
100    XMin=min(ObjectData.RangeX);
[8]101end
[379]102if isfield(ObjectData,'RangeY')&&~isempty(ObjectData.RangeY)
103    YMax=max(ObjectData.RangeY);
104    YMin=min(ObjectData.RangeY);
[8]105end
[379]106if isfield(ObjectData,'RangeZ')&&~isempty(ObjectData.RangeZ)
107    ZMax=max(ObjectData.RangeZ);
108    ZMin=min(ObjectData.RangeZ);
[8]109end
[379]110switch ObjectData.Type
111    case 'points'
112        if strcmp(ObjectData.ProjMode,'projection')
113            YMax=max(XMax,YMax);
114            YMax=max(YMax,ZMax);
115        end
116    case {'rectangle','ellipse','volume'}
[622]117%         if  isequal(YMax,0)
118%             ylim=get(haxes,'YLim');
119%             YMax=(ylim(2)-ylim(1))/100;
120%         end
121%         if isequal(XMax,0)
122%             XMax=YMax;%default
123%         end
[379]124    case 'plane'
125        if  isequal(XMax,0)
126            xlim=get(haxes,'XLim');
127            XMax=xlim(2);
128        end
129        if  isequal(YMax,0)
130            ylim=get(haxes,'YLim');
131            YMax=ylim(2);
132        end
[8]133end
134sizcoord=size(ObjectData.Coord);
135
[156]136%% determine the coordinates xline, yline,xsup,xinf, yinf,ysup determining the new object plot
[379]137test_line= isequal(ObjectData.Type,'points')|isequal(ObjectData.Type,'line')|isequal(ObjectData.Type,'polyline')|...
138    isequal(ObjectData.Type,'polygon')| isequal(ObjectData.Type,'plane')| isequal(ObjectData.Type,'volume');
139test_patch=isequal(ObjectData.ProjMode,'inside')||isequal(ObjectData.ProjMode,'outside')||isequal(ObjectData.Type,'volume')...
[8]140    ||isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside');
141if test_line
142    xline=ObjectData.Coord(:,1);
143    yline=ObjectData.Coord(:,2);
[177]144    nbpoints=numel(xline);
[379]145    if isequal(ObjectData.Type,'polygon')
[8]146        xline=[xline; ObjectData.Coord(1,1)];%closing the line
147        yline=[yline; ObjectData.Coord(1,2)];
[379]148    elseif isequal(ObjectData.Type,'plane')|| isequal(ObjectData.Type,'volume')
[397]149        phi=ObjectData.Angle(3)*pi/180;%angle in radians
[8]150        Xend_x=xline(1)+XMax*cos(phi);
151        Xend_y=yline(1)+XMax*sin(phi);
152        Xbeg_x=xline(1)+XMin*cos(phi);
153        Xbeg_y=yline(1)+XMin*sin(phi);
154        Yend_x=xline(1)-YMax*sin(phi);
155        Yend_y=yline(1)+YMax*cos(phi);
156        Ybeg_x=xline(1)-YMin*sin(phi);
157        Ybeg_y=yline(1)+YMin*cos(phi);
158        xline=[Xbeg_x Xend_x NaN Ybeg_x Yend_x];
159        yline=[Xbeg_y Xend_y NaN Ybeg_y Yend_y];
160    end
161    SubLineStyle='none';%default
162    if isfield(ObjectData,'ProjMode')
163        if isequal(ObjectData.ProjMode,'projection')
164            SubLineStyle='--'; %range of projection marked by dash
165            if isfield (ObjectData,'DX')
[177]166               ObjectData=rmfield(ObjectData,'DX');
[8]167            end
168            if isfield (ObjectData,'DY')
[177]169               ObjectData=rmfield(ObjectData,'DY');
[8]170            end
171        elseif isequal(ObjectData.ProjMode,'filter')
172            SubLineStyle=':';%range of projection not visible
173        end
174    end
[379]175    if isequal(ObjectData.Type,'line')||isequal(ObjectData.Type,'polyline')||isequal(ObjectData.Type,'polygon')
[8]176        if length(xline)<2
177            theta=0;
178        else
[187]179            theta=angle(diff(xline)+1i*diff(yline));
[8]180            theta(length(xline))=theta(length(xline)-1);
181        end
182        xsup(1)=xline(1)+YMax*sin(theta(1));
183        xinf(1)=xline(1)-YMax*sin(theta(1));
184        ysup(1)=yline(1)-YMax*cos(theta(1));
185        yinf(1)=yline(1)+YMax*cos(theta(1));
186        for ip=2:length(xline)
187            xsup(ip)=xline(ip)+YMax*sin((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
188            xinf(ip)=xline(ip)-YMax*sin((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
189            ysup(ip)=yline(ip)-YMax*cos((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
190            yinf(ip)=yline(ip)+YMax*cos((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
191        end
192    end
193end
194
[156]195%% shading image
[8]196if test_patch
197    npMx=512;
198    npMy=512; 
199    flag=zeros(npMy,npMx);
[379]200    if isequal(ObjectData.Type,'ellipse')
[8]201        XimaMin=ObjectData.Coord(1,1)-XMax;
202        XimaMax=ObjectData.Coord(1,1)+XMax;
203        YimaMin=ObjectData.Coord(1,2)-YMax;
204        YimaMax=ObjectData.Coord(1,2)+YMax;
205        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];%create an image around the ellipse
206        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
207        scale_x=2*1.4*XMax/npMx;
208        scale_y=2*1.4*YMax/npMy;
[177]209        xi=(0.5:npMx-0.5)*scale_x+xlim(1);
210        yi=(0.5:npMy-0.5)*scale_y+ylim(1);
[8]211        [Xi,Yi]=meshgrid(xi,yi);
212        X2Max=XMax*XMax;
213        Y2Max=YMax*YMax;
214        distX=(Xi-ObjectData.Coord(1,1));
215        distY=(Yi-ObjectData.Coord(1,2));
216        flag=(distX.*distX/X2Max+distY.*distY/Y2Max)<1;
[379]217    elseif isequal(ObjectData.Type,'rectangle')||isequal(ObjectData.Type,'volume')
[8]218        XimaMin=ObjectData.Coord(1,1)-XMax;
219        XimaMax=ObjectData.Coord(1,1)+XMax;
220        YimaMin=ObjectData.Coord(1,2)-YMax;
221        YimaMax=ObjectData.Coord(1,2)+YMax;
222        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];%create an image around the ellipse
223        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
224        scale_x=2*1.4*XMax/npMx;
225        scale_y=2*1.4*YMax/npMy;
[177]226        xi=(0.5:npMx-0.5)*scale_x+xlim(1);
227        yi=(0.5:npMy-0.5)*scale_y+ylim(1);
[8]228        [Xi,Yi]=meshgrid(xi,yi);
229        distX=abs(Xi-ObjectData.Coord(1,1));
230        distY=abs(Yi-ObjectData.Coord(1,2));
231        flag=distX<XMax & distY< YMax;
[379]232    elseif isequal(ObjectData.Type,'polygon')
[8]233        XimaMin=min(ObjectData.Coord(:,1));
234        XimaMax=max(ObjectData.Coord(:,1));
235        YimaMin=min(ObjectData.Coord(:,2));
236        YimaMax=max(ObjectData.Coord(:,2));
237        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];
238        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
239        [Xlim,Ylim]=meshgrid(linspace(xlim(1),xlim(2),npMx),linspace(ylim(1),ylim(2),npMy));
240        %flag=roipoly(xlim,ylim,flag,ObjectData.Coord(:,1),ObjectData.Coord(:,2));%=1 inside the polygon, 0 outsid
241        flag=inpolygon(Xlim,Ylim,ObjectData.Coord(:,1),ObjectData.Coord(:,2));%=1 inside the polygon, 0 outsid
242    end
243    if isequal(ObjectData.ProjMode,'outside')||isequal(ObjectData.ProjMode,'mask_outside')
244        flag=~flag;
245    end
246    imflag=zeros(npMx,npMy,3);
247    imflag(:,:,3)=flag; % blue color
248    if isequal(col,'m')
249         imflag(:,:,1)=flag; % magenta color
250    end
251    dx=(xlim(2)-xlim(1))/npMx;
252    dy=(ylim(2)-ylim(1))/npMy;
253end
254
255PlotData=[];%default
[177]256
[156]257%% MODIFY AN EXISTING OBJECT PLOT
[8]258if test_newobj==0;
259    hh=hplot;
260    PlotData=get(hplot,'UserData');           
261    if test_line
262        set(hplot,'XData',xline)
263        set(hplot,'YData',yline)
264    %modify subobjects
265        if isfield(PlotData,'SubObject')
[379]266           if length(PlotData.SubObject)==2 && ~isequal(ObjectData.Type,'points')&& ~isequal(ObjectData.Type,'plane');
[8]267                set(PlotData.SubObject(1),'XData',xinf);
268                set(PlotData.SubObject(1),'YData',yinf);
269                set(PlotData.SubObject(2),'XData',xsup);
270                set(PlotData.SubObject(2),'YData',ysup);
[379]271           elseif isequal(ObjectData.Type,'points')&& ~isequal(YMax,0)
[8]272               for ipt=1:min(length(PlotData.SubObject),size(ObjectData.Coord,1))
273                    set(PlotData.SubObject(ipt),'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax])
274               end
275               %complement missing points
[622]276                if length(PlotData.SubObject)>nbpoints% fpoints in excess on the graph
277               for ii=nbpoints+1: length(PlotData.SubObject);
278                   if ishandle(PlotData.SubObject(ii))
279                        delete(PlotData.SubObject(ii))
280                   end
281               end
282%                NbDeformPoint=nbpoints;
283           end
284               if nbpoints>length(PlotData.SubObject)
285                   for ipt=length(PlotData.SubObject)+1:nbpoints
[8]286                     PlotData.SubObject(ipt)=rectangle('Curvature',[1 1],...
287                  'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax],'EdgeColor',col,...
288                  'LineStyle',SubLineStyle,'Tag','proj_object');
289                   end
290               end                                         
291           end
292        end
293        if isfield(PlotData,'DeformPoint')
[622]294            NbDeformPoint=length(PlotData.DeformPoint);
295           if NbDeformPoint>nbpoints% fpoints in excess on the graph
296               for ii=nbpoints+1:NbDeformPoint;
297                   if ishandle(PlotData.DeformPoint(ii))
298                        delete(PlotData.DeformPoint(ii))
299                   end
300               end
301               NbDeformPoint=nbpoints;
302           end
303           for ipt=1:NbDeformPoint
[8]304               if ishandle(PlotData.DeformPoint(ipt))
[177]305                   if nbpoints>=ipt 
[8]306                        set(PlotData.DeformPoint(ipt),'XData',xline(ipt),'YData',yline(ipt));
307                    end
308               end
309           end
[177]310           if nbpoints>length(PlotData.DeformPoint)
311               for ipt=length(PlotData.DeformPoint)+1:nbpoints
[8]312                    PlotData.DeformPoint(ipt)=line(xline(ipt),yline(ipt),'Color',col,'LineStyle','.','Tag','DeformPoint',...
313                        'SelectionHighlight','off','UserData',hplot);
314               end
315               set(hplot,'UserData',PlotData)
316           end
317        end
[622]318    elseif (isequal(ObjectData.Type,'rectangle')||isequal(ObjectData.Type,'ellipse'))&&XMax>0 && YMax>0
[8]319        set(hplot,'Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax])         
320    end
321    if test_patch
322        for iobj=1:length(PlotData.SubObject)
323            objtype=get(PlotData.SubObject(iobj),'Type');
324            if isequal(objtype,'image')
325                set(PlotData.SubObject(iobj),'CData',imflag,'AlphaData',(flag)*0.2)
326                set(PlotData.SubObject(iobj),'XData',[xlim(1)+dx/2 xlim(2)-dx/2])
327                set(PlotData.SubObject(iobj),'YData',[ylim(1)+dy/2 ylim(2)-dy/2])
328            end
329        end
330    end
331end
332
[156]333%% create the object
[8]334if test_newobj
[231]335%     axes(haxes)
[8]336    hother=findobj('Tag','proj_object');%find all the proj objects
337    for iobj=1:length(hother)
[177]338        if strcmp(get(hother(iobj),'Type'),'rectangle')|| strcmp(get(hother(iobj),'Type'),'patch')
[8]339            set(hother(iobj),'EdgeColor','b')
340            if isequal(get(hother(iobj),'FaceColor'),'m')
341                set(hother(iobj),'FaceColor','b')
342            end
343        elseif isequal(get(hother(iobj),'Type'),'image')
344               Acolor=get(hother(iobj),'CData');
345               Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
346               set(hother(iobj),'CData',Acolor);
347        else
348             set(hother(iobj),'Color','b')
349        end
350        set(hother(iobj),'Selected','off')
351    end
352    hother=findobj('Tag','DeformPoint');
353    set(hother,'Color','b');
354    set(hother,'Selected','off') 
[379]355    if isequal(ObjectData.Type,'points')
[8]356        hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color',col,'LineStyle','.','Marker','+');
357        for ipt=1:length(xline)
358              PlotData.DeformPoint(ipt)=line(ObjectData.Coord(ipt,1),ObjectData.Coord(ipt,2),'Color',...
359                  col,'LineStyle','.','SelectionHighlight','off','UserData',hh,'Tag','DeformPoint');
360              %create circle around each point
361              if ~isequal(YMax,0)
362                 PlotData.SubObject(ipt)=rectangle('Curvature',[1 1],...
363                  'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax],'EdgeColor',col,...
364                  'LineStyle',SubLineStyle,'Tag','proj_object');
365              end
366        end
[379]367    elseif  strcmp(ObjectData.Type,'line')||strcmp(ObjectData.Type,'polyline')||...       
368          strcmp(ObjectData.Type,'polygon') ||strcmp(ObjectData.Type,'plane')||strcmp(ObjectData.Type,'volume')%  (isequal(ObjectData.Type,'polygon') & ~test_patch) |isequal(ObjectData.Type,'plane')
[8]369        hh=line(xline,yline,'Color',col);
[379]370        if ~strcmp(ObjectData.Type,'plane') && ~strcmp(ObjectData.Type,'volume')
[177]371            PlotData.SubObject(1)=line(xinf,yinf,'Color',col,'LineStyle',SubLineStyle,'Tag','proj_object');%draw sub-lines
372            PlotData.SubObject(2)=line(xsup,ysup,'Color',col,'LineStyle',SubLineStyle,'Tag','proj_object');
[8]373            for ipt=1:sizcoord(1)
374                PlotData.DeformPoint(ipt)=line(ObjectData.Coord(ipt,1),ObjectData.Coord(ipt,2),'Color',...
375                      col,'LineStyle','none','Marker','.','Tag','DeformPoint','SelectionHighlight','off','UserData',hh);
376            end
377        end
378   
[379]379    elseif strcmp(ObjectData.Type,'rectangle')
[8]380        hh=rectangle('Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax],'EdgeColor',col);   
[379]381    elseif strcmp(ObjectData.Type,'ellipse')
[8]382        hh=rectangle('Curvature',[1 1],'Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax],'EdgeColor',col);
383    else
[379]384        msgbox_uvmat('ERROR','unknown ObjectData.Type in plot_object.m')
[8]385        return
386    end
387    set(hh,'Tag','proj_object')
[187]388     if test_patch
389         hold on
[8]390        hhh=image([xlim(1)+dx/2 xlim(2)-dx/2],[ylim(1)+dy/2 ylim(2)-dy/2],imflag,'Tag','proj_object','HitTest','off');
[187]391       set(hhh,'AlphaData',(flag)*0.2)% set partial transparency to the filling color
392         PlotData.SubObject=hhh;   
393     end
[8]394    if isfield(PlotData,'SubObject')
395        set(PlotData.SubObject,'UserData',hh)%record the parent handles in the SubObjects
396    end
397    if isfield(PlotData,'DeformPoint')
398        for ipt=1:sizcoord(1)
399            set(PlotData.DeformPoint(ipt),'UserData',hh);%record the parent handles in the SubObjects
400        end
401        set(PlotData.DeformPoint,'UserData',hh)%record the parent handles in the SubObjects
402    end
403end
404set(hh,'UserData',PlotData)
Note: See TracBrowser for help on using the repository browser.