Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
UestcJay committed Jul 16, 2022
1 parent a6048d6 commit 156c33c
Show file tree
Hide file tree
Showing 20 changed files with 1,506 additions and 0 deletions.
16 changes: 16 additions & 0 deletions MATLAB/Reshape.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Reshape(ax, r, theta, phi, c0, viewangle, permute)
if nargin < 5
c0 = [0, 0, 0];
viewangle = 20;
permute=[0, 1, 2];
end

CamX=r*sind(theta)*sind(phi) + c0(1); CamY=r*cosd(theta) + c0(2); CamZ=r*sind(theta)*cosd(phi) + c0(3);
UpX=-cosd(theta)*sind(phi);UpY=sind(theta);UpZ=-cosd(theta)*cosd(phi);
cam_pos = [CamX,CamY,CamZ];
up_dir = [UpX,UpY,UpZ];
cam_pos = cam_pos(permute);
up_dir = up_dir(permute);
set(ax,'CameraPosition',cam_pos,'CameraTarget',c0,...
'CameraUpVector',up_dir, 'CameraViewAngle', viewangle);
end
6 changes: 6 additions & 0 deletions MATLAB/Reshape_cup.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function Reshape(ax, r, theta, phi)
CamX=r*sind(theta)*sind(phi); CamY=r*cosd(theta); CamZ=r*sind(theta)*cosd(phi);
UpX=-cosd(theta)*sind(phi);UpY=sind(theta);UpZ=-cosd(theta)*cosd(phi);
set(ax,'CameraPosition',[CamX,CamZ,CamY],'CameraTarget',[0 0 0],...
'CameraUpVector',[UpX,UpZ,UpY],'CameraViewAngle', 60);
end
54 changes: 54 additions & 0 deletions MATLAB/Sphere2RGBCube.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function [ RGB ] = Sphere2RGBCube( V )
%Sphere2RGBCube converts the normalized vector V (representing a point on
%the unit spher) into its corresponding RGB cube values. zero vectors are
%outpt as NaN
%Author:Itzik Ben Sabat sitzikbs[at]gmail.com
%Date: 27.1.2016

if size(V,2) > 3
V = V';
transposeglag = true;
else
transposeglag = false;
end
RGB = zeros(size(V));
V = V./repmat((sqrt(sum(V.^2,2))),1,3); %make sure V is normalized


%Map unit sphere to unit cubev
x = V(:,1);
y = V(:,2);
z = V(:,3);

absx = abs(x);
absy = abs(y);
absz = abs(z);

%Map left and right cube planes : y=+-1
leftright = and(absy>=absx, absy>=absz);
RGB(leftright,1) = (1 ./ absy(leftright)).*x(leftright);
RGB(leftright,3) = (1 ./ absy(leftright)).*z(leftright);
RGB(and(leftright , y > 0),2) = 1;
RGB(and(leftright , y < 0),2) = -1;

%Map front and back cube planes : x=+-1
frontback = and(absx >= absy , absx >= absz);
RGB(frontback,2) = (1 ./ absx(frontback)).*y(frontback);
RGB(frontback,3) = (1 ./ absx(frontback)).*z(frontback);
RGB(and(frontback , x > 0),1) = 1;
RGB(and(frontback , x < 0),1) = -1;

%Map top and bottom cube planes : z=+-1
topbottom = and(absz >= absx , absz >= absy);
RGB(topbottom,1) = (1 ./ absz(topbottom)).*x(topbottom);
RGB(topbottom,2) = (1 ./ absz(topbottom)).*y(topbottom);
RGB(and(topbottom , z > 0),3) = 1;
RGB(and(topbottom , z < 0),3) = -1;

%Map from unit cube to RGB Cube
RGB = 0.5*RGB+0.5;
RGB(all(isnan(V),2),:)=nan; % zero vectors are mapped to black
if transposeglag
RGB = RGB';
end
end
12 changes: 12 additions & 0 deletions MATLAB/camera_parameters.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function camera_params = camera_parameters()

