// ****************************************************************************
//
//  $Id: cisSegment3D-inl.h,v 1.1.1.1 2001/09/27 20:12:22 anton Exp $
//
//  Description:
//		Inline methods for CISSegment3D.hpp
//
//  Usage:
//		Included only by CISSegment3D.hpp
//
//  See Also:
//		CISSegment3D.hpp
//
//  Author(s):	Andrew Bzostek
//  Created on: ?/?/?
//
//
//              Developed by the Engineering Research Center for
//          Computer-Integrated Surgical Systems & Technology (CISST)
//
//               Copyright(c) 2001, The Johns Hopkins University
//                          All Rights Reserved.
//
//  Experimental Software - Not certified for clinical use.
//  For use only by CISST sponsored research projects.  For use outside of
//  CISST, please contact Dr. Russell Taylor, CISST Director, at rht@cs.jhu.edu
//  or Dr. Gabor Fichtinger, CISST Engineering Director, at gabor@cs.jhu.edu.
//  
// ****************************************************************************


#ifndef _cisAbstractSegment3D_inl_h
#error "include of cisAbstractSegment3D-inl.h is not allowed, include cisAbstractSegment3D.h instead"
#endif

#include "cisLine3D.h"

#include <assert.h>


inline
cisScalar cisSegment3D::GetLength() const
{
  return (GetVertex1() - GetVertex0()).Length();
}


inline
cisSegment3D::LocalDirectionType cisSegment3D::GetDirection() const
{
  return (GetVertex1() - GetVertex0()).Normalize();
}


inline
cisSegment3D::LocalDirectionType cisSegment3D::GetDiffVector() const
{ 
  return (GetVertex1() - GetVertex0());
}


inline
cisSegment3D::LocalPointType cisSegment3D::GetMidPoint() const
{
  return ( (GetVertex1() + GetVertex0()) / 2);
}


inline
cisSegment3D::LocalPointType cisSegment3D::EvalAt(ParametrizationType p) const
{
	assert( (0 <= 0) && (p <= 1) );
	return ( (1-p) * GetVertex0() + p * GetVertex1() );
}


inline
cisSegment3D::ParametrizationType 
cisSegment3D::ProjectPointLocal(const GlobalPointType & pt) const
{
	const LocalPointType & vertex1 = GetVertex0();
	const LocalPointType & vertex2 = GetVertex1();

	LocalDirectionType dir( vertex2 - vertex1 );
	cisVec3 displacement( pt - vertex1) );

	cisScalar prod = (displacement * dir) / dir.LengthSquare();

	return 
		(prod < 0) ? 0
		: (prod > 1) ? 1
		: prod;
}


inline
cisSegment3D::LocalPointType
cisSegment3D::ProjectPoint(const GlobalPointType & pt) const
{
	ParametrizationType p = ProjectPointLocal(pt);
	return this->EvalAt(p);
}


inline
cisScalar cisSegment3D::DistanceSquareToPoint(const GlobalPointType & pt) const
{
	LocalPointType proj( ProjectPoint(pt) );
	LocalDirectionType diff(pt - proj);
	return diff.LengthSquare();
}
		

inline
cisScalar cisSegment3D::DistanceToPoint(const GlobalPointType & pt) const
{
	return sqrt(DistanceSquareToPoint(pt));
}


// ****************************************************************************
//                              Change History
// ****************************************************************************
//
//  $Log: cisSegment3D-inl.h,v $
//  Revision 1.1.1.1  2001/09/27 20:12:22  anton
//  Creation of new repository for CIS 2.
//
//
//  Older:
//
//  Revision 1.2  2001/05/29 19:59:46  anton
//  Rewrote effectless (Vec3)(x, y, z) to Vec3(x, y, z) when converting IntVec3 to Vec3
//
//  Revision 1.1  2001/03/26 19:32:05  anton
//  Import of GeomObj within CIS from Andy Bzostek code with some formating and cleaning.  Add some -inl.hpp files.
//
//
// ****************************************************************************


