orientCurves.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools version 1.1
00003 //
00004 // GoTools module: CORE
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _ORIENTCURVES_H
00034 #define _ORIENTCURVES_H
00035 
00036 namespace Go
00037 {
00040 
00041 
00042 //===========================================================================
00065 template <class PtrToCurveType>
00066 inline void orientCurves(const std::vector<PtrToCurveType>& curves,
00067                          std::vector<int>& permutation,
00068                          std::vector<bool>& reversed,
00069                          double neighbour_tol,
00070                          bool assume_manifold = true)
00071 //===========================================================================
00072 {
00073     // registering all points
00074     int i, num_seg = curves.size();
00075     std::vector<Point> endpoints(2 * num_seg);
00076     for (i = 0; i < num_seg; ++i) {
00077         curves[i]->point(endpoints[2 * i + 0], curves[i]->startparam());
00078         curves[i]->point(endpoints[2 * i + 1], curves[i]->endparam());
00079     }
00080 
00081     // detecting connections
00082     std::vector<int> connected_to(2 * num_seg, -1);
00083     for (i = 0; i < 2 * num_seg; ++i) {
00084         if (connected_to[i] == -1 || !assume_manifold) {
00085             Point& p1 = endpoints[i];
00086             bool found = (connected_to[i] != -1);
00087             // this point has not been checked for connections yet
00088             for (int j = i+1; j < 2 * num_seg; ++j) {
00089                 if (connected_to[j] == -1) {
00090                     if (p1.dist(endpoints[j]) < neighbour_tol) {
00091                         // we consider these points to be connected
00092                         connected_to[i] = j;
00093                         connected_to[j] = i;
00094                         if (assume_manifold) {
00095                             break;
00096                         } else if (found == true) {
00097                             THROW("Multiple connected points detected in "
00098                                   " orientCurves().  Not a 1-manifold!" );
00099                         } else { 
00100                             found = true;
00101                         }
00102                     }
00103                 }
00104             }
00105         }
00106     }
00107 
00108     // Making the permutation std::vector
00109     permutation.clear();
00110     permutation.reserve(num_seg);
00111     reversed.clear();
00112     reversed.reserve(num_seg);
00113     std::vector<bool> visited(2*num_seg, false);
00114     // First handle all open chains
00115     for (i = 0; i < 2*num_seg; ++i) {
00116         if (!visited[i] && connected_to[i] == -1) {
00117             int current = i;
00118             do {
00119                 permutation.push_back(current/2);
00120                 bool current_parity = (current%2 == 0);
00121                 int partner = current_parity ? current + 1 : current - 1;
00122                 reversed.push_back(!current_parity);
00123                 visited[current] = true;
00124                 visited[partner] = true;
00125                 current = connected_to[partner];
00126             } while (current != -1);
00127         }
00128     }
00129     // Then all closed chains (loops)
00130     for (i = 0; i < 2*num_seg; ++i) {
00131         if (!visited[i]) {
00132             int current = i;
00133             do {
00134                 permutation.push_back(current/2);
00135                 bool current_parity = (current%2 == 0);
00136                 int partner = current_parity ? current + 1 : current - 1;
00137                 reversed.push_back(!current_parity);
00138                 visited[current] = true;
00139                 visited[partner] = true;
00140                 current = connected_to[partner];
00141             } while (current != i);
00142         }
00143     }
00144 }
00145 
00146 
00148 } // namespace Go
00149 
00150 #endif // _ORIENTCURVES_H
00151 
00152 

Generated on Mon Jun 11 14:48:18 2007 for GoTools Core Library by  doxygen 1.5.1