camera_params.name = {'Liberty','netsuke', 'galera', 'Cup', 'pipe_curve', 'column100k'};
camera_params.r = {10, 10, 10, 10, 10, 10};
camera_params.phi = {45, 90-35.26, 45 + 180, 45, 45, 45};
camera_params.theta = {35.26, 90, 35.26, 35.26, 35.26+90, 35.26};
camera_params.view_angle = {10, 10, 10, 15, 10, 10};
camera_params.x_scale = {1, 1, 1, 1, 1, 1};
camera_params.y_scale = {1, 1, -1, 1, 1, 1};
camera_params.z_scale = {1, 1, 1, 1, 1, 1};
camera_params.permute = {[1,2,3], [1,2,3], [1,2,3], [1, 3, 2], [1, 2, 3], [1, 2, 3]};
end
113 changes: 113 additions & 0 deletions MATLAB/compute_expert_statistics.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
clear all
close all
clc

data_path = '/home/itzik/PycharmProjects/NestiNet/data/pcpnet/';
results_path = '/home/itzik/PycharmProjects/NestiNet/log/experts/pcpnet_results/';
output_path = [results_path, '/images/expert_statistics/'];
output_path_avg_err = [output_path, 'Average expert error/'];
output_path_expert_cnt = [output_path, 'Expert point clount/'];

use_subset = true;
n_experts = 7;
hist_bins = 1:n_experts;
if ~exist(output_path, 'dir')
mkdir(output_path)
if ~exist(output_path_avg_err, 'dir')
mkdir(output_path_avg_err);
end
if ~exist(output_path_expert_cnt, 'dir')
mkdir(output_path_expert_cnt);
end
end

xyz_file_list = dir([data_path,'*.xyz']);
file_list_to_export = [data_path, 'testset_all.txt'];
shapes_to_export = strsplit(fileread(file_list_to_export));
shapes_to_export = shapes_to_export(~cellfun('isempty',shapes_to_export)); % remove empty cells

accum_expert_count = zeros(1, n_experts);
accume_expert_error = zeros(1,n_experts);
for shape = shapes_to_export

disp(['processing ', shape{1}, '...']);
xyz_file_name = [data_path, shape{1}, '.xyz'];
normals_gt_file_name = [data_path, shape{1}, '.normals'];
normals_file_name = [results_path, shape{1}, '.normals'];
expert_file_name = [results_path, shape{1}, '.experts'];
idx_file_name = [data_path, shape{1}, '.pidx'];
points = dlmread(xyz_file_name);
points = points - mean(points);
points = points.* (1./max(sqrt(sum(points.^2, 2))));
normals_gt = dlmread(normals_gt_file_name);
normals = dlmread(normals_file_name);
expert = dlmread(expert_file_name) + 1;
n_normals = size(normals, 1);
npoints = size(points,1);

if npoints ~= n_normals
idxs = dlmread(idx_file_name) + 1;
points = points(idxs, :);
normals_gt = normals_gt(idxs, :);
elseif use_subset
idxs = dlmread(idx_file_name) + 1;
points = points(idxs, :);
normals_gt = normals_gt(idxs, :);
normals = normals(idxs, :);
expert = expert(idxs, :);
end

error = acosd(abs(sum(normals.*normals_gt,2))./ (sqrt(sum(normals.^2,2)).* sqrt(sum(normals_gt.^2,2))));
shape_error_per_expert = zeros(1, n_experts);
for i =1:n_experts
shape_error_per_expert(i) = sum(error(expert == i));
end
accume_expert_error = accume_expert_error + shape_error_per_expert;
edges = [1: n_experts + 1] - 0.5;
[expert_count, edges] = histcounts(expert, edges);
accum_expert_count = accum_expert_count + expert_count;

fig_h = figure('color','w', 'numbertitle','off','name','Average expert error');
ax = axes('fontsize',20, 'fontname','serif', 'xtick',1:n_experts, 'xlim',[1 - 0.5, n_experts + 0.5]);
xlabel('Expert')
ylabel('Average error [deg]');
title('Average expert error')
hold all
bar(1:n_experts, shape_error_per_expert./ expert_count)
image_filename = [output_path_avg_err, shape{1}, '.png'];
print(image_filename, '-dpng')
close(fig_h);

fig_h = figure('color','w', 'numbertitle','off','name','Expert point count');
ax = axes('fontsize',20, 'fontname','serif', 'xtick',1:n_experts, 'xlim',[1 - 0.5, n_experts + 0.5]);
xlabel('Expert')
ylabel('Points per expert');
title('Expert point clount')
hold all
bar(1:n_experts, expert_count)
image_filename = [output_path_expert_cnt, shape{1}, '.png'];
print(image_filename, '-dpng')
close(fig_h);
end
avg_error = accume_expert_error./accum_expert_count;
avg_error(isnan(avg_error)) = 0;

