PROJPOINTONCIRCLE3D Project a 3D point onto a 3D circle. PT2 = projPointOnCircle3d(PT, CIRCLE). Computes the projection of 3D point PT onto the 3D circle CIRCLE. Point PT is a N-by-3 array, and CIRCLE is a 1-by-7 array. Result PT2 is a N-by-3 array, containing coordinates of projections of PT onto the circle CIRCLE. See also projPointOnLine3d, projPointOnPlane Source https://www.geometrictools.com/Documentation/DistanceToCircle3.pdf --------- Author: oqilipo Created: 2020-10-12 Copyright 2020
0001 function point2 = projPointOnCircle3d(point, circle) 0002 %PROJPOINTONCIRCLE3D Project a 3D point onto a 3D circle. 0003 % 0004 % PT2 = projPointOnCircle3d(PT, CIRCLE). 0005 % Computes the projection of 3D point PT onto the 3D circle CIRCLE. 0006 % 0007 % Point PT is a N-by-3 array, and CIRCLE is a 1-by-7 array. 0008 % Result PT2 is a N-by-3 array, containing coordinates of projections of 0009 % PT onto the circle CIRCLE. 0010 % 0011 % See also 0012 % projPointOnLine3d, projPointOnPlane 0013 % 0014 % Source 0015 % https://www.geometrictools.com/Documentation/DistanceToCircle3.pdf 0016 % 0017 % --------- 0018 % Author: oqilipo 0019 % Created: 2020-10-12 0020 % Copyright 2020 0021 % 0022 0023 center = circle(1:3); 0024 radius = circle(4); 0025 0026 % Compute transformation from local basis to world basis 0027 TFM = localToGlobal3d(center, circle(5), circle(6), circle(7)); 0028 0029 % Create circle plane 0030 circlePlaneNormal = transformVector3d([0 0 1], TFM); 0031 circlePlane = createPlane(center, circlePlaneNormal); 0032 0033 % Project point on circle plane 0034 PTonCP = projPointOnPlane(point, circlePlane); 0035 0036 % Calculate vector from the projected point to the center of the circle 0037 PTtoCenter = normalizeVector3d(circle(1:3) - PTonCP); 0038 0039 % Calculate final point 0040 point2 = PTonCP + PTtoCenter.*(distancePoints3d(PTonCP, center) - radius); 0041 0042 % Take an arbitrary point of the circle if the point is the center of the circle 0043 if any(all(isnan(point2),2)) 0044 point2(all(isnan(point2),2),:) = center + normalizeVector3d(circlePlane(4:6))*radius; 0045 end 0046 % Take an arbitrary point of the circle if the point lies on the normal of the circle plane 0047 if any(sum(PTtoCenter == 0,2) == 2) 0048 point2(sum(PTtoCenter == 0,2) == 2,:) = center + normalizeVector3d(circlePlane(4:6))*radius; 0049 end 0050 end