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
Line 
1#include "localmap.h"
2#include "nbmath.h"
3#include <complex>
4#include "piocmplx.h"
5
6#include <string.h>
7#include <iostream.h>
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
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//++
80template<class T>
81LocalMap<T>::LocalMap()
82//
83// Constructeur par defaut
84//--
85{
86 InitNul();
87}
88
89//++
90template<class T>
91LocalMap<T>::LocalMap(int nx, int ny) : nSzX_(nx), nSzY_(ny)
92//
93// Constructeur
94//--
95{
96 InitNul();
97 nPix_= nx*ny;
98 pixels_.ReSize(nPix_);
99 pixels_.Reset();
100}
101
102//++
103template<class T>
104LocalMap<T>::LocalMap(const LocalMap<T>& lm)
105//
106// Constructeur de recopie
107//--
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
131//++
132template<class T>
133LocalMap<T>::~LocalMap()
134//
135// Destructeur
136//--
137{
138 InitNul();
139}
140
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}
154
155//++
156template<class T>
157void LocalMap<T>::ReSize(int nx, int ny)
158//
159// Redimensionne l'espace de stokage des pixels
160//--
161{
162 InitNul();
163 nSzX_ = nx;
164 nSzY_ = ny;
165 nPix_= nx*ny;
166 pixels_.ReSize(nPix_);
167 pixels_.Reset();
168}
169
170//++
171// Titre Methodes
172//--
173
174
175//++
176template<class T>
177int LocalMap<T>::NbPixels() const
178//
179// Retourne le nombre de pixels du découpage
180//--
181{
182 return(nPix_);
183}
184
185//++
186template<class T>
187T& LocalMap<T>::PixVal(int k)
188//
189// Retourne la valeur du contenu du pixel d'indice k
190//--
191{
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));
200}
201
202//++
203
204template<class T>
205T const& LocalMap<T>::PixVal(int k) const
206//
207// Retourne la valeur du contenu du pixel d'indice k
208//--
209{
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);
218}
219
220//++
221template<class T>
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>
230int LocalMap<T>::PixIndexSph(double theta,double phi) const
231//
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;
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)
244 double theta_aux= theta;
245 double phi_aux = phi;
246 UserToReference(theta_aux, phi_aux);
247
248 // coordonnees dans le plan local en unites de pixels
249 double x,y;
250 AngleProjToPix(theta_aux,phi_aux, x, y);
251
252 double xmin= -x0_-0.5;
253 double xmax= xmin+nSzX_;
254 if((x > xmax) || (x < xmin)) return(-1);
255 double xcurrent= xmin;
256 for(i = 0; i < nSzX_; i++ )
257 {
258 xcurrent += 1.;
259 if( x < xcurrent ) break;
260 }
261 double ymin= -y0_-0.5;
262 double ymax= ymin+nSzY_;
263 if((y > ymax) || (y < ymin)) return(-1);
264 double ycurrent= ymin;
265 for(j = 0; j < nSzY_; j++ )
266 {
267 ycurrent += 1.;
268 if( y < ycurrent ) break;
269 }
270 return (j*nSzX_+i);
271}
272
273//++
274template<class T>
275void LocalMap<T>::PixThetaPhi(int k,double& theta,double& phi) const
276//
277// Retourne les coordonnées (theta,phi) du milieu du pixel d'indice k
278//--
279{
280 if(!(originFlag_) || !(extensFlag_))
281 {
282 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
283 exit(0);
284 }
285
286 int i,j;
287 Getij(k,i,j);
288
289 double X= double(i-x0_);
290 double Y= double(j-y0_);
291 // situation de ce pixel dans le plan de reference
292 double x= X*cos_angle_-Y*sin_angle_;
293 double y= X*sin_angle_+Y* cos_angle_;
294 // projection sur la sphere
295 PixProjToAngle(x, y, theta, phi);
296 // retour au plan utilisateur
297 ReferenceToUser(theta, phi);
298}
299
300//++
301template<class T>
302double LocalMap<T>::PixSolAngle(int k) const
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.
307//--
308{
309 int i,j;
310 Getij(k,i,j);
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
318 // situation dans le plan de reference
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
326 // projection sur la sphere
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
332 // angle solide
333 double sol= fabs((xa-x0)*(yb-y0)-(xb-x0)*(ya-y0));
334 return sol;
335}
336
337//++
338template<class T>
339void LocalMap<T>::SetOrigin(double theta0,double phi0,double angle)
340//
341// Fixe la repere de reference ( angles en degres)
342//--
343{
344 theta0_= theta0;
345 phi0_ = phi0;
346 angle_ = angle;
347 x0_= nSzX_/2;
348 y0_= nSzY_/2;
349 cos_angle_= cos(angle*Pi/180.);
350 sin_angle_= sin(angle*Pi/180.);
351 originFlag_= true;
352 cout << " LocalMap:: set origin 1 done" << endl;
353}
354
355//++
356template<class T>
357void LocalMap<T>::SetOrigin(double theta0,double phi0,int x0,int y0,double angle)
358//
359// Fixe le repere de reference (angles en degres)
360//--
361{
362 theta0_= theta0;
363 phi0_ = phi0;
364 angle_ = angle;
365 x0_= x0;
366 y0_= y0;
367 cos_angle_= cos(angle*Pi/180.);
368 sin_angle_= sin(angle*Pi/180.);
369 originFlag_= true;
370 cout << " LocalMap:: set origin 2 done" << endl;
371}
372
373//++
374template<class T>
375void LocalMap<T>::SetSize(double angleX,double angleY)
376//
377// Fixe l'extension de la carte (angles en degres)
378//--
379{
380 angleX_= angleX;
381 angleY_= angleY;
382
383 // tangente de la moitie de l'ouverture angulaire totale
384 tgAngleX_= tan(0.5*angleX_*Pi/180.);
385 tgAngleY_= tan(0.5*angleY_*Pi/180.);
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
396//--
397{
398 for(int m = 0; m < nPix_; m++)
399 {
400 double theta,phi;
401 PixThetaPhi(m,theta,phi);
402 sphere(theta,phi)= pixels_(m);
403 // cout << "theta " << theta << " phi " << phi << " valeur " << sphere(theta,phi)<< endl;
404 }
405}
406
407//++
408template<class T>
409void LocalMap<T>::Getij(int k,int& i,int& j) const
410//
411//--
412{
413 i= (k+1)%nSzX_-1;
414 if(i == -1) i= nSzX_-1;
415 j= (k-i+2)/nSzX_;
416}
417
418//++
419template<class T>
420void LocalMap<T>::ReferenceToUser(double& theta,double& phi) const
421//
422// --
423{
424 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
425 {
426 throw "LocalMap::ReferenceToUser (theta,phi) out of range";
427 }
428
429 theta= theta0_*Pi/180.+theta-Pi*0.5;
430 if(theta < 0.)
431 {
432 theta= -theta;
433 phi += Pi;
434 }
435 else
436 {
437 if(theta > Pi)
438 {
439 theta= 2.*Pi-theta;
440 phi += Pi;
441 }
442 }
443
444 phi= phi0_*Pi/180.+phi;
445 while(phi >= 2.*Pi) phi-= 2.*Pi;
446
447 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
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 }
453}
454
455//++
456template<class T>
457void LocalMap<T>::UserToReference(double& theta,double& phi) const
458//
459// --
460{
461 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
462 {
463 cout<<" LocalMap::UserToReference: exceptions a mettre en place" <<endl;
464 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
465 throw "LocalMap::UserToReference (theta,phi) out of range";
466 }
467
468 double phi1= phi-phi0_*Pi/180.;
469 if(phi1 < 0.) phi1+= 2.*Pi;
470
471 double theta1= theta-theta0_*Pi/180.+Pi*0.5;
472 if(theta1 < 0.)
473 {
474 theta= -theta1;
475 phi1+= Pi;
476 }
477 else
478 {
479 if(theta1 > Pi)
480 {
481 theta= 2.*Pi-theta1;
482 phi1+= Pi;
483 }
484 }
485
486 while(phi1 >= 2.*Pi) phi1-= 2.*Pi;
487 phi= phi1;
488 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
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 }
494}
495
496//++
497template<class T>
498void LocalMap<T>::PixProjToAngle(double x,double y,double& theta,double& phi) const
499//
500// (x,y) representent les coordonnees en unites de pixels d'un point DANS LE PLAN DE REFERENCE.
501// On recupere (theta,phi) par rapport au repere "absolu" theta=pi/2 et phi=0.
502//--
503{
504 theta= Pi*0.5-atan(2.*y*tgAngleY_/(double)nSzY_);
505 phi= atan2(2.*x*tgAngleX_,(double)nSzX_);
506 if(phi < 0.) phi += DeuxPi;
507}
508
509//++
510template<class T>
511void LocalMap<T>::AngleProjToPix(double theta,double phi,double& x,double& y) const
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{
517 if(phi >= Pi) phi-= DeuxPi;
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}
522
523template<class T>
524void LocalMap<T>::print(ostream& os) const
525{
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 }
535
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;
543}
544
545//*******************************************************************
546// class FIO_LocalMap<T>
547// Les objets delegues pour la gestion de persistance
548//*******************************************************************
549
550template <class T>
551FIO_LocalMap<T>::FIO_LocalMap()
552{
553 dobj= new LocalMap<T>;
554 ownobj= true;
555}
556
557template <class T>
558FIO_LocalMap<T>::FIO_LocalMap(string const& filename)
559{
560 dobj= new LocalMap<T>;
561 ownobj= true;
562 Read(filename);
563}
564
565template <class T>
566FIO_LocalMap<T>::FIO_LocalMap(const LocalMap<T>& obj)
567{
568 dobj= new LocalMap<T>(obj);
569 ownobj= true;
570}
571
572template <class T>
573FIO_LocalMap<T>::FIO_LocalMap(LocalMap<T>* obj)
574{
575 dobj= obj;
576 ownobj= false;
577}
578
579template <class T>
580FIO_LocalMap<T>::~FIO_LocalMap()
581{
582 if (ownobj && dobj) delete dobj;
583}
584
585template <class T>
586AnyDataObj* FIO_LocalMap<T>::DataObj()
587{
588 return(dobj);
589}
590
591template <class T>
592void FIO_LocalMap<T>::ReadSelf(PInPersist& is)
593{
594 cout << " FIO_LocalMap:: ReadSelf " << endl;
595
596 if(dobj == NULL)
597 {
598 dobj= new LocalMap<T>;
599 }
600
601// Let's Read the SphereCoordSys object -- ATTENTIOn - $CHECK$
602 SphereCoordSys* cs = dynamic_cast<SphereCoordSys*>(is.ReadObject());
603 dobj->SetCoordSys(cs);
604
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
615 int nSzX;
616 is.GetI4(nSzX);
617 dobj->setSize_x(nSzX);
618
619 int nSzY;
620 is.GetI4(nSzY);
621 dobj->setSize_y(nSzY);
622
623 int nPix;
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;
633 int x0, y0;
634 double theta, phi, angle;
635 is.GetI4(x0);
636 is.GetI4(y0);
637 is.GetR8(theta);
638 is.GetR8(phi);
639 is.GetR8(angle);
640 dobj->SetOrigin(theta, phi, x0, y0, angle);
641
642 double angleX, angleY;
643 is.GetR8(angleX);
644 is.GetR8(angleY);
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;
652}
653
654template <class T>
655void FIO_LocalMap<T>::WriteSelf(POutPersist& os) const
656{
657 cout << " FIO_LocalMap:: WriteSelf " << endl;
658
659 if(dobj == NULL)
660 {
661 cout << " WriteSelf:: dobj= null " << endl;
662 return;
663 }
664
665// Let's write the SphereCoordSys object
666 dobj->GetCoordSys()->Write(os);
667
668 char strg[256];
669 int nSzX= dobj->Size_x();
670 int nSzY= dobj->Size_y();
671 int nPix= dobj->NbPixels();
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 }
684
685 os.PutI4(nSzX);
686 os.PutI4(nSzY);
687 os.PutI4(nPix);
688
689 if(dobj->LocalMap_isDone())
690 {
691 string ss("local mapping is done");
692 os.PutStr(ss);
693 int x0, y0;
694 double theta, phi, angle;
695 dobj->Origin(theta, phi, x0, y0, angle);
696 os.PutI4(x0);
697 os.PutI4(y0);
698 os.PutR8(theta);
699 os.PutR8(phi);
700 os.PutR8(angle);
701
702 double angleX, angleY;
703 dobj->Aperture(angleX, angleY);
704 os.PutR8(angleX);
705 os.PutR8(angleY);
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.