-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointCloud.m
169 lines (101 loc) · 6.54 KB
/
PointCloud.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
%Copyright (C) 2016 Piotr Beben
%This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
%This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
%You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
classdef PointCloud
methods (Static)
%---------------------------------------------------------------------------------------------
function output = precision(newPrecision)
persistent currentPrecision;
if nargin >= 1
currentPrecision = newPrecision;
elseif isempty(currentPrecision)
currentPrecision = 'double';
end
output = currentPrecision;
end
%---------------------------------------------------------------------------------------------
function points = build_grid( subdivisions, dims, center )
dimension = length(subdivisions);
if dimension == 0
points = center;
else
d = dimension;
section = PointCloud.build_grid( subdivisions(1:(d-1)), dims(1:(d-1)), center );
if subdivisions(d)>0
incr = [ zeros(d-1,1); dims(d)/subdivisions(d); zeros(length(center)-d,1) ];
bottom = [ zeros(d-1,1); -dims(d)/2; zeros(length(center)-d,1) ];
points = NaN(length(center),prod(subdivisions+1),PointCloud.precision());
for i = 1:(subdivisions(d)+1)
j = (i-1)*size(section,2);
points( :, (j+1):(j+size(section,2)) ) = bsxfun(@plus,section,bottom+(i-1)*incr);
end
else
points = section;
end
end
end
%---------------------------------------------------------------------------------------------
function points = build_random( dimension, numPoints, type, mode, varargin )
switch type
case 'centered'
center = varargin{1}; %column vector
axisScale = varargin{2};
switch mode
case 'normal'
points = bsxfun( @plus, diag(axisScale)*randn(dimension,numPoints), center );
case 'uniform'
points = bsxfun( @plus, diag(2*axisScale)*(rand(dimension,numPoints)-1), center );
otherwise
throw( MException('Invalid:Option','Invalid Option: %s',mode) );
end
case 'custom'
curve_array = varargin{1}; %cell array of parametric curves (function handles), 1 per dimension
tMin = varargin{2}; %parameter lower and upper bounds, 1 entry for each unique parameter
tMax = varargin{3};
noise = varargin{4};
switch mode
case 'normal'
rand_noise = @() noise*randn(1,numPoints,PointCloud.precision());
case 'uniform'
rand_noise = @() (2*noise)*rand(1,numPoints,PointCloud.precision()) - noise;
case 'none'
rand_noise = @() zeros(1,numPoints,PointCloud.precision());
otherwise
throw( MException('Invalid:Option','Invalid Option: %s',mode) );
end
parameters = cell( 1, length(tMin) );
for i = 1:length(parameters)
parameters{i} = (tMax(i)-tMin(i))*rand(1,numPoints) + tMin(i);
end
points = NaN(dimension,numPoints,PointCloud.precision());
for i = 1:dimension
f = curve_array{i};
points(i,:) = f( parameters ) + rand_noise();
end
otherwise
throw( MException('Invalid:Option','Invalid Option: %s',type) );
end
end
%---------------------------------------------------------------------------------------------
function [dims,center] = get_cloud_dimensions(cloud)
dims = zeros(1,size(cloud,1));
center = zeros(size(cloud,1),1);
for i = 1:length(dims)
M = max(cloud(i,:));
m = min(cloud(i,:));
dims(i) = M - m;
center(i) = m + (M-m)/2;
end
end
%---------------------------------------------------------------------------------------------
function plot = plot2D( points, color )
plot = scatter( points(1,:), points(2,:), 20, color, 'filled' );
end
%---------------------------------------------------------------------------------------------
function plot = plot3D( points, color )
plot = scatter3( points(1,:), points(2,:), points(3,:), 10, color, 'filled' );
end
%---------------------------------------------------------------------------------------------
end
end