Would like to generate surface of revolution from bezier curve - matlab

I would like to generate surface of revolution from bezier curve. I have made bezier curve in MATLAB but beyond this point I am stuck and do not know how to proceed. Please help.
Below is the code that I have made.
clc
clear
close all
% Name : Savla Jinesh Shantilal
% Bits ID : 2021HT30609
%% Define inout parameters
B = [1,1; 2,3; 4,3; 3,1]; % Creating matrix for polygon vertices
[r,s] = size(B); % getting size of matrix in terms of rows and columns
n = r-1; % n+1 represents number of vertices of the polygon
np = 20; % represents number of equi-distance points on the bezier curve
t = linspace(0,1,np);
%% Plot polygon
for k = 1:n
plot([B(k,1),B(k+1,1)], [B(k,2),B(k+1,2)], 'r', 'LineWidth', 2)
hold on
grid on
end
%% Generate the points on the bezier curve
for j = 1:np
P = [0,0];
for i = 0:n
M(i+1) = (factorial(n)/(factorial(i)*factorial(n-i)))*((t(j))^i)*((1-t(j))^(n-i));
P = P + B(i+1,:)*M(i+1);
end
Q(j,:) = P;
end
%% Plot the bezier curve from the obtained points
for l = 1:np-1
plot([Q(l,1),Q(l+1,1)],[Q(l,2),Q(l+1,2)], '-- b', 'LineWidth', 2);
hold on
end

