can someone explain me how the methode Matrix.CreatePerspectiveFieldOfView(fov, aspect, nearClip, farClip); is defined in XNA --> i mean the matrix behind it !
thanks you !
Assuming w, and h of:
h = cot(fieldOfViewY/2)
w = h * aspectRatio
This uses the following matrix:
w 0 0 0
0 h 0 0
0 0 farClip/(nearClip-farClip) -1
0 0 nearClip*farClip/(nearClip-farClip) 0
This isn't documented in the XNA docs, but is in the DX documentation.
Related
I want to make the grid of the smaller mesh, some idea of how it is done?
That is to say something like that the path in the x-axis and y are 0.25 in 0.25,
x = (0: 0.25: 7);
y = (0: 0.25: 7);
I've the following matrix. This matrix modeling the dynamic of a population (projection matrix)
L2=[0 0 0 0 127 4 80;
0.6747 0.7370 0 0 0 0 0;
0 0.0486 0.6610 0 0 0 0;
0 0 0.0147 0.6907 0 0 0;
0 0 0 0.0518 0 0 0;
0 0 0 0 0.8091 0 0;
0 0 0 0 0 0.8091 0.8089];
With this code, I find the eingenvectors rigth and left of L2. Likewise I find the sensivity and elasticity matrix.
A=L2;
[W,lambdas]=eig(A);
V=conj(inv(W));
lambdas=diag(lambdas);
[lambdas,I]=sort(lambdas);
lambdas=flipud(lambdas);
lambda1=lambdas(1);
I=flipud(I);
W=W(:,I);
V=V(I,:);
w=W(:,1);
w=w/sum(w);
v=real(V(1,:))';
v=v/v(1);
% matrix of sensitivity
senmat=v*w';
% matrix of elasticity
emat=senmat.*A/max(eig(A));
Then, I make a surface of sensivity matrix.
surf(senmat)
This is the result:
I need to make the squares (grid) of the surface smaller.
any ideas?
best regards!
YOu can use interp2 if you have (x,y) which are defined for senmat. Read about interp2. If you want only senmat to refine use imresize.
A = imresize(senmat,[100,100]) ;
surf(A)
I am trying to generate a rectangular matrix with 1s on the diagonal above the main diagonal and -1s on the main diagonal. I used "eye" which does not create the diagonal above the main.
Please find my attempt to this below.
N = 5
M1 = -eye([N-1 N])
M2 = eye([N N-1])'
M = M1+M2
I am unable to resolve this issue on my own. Any help or links to relevant documentation would be greatly appreciated.
I don't know of any prebuild function, but you can easily make such a matrix yourself:
N=5;
M=7;
diag=-eye(N,M);
upper_diag=horzcat(zeros(N,1),eye(N,M-1))
final=diag+upper_diag
using the identity matrix and some concatenation to shift the diagonal around. This example assumes you are looking for a square matrix.
The result looks like:
final =
-1 1 0 0 0 0 0
0 -1 1 0 0 0 0
0 0 -1 1 0 0 0
0 0 0 -1 1 0 0
0 0 0 0 -1 1 0
Just create eye and diag matrices as per normal, add them together, then chop away the rows you do not need:
nCol = 7;
nRow = 5;
M = -eye(nCol) + diag(ones(nCol - 1, 1), 1);
M = M(1:nRow, 1:nCol)
produces
M =
-1 1 0 0 0 0 0
0 -1 1 0 0 0 0
0 0 -1 1 0 0 0
0 0 0 -1 1 0 0
0 0 0 0 -1 1 0
The four-input version of spdiags does just that, producing a sparse matrix. You may need to convert to full then.
M = 5; %// number of rows
N = 7; %// number of columns
d = [0 1]; %// specify main diagonal and the one above
v = [-1 1]; %// values in those diagonals
result = full(spdiags(ones(M,1)*v, d, M, N));
This gives
result =
-1 1 0 0 0 0 0
0 -1 1 0 0 0 0
0 0 -1 1 0 0 0
0 0 0 -1 1 0 0
0 0 0 0 -1 1 0
Say I have a vector y like
2
4
3
10
and I want to obtain a matrix like
0 1 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
What's the simplest/cleanest/"best" way of doing this in octave/matlab? I came up with:
repmat(y, 1, 10) == repmat(1:10, 4, 1)
Anyone have a better approach than this? It makes sense to me after having thought about it but I feel like it's a bit hard to parse for someone reading the code (though idk, I'm relatively new to vectorization, and a lot of vectorized code feels like that to me).
If you want your result to be sparse you can simply
n = numel( y ); % number of rows in resulting matrix
k = max(y(:)); % if you do not know k (num of columns) in advance
U = sparse( 1:n, y, 1, n, k );
If you want your result to be a full matrix
U = zeros( n, k );
U( sub2ind([n k], 1:n, y ) ) = 1;
I want to a element-by-element binary operation apply to large logical vectors. The content of these vectors is manly false, so for performance considerations it better to work with sparse matrices. If i do so the resulting matrix is not correct.
Examble
A = logical([0;1;0;0]);
B = logical([0 0 1 1]);
C = bsxfun(#and,A,B)
In this case C is
C =
0 0 0 0
0 0 1 1
0 0 0 0
0 0 0 0
If i use sparse matrices C is
C = full(bsxfun(#and,sparse(A),sparse(B)))
C =
0 0 0 0
1 1 1 1
0 0 0 0
0 0 0 0
Which is obviously wrong.
Did i oversee something or is this a Matlab bug.
I can reproduce this so it certainly seems to be a MATLAB bug. Especially considering that:
C = full(bsxfun(#times,sparse(A),sparse(B)))
C =
0 0 0 0
0 0 1 1
0 0 0 0
0 0 0 0
So, I would report it to The Mathworks.
However, in this particular case, I can't help feeling that bsxfun with sparse matrices isn't going to be the most efficient. Consider the following:
A = sparse(logical([0;1;0;0]));
B = sparse(logical([0 0 1 1]));
C_bsxfun = bsxfun(#and,full(A),full(B));
[i j] = ndgrid(find(A), find(B));
C_sparse = sparse(i, j, true, numel(A), numel(B));
isequal(C_bsxfun, full(C_sparse))
Down I have edited my question.
I am projecting Wxyz to image(u,v) and getting some wrong result. I can't figure out where is mistake. Please help some one to figure out.
Given:
Wxyzz =
386.06 197.02 -1821.8 1
407.32 -108 -1859.1 1
4.9764 290.92 -1531.2 1
103.39 -233.07 -1643.2 1
189.7 320.19 -1667.9 1
76.279 147.9 -1597.2 1
47.798 -319.51 -1605.8 1
164.74 -223.1 -1688.6 1
482.06 -251.66 -1921.2 1
226.42 -213.27 -1733.6 1
Wxyz=Wxyz' %';
%# intrinsic
fu = 2640; fv =2640; uo= 1514; vo = 994;
A= [ fu 0 uo 0
0 fv vo 0
0 0 1 0
0 0 0 1];
Exterior=[-6.6861,12.6118,-8.0660,[-0.4467,-0.3168,0.2380]*pi/180];%# deg 2 rad
%#data
X0=Exterior(1);
Y0=Exterior(2);
Z0=Exterior(3);
ax=Exterior(4);
by=Exterior(5);
cz=Exterior(6);
%#Rotation in X
Rx = [1 0 0
0 cos(ax) -sin(ax)
0 sin(ax) cos(ax)];
%#Rotation in Y
Ry = [cos(by) 0 sin(by)
0 1 0
-sin(by) 0 cos(by)];
%#Rotation in Z
Rz = [cos(cz) -sin(cz) 0
sin(cz) cos(cz) 0
0 0 1];
R=Rx*Ry*Rz;
T=[X0;Y0;Z0];
Extrinsic= R';
Extrinsic(:,4) = -(R')* (T);
Extrinsic(4,:) = [0 0 0 1]
PImage = A* Extrinsic* Wxyz;
%#Obtain the X's
PImage(1 ,:) = PImage(1 ,:) ./ PImage(3 ,:);
%#Obtain the Y's
PImage(2 ,:) = PImage(2 ,:) ./ PImage(3 ,:);
I am getting
PImage =
955.93 707.43 -1814.4 1
939.75 1147.6 -1854.2 1
1506.3 490.36 -1521 1
1352.7 1370.2 -1637.6 1
1213.9 485.04 -1658.4 1
1389.7 749.08 -1588.5 1
1440.9 1521.6 -1600.6 1
1261.2 1344.2 -1683.2 1
857.22 1340 -1917.8 1
1173.9 1319.9 -1728.5 1
I supposed to get( real 2 D points)
img =
2072.8 706.69
2088.9 1146.9
1522.6 489.6
1676.1 1369.5
1814.9 484.3
1639.2 748.35
1587.9 1520.9
1767.6 1343.5
2171.4 1339.3
1854.9 1319.2
If I use
A= [ -fu 0 uo 0
0 fv vo 0
0 0 1 0
0 0 0 1];
I get corect answer.
I can't figure out where is mistake in rotation or translation; Why Pimage(:,1) is not identical with img(:,1).
Any help will be grateful.
You've done the rotation, and you've done the transformation.
What you need to do is the projection! Now I don't know what exactly you're trying to accomplish, but I don't see a projection matrix anywhere. So that's the first thing you need to fix.
Remember that doing a projection is a R4 -> R4 operation. If you're trying to get an R2 vector, you're trying to do something else (more).
see more at wikipedia.
if you really want to learn the details, you can watch and learn courtesy of YouTube