source: Sophya/trunk/SophyaLib/Samba/utilgeom.cc@ 746

Last change on this file since 746 was 738, checked in by ansari, 26 years ago

D.Y. Petite modif dans scangle pour eviter les plantages aux petits angles

File size: 2.2 KB
RevLine 
[262]1#include <math.h>
[272]2#include <iostream.h>
3#include "utilgeom.h"
[565]4//++
5// Titre Some utilitary functions for geometry (utilgeom.h)...
6//--
7//++
[272]8int sign(double d)
[565]9//
10//--
[272]11{
12 return (d >= 0) - (d < 0);
13}
[565]14//++
[272]15double absd(double d)
[565]16//
17//--
[272]18{
19 return sqrt(d*d);
20}
[565]21//++
[272]22double mod(double d, double periode)
[565]23//
24//--
[272]25{
26 if( d >= 0 ) return d-floor(d/periode)*periode;
27 else return d-ceil(d/periode)*periode+periode;
28}
[565]29//++
[272]30void swap(double& d1, double& d2)
[565]31//
32//--
[272]33{
34 double temp;
35 temp=d2;
36 d2=d1;
37 d1=temp;
38}
[565]39//++
[272]40double min(double d1, double d2)
[565]41//
42//--
[272]43{
44 if( d1 >= d2 ) return d2;
45 else return d1;
46}
[565]47//++
[272]48double max(double d1, double d2)
[565]49//
50//--
[272]51{
52 return -min(-d1,-d2);
53}
[565]54//++
[272]55int arrondi(double d)
[565]56//
57//--
[272]58{
59 return (int)(((d-floor(d)) >= 0.5)*ceil(d)+((d-floor(d)) < 0.5)*floor(d));
60}
[565]61//++
[272]62long rangijd(int nc, int i, int j, int d)
[565]63//
64//--
[272]65{
66 if( i < j ) return 2*i*(2*nc-i-1)+4*(j-i-1)+d;
67 if( i > j ) return 2*j*(2*nc-j-1)+4*(i-j-1)+d+2;
68 if( i == j )
69 {
70 cout << "pas d'intersection entre le cercle " << i
71 << " et le cercle " << j << "." << endl;
72 return -1;
73 }
74 else return -1;
75}
[565]76//++
[272]77long rangdiff(int nc, int i, int j, int d)
[565]78//
79//--
[272]80{
81 if( i == j )
82 {
83 cout << "diff n'est pas defini entre le cercle " << i
84 << " et le cercle " << j << "." << endl;
85 return 0;
86 }
87 int indm=(int)min(i,j);
88 int indM=(int)max(i,j);
89 return indm*(2*nc-indm-1)+2*(indM-indm-1)+d;
90}
[565]91//++
[272]92long rangik(int NtotEch, int i, int k)
[565]93//
94//--
[272]95{
96 return NtotEch*i+k;
97}
[565]98//++
[272]99long ranghk(int NtotEch, int h, int k)
[565]100//
101//--
[272]102{
103 return NtotEch*h+k;
104}
[565]105//++
[272]106double scangle(double sinus, double cosinus)
[565]107//
108//--
[738]109{ // Modifie par D. Y. pour eviter des plantages aux petis angles
110 double eps=1.e-10;
111 if( fabs(1.-sinus*sinus-cosinus*cosinus) > eps)
112 { // On renormalise D.Y.
113 // cerr << "angle non valide: "<< "sinus = " << sinus << " cosinus = " << cosinus << '\n'; // BUGGG
114 double norm= cosinus*cosinus+sinus*sinus;
115 cosinus=cosinus/norm;
116 sinus=sinus/norm;
[272]117 }
[738]118 if( fabs(cosinus) >1. ) cosinus=1.*copysign(1.,cosinus);
119 if( fabs(sinus) >1. ) sinus=1.*copysign(1.,sinus);
120/* if( fabs(sinus) == 0. ) sinus=0.;
121 if( fabs(cosinus) == 0. ) cosinus=0.;
122*/
[272]123 return acos(cosinus)*copysign(1,sinus);
124}
125
Note: See TracBrowser for help on using the repository browser.