source: Sophya/trunk/SophyaLib/SkyMap/localmap.cc@ 828

Last change on this file since 828 was 809, checked in by ansari, 25 years ago

Adaptation modifs TArray<T>,PPersist - Reza 03/04/2000

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