-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearObjectCentroid.m
74 lines (51 loc) · 1.93 KB
/
LinearObjectCentroid.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
% This function will calculate the closest point of intersection for an
% arbitrary number of lines and points.
%Parameter P: A cell array of points
%Parameter L: A cell array of Lines
%Parameter A: A cell array of planes
%Return point: A point that represents the centroid of all the objects
function point = LinearObjectCentroid( P, L, A )
% For points, we want
% point = point
U = zeros( 0 , 3 );
B = zeros( 0 , 1 );
for i = 1:numel(P)
U = cat( 1, U, eye(3) );
B = cat( 1, B, P{i}.point );
end %for
% The idea is to minimize the dot product of the normal vectors with the
% intersection point in the least squares sense
% ( basePoint - point ) . u = ( basePoint - point ) . v = 0
for i = 1:numel(L)
% Find the u and v vectors orthogonal to the line (Gram-Schmidt)
direction = L{i}.GetDirection();
% Use the standard basis as a starting point
if ( max( abs( direction ) ) == abs( direction(1) ) )
e1 = [ 0; 1; 0 ];
e2 = [ 0; 0; 1 ];
elseif ( max( abs( direction ) ) == abs( direction(2) ) )
e1 = [ 1; 0; 0 ];
e2 = [ 0; 0; 1 ];
elseif ( max( abs( direction ) ) == abs( direction(3) ) )
e1 = [ 1; 0; 0 ];
e2 = [ 0; 1; 0 ];
end
u = e1 - dot( e1, direction ) * direction;
u = u / norm( u );
v = e2 - dot( e2, direction ) * direction - dot( e2, u ) * u;
v = v / norm( v );
% Now add to the U and B matrices
U = cat( 1, U, u' );
U = cat( 1, U, v' );
B = cat( 1, B, dot( L{i}.endPoint1, u ) );
B = cat( 1, B, dot( L{i}.endPoint1, v ) );
end %for
% The idea is to minimize the dot product of the normal vector with the
% centroid point in the least sqaures sense
% ( basePoint - point ) . n = 0
for i = 1:numel(A)
U = cat( 1, U, A{i}.GetNormal()' );
B = cat( 1, B, dot( A{i}.basePoint, A{i}.GetNormal() ) );
end %for
% Solve for point in the least squares sense
point = ( U' * U ) \ U' * B;