source: trunk/src/plot_object.m @ 187

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

various bug repairs

File size: 17.4 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    elseif isequal(get(hplot,'Type'),'axes')% hplot is the handle of an axis
73        currentfig=get(hplot,'parent');
74        set(0,'CurrentFigure',currentfig)
75        haxes=hplot;
76        set(currentfig,'CurrentAxes',haxes);
77    elseif isequal(get(hplot,'Type'),'figure')% hplot is the handle of a figure
78        set(0,'CurrentFigure',hplot);%set the input figure as the current one
79        haxes=findobj(hplot,'Type','axes');%look for axes in the figure
80        haxes=haxes(1);
81        set(hplot,'CurrentAxes',haxes);%set the first found axis as the current one
82    else
83        figure; %create new figure
84        hplot=axes;%create new axes
85        haxes=hplot;
86    end
87else
88    figure; %create new figure
89    hplot=axes;%create new axes
90    haxes=hplot;
91end
92
93%% default input parameters
94if ~isfield(ObjectData,'ProjMode')||isempty(ObjectData.ProjMode)
95     ObjectData.ProjMode='projection';%default
96end
97if ~isfield(ObjectData,'Coord')||isempty(ObjectData.Coord)
98     ObjectData.Coord=[0 0 0];%default
99end
100if ~isfield(ObjectData,'Phi')||isempty(ObjectData.Phi)
101     ObjectData.Phi=0;%default
102end
103if ~isfield(ObjectData,'Range')
104    ObjectData.Range(1,1)=0; %edfault
105end
106if size(ObjectData.Range,2)>=2
107    YMax=ObjectData.Range(1,2);%default
108end
109if size(ObjectData.Range,2)>=2 & size(ObjectData.Range,1)>=2
110    YMin=ObjectData.Range(2,2);
111else
112    YMin=0;
113end
114XMax=ObjectData.Range(1,1);
115if size(ObjectData.Range,1)>=2
116    XMin=ObjectData.Range(2,1);
117end
118if isfield(ObjectData,'RangeX')
119   XMax=max(ObjectData.RangeX);
120   XMin=min(ObjectData.RangeX);
121end
122if isfield(ObjectData,'RangeY')
123   YMax=max(ObjectData.RangeY);
124   YMin=min(ObjectData.RangeY);
125end
126if isfield(ObjectData,'RangeZ')
127   ZMax=max(ObjectData.RangeZ);
128   ZMin=min(ObjectData.RangeZ);
129end
130if isequal(ObjectData.Style,'points')&isequal(ObjectData.ProjMode,'projection')
131    YMax=max(XMax,YMax);
132    YMax=max(YMax,ZMax);
133elseif isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')||isequal(ObjectData.Style,'volume')
134    if  isequal(YMax,0)
135        ylim=get(haxes,'YLim');
136        YMax=(ylim(2)-ylim(1))/100;
137    end
138    if isequal(XMax,0)
139        XMax=YMax;%default
140    end
141elseif isequal(ObjectData.Style,'plane')
142   if  isequal(XMax,0)
143        xlim=get(haxes,'XLim');
144        XMax=xlim(2);
145   end
146   if  isequal(YMax,0)
147        ylim=get(haxes,'YLim');
148        YMax=ylim(2);
149   end
150end
151sizcoord=size(ObjectData.Coord);
152
153%% determine the coordinates xline, yline,xsup,xinf, yinf,ysup determining the new object plot
154test_line= isequal(ObjectData.Style,'points')|isequal(ObjectData.Style,'line')|isequal(ObjectData.Style,'polyline')|...
155    isequal(ObjectData.Style,'polygon')| isequal(ObjectData.Style,'plane')| isequal(ObjectData.Style,'volume');
156test_patch=isequal(ObjectData.ProjMode,'inside')||isequal(ObjectData.ProjMode,'outside')||isequal(ObjectData.Style,'volume')...
157    ||isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside');
158if test_line
159    xline=ObjectData.Coord(:,1);
160    yline=ObjectData.Coord(:,2);
161    nbpoints=numel(xline);
162    if isequal(ObjectData.Style,'polygon')
163        xline=[xline; ObjectData.Coord(1,1)];%closing the line
164        yline=[yline; ObjectData.Coord(1,2)];
165    elseif isequal(ObjectData.Style,'plane')|| isequal(ObjectData.Style,'volume')
166        phi=ObjectData.Phi*pi/180;%angle in radians
167        Xend_x=xline(1)+XMax*cos(phi);
168        Xend_y=yline(1)+XMax*sin(phi);
169        Xbeg_x=xline(1)+XMin*cos(phi);
170        Xbeg_y=yline(1)+XMin*sin(phi);
171        Yend_x=xline(1)-YMax*sin(phi);
172        Yend_y=yline(1)+YMax*cos(phi);
173        Ybeg_x=xline(1)-YMin*sin(phi);
174        Ybeg_y=yline(1)+YMin*cos(phi);
175        xline=[Xbeg_x Xend_x NaN Ybeg_x Yend_x];
176        yline=[Xbeg_y Xend_y NaN Ybeg_y Yend_y];
177    end
178    SubLineStyle='none';%default
179    if isfield(ObjectData,'ProjMode')
180        if isequal(ObjectData.ProjMode,'projection')
181            SubLineStyle='--'; %range of projection marked by dash
182            if isfield (ObjectData,'DX')
183               ObjectData=rmfield(ObjectData,'DX');
184            end
185            if isfield (ObjectData,'DY')
186               ObjectData=rmfield(ObjectData,'DY');
187            end
188        elseif isequal(ObjectData.ProjMode,'filter')
189            SubLineStyle=':';%range of projection not visible
190        end
191    end
192    if isequal(ObjectData.Style,'line')||isequal(ObjectData.Style,'polyline')||isequal(ObjectData.Style,'polygon')
193        if length(xline)<2
194            theta=0;
195        else
196            theta=angle(diff(xline)+1i*diff(yline));
197            theta(length(xline))=theta(length(xline)-1);
198        end
199        xsup(1)=xline(1)+YMax*sin(theta(1));
200        xinf(1)=xline(1)-YMax*sin(theta(1));
201        ysup(1)=yline(1)-YMax*cos(theta(1));
202        yinf(1)=yline(1)+YMax*cos(theta(1));
203        for ip=2:length(xline)
204            xsup(ip)=xline(ip)+YMax*sin((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
205            xinf(ip)=xline(ip)-YMax*sin((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
206            ysup(ip)=yline(ip)-YMax*cos((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
207            yinf(ip)=yline(ip)+YMax*cos((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
208        end
209    end
210end
211
212%% shading image
213if test_patch
214    npMx=512;
215    npMy=512; 
216    flag=zeros(npMy,npMx);
217    if isequal(ObjectData.Style,'ellipse')
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        X2Max=XMax*XMax;
230        Y2Max=YMax*YMax;
231        distX=(Xi-ObjectData.Coord(1,1));
232        distY=(Yi-ObjectData.Coord(1,2));
233        flag=(distX.*distX/X2Max+distY.*distY/Y2Max)<1;
234    elseif isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'volume')
235        XimaMin=ObjectData.Coord(1,1)-XMax;
236        XimaMax=ObjectData.Coord(1,1)+XMax;
237        YimaMin=ObjectData.Coord(1,2)-YMax;
238        YimaMax=ObjectData.Coord(1,2)+YMax;
239        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];%create an image around the ellipse
240        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
241        scale_x=2*1.4*XMax/npMx;
242        scale_y=2*1.4*YMax/npMy;
243        xi=(0.5:npMx-0.5)*scale_x+xlim(1);
244        yi=(0.5:npMy-0.5)*scale_y+ylim(1);
245        [Xi,Yi]=meshgrid(xi,yi);
246        distX=abs(Xi-ObjectData.Coord(1,1));
247        distY=abs(Yi-ObjectData.Coord(1,2));
248        flag=distX<XMax & distY< YMax;
249    elseif isequal(ObjectData.Style,'polygon')
250        XimaMin=min(ObjectData.Coord(:,1));
251        XimaMax=max(ObjectData.Coord(:,1));
252        YimaMin=min(ObjectData.Coord(:,2));
253        YimaMax=max(ObjectData.Coord(:,2));
254        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];
255        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
256        [Xlim,Ylim]=meshgrid(linspace(xlim(1),xlim(2),npMx),linspace(ylim(1),ylim(2),npMy));
257        %flag=roipoly(xlim,ylim,flag,ObjectData.Coord(:,1),ObjectData.Coord(:,2));%=1 inside the polygon, 0 outsid
258        flag=inpolygon(Xlim,Ylim,ObjectData.Coord(:,1),ObjectData.Coord(:,2));%=1 inside the polygon, 0 outsid
259    end
260    if isequal(ObjectData.ProjMode,'outside')||isequal(ObjectData.ProjMode,'mask_outside')
261        flag=~flag;
262    end
263    imflag=zeros(npMx,npMy,3);
264    imflag(:,:,3)=flag; % blue color
265    if isequal(col,'m')
266         imflag(:,:,1)=flag; % magenta color
267    end
268    dx=(xlim(2)-xlim(1))/npMx;
269    dy=(ylim(2)-ylim(1))/npMy;
270end
271
272PlotData=[];%default
273
274%% MODIFY AN EXISTING OBJECT PLOT
275if test_newobj==0;
276    hh=hplot;
277    PlotData=get(hplot,'UserData');           
278    if test_line
279        set(hplot,'XData',xline)
280        set(hplot,'YData',yline)
281    %modify subobjects
282        if isfield(PlotData,'SubObject')
283           if length(PlotData.SubObject)==2 && ~isequal(ObjectData.Style,'points')&& ~isequal(ObjectData.Style,'plane');
284                set(PlotData.SubObject(1),'XData',xinf);
285                set(PlotData.SubObject(1),'YData',yinf);
286                set(PlotData.SubObject(2),'XData',xsup);
287                set(PlotData.SubObject(2),'YData',ysup);
288           elseif isequal(ObjectData.Style,'points')&& ~isequal(YMax,0)
289               for ipt=1:min(length(PlotData.SubObject),size(ObjectData.Coord,1))
290                    set(PlotData.SubObject(ipt),'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax])
291               end
292               %complement missing points
293               if size(ObjectData.Coord,1)>length(PlotData.SubObject)
294                   for ipt=length(PlotData.SubObject)+1:size(ObjectData.Coord,1)
295                     PlotData.SubObject(ipt)=rectangle('Curvature',[1 1],...
296                  'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax],'EdgeColor',col,...
297                  'LineStyle',SubLineStyle,'Tag','proj_object');
298                   end
299               end                                         
300           end
301        end
302        if isfield(PlotData,'DeformPoint')
303           for ipt=1:length(PlotData.DeformPoint)
304               if ishandle(PlotData.DeformPoint(ipt))
305                   if nbpoints>=ipt 
306                        set(PlotData.DeformPoint(ipt),'XData',xline(ipt),'YData',yline(ipt));
307                    end
308               end
309           end
310           if nbpoints>length(PlotData.DeformPoint)
311               for ipt=length(PlotData.DeformPoint)+1:nbpoints
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
318    elseif isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')
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
333%% create the object
334if test_newobj
335    axes(haxes)
336    hother=findobj('Tag','proj_object');%find all the proj objects
337    for iobj=1:length(hother)
338        if strcmp(get(hother(iobj),'Type'),'rectangle')|| strcmp(get(hother(iobj),'Type'),'patch')
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') 
355    if isequal(ObjectData.Style,'points')
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
367    elseif  strcmp(ObjectData.Style,'line')||strcmp(ObjectData.Style,'polyline')||...       
368          strcmp(ObjectData.Style,'polygon') ||strcmp(ObjectData.Style,'plane')||strcmp(ObjectData.Style,'volume')%  (isequal(ObjectData.Style,'polygon') & ~test_patch) |isequal(ObjectData.Style,'plane')
369        hh=line(xline,yline,'Color',col);
370        if ~strcmp(ObjectData.Style,'plane') && ~strcmp(ObjectData.Style,'volume')
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');
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    elseif strcmp(ObjectData.Style,'rectangle')
380        hh=rectangle('Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax],'EdgeColor',col);   
381    elseif strcmp(ObjectData.Style,'ellipse')
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
384        msgbox_uvmat('ERROR','unknown ObjectData.Style in plot_object.m')
385        return
386    end
387    set(hh,'Tag','proj_object')
388     if test_patch
389         hold on
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');
391       set(hhh,'AlphaData',(flag)*0.2)% set partial transparency to the filling color
392         PlotData.SubObject=hhh;   
393     end
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.