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

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

versions templatees, NdataBlocks etc. 15-OCT-99-GLM

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