Usually one can use the built-in cylinder function for monotonically increasing x-values. Here, the bezier curve has non monotonic values from max(x) so we break it to two parts to parameterize it, and then add an angle rotation.
% first define the x and y coordinate from your Q info:
xx = Q(:,1);
yy = Q(:,2);
N = 1e2;
[~, id] = max(xx); % the position where we split
t = linspace(xx(1),xx(id),N);
% Parameterize the function:
t = linspace(0,2*(xx(id)-xx(1)),N);
x = zeros(1, N);
L = t <= ( xx(id)-xx(1) ); % the "Left" side of the curve
x(L) = t(L)+xx(1);
x(~L) = flip(x(L));
%define the r value
r = x;
r(L) = interp1(xx(1:id) ,yy(1:id) , x(L) ); % Left side
r(~L) = interp1(xx(id:end),yy(id:end), x(~L)); % right side (not left)
% define your x values
x = repmat(x', [1 N]);
% define the theta that will perform the rotation
theta = linspace(0,2*pi, N);
% initialize values for y and z
y = zeros(N);
z = zeros(N);
% calculate the y and z values
for i=1:N
y(i,:) = r(i) *cos(theta);
z(i,:) = r(i) *sin(theta);
end
%plot the surface of revolution and the original curve
s = surf(x,y,z);
alpha 0.4
hold on
plot(xx,yy,'k','LineWidth',3)

Related

How to generate a dome by using points in MATLAB

I am trying to generate a semi ellipsoidal dome shape by using x, y, z values. In my code below, I define the x,y,z values but I am unable to assign those values to sphere.
How do I resolve this?
clc
x = [65 55.2125 50.8267 46.7398 42.9232 39.3476 35.9815 32.7882 29.7175 26.6833 23.4690 18.7605];
y = x;
z = [0.0,0.9,2.7,5.2,8.2,11.8,15.8,20.3,25.2,30.7,37.1,47.5]; % max height of dome is 47.5
[x,y,z] = sphere(20);
x = x(12:end,:);
y = y(12:end,:);
z = z(12:end,:);
r = 65; % radius of the dome
surf(r.*x,r.*y,r.*z);
axis equal;
It will be simpler or at least more elegant to use the parametric equations of an ellipsoide.
% semi axis parameters
a = 65; % x-axis
b = 65; % y-axis
c = 47.5; % z-axis
%% Parametrisation
%
% To reach each point of the ellipsoide we need two angle:
% phi ∈ [0,2𝜋]
% theta ∈ [0, 𝜋]
%
% But since we only need half of an ellipsoide we can set
% theta ∈ [0,𝜋/2]
[theta,phi] = ndgrid(linspace(0,pi/2,50),linspace(0,2*pi,50));
x = a*sin(theta).*cos(phi);
y = b*sin(theta).*sin(phi);
z = c*cos(theta);
%plot
surf(x,y,z)
axis equal
Result:
You can remove the bottom half of the sphere by assigning NaNs to the approptiate elements of x and y:
[x,y,z] = sphere(20);
I = (z<0);
x(I) = NaN;
y(I) = NaN;
surf(x,y,z)

Ordered Isoline Calculation from 3D Triangular Surface in MATLAB

I need to extract the isoline coordinates of a 4D variable from a 3D surface defined using a triangulated mesh in MATLAB. I need the isoline coordinates to be a ordered in such a manner that if they were followed in order they would trace the path i.e. the order of the points a 3D printer would follow.
I have found a function that can calculate the coordinates of these isolines (see Isoline function here) but the problem is this function does not consider the isolines to be joined in the correct order and is instead a series of 2 points separated by a Nan value. This makes this function only suitable for visualisation purposes and not the path to follow.
Here is a MWE of the problem of a simplified problem, the surface I'm applying it too is much more complex and I cannot share it. Where x, y and z are nodes, with TRI providing the element connectivity list and v is the variable of which I want the isolines extracted from and is not equal to z.
If anyone has any idea on either.....
A function to extract isoline values in the correct order for a 3D tri mesh.
How to sort the data given by the function Isoline so that they are in the correct order.
.... it would be very much appreciated.
Here is the MWE,
% Create coordinates
[x y] = meshgrid( -10:0.5:10, -10:0.5:10 );
z = (x.^2 + y.^2)/20; % Z height
v = x+y; % 4th dimension value
% Reshape coordinates into list to be converted to tri mesh
x = reshape(x,[],1); y = reshape(y,[],1); z = reshape(z,[],1); v = reshape(v,[],1);
TRI = delaunay(x,y); % Convertion to a tri mesh
% This function calculates the isoline coordinates
[xTows, yTows, zTows] = IsoLine( {TRI,[x, y, z]}, v, -18:2:18);
% Plotting
figure(1); clf(1)
subplot(1,2,1)
trisurf(TRI,x,y,z,v)
hold on
for i = 1:size(xTows,1)
plot3( xTows{i,1}, yTows{i,1}, zTows{i,1}, '-k')
end
hold off
shading interp
xlabel('x'); ylabel('y'); zlabel('z'); title('Isolines'), axis equal
%% This section is solely to show that the isolines are not in order
for i = 1:size(xTows,1)
% Arranging data into colums and getting rid of Nans that appear
xb = xTows{i,1}; yb = yTows{i,1}; zb = zTows{i,1};
xb = reshape(xb, 3, [])'; xb(:,3) = [];
yb = reshape(yb, 3, [])'; yb(:,3) = [];
zb = reshape(zb, 3, [])'; zb(:,3) = [];
subplot(1,2,2)
trisurf(TRI,x,y,z,v)
shading interp
view(2)
xlabel('x'); ylabel('y'); zlabel('z'); title('Plotting Isolines in Order')
axis equal; axis tight; hold on
for i = 1:size(xb,1)
plot3( [xb(i,1) xb(i,2)], [yb(i,1) yb(i,2)], [zb(i,1) zb(i,2)], '-k')
drawnow
end
end
and here is the function Isoline, which I have slightly adpated.
function [xTows, yTows, zTows] = IsoLine(Surf,F,V,Col)
if length(Surf)==3 % convert mesh to triangulation
P = [Surf{1}(:) Surf{2}(:) Surf{3}(:)];
Surf{1}(end,:) = 1i;
Surf{1}(:,end) = 1i;
i = find(~imag(Surf{1}(:)));
n = size(Surf{1},1);
T = [i i+1 i+n; i+1 i+n+1 i+n];
else
T = Surf{1};
P = Surf{2};
end
f = F(T(:));
if nargin==2
V = linspace(min(f),max(f),22);
V = V(2:end-1);
elseif numel(V)==1
V = linspace(min(f),max(f),V+2);
V = V(2:end-1);
end
if nargin<4
Col = 'k';
end
H = NaN + V(:);
q = [1:3 1:3];
% -------------------------------------------------------------------------
% Loop over iso-values ----------------------------------------------------
xTows = [];
yTows = [];
zTows = [];
for k = 1:numel(V)
R = {[],[]};
G = F(T) - V(k);
C = 1./(1-G./G(:,[2 3 1]));
f = unique(T(~isfinite(C))); % remove degeneracies by random perturbation
F(f) = F(f).*(1+1e-12*rand(size(F(f)))) + 1e-12*rand(size(F(f)));
G = F(T) - V(k);
C = 1./(1-G./G(:,[2 3 1]));
C(C<0|C>1) = -1;
% process active triangles
for i = 1:3
f = any(C>=0,2) & C(:,i)<0;
for j = i+1:i+2
w = C(f,q([j j j]));
R{j-i} = [R{j-i}; w.*P(T(f,q(j)),:)+(1-w).*P(T(f,q(j+1)),:)];
end
end
% define isoline
for i = 1:3
X{i} = [R{1}(:,i) R{2}(:,i) nan+R{1}(:,i)]';
% X{i} = [R{1}(:,i) R{2}(:,i)]'; % Changed by Matt
X{i} = X{i}(:)';
end
% plot isoline
if ~isempty(R{1})
% hold on
% H(k) = plot3(X{1},X{2},X{3},Col);
% Added by M.Thomas
xTows{k,1} = X{1};
yTows{k,1} = X{2};
zTows{k,1} = X{3};
end
end
What you will notice is that the isolines (xTows, yTows and zTows) are not in order there "jump around" when plotted sequentially. I need to sort the tows so that they give a smooth plot in order.

Create random unit vector inside a defined conical region

I'm looking for a simple way for creating a random unit vector constrained by a conical region. The origin is always the [0,0,0].
My solution up to now:
function v = GetRandomVectorInsideCone(coneDir,coneAngleDegree)
coneDir = normc(coneDir);
ang = coneAngleDegree + 1;
while ang > coneAngleDegree
v = [randn(1); randn(1); randn(1)];
v = v + coneDir;
v = normc(v);
ang = atan2(norm(cross(v,coneDir)), dot(v,coneDir))*180/pi;
end
My code loops until the random generated unit vector is inside the defined cone. Is there a better way to do that?
Resultant image from test code bellow
Resultant frequency distribution using Ahmed Fasih code (in comments).
I wonder how to get a rectangular or normal distribution.
c = [1;1;1]; angs = arrayfun(#(i) subspace(c, GetRandomVectorInsideCone(c, 30)), 1:1e5) * 180/pi; figure(); hist(angs, 50);
Testing code:
clearvars; clc; close all;
coneDir = [randn(1); randn(1); randn(1)];
coneDir = [0 0 1]';
coneDir = normc(coneDir);
coneAngle = 45;
N = 1000;
vAngles = zeros(N,1);
vs = zeros(3,N);
for i=1:N
vs(:,i) = GetRandomVectorInsideCone(coneDir,coneAngle);
vAngles(i) = subspace(vs(:,i),coneDir)*180/pi;
end
maxAngle = max(vAngles);
minAngle = min(vAngles);
meanAngle = mean(vAngles);
AngleStd = std(vAngles);
fprintf('v angle\n');
fprintf('Direction: [%.3f %.3f %.3f]^T. Angle: %.2fº\n',coneDir,coneAngle);
fprintf('Min: %.2fº. Max: %.2fº\n',minAngle,maxAngle);
fprintf('Mean: %.2fº\n',meanAngle);
fprintf('Standard Dev: %.2fº\n',AngleStd);
%% Plot
figure;
grid on;
rotate3d on;
axis equal;
axis vis3d;
axis tight;
hold on;
xlabel('X'); ylabel('Y'); zlabel('Z');
% Plot all vectors
p1 = [0 0 0]';
for i=1:N
p2 = vs(:,i);
plot3ex(p1,p2);
end
% Trying to plot the limiting cone, but no success here :(
% k = [0 1];
% [X,Y,Z] = cylinder([0 1 0]');
% testsubject = surf(X,Y,Z);
% set(testsubject,'FaceAlpha',0.5)
% N = 50;
% r = linspace(0, 1, N);
% [X,Y,Z] = cylinder(r, N);
%
% h = surf(X, Y, Z);
%
% rotate(h, [1 1 0], 90);
plot3ex.m:
function p = plot3ex(varargin)
% Plots a line from each p1 to each p2.
% Inputs:
% p1 3xN
% p2 3xN
% args plot3 configuration string
% NOTE: p1 and p2 number of points can range from 1 to N
% but if the number of points are different, one must be 1!
% PVB 2016
p1 = varargin{1};
p2 = varargin{2};
extraArgs = varargin(3:end);
N1 = size(p1,2);
N2 = size(p2,2);
N = N1;
if N1 == 1 && N2 > 1
N = N2;
elseif N1 > 1 && N2 == 1
N = N1
elseif N1 ~= N2
error('if size(p1,2) ~= size(p1,2): size(p1,2) and/or size(p1,2) must be 1 !');
end
for i=1:N
i1 = i;
i2 = i;
if i > N1
i1 = N1;
end
if i > N2
i2 = N2;
end
x = [p1(1,i1) p2(1,i2)];
y = [p1(2,i1) p2(2,i2)];
z = [p1(3,i1) p2(3,i2)];
p = plot3(x,y,z,extraArgs{:});
end
Here’s the solution. It’s based on the wonderful answer at https://math.stackexchange.com/a/205589/81266. I found this answer by googling “random points on spherical cap”, after I learned on Mathworld that a spherical cap is this cut of a 3-sphere with a plane.
Here’s the function:
function r = randSphericalCap(coneAngleDegree, coneDir, N, RNG)
if ~exist('coneDir', 'var') || isempty(coneDir)
coneDir = [0;0;1];
end
if ~exist('N', 'var') || isempty(N)
N = 1;
end
if ~exist('RNG', 'var') || isempty(RNG)
RNG = RandStream.getGlobalStream();
end
coneAngle = coneAngleDegree * pi/180;
% Generate points on the spherical cap around the north pole [1].
% [1] See https://math.stackexchange.com/a/205589/81266
z = RNG.rand(1, N) * (1 - cos(coneAngle)) + cos(coneAngle);
phi = RNG.rand(1, N) * 2 * pi;
x = sqrt(1-z.^2).*cos(phi);
y = sqrt(1-z.^2).*sin(phi);
% If the spherical cap is centered around the north pole, we're done.
if all(coneDir(:) == [0;0;1])
r = [x; y; z];
return;
end
% Find the rotation axis `u` and rotation angle `rot` [1]
u = normc(cross([0;0;1], normc(coneDir)));
rot = acos(dot(normc(coneDir), [0;0;1]));
% Convert rotation axis and angle to 3x3 rotation matrix [2]
% [2] See https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
crossMatrix = #(x,y,z) [0 -z y; z 0 -x; -y x 0];
R = cos(rot) * eye(3) + sin(rot) * crossMatrix(u(1), u(2), u(3)) + (1-cos(rot))*(u * u');
% Rotate [x; y; z] from north pole to `coneDir`.
r = R * [x; y; z];
end
function y = normc(x)
y = bsxfun(#rdivide, x, sqrt(sum(x.^2)));
end
This code just implements joriki’s answer on math.stackexchange, filling in all the details that joriki omitted.
Here’s a script that shows how to use it.
clearvars
coneDir = [1;1;1];
coneAngleDegree = 30;
N = 1e4;
sol = randSphericalCap(coneAngleDegree, coneDir, N);
figure;plot3(sol(1,:), sol(2,:), sol(3,:), 'b.', 0,0,0,'rx');
grid
xlabel('x'); ylabel('y'); zlabel('z')
legend('random points','origin','location','best')
title('Final random points on spherical cap')
Here is a 3D plot of 10'000 points from the 30° spherical cap centered around the [1; 1; 1] vector:
Here’s 120° spherical cap:
Now, if you look at the histogram of the angles between these random points at the coneDir = [1;1;1], you will see that the distribution is skewed. Here’s the distribution:
Code to generate this:
normc = #(x) bsxfun(#rdivide, x, sqrt(sum(x.^2)));
mysubspace = #(a,b) real(acos(sum(bsxfun(#times, normc(a), normc(b)))));
angs = arrayfun(#(i) mysubspace(coneDir, sol(:,i)), 1:N) * 180/pi;
nBins = 16;
[n, edges] = histcounts(angs, nBins);
centers = diff(edges(1:2))*[0:(length(n)-1)] + mean(edges(1:2));
figure('color','white');
bar(centers, n);
xlabel('Angle (degrees)')
ylabel('Frequency')
title(sprintf('Histogram of angles between coneDir and random points: %d deg', coneAngleDegree))
Well, this makes sense! If you generate points from the 120° spherical cap around coneDir, of course the 1° cap is going to have very few of those samples, whereas the strip between the 10° and 11° caps will have far more points. So what we want to do is normalize the number of points at a given angle by the surface area of the spherical cap at that angle.
Here’s a function that gives us the surface area of the spherical cap with radius R and angle in radians theta (equation 16 on Mathworld’s spherical cap article):
rThetaToH = #(R, theta) R * (1 - cos(theta));
rThetaToS = #(R, theta) 2 * pi * R * rThetaToH(R, theta);
Then, we can normalize the histogram count for each bin (n above) by the difference in surface area of the spherical caps at the bin’s edges:
figure('color','white');
bar(centers, n ./ diff(rThetaToS(1, edges * pi/180)))
The figure:
This tells us “the number of random vectors divided by the surface area of the spherical segment between histogram bin edges”. This is uniform!
(N.B. If you do this normalized histogram for the vectors generated by your original code, using rejection sampling, the same holds: the normalized histogram is uniform. It’s just that rejection sampling is expensive compared to this.)
(N.B. 2: note that the naive way of picking random points on a sphere—by first generating azimuth/elevation angles and then converting these spherical coordinates to Cartesian coordinates—is no good because it bunches points near the poles: Mathworld, example, example 2. One way to pick points on the entire sphere is sampling from the 3D normal distribution: that way you won’t get bunching near poles. So I believe that your original technique is perfectly appropriate, giving you nice, evenly-distributed points on the sphere without any bunching. This algorithm described above also does the “right thing” and should avoid bunching. Carefully evaluate any proposed algorithms to ensure that the bunching-near-poles problem is avoided.)
it is better to use spherical coordinates and convert it to cartesian coordinates:
coneDirtheta = rand(1) * 2 * pi;
coneDirphi = rand(1) * pi;
coneAngle = 45;
N = 1000;
%perfom transformation preventing concentration of points around the pole
rpolar = acos(cos(coneAngle/2*pi/180) + (1-cos(coneAngle/2*pi/180)) * rand(N, 1));
thetapolar = rand(N,1) * 2 * pi;
x0 = rpolar .* cos(thetapolar);
y0 = rpolar .* sin(thetapolar);
theta = coneDirtheta + x0;
phi = coneDirphi + y0;
r = rand(N, 1);
x = r .* cos(theta) .* sin(phi);
y = r .* sin(theta) .* sin(phi);
z = r .* cos(phi);
scatter3(x,y,z)
if all points should be of length 1 set r = ones(N,1);
Edit:
since intersection of cone with sphere forms a circle first we create random points inside a circle with raduis of (45 / 2) in polar coordinates and as #Ahmed Fasih commented to prevent concentration of points near the pole we should first transform this random points, then convert polar to cartesian 2D coordinates to form x0 and y0
we can use x0 and y0 as phi & theta angle of spherical coordinates and add coneDirtheta & coneDirphi as offsets to these coordinates.
then convert spherical to cartesian 3D coordinates

Bending a plane into a conical surface

I am currently trying to compute Origami structures on Matlab, and I am looking for a method to bend my crease patterns on a conical surface, in the same way that this answer : Bending a plane into a closed surface/cylinder into a cylinder.
How can I do this, please ?
Cheers,
You should just have to map an (x,y) coordinate into a 3D coordinate for a conical surface. So to do this, it is important to parametrize a conical surface as a function of two variables. Thus:
( r, theta, z ) = ( a * z + c, theta, z ) for some a,c you define
Then you just need to create a relationship between (x,y) and (theta,z) so you can find a given (theta,z) as a function of (x,y). Then it comes down to a simple set of for loops to iterate through the (x,y) points to find the mapped coordinates on the conical surface.
=== Edit ===
So I wrote some codes to illustrate how simple the mapping can be. First, here are some images. The first is the planar mesh that will be mapped, and the second image is the result.
% This is file: gen_mesh.m
function [x, y, tri_mesh] = gen_mesh()
% Initialize output coordinates of points that make up the mesh
y = [];
x = [];
% Initialize mesh
tri_mesh= [];
% Number of points in the x dimension
Nx = 5;
% Number of points in the y dimension
Ny = 17;
% For this mesh, make each row have a slightly
% different number of points
x1 = linspace(0,1,Nx);
dx = x1(2)-x1(1);
x2 = linspace(dx/2, 1-dx/2, Nx-1);
% Create the array with the y values
y1 = linspace(0,1,Ny);
%% Generate the associated (x,y) pairs
for iy = 1:Ny
if( mod(iy,2) == 0 )
y = [y, ones(1,Nx-1)*y1(iy)];
x = [x, x2];
else
y = [y, ones(1,Nx)*y1(iy)];
x = [x, x1];
end
end
%% Generate the Mesh of triangles
% Make sure that the mesh wraps to each
% opposite x bound. This is to make sure that
% the cyclic nature of the conical surface
% doesnt mess up the look of the mesh
count = 1;
curr = 1;
ol = [];
el = [];
for iy = 1:Ny
if( mod(iy, 2) == 0 )
el.x = x(curr:curr+(Nx-2));
el.y = y(curr:curr+(Nx-2));
else
ol.x = x(curr:curr + Nx - 1);
ol.y = y(curr:curr + Nx - 1);
end
if( iy ~= 1 )
for i = 2:Nx
tri_mesh(count).x = [ol.x(i), el.x(i-1), ol.x(i-1)];
tri_mesh(count).y = [ol.y(i), el.y(i-1), ol.y(i-1)];
count = count + 1;
end
for i = 2:(Nx-1)
tri_mesh(count).x = [el.x(i), ol.x(i), el.x(i-1)];
tri_mesh(count).y = [el.y(i), ol.y(i), el.y(i-1)];
count = count + 1;
end
tri_mesh(count).x = [el.x(1), ol.x(1), el.x(end)];
tri_mesh(count).y = [el.y(1), ol.y(1), el.y(end)];
count = count + 1;
end
if( mod(iy, 2) == 0 )
curr = curr + (Nx-1);
else
curr = curr + Nx;
end
end
end
% This is file: map_2D_to_3DCone.m
function [xh, yh, z] = map_2D_to_3DCone( x, x_rng, y, y_rng )
% x: an array of x values part of a planar coordinate
%
% x_rng: the smallest and largest possible x values in the planar domain
% -> x_rng = [min_x, max_x]
%
% y: an array of y values part of a planar coordinate
%
% y_rng: the smallest and largest possible y values in the planar domain
% -> y_rng = [min_y, max_y]
% The bottom z (height) value
zb = 0;
% The top z value
zt = 1;
% The radius value at z = zb
rb = 3;
% The radius value at z = zt
rt = 1;
%% Obtain the Conical Surface 3D coordinates
% Find z as a function of y in the planar domain
% This mapping is a simple 1-D Lagrange interpolation
z = (zt*( y - y_rng(1) ) - zb*( y - y_rng(2) ))/(diff(y_rng));
% Find the parametrized angle as a function of x
% using a 1D Lagrange interpolation
theta = 2*pi*( x - x_rng(1) )/(diff(x_rng));
% Find the radius as a function of z using
% a simple 1D legrange interpolation
r = ( rt*(z - zb) - rb*(z - zt) )/( zt - zb );
% Find the absolute x and y components of the
% 3D conical coordinates
xh = r.*cos(theta);
yh = r.*sin(theta);
end
% This is in file: PlaneMesh_to_ConicalMesh.m
function mesh3d = PlaneMesh_to_ConicalMesh( mesh2d )
% Generate the 3D version of each planar triangle, based
% on the mapping function that takes an (x,y) planar
% coordinate and maps it onto a conical surface
N = length(mesh2d);
mesh3d(N).x = [];
mesh3d(N).y = [];
mesh3d(N).z = [];
for i = 1:N
[xh, yh, z] = map_2D_to_3DCone( mesh2d(i).x, [0,1], mesh2d(i).y, [0,1] );
mesh3d(i).x = xh;
mesh3d(i).y = yh;
mesh3d(i).z = z;
end
end
% This is in file: gen3D_MappedCone.m
% Generate the 3D object
close all
% Generate a 2D planar mesh to morph onto
% a conical surface
[x, y, mesh2d] = gen_mesh();
% Map the 2D mesh into a 3D one based on the
% planar to conical surface mapping
mesh3d = PlaneMesh_to_ConicalMesh( mesh2d );
% Obtain the number of triangles making up
% the mesh
N = length(mesh3d);
% Define a color mapping function for the sake
% of visualizing the mapping
color_map = #(x) [1, 0, 0].*(1-x) + [0, 0, 1].*x;
% Create the first image based on the planar
% mesh
figure(1)
hold on
for i = 1:N
color = color_map( (i-1)/(N-1) );
h = fill( mesh2d(i).x,mesh2d(i).y, color );
set(h, 'facealpha',0.9)
end
axis([0,1,0,1])
% Create the next figure showing the 3D mesh
% based on the planar to conical surface transformation
figure(2)
hold on
for i = 1:N
color = color_map( (i-1)/(N-1) );
h = fill3(mesh3d(i).x,mesh3d(i).y,mesh3d(i).z, color);
set(h, 'facealpha',0.9)
end
grid on
hold off

super resolution of low resolution images using delaunay triangulation, negative pixel values for the resultant High resolution image

i have to do super resolution of two low resolution images to obtain a high resolution image.
2nd image is taken as base image and the first image is registered with respect to it . i used SURF algorithm for image registration . A Delaunay triangulation is
constructed over the points using a built-in MATLAB delaunay
function . The HR grid of size is
constructed for a prespecified resolution enhancement factor R Then HR algorithm for interpolating the pixel values on the
HR grid is summarized next.
HR Algorithm Steps:
1. Construct the Delaunay triangulation
over the set of scattered vertices in the
irregularly sampled raster formed from the
LR frames.
Estimate the gradient vector at each
vertex of the triangulation by calculating the unit normal vector of neighbouring vector using cross product method.Sum of the unit normal vector of each triangle multiplied by its area is divided by summation of area of all neighbouring triangles to get the vertex normal.
Approximate each triangle patch in
the triangulation by a continuous and,
possibly, a continuously differentiable
surface, subject to some smoothness constraint.
Bivariate polynomials or splines
could be the approximants as explained
below.
Set the resolution enhancement factor
along the horizontal and vertical directions
and then calculate the pixel value
at each regularly spaced HR grid point to
construct the initial HR image
The bivariate polynomial i used is mentioned in the code, using pixel values at each vertex of a triangle and corresponding gradient in x and y directions i calculated the nine constants associated with each triangle then defined a high resolution grid , calculated the pixel values at each point using the constants calculated
i am attaching my code with it, the problem i am facing is that i am just getting a gray image as out put HR image , because the constants i have calculated have negative values resulting in negative pixel values
another problem i realized with my code is in gradient estimation i get a lot of 'NaN' as a result of gradient calculation.
if any one can please spent some time to help me out
close all
clear all
K = 2;
P1 = imread('C:\Users\Javeria Farooq\Desktop\project images\a.pgm');
%reads the image to be registered
P2 = imread('C:\Users\Javeria Farooq\Desktop\project images\b.pgm');
%reads the base image
image1_gray = makelr(P1, 1, 100, 1/2);
%image1_gray = P1;
% makes lr image of first
image2_gray= makelr(P2, 1, 100, 1/2);
%image2_gray= P2;
%makes lr image of second
figure(1),imshow(image1_gray)
axis on;
grid on;
title('Unregistered image');
figure(2),imshow(image2_gray)
axis on;
grid on;
title('Base image ');
impixelinfo
% both image displayed with pixel info
hold on
points_image1= detectSURFFeatures(image1_gray, 'NumScaleLevels', 100, 'NumOctaves', 12, 'MetricThreshold', 500 );
%detects surf features of first image
points_image2 = detectSURFFeatures(image2_gray, 'NumScaleLevels', 100, 'NumOctaves', 12, 'MetricThreshold', 500 );
%detects surf features of second image
[features_image1, validPoints_image1] = extractFeatures(image1_gray, points_image1);
[features_image2, validPoints_image2] = extractFeatures(image2_gray, points_image2);
%extracts features of both images
indexPairs = matchFeatures(features_image1, features_image2, 'Prenormalized', true) ;
% get matching points
matched_pts1 = validPoints_image1(indexPairs(:, 1));
matched_pts2 = validPoints_image2(indexPairs(:, 2));
figure; showMatchedFeatures(image1_gray,image2_gray,matched_pts1,matched_pts2,'montage');
%matched features of both images are displayed
legend('matched points 1','matched points 2');
% Compute the transformation matrix
tform = estimateGeometricTransform(matched_pts1,matched_pts2,'projective')
%calculate transformation matrix using projective transform
T=tform.T;
r=[];
A=[];
l=1
[N1 N2]=size(image2_gray)
registeredPts = zeros(N1*N2,2);
% s= zeros(N1*N2,2);
pixelVals = zeros(N1*N2,1);
[N1 N2]=size(image2_gray)
for row = 1:N1
for col = 1:N2
pixNum = (row-1)*N2 + col;
pixelVals(pixNum,1) = image2_gray(row,col);
registeredPts(pixNum,:) = [col,row];
end
end
[r]=transformPointsForward(tform,registeredPts);
%coordinates of base image
image2_gray=double(image2_gray);
R=2;
r1=r(:,1);
r2=r(:,2);
for row = 1:N1
for col = 1:N2
pixNum = N1*N2 + (row-1)*N2 + col;
pixelVals(pixNum,1) = image1_gray(row,col);
registeredPts(pixNum,:) = [r1(row,1),r2(row,1)];
end
end
% all pixel values are saved in pixelVals
%all registered points are saved first base image then unregistered image
%delaunay triangulation of all coordinates passing x and y coordinates from registered Points
tri = delaunay(registeredPts(:,1),registeredPts(:,2));
figure(3), triplot(tri,registeredPts(:,1),registeredPts(:,2))
save tri
% Estimate the gradient vector at each vertex
[totalTris,three] = size(tri);
[totalPoints,two] = size(registeredPts);
vGradientVecs = zeros(totalPoints,2);
triAreas = zeros(totalTris,1);
triUnitNormals = zeros(totalTris,3);
vUnitNormals = zeros(totalPoints,3);
% 1. Find the unit normal vectors and the areas of all triangles,
% then find the product of these two numbers for each triangle
for triNum = 1:totalTris
v = tri(triNum,:);
% 3D triangle points: x,y,pixel
b=pixelVals(v);
b=b(:);
p = [registeredPts(v,:),b];
% triangle area
triAreas(triNum) = polyarea([p(:,1)],[p(:,2)]);
% directional vectors representing the surface of the plane
d1 = p(2,:)-p(1,:);
d2 = p(3,:)-p(1,:);
% cross product of these vectors
crossp = cross(d1,d2);
% If u = [u1 u2 u3] and v = [v1 v2 v3], we know that the product w is defined as w = [(u2v3 – u3v2) (u3v1 - u1v3) (u1v2 - u2v1)]
% normalized cross product = unit normal vector for the triangle
dist = sqrt(sum(crossp.^2));
triUnitNormals(triNum,:) = crossp./dist;
end
% %2. %Estimate the unit normal vector at each vertex
% a. Find the triangle patches that neighbor the vertex
% b. Find the unit normal vectors of these regions
% c. Multiply each of these vectors by the area of the
% associated region, then sum these numbers and divide
% by the total area of all the regions
for pointNum = 1:totalPoints
[neighbors,x] = find(tri==pointNum);
areas = triAreas(neighbors);
areas3 = [areas,areas,areas];
triNormsSum = sum(triUnitNormals(neighbors,:).*areas3);
triAreasSum = sum(areas);
vUnormalized = triNormsSum./triAreasSum;
vUnitNormals(pointNum,:) = ...
vUnormalized./sqrt(sum(vUnormalized.^2));
if( triAreasSum == 0 )
triAreasSum = 0.0001;
vUnormalized = triNormsSum./triAreasSum;
% re-normalize
vUnitNormals(pointNum,:) = ...
vUnormalized./sqrt(sum(vUnormalized.^2));
end
% 3. Find the gradients along the x and y directions for each vertex
% vertex's unit normal: n = [nx,ny,nz]
% x-direction gradient: dz/dx = -nx/nz
% y-direction gradient: dz/dy = -ny/nz
%
for pointNum = 1:totalPoints
nz = vUnitNormals(pointNum,3);
if( nz == 0 )
nz = 0.0001;
end
vGradientVecs(pointNum,1) = -vUnitNormals(pointNum,1)./nz;
vGradientVecs(pointNum,2) = -vUnitNormals(pointNum,2)./nz;
% end
end
end
% 1. Find the 3 equations for each vertex, and
% place them in c_equations matrix;
% c_equations = [A for vertex 1;
% A for vertex 2; ...
% A for vertex totalPoints]
% c(point,row,:) gives one row from an A matrix
Btotal = zeros(3,totalPoints);
c_equations = zeros(3*totalPoints,3,9);
for pointNum = 1:totalPoints
% % B = [pixVal; x gradient; y gradient] at this vertex
z = pixelVals(pointNum);
B = [z; vGradientVecs(pointNum,1); vGradientVecs(pointNum,2)];
%
% % Compile all B matrices into a vector
Btotal(:,pointNum) = B;
% B = Ac to calculate c which is c=[c1 c2 .....c9]' take invA and
% multiply by B
x = registeredPts(pointNum,1);
y = registeredPts(pointNum,2);
A = [1 x y x^2 y^2 x^3 (x^2)*y x*(y^2) y^3; ...
0 1 0 2*x 0 3*(x^2) 2*x*y y^2 0; ...
0 0 1 0 2*y 0 x^2 2*x*y 3*(y^2)];
% Compile all A matrices into a vector
c_equations(pointNum,1,:) = A(1,:);
c_equations(pointNum,2,:) = A(2,:);
c_equations(pointNum,3,:) = A(3,:);
end
% 2. Find the c values for each triangle patch
c = zeros(totalTris,9);
c9 = zeros(9,9);
for triNum = 1:totalTris
p1 = tri(triNum,1);
p2 = tri(triNum,2);
p3 = tri(triNum,3);
B9 = [Btotal(:,p1); Btotal(:,p2); Btotal(:,p3)];
c9 = [(c_equations(p1,1,:)); (c_equations(p1,2,:)); (c_equations(p1,3,:)); ...
(c_equations(p2,1,:)); (c_equations(p2,2,:));( c_equations(p2,3,:)); ...
(c_equations(p3,1,:)); (c_equations(p3,2,:));( c_equations(p3,3,:))];
C9=squeeze(c9);
c(triNum,:) = pinv(C9)*B9; %linsolve(c9,B9);
end
% xc = findBPolyCoefficients1(tri,registeredPts,pixelVals,vGradientVecs);
% save xc
% % 2. For each point on the HR grid, find the associated triangle patch,
% % extract its c values, and use these values as the coefficients
% % in a bivariate polynomial to calculate the HR pixel value at
% % each grid point (x,y)
[N1,N2]=size(image1_gray);
[totalTris,three] = size(tri);
M = N1*R-1;
N = N2*R-1;
HRimage = zeros(M,N);
HRtriangles = zeros(M,N);
[X,Y] = meshgrid(1:1/R:N2,1:1/R:N1);
% Check all the triangles in order noting in which triangle each HR
% grid point occurs.
for triNum = 1:totalTris
pts = registeredPts(tri(triNum,:),:);
IN = inpolygon(X,Y,pts(:,1),pts(:,2)); % NxM
HRtriangles(ind2sub(size(IN),find(IN==1))) = triNum;
end
% there is a problem with this part of code ,
for y = 1:M % row
for x = 1:N % col
% For testing, average the pixels from the vertices of the
% triangle the HR point is in.
% pix = pixelVals(tri(HRtriangles(x,y),:));
% HRimage(x,y) = (pix(1) + pix(2) + pix(3))/3;
% Extract appropriate set of 9 c values
HRptC = c(HRtriangles(x,y),:);
% Bivariate polynomial
HRimage(x,y) = sum(HRptC.*[1,x,y,x^2,y^2,x^3,(x^2)*y,x*(y^2),y^3]);
g(x,y)=HRimage(x,y);
%changd xy with yx
end
end
% HRimage = estimateGridVals1(tri,registeredPts,R,N1,N2,pixelVals);
% %Estimating Grid values at each patch
% %save HRimage
g(g(:,:)<0)=0;
figure(8),imshow(g,[]);