source: trunk/src/plot_object.m @ 156

Last change on this file since 156 was 156, checked in by sommeria, 13 years ago

many bug repairs and corrections for mouse action
create_grid: option for black marjkers for grid detection

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 % no object representation yet available
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    if isequal(ObjectData.Style,'polygon')
162        xline=[xline; ObjectData.Coord(1,1)];%closing the line
163        yline=[yline; ObjectData.Coord(1,2)];
164    elseif isequal(ObjectData.Style,'plane')| isequal(ObjectData.Style,'volume')
165        phi=ObjectData.Phi*pi/180;%angle in radians
166        Xend_x=xline(1)+XMax*cos(phi);
167        Xend_y=yline(1)+XMax*sin(phi);
168        Xbeg_x=xline(1)+XMin*cos(phi);
169        Xbeg_y=yline(1)+XMin*sin(phi);
170        Yend_x=xline(1)-YMax*sin(phi);
171        Yend_y=yline(1)+YMax*cos(phi);
172        Ybeg_x=xline(1)-YMin*sin(phi);
173        Ybeg_y=yline(1)+YMin*cos(phi);
174        xline=[Xbeg_x Xend_x NaN Ybeg_x Yend_x];
175        yline=[Xbeg_y Xend_y NaN Ybeg_y Yend_y];
176    end
177    SubLineStyle='none';%default
178    if isfield(ObjectData,'ProjMode')
179        if isequal(ObjectData.ProjMode,'projection')
180            SubLineStyle='--'; %range of projection marked by dash
181            if isfield (ObjectData,'DX')
182               rmfield(ObjectData,'DX');
183            end
184            if isfield (ObjectData,'DY')
185               rmfield(ObjectData,'DY');
186            end
187        elseif isequal(ObjectData.ProjMode,'filter')
188            SubLineStyle=':';%range of projection not visible
189        end
190    end
191    if isequal(ObjectData.Style,'line')|isequal(ObjectData.Style,'polyline')|isequal(ObjectData.Style,'polygon')
192        if length(xline)<2
193            theta=0;
194        else
195            theta=angle(diff(xline)+i*diff(yline));
196            theta(length(xline))=theta(length(xline)-1);
197        end
198        xsup(1)=xline(1)+YMax*sin(theta(1));
199        xinf(1)=xline(1)-YMax*sin(theta(1));
200        ysup(1)=yline(1)-YMax*cos(theta(1));
201        yinf(1)=yline(1)+YMax*cos(theta(1));
202        for ip=2:length(xline)
203            xsup(ip)=xline(ip)+YMax*sin((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
204            xinf(ip)=xline(ip)-YMax*sin((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
205            ysup(ip)=yline(ip)-YMax*cos((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
206            yinf(ip)=yline(ip)+YMax*cos((theta(ip)+theta(ip-1))/2)/cos((theta(ip-1)-theta(ip))/2);
207        end
208    end
209end
210
211%% shading image
212if test_patch
213    npMx=512;
214    npMy=512; 
215    flag=zeros(npMy,npMx);
216    if isequal(ObjectData.Style,'ellipse')
217        XimaMin=ObjectData.Coord(1,1)-XMax;
218        XimaMax=ObjectData.Coord(1,1)+XMax;
219        YimaMin=ObjectData.Coord(1,2)-YMax;
220        YimaMax=ObjectData.Coord(1,2)+YMax;
221        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];%create an image around the ellipse
222        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
223        scale_x=2*1.4*XMax/npMx;
224        scale_y=2*1.4*YMax/npMy;
225        xi=[0.5:npMx-0.5]*scale_x+xlim(1);
226        yi=[0.5:npMy-0.5]*scale_y+ylim(1);
227        [Xi,Yi]=meshgrid(xi,yi);
228        X2Max=XMax*XMax;
229        Y2Max=YMax*YMax;
230        distX=(Xi-ObjectData.Coord(1,1));
231        distY=(Yi-ObjectData.Coord(1,2));
232        flag=(distX.*distX/X2Max+distY.*distY/Y2Max)<1;
233    elseif isequal(ObjectData.Style,'rectangle')|isequal(ObjectData.Style,'volume')
234        XimaMin=ObjectData.Coord(1,1)-XMax;
235        XimaMax=ObjectData.Coord(1,1)+XMax;
236        YimaMin=ObjectData.Coord(1,2)-YMax;
237        YimaMax=ObjectData.Coord(1,2)+YMax;
238        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];%create an image around the ellipse
239        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
240        scale_x=2*1.4*XMax/npMx;
241        scale_y=2*1.4*YMax/npMy;
242        xi=[0.5:npMx-0.5]*scale_x+xlim(1);
243        yi=[0.5:npMy-0.5]*scale_y+ylim(1);
244        [Xi,Yi]=meshgrid(xi,yi);
245        distX=abs(Xi-ObjectData.Coord(1,1));
246        distY=abs(Yi-ObjectData.Coord(1,2));
247        flag=distX<XMax & distY< YMax;
248    elseif isequal(ObjectData.Style,'polygon')
249        XimaMin=min(ObjectData.Coord(:,1));
250        XimaMax=max(ObjectData.Coord(:,1));
251        YimaMin=min(ObjectData.Coord(:,2));
252        YimaMax=max(ObjectData.Coord(:,2));
253        xlim=[1.2*XimaMin-0.2*XimaMax 1.2*XimaMax-0.2*XimaMin];
254        ylim=[1.2*YimaMin-0.2*YimaMax 1.2*YimaMax-0.2*YimaMin];
255        [Xlim,Ylim]=meshgrid(linspace(xlim(1),xlim(2),npMx),linspace(ylim(1),ylim(2),npMy));
256        %flag=roipoly(xlim,ylim,flag,ObjectData.Coord(:,1),ObjectData.Coord(:,2));%=1 inside the polygon, 0 outsid
257        flag=inpolygon(Xlim,Ylim,ObjectData.Coord(:,1),ObjectData.Coord(:,2));%=1 inside the polygon, 0 outsid
258    end
259    if isequal(ObjectData.ProjMode,'outside')||isequal(ObjectData.ProjMode,'mask_outside')
260        flag=~flag;
261    end
262    imflag=zeros(npMx,npMy,3);
263    imflag(:,:,3)=flag; % blue color
264    if isequal(col,'m')
265         imflag(:,:,1)=flag; % magenta color
266    end
267    dx=(xlim(2)-xlim(1))/npMx;
268    dy=(ylim(2)-ylim(1))/npMy;
269end
270
271PlotData=[];%default
272%% MODIFY AN EXISTING OBJECT PLOT
273if test_newobj==0;
274    hh=hplot;
275    PlotData=get(hplot,'UserData');           
276    if test_line
277        set(hplot,'XData',xline)
278        set(hplot,'YData',yline)
279    %modify subobjects
280        if isfield(PlotData,'SubObject')
281           if length(PlotData.SubObject)==2 && ~isequal(ObjectData.Style,'points')&& ~isequal(ObjectData.Style,'plane');
282                set(PlotData.SubObject(1),'XData',xinf);
283                set(PlotData.SubObject(1),'YData',yinf);
284                set(PlotData.SubObject(2),'XData',xsup);
285                set(PlotData.SubObject(2),'YData',ysup);
286           elseif isequal(ObjectData.Style,'points')&& ~isequal(YMax,0)
287               for ipt=1:min(length(PlotData.SubObject),size(ObjectData.Coord,1))
288                    set(PlotData.SubObject(ipt),'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax])
289               end
290               %complement missing points
291               if size(ObjectData.Coord,1)>length(PlotData.SubObject)
292                   for ipt=length(PlotData.SubObject)+1:size(ObjectData.Coord,1)
293                     PlotData.SubObject(ipt)=rectangle('Curvature',[1 1],...
294                  'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax],'EdgeColor',col,...
295                  'LineStyle',SubLineStyle,'Tag','proj_object');
296                   end
297               end                                         
298           end
299        end
300        if isfield(PlotData,'DeformPoint')
301           for ipt=1:length(PlotData.DeformPoint)
302               if ishandle(PlotData.DeformPoint(ipt))
303                   if length(xline)>=ipt &   length(yline)>=ipt   
304                        set(PlotData.DeformPoint(ipt),'XData',xline(ipt),'YData',yline(ipt));
305                    end
306               end
307           end
308           if length(xline)>length(PlotData.DeformPoint)
309               for ipt=length(PlotData.DeformPoint)+1:length(xline)
310                    PlotData.DeformPoint(ipt)=line(xline(ipt),yline(ipt),'Color',col,'LineStyle','.','Tag','DeformPoint',...
311                        'SelectionHighlight','off','UserData',hplot);
312               end
313               set(hplot,'UserData',PlotData)
314           end
315        end
316    elseif isequal(ObjectData.Style,'rectangle')|isequal(ObjectData.Style,'ellipse')
317        set(hplot,'Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax])         
318    end
319    if test_patch
320        for iobj=1:length(PlotData.SubObject)
321            objtype=get(PlotData.SubObject(iobj),'Type');
322            if isequal(objtype,'image')
323                set(PlotData.SubObject(iobj),'CData',imflag,'AlphaData',(flag)*0.2)
324                set(PlotData.SubObject(iobj),'XData',[xlim(1)+dx/2 xlim(2)-dx/2])
325                set(PlotData.SubObject(iobj),'YData',[ylim(1)+dy/2 ylim(2)-dy/2])
326            end
327        end
328    end
329end
330
331%% create the object
332if test_newobj
333    axes(haxes)
334    hother=findobj('Tag','proj_object');%find all the proj objects
335    for iobj=1:length(hother)
336        if isequal(get(hother(iobj),'Type'),'rectangle')|isequal(get(hother(iobj),'Type'),'patch')
337            set(hother(iobj),'EdgeColor','b')
338            if isequal(get(hother(iobj),'FaceColor'),'m')
339                set(hother(iobj),'FaceColor','b')
340            end
341        elseif isequal(get(hother(iobj),'Type'),'image')
342               Acolor=get(hother(iobj),'CData');
343               Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
344               set(hother(iobj),'CData',Acolor);
345        else
346             set(hother(iobj),'Color','b')
347        end
348        set(hother(iobj),'Selected','off')
349    end
350    hother=findobj('Tag','DeformPoint');
351    set(hother,'Color','b');
352    set(hother,'Selected','off') 
353    if isequal(ObjectData.Style,'points')
354        hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color',col,'LineStyle','.','Marker','+');
355        for ipt=1:length(xline)
356              PlotData.DeformPoint(ipt)=line(ObjectData.Coord(ipt,1),ObjectData.Coord(ipt,2),'Color',...
357                  col,'LineStyle','.','SelectionHighlight','off','UserData',hh,'Tag','DeformPoint');
358              %create circle around each point
359              if ~isequal(YMax,0)
360                 PlotData.SubObject(ipt)=rectangle('Curvature',[1 1],...
361                  'Position',[ObjectData.Coord(ipt,1)-YMax ObjectData.Coord(ipt,2)-YMax 2*YMax 2*YMax],'EdgeColor',col,...
362                  'LineStyle',SubLineStyle,'Tag','proj_object');
363              end
364        end
365    elseif  isequal(ObjectData.Style,'line')|isequal(ObjectData.Style,'polyline')|...       
366          isequal(ObjectData.Style,'polygon') |isequal(ObjectData.Style,'plane')|isequal(ObjectData.Style,'volume')%  (isequal(ObjectData.Style,'polygon') & ~test_patch) |isequal(ObjectData.Style,'plane')
367        hh=line(xline,yline,'Color',col);
368        if ~isequal(ObjectData.Style,'plane')& ~isequal(ObjectData.Style,'volume')
369            for ipt=1:sizcoord(1)
370                PlotData.DeformPoint(ipt)=line(ObjectData.Coord(ipt,1),ObjectData.Coord(ipt,2),'Color',...
371                      col,'LineStyle','none','Marker','.','Tag','DeformPoint','SelectionHighlight','off','UserData',hh);
372            end
373            PlotData.SubObject(1)=line(xinf,yinf,'Color',col,'LineStyle',SubLineStyle,'Tag','proj_object');%draw sub-lines
374            PlotData.SubObject(2)=line(xsup,ysup,'Color',col,'LineStyle',SubLineStyle,'Tag','proj_object');
375        end
376   
377    elseif isequal(ObjectData.Style,'rectangle')
378        hh=rectangle('Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax],'EdgeColor',col);   
379    elseif isequal(ObjectData.Style,'ellipse')
380        hh=rectangle('Curvature',[1 1],'Position',[ObjectData.Coord(1,1)-XMax ObjectData.Coord(1,2)-YMax 2*XMax 2*YMax],'EdgeColor',col);
381    else
382        msgbox_uvmat('ERROR','unknown ObjectData.Style in plot_object.m')
383        return
384    end
385    set(hh,'Tag','proj_object')
386    if test_patch
387        hold on
388        hhh=image([xlim(1)+dx/2 xlim(2)-dx/2],[ylim(1)+dy/2 ylim(2)-dy/2],imflag,'Tag','proj_object','HitTest','off');
389        set(hhh,'AlphaData',(flag)*0.2)
390        PlotData.SubObject=hhh;   
391    end
392    if isfield(PlotData,'SubObject')
393        set(PlotData.SubObject,'UserData',hh)%record the parent handles in the SubObjects
394    end
395    if isfield(PlotData,'DeformPoint')
396        for ipt=1:sizcoord(1)
397            set(PlotData.DeformPoint(ipt),'UserData',hh);%record the parent handles in the SubObjects
398        end
399        set(PlotData.DeformPoint,'UserData',hh)%record the parent handles in the SubObjects
400    end
401end
402set(hh,'UserData',PlotData)
Note: See TracBrowser for help on using the repository browser.