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

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

Bugs InitNul et autres - Reza 20/11/99

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