random sample cylinders and plot them on the fields
parent
6732beb1b4
commit
9485553d99
|
@ -0,0 +1,31 @@
|
||||||
|
function plotCylinderSamples(cylinders, fieldSize)
|
||||||
|
|
||||||
|
holdstate = ishold;
|
||||||
|
hold on
|
||||||
|
|
||||||
|
num = size(cylinders, 1);
|
||||||
|
|
||||||
|
sampleDensity = 120;
|
||||||
|
figure
|
||||||
|
|
||||||
|
for i = 1:num
|
||||||
|
%base.z = cylinders{i}.centroid.z - cylinders{i}.height/2;
|
||||||
|
[X,Y,Z] = cylinder(cylinders{i}.radius, sampleDensity * cylinders{i}.radius * cylinders{i}.height);
|
||||||
|
|
||||||
|
X = X + cylinders{i}.centroid.x;
|
||||||
|
Y = Y + cylinders{i}.centroid.y;
|
||||||
|
Z = Z * cylinders{i}.height;
|
||||||
|
|
||||||
|
cylinderHandle = surf(X,Y,Z);
|
||||||
|
set(cylinderHandle, 'FaceAlpha', 0.5);
|
||||||
|
hold on
|
||||||
|
end
|
||||||
|
|
||||||
|
axis equal
|
||||||
|
axis([0, fieldSize.x, 0, fieldSize.y, 0, 20]);
|
||||||
|
|
||||||
|
if ~holdstate
|
||||||
|
hold off
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -2,27 +2,33 @@ clear all;
|
||||||
clc;
|
clc;
|
||||||
import gtsam.*
|
import gtsam.*
|
||||||
|
|
||||||
cylinder_num = 10;
|
cylinder_num = 20;
|
||||||
cylinders = cell(cylinder_num, 1);
|
cylinders = cell(cylinder_num, 1);
|
||||||
|
|
||||||
% generate a set of cylinders
|
% generate a set of cylinders
|
||||||
|
fieldSize = Point2([100, 100]');
|
||||||
|
|
||||||
|
% random generate cylinders on the fields
|
||||||
for i = 1:cylinder_num
|
for i = 1:cylinder_num
|
||||||
cylinder_center = Point2([10, 5 * i]');
|
baseCentroid = Point2([fieldSize.x * rand, fieldSize.y * rand]');
|
||||||
cylinders{i,1} = cylinderSampling(cylinder_center, 1, 5, 30);
|
cylinders{i,1} = cylinderSampling(baseCentroid, 1, 5, 30);
|
||||||
end
|
end
|
||||||
|
|
||||||
|
% plot all the cylinders and sampled points
|
||||||
|
% now is plotting on a 100 * 100 field
|
||||||
|
plotCylinderSamples(cylinders, fieldSize);
|
||||||
|
|
||||||
% visibility validation
|
% visibility validation
|
||||||
%camera_transform = Pose3(Rot3.RzRyRx(-pi/2, 0, -pi/2),y_shift);
|
|
||||||
|
|
||||||
K = Cal3_S2(525,525,0,320,240);
|
K = Cal3_S2(525,525,0,320,240);
|
||||||
cam_pose = Pose3();
|
cameraPose = Pose3(); % now set to be default
|
||||||
cam = SimpleCamera(cam_pose, K);
|
|
||||||
|
|
||||||
% the projections of all visible 3D points
|
% the projections of all visible 3D points
|
||||||
visiblePoints3 = cylinderSampleProjection(cam, cam_pose, cylinders);
|
% visiblePoints3 = cylinderSampleProjection(K, cameraPose, cylinders);
|
||||||
|
|
||||||
%
|
|
||||||
|
|
||||||
%
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,41 @@
|
||||||
function [] = cylinderSampleProjection(Cam, Pose3, cylinders)
|
function [] = cylinderSampleProjection(K, cameraPose, imageSize, cylinders)
|
||||||
|
% Project sampled points on cylinder to camera frame
|
||||||
|
% Authors: Zhaoyang Lv
|
||||||
|
|
||||||
cylinder_num = size(cylinders, 1);
|
cylinder_num = size(cylinders, 1);
|
||||||
|
|
||||||
|
camera = SimpleCamera(cameraPose, K);
|
||||||
|
|
||||||
for i = 1:cylinder_num
|
for i = 1:cylinder_num
|
||||||
cylinder = cylinders{i};
|
|
||||||
|
point_num = size( cylinders{i}.Points, 1);
|
||||||
|
|
||||||
point_num = size(cylinder.Points, 1);
|
% to check point visibility
|
||||||
% to be finished
|
for j = 1:point_num
|
||||||
|
sampledPoint3 = cylinders{i}.Poinsts{j};
|
||||||
% for j = 1:point_num
|
measurements2d = camera.project(sampledPoint3);
|
||||||
%
|
|
||||||
% cylinderPoints =
|
% ignore points not visible in the scene
|
||||||
%
|
if measurements2d.x < 0 || measurements.x >= imageSize.x ...
|
||||||
% end
|
|| measurements2d.y < 0 || measurements.y >= imageSize.y
|
||||||
|
continue;
|
||||||
|
end
|
||||||
|
% ignore points occluded
|
||||||
|
% use a simple math hack to check occlusion:
|
||||||
|
% 1. All points in front of cylinders' surfaces are visible
|
||||||
|
% 2. For points behind the cylinders' surfaces, the cylinder
|
||||||
|
for k = 1:cylinder_num
|
||||||
|
|
||||||
|
rayCameraToPoint = sampledPoint3 - cameraPose.t;
|
||||||
|
rayCameraToCylinder = cylinders{i} - cameraPose.t;
|
||||||
|
|
||||||
|
projectedRay = dot(rayCameraToPoint, rayCameraToCylinder);
|
||||||
|
distCameraToCylinder = norm(rayCameraToCylinder);
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,22 +1,25 @@
|
||||||
function [cylinder] = cylinderSampling(Point2, radius, height, density)
|
function [cylinder] = cylinderSampling(baseCentroid, radius, height, density)
|
||||||
%
|
%
|
||||||
import gtsam.*
|
import gtsam.*
|
||||||
% calculate the cylinder area
|
% calculate the cylinder area
|
||||||
A = 2 * pi * radius * height;
|
area = 2 * pi * radius * height;
|
||||||
|
|
||||||
PointsNum = round(A * density);
|
pointsNum = round(area * density);
|
||||||
|
|
||||||
Points3 = cell(PointsNum, 1);
|
points3 = cell(pointsNum, 1);
|
||||||
|
|
||||||
% sample the points
|
% sample the points
|
||||||
for i = 1:PointsNum
|
for i = 1:pointsNum
|
||||||
theta = 2 * pi * rand;
|
theta = 2 * pi * rand;
|
||||||
x = radius * cos(theta) + Point2.x;
|
x = radius * cos(theta) + baseCentroid.x;
|
||||||
y = radius * sin(theta) + Point2.y;
|
y = radius * sin(theta) + baseCentroid.y;
|
||||||
z = height * rand;
|
z = height * rand;
|
||||||
Points3{i,1} = Point3([x,y,z]');
|
points3{i,1} = Point3([x,y,z]');
|
||||||
end
|
end
|
||||||
|
|
||||||
cylinder.area = A;
|
cylinder.area = area;
|
||||||
cylinder.Points = Points3;
|
cylinder.radius = radius;
|
||||||
|
cylinder.height = height;
|
||||||
|
cylinder.Points = points3;
|
||||||
|
cylinder.centroid = Point3(baseCentroid.x, baseCentroid.y, height/2);
|
||||||
end
|
end
|
Loading…
Reference in New Issue