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

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

Compilation Mac pour CodeWarrior PRO 5

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