-
Notifications
You must be signed in to change notification settings - Fork 2
/
intersectPlaneSphere.m
91 lines (78 loc) · 2.5 KB
/
intersectPlaneSphere.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
function [circle0 Rc nor] = intersectPlaneSphere(plane, sphere)
%INTERSECTPLANESPHERE Return intersection circle between a plane and a sphere
%
% CIRC = intersectPlaneSphere(PLANE, SPHERE)
% Returns the circle which is the intersection of the given plane
% and sphere.
% PLANE : [x0 y0 z0 dx1 dy1 dz1 dx2 dy2 dz2]
% SPHERE : [XS YS ZS RS]
% CIRC : [XC YC ZC RC THETA PHI PSI]
% [x0 y0 z0] is the origin of the plane, [dx1 dy1 dz1] and [dx2 dy2 dz2]
% are two direction vectors,
% [XS YS ZS] are coordinates of the sphere center, RS is the sphere
% radius,
% [XC YC ZC] are coordinates of the circle center, RC is the radius of
% the circle, [THETA PHI] is the normal of the plane containing the
% circle (THETA being the colatitude, and PHI the azimut), and PSI is a
% rotation angle around the normal (equal to zero in this function, but
% kept for compatibility with other functions). All angles are given in
% degrees.
%
% See Also:
% planes3d, spheres, circles3d, intersectLinePlane, intersectLineSphere
%
% ---------
% author : David Legland
% INRA - TPV URPOI - BIA IMASTE
% created the 18/02/2005.
%
% HISTORY
% 27/06/2007: change output format of circle, add support for multiple
% data
% 2011-06-21 use degrees for angles
% number of inputs of each type
Ns = size(sphere, 1);
Np = size(plane, 1);
% unify data dimension
if Ns ~= Np
if Ns == 1
sphere = sphere(ones(Np, 1), :);
elseif Np == 1
plane = plane(ones(Ns, 1), :);
else
error('data should have same length, or one data should have length 1');
end
end
% center of the spheres
center = sphere(:,1:3);
% radius of spheres
if size(sphere, 2) == 4
Rs = sphere(:,4);
else
% assume default radius equal to 1
Rs = ones(size(sphere, 1), 1);
end
% projection of sphere center on plane -> gives circle center
%circle0 = projPointOnPlane(center, plane);
nor=cross(plane(1,4:6),plane(1,7:9));
Q=plane(1,1:3);
nor=nor/norm(nor);
N2=nor.'*nor;
circle0=center*(eye(3)-N2)+repmat(Q*N2,1,1);
% radius of circles
%d = distancePoints3d(center, circle0);
d=sqrt((center(1)-circle0(1))^2+(center(2)-circle0(2))^2+(center(3)-circle0(3))^2);
if d<=Rs
Rc = sqrt(Rs.*Rs - d.*d);
% normal of planes = normal of circles
%nor = planeNormal(plane);
% convert to angles
%[theta, phi] = cart2sph2(nor(:,1), nor(:,2), nor(:,3));
psi = zeros(Np, 1);
% create structure for circle
k = 180 / pi;
%circle = [circle0 Rc [theta phi psi]*k];
%circle = [circle0 Rc nor];
else
Rc=0;
end