source: trunk/src/plot_object.m @ 19

Last change on this file since 19 was 19, checked in by gostiaux, 11 years ago

the private files have been moved down to the root folder

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