source: trunk/src/plot_object.m @ 231

Last change on this file since 231 was 231, checked in by sommeria, 10 years ago

interactive test for piv introduced various bug corrections

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