source: trunk/src/plot_object.m @ 446

Last change on this file since 446 was 432, checked in by sommeria, 12 years ago

system of projection object manipulation with uvmat improved.

File size: 17.1 KB
Line 
1%'plot_object': draws a projection object (points, line, plane...)
2%-------------------------------------------------------------------
3% function hh=plot_object(ObjectData,ProjObject,hplot,col)
4%
5%OUTPUT
6%             hh: handles of the graphic object (core part)
7%
8%INPUT:
9%
10% ObjectData: structure representing the object properties:
11%        .Type : style of projection object
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
38function [hh]=plot_object(ObjectData,ProjObject,hplot,col)
39
40%% default output
41hh=[];%default output
42% object representation is canceled if the field is not projected on a plane or is the same as the represented object
43if ~isfield(ObjectData,'Type')|| isequal(ProjObject,ObjectData)|| ~strcmp(ProjObject.Type,'plane')
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
55end
56XMin=0;%default
57XMax=0;
58YMin=0;
59YMax=0;
60ZMin=0;
61ZMax=0;
62
63%% determine the plotting axes (with handle 'haxes')
64test_newobj=1;
65if ishandle(hplot)
66    if isequal(get(hplot,'Tag'),'proj_object')% hplot is the handle of an object representation 
67        test_newobj=0;
68        haxes=get(hplot,'parent');
69        currentfig=get(haxes,'parent');
70    elseif isequal(get(hplot,'Type'),'axes')% hplot is the handle of an axis
71        haxes=hplot;
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
75        haxes=findobj(hplot,'Type','axes');%look for axes in the figure
76        haxes=haxes(1);
77        currentfig=hplot;
78    else
79        currentfig=figure; %create new figure
80        hplot=axes;%create new axes
81        haxes=hplot;
82    end
83else
84    currentfig=figure; %create new figure
85    hplot=axes;%create new axes
86    haxes=hplot;
87end
88set(0,'CurrentFigure',currentfig)%set the currentfigure as the current one
89set(currentfig,'CurrentAxes',haxes);%set the current axes in the current figure
90
91%% default input parameters
92if ~isfield(ObjectData,'ProjMode')||isempty(ObjectData.ProjMode)
93     ObjectData.ProjMode='projection';%default
94end
95if ~isfield(ObjectData,'Coord')||isempty(ObjectData.Coord)
96     ObjectData.Coord=[0 0 0];%default
97end
98if isfield(ObjectData,'RangeX') && ~isempty(ObjectData.RangeX)
99    XMax=max(ObjectData.RangeX);
100    XMin=min(ObjectData.RangeX);
101end
102if isfield(ObjectData,'RangeY')&&~isempty(ObjectData.RangeY)
103    YMax=max(ObjectData.RangeY);
104    YMin=min(ObjectData.RangeY);
105end
106if isfield(ObjectData,'RangeZ')&&~isempty(ObjectData.RangeZ)
107    ZMax=max(ObjectData.RangeZ);
108    ZMin=min(ObjectData.RangeZ);
109end
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'}
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
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
133end
134sizcoord=size(ObjectData.Coord);
135
136%% determine the coordinates xline, yline,xsup,xinf, yinf,ysup determining the new object plot
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')...
140    ||isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside');
141if test_line
142    xline=ObjectData.Coord(:,1);
143    yline=ObjectData.Coord(:,2);
144    nbpoints=numel(xline);
145    if isequal(ObjectData.Type,'polygon')
146        xline=[xline; ObjectData.Coord(1,1)];%closing the line
147        yline=[yline; ObjectData.Coord(1,2)];
148    elseif isequal(ObjectData.Type,'plane')|| isequal(ObjectData.Type,'volume')
149        phi=ObjectData.Angle(3)*pi/180;%angle in radians
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')
166               ObjectData=rmfield(ObjectData,'DX');
167            end
168            if isfield (ObjectData,'DY')
169               ObjectData=rmfield(ObjectData,'DY');
170            end
171        elseif isequal(ObjectData.ProjMode,'filter')
172            SubLineStyle=':';%range of projection not visible
173        end
174    end
175    if isequal(ObjectData.Type,'line')||isequal(ObjectData.Type,'polyline')||isequal(ObjectData.Type,'polygon')
176        if length(xline)<2
177            theta=0;
178        else
179            theta=angle(diff(xline)+1i*diff(yline));
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
195%% shading image
196if test_patch
197    npMx=512;
198    npMy=512; 
199    flag=zeros(npMy,npMx);
200    if isequal(ObjectData.Type,'ellipse')
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;
209        xi=(0.5:npMx-0.5)*scale_x+xlim(1);
210        yi=(0.5:npMy-0.5)*scale_y+ylim(1);
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;
217    elseif isequal(ObjectData.Type,'rectangle')||isequal(ObjectData.Type,'volume')
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;
226        xi=(0.5:npMx-0.5)*scale_x+xlim(1);
227        yi=(0.5:npMy-0.5)*scale_y+ylim(1);
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;
232    elseif isequal(ObjectData.Type,'polygon')
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
256
257%% MODIFY AN EXISTING OBJECT PLOT
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')
266           if length(PlotData.SubObject)==2 && ~isequal(ObjectData.Type,'points')&& ~isequal(ObjectData.Type,'plane');
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);
271           elseif isequal(ObjectData.Type,'points')&& ~isequal(YMax,0)
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
276               if size(ObjectData.Coord,1)>length(PlotData.SubObject)
277                   for ipt=length(PlotData.SubObject)+1:size(ObjectData.Coord,1)
278                     PlotData.SubObject(ipt)=rectangle('Curvature',[1 1],...
279                  'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax],'EdgeColor',col,...
280                  'LineStyle',SubLineStyle,'Tag','proj_object');
281                   end
282               end                                         
283           end
284        end
285        if isfield(PlotData,'DeformPoint')
286           for ipt=1:length(PlotData.DeformPoint)
287               if ishandle(PlotData.DeformPoint(ipt))
288                   if nbpoints>=ipt 
289                        set(PlotData.DeformPoint(ipt),'XData',xline(ipt),'YData',yline(ipt));
290                    end
291               end
292           end
293           if nbpoints>length(PlotData.DeformPoint)
294               for ipt=length(PlotData.DeformPoint)+1:nbpoints
295                    PlotData.DeformPoint(ipt)=line(xline(ipt),yline(ipt),'Color',col,'LineStyle','.','Tag','DeformPoint',...
296                        'SelectionHighlight','off','UserData',hplot);
297               end
298               set(hplot,'UserData',PlotData)
299           end
300        end
301    elseif isequal(ObjectData.Type,'rectangle')||isequal(ObjectData.Type,'ellipse')
302        set(hplot,'Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax])         
303    end
304    if test_patch
305        for iobj=1:length(PlotData.SubObject)
306            objtype=get(PlotData.SubObject(iobj),'Type');
307            if isequal(objtype,'image')
308                set(PlotData.SubObject(iobj),'CData',imflag,'AlphaData',(flag)*0.2)
309                set(PlotData.SubObject(iobj),'XData',[xlim(1)+dx/2 xlim(2)-dx/2])
310                set(PlotData.SubObject(iobj),'YData',[ylim(1)+dy/2 ylim(2)-dy/2])
311            end
312        end
313    end
314end
315
316%% create the object
317if test_newobj
318%     axes(haxes)
319    hother=findobj('Tag','proj_object');%find all the proj objects
320    for iobj=1:length(hother)
321        if strcmp(get(hother(iobj),'Type'),'rectangle')|| strcmp(get(hother(iobj),'Type'),'patch')
322            set(hother(iobj),'EdgeColor','b')
323            if isequal(get(hother(iobj),'FaceColor'),'m')
324                set(hother(iobj),'FaceColor','b')
325            end
326        elseif isequal(get(hother(iobj),'Type'),'image')
327               Acolor=get(hother(iobj),'CData');
328               Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
329               set(hother(iobj),'CData',Acolor);
330        else
331             set(hother(iobj),'Color','b')
332        end
333        set(hother(iobj),'Selected','off')
334    end
335    hother=findobj('Tag','DeformPoint');
336    set(hother,'Color','b');
337    set(hother,'Selected','off') 
338    if isequal(ObjectData.Type,'points')
339        hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color',col,'LineStyle','.','Marker','+');
340        for ipt=1:length(xline)
341              PlotData.DeformPoint(ipt)=line(ObjectData.Coord(ipt,1),ObjectData.Coord(ipt,2),'Color',...
342                  col,'LineStyle','.','SelectionHighlight','off','UserData',hh,'Tag','DeformPoint');
343              %create circle around each point
344              if ~isequal(YMax,0)
345                 PlotData.SubObject(ipt)=rectangle('Curvature',[1 1],...
346                  'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax],'EdgeColor',col,...
347                  'LineStyle',SubLineStyle,'Tag','proj_object');
348              end
349        end
350    elseif  strcmp(ObjectData.Type,'line')||strcmp(ObjectData.Type,'polyline')||...       
351          strcmp(ObjectData.Type,'polygon') ||strcmp(ObjectData.Type,'plane')||strcmp(ObjectData.Type,'volume')%  (isequal(ObjectData.Type,'polygon') & ~test_patch) |isequal(ObjectData.Type,'plane')
352        hh=line(xline,yline,'Color',col);
353        if ~strcmp(ObjectData.Type,'plane') && ~strcmp(ObjectData.Type,'volume')
354            PlotData.SubObject(1)=line(xinf,yinf,'Color',col,'LineStyle',SubLineStyle,'Tag','proj_object');%draw sub-lines
355            PlotData.SubObject(2)=line(xsup,ysup,'Color',col,'LineStyle',SubLineStyle,'Tag','proj_object');
356            for ipt=1:sizcoord(1)
357                PlotData.DeformPoint(ipt)=line(ObjectData.Coord(ipt,1),ObjectData.Coord(ipt,2),'Color',...
358                      col,'LineStyle','none','Marker','.','Tag','DeformPoint','SelectionHighlight','off','UserData',hh);
359            end
360        end
361   
362    elseif strcmp(ObjectData.Type,'rectangle')
363        hh=rectangle('Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax],'EdgeColor',col);   
364    elseif strcmp(ObjectData.Type,'ellipse')
365        hh=rectangle('Curvature',[1 1],'Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax],'EdgeColor',col);
366    else
367        msgbox_uvmat('ERROR','unknown ObjectData.Type in plot_object.m')
368        return
369    end
370    set(hh,'Tag','proj_object')
371     if test_patch
372         hold on
373        hhh=image([xlim(1)+dx/2 xlim(2)-dx/2],[ylim(1)+dy/2 ylim(2)-dy/2],imflag,'Tag','proj_object','HitTest','off');
374       set(hhh,'AlphaData',(flag)*0.2)% set partial transparency to the filling color
375         PlotData.SubObject=hhh;   
376     end
377    if isfield(PlotData,'SubObject')
378        set(PlotData.SubObject,'UserData',hh)%record the parent handles in the SubObjects
379    end
380    if isfield(PlotData,'DeformPoint')
381        for ipt=1:sizcoord(1)
382            set(PlotData.DeformPoint(ipt),'UserData',hh);%record the parent handles in the SubObjects
383        end
384        set(PlotData.DeformPoint,'UserData',hh)%record the parent handles in the SubObjects
385    end
386end
387set(hh,'UserData',PlotData)
Note: See TracBrowser for help on using the repository browser.