% **************************** Visualization *********
figure('color','w', 'numbertitle','off','name','Average expert error');
ax = axes('fontsize',20, 'fontname','serif', 'xtick',1:n_experts, 'xlim',[1 - 0.5, n_experts + 0.5]);
xlabel('Expert');
ylabel('Average error [deg]');
title('Average expert error');
hold all
bar(1:n_experts, avg_error);
image_filename = [output_path, 'Average expert error', '.png'];
print(image_filename, '-dpng');
figure('color','w', 'numbertitle','off','name','Expert point count');
ax = axes('fontsize',20, 'fontname','serif', 'xtick',1:n_experts, 'xlim',[1 - 0.5, n_experts + 0.5]);
xlabel('Expert')
ylabel('Points per expert');
title('Expert point count');
hold all
bar(1:n_experts, accum_expert_count);
image_filename = [output_path, 'Expert point count', '.png'];
print(image_filename, '-dpng');
25 changes: 25 additions & 0 deletions MATLAB/curvature_color_mapping.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Cdata = curvature_color_mapping(PrincipalCurvatures, k1_range, k2_range)
% map the principal curvatures to color space where pp is red, nn is blue,
% pn is green and 00 is white (p=positiv,m n=negative)
k1min = k1_range(1);
k1max = k1_range(2);
k2min = k2_range(1);
k2max = k2_range(2);
PrincipalCurvatures(find(PrincipalCurvatures(:, 1)>k1max), 1)=k1max;
PrincipalCurvatures(find(PrincipalCurvatures(:, 1)<k1min), 1)=k1min;
PrincipalCurvatures(find(PrincipalCurvatures(:, 2)>k2max), 2)=k2max;
PrincipalCurvatures(find(PrincipalCurvatures(:, 2)<k2min), 2)=k2min;

[X, Y] = meshgrid([k1min 0 k1max], [k2min 0 k2max]);

red_dist= [0 0.5 0; 0.5 1 1 ; 0 1 1 ];
green_dist = [0 1 1; 1 1 1 ; 1 1 0 ];
blue_dist = [1 1 0; 1 1 0.5; 0 0.5 0 ];


Xq=PrincipalCurvatures(:, 1);
Yq=PrincipalCurvatures(:, 2);
Cdata(:,1)=interp2(X,Y,red_dist,Xq,Yq);
Cdata(:,2)=interp2(X,Y,green_dist,Xq,Yq);
Cdata(:,3)=interp2(X,Y,blue_dist,Xq,Yq);
end
147 changes: 147 additions & 0 deletions MATLAB/distinguishable_colors.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
function colors = distinguishable_colors(n_colors,bg,func)
% DISTINGUISHABLE_COLORS: pick colors that are maximally perceptually distinct
%
% When plotting a set of lines, you may want to distinguish them by color.
% By default, Matlab chooses a small set of colors and cycles among them,
% and so if you have more than a few lines there will be confusion about
% which line is which. To fix this problem, one would want to be able to
% pick a much larger set of distinct colors, where the number of colors
% equals or exceeds the number of lines you want to plot. Because our
% ability to distinguish among colors has limits, one should choose these
% colors to be "maximally perceptually distinguishable."
%
% This function generates a set of colors which are distinguishable
% by reference to the "Lab" color space, which more closely matches
% human color perception than RGB. Given an initial large list of possible
% colors, it iteratively chooses the entry in the list that is farthest (in
% Lab space) from all previously-chosen entries. While this "greedy"
% algorithm does not yield a global maximum, it is simple and efficient.
% Moreover, the sequence of colors is consistent no matter how many you
% request, which facilitates the users' ability to learn the color order
% and avoids major changes in the appearance of plots when adding or
% removing lines.
%
% Syntax:
% colors = distinguishable_colors(n_colors)
% Specify the number of colors you want as a scalar, n_colors. This will
% generate an n_colors-by-3 matrix, each row representing an RGB
% color triple. If you don't precisely know how many you will need in
% advance, there is no harm (other than execution time) in specifying
% slightly more than you think you will need.
%
% colors = distinguishable_colors(n_colors,bg)
% This syntax allows you to specify the background color, to make sure that
% your colors are also distinguishable from the background. Default value
% is white. bg may be specified as an RGB triple or as one of the standard
% "ColorSpec" strings. You can even specify multiple colors:
% bg = {'w','k'}
% or
% bg = [1 1 1; 0 0 0]
% will only produce colors that are distinguishable from both white and
% black.
%
% colors = distinguishable_colors(n_colors,bg,rgb2labfunc)
% By default, distinguishable_colors uses the image processing toolbox's
% color conversion functions makecform and applycform. Alternatively, you
% can supply your own color conversion function.
%
% Example:
% c = distinguishable_colors(25);
% figure
% image(reshape(c,[1 size(c)]))
%
% Example using the file exchange's 'colorspace':
% func = @(x) colorspace('RGB->Lab',x);
% c = distinguishable_colors(25,'w',func);
% Copyright 2010-2011 by Timothy E. Holy
% Parse the inputs
if (nargin < 2)
bg = [1 1 1]; % default white background
else
if iscell(bg)
% User specified a list of colors as a cell aray
bgc = bg;
for i = 1:length(bgc)
bgc{i} = parsecolor(bgc{i});
end
bg = cat(1,bgc{:});
else
% User specified a numeric array of colors (n-by-3)
bg = parsecolor(bg);
end
end

