1## Copyright (C) 2021 David Legland
2## All rights reserved.
3##
4## Redistribution and use in source and binary forms, with or without
5## modification, are permitted provided that the following conditions are met:
6##
7##     1 Redistributions of source code must retain the above copyright notice,
8##       this list of conditions and the following disclaimer.
9##     2 Redistributions in binary form must reproduce the above copyright
10##       notice, this list of conditions and the following disclaimer in the
11##       documentation and/or other materials provided with the distribution.
12##
13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23##
24## The views and conclusions contained in the software and documentation are
25## those of the authors and should not be interpreted as representing official
26## policies, either expressed or implied, of the copyright holders.
27
28function point2 = projPointOnCircle3d(point, circle)
29%PROJPOINTONCIRCLE3D Project a 3D point onto a 3D circle.
30%
31%   PT2 = projPointOnCircle3d(PT, CIRCLE).
32%   Computes the projection of 3D point PT onto the 3D circle CIRCLE.
33%
34%   Point PT is a N-by-3 array, and CIRCLE is a 1-by-7 array.
35%   Result PT2 is a N-by-3 array, containing coordinates of projections of
36%   PT onto the circle CIRCLE.
37%
38%   See also
39%   projPointOnLine3d, projPointOnPlane
40%
41%   Source
42%   https://www.geometrictools.com/Documentation/DistanceToCircle3.pdf
43%
44% ---------
45% Author: oqilipo
46% Created: 2020-10-12
47% Copyright 2020
48%
49
50center = circle(1:3);
51radius = circle(4);
52
53% Compute transformation from local basis to world basis
54TFM = localToGlobal3d(center, circle(5), circle(6), circle(7));
55
56% Create circle plane
57circlePlaneNormal = transformVector3d([0 0 1], TFM);
58circlePlane = createPlane(center, circlePlaneNormal);
59
60% Project point on circle plane
61PTonCP = projPointOnPlane(point, circlePlane);
62
63% Calculate vector from the projected point to the center of the circle
64PTtoCenter = normalizeVector3d(circle(1:3) - PTonCP);
65
66% Calculate final point
67point2 = PTonCP + PTtoCenter.*(distancePoints3d(PTonCP, center) - radius);
68
69% Take an arbitrary point of the circle if the point is the center of the circle
70if any(all(isnan(point2),2))
71    point2(all(isnan(point2),2),:) = center + normalizeVector3d(circlePlane(4:6))*radius;
72end
73% Take an arbitrary point of the circle if the point lies on the normal of the circle plane
74if any(sum(PTtoCenter == 0,2) == 2)
75    point2(sum(PTtoCenter == 0,2) == 2,:) = center + normalizeVector3d(circlePlane(4:6))*radius;
76end
77end
78