Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gfx/Quad3.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Quad3.cpp 1030 2004-02-11 20:46:17Z jungd $
00019   $Revision: 1.1 $
00020   $Date: 2004-02-11 15:46:17 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <math.h>
00026 
00027 #include <gfx/Quad3>
00028 
00029 #include <gfx/Plane>
00030 
00031 using gfx::Quad3;
00032 using gfx::Point3;
00033 using gfx::Segment3;
00034 using gfx::Triangle3;
00035 using gfx::Plane;
00036 
00037 
00038 Quad3::Quad3(const Point3& c1, const Point3& c2, const Point3& c3, const Point3& c4)
00039   : c1(c1), c2(c2), c3(c3), c4(c4) 
00040 {
00041   // check coplanar
00042   Plane p(c1,c2,c3);
00043   Assertm(p.contains(c4),"corners are coplanar");
00044 }  
00045 
00046 
00047 Real Quad3::distanceTo(const Point3& p) const
00048 {
00049   return (p-pointClosestTo(p)).length();
00050 }
00051 
00052 
00053 Point3 Quad3::pointClosestTo(const Point3& p) const
00054 {
00055   Triangle3 t1(c1,c2,c3);
00056   Triangle3 t2(c3,c4,c1);
00057   
00058   Point3 p1 = t1.pointClosestTo(p);
00059   Point3 p2 = t2.pointClosestTo(p);
00060   
00061   if ((p-p1).norm() < (p-p2).norm())
00062     return p1;
00063   else
00064     return p2;
00065 }
00066 
00067   
00068 Segment3 Quad3::shortestSegmentBetween(const Segment3& s) const
00069 {
00070   Triangle3 t1(c1,c2,c3);
00071   Triangle3 t2(c3,c4,c1);
00072 
00073   Segment3 s1 = t1.shortestSegmentBetween(s);
00074   Segment3 s2 = t2.shortestSegmentBetween(s);
00075   
00076   if (s1.norm() < s2.norm())
00077     return s1;
00078   else
00079     return s2;
00080 }
00081  
00082  
00083 Real Quad3::distanceTo(const Segment3& s) const
00084 {
00085   return shortestSegmentBetween(s).length();
00086 }
00087 
00088   
00089 Segment3 Quad3::shortestSegmentBetween(const Triangle3& t) const
00090 {
00091   Triangle3 t1(c1,c2,c3);
00092   Triangle3 t2(c3,c4,c1);
00093   
00094   Segment3 s1 = t1.shortestSegmentBetween(t);
00095   Segment3 s2 = t2.shortestSegmentBetween(t);
00096 
00097   if (s1.norm() < s2.norm())
00098     return s1;
00099   else
00100     return s2;
00101 }
00102 
00103   
00104 Real Quad3::distanceTo(const Triangle3& t) const
00105 {
00106   return shortestSegmentBetween(t).length();
00107 }
00108 
00109 
00110 Segment3 Quad3::shortestSegmentBetween(const Quad3& q) const
00111 {
00112   Triangle3 t1(c1,c2,c3);
00113   Triangle3 t2(c3,c4,c1);
00114   
00115   Segment3 s1 = q.shortestSegmentBetween(t1);
00116   Segment3 s2 = q.shortestSegmentBetween(t2);
00117 
00118   if (s1.norm() < s2.norm())
00119     return s1;
00120   else
00121     return s2;
00122 }
00123 
00124 
00125 Real Quad3::distanceTo(const Quad3& q) const
00126 {
00127   return shortestSegmentBetween(q).length();
00128 }

Generated on Thu Jul 29 15:56:19 2004 for OpenSim by doxygen 1.3.6