% Generate a sizable number of RGB triples. This represents our space of
% possible choices. By starting in RGB space, we ensure that all of the
% colors can be generated by the monitor.
n_grid = 30; % number of grid divisions along each axis in RGB space
x = linspace(0,1,n_grid);
[R,G,B] = ndgrid(x,x,x);
rgb = [R(:) G(:) B(:)];
if (n_colors > size(rgb,1)/3)
error('You can''t readily distinguish that many colors');
end

% Convert to Lab color space, which more closely represents human
% perception
if (nargin > 2)
lab = func(rgb);
bglab = func(bg);
else
C = makecform('srgb2lab');
lab = applycform(rgb,C);
bglab = applycform(bg,C);
end
% If the user specified multiple background colors, compute distances
% from the candidate colors to the background colors
mindist2 = inf(size(rgb,1),1);
for i = 1:size(bglab,1)-1
dX = bsxfun(@minus,lab,bglab(i,:)); % displacement all colors from bg
dist2 = sum(dX.^2,2); % square distance
mindist2 = min(dist2,mindist2); % dist2 to closest previously-chosen color
end

% Iteratively pick the color that maximizes the distance to the nearest
% already-picked color
colors = zeros(n_colors,3);
lastlab = bglab(end,:); % initialize by making the "previous" color equal to background
for i = 1:n_colors
dX = bsxfun(@minus,lab,lastlab); % displacement of last from all colors on list
dist2 = sum(dX.^2,2); % square distance
mindist2 = min(dist2,mindist2); % dist2 to closest previously-chosen color
[~,index] = max(mindist2); % find the entry farthest from all previously-chosen colors
colors(i,:) = rgb(index,:); % save for output
lastlab = lab(index,:); % prepare for next iteration
end
end
function c = parsecolor(s)
if ischar(s)
c = colorstr2rgb(s);
elseif isnumeric(s) && size(s,2) == 3
c = s;
else
error('MATLAB:InvalidColorSpec','Color specification cannot be parsed.');
end
end
function c = colorstr2rgb(c)
% Convert a color string to an RGB value.
% This is cribbed from Matlab's whitebg function.
% Why don't they make this a stand-alone function?
rgbspec = [1 0 0;0 1 0;0 0 1;1 1 1;0 1 1;1 0 1;1 1 0;0 0 0];
cspec = 'rgbwcmyk';
k = find(cspec==c(1));
if isempty(k)
error('MATLAB:InvalidColorString','Unknown color string.');
end
if k~=3 || length(c)==1,
c = rgbspec(k,:);
elseif length(c)>2,
if strcmpi(c(1:3),'bla')
c = [0 0 0];
elseif strcmpi(c(1:3),'blu')
c = [0 0 1];
else
error('MATLAB:UnknownColorString', 'Unknown color string.');
end
end
end
Loading

0 comments on commit 156c33c

Please sign in to comment.