MOAB: Mesh Oriented datABase  (version 5.2.1)
MsqGeomPrim.hpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2008 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retain certain rights to this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2008) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file MsqGeomPrim.hpp
00028  *  \brief Classes for primitive geometry
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #ifndef MSQ_MSQ_GEOM_PRIM_HPP
00033 #define MSQ_MSQ_GEOM_PRIM_HPP
00034 
00035 #include "Mesquite.hpp"
00036 #include "Vector3D.hpp"
00037 
00038 namespace MBMesquite
00039 {
00040 
00041 /**\brief Line in R^3 */
00042 class MESQUITE_EXPORT MsqLine
00043 {
00044   private:
00045     Vector3D mPoint;
00046     Vector3D mDirection;  // unit direction
00047 
00048   public:
00049     MsqLine( const Vector3D& p_point, const Vector3D& p_dir ) : mPoint( p_point ), mDirection( p_dir / p_dir.length() )
00050     {
00051     }
00052 
00053     MsqLine() : mPoint( 0, 0, 0 ), mDirection( 0, 0, 0 ) {}
00054 
00055     static MsqLine two_point( const Vector3D& p1, const Vector3D& p2 )
00056     {
00057         return MsqLine( p1, p2 - p1 );
00058     }
00059 
00060     const Vector3D& point() const
00061     {
00062         return mPoint;
00063     }
00064     const Vector3D& direction() const
00065     {
00066         return mDirection;
00067     }
00068 
00069     //! Get point given parameter (mPoint + param * mDirection)
00070     Vector3D point( double param ) const
00071     {
00072         return point() + param * direction();
00073     }
00074 
00075     //! Get parameter value for location on line closest to input point.
00076     double closest( const Vector3D& from_point ) const
00077     {
00078         return mDirection % ( from_point - mPoint );
00079     }
00080 
00081     double distance( const Vector3D& from_point ) const
00082     {
00083         return ( point( closest( from_point ) ) - from_point ).length();
00084     }
00085 
00086     //! Find intersection between two lines
00087     //!\return false if no point intersection (skew, parallel, coincident, etc.)
00088     bool intersect( const MsqLine& other, double& param, double epsilon ) const;
00089 
00090     //! Find parameter of closest position on this line to another line
00091     bool closest( const MsqLine& other, double& param ) const;
00092 };
00093 
00094 /**\brief Circle in R^3 */
00095 class MESQUITE_EXPORT MsqCircle
00096 {
00097   private:
00098     Vector3D mCenter;
00099     Vector3D mNormal;  //!< unit normal
00100     double mRadius;
00101 
00102   public:
00103     MsqCircle() : mCenter( 0, 0, 0 ), mNormal( 0, 0, 0 ), mRadius( 0 ) {}
00104 
00105     MsqCircle( const Vector3D& p_center, const Vector3D& p_normal, double p_radius )
00106         : mCenter( p_center ), mNormal( p_normal / p_normal.length() ), mRadius( p_radius )
00107     {
00108     }
00109 
00110     //! Find circle passing through three points
00111     //!\return false if points are colinear, true otherwise.
00112     static bool three_point( const Vector3D& p1, const Vector3D& p2, const Vector3D& p3, MsqCircle& result );
00113 
00114     //! Find circle with center and two points
00115     //!\return false if points are colinear or not equidistant from center, true otherwise.
00116     static bool two_point( const Vector3D& center, const Vector3D& p1, const Vector3D& p2, MsqCircle& result );
00117 
00118     const Vector3D& center() const
00119     {
00120         return mCenter;
00121     }
00122     const Vector3D& normal() const
00123     {
00124         return mNormal;
00125     }
00126     double radius() const
00127     {
00128         return mRadius;
00129     }
00130 
00131     //! Get arbitrary radial vector (vector in plane with length equal to radius)
00132     Vector3D radial_vector() const;
00133 
00134     //! Find closest point on circle to input position.  Returns
00135     //! arbitrary location on circle if point is at center.
00136     Vector3D closest( const Vector3D& point ) const;
00137 
00138     //! Find closest point on circle to input position, and tangent
00139     //! at that point.  Fails and returns false if point is at center
00140     //! of circle.
00141     bool closest( const Vector3D& point, Vector3D& point_on_circle, Vector3D& tangent_at_point ) const;
00142 };
00143 
00144 /**\brief Plane */
00145 class MESQUITE_EXPORT MsqPlane
00146 {
00147   private:
00148     Vector3D mNormal;  //!< unit normal
00149     double mCoeff;
00150 
00151   public:
00152     MsqPlane( const Vector3D& p_normal, double coeff );
00153     MsqPlane( const Vector3D& p_normal, const Vector3D& p_point );
00154     //! ax+by+cz+d=0
00155     MsqPlane( double a, double b, double c, double d );
00156 
00157     //! get unit normal vector for plane
00158     const Vector3D& normal() const
00159     {
00160         return mNormal;
00161     }
00162 
00163     //! get coefficient term for plane
00164     double coefficient() const
00165     {
00166         return mCoeff;
00167     }
00168 
00169     //! Get point on plane closest to origin.
00170     Vector3D point() const
00171     {
00172         return -mCoeff * mNormal;
00173     }
00174 
00175     //! Get distance from point to plane
00176     double distance( const Vector3D& p_point ) const
00177     {
00178         return fabs( normal() % p_point + coefficient() );
00179     }
00180 
00181     //! Get closest location on plane to input position
00182     Vector3D closest( const Vector3D& p_point ) const
00183     {
00184         return p_point - normal() * ( normal() % p_point + coefficient() );
00185     }
00186 
00187     //! Find intersection of this plane with another.
00188     //!\return false if parallel or invalid plane, true otherwise
00189     bool intersect( const MsqPlane& plane, MsqLine& result ) const;
00190 
00191     //! Find point of itersection with a line
00192     //!\return false if parallel, true otherwise
00193     bool intersect( const MsqLine& line, double& line_param ) const;
00194 };
00195 
00196 /**\brief Sphere */
00197 class MESQUITE_EXPORT MsqSphere
00198 {
00199   private:
00200     Vector3D mCenter;
00201     double mRadius;
00202 
00203   public:
00204     MsqSphere( const Vector3D& p_center, double p_radius ) : mCenter( p_center ), mRadius( p_radius ) {}
00205 
00206     const Vector3D& center() const
00207     {
00208         return mCenter;
00209     }
00210     double radius() const
00211     {
00212         return mRadius;
00213     }
00214 
00215     //! Get location on sphere closest to input location.
00216     //! Returns arbitrary point if input location is at center.
00217     Vector3D closest( const Vector3D& point ) const;
00218 
00219     //! Get location on sphere closest to input location, and
00220     //! normal at closest point.
00221     //!\return false if input point is at center, true otherwise.
00222     bool closest( const Vector3D& point, Vector3D& point_on_sphere, Vector3D& normal_at_point ) const;
00223 
00224     double distance( const Vector3D& point ) const
00225     {
00226         return fabs( ( center() - point ).length() - radius() );
00227     }
00228 
00229     //! Intersect plane with sphere
00230     //!\return true if intersection exists, false otherwise.
00231     bool intersect( const MsqPlane& plane, MsqCircle& result ) const;
00232 
00233     //! Intersect spheres
00234     //!\return true if intersection exists, false otherwise.
00235     bool intersect( const MsqSphere& sphere, MsqCircle& result ) const;
00236 };
00237 
00238 }  // namespace MBMesquite
00239 
00240 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines