Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: Marching square algorithm 4 : * Purpose: Core algorithm implementation for contour line generation. 5 : * Author: Oslandia <infos at oslandia dot com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2018, Oslandia <infos at oslandia dot com> 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : #ifndef MARCHING_SQUARE_UTILITY_H 29 : #define MARCHING_SQUARE_UTILITY_H 30 : 31 : #include <limits> 32 : #include <cmath> 33 : 34 : namespace marching_squares 35 : { 36 : 37 : // This is used to determine the maximum level value for polygons, 38 : // the one that spans all the remaining plane 39 : const double Inf = std::numeric_limits<double>::max(); 40 : 41 : const double NaN = std::numeric_limits<double>::quiet_NaN(); 42 : 43 : #define debug(format, ...) CPLDebug("MarchingSquare", format, ##__VA_ARGS__) 44 : 45 : // Perturb a value if it is too close to a level value 46 1707947 : inline double fudge(double level, double value) 47 : { 48 : // FIXME 49 : // This is too "hard coded". The perturbation to apply really depend on 50 : // values between which we have to interpolate, so that the result of 51 : // interpolation should give coordinates that are "numerically" stable for 52 : // classical algorithms to work (on polygons for instance). 53 : // 54 : // Ideally we should probably use snap rounding to ensure no contour lines 55 : // are within a user-provided minimum distance. 56 : 57 1707947 : const double absTol = 1e-6; 58 1707947 : return std::abs(level - value) < absTol ? value + absTol : value; 59 : } 60 : 61 : } // namespace marching_squares 62 : #endif