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

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

ajout arg share ds constructeurs, et methode ContainsSph ds localmap.cc et operateur = , Reza 12/11/99

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