% MotionGenesis file: TetrahedronCMPosition.r
% Copyright (c) 2025-2026 Motion Genesis LLC.  All rights reserved.
%----------------------------------------------------------------------------
%TetrahedronCMPosition
%TetrahedronCMPosition
%
% Purpose:  Calculates a tetrahedron's center of mass position from an arbitrary
%           point P, using position vectors from P to tetrahedron vertices.
%
%Syntax 1:  TetrahedronCMPosition( p0>, p1>, p2>, p3> )
%Syntax 2:  TetrahedronCMPosition( p1>, p2>, p3> )
%
%   Input:  p0> -- position vector from point P to vertex 0.
%           p1> -- position vector from point P to vertex 1.
%           p2> -- position vector from point P to vertex 2.
%           p3> -- position vector from point P to vertex 3.
%
%Output 1:  Position vector from P to the tetrahedron's center of mass.
%Output 2:  Position vector from P to the tetrahedron's center of mass, where
%           point P is vertex 0.
%
% Related:  SetMass          GetMass         SetInertia
%           GetCmPosition    GetCmVelocity   GetCmAcceleration
%           GetInertiaDyadic  GetInertiaMatrix  GetMomentOfInertia
%           GetProductOfInertia  GetRadiusOfGyration  GetInertiaVector
%	    TriangleArea       TriangleCMPosition     TriangleInertiaAboutVertex0
%	    TetrahedronVolume  TetrahedronCMPosition  TetrahedronInertiaAboutVertex0
%----------------------------------------------------------------------------
IF(     #NUM_ARGS# < 3 )  { echo("\n\a\kError: The command TetrahedronCMPosition has too few arguments.") }
ELSEIF( #NUM_ARGS# > 4 )  { echo("\n\a\kError: The command TetrahedronCMPosition has too many arguments.") }
ELSEIF( #NUM_ARGS# == 4 ) 
{ 
   (#1# + #2# + #3# + #4#)/4
%  TetrahedronCMPosition(#2# - #1#, #3# - #1#, #4# - #1#) + #1#  % Less efficient version.
}
ELSE 
{ 
   (#1# + #2# + #3#)/4
}
% end of file
