Branch data Line data Source code
1 : : /* *****************************************************************
2 : : MESQUITE -- The Mesh Quality Improvement Toolkit
3 : :
4 : : Copyright 2008 Sandia National Laboratories. Developed at the
5 : : University of Wisconsin--Madison under SNL contract number
6 : : 624796. The U.S. Government and the University of Wisconsin
7 : : retain certain rights to this software.
8 : :
9 : : This library is free software; you can redistribute it and/or
10 : : modify it under the terms of the GNU Lesser General Public
11 : : License as published by the Free Software Foundation; either
12 : : version 2.1 of the License, or (at your option) any later version.
13 : :
14 : : This library is distributed in the hope that it will be useful,
15 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : Lesser General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU Lesser General Public License
20 : : (lgpl.txt) along with this library; if not, write to the Free Software
21 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 : :
23 : : (2008) [email protected]
24 : :
25 : : ***************************************************************** */
26 : :
27 : : /** \file MsqGeomPrim.hpp
28 : : * \brief Classes for primitive geometry
29 : : * \author Jason Kraftcheck
30 : : */
31 : :
32 : : #ifndef MSQ_MSQ_GEOM_PRIM_HPP
33 : : #define MSQ_MSQ_GEOM_PRIM_HPP
34 : :
35 : : #include "Mesquite.hpp"
36 : : #include "Vector3D.hpp"
37 : :
38 : : namespace MBMesquite
39 : : {
40 : :
41 : : /**\brief Line in R^3 */
42 : 0 : class MESQUITE_EXPORT MsqLine
43 : : {
44 : : private:
45 : : Vector3D mPoint;
46 : : Vector3D mDirection; // unit direction
47 : :
48 : : public:
49 : 24 : MsqLine( const Vector3D& p_point, const Vector3D& p_dir ) : mPoint( p_point ), mDirection( p_dir / p_dir.length() )
50 : : {
51 : 12 : }
52 : :
53 : : MsqLine() : mPoint( 0, 0, 0 ), mDirection( 0, 0, 0 ) {}
54 : :
55 : : static MsqLine two_point( const Vector3D& p1, const Vector3D& p2 )
56 : : {
57 : : return MsqLine( p1, p2 - p1 );
58 : : }
59 : :
60 : 192 : const Vector3D& point() const
61 : : {
62 : 192 : return mPoint;
63 : : }
64 : 192 : const Vector3D& direction() const
65 : : {
66 : 192 : return mDirection;
67 : : }
68 : :
69 : : //! Get point given parameter (mPoint + param * mDirection)
70 : 192 : Vector3D point( double param ) const
71 : : {
72 [ + - ][ + - ]: 192 : return point() + param * direction();
73 : : }
74 : :
75 : : //! Get parameter value for location on line closest to input point.
76 : 216 : double closest( const Vector3D& from_point ) const
77 : : {
78 [ + - ]: 216 : return mDirection % ( from_point - mPoint );
79 : : }
80 : :
81 : : double distance( const Vector3D& from_point ) const
82 : : {
83 : : return ( point( closest( from_point ) ) - from_point ).length();
84 : : }
85 : :
86 : : //! Find intersection between two lines
87 : : //!\return false if no point intersection (skew, parallel, coincident, etc.)
88 : : bool intersect( const MsqLine& other, double& param, double epsilon ) const;
89 : :
90 : : //! Find parameter of closest position on this line to another line
91 : : bool closest( const MsqLine& other, double& param ) const;
92 : : };
93 : :
94 : : /**\brief Circle in R^3 */
95 : 0 : class MESQUITE_EXPORT MsqCircle
96 : : {
97 : : private:
98 : : Vector3D mCenter;
99 : : Vector3D mNormal; //!< unit normal
100 : : double mRadius;
101 : :
102 : : public:
103 : : MsqCircle() : mCenter( 0, 0, 0 ), mNormal( 0, 0, 0 ), mRadius( 0 ) {}
104 : :
105 : 0 : MsqCircle( const Vector3D& p_center, const Vector3D& p_normal, double p_radius )
106 : 0 : : mCenter( p_center ), mNormal( p_normal / p_normal.length() ), mRadius( p_radius )
107 : : {
108 : 0 : }
109 : :
110 : : //! Find circle passing through three points
111 : : //!\return false if points are colinear, true otherwise.
112 : : static bool three_point( const Vector3D& p1, const Vector3D& p2, const Vector3D& p3, MsqCircle& result );
113 : :
114 : : //! Find circle with center and two points
115 : : //!\return false if points are colinear or not equidistant from center, true otherwise.
116 : : static bool two_point( const Vector3D& center, const Vector3D& p1, const Vector3D& p2, MsqCircle& result );
117 : :
118 : 0 : const Vector3D& center() const
119 : : {
120 : 0 : return mCenter;
121 : : }
122 : 0 : const Vector3D& normal() const
123 : : {
124 : 0 : return mNormal;
125 : : }
126 : 0 : double radius() const
127 : : {
128 : 0 : return mRadius;
129 : : }
130 : :
131 : : //! Get arbitrary radial vector (vector in plane with length equal to radius)
132 : : Vector3D radial_vector() const;
133 : :
134 : : //! Find closest point on circle to input position. Returns
135 : : //! arbitrary location on circle if point is at center.
136 : : Vector3D closest( const Vector3D& point ) const;
137 : :
138 : : //! Find closest point on circle to input position, and tangent
139 : : //! at that point. Fails and returns false if point is at center
140 : : //! of circle.
141 : : bool closest( const Vector3D& point, Vector3D& point_on_circle, Vector3D& tangent_at_point ) const;
142 : : };
143 : :
144 : : /**\brief Plane */
145 : : class MESQUITE_EXPORT MsqPlane
146 : : {
147 : : private:
148 : : Vector3D mNormal; //!< unit normal
149 : : double mCoeff;
150 : :
151 : : public:
152 : : MsqPlane( const Vector3D& p_normal, double coeff );
153 : : MsqPlane( const Vector3D& p_normal, const Vector3D& p_point );
154 : : //! ax+by+cz+d=0
155 : : MsqPlane( double a, double b, double c, double d );
156 : :
157 : : //! get unit normal vector for plane
158 : 0 : const Vector3D& normal() const
159 : : {
160 : 0 : return mNormal;
161 : : }
162 : :
163 : : //! get coefficient term for plane
164 : 0 : double coefficient() const
165 : : {
166 : 0 : return mCoeff;
167 : : }
168 : :
169 : : //! Get point on plane closest to origin.
170 : : Vector3D point() const
171 : : {
172 : : return -mCoeff * mNormal;
173 : : }
174 : :
175 : : //! Get distance from point to plane
176 : : double distance( const Vector3D& p_point ) const
177 : : {
178 : : return fabs( normal() % p_point + coefficient() );
179 : : }
180 : :
181 : : //! Get closest location on plane to input position
182 : 0 : Vector3D closest( const Vector3D& p_point ) const
183 : : {
184 [ # # ]: 0 : return p_point - normal() * ( normal() % p_point + coefficient() );
185 : : }
186 : :
187 : : //! Find intersection of this plane with another.
188 : : //!\return false if parallel or invalid plane, true otherwise
189 : : bool intersect( const MsqPlane& plane, MsqLine& result ) const;
190 : :
191 : : //! Find point of itersection with a line
192 : : //!\return false if parallel, true otherwise
193 : : bool intersect( const MsqLine& line, double& line_param ) const;
194 : : };
195 : :
196 : : /**\brief Sphere */
197 : : class MESQUITE_EXPORT MsqSphere
198 : : {
199 : : private:
200 : : Vector3D mCenter;
201 : : double mRadius;
202 : :
203 : : public:
204 : : MsqSphere( const Vector3D& p_center, double p_radius ) : mCenter( p_center ), mRadius( p_radius ) {}
205 : :
206 : 0 : const Vector3D& center() const
207 : : {
208 : 0 : return mCenter;
209 : : }
210 : 0 : double radius() const
211 : : {
212 : 0 : return mRadius;
213 : : }
214 : :
215 : : //! Get location on sphere closest to input location.
216 : : //! Returns arbitrary point if input location is at center.
217 : : Vector3D closest( const Vector3D& point ) const;
218 : :
219 : : //! Get location on sphere closest to input location, and
220 : : //! normal at closest point.
221 : : //!\return false if input point is at center, true otherwise.
222 : : bool closest( const Vector3D& point, Vector3D& point_on_sphere, Vector3D& normal_at_point ) const;
223 : :
224 : : double distance( const Vector3D& point ) const
225 : : {
226 : : return fabs( ( center() - point ).length() - radius() );
227 : : }
228 : :
229 : : //! Intersect plane with sphere
230 : : //!\return true if intersection exists, false otherwise.
231 : : bool intersect( const MsqPlane& plane, MsqCircle& result ) const;
232 : :
233 : : //! Intersect spheres
234 : : //!\return true if intersection exists, false otherwise.
235 : : bool intersect( const MsqSphere& sphere, MsqCircle& result ) const;
236 : : };
237 : :
238 : : } // namespace MBMesquite
239 : :
240 : : #endif
|