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

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

ajout doc GLM

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//
[568]42// A local map is a 2 dimensional array, with i as column index and j
43// as row index. The map is supposed to lie on a plan tangent to the
44// celestial sphere in a point whose coordinates are (x0,y0) on the local
45// map and (theta0, phi0) on the sphere. The range of the map is defined
46// by two values of angles covered respectively by all the pixels in
47// x direction and all the pixels in y direction (SetSize()).
48//
49// A "reference plane" is considered : this plane is tangent to the
50// celestial sphere in a point with angles theta=Pi/2 and phi=0. This
51// point is the origine of coordinates is of the reference plane. The
52// x-axis is the tangent parallel to the equatorial line and oriented
53// toward the increasing phi's ; the y-axis is parallel to the meridian
54// line and oriented toward the north pole.
55//
56// Internally, a map is first defined within this reference plane and
57// tranported until the point (theta0, phi0) in such a way that both
58// axes are kept parallel to meridian and parallel lines of the sphere.
59// The user can define its own map with axes rotated with respect to
60// reference axes (this rotation is characterized by angle between
61// the local parallel line and the wanted x-axis-- see method
62// SetOrigin(...))
63//
64//
[228]65
66//
67//--
68//++
69//
70// Links Parents
71//
72// PixelMap
73//
74//--
75//++
76//
[568]77// Links Childs
[228]78//
79//
80//--
81//++
[568]82// Titre Constructors
[228]83//--
84//++
[470]85template<class T>
86LocalMap<T>::LocalMap()
87//
[568]88//
[228]89//--
[470]90{
91 InitNul();
92}
[228]93
94//++
[470]95template<class T>
[473]96LocalMap<T>::LocalMap(int nx, int ny) : nSzX_(nx), nSzY_(ny)
[470]97//
[568]98//
[228]99//--
100{
101 InitNul();
[470]102 nPix_= nx*ny;
103 pixels_.ReSize(nPix_);
104 pixels_.Reset();
[228]105}
106
107//++
[470]108template<class T>
109LocalMap<T>::LocalMap(const LocalMap<T>& lm)
110//
[568]111// copy constructor
[228]112//--
[470]113{
114 cout<<" LocalMap:: Appel du constructeur de recopie " << endl;
115
116 if(lm.mInfo_) mInfo_= new DVList(*lm.mInfo_);
117 nSzX_= lm.nSzX_;
118 nSzY_= lm.nSzY_;
119 nPix_= lm.nPix_;
120 originFlag_= lm.originFlag_;
121 extensFlag_= lm.extensFlag_;
122 x0_= lm.x0_;
123 y0_= lm.y0_;
124 theta0_= lm.theta0_;
125 phi0_= lm.phi0_;
126 angle_= lm.angle_;
127 cos_angle_= lm.cos_angle_;
128 sin_angle_= lm.sin_angle_;
129 angleX_= lm.angleX_;
130 angleY_= lm.angleY_;
131 tgAngleX_= lm.tgAngleX_;
132 tgAngleY_= lm.tgAngleY_;
133 pixels_= lm.pixels_;
134}
135
[228]136//++
[568]137// Titre Destructor
138//--
139//++
[470]140template<class T>
141LocalMap<T>::~LocalMap()
142//
[228]143//--
144{
[470]145 InitNul();
[228]146}
147
[568]148
149
[470]150//++
[568]151// Titre Public Methods
[470]152//--
[228]153
[470]154//++
155template<class T>
[473]156void LocalMap<T>::ReSize(int nx, int ny)
[470]157//
[568]158// Resize storage area for pixels
[470]159//--
[228]160{
[470]161 InitNul();
162 nSzX_ = nx;
163 nSzY_ = ny;
164 nPix_= nx*ny;
165 pixels_.ReSize(nPix_);
166 pixels_.Reset();
[228]167}
168
169//++
[470]170template<class T>
[473]171int LocalMap<T>::NbPixels() const
[470]172//
[568]173// Return number of pixels
[228]174//--
175{
[470]176 return(nPix_);
[228]177}
178
179//++
[470]180template<class T>
[473]181T& LocalMap<T>::PixVal(int k)
[470]182//
[568]183// Return value of pixel with index k
[228]184//--
185{
[470]186 if((k < 0) || (k >= nPix_))
187 {
188 cout << " LocalMap::PIxVal : exceptions a mettre en place" <<endl;
189 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
190 THROW(rangeCheckErr);
191 //throw "LocalMap::PIxVal Pixel index out of range";
192 }
193 return(pixels_(k));
[228]194}
195
196//++
197
[470]198template<class T>
[473]199T const& LocalMap<T>::PixVal(int k) const
[470]200//
[568]201// const version of previous method
[228]202//--
203{
[470]204 if((k < 0) || (k >= nPix_))
205 {
206 cout << " LocalMap::PIxVal : exceptions a mettre en place" <<endl;
207 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
208
209 throw "LocalMap::PIxVal Pixel index out of range";
210 }
211 return *(pixels_.Data()+k);
[228]212}
213
214//++
[470]215template<class T>
[473]216int LocalMap<T>::PixIndexSph(double theta,double phi) const
[470]217//
[568]218// Return index of the pixel with spherical coordinates (theta,phi)
[228]219//--
220{
221 int i,j;
[470]222 if(!(originFlag_) || !(extensFlag_))
223 {
224 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
225 exit(0);
226 }
227
228 // theta et phi en coordonnees relatives (on se ramene a une situation par rapport au plan de reference)
[473]229 double theta_aux= theta;
230 double phi_aux = phi;
[228]231 UserToReference(theta_aux, phi_aux);
[470]232
[228]233 // coordonnees dans le plan local en unites de pixels
[473]234 double x,y;
235 AngleProjToPix(theta_aux,phi_aux, x, y);
[228]236
[473]237 double xmin= -x0_-0.5;
238 double xmax= xmin+nSzX_;
239 if((x > xmax) || (x < xmin)) return(-1);
240 double xcurrent= xmin;
[470]241 for(i = 0; i < nSzX_; i++ )
242 {
243 xcurrent += 1.;
244 if( x < xcurrent ) break;
245 }
[473]246 double ymin= -y0_-0.5;
247 double ymax= ymin+nSzY_;
[470]248 if((y > ymax) || (y < ymin)) return(-1);
[473]249 double ycurrent= ymin;
[470]250 for(j = 0; j < nSzY_; j++ )
251 {
252 ycurrent += 1.;
253 if( y < ycurrent ) break;
254 }
255 return (j*nSzX_+i);
[228]256}
257
258//++
[470]259template<class T>
[473]260void LocalMap<T>::PixThetaPhi(int k,double& theta,double& phi) const
[470]261//
[568]262// Return (theta, phi) coordinates of pixel with index k
[228]263//--
264{
[470]265 if(!(originFlag_) || !(extensFlag_))
266 {
267 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
268 exit(0);
269 }
270
[228]271 int i,j;
272 Getij(k,i,j);
[470]273
[473]274 double X= double(i-x0_);
275 double Y= double(j-y0_);
[228]276 // situation de ce pixel dans le plan de reference
[473]277 double x= X*cos_angle_-Y*sin_angle_;
278 double y= X*sin_angle_+Y* cos_angle_;
[228]279 // projection sur la sphere
[470]280 PixProjToAngle(x, y, theta, phi);
[228]281 // retour au plan utilisateur
282 ReferenceToUser(theta, phi);
283}
284
285//++
[470]286template<class T>
[473]287double LocalMap<T>::PixSolAngle(int k) const
[470]288//
[568]289// Pixel Solid angle (steradians)
290// All the pixels have not necessarly the same size in (theta, phi)
291// because of the projection scheme which is not yet fixed.
[228]292//--
293{
294 int i,j;
295 Getij(k,i,j);
[473]296 double X= double(i-x0_);
297 double Y= double(j-y0_);
298 double XR= X+double(i)*0.5;
299 double XL= X-double(i)*0.5;
300 double YU= Y+double(j)*0.5;
301 double YL= Y-double(j)*0.5;
302
[228]303 // situation dans le plan de reference
[473]304 double x0= XL*cos_angle_-YL*sin_angle_;
305 double y0= XL*sin_angle_+YL*cos_angle_;
306 double xa= XR*cos_angle_-YL*sin_angle_;
307 double ya= XR*sin_angle_+YL*cos_angle_;
308 double xb= XL*cos_angle_-YU*sin_angle_;
309 double yb= XL*sin_angle_+YU*cos_angle_;
310
[228]311 // projection sur la sphere
[473]312 double thet0,phi0,theta,phia,thetb,phib;
313 PixProjToAngle(x0, y0, thet0, phi0);
314 PixProjToAngle(xa, ya, theta, phia);
315 PixProjToAngle(xb, yb, thetb, phib);
316
[228]317 // angle solide
[473]318 double sol= fabs((xa-x0)*(yb-y0)-(xb-x0)*(ya-y0));
319 return sol;
[228]320}
321
322//++
[470]323template<class T>
[473]324void LocalMap<T>::SetOrigin(double theta0,double phi0,double angle)
[470]325//
[568]326// set the referential of the map (angles in degrees)
327// (default x0=siz_x/2, y0=siz_y/2)
[228]328//--
329{
[473]330 theta0_= theta0;
331 phi0_ = phi0;
332 angle_ = angle;
[470]333 x0_= nSzX_/2;
334 y0_= nSzY_/2;
[473]335 cos_angle_= cos(angle*Pi/180.);
336 sin_angle_= sin(angle*Pi/180.);
[470]337 originFlag_= true;
338 cout << " LocalMap:: set origin 1 done" << endl;
[228]339}
340
341//++
[470]342template<class T>
[473]343void LocalMap<T>::SetOrigin(double theta0,double phi0,int x0,int y0,double angle)
[470]344//
[568]345// set the referential of the map (angles in degrees)
[228]346//--
347{
[473]348 theta0_= theta0;
349 phi0_ = phi0;
350 angle_ = angle;
[470]351 x0_= x0;
352 y0_= y0;
[473]353 cos_angle_= cos(angle*Pi/180.);
354 sin_angle_= sin(angle*Pi/180.);
[470]355 originFlag_= true;
356 cout << " LocalMap:: set origin 2 done" << endl;
[228]357}
358
359//++
[470]360template<class T>
[473]361void LocalMap<T>::SetSize(double angleX,double angleY)
[470]362//
[568]363// angle range of tthe map (angles in degrees)
[470]364//--
365{
[473]366 angleX_= angleX;
367 angleY_= angleY;
[228]368
[470]369 // tangente de la moitie de l'ouverture angulaire totale
[473]370 tgAngleX_= tan(0.5*angleX_*Pi/180.);
371 tgAngleY_= tan(0.5*angleY_*Pi/180.);
[470]372
373 extensFlag_= true;
374 cout << " LocalMap:: set extension done" << endl;
375}
376
377//++
378template<class T>
379void LocalMap<T>::Project(SphericalMap<T>& sphere) const
380//
[568]381// Projection to a spherical map
[228]382//--
383{
[470]384 for(int m = 0; m < nPix_; m++)
385 {
[473]386 double theta,phi;
[470]387 PixThetaPhi(m,theta,phi);
388 sphere(theta,phi)= pixels_(m);
389 // cout << "theta " << theta << " phi " << phi << " valeur " << sphere(theta,phi)<< endl;
390 }
[228]391}
[568]392// Titre Private Methods
393//++
394template<class T>
395void LocalMap<T>::InitNul()
396//
397// set some attributes to zero
398//--
399{
400 originFlag_= false;
401 extensFlag_= false;
402 cos_angle_= 1.0;
403 sin_angle_= 0.0;
404 pixels_.Reset();
405}
[470]406
[228]407//++
[470]408template<class T>
[473]409void LocalMap<T>::Getij(int k,int& i,int& j) const
[470]410//
[568]411// Return 2 indices corresponding to the pixel number k
[228]412//--
413{
[470]414 i= (k+1)%nSzX_-1;
415 if(i == -1) i= nSzX_-1;
416 j= (k-i+2)/nSzX_;
[228]417}
418
[470]419//++
420template<class T>
[473]421void LocalMap<T>::ReferenceToUser(double& theta,double& phi) const
[228]422//
[568]423// Transform a pair of coordinates (theta, phi) given in
424// reference coordinates into map coordinates
425//--
[470]426{
[473]427 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]428 {
429 throw "LocalMap::ReferenceToUser (theta,phi) out of range";
430 }
431
[473]432 theta= theta0_*Pi/180.+theta-Pi*0.5;
[470]433 if(theta < 0.)
434 {
435 theta= -theta;
[473]436 phi += Pi;
[470]437 }
438 else
439 {
[473]440 if(theta > Pi)
[470]441 {
[473]442 theta= 2.*Pi-theta;
443 phi += Pi;
[470]444 }
445 }
446
[473]447 phi= phi0_*Pi/180.+phi;
448 while(phi >= 2.*Pi) phi-= 2.*Pi;
[470]449
[473]450 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]451 {
452 cout << " LocalMap::ReferenceToUser : erreur bizarre dans le transfert a la carte utilisateur " << endl;
453 cout << " theta= " << theta << " phi= " << phi << endl;
454 exit(0);
455 }
[228]456}
457
[470]458//++
459template<class T>
[473]460void LocalMap<T>::UserToReference(double& theta,double& phi) const
[228]461//
[568]462// Transform a pair of coordinates (theta, phi) given in
463// map coordinates into reference coordinates
464//--
[470]465{
[473]466 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]467 {
[473]468 cout<<" LocalMap::UserToReference: exceptions a mettre en place" <<endl;
469 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
[470]470 throw "LocalMap::UserToReference (theta,phi) out of range";
471 }
472
[473]473 double phi1= phi-phi0_*Pi/180.;
474 if(phi1 < 0.) phi1+= 2.*Pi;
[470]475
[473]476 double theta1= theta-theta0_*Pi/180.+Pi*0.5;
[470]477 if(theta1 < 0.)
478 {
479 theta= -theta1;
[473]480 phi1+= Pi;
[470]481 }
482 else
483 {
[473]484 if(theta1 > Pi)
[470]485 {
[473]486 theta= 2.*Pi-theta1;
487 phi1+= Pi;
[470]488 }
489 }
490
[473]491 while(phi1 >= 2.*Pi) phi1-= 2.*Pi;
[470]492 phi= phi1;
[473]493 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
[470]494 {
495 cout << " LocalMap::UserToReference : erreur bizarre dans le transfert a la carte de reference " << endl;
496 cout << " theta= " << theta << " phi= " << phi << endl;
497 exit(0);
498 }
[228]499}
500
[470]501//++
502template<class T>
[473]503void LocalMap<T>::PixProjToAngle(double x,double y,double& theta,double& phi) const
[470]504//
[568]505//
506// Given coordinates in pixel units in the REFERENCE PLANE, return
507// (theta, phi) in "absolute" referential theta=pi/2 ,phi=0.
[470]508//--
[228]509{
[473]510 theta= Pi*0.5-atan(2.*y*tgAngleY_/(double)nSzY_);
511 phi= atan2(2.*x*tgAngleX_,(double)nSzX_);
[470]512 if(phi < 0.) phi += DeuxPi;
[228]513}
514
[470]515//++
516template<class T>
[473]517void LocalMap<T>::AngleProjToPix(double theta,double phi,double& x,double& y) const
[470]518//
[568]519// Given coordinates (theta, phi) in "absolute" referential
520// theta=pi/2 ,phi=0 return pixel indices (i,j) in the REFERENCE PLANE.
[470]521//--
522{
[473]523 if(phi >= Pi) phi-= DeuxPi;
[470]524 // y=0.5*mSzY_*cot(theta)/tgAngleY_; $CHECK-REZA-04/99$
525 y= 0.5*nSzY_/tan(theta)/tgAngleY_; // ? cot = 1/tan ?
526 x= 0.5*nSzX_*tan(phi)/tgAngleX_;
527}
[228]528
[470]529template<class T>
530void LocalMap<T>::print(ostream& os) const
[228]531{
[470]532 os<<" SzX= "<<nSzX_<<", SzY= "<<nSzY_<<", NPix= "<<nPix_<<endl;
533 if(LocalMap_isDone())
534 {
535 os<<" theta0= "<<theta0_<<", phi0= "<<phi0_<<", angle= "<<angle_<<endl;
536 os<<" x0= "<<x0_<<", y0= "<<y0_<<endl;
537 os<<" cos= "<<cos_angle_<<", & sin= "<<sin_angle_<<endl;
538 os<<" angleX= "<<angleX_<<", angleY= "<<angleY_<<endl;
539 os<<" tg(angleX)= "<<tgAngleX_<<", tg(angleY)= "<<tgAngleY_<<endl;
540 }
[228]541
[470]542 os << " contenu de pixels : ";
543 for(int i=0; i < nPix_; i++)
544 {
545 if(i%5 == 0) os << endl;
546 os << pixels_(i) <<", ";
547 }
548 os << endl;
[228]549}
[568]550//++
551// Titre class FIO_LocalMap
552// Delegated objects for persitance management
553//--
[228]554
[470]555//*******************************************************************
556// class FIO_LocalMap<T>
557// Les objets delegues pour la gestion de persistance
558//*******************************************************************
[228]559
[568]560//++
[470]561template <class T>
562FIO_LocalMap<T>::FIO_LocalMap()
[568]563//
564//--
[228]565{
[470]566 dobj= new LocalMap<T>;
567 ownobj= true;
568}
[568]569//++
[470]570template <class T>
571FIO_LocalMap<T>::FIO_LocalMap(string const& filename)
[568]572//
573//--
[470]574{
575 dobj= new LocalMap<T>;
576 ownobj= true;
577 Read(filename);
578}
[228]579
[568]580//++
[470]581template <class T>
582FIO_LocalMap<T>::FIO_LocalMap(const LocalMap<T>& obj)
[568]583//
584//--
[470]585{
586 dobj= new LocalMap<T>(obj);
587 ownobj= true;
[228]588}
589
[470]590template <class T>
591FIO_LocalMap<T>::FIO_LocalMap(LocalMap<T>* obj)
592{
593 dobj= obj;
594 ownobj= false;
595}
[228]596
[568]597//++
[470]598template <class T>
599FIO_LocalMap<T>::~FIO_LocalMap()
[568]600//
601//--
[470]602{
603 if (ownobj && dobj) delete dobj;
604}
[568]605//++
[470]606template <class T>
607AnyDataObj* FIO_LocalMap<T>::DataObj()
[568]608//
609//--
[470]610{
611 return(dobj);
[228]612}
613
[568]614//++
[470]615template <class T>
616void FIO_LocalMap<T>::ReadSelf(PInPersist& is)
[568]617//
618//--
[470]619{
[228]620
[470]621 if(dobj == NULL)
622 {
623 dobj= new LocalMap<T>;
624 }
625
626 // Pour savoir s'il y avait un DVList Info associe
627 char strg[256];
628 is.GetLine(strg, 255);
629 bool hadinfo= false;
630 if(strncmp(strg+strlen(strg)-7, "HasInfo", 7) == 0) hadinfo= true;
631 if(hadinfo)
632 { // Lecture eventuelle du DVList Info
633 is >> dobj->Info();
634 }
635
[473]636 int nSzX;
[470]637 is.GetI4(nSzX);
638 dobj->setSize_x(nSzX);
639
[473]640 int nSzY;
[470]641 is.GetI4(nSzY);
642 dobj->setSize_y(nSzY);
643
[473]644 int nPix;
[470]645 is.GetI4(nPix);
646 dobj->setNbPixels(nPix);
647
648 string ss("local mapping is done");
649 string sso;
650 is.GetStr(sso);
651 if(sso == ss)
652 {
653 cout<<" ReadSelf:: local mapping"<<endl;
[473]654 int x0, y0;
655 double theta, phi, angle;
[470]656 is.GetI4(x0);
657 is.GetI4(y0);
[473]658 is.GetR8(theta);
659 is.GetR8(phi);
660 is.GetR8(angle);
[470]661 dobj->SetOrigin(theta, phi, x0, y0, angle);
662
[473]663 double angleX, angleY;
664 is.GetR8(angleX);
665 is.GetR8(angleY);
[470]666 dobj->SetSize(angleX, angleY);
667 }
668
669 T* pixels= new T[nPix];
670 PIOSReadArray(is, pixels, nPix);
671 dobj->setDataBlock(pixels, nPix);
672 delete [] pixels;
[228]673}
674
[568]675//++
[470]676template <class T>
677void FIO_LocalMap<T>::WriteSelf(POutPersist& os) const
[568]678//
679//--
[470]680{
681 if(dobj == NULL)
682 {
[568]683 cout << " FIO_LocalMap::WriteSelf:: dobj= null " << endl;
[470]684 return;
685 }
[228]686
[470]687 char strg[256];
[473]688 int nSzX= dobj->Size_x();
689 int nSzY= dobj->Size_y();
690 int nPix= dobj->NbPixels();
[470]691
692 if(dobj->ptrInfo())
693 {
694 sprintf(strg,"LocalMap: NPixX=%6d NPixY=%9d HasInfo",nSzX,nSzY);
695 os.PutLine(strg);
696 os << dobj->Info();
697 }
698 else
699 {
700 sprintf(strg,"LocalMap: NPixX=%6d NPixY=%9d ",nSzX,nSzY);
701 os.PutLine(strg);
702 }
[228]703
[470]704 os.PutI4(nSzX);
705 os.PutI4(nSzY);
706 os.PutI4(nPix);
[228]707
[470]708 if(dobj->LocalMap_isDone())
709 {
710 string ss("local mapping is done");
711 os.PutStr(ss);
[473]712 int x0, y0;
713 double theta, phi, angle;
[470]714 dobj->Origin(theta, phi, x0, y0, angle);
715 os.PutI4(x0);
716 os.PutI4(y0);
[473]717 os.PutR8(theta);
718 os.PutR8(phi);
719 os.PutR8(angle);
[470]720
[473]721 double angleX, angleY;
[470]722 dobj->Aperture(angleX, angleY);
[473]723 os.PutR8(angleX);
724 os.PutR8(angleY);
[470]725 }
726 else
727 {
728 string ss("no local mapping");
729 os.PutStr(ss);
730 }
731
732 PIOSWriteArray(os,(dobj->getDataBlock())->Data(), nPix);
733}
734
Note: See TracBrowser for help on using the repository browser.