source: Sophya/trunk/SophyaLib/Samba/localmap.cc@ 473

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

modifs francois : passage en double etc. 18-OCT-99

File size: 15.8 KB
RevLine 
[228]1#include "localmap.h"
[470]2#include "nbmath.h"
3#include <complex>
4#include "piocmplx.h"
5
6#include <string.h>
[228]7#include <iostream.h>
[470]8
9#ifdef __CXX_PRAGMA_TEMPLATES__
10#pragma define_template LocalMap<double>
11#pragma define_template LocalMap<float>
12#pragma define_template LocalMap< complex<double> >
13#pragma define_template LocalMap< complex<float> >
14#pragma define_template FIO_LocalMap<double>
15#pragma define_template FIO_LocalMap<float>
16#pragma define_template FIO_LocalMap< complex<float> >
17#pragma define_template FIO_LocalMap< complex<double> >
18#endif
19#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
20template class LocalMap<double>;
21template class LocalMap<float>;
22template class LocalMap< complex<double> >;
23template class LocalMap< complex<float> >;
24template class FIO_LocalMap<double>;
25template class FIO_LocalMap<float>;
26template class FIO_LocalMap< complex<float> >;
27template class FIO_LocalMap< complex<double> >;
28#endif
29
[228]30//*****************************************************************************
31//++
32// Class LocalMap
33//
34// include localmap.h nbmath.h
35//
36// A local map of a region of the sky, in cartesian coordinates.
37// It has an origin in (theta0, phi0), mapped to pixel(x0, y0)
38// (x0, y0 might be outside of this local map)
39// default value of (x0, y0) is middle of the map, center of
40// pixel(nx/2, ny/2)
41//
42// la carte est consideree comme un tableau a deux indices i et j, i etant
43// indice de colonne et j indice de ligne. La carte est supposee resider
44// dans un plan tangent, dont le point de tangence est repere (x0,y0) dans
45// la carte et (theta0, phi0) sur la sphere celeste. L'extension de la
46// carte est definie par les valeurs de deux angles couverts respectivement
47// par la totalite des pixels en x de la carte et la totalite des pixels
48// en y. (SetSize()).
49// On considere un "plan de reference" : plan tangent a la sphere celeste
50// aux angles theta=Pi/2 et phi=0. Dans ce plan L'origine des coordonnees
51// est le point de tangence. L'axe Ox est la tangente parallele a
52// l'equateur, dirige vers les phi croissants, l'axe Oy est parallele
53// au meridien, dirige vers le pole nord.
54// De maniere interne a la classe une carte est definie dans ce plan de
55// reference et transportee jusqu'au point (theta0, phi0) de sorte que
56// les axes restent paralleles aux meridiens et paralleles. L'utilisateur
57// peut definir sa carte selon un repere en rotation par rapport au repere
58// de reference (par l'angle entre le parallele et l'axe Ox souhaite --
59// methode SetOrigin(...))
60
61//
62//--
63//++
64//
65// Links Parents
66//
67// PixelMap
68//
69//--
70//++
71//
72// Links Descendants
73//
74//
75//--
76//++
77// Titre Constructeurs
78//--
79//++
[470]80template<class T>
81LocalMap<T>::LocalMap()
82//
83// Constructeur par defaut
[228]84//--
[470]85{
86 InitNul();
87}
[228]88
89//++
[470]90template<class T>
[473]91LocalMap<T>::LocalMap(int nx, int ny) : nSzX_(nx), nSzY_(ny)
[470]92//
93// Constructeur
[228]94//--
95{
96 InitNul();
[470]97 nPix_= nx*ny;
98 pixels_.ReSize(nPix_);
99 pixels_.Reset();
[228]100}
101
102//++
[470]103template<class T>
104LocalMap<T>::LocalMap(const LocalMap<T>& lm)
105//
106// Constructeur de recopie
[228]107//--
[470]108{
109 cout<<" LocalMap:: Appel du constructeur de recopie " << endl;
110
111 if(lm.mInfo_) mInfo_= new DVList(*lm.mInfo_);
112 nSzX_= lm.nSzX_;
113 nSzY_= lm.nSzY_;
114 nPix_= lm.nPix_;
115 originFlag_= lm.originFlag_;
116 extensFlag_= lm.extensFlag_;
117 x0_= lm.x0_;
118 y0_= lm.y0_;
119 theta0_= lm.theta0_;
120 phi0_= lm.phi0_;
121 angle_= lm.angle_;
122 cos_angle_= lm.cos_angle_;
123 sin_angle_= lm.sin_angle_;
124 angleX_= lm.angleX_;
125 angleY_= lm.angleY_;
126 tgAngleX_= lm.tgAngleX_;
127 tgAngleY_= lm.tgAngleY_;
128 pixels_= lm.pixels_;
129}
130
[228]131//++
[470]132template<class T>
133LocalMap<T>::~LocalMap()
134//
135// Destructeur
[228]136//--
137{
[470]138 InitNul();
[228]139}
140
[470]141//++
142template<class T>
143void LocalMap<T>::InitNul()
144//
145// Initialise à zéro certaines variables internes
146//--
147{
148 originFlag_= false;
149 extensFlag_= false;
150 cos_angle_= 1.0;
151 sin_angle_= 0.0;
152 pixels_.Reset();
153}
[228]154
[470]155//++
156template<class T>
[473]157void LocalMap<T>::ReSize(int nx, int ny)
[470]158//
159// Redimensionne l'espace de stokage des pixels
160//--
[228]161{
[470]162 InitNul();
163 nSzX_ = nx;
164 nSzY_ = ny;
165 nPix_= nx*ny;
166 pixels_.ReSize(nPix_);
167 pixels_.Reset();
[228]168}
169
170//++
171// Titre Methodes
172//--
173
174
175//++
[470]176template<class T>
[473]177int LocalMap<T>::NbPixels() const
[470]178//
[228]179// Retourne le nombre de pixels du découpage
180//--
181{
[470]182 return(nPix_);
[228]183}
184
185//++
[470]186template<class T>
[473]187T& LocalMap<T>::PixVal(int k)
[470]188//
[228]189// Retourne la valeur du contenu du pixel d'indice k
190//--
191{
[470]192 if((k < 0) || (k >= nPix_))
193 {
194 cout << " LocalMap::PIxVal : exceptions a mettre en place" <<endl;
195 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
196 THROW(rangeCheckErr);
197 //throw "LocalMap::PIxVal Pixel index out of range";
198 }
199 return(pixels_(k));
[228]200}
201
202//++
203
[470]204template<class T>
[473]205T const& LocalMap<T>::PixVal(int k) const
[470]206//
[228]207// Retourne la valeur du contenu du pixel d'indice k
208//--
209{
[470]210 if((k < 0) || (k >= nPix_))
211 {
212 cout << " LocalMap::PIxVal : exceptions a mettre en place" <<endl;
213 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
214
215 throw "LocalMap::PIxVal Pixel index out of range";
216 }
217 return *(pixels_.Data()+k);
[228]218}
219
220//++
[470]221template<class T>
[473]222int LocalMap<T>::PixIndexSph(double theta,double phi) const
[470]223//
[228]224// Retourne l'indice du pixel vers lequel pointe une direction définie par
225// ses coordonnées sphériques
226//--
227{
228 int i,j;
[470]229 if(!(originFlag_) || !(extensFlag_))
230 {
231 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
232 exit(0);
233 }
234
235 // theta et phi en coordonnees relatives (on se ramene a une situation par rapport au plan de reference)
[473]236 double theta_aux= theta;
237 double phi_aux = phi;
[228]238 UserToReference(theta_aux, phi_aux);
[470]239
[228]240 // coordonnees dans le plan local en unites de pixels
[473]241 double x,y;
242 AngleProjToPix(theta_aux,phi_aux, x, y);
[228]243
[473]244 double xmin= -x0_-0.5;
245 double xmax= xmin+nSzX_;
246 if((x > xmax) || (x < xmin)) return(-1);
247 double xcurrent= xmin;
[470]248 for(i = 0; i < nSzX_; i++ )
249 {
250 xcurrent += 1.;
251 if( x < xcurrent ) break;
252 }
[473]253 double ymin= -y0_-0.5;
254 double ymax= ymin+nSzY_;
[470]255 if((y > ymax) || (y < ymin)) return(-1);
[473]256 double ycurrent= ymin;
[470]257 for(j = 0; j < nSzY_; j++ )
258 {
259 ycurrent += 1.;
260 if( y < ycurrent ) break;
261 }
262 return (j*nSzX_+i);
[228]263}
264
265//++
[470]266template<class T>
[473]267void LocalMap<T>::PixThetaPhi(int k,double& theta,double& phi) const
[470]268//
[228]269// Retourne les coordonnées (theta,phi) du milieu du pixel d'indice k
270//--
271{
[470]272 if(!(originFlag_) || !(extensFlag_))
273 {
274 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
275 exit(0);
276 }
277
[228]278 int i,j;
279 Getij(k,i,j);
[470]280
[473]281 double X= double(i-x0_);
282 double Y= double(j-y0_);
[228]283 // situation de ce pixel dans le plan de reference
[473]284 double x= X*cos_angle_-Y*sin_angle_;
285 double y= X*sin_angle_+Y* cos_angle_;
[228]286 // projection sur la sphere
[470]287 PixProjToAngle(x, y, theta, phi);
[228]288 // retour au plan utilisateur
289 ReferenceToUser(theta, phi);
290}
291
292//++
[470]293template<class T>
[473]294double LocalMap<T>::PixSolAngle(int k) const
[470]295//
296// Pixel Solid angle (steradians)
297// All the pixels have not necessarly the same size in (theta, phi)
298// because of the projection scheme which is not yet fixed.
[228]299//--
300{
301 int i,j;
302 Getij(k,i,j);
[473]303 double X= double(i-x0_);
304 double Y= double(j-y0_);
305 double XR= X+double(i)*0.5;
306 double XL= X-double(i)*0.5;
307 double YU= Y+double(j)*0.5;
308 double YL= Y-double(j)*0.5;
309
[228]310 // situation dans le plan de reference
[473]311 double x0= XL*cos_angle_-YL*sin_angle_;
312 double y0= XL*sin_angle_+YL*cos_angle_;
313 double xa= XR*cos_angle_-YL*sin_angle_;
314 double ya= XR*sin_angle_+YL*cos_angle_;
315 double xb= XL*cos_angle_-YU*sin_angle_;
316 double yb= XL*sin_angle_+YU*cos_angle_;
317
[228]318 // projection sur la sphere
[473]319 double thet0,phi0,theta,phia,thetb,phib;
320 PixProjToAngle(x0, y0, thet0, phi0);
321 PixProjToAngle(xa, ya, theta, phia);
322 PixProjToAngle(xb, yb, thetb, phib);
323
[228]324 // angle solide
[473]325 double sol= fabs((xa-x0)*(yb-y0)-(xb-x0)*(ya-y0));
326 return sol;
[228]327}
328
329//++
[470]330template<class T>
[473]331void LocalMap<T>::SetOrigin(double theta0,double phi0,double angle)
[470]332//
333// Fixe la repere de reference ( angles en degres)
[228]334//--
335{
[473]336 theta0_= theta0;
337 phi0_ = phi0;
338 angle_ = angle;
[470]339 x0_= nSzX_/2;
340 y0_= nSzY_/2;
[473]341 cos_angle_= cos(angle*Pi/180.);
342 sin_angle_= sin(angle*Pi/180.);
[470]343 originFlag_= true;
344 cout << " LocalMap:: set origin 1 done" << endl;
[228]345}
346
347//++
[470]348template<class T>
[473]349void LocalMap<T>::SetOrigin(double theta0,double phi0,int x0,int y0,double angle)
[470]350//
351// Fixe le repere de reference (angles en degres)
[228]352//--
353{
[473]354 theta0_= theta0;
355 phi0_ = phi0;
356 angle_ = angle;
[470]357 x0_= x0;
358 y0_= y0;
[473]359 cos_angle_= cos(angle*Pi/180.);
360 sin_angle_= sin(angle*Pi/180.);
[470]361 originFlag_= true;
362 cout << " LocalMap:: set origin 2 done" << endl;
[228]363}
364
365//++
[470]366template<class T>
[473]367void LocalMap<T>::SetSize(double angleX,double angleY)
[470]368//
369// Fixe l'extension de la carte (angles en degres)
370//--
371{
[473]372 angleX_= angleX;
373 angleY_= angleY;
[228]374
[470]375 // tangente de la moitie de l'ouverture angulaire totale
[473]376 tgAngleX_= tan(0.5*angleX_*Pi/180.);
377 tgAngleY_= tan(0.5*angleY_*Pi/180.);
[470]378
379 extensFlag_= true;
380 cout << " LocalMap:: set extension done" << endl;
381}
382
383//++
384template<class T>
385void LocalMap<T>::Project(SphericalMap<T>& sphere) const
386//
387// Projection to spherical map
[228]388//--
389{
[470]390 for(int m = 0; m < nPix_; m++)
391 {
[473]392 double theta,phi;
[470]393 PixThetaPhi(m,theta,phi);
394 sphere(theta,phi)= pixels_(m);
395 // cout << "theta " << theta << " phi " << phi << " valeur " << sphere(theta,phi)<< endl;
396 }
[228]397}
[470]398
[228]399//++
[470]400template<class T>
[473]401void LocalMap<T>::Getij(int k,int& i,int& j) const
[470]402//
[228]403//--
404{
[470]405 i= (k+1)%nSzX_-1;
406 if(i == -1) i= nSzX_-1;
407 j= (k-i+2)/nSzX_;
[228]408}
409
[470]410//++
411template<class T>
[473]412void LocalMap<T>::ReferenceToUser(double& theta,double& phi) const
[228]413//
[470]414// --
415{
[473]416 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]417 {
418 throw "LocalMap::ReferenceToUser (theta,phi) out of range";
419 }
420
[473]421 theta= theta0_*Pi/180.+theta-Pi*0.5;
[470]422 if(theta < 0.)
423 {
424 theta= -theta;
[473]425 phi += Pi;
[470]426 }
427 else
428 {
[473]429 if(theta > Pi)
[470]430 {
[473]431 theta= 2.*Pi-theta;
432 phi += Pi;
[470]433 }
434 }
435
[473]436 phi= phi0_*Pi/180.+phi;
437 while(phi >= 2.*Pi) phi-= 2.*Pi;
[470]438
[473]439 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]440 {
441 cout << " LocalMap::ReferenceToUser : erreur bizarre dans le transfert a la carte utilisateur " << endl;
442 cout << " theta= " << theta << " phi= " << phi << endl;
443 exit(0);
444 }
[228]445}
446
[470]447//++
448template<class T>
[473]449void LocalMap<T>::UserToReference(double& theta,double& phi) const
[228]450//
[470]451// --
452{
[473]453 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]454 {
[473]455 cout<<" LocalMap::UserToReference: exceptions a mettre en place" <<endl;
456 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
[470]457 throw "LocalMap::UserToReference (theta,phi) out of range";
458 }
459
[473]460 double phi1= phi-phi0_*Pi/180.;
461 if(phi1 < 0.) phi1+= 2.*Pi;
[470]462
[473]463 double theta1= theta-theta0_*Pi/180.+Pi*0.5;
[470]464 if(theta1 < 0.)
465 {
466 theta= -theta1;
[473]467 phi1+= Pi;
[470]468 }
469 else
470 {
[473]471 if(theta1 > Pi)
[470]472 {
[473]473 theta= 2.*Pi-theta1;
474 phi1+= Pi;
[470]475 }
476 }
477
[473]478 while(phi1 >= 2.*Pi) phi1-= 2.*Pi;
[470]479 phi= phi1;
[473]480 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]481 {
482 cout << " LocalMap::UserToReference : erreur bizarre dans le transfert a la carte de reference " << endl;
483 cout << " theta= " << theta << " phi= " << phi << endl;
484 exit(0);
485 }
[228]486}
487
[470]488//++
489template<class T>
[473]490void LocalMap<T>::PixProjToAngle(double x,double y,double& theta,double& phi) const
[470]491//
[473]492// (x,y) representent les coordonnees en unites de pixels d'un point DANS LE PLAN DE REFERENCE.
[470]493// On recupere (theta,phi) par rapport au repere "absolu" theta=pi/2 et phi=0.
494//--
[228]495{
[473]496 theta= Pi*0.5-atan(2.*y*tgAngleY_/(double)nSzY_);
497 phi= atan2(2.*x*tgAngleX_,(double)nSzX_);
[470]498 if(phi < 0.) phi += DeuxPi;
[228]499}
500
[470]501//++
502template<class T>
[473]503void LocalMap<T>::AngleProjToPix(double theta,double phi,double& x,double& y) const
[470]504//
505// (theta,phi) par rapport au repere "absolu" theta=pi/2,phi=0. On recupere
506// (i,j) DANS LE PLAN DE REFERENCE.
507//--
508{
[473]509 if(phi >= Pi) phi-= DeuxPi;
[470]510 // y=0.5*mSzY_*cot(theta)/tgAngleY_; $CHECK-REZA-04/99$
511 y= 0.5*nSzY_/tan(theta)/tgAngleY_; // ? cot = 1/tan ?
512 x= 0.5*nSzX_*tan(phi)/tgAngleX_;
513}
[228]514
[470]515template<class T>
516void LocalMap<T>::print(ostream& os) const
[228]517{
[470]518 os<<" SzX= "<<nSzX_<<", SzY= "<<nSzY_<<", NPix= "<<nPix_<<endl;
519 if(LocalMap_isDone())
520 {
521 os<<" theta0= "<<theta0_<<", phi0= "<<phi0_<<", angle= "<<angle_<<endl;
522 os<<" x0= "<<x0_<<", y0= "<<y0_<<endl;
523 os<<" cos= "<<cos_angle_<<", & sin= "<<sin_angle_<<endl;
524 os<<" angleX= "<<angleX_<<", angleY= "<<angleY_<<endl;
525 os<<" tg(angleX)= "<<tgAngleX_<<", tg(angleY)= "<<tgAngleY_<<endl;
526 }
[228]527
[470]528 os << " contenu de pixels : ";
529 for(int i=0; i < nPix_; i++)
530 {
531 if(i%5 == 0) os << endl;
532 os << pixels_(i) <<", ";
533 }
534 os << endl;
[228]535}
536
[470]537//*******************************************************************
538// class FIO_LocalMap<T>
539// Les objets delegues pour la gestion de persistance
540//*******************************************************************
[228]541
[470]542template <class T>
543FIO_LocalMap<T>::FIO_LocalMap()
[228]544{
[470]545 dobj= new LocalMap<T>;
546 ownobj= true;
547}
[228]548
[470]549template <class T>
550FIO_LocalMap<T>::FIO_LocalMap(string const& filename)
551{
552 dobj= new LocalMap<T>;
553 ownobj= true;
554 Read(filename);
555}
[228]556
[470]557template <class T>
558FIO_LocalMap<T>::FIO_LocalMap(const LocalMap<T>& obj)
559{
560 dobj= new LocalMap<T>(obj);
561 ownobj= true;
[228]562}
563
[470]564template <class T>
565FIO_LocalMap<T>::FIO_LocalMap(LocalMap<T>* obj)
566{
567 dobj= obj;
568 ownobj= false;
569}
[228]570
[470]571template <class T>
572FIO_LocalMap<T>::~FIO_LocalMap()
573{
574 if (ownobj && dobj) delete dobj;
575}
[228]576
[470]577template <class T>
578AnyDataObj* FIO_LocalMap<T>::DataObj()
579{
580 return(dobj);
[228]581}
582
[470]583template <class T>
584void FIO_LocalMap<T>::ReadSelf(PInPersist& is)
585{
586 cout << " FIO_LocalMap:: ReadSelf " << endl;
[228]587
[470]588 if(dobj == NULL)
589 {
590 dobj= new LocalMap<T>;
591 }
592
593 // Pour savoir s'il y avait un DVList Info associe
594 char strg[256];
595 is.GetLine(strg, 255);
596 bool hadinfo= false;
597 if(strncmp(strg+strlen(strg)-7, "HasInfo", 7) == 0) hadinfo= true;
598 if(hadinfo)
599 { // Lecture eventuelle du DVList Info
600 is >> dobj->Info();
601 }
602
[473]603 int nSzX;
[470]604 is.GetI4(nSzX);
605 dobj->setSize_x(nSzX);
606
[473]607 int nSzY;
[470]608 is.GetI4(nSzY);
609 dobj->setSize_y(nSzY);
610
[473]611 int nPix;
[470]612 is.GetI4(nPix);
613 dobj->setNbPixels(nPix);
614
615 string ss("local mapping is done");
616 string sso;
617 is.GetStr(sso);
618 if(sso == ss)
619 {
620 cout<<" ReadSelf:: local mapping"<<endl;
[473]621 int x0, y0;
622 double theta, phi, angle;
[470]623 is.GetI4(x0);
624 is.GetI4(y0);
[473]625 is.GetR8(theta);
626 is.GetR8(phi);
627 is.GetR8(angle);
[470]628 dobj->SetOrigin(theta, phi, x0, y0, angle);
629
[473]630 double angleX, angleY;
631 is.GetR8(angleX);
632 is.GetR8(angleY);
[470]633 dobj->SetSize(angleX, angleY);
634 }
635
636 T* pixels= new T[nPix];
637 PIOSReadArray(is, pixels, nPix);
638 dobj->setDataBlock(pixels, nPix);
639 delete [] pixels;
[228]640}
641
[470]642template <class T>
643void FIO_LocalMap<T>::WriteSelf(POutPersist& os) const
644{
645 cout << " FIO_LocalMap:: WriteSelf " << endl;
[228]646
[470]647 if(dobj == NULL)
648 {
649 cout << " WriteSelf:: dobj= null " << endl;
650 return;
651 }
[228]652
[470]653 char strg[256];
[473]654 int nSzX= dobj->Size_x();
655 int nSzY= dobj->Size_y();
656 int nPix= dobj->NbPixels();
[470]657
658 if(dobj->ptrInfo())
659 {
660 sprintf(strg,"LocalMap: NPixX=%6d NPixY=%9d HasInfo",nSzX,nSzY);
661 os.PutLine(strg);
662 os << dobj->Info();
663 }
664 else
665 {
666 sprintf(strg,"LocalMap: NPixX=%6d NPixY=%9d ",nSzX,nSzY);
667 os.PutLine(strg);
668 }
[228]669
[470]670 os.PutI4(nSzX);
671 os.PutI4(nSzY);
672 os.PutI4(nPix);
[228]673
[470]674 if(dobj->LocalMap_isDone())
675 {
676 string ss("local mapping is done");
677 os.PutStr(ss);
[473]678 int x0, y0;
679 double theta, phi, angle;
[470]680 dobj->Origin(theta, phi, x0, y0, angle);
681 os.PutI4(x0);
682 os.PutI4(y0);
[473]683 os.PutR8(theta);
684 os.PutR8(phi);
685 os.PutR8(angle);
[470]686
[473]687 double angleX, angleY;
[470]688 dobj->Aperture(angleX, angleY);
[473]689 os.PutR8(angleX);
690 os.PutR8(angleY);
[470]691 }
692 else
693 {
694 string ss("no local mapping");
695 os.PutStr(ss);
696 }
697
698 PIOSWriteArray(os,(dobj->getDataBlock())->Data(), nPix);
699}
700
Note: See TracBrowser for help on using the repository browser.