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

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

Introduction de SpherePosition and SphereCoordSys, and Initiator for module Samba - Reza+I. Grivell 26/10/99

File size: 16.2 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>
[518]222bool LocalMap<T>::ContainsSph(double theta, double phi) const
223//--
224{
225 return(true); // $CHECK$ A MODIFIER - Reza 26/10/99
226}
227
228//++
229template<class T>
[473]230int LocalMap<T>::PixIndexSph(double theta,double phi) const
[470]231//
[228]232// Retourne l'indice du pixel vers lequel pointe une direction définie par
233// ses coordonnées sphériques
234//--
235{
236 int i,j;
[470]237 if(!(originFlag_) || !(extensFlag_))
238 {
239 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
240 exit(0);
241 }
242
243 // theta et phi en coordonnees relatives (on se ramene a une situation par rapport au plan de reference)
[473]244 double theta_aux= theta;
245 double phi_aux = phi;
[228]246 UserToReference(theta_aux, phi_aux);
[470]247
[228]248 // coordonnees dans le plan local en unites de pixels
[473]249 double x,y;
250 AngleProjToPix(theta_aux,phi_aux, x, y);
[228]251
[473]252 double xmin= -x0_-0.5;
253 double xmax= xmin+nSzX_;
254 if((x > xmax) || (x < xmin)) return(-1);
255 double xcurrent= xmin;
[470]256 for(i = 0; i < nSzX_; i++ )
257 {
258 xcurrent += 1.;
259 if( x < xcurrent ) break;
260 }
[473]261 double ymin= -y0_-0.5;
262 double ymax= ymin+nSzY_;
[470]263 if((y > ymax) || (y < ymin)) return(-1);
[473]264 double ycurrent= ymin;
[470]265 for(j = 0; j < nSzY_; j++ )
266 {
267 ycurrent += 1.;
268 if( y < ycurrent ) break;
269 }
270 return (j*nSzX_+i);
[228]271}
272
273//++
[470]274template<class T>
[473]275void LocalMap<T>::PixThetaPhi(int k,double& theta,double& phi) const
[470]276//
[228]277// Retourne les coordonnées (theta,phi) du milieu du pixel d'indice k
278//--
279{
[470]280 if(!(originFlag_) || !(extensFlag_))
281 {
282 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
283 exit(0);
284 }
285
[228]286 int i,j;
287 Getij(k,i,j);
[470]288
[473]289 double X= double(i-x0_);
290 double Y= double(j-y0_);
[228]291 // situation de ce pixel dans le plan de reference
[473]292 double x= X*cos_angle_-Y*sin_angle_;
293 double y= X*sin_angle_+Y* cos_angle_;
[228]294 // projection sur la sphere
[470]295 PixProjToAngle(x, y, theta, phi);
[228]296 // retour au plan utilisateur
297 ReferenceToUser(theta, phi);
298}
299
300//++
[470]301template<class T>
[473]302double LocalMap<T>::PixSolAngle(int k) const
[470]303//
304// Pixel Solid angle (steradians)
305// All the pixels have not necessarly the same size in (theta, phi)
306// because of the projection scheme which is not yet fixed.
[228]307//--
308{
309 int i,j;
310 Getij(k,i,j);
[473]311 double X= double(i-x0_);
312 double Y= double(j-y0_);
313 double XR= X+double(i)*0.5;
314 double XL= X-double(i)*0.5;
315 double YU= Y+double(j)*0.5;
316 double YL= Y-double(j)*0.5;
317
[228]318 // situation dans le plan de reference
[473]319 double x0= XL*cos_angle_-YL*sin_angle_;
320 double y0= XL*sin_angle_+YL*cos_angle_;
321 double xa= XR*cos_angle_-YL*sin_angle_;
322 double ya= XR*sin_angle_+YL*cos_angle_;
323 double xb= XL*cos_angle_-YU*sin_angle_;
324 double yb= XL*sin_angle_+YU*cos_angle_;
325
[228]326 // projection sur la sphere
[473]327 double thet0,phi0,theta,phia,thetb,phib;
328 PixProjToAngle(x0, y0, thet0, phi0);
329 PixProjToAngle(xa, ya, theta, phia);
330 PixProjToAngle(xb, yb, thetb, phib);
331
[228]332 // angle solide
[473]333 double sol= fabs((xa-x0)*(yb-y0)-(xb-x0)*(ya-y0));
334 return sol;
[228]335}
336
337//++
[470]338template<class T>
[473]339void LocalMap<T>::SetOrigin(double theta0,double phi0,double angle)
[470]340//
341// Fixe la repere de reference ( angles en degres)
[228]342//--
343{
[473]344 theta0_= theta0;
345 phi0_ = phi0;
346 angle_ = angle;
[470]347 x0_= nSzX_/2;
348 y0_= nSzY_/2;
[473]349 cos_angle_= cos(angle*Pi/180.);
350 sin_angle_= sin(angle*Pi/180.);
[470]351 originFlag_= true;
352 cout << " LocalMap:: set origin 1 done" << endl;
[228]353}
354
355//++
[470]356template<class T>
[473]357void LocalMap<T>::SetOrigin(double theta0,double phi0,int x0,int y0,double angle)
[470]358//
359// Fixe le repere de reference (angles en degres)
[228]360//--
361{
[473]362 theta0_= theta0;
363 phi0_ = phi0;
364 angle_ = angle;
[470]365 x0_= x0;
366 y0_= y0;
[473]367 cos_angle_= cos(angle*Pi/180.);
368 sin_angle_= sin(angle*Pi/180.);
[470]369 originFlag_= true;
370 cout << " LocalMap:: set origin 2 done" << endl;
[228]371}
372
373//++
[470]374template<class T>
[473]375void LocalMap<T>::SetSize(double angleX,double angleY)
[470]376//
377// Fixe l'extension de la carte (angles en degres)
378//--
379{
[473]380 angleX_= angleX;
381 angleY_= angleY;
[228]382
[470]383 // tangente de la moitie de l'ouverture angulaire totale
[473]384 tgAngleX_= tan(0.5*angleX_*Pi/180.);
385 tgAngleY_= tan(0.5*angleY_*Pi/180.);
[470]386
387 extensFlag_= true;
388 cout << " LocalMap:: set extension done" << endl;
389}
390
391//++
392template<class T>
393void LocalMap<T>::Project(SphericalMap<T>& sphere) const
394//
395// Projection to spherical map
[228]396//--
397{
[470]398 for(int m = 0; m < nPix_; m++)
399 {
[473]400 double theta,phi;
[470]401 PixThetaPhi(m,theta,phi);
402 sphere(theta,phi)= pixels_(m);
403 // cout << "theta " << theta << " phi " << phi << " valeur " << sphere(theta,phi)<< endl;
404 }
[228]405}
[470]406
[228]407//++
[470]408template<class T>
[473]409void LocalMap<T>::Getij(int k,int& i,int& j) const
[470]410//
[228]411//--
412{
[470]413 i= (k+1)%nSzX_-1;
414 if(i == -1) i= nSzX_-1;
415 j= (k-i+2)/nSzX_;
[228]416}
417
[470]418//++
419template<class T>
[473]420void LocalMap<T>::ReferenceToUser(double& theta,double& phi) const
[228]421//
[470]422// --
423{
[473]424 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]425 {
426 throw "LocalMap::ReferenceToUser (theta,phi) out of range";
427 }
428
[473]429 theta= theta0_*Pi/180.+theta-Pi*0.5;
[470]430 if(theta < 0.)
431 {
432 theta= -theta;
[473]433 phi += Pi;
[470]434 }
435 else
436 {
[473]437 if(theta > Pi)
[470]438 {
[473]439 theta= 2.*Pi-theta;
440 phi += Pi;
[470]441 }
442 }
443
[473]444 phi= phi0_*Pi/180.+phi;
445 while(phi >= 2.*Pi) phi-= 2.*Pi;
[470]446
[473]447 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]448 {
449 cout << " LocalMap::ReferenceToUser : erreur bizarre dans le transfert a la carte utilisateur " << endl;
450 cout << " theta= " << theta << " phi= " << phi << endl;
451 exit(0);
452 }
[228]453}
454
[470]455//++
456template<class T>
[473]457void LocalMap<T>::UserToReference(double& theta,double& phi) const
[228]458//
[470]459// --
460{
[473]461 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]462 {
[473]463 cout<<" LocalMap::UserToReference: exceptions a mettre en place" <<endl;
464 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
[470]465 throw "LocalMap::UserToReference (theta,phi) out of range";
466 }
467
[473]468 double phi1= phi-phi0_*Pi/180.;
469 if(phi1 < 0.) phi1+= 2.*Pi;
[470]470
[473]471 double theta1= theta-theta0_*Pi/180.+Pi*0.5;
[470]472 if(theta1 < 0.)
473 {
474 theta= -theta1;
[473]475 phi1+= Pi;
[470]476 }
477 else
478 {
[473]479 if(theta1 > Pi)
[470]480 {
[473]481 theta= 2.*Pi-theta1;
482 phi1+= Pi;
[470]483 }
484 }
485
[473]486 while(phi1 >= 2.*Pi) phi1-= 2.*Pi;
[470]487 phi= phi1;
[473]488 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]489 {
490 cout << " LocalMap::UserToReference : erreur bizarre dans le transfert a la carte de reference " << endl;
491 cout << " theta= " << theta << " phi= " << phi << endl;
492 exit(0);
493 }
[228]494}
495
[470]496//++
497template<class T>
[473]498void LocalMap<T>::PixProjToAngle(double x,double y,double& theta,double& phi) const
[470]499//
[473]500// (x,y) representent les coordonnees en unites de pixels d'un point DANS LE PLAN DE REFERENCE.
[470]501// On recupere (theta,phi) par rapport au repere "absolu" theta=pi/2 et phi=0.
502//--
[228]503{
[473]504 theta= Pi*0.5-atan(2.*y*tgAngleY_/(double)nSzY_);
505 phi= atan2(2.*x*tgAngleX_,(double)nSzX_);
[470]506 if(phi < 0.) phi += DeuxPi;
[228]507}
508
[470]509//++
510template<class T>
[473]511void LocalMap<T>::AngleProjToPix(double theta,double phi,double& x,double& y) const
[470]512//
513// (theta,phi) par rapport au repere "absolu" theta=pi/2,phi=0. On recupere
514// (i,j) DANS LE PLAN DE REFERENCE.
515//--
516{
[473]517 if(phi >= Pi) phi-= DeuxPi;
[470]518 // y=0.5*mSzY_*cot(theta)/tgAngleY_; $CHECK-REZA-04/99$
519 y= 0.5*nSzY_/tan(theta)/tgAngleY_; // ? cot = 1/tan ?
520 x= 0.5*nSzX_*tan(phi)/tgAngleX_;
521}
[228]522
[470]523template<class T>
524void LocalMap<T>::print(ostream& os) const
[228]525{
[470]526 os<<" SzX= "<<nSzX_<<", SzY= "<<nSzY_<<", NPix= "<<nPix_<<endl;
527 if(LocalMap_isDone())
528 {
529 os<<" theta0= "<<theta0_<<", phi0= "<<phi0_<<", angle= "<<angle_<<endl;
530 os<<" x0= "<<x0_<<", y0= "<<y0_<<endl;
531 os<<" cos= "<<cos_angle_<<", & sin= "<<sin_angle_<<endl;
532 os<<" angleX= "<<angleX_<<", angleY= "<<angleY_<<endl;
533 os<<" tg(angleX)= "<<tgAngleX_<<", tg(angleY)= "<<tgAngleY_<<endl;
534 }
[228]535
[470]536 os << " contenu de pixels : ";
537 for(int i=0; i < nPix_; i++)
538 {
539 if(i%5 == 0) os << endl;
540 os << pixels_(i) <<", ";
541 }
542 os << endl;
[228]543}
544
[470]545//*******************************************************************
546// class FIO_LocalMap<T>
547// Les objets delegues pour la gestion de persistance
548//*******************************************************************
[228]549
[470]550template <class T>
551FIO_LocalMap<T>::FIO_LocalMap()
[228]552{
[470]553 dobj= new LocalMap<T>;
554 ownobj= true;
555}
[228]556
[470]557template <class T>
558FIO_LocalMap<T>::FIO_LocalMap(string const& filename)
559{
560 dobj= new LocalMap<T>;
561 ownobj= true;
562 Read(filename);
563}
[228]564
[470]565template <class T>
566FIO_LocalMap<T>::FIO_LocalMap(const LocalMap<T>& obj)
567{
568 dobj= new LocalMap<T>(obj);
569 ownobj= true;
[228]570}
571
[470]572template <class T>
573FIO_LocalMap<T>::FIO_LocalMap(LocalMap<T>* obj)
574{
575 dobj= obj;
576 ownobj= false;
577}
[228]578
[470]579template <class T>
580FIO_LocalMap<T>::~FIO_LocalMap()
581{
582 if (ownobj && dobj) delete dobj;
583}
[228]584
[470]585template <class T>
586AnyDataObj* FIO_LocalMap<T>::DataObj()
587{
588 return(dobj);
[228]589}
590
[470]591template <class T>
592void FIO_LocalMap<T>::ReadSelf(PInPersist& is)
593{
594 cout << " FIO_LocalMap:: ReadSelf " << endl;
[228]595
[470]596 if(dobj == NULL)
597 {
598 dobj= new LocalMap<T>;
599 }
600
[518]601// Let's Read the SphereCoordSys object -- ATTENTIOn - $CHECK$
602 SphereCoordSys* cs = dynamic_cast<SphereCoordSys*>(is.ReadObject());
603 dobj->SetCoordSys(cs);
604
[470]605 // Pour savoir s'il y avait un DVList Info associe
606 char strg[256];
607 is.GetLine(strg, 255);
608 bool hadinfo= false;
609 if(strncmp(strg+strlen(strg)-7, "HasInfo", 7) == 0) hadinfo= true;
610 if(hadinfo)
611 { // Lecture eventuelle du DVList Info
612 is >> dobj->Info();
613 }
614
[473]615 int nSzX;
[470]616 is.GetI4(nSzX);
617 dobj->setSize_x(nSzX);
618
[473]619 int nSzY;
[470]620 is.GetI4(nSzY);
621 dobj->setSize_y(nSzY);
622
[473]623 int nPix;
[470]624 is.GetI4(nPix);
625 dobj->setNbPixels(nPix);
626
627 string ss("local mapping is done");
628 string sso;
629 is.GetStr(sso);
630 if(sso == ss)
631 {
632 cout<<" ReadSelf:: local mapping"<<endl;
[473]633 int x0, y0;
634 double theta, phi, angle;
[470]635 is.GetI4(x0);
636 is.GetI4(y0);
[473]637 is.GetR8(theta);
638 is.GetR8(phi);
639 is.GetR8(angle);
[470]640 dobj->SetOrigin(theta, phi, x0, y0, angle);
641
[473]642 double angleX, angleY;
643 is.GetR8(angleX);
644 is.GetR8(angleY);
[470]645 dobj->SetSize(angleX, angleY);
646 }
647
648 T* pixels= new T[nPix];
649 PIOSReadArray(is, pixels, nPix);
650 dobj->setDataBlock(pixels, nPix);
651 delete [] pixels;
[228]652}
653
[470]654template <class T>
655void FIO_LocalMap<T>::WriteSelf(POutPersist& os) const
656{
657 cout << " FIO_LocalMap:: WriteSelf " << endl;
[228]658
[470]659 if(dobj == NULL)
660 {
661 cout << " WriteSelf:: dobj= null " << endl;
662 return;
663 }
[228]664
[518]665// Let's write the SphereCoordSys object
666 dobj->GetCoordSys()->Write(os);
667
[470]668 char strg[256];
[473]669 int nSzX= dobj->Size_x();
670 int nSzY= dobj->Size_y();
671 int nPix= dobj->NbPixels();
[470]672
673 if(dobj->ptrInfo())
674 {
675 sprintf(strg,"LocalMap: NPixX=%6d NPixY=%9d HasInfo",nSzX,nSzY);
676 os.PutLine(strg);
677 os << dobj->Info();
678 }
679 else
680 {
681 sprintf(strg,"LocalMap: NPixX=%6d NPixY=%9d ",nSzX,nSzY);
682 os.PutLine(strg);
683 }
[228]684
[470]685 os.PutI4(nSzX);
686 os.PutI4(nSzY);
687 os.PutI4(nPix);
[228]688
[470]689 if(dobj->LocalMap_isDone())
690 {
691 string ss("local mapping is done");
692 os.PutStr(ss);
[473]693 int x0, y0;
694 double theta, phi, angle;
[470]695 dobj->Origin(theta, phi, x0, y0, angle);
696 os.PutI4(x0);
697 os.PutI4(y0);
[473]698 os.PutR8(theta);
699 os.PutR8(phi);
700 os.PutR8(angle);
[470]701
[473]702 double angleX, angleY;
[470]703 dobj->Aperture(angleX, angleY);
[473]704 os.PutR8(angleX);
705 os.PutR8(angleY);
[470]706 }
707 else
708 {
709 string ss("no local mapping");
710 os.PutStr(ss);
711 }
712
713 PIOSWriteArray(os,(dobj->getDataBlock())->Data(), nPix);
714}
715
Note: See TracBrowser for help on using the repository browser.