source: trunk/src/plot_object.m @ 421

Last change on this file since 421 was 397, checked in by sommeria, 12 years ago

civ_matlab and patch improved, changes in the management of interpolation (still in progress).
adapatation to movies (use of VideoReader?)

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