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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
// Copyright (c) 1997-2002 Max-Planck-Institute Saarbruecken (Germany).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Nef_S2/include/CGAL/Nef_S2/Normalizing.h $
// $Id: include/CGAL/Nef_S2/Normalizing.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Peter Hachenberger <hachenberger@mpi-sb.mpg.de>
#ifndef CGAL_NORMALIZING_H
#define CGAL_NORMALIZING_H
#include <CGAL/license/Nef_S2.h>
#include <CGAL/Nef_S2/Sphere_point.h>
#include <CGAL/Nef_S2/Sphere_circle.h>
#include <CGAL/Nef_S2/Sphere_direction.h>
#include <CGAL/Fraction_traits.h>
#undef CGAL_NEF_DEBUG
#define CGAL_NEF_DEBUG 307
#include <CGAL/Nef_2/debug.h>
#ifdef CCGAL_USE_LEDA
#include <CGAL/Cartesian.h>
#include <CGAL/leda_rational.h>
#endif
namespace CGAL {
class Homogeneous_tag;
class Cartesian_tag;
template<typename Tag> class Normalizing;
template<>
class Normalizing<Homogeneous_tag> {
public:
template <typename iterator> static
void normalized(iterator begin, iterator end) {
typedef typename std::iterator_traits<iterator>::value_type RT;
iterator i = begin;
while(i!=end && *i == RT(0)) ++i;
if(i==end)
return;
RT g = *i;
for(iterator j=i+1; j!=end; ++j)
g = (*j == 0 ? g : CGAL_NTS gcd(g,*j));
g=CGAL_NTS abs(g);
for(; i!=end; ++i)
*i = CGAL::integral_division(*i,g);
}
template <typename R> static
CGAL::Point_3<R> normalized(const CGAL::Point_3<R>& p) {
typedef typename R::RT RT;
RT g = p.hw();
g = (p.hx() == 0 ? g : CGAL_NTS gcd(g,p.hx()));
g = (p.hy() == 0 ? g : CGAL_NTS gcd(g,p.hy()));
g = (p.hz() == 0 ? g : CGAL_NTS gcd(g,p.hz()));
RT x = p.hx()/g;
RT y = p.hy()/g;
RT z = p.hz()/g;
RT w = p.hw()/g;
return typename R::Point_3(x,y,z,w);
}
template <typename R> static
CGAL::Sphere_point<R> normalized(const CGAL::Sphere_point<R>& p) {
typedef typename R::RT RT;
RT g = (p.x()==0) ? ((p.y()==0) ? ((p.z()==0) ? 1: p.z()): p.y()): p.x();
if(p.y() != 0) g = CGAL_NTS gcd(g,p.y());
if(p.z() != 0) g = CGAL_NTS gcd(g,p.z());
if(g<0) g = -g;
RT x = p.x()/g;
RT y = p.y()/g;
RT z = p.z()/g;
return CGAL::Sphere_point<R>(x,y,z);
}
template <typename R> static
CGAL::Vector_3<R> normalized(const CGAL::Vector_3<R>& p) {
typedef typename R::RT RT;
RT g = (p.hx()==0) ? ((p.hy()==0) ? ((p.hz()==0) ? 1: p.hz()): p.hy()): p.hx();
if(p.hy() != 0) g = CGAL_NTS gcd(g,p.hy());
if(p.hz() != 0) g = CGAL_NTS gcd(g,p.hz());
if(g<0) g = -g;
RT x = p.hx()/g;
RT y = p.hy()/g;
RT z = p.hz()/g;
return typename R::Vector_3(x,y,z);
}
template <typename R> static
CGAL::Sphere_direction<R> normalized(const CGAL::Sphere_direction<R>& c) {
typename R::Plane_3 h = c.plane();
CGAL_assertion(!(h.a()==0 && h.b()==0 && h.c()==0 && h.d()==0));
typedef typename R::RT RT;
RT x = (h.a()==0) ? ((h.b()==0) ? ((h.c()==0) ? ((h.d()==0) ? 1
: h.d())
: h.c())
: h.b())
: h.a();
if(h.b() != 0)
x = gcd(x,h.b());
if(h.c() != 0)
x = gcd(x,h.c());
if(h.d() !=0)
x = gcd(x,h.d());
x = CGAL_NTS abs(x);
RT pa = h.a()/x;
RT pb = h.b()/x;
RT pc = h.c()/x;
RT pd = h.d()/x;
return CGAL::Sphere_direction<R>(typename R::Plane_3(pa,pb,pc,pd));
}
template <typename R> static
CGAL::Plane_3<R> normalized(const CGAL::Plane_3<R>& h) {
CGAL_assertion(!(h.a()==0 && h.b()==0 && h.c()==0 && h.d()==0));
typedef typename R::RT RT;
RT x = (h.a()==0) ? ((h.b()==0) ? ((h.c()==0) ? ((h.d()==0) ? 1
: h.d())
: h.c())
: h.b())
: h.a();
CGAL_NEF_TRACE("gcd... i"<<' ');
if(h.b() != 0)
x = CGAL_NTS gcd(x,h.b());
CGAL_NEF_TRACE(x<<' ');
if(h.c() != 0)
x = CGAL_NTS gcd(x,h.c());
CGAL_NEF_TRACE(x<<' ');
if(h.d() !=0)
x = CGAL_NTS gcd(x,h.d());
CGAL_NEF_TRACEN(x);
x = CGAL_NTS abs(x);
RT pa = h.a()/x;
RT pb = h.b()/x;
RT pc = h.c()/x;
RT pd = h.d()/x;
CGAL_NEF_TRACEN(" after normalizing " << typename R::Plane_3(pa,pb,pc,pd));
return typename R::Plane_3(pa,pb,pc,pd);
}
template <typename R> static
CGAL::Sphere_circle<R> normalized(const CGAL::Sphere_circle<R>& c) {
typename R::Plane_3 h = c.plane();
CGAL_assertion(!(h.a()==0 && h.b()==0 && h.c()==0 && h.d()==0));
typedef typename R::RT RT;
RT x = (h.a()==0) ? ((h.b()==0) ? ((h.c()==0) ? ((h.d()==0) ? 1
: h.d())
: h.c())
: h.b())
: h.a();
if(h.b() != 0)
x = CGAL_NTS gcd(x,h.b());
if(h.c() != 0)
x = CGAL_NTS gcd(x,h.c());
if(h.d() !=0)
x = CGAL_NTS gcd(x,h.d());
x = CGAL_NTS abs(x);
RT pa = h.a()/x;
RT pb = h.b()/x;
RT pc = h.c()/x;
RT pd = h.d()/x;
return CGAL::Sphere_circle<R>(typename R::Plane_3(pa,pb,pc,pd));
}
};
template<>
class Normalizing<Cartesian_tag> {
public:
template <typename R> static
CGAL::Point_3<R> normalized(const CGAL::Point_3<R>& p) {
return p;
}
template <typename R> static
CGAL::Vector_3<R> normalized(const CGAL::Vector_3<R>& p) {
return p;
}
template <typename R> static
CGAL::Sphere_point<R> normalized(const CGAL::Sphere_point<R>& p) {
typedef typename R::RT RT;
RT g = (p.hx() != 0 ? p.hx() : (p.hy() != 0 ? p.hy() : p.hz()));
g = CGAL_NTS abs(g);
RT x = p.hx()/g;
RT y = p.hy()/g;
RT z = p.hz()/g;
return CGAL::Sphere_point<R>(x,y,z);
}
template <typename R> static
CGAL::Sphere_direction<R> normalized(const CGAL::Sphere_direction<R>& c) {
return c;
}
#ifdef CCGAL_USE_LEDA
// specialization: Plane_3 < Cartesian < leda_rational > >
static Plane_3<CGAL::Cartesian<leda_rational> >
normalized(Plane_3<CGAL::Cartesian<leda_rational> >& h) {
CGAL_assertion(!(h.a()==0 && h.b()==0 && h.c()==0 && h.d()==0));
typedef leda_rational FT;
FT x = (h.a()==0) ? ((h.b()==0) ? ((h.c()==0) ? ((h.d()==0) ? 1
: h.d())
: h.c())
: h.b())
: h.a();
x = CGAL_NTS abs(x);
FT pa = h.a()/x;
FT pb = h.b()/x;
FT pc = h.c()/x;
FT pd = h.d()/x;
pa.normalize();
pb.normalize();
pc.normalize();
pd.normalize();
CGAL_NEF_TRACEN(" after normalizing " << CGAL::Plane_3<CGAL::Cartesian<leda_rational> >(pa,pb,pc,pd));
return CGAL::Plane_3<CGAL::Cartesian<leda_rational> >(pa,pb,pc,pd);
}
#endif
template <typename R> static
CGAL::Plane_3<R> normalized(const CGAL::Plane_3<R>& h,Tag_true) {
CGAL_assertion(!(h.a()==0 && h.b()==0 && h.c()==0 && h.d()==0));
typedef typename R::FT FT;
typedef Fraction_traits<FT> FracTraits;
typedef std::vector<typename FracTraits::Numerator_type> NV;
typename FracTraits::Numerator_type num;
typename FracTraits::Denominator_type denom;
typename FracTraits::Decompose decomposer;
typename FracTraits::Compose composer;
NV vec;
decomposer(h.a(),num,denom);
vec.push_back(num);
vec.push_back(denom);
vec.push_back(denom);
vec.push_back(denom);
decomposer(h.b(),num,denom);
vec[0]*=denom;
vec[1]*=num;
vec[2]*=denom;
vec[3]*=denom;
decomposer(h.c(),num,denom);
vec[0]*=denom;
vec[1]*=denom;
vec[2]*=num;
vec[3]*=denom;
decomposer(h.d(),num,denom);
vec[0]*=denom;
vec[1]*=denom;
vec[2]*=denom;
vec[3]*=num;
Normalizing<Homogeneous_tag>::
normalized(vec.begin(),vec.end());
return typename R::Plane_3(composer(vec[0],1),
composer(vec[1],1),
composer(vec[2],1),
composer(vec[3],1));
}
template <typename R> static
CGAL::Plane_3<R> normalized(const CGAL::Plane_3<R>& h,Tag_false) {
CGAL_assertion(!(h.a()==0 && h.b()==0 && h.c()==0 && h.d()==0));
typedef typename R::FT FT;
if (h.a()!=0)
return typename R::Plane_3(FT(1),h.b()/h.a(),h.c()/h.a(),h.d()/h.a());
if (h.b()!=0)
return typename R::Plane_3(h.a()/h.b(),FT(1),h.c()/h.b(),h.d()/h.b());
if (h.c()!=0)
return typename R::Plane_3(h.a()/h.c(),h.b()/h.c(),FT(1),h.d()/h.c());
return typename R::Plane_3(h.a()/h.d(),h.b()/h.d(),h.c()/h.d(),FT(1));
}
template <typename R> static
CGAL::Plane_3<R> normalized(const CGAL::Plane_3<R>& h) {
return normalized( h,typename Fraction_traits<typename R::FT>::Is_fraction() );
}
template <typename R> static
CGAL::Sphere_circle<R> normalized(CGAL::Sphere_circle<R>& c) {
return c;
}
};
/*
template <typename R, typename iterator>
void normalized(iterator begin, iterator end) {
Normalizing<typename R::Kernel_tag>::normalized(begin, end);
}
*/
template <typename R>
CGAL::Point_3<R> normalized(const CGAL::Point_3<R>& p) {
return Normalizing<typename R::Kernel_tag>::normalized(p);
}
template <typename R>
CGAL::Sphere_point<R> normalized(const CGAL::Sphere_point<R>& p) {
return Normalizing<typename R::Kernel_tag>::normalized(p);
}
template <typename R>
CGAL::Vector_3<R> normalized(const CGAL::Vector_3<R>& p) {
return Normalizing<typename R::Kernel_tag>::normalized(p);
}
template <typename R>
CGAL::Sphere_direction<R> normalized(const CGAL::Sphere_direction<R>& c) {
return Normalizing<typename R::Kernel_tag>::normalized(c);
}
template <typename R>
CGAL::Plane_3<R> normalized(const CGAL::Plane_3<R>& h) {
return Normalizing<typename R::Kernel_tag>::normalized(h);
}
template <typename R>
CGAL::Sphere_circle<R> normalized(const CGAL::Sphere_circle<R>& c) {
return Normalizing<typename R::Kernel_tag>::normalized(c);
}
} //namespace CGAL
#endif // CGAL_NORMALIZING_H
|