source: projectionDesigner/trunk/projdesigner/include/gmtl/LineSegOps.h @ 4

Last change on this file since 4 was 4, checked in by Torben Dannhauer, 14 years ago
File size: 3.1 KB
Line 
1/************************************************************** ggt-head beg
2 *
3 * GGT: Generic Graphics Toolkit
4 *
5 * Original Authors:
6 *   Allen Bierbaum
7 *
8 * -----------------------------------------------------------------
9 * File:          LineSegOps.h,v
10 * Date modified: 2004/09/16 21:21:09
11 * Version:       1.8
12 * -----------------------------------------------------------------
13 *
14 *********************************************************** ggt-head end */
15/*************************************************************** ggt-cpr beg
16*
17* GGT: The Generic Graphics Toolkit
18* Copyright (C) 2001,2002 Allen Bierbaum
19*
20* This library is free software; you can redistribute it and/or
21* modify it under the terms of the GNU Lesser General Public
22* License as published by the Free Software Foundation; either
23* version 2.1 of the License, or (at your option) any later version.
24*
25* This library is distributed in the hope that it will be useful,
26* but WITHOUT ANY WARRANTY; without even the implied warranty of
27* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28* Lesser General Public License for more details.
29*
30* You should have received a copy of the GNU Lesser General Public
31* License along with this library; if not, write to the Free Software
32* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33*
34 ************************************************************ ggt-cpr end */
35#ifndef _GMTL_LINESEGOPS_H_
36#define _GMTL_LINESEGOPS_H_
37
38#include <gmtl/LineSeg.h>
39#include <gmtl/RayOps.h>
40
41namespace gmtl {
42
43/**
44 * Finds the closest point on the line segment to a given point.
45 *
46 * @param lineseg    the line segment to test
47 * @param pt         the point which to test against lineseg
48 *
49 * @return  the point on the line segment closest to pt
50 */
51template< class DATA_TYPE >
52Point<DATA_TYPE, 3> findNearestPt( const LineSeg<DATA_TYPE>& lineseg,
53                                   const Point<DATA_TYPE, 3>& pt )
54{
55   // result = origin + dir * dot((pt-origin), dir)
56   return ( lineseg.mOrigin + lineseg.mDir *
57            dot(pt - lineseg.mOrigin, lineseg.mDir) );
58}
59
60/**
61 * Computes the shortest distance from the line segment to the given point.
62 *
63 * @param lineseg    the line segment to test
64 * @param pt         the point which to test against lineseg
65 *
66 * @return  the shortest distance from pt to lineseg
67 */
68template< class DATA_TYPE >
69inline DATA_TYPE distance( const LineSeg<DATA_TYPE>& lineseg,
70                           const Point<DATA_TYPE, 3>& pt )
71{
72   return gmtl::length(gmtl::Vec<DATA_TYPE, 3>(pt - findNearestPt(lineseg, pt)));
73}
74
75/**
76 * Computes the shortest distance from the line segment to the given point.
77 *
78 * @param lineseg    the line segment to test
79 * @param pt         the point which to test against lineseg
80 *
81 * @return  the squared shortest distance from pt to lineseg (value is squared, this func is slightly faster since it doesn't involve a sqrt)
82 */
83template< class DATA_TYPE >
84inline DATA_TYPE distanceSquared( const LineSeg<DATA_TYPE>& lineseg,
85                           const Point<DATA_TYPE, 3>& pt )
86{
87   return gmtl::lengthSquared(gmtl::Vec<DATA_TYPE, 3>(pt - findNearestPt(lineseg, pt)));
88}
89
90
91} // namespace gmtl
92#endif
Note: See TracBrowser for help on using the repository browser.