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
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>
222int LocalMap<T>::PixIndexSph(double theta,double phi) const
223//
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;
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)
236 double theta_aux= theta;
237 double phi_aux = phi;
238 UserToReference(theta_aux, phi_aux);
239
240 // coordonnees dans le plan local en unites de pixels
241 double x,y;
242 AngleProjToPix(theta_aux,phi_aux, x, y);
243
244 double xmin= -x0_-0.5;
245 double xmax= xmin+nSzX_;
246 if((x > xmax) || (x < xmin)) return(-1);
247 double xcurrent= xmin;
248 for(i = 0; i < nSzX_; i++ )
249 {
250 xcurrent += 1.;
251 if( x < xcurrent ) break;
252 }
253 double ymin= -y0_-0.5;
254 double ymax= ymin+nSzY_;
255 if((y > ymax) || (y < ymin)) return(-1);
256 double ycurrent= ymin;
257 for(j = 0; j < nSzY_; j++ )
258 {
259 ycurrent += 1.;
260 if( y < ycurrent ) break;
261 }
262 return (j*nSzX_+i);
263}
264
265//++
266template<class T>
267void LocalMap<T>::PixThetaPhi(int k,double& theta,double& phi) const
268//
269// Retourne les coordonnées (theta,phi) du milieu du pixel d'indice k
270//--
271{
272 if(!(originFlag_) || !(extensFlag_))
273 {
274 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
275 exit(0);
276 }
277
278 int i,j;
279 Getij(k,i,j);
280
281 double X= double(i-x0_);
282 double Y= double(j-y0_);
283 // situation de ce pixel dans le plan de reference
284 double x= X*cos_angle_-Y*sin_angle_;
285 double y= X*sin_angle_+Y* cos_angle_;
286 // projection sur la sphere
287 PixProjToAngle(x, y, theta, phi);
288 // retour au plan utilisateur
289 ReferenceToUser(theta, phi);
290}
291
292//++
293template<class T>
294double LocalMap<T>::PixSolAngle(int k) const
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.
299//--
300{
301 int i,j;
302 Getij(k,i,j);
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
310 // situation dans le plan de reference
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
318 // projection sur la sphere
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
324 // angle solide
325 double sol= fabs((xa-x0)*(yb-y0)-(xb-x0)*(ya-y0));
326 return sol;
327}
328
329//++
330template<class T>
331void LocalMap<T>::SetOrigin(double theta0,double phi0,double angle)
332//
333// Fixe la repere de reference ( angles en degres)
334//--
335{
336 theta0_= theta0;
337 phi0_ = phi0;
338 angle_ = angle;
339 x0_= nSzX_/2;
340 y0_= nSzY_/2;
341 cos_angle_= cos(angle*Pi/180.);
342 sin_angle_= sin(angle*Pi/180.);
343 originFlag_= true;
344 cout << " LocalMap:: set origin 1 done" << endl;
345}
346
347//++
348template<class T>
349void LocalMap<T>::SetOrigin(double theta0,double phi0,int x0,int y0,double angle)
350//
351// Fixe le repere de reference (angles en degres)
352//--
353{
354 theta0_= theta0;
355 phi0_ = phi0;
356 angle_ = angle;
357 x0_= x0;
358 y0_= y0;
359 cos_angle_= cos(angle*Pi/180.);
360 sin_angle_= sin(angle*Pi/180.);
361 originFlag_= true;
362 cout << " LocalMap:: set origin 2 done" << endl;
363}
364
365//++
366template<class T>
367void LocalMap<T>::SetSize(double angleX,double angleY)
368//
369// Fixe l'extension de la carte (angles en degres)
370//--
371{
372 angleX_= angleX;
373 angleY_= angleY;
374
375 // tangente de la moitie de l'ouverture angulaire totale
376 tgAngleX_= tan(0.5*angleX_*Pi/180.);
377 tgAngleY_= tan(0.5*angleY_*Pi/180.);
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
388//--
389{
390 for(int m = 0; m < nPix_; m++)
391 {
392 double theta,phi;
393 PixThetaPhi(m,theta,phi);
394 sphere(theta,phi)= pixels_(m);
395 // cout << "theta " << theta << " phi " << phi << " valeur " << sphere(theta,phi)<< endl;
396 }
397}
398
399//++
400template<class T>
401void LocalMap<T>::Getij(int k,int& i,int& j) const
402//
403//--
404{
405 i= (k+1)%nSzX_-1;
406 if(i == -1) i= nSzX_-1;
407 j= (k-i+2)/nSzX_;
408}
409
410//++
411template<class T>
412void LocalMap<T>::ReferenceToUser(double& theta,double& phi) const
413//
414// --
415{
416 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
417 {
418 throw "LocalMap::ReferenceToUser (theta,phi) out of range";
419 }
420
421 theta= theta0_*Pi/180.+theta-Pi*0.5;
422 if(theta < 0.)
423 {
424 theta= -theta;
425 phi += Pi;
426 }
427 else
428 {
429 if(theta > Pi)
430 {
431 theta= 2.*Pi-theta;
432 phi += Pi;
433 }
434 }
435
436 phi= phi0_*Pi/180.+phi;
437 while(phi >= 2.*Pi) phi-= 2.*Pi;
438
439 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
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 }
445}
446
447//++
448template<class T>
449void LocalMap<T>::UserToReference(double& theta,double& phi) const
450//
451// --
452{
453 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
454 {
455 cout<<" LocalMap::UserToReference: exceptions a mettre en place" <<endl;
456 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
457 throw "LocalMap::UserToReference (theta,phi) out of range";
458 }
459
460 double phi1= phi-phi0_*Pi/180.;
461 if(phi1 < 0.) phi1+= 2.*Pi;
462
463 double theta1= theta-theta0_*Pi/180.+Pi*0.5;
464 if(theta1 < 0.)
465 {
466 theta= -theta1;
467 phi1+= Pi;
468 }
469 else
470 {
471 if(theta1 > Pi)
472 {
473 theta= 2.*Pi-theta1;
474 phi1+= Pi;
475 }
476 }
477
478 while(phi1 >= 2.*Pi) phi1-= 2.*Pi;
479 phi= phi1;
480 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
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 }
486}
487
488//++
489template<class T>
490void LocalMap<T>::PixProjToAngle(double x,double y,double& theta,double& phi) const
491//
492// (x,y) representent les coordonnees en unites de pixels d'un point DANS LE PLAN DE REFERENCE.
493// On recupere (theta,phi) par rapport au repere "absolu" theta=pi/2 et phi=0.
494//--
495{
496 theta= Pi*0.5-atan(2.*y*tgAngleY_/(double)nSzY_);
497 phi= atan2(2.*x*tgAngleX_,(double)nSzX_);
498 if(phi < 0.) phi += DeuxPi;
499}
500
501//++
502template<class T>
503void LocalMap<T>::AngleProjToPix(double theta,double phi,double& x,double& y) const
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{
509 if(phi >= Pi) phi-= DeuxPi;
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}
514
515template<class T>
516void LocalMap<T>::print(ostream& os) const
517{
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 }
527
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;
535}
536
537//*******************************************************************
538// class FIO_LocalMap<T>
539// Les objets delegues pour la gestion de persistance
540//*******************************************************************
541
542template <class T>
543FIO_LocalMap<T>::FIO_LocalMap()
544{
545 dobj= new LocalMap<T>;
546 ownobj= true;
547}
548
549template <class T>
550FIO_LocalMap<T>::FIO_LocalMap(string const& filename)
551{
552 dobj= new LocalMap<T>;
553 ownobj= true;
554 Read(filename);
555}
556
557template <class T>
558FIO_LocalMap<T>::FIO_LocalMap(const LocalMap<T>& obj)
559{
560 dobj= new LocalMap<T>(obj);
561 ownobj= true;
562}
563
564template <class T>
565FIO_LocalMap<T>::FIO_LocalMap(LocalMap<T>* obj)
566{
567 dobj= obj;
568 ownobj= false;
569}
570
571template <class T>
572FIO_LocalMap<T>::~FIO_LocalMap()
573{
574 if (ownobj && dobj) delete dobj;
575}
576
577template <class T>
578AnyDataObj* FIO_LocalMap<T>::DataObj()
579{
580 return(dobj);
581}
582
583template <class T>
584void FIO_LocalMap<T>::ReadSelf(PInPersist& is)
585{
586 cout << " FIO_LocalMap:: ReadSelf " << endl;
587
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
603 int nSzX;
604 is.GetI4(nSzX);
605 dobj->setSize_x(nSzX);
606
607 int nSzY;
608 is.GetI4(nSzY);
609 dobj->setSize_y(nSzY);
610
611 int nPix;
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;
621 int x0, y0;
622 double theta, phi, angle;
623 is.GetI4(x0);
624 is.GetI4(y0);
625 is.GetR8(theta);
626 is.GetR8(phi);
627 is.GetR8(angle);
628 dobj->SetOrigin(theta, phi, x0, y0, angle);
629
630 double angleX, angleY;
631 is.GetR8(angleX);
632 is.GetR8(angleY);
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;
640}
641
642template <class T>
643void FIO_LocalMap<T>::WriteSelf(POutPersist& os) const
644{
645 cout << " FIO_LocalMap:: WriteSelf " << endl;
646
647 if(dobj == NULL)
648 {
649 cout << " WriteSelf:: dobj= null " << endl;
650 return;
651 }
652
653 char strg[256];
654 int nSzX= dobj->Size_x();
655 int nSzY= dobj->Size_y();
656 int nPix= dobj->NbPixels();
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 }
669
670 os.PutI4(nSzX);
671 os.PutI4(nSzY);
672 os.PutI4(nPix);
673
674 if(dobj->LocalMap_isDone())
675 {
676 string ss("local mapping is done");
677 os.PutStr(ss);
678 int x0, y0;
679 double theta, phi, angle;
680 dobj->Origin(theta, phi, x0, y0, angle);
681 os.PutI4(x0);
682 os.PutI4(y0);
683 os.PutR8(theta);
684 os.PutR8(phi);
685 os.PutR8(angle);
686
687 double angleX, angleY;
688 dobj->Aperture(angleX, angleY);
689 os.PutR8(angleX);
690 os.PutR8(angleY);
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.