source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/extras/@lmi/plot.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 4.8 KB
Line 
1function x_opt = plot(varargin)
2%plot               plots feasible set
3%
4% p = plot(F,x,c,n,options)
5%
6% F:  set object
7% x:  projected variables [At most three variables]
8% c:  color [double] ([r g b] format)
9% n:  #vertices [double ]
10% options: options structure from sdpsettings
11
12
13% Author Johan Löfberg
14% $Id: plot.m,v 1.13 2006/04/25 11:22:57 joloef Exp $
15
16% Get the onstraints
17if nargin<1
18    return
19end
20
21F = varargin{1};
22
23if length(F)==0
24    return;
25end
26
27if nargin < 5
28    opts = sdpsettings('verbose',0);
29else
30    opts = varargin{5};
31    if isempty(opts)
32        opts = sdpsettings('verbose',0);
33    end
34end
35
36if nargin < 3
37    color = [1 0.1 0.1];
38else
39    color = varargin{3};
40    if isempty(color)
41        color = [1 0.1 0.1];
42    end
43end
44
45% Plot onto this projection (at most in 3D)
46if nargin < 2
47    x = [];
48else
49    x = varargin{2};
50    if ~isempty(x)
51        x = x(:);
52        x = x(1:min(3,length(x)));
53    end
54end
55
56if isempty(F)
57    return
58end
59
60% Create a model in YALMIPs low level format
61% All we change later is the cost vector
62%sol = solvesdp(F,sum(x),opts);
63[model,recoverdata,diagnostic,internalmodel] = export(F,[],opts,[],[],0);
64if isempty(internalmodel) | (~isempty(diagnostic) & diagnostic.problem)
65    error('Could not create model. Can you actually solve problems with this model?')
66end
67internalmodel.options.saveduals = 0;
68internalmodel.getsolvertime = 0;
69internalmodel.options.dimacs = 0;
70
71% Try to find a suitable set to plot
72if isempty(x)
73    if isempty(internalmodel.extended_variables)
74        x = depends(F);
75        x = x(1:min(3,length(x)));
76        localindex = 1;
77        localindex = find(ismember(recoverdata.used_variables,x));
78    else
79        % not extended variables
80        candidates = setdiff(1:length(internalmodel.c),internalmodel.extended_variables);
81        % Not nonlinear variables
82        candidates = candidates(find(internalmodel.variabletype(candidates)==0));
83        % Settle with this guess
84        localindex = candidates(1:min(3,length(candidates)));
85        x = localindex;
86    end
87else
88    localindex = [];
89    for i = 1:length(x)
90        localindex = [localindex find(ismember(recoverdata.used_variables,getvariables(x(i))))];
91    end
92end
93
94if nargin < 4
95    if length(x)==3
96        n = 100;
97    else
98        n = 25;
99    end
100else
101    n = varargin{4};
102    if isempty(n)
103        if length(x)==3
104            n = 100;
105        else
106            n = 25;
107        end
108    end
109    if ~isa(n,'double')
110        error('4th argument should be an integer>0');
111    end
112end
113
114
115x_opt = [];
116phi = [];
117status = 0;
118try % Try to ensure that we close h
119    if length(x)==2
120        mu =0.5;
121    else
122        mu=1;
123    end
124    n_ = n;
125    n = ceil(mu*n);
126    h = waitbar(0,['Please wait, solving ' num2str(n_) ' problems using ' internalmodel.solver.tag]);
127    angles = (0:(n))*2*pi/n;
128    if length(x)==2
129        c = [cos(angles);sin(angles)];
130    else
131        c = randn(3,n);
132    end
133    i=1;
134    while i<=n & status == 0
135        xi = solvefordirection(c(:,i),internalmodel,localindex);
136        x_opt = [x_opt xi];
137        waitbar(i/n_,h)
138        i=i+1;
139    end
140
141    if status==0 & length(x)==2
142        % Close the set
143        x_opt = [x_opt x_opt(:,1)];
144
145        % Add points adaptively
146        pick = 1;
147        n = floor((1-mu)*n_);
148        for i = 1:1:n
149            for j= 1:(size(x_opt,2)-1)
150                d = x_opt(:,j)-x_opt(:,j+1);
151                distance(j,1) = d'*d;
152            end
153            [dist,pos]=sort(-distance);
154            % Select insertion point
155            phii=(angles(pos(pick))+angles(pos(pick)+1))/2;
156            xi = solvefordirection([cos(phii);sin(phii)],internalmodel,localindex);
157            d1=xi-x_opt(:,pos(pick));
158            d2=xi-x_opt(:,pos(pick)+1);
159            if d1'*d1<1e-3 | d2'*d2<1e-3
160                pick = pick+1;
161            else
162                angles = [angles(1:pos(pick)) phii  angles((pos(pick))+1:end)];
163                x_opt = [x_opt(:,1:pos(pick)) xi  x_opt(:,(pos(pick))+1:end)];
164            end
165            waitbar((ceil(n_*mu)+i)/n_,h);
166        end
167    end
168    close(h);
169catch
170    close(h);
171end
172
173if status
174    if nargout==0
175        plot(0);
176    end
177end
178
179if nargout == 0 & status==0
180    if length(x)==2
181        patch(x_opt(1,:),x_opt(2,:),color);
182    else
183        K = convhulln(x_opt');
184        p = patch('Vertices', x_opt', 'Faces', K, 'FaceVertexCData', color, 'FaceColor', color);%, 'FaceAlpha', 0.5);
185        set(p,'LineStyle','none')
186        lighting gouraud;
187        view(3);
188        camlight('headlight','local');
189        camlight('headlight','local');
190        camlight('right','local');
191        camlight('left','local');
192    end
193end
194
195
196function [xout,status] = solvefordirection(c,internalmodel,uv)
197internalmodel.c = 0*internalmodel.c;
198internalmodel.c(uv) = c;
199sol  = feval(internalmodel.solver.call,internalmodel);
200xout = sol.Primal;
201xout = xout(uv(:));
202status = sol.problem;
203
204
205
206
Note: See TracBrowser for help on using the repository browser.