1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* $Revision: 2552 $
*
* last checkin:
* $Author: gutwenger $
* $Date: 2012-07-05 16:45:20 +0200 (Do, 05. Jul 2012) $
***************************************************************/
/** \file
* \brief Implementation of class numexcept (handling of numeric problems).
*
* \author Stefan Hachul
*
* \par License:
* This file is part of the Open Graph Drawing Framework (OGDF).
*
* \par
* Copyright (C)<br>
* See README.txt in the root directory of the OGDF installation for details.
*
* \par
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* Version 2 or 3 as published by the Free Software Foundation;
* see the file LICENSE.txt included in the packaging of this file
* for details.
*
* \par
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* \par
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* \see http://www.gnu.org/copyleft/gpl.html
***************************************************************/
#include "numexcept.h"
#include "../basic/basic.h"
#define epsilon 0.1
#define POS_SMALL_DOUBLE 1e-300
#define POS_BIG_DOUBLE 1e+300
namespace ogdf {
DPoint numexcept::choose_distinct_random_point_in_disque(DPoint old_point,
double xmin,double xmax,double ymin,double ymax)
{
const int BILLION = 1000000000;
double mindist;//minimal distance from old_point to the boundaries of the disc
double mindist_to_xmin,mindist_to_xmax,mindist_to_ymin,mindist_to_ymax;
double rand_x,rand_y;
DPoint new_point;
mindist_to_xmin = old_point.m_x - xmin;
mindist_to_xmax = xmax - old_point.m_x;
mindist_to_ymin = old_point.m_y - ymin;
mindist_to_ymax = ymax - old_point.m_y;
mindist = min(min(mindist_to_xmin,mindist_to_xmax), min(mindist_to_ymin,mindist_to_ymax));
if(mindist > 0)
do {
//assign random double values in range (-1,1)
rand_x = 2*(double(randomNumber(1,BILLION)+1)/(BILLION+2)-0.5);
rand_y = 2*(double(randomNumber(1,BILLION)+1)/(BILLION+2)-0.5);
new_point.m_x = old_point.m_x+mindist*rand_x*epsilon;
new_point.m_y = old_point.m_y+mindist*rand_y*epsilon;
} while((old_point == new_point)||((old_point-new_point).norm() >= mindist*epsilon));
else if(mindist == 0) //old_point lies at the boundaries
{//else1
double mindist_x =0;
double mindist_y =0;
if (mindist_to_xmin > 0)
mindist_x = (-1)* mindist_to_xmin;
else if (mindist_to_xmax > 0)
mindist_x = mindist_to_xmax;
if (mindist_to_ymin > 0)
mindist_y = (-1)* mindist_to_ymin;
else if (mindist_to_ymax > 0)
mindist_y = mindist_to_ymax;
if((mindist_x != 0)||(mindist_y != 0))
do {
//assign random double values in range (0,1)
rand_x = double(randomNumber(1,BILLION)+1)/(BILLION+2);
rand_y = double(randomNumber(1,BILLION)+1)/(BILLION+2);
new_point.m_x = old_point.m_x+mindist_x*rand_x*epsilon;
new_point.m_y = old_point.m_y+mindist_y*rand_y*epsilon;
} while(old_point == new_point);
else
cout<<"Error DIM2:: box is equal to old_pos"<<endl;
}//else1
else //mindist < 0
{//else2
cout<<"Error DIM2:: choose_distinct_random_point_in_disque: old_point not ";
cout<<"in box"<<endl;
}//else2
return new_point;
}
DPoint numexcept::choose_distinct_random_point_in_radius_epsilon(DPoint old_pos)
{
double xmin = old_pos.m_x-1*epsilon;
double xmax = old_pos.m_x+1*epsilon;
double ymin = old_pos.m_y-1*epsilon;
double ymax = old_pos.m_y+1*epsilon;
return choose_distinct_random_point_in_disque(old_pos,xmin,xmax,ymin,ymax);
}
bool numexcept::f_rep_near_machine_precision(double distance,DPoint& force )
{
const double POS_BIG_LIMIT = POS_BIG_DOUBLE * 1e-190;
const double POS_SMALL_LIMIT = POS_SMALL_DOUBLE * 1e190;
const int BILLION = 1000000000;
if(distance > POS_BIG_LIMIT)
{
//create random number in range (0,1)
double randx = double(randomNumber(1,BILLION)+1)/(BILLION+2);
double randy = double(randomNumber(1,BILLION)+1)/(BILLION+2);
int rand_sign_x = randomNumber(0,1);
int rand_sign_y = randomNumber(0,1);
force.m_x = POS_SMALL_LIMIT*(1+randx)*pow(-1.0,rand_sign_x);
force.m_y = POS_SMALL_LIMIT*(1+randy)*pow(-1.0,rand_sign_y);
return true;
} else if (distance < POS_SMALL_LIMIT)
{
//create random number in range (0,1)
double randx = double(randomNumber(1,BILLION)+1)/(BILLION+2);
double randy = double(randomNumber(1,BILLION)+1)/(BILLION+2);
int rand_sign_x = randomNumber(0,1);
int rand_sign_y = randomNumber(0,1);
force.m_x = POS_BIG_LIMIT*randx*pow(-1.0,rand_sign_x);
force.m_y = POS_BIG_LIMIT*randy*pow(-1.0,rand_sign_y);
return true;
} else
return false;
}
bool numexcept::f_near_machine_precision(double distance,DPoint& force )
{
const double POS_BIG_LIMIT = POS_BIG_DOUBLE * 1e-190;
const double POS_SMALL_LIMIT = POS_SMALL_DOUBLE * 1e190;
const int BILLION = 1000000000;
if(distance < POS_SMALL_LIMIT)
{
//create random number in range (0,1)
double randx = double(randomNumber(1,BILLION)+1)/(BILLION+2);
double randy = double(randomNumber(1,BILLION)+1)/(BILLION+2);
int rand_sign_x = randomNumber(0,1);
int rand_sign_y = randomNumber(0,1);
force.m_x = POS_SMALL_LIMIT*(1+randx)*pow(-1.0,rand_sign_x);
force.m_y = POS_SMALL_LIMIT*(1+randy)*pow(-1.0,rand_sign_y);
return true;
} else if (distance > POS_BIG_LIMIT)
{
//create random number in range (0,1)
double randx = double(randomNumber(1,BILLION)+1)/(BILLION+2);
double randy = double(randomNumber(1,BILLION)+1)/(BILLION+2);
int rand_sign_x = randomNumber(0,1);
int rand_sign_y = randomNumber(0,1);
force.m_x = POS_BIG_LIMIT*randx*pow(-1.0,rand_sign_x);
force.m_x = POS_BIG_LIMIT*randy*pow(-1.0,rand_sign_y);
return true;
} else
return false;
}
bool numexcept::nearly_equal(double a,double b)
{
double delta = 1e-10;
double small_b,big_b;
if(b > 0) {
small_b = b*(1-delta);
big_b = b*(1+delta);
} else //b <= 0
{
small_b = b*(1+delta);
big_b = b*(1-delta);
}
if((small_b <= a) && (a <= big_b))
return true;
else
return false;
}
}//namespace ogdf
|