![]() |
MechSys
1.0
Computing library for simulations in continuum and discrete mechanics
|
00001 /************************************************************************ 00002 * MechSys - Open Library for Mechanical Systems * 00003 * Copyright (C) 2005 Dorival M. Pedroso * 00004 * * 00005 * This program is free software: you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation, either version 3 of the License, or * 00008 * any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program. If not, see <http://www.gnu.org/licenses/> * 00017 ************************************************************************/ 00018 00019 /* CHull2D - Copyright (C) 2007 Dorival de Moraes Pedroso */ 00020 00021 #ifndef MPM_CHULL2D_H 00022 #define MPM_CHULL2D_H 00023 00024 // STL 00025 #include<algorithm> // for std::sort 00026 00027 // Local 00028 #include <mechsys/mpm/defs.h> 00029 #include <mechsys/util/array.h> 00030 00031 namespace MPM { 00032 00034 inline bool CompareVec2D (Vector3D const & A, Vector3D const & B) 00035 { 00036 return (A(0)<B(0) || (A(0)==B(0) && A(1)<B(1))); 00037 } 00038 00042 inline double Cross2D (Vector3D const & O, Vector3D const & A, Vector3D const & B) 00043 { 00044 return (A(0)-O(0)) * (B(1)-O(1)) - (A(1)-O(1)) * (B(0)-O(0)); 00045 } 00046 00049 inline void CHull2D (Array<Vector3D> & P, Array<size_t> & H) 00050 { 00051 // Input 00052 int n = P.Size(); 00053 int k = 0; 00054 Array<Vector3D> h(2*n); // points on the hull 00055 Array<size_t> j(2*n); // indexes of the points in the hull 00056 00057 // Sort points lexicographically 00058 std::sort (P.GetPtr(), P.GetPtr()+P.Size(), CompareVec2D); 00059 00060 // Build lower hull 00061 for (int i=0; i<n; i++) 00062 { 00063 while (k>=2 && Cross2D (h[k-2], h[k-1], P[i])<=0) k--; 00064 h[k++] = P[i]; 00065 j[k-1] = i; 00066 } 00067 00068 // Build upper hull 00069 for (int i=n-2, t=k+1; i>=0; i--) 00070 { 00071 while (k >= t && Cross2D (h[k-2], h[k-1], P[i])<=0) k--; 00072 h[k++] = P[i]; 00073 j[k-1] = i; 00074 } 00075 00076 // Results 00077 if (k>1) 00078 { 00079 H.Resize(k-1); 00080 for (int i=0; i<k-1; ++i) H[i] = j[i]; 00081 } 00082 } 00083 00084 }; // namespace MPM 00085 00086 #endif // MPM_CHULL2D_H