Home > matGeom > geom3d > fillSphericalTriangle.m

fillSphericalTriangle

PURPOSE ^

FILLSPHERICALTRIANGLE Fill a triangle on a sphere.

SYNOPSIS ^

function varargout = fillSphericalTriangle(sphere, p1, p2, p3, varargin)

DESCRIPTION ^

FILLSPHERICALTRIANGLE Fill a triangle on a sphere.

   fillSphericalTriangle(SPHERE, PT1, PT2, PT3);


   See also
   fillSphericalPolygon, drawSphericalTriangle, drawSphere

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = fillSphericalTriangle(sphere, p1, p2, p3, varargin)
0002 %FILLSPHERICALTRIANGLE Fill a triangle on a sphere.
0003 %
0004 %   fillSphericalTriangle(SPHERE, PT1, PT2, PT3);
0005 %
0006 %
0007 %   See also
0008 %   fillSphericalPolygon, drawSphericalTriangle, drawSphere
0009 
0010 % ------
0011 % Author: David Legland
0012 % E-mail: david.legland@inrae.fr
0013 % Created: 2005-02-22
0014 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE
0015 
0016 % extract data of the sphere
0017 ori = sphere(:, 1:3);
0018 r   = sphere(4);
0019 
0020 % extract direction vectors for each point
0021 v1  = normalizeVector3d(p1 - ori);
0022 v2  = normalizeVector3d(p2 - ori);
0023 v3  = normalizeVector3d(p3 - ori);
0024 
0025 % create a plane tangent to the sphere containing first point
0026 plane = createPlane(v1, v1);
0027 
0028 % position on the plane of the direction vectors
0029 pp2 = planePosition(intersectLinePlane([ori v2], plane), plane);
0030 pp3 = planePosition(intersectLinePlane([ori v3], plane), plane);
0031 
0032 % create rough parametrization with 2 variables
0033 nTri = 5;
0034 s  = linspace(0, 1, nTri);
0035 t  = linspace(0, 1, nTri);
0036 ns = length(s);
0037 nt = length(t);
0038 s  = repmat(s, [nt, 1]);
0039 t  = repmat(t', [1, ns]);
0040 
0041 % convert to plane coordinates
0042 xp = s * pp2(1) + t .* (1-s) * pp3(1);
0043 yp = s * pp2(2) + t .* (1-s) * pp3(2);
0044 
0045 % convert to 3D coordinates (still on the 3D plane)
0046 x  = plane(1) * ones(size(xp)) + plane(4) * xp + plane(7) * yp - ori(1);
0047 y  = plane(2) * ones(size(xp)) + plane(5) * xp + plane(8) * yp - ori(2);
0048 z  = plane(3) * ones(size(xp)) + plane(6) * xp + plane(9) * yp - ori(3);
0049 
0050 % project on the sphere
0051 norm = hypot(hypot(x, y), z);
0052 xn = x ./ norm * r + ori(1);
0053 yn = y ./ norm * r + ori(2);
0054 zn = z ./ norm * r + ori(3);
0055 
0056 
0057 if nargout == 0
0058     % simply display the patch
0059     surf(xn, yn, zn, 'FaceColor', 'g', 'EdgeColor', 'none', varargin{:});
0060     
0061 elseif nargout == 1
0062     % display the patch and return a handle
0063     h = surf(xn, yn, zn, 'FaceColor', 'g', 'EdgeColor', 'none', varargin{:});
0064     varargout = {h};
0065     
0066 elseif nargout == 3
0067     % If 3 outputs are required, return patch vertex coordinates
0068     varargout = {x, y, z};
0069 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022