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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/**********************************************************************
* $Id: RobustDeterminant.cpp 1820 2006-09-06 16:54:23Z mloskot $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* Copyright (C) 2006 Refractions Research Inc.
* Copyright (C) 2001-2002 Vivid Solutions Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************
*
* RobustDeterminant implements an algorithm to compute the
* sign of a 2x2 determinant for double precision values robustly.
* It is a direct translation of code developed by Olivier Devillers.
*
* The original code carries the following copyright notice:
*
*************************************************************************
* Author : Olivier Devillers
* Olivier.Devillers@sophia.inria.fr
* http:/www.inria.fr:/prisme/personnel/devillers/anglais/determinant.html
**************************************************************************
*
**************************************************************************
* Copyright (c) 1995 by INRIA Prisme Project
* BP 93 06902 Sophia Antipolis Cedex, France.
* All rights reserved
**************************************************************************
*/
#include <geos/algorithm/RobustDeterminant.h>
#include <cmath>
namespace geos {
namespace algorithm { // geos.algorithm
int RobustDeterminant::signOfDet2x2(double x1,double y1,double x2,double y2) {
// returns -1 if the determinant is negative,
// returns 1 if the determinant is positive,
// retunrs 0 if the determinant is null.
int sign=1;
double swap;
double k;
long count=0;
/*
* testing null entries
*/
if ((x1==0.0) || (y2==0.0)) {
if ((y1==0.0) || (x2==0.0)) {
return 0;
} else if (y1>0) {
if (x2>0) {
return -sign;
} else {
return sign;
}
} else {
if (x2>0) {
return sign;
} else {
return -sign;
}
}
}
if ((y1==0.0) || (x2==0.0)) {
if (y2>0) {
if (x1>0) {
return sign;
} else {
return -sign;
}
} else {
if (x1>0) {
return -sign;
} else {
return sign;
}
}
}
/*
* making y coordinates positive and permuting the entries
* so that y2 is the biggest one
*/
if (0.0<y1) {
if (0.0<y2) {
if (y1<=y2) {
;
} else {
sign=-sign;
swap=x1;
x1=x2;
x2=swap;
swap=y1;
y1=y2;
y2=swap;
}
} else {
if (y1<=-y2) {
sign=-sign;
x2=-x2;
y2=-y2;
} else {
swap=x1;
x1=-x2;
x2=swap;
swap=y1;
y1=-y2;
y2=swap;
}
}
} else {
if (0.0<y2) {
if (-y1<=y2) {
sign=-sign;
x1=-x1;
y1=-y1;
} else {
swap=-x1;
x1=x2;
x2=swap;
swap=-y1;
y1=y2;
y2=swap;
}
} else {
if (y1>=y2) {
x1=-x1;
y1=-y1;
x2=-x2;
y2=-y2;
} else {
sign=-sign;
swap=-x1;
x1=-x2;
x2=swap;
swap=-y1;
y1=-y2;
y2=swap;
}
}
}
/*
* making x coordinates positive
*/
/*
* if |x2|<|x1| one can conclude
*/
if (0.0<x1) {
if (0.0<x2) {
if (x1 <= x2) {
;
} else {
return sign;
}
} else {
return sign;
}
} else {
if (0.0<x2) {
return -sign;
} else {
if (x1 >= x2) {
sign=-sign;
x1=-x1;
x2=-x2;
} else {
return -sign;
}
}
}
/*
* all entries strictly positive x1 <= x2 and y1 <= y2
*/
while (true) {
count=count+1;
k=floor(x2/x1);
x2=x2-k*x1;
y2=y2-k*y1;
/*
* testing if R (new U2) is in U1 rectangle
*/
if (y2<0.0) {
return -sign;
}
if (y2>y1) {
return sign;
}
/*
* finding R'
*/
if (x1>x2+x2) {
if (y1<y2+y2) {
return sign;
}
} else {
if (y1>y2+y2) {
return -sign;
} else {
x2=x1-x2;
y2=y1-y2;
sign=-sign;
}
}
if (y2==0.0) {
if (x2==0.0) {
return 0;
} else {
return -sign;
}
}
if (x2==0.0) {
return sign;
}
/*
* exchange 1 and 2 role.
*/
k=floor(x1/x2);
x1=x1-k*x2;
y1=y1-k*y2;
/*
* testing if R (new U1) is in U2 rectangle
*/
if (y1<0.0) {
return sign;
}
if (y1>y2) {
return -sign;
}
/*
* finding R'
*/
if (x2>x1+x1) {
if (y2<y1+y1) {
return -sign;
}
} else {
if (y2>y1+y1) {
return sign;
} else {
x1=x2-x1;
y1=y2-y1;
sign=-sign;
}
}
if (y1==0.0) {
if (x1==0.0) {
return 0;
} else {
return sign;
}
}
if (x1==0.0) {
return -sign;
}
}
}
} // namespace geos.algorithm
} // namespace geos
/**********************************************************************
* $Log$
* Revision 1.10 2006/03/21 11:12:23 strk
* Cleanups: headers inclusion and Log section
*
* Revision 1.9 2006/03/21 10:46:03 strk
* streamlined header inclusion, put original copyright on top
*
* Revision 1.8 2006/02/19 19:46:49 strk
* Packages <-> namespaces mapping for most GEOS internal code (uncomplete, but working). Dir-level libs for index/ subdirs.
*
**********************************************************************/
|