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
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// 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//
65
66//
67//--
68//++
69//
70// Links Parents
71//
72// PixelMap
73//
74//--
75//++
76//
77// Links Childs
78//
79//
80//--
81//++
82// Titre Constructors
83//--
84//++
85template<class T>
86LocalMap<T>::LocalMap()
87//
88//
89//--
90{
91 InitNul();
92}
93
94//++
95template<class T>
96LocalMap<T>::LocalMap(int nx, int ny) : nSzX_(nx), nSzY_(ny)
97//
98//
99//--
100{
101 InitNul();
102 nPix_= nx*ny;
103 pixels_.ReSize(nPix_);
104 pixels_.Reset();
105}
106
107//++
108template<class T>
109LocalMap<T>::LocalMap(const LocalMap<T>& lm)
110//
111// copy constructor
112//--
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
136//++
137// Titre Destructor
138//--
139//++
140template<class T>
141LocalMap<T>::~LocalMap()
142//
143//--
144{
145 InitNul();
146}
147
148
149
150//++
151// Titre Public Methods
152//--
153
154//++
155template<class T>
156void LocalMap<T>::ReSize(int nx, int ny)
157//
158// Resize storage area for pixels
159//--
160{
161 InitNul();
162 nSzX_ = nx;
163 nSzY_ = ny;
164 nPix_= nx*ny;
165 pixels_.ReSize(nPix_);
166 pixels_.Reset();
167}
168
169//++
170template<class T>
171int LocalMap<T>::NbPixels() const
172//
173// Return number of pixels
174//--
175{
176 return(nPix_);
177}
178
179//++
180template<class T>
181T& LocalMap<T>::PixVal(int k)
182//
183// Return value of pixel with index k
184//--
185{
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));
194}
195
196//++
197
198template<class T>
199T const& LocalMap<T>::PixVal(int k) const
200//
201// const version of previous method
202//--
203{
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);
212}
213
214//++
215template<class T>
216int LocalMap<T>::PixIndexSph(double theta,double phi) const
217//
218// Return index of the pixel with spherical coordinates (theta,phi)
219//--
220{
221 int i,j;
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)
229 double theta_aux= theta;
230 double phi_aux = phi;
231 UserToReference(theta_aux, phi_aux);
232
233 // coordonnees dans le plan local en unites de pixels
234 double x,y;
235 AngleProjToPix(theta_aux,phi_aux, x, y);
236
237 double xmin= -x0_-0.5;
238 double xmax= xmin+nSzX_;
239 if((x > xmax) || (x < xmin)) return(-1);
240 double xcurrent= xmin;
241 for(i = 0; i < nSzX_; i++ )
242 {
243 xcurrent += 1.;
244 if( x < xcurrent ) break;
245 }
246 double ymin= -y0_-0.5;
247 double ymax= ymin+nSzY_;
248 if((y > ymax) || (y < ymin)) return(-1);
249 double ycurrent= ymin;
250 for(j = 0; j < nSzY_; j++ )
251 {
252 ycurrent += 1.;
253 if( y < ycurrent ) break;
254 }
255 return (j*nSzX_+i);
256}
257
258//++
259template<class T>
260void LocalMap<T>::PixThetaPhi(int k,double& theta,double& phi) const
261//
262// Return (theta, phi) coordinates of pixel with index k
263//--
264{
265 if(!(originFlag_) || !(extensFlag_))
266 {
267 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
268 exit(0);
269 }
270
271 int i,j;
272 Getij(k,i,j);
273
274 double X= double(i-x0_);
275 double Y= double(j-y0_);
276 // situation de ce pixel dans le plan de reference
277 double x= X*cos_angle_-Y*sin_angle_;
278 double y= X*sin_angle_+Y* cos_angle_;
279 // projection sur la sphere
280 PixProjToAngle(x, y, theta, phi);
281 // retour au plan utilisateur
282 ReferenceToUser(theta, phi);
283}
284
285//++
286template<class T>
287double LocalMap<T>::PixSolAngle(int k) const
288//
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.
292//--
293{
294 int i,j;
295 Getij(k,i,j);
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
303 // situation dans le plan de reference
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
311 // projection sur la sphere
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
317 // angle solide
318 double sol= fabs((xa-x0)*(yb-y0)-(xb-x0)*(ya-y0));
319 return sol;
320}
321
322//++
323template<class T>
324void LocalMap<T>::SetOrigin(double theta0,double phi0,double angle)
325//
326// set the referential of the map (angles in degrees)
327// (default x0=siz_x/2, y0=siz_y/2)
328//--
329{
330 theta0_= theta0;
331 phi0_ = phi0;
332 angle_ = angle;
333 x0_= nSzX_/2;
334 y0_= nSzY_/2;
335 cos_angle_= cos(angle*Pi/180.);
336 sin_angle_= sin(angle*Pi/180.);
337 originFlag_= true;
338 cout << " LocalMap:: set origin 1 done" << endl;
339}
340
341//++
342template<class T>
343void LocalMap<T>::SetOrigin(double theta0,double phi0,int x0,int y0,double angle)
344//
345// set the referential of the map (angles in degrees)
346//--
347{
348 theta0_= theta0;
349 phi0_ = phi0;
350 angle_ = angle;
351 x0_= x0;
352 y0_= y0;
353 cos_angle_= cos(angle*Pi/180.);
354 sin_angle_= sin(angle*Pi/180.);
355 originFlag_= true;
356 cout << " LocalMap:: set origin 2 done" << endl;
357}
358
359//++
360template<class T>
361void LocalMap<T>::SetSize(double angleX,double angleY)
362//
363// angle range of tthe map (angles in degrees)
364//--
365{
366 angleX_= angleX;
367 angleY_= angleY;
368
369 // tangente de la moitie de l'ouverture angulaire totale
370 tgAngleX_= tan(0.5*angleX_*Pi/180.);
371 tgAngleY_= tan(0.5*angleY_*Pi/180.);
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//
381// Projection to a spherical map
382//--
383{
384 for(int m = 0; m < nPix_; m++)
385 {
386 double theta,phi;
387 PixThetaPhi(m,theta,phi);
388 sphere(theta,phi)= pixels_(m);
389 // cout << "theta " << theta << " phi " << phi << " valeur " << sphere(theta,phi)<< endl;
390 }
391}
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}
406
407//++
408template<class T>
409void LocalMap<T>::Getij(int k,int& i,int& j) const
410//
411// Return 2 indices corresponding to the pixel number k
412//--
413{
414 i= (k+1)%nSzX_-1;
415 if(i == -1) i= nSzX_-1;
416 j= (k-i+2)/nSzX_;
417}
418
419//++
420template<class T>
421void LocalMap<T>::ReferenceToUser(double& theta,double& phi) const
422//
423// Transform a pair of coordinates (theta, phi) given in
424// reference coordinates into map coordinates
425//--
426{
427 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
428 {
429 throw "LocalMap::ReferenceToUser (theta,phi) out of range";
430 }
431
432 theta= theta0_*Pi/180.+theta-Pi*0.5;
433 if(theta < 0.)
434 {
435 theta= -theta;
436 phi += Pi;
437 }
438 else
439 {
440 if(theta > Pi)
441 {
442 theta= 2.*Pi-theta;
443 phi += Pi;
444 }
445 }
446
447 phi= phi0_*Pi/180.+phi;
448 while(phi >= 2.*Pi) phi-= 2.*Pi;
449
450 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
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 }
456}
457
458//++
459template<class T>
460void LocalMap<T>::UserToReference(double& theta,double& phi) const
461//
462// Transform a pair of coordinates (theta, phi) given in
463// map coordinates into reference coordinates
464//--
465{
466 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
467 {
468 cout<<" LocalMap::UserToReference: exceptions a mettre en place" <<endl;
469 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
470 throw "LocalMap::UserToReference (theta,phi) out of range";
471 }
472
473 double phi1= phi-phi0_*Pi/180.;
474 if(phi1 < 0.) phi1+= 2.*Pi;
475
476 double theta1= theta-theta0_*Pi/180.+Pi*0.5;
477 if(theta1 < 0.)
478 {
479 theta= -theta1;
480 phi1+= Pi;
481 }
482 else
483 {
484 if(theta1 > Pi)
485 {
486 theta= 2.*Pi-theta1;
487 phi1+= Pi;
488 }
489 }
490
491 while(phi1 >= 2.*Pi) phi1-= 2.*Pi;
492 phi= phi1;
493 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
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 }
499}
500
501//++
502template<class T>
503void LocalMap<T>::PixProjToAngle(double x,double y,double& theta,double& phi) const
504//
505//
506// Given coordinates in pixel units in the REFERENCE PLANE, return
507// (theta, phi) in "absolute" referential theta=pi/2 ,phi=0.
508//--
509{
510 theta= Pi*0.5-atan(2.*y*tgAngleY_/(double)nSzY_);
511 phi= atan2(2.*x*tgAngleX_,(double)nSzX_);
512 if(phi < 0.) phi += DeuxPi;
513}
514
515//++
516template<class T>
517void LocalMap<T>::AngleProjToPix(double theta,double phi,double& x,double& y) const
518//
519// Given coordinates (theta, phi) in "absolute" referential
520// theta=pi/2 ,phi=0 return pixel indices (i,j) in the REFERENCE PLANE.
521//--
522{
523 if(phi >= Pi) phi-= DeuxPi;
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}
528
529template<class T>
530void LocalMap<T>::print(ostream& os) const
531{
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 }
541
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;
549}
550//++
551// Titre class FIO_LocalMap
552// Delegated objects for persitance management
553//--
554
555//*******************************************************************
556// class FIO_LocalMap<T>
557// Les objets delegues pour la gestion de persistance
558//*******************************************************************
559
560//++
561template <class T>
562FIO_LocalMap<T>::FIO_LocalMap()
563//
564//--
565{
566 dobj= new LocalMap<T>;
567 ownobj= true;
568}
569//++
570template <class T>
571FIO_LocalMap<T>::FIO_LocalMap(string const& filename)
572//
573//--
574{
575 dobj= new LocalMap<T>;
576 ownobj= true;
577 Read(filename);
578}
579
580//++
581template <class T>
582FIO_LocalMap<T>::FIO_LocalMap(const LocalMap<T>& obj)
583//
584//--
585{
586 dobj= new LocalMap<T>(obj);
587 ownobj= true;
588}
589
590template <class T>
591FIO_LocalMap<T>::FIO_LocalMap(LocalMap<T>* obj)
592{
593 dobj= obj;
594 ownobj= false;
595}
596
597//++
598template <class T>
599FIO_LocalMap<T>::~FIO_LocalMap()
600//
601//--
602{
603 if (ownobj && dobj) delete dobj;
604}
605//++
606template <class T>
607AnyDataObj* FIO_LocalMap<T>::DataObj()
608//
609//--
610{
611 return(dobj);
612}
613
614//++
615template <class T>
616void FIO_LocalMap<T>::ReadSelf(PInPersist& is)
617//
618//--
619{
620
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
636 int nSzX;
637 is.GetI4(nSzX);
638 dobj->setSize_x(nSzX);
639
640 int nSzY;
641 is.GetI4(nSzY);
642 dobj->setSize_y(nSzY);
643
644 int nPix;
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;
654 int x0, y0;
655 double theta, phi, angle;
656 is.GetI4(x0);
657 is.GetI4(y0);
658 is.GetR8(theta);
659 is.GetR8(phi);
660 is.GetR8(angle);
661 dobj->SetOrigin(theta, phi, x0, y0, angle);
662
663 double angleX, angleY;
664 is.GetR8(angleX);
665 is.GetR8(angleY);
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;
673}
674
675//++
676template <class T>
677void FIO_LocalMap<T>::WriteSelf(POutPersist& os) const
678//
679//--
680{
681 if(dobj == NULL)
682 {
683 cout << " FIO_LocalMap::WriteSelf:: dobj= null " << endl;
684 return;
685 }
686
687 char strg[256];
688 int nSzX= dobj->Size_x();
689 int nSzY= dobj->Size_y();
690 int nPix= dobj->NbPixels();
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 }
703
704 os.PutI4(nSzX);
705 os.PutI4(nSzY);
706 os.PutI4(nPix);
707
708 if(dobj->LocalMap_isDone())
709 {
710 string ss("local mapping is done");
711 os.PutStr(ss);
712 int x0, y0;
713 double theta, phi, angle;
714 dobj->Origin(theta, phi, x0, y0, angle);
715 os.PutI4(x0);
716 os.PutI4(y0);
717 os.PutR8(theta);
718 os.PutR8(phi);
719 os.PutR8(angle);
720
721 double angleX, angleY;
722 dobj->Aperture(angleX, angleY);
723 os.PutR8(angleX);
724 os.PutR8(angleY);
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.