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

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

modifs constructeurs copie et operateur =

File size: 14.3 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() : pixels_()
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
99 if(lm.mInfo_) mInfo_= new DVList(*lm.mInfo_);
100 recopierVariablesSimples(lm);
101}
102
103//++
104template<class T>
105LocalMap<T>::LocalMap(const LocalMap<T>& lm)
106 : pixels_(lm.pixels_)
107
108//
109// copy constructor
110//--
111{
112
113 if(lm.mInfo_) mInfo_= new DVList(*lm.mInfo_);
114 recopierVariablesSimples(lm);
115}
116
117//++
118// Titre Destructor
119//--
120//++
121template<class T>
122LocalMap<T>::~LocalMap()
123//
124//--
125{
126 InitNul();
127}
128
129
130
131//++
132// Titre Public Methods
133//--
134
135//++
136template<class T>
137void LocalMap<T>::ReSize(int_4 nx, int_4 ny)
138//
139// Resize storage area for pixels
140//--
141{
142 InitNul();
143 nSzX_ = nx;
144 nSzY_ = ny;
145 nPix_= nx*ny;
146 pixels_.ReSize(nPix_);
147 pixels_.Reset();
148}
149
150////////////////////////// methodes de copie/share
151
152template<class T>
153void LocalMap<T>::CloneOrShare(const LocalMap<T>& a)
154{
155 recopierVariablesSimples(a);
156 pixels_.CloneOrShare(a.pixels_);
157}
158
159template<class T>
160LocalMap<T>& LocalMap<T>::CopyElt(const LocalMap<T>& a)
161{
162 if (NbPixels() < 1)
163 throw RangeCheckError("LocalMap<T>::CopyElt(const LocalMap<T>& ) - Not Allocated Array ! ");
164 if (NbPixels() != a.NbPixels())
165 throw(SzMismatchError("LocalMap<T>::CopyElt(const LocalMap<T>&) SizeMismatch")) ;
166 recopierVariablesSimples(a);
167 int k;
168 for (k=0; k< nPix_; k++) pixels_(k) = a.pixels_(k);
169 return(*this);
170
171}
172
173template<class T>
174LocalMap<T>& LocalMap<T>::Set(const LocalMap<T>& a)
175 {
176 if (this != &a)
177 {
178 if (a.NbPixels() < 1)
179 throw RangeCheckError("LocalMap<T>::Set(a ) - Array a not allocated ! ");
180 if (NbPixels() < 1) CloneOrShare(a);
181 else CopyElt(a);
182 if (mInfo_) delete mInfo_;
183 mInfo_ = NULL;
184 if (a.mInfo_) mInfo_ = new DVList(*(a.mInfo_));
185 }
186 return(*this);
187 }
188
189
190//++
191template<class T>
192int_4 LocalMap<T>::NbPixels() const
193//
194// Return number of pixels
195//--
196{
197 return(nPix_);
198}
199
200//++
201template<class T>
202T& LocalMap<T>::PixVal(int_4 k)
203//
204// Return value of pixel with index k
205//--
206{
207 if((k < 0) || (k >= nPix_))
208 {
209 cout << " LocalMap::PIxVal : exceptions a mettre en place" <<endl;
210 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
211 THROW(rangeCheckErr);
212 //throw "LocalMap::PIxVal Pixel index out of range";
213 }
214 return(pixels_(k));
215}
216
217//++
218
219template<class T>
220T const& LocalMap<T>::PixVal(int_4 k) const
221//
222// const version of previous method
223//--
224{
225 if((k < 0) || (k >= nPix_))
226 {
227 cout << " LocalMap::PIxVal : exceptions a mettre en place" <<endl;
228 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
229
230 throw "LocalMap::PIxVal Pixel index out of range";
231 }
232 return *(pixels_.Data()+k);
233}
234
235template<class T>
236bool LocalMap<T>::ContainsSph(double theta, double phi) const
237{
238// $CHECK$ Reza 11/11/99 - A modifier
239return(true);
240}
241
242//++
243template<class T>
244int_4 LocalMap<T>::PixIndexSph(double theta,double phi) const
245//
246// Return index of the pixel with spherical coordinates (theta,phi)
247//--
248{
249 int_4 i,j;
250 if(!(originFlag_) || !(extensFlag_))
251 {
252 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
253 exit(0);
254 }
255
256 // theta et phi en coordonnees relatives (on se ramene a une situation par rapport au plan de reference)
257 double theta_aux= theta;
258 double phi_aux = phi;
259 UserToReference(theta_aux, phi_aux);
260
261 // coordonnees dans le plan local en unites de pixels
262 double x,y;
263 AngleProjToPix(theta_aux,phi_aux, x, y);
264
265 double xmin= -x0_-0.5;
266 double xmax= xmin+nSzX_;
267 if((x > xmax) || (x < xmin)) return(-1);
268 double xcurrent= xmin;
269 for(i = 0; i < nSzX_; i++ )
270 {
271 xcurrent += 1.;
272 if( x < xcurrent ) break;
273 }
274 double ymin= -y0_-0.5;
275 double ymax= ymin+nSzY_;
276 if((y > ymax) || (y < ymin)) return(-1);
277 double ycurrent= ymin;
278 for(j = 0; j < nSzY_; j++ )
279 {
280 ycurrent += 1.;
281 if( y < ycurrent ) break;
282 }
283 return (j*nSzX_+i);
284}
285
286//++
287template<class T>
288void LocalMap<T>::PixThetaPhi(int_4 k,double& theta,double& phi) const
289//
290// Return (theta, phi) coordinates of pixel with index k
291//--
292{
293 if(!(originFlag_) || !(extensFlag_))
294 {
295 cout << " LocalMap: correspondance carte-sphere non etablie" << endl;
296 exit(0);
297 }
298
299 int_4 i,j;
300 Getij(k,i,j);
301
302 double X= double(i-x0_);
303 double Y= double(j-y0_);
304 // situation de ce pixel dans le plan de reference
305 double x= X*cos_angle_-Y*sin_angle_;
306 double y= X*sin_angle_+Y* cos_angle_;
307 // projection sur la sphere
308 PixProjToAngle(x, y, theta, phi);
309 // retour au plan utilisateur
310 ReferenceToUser(theta, phi);
311}
312
313template <class T>
314T LocalMap<T>::SetPixels(T v)
315{
316pixels_.Reset(v);
317return(v);
318}
319
320//++
321template<class T>
322double LocalMap<T>::PixSolAngle(int_4 k) const
323//
324// Pixel Solid angle (steradians)
325// All the pixels have not necessarly the same size in (theta, phi)
326// because of the projection scheme which is not yet fixed.
327//--
328{
329 int_4 i,j;
330 Getij(k,i,j);
331 double X= double(i-x0_);
332 double Y= double(j-y0_);
333 double XR= X+double(i)*0.5;
334 double XL= X-double(i)*0.5;
335 double YU= Y+double(j)*0.5;
336 double YL= Y-double(j)*0.5;
337
338 // situation dans le plan de reference
339 double x0= XL*cos_angle_-YL*sin_angle_;
340 double y0= XL*sin_angle_+YL*cos_angle_;
341 double xa= XR*cos_angle_-YL*sin_angle_;
342 double ya= XR*sin_angle_+YL*cos_angle_;
343 double xb= XL*cos_angle_-YU*sin_angle_;
344 double yb= XL*sin_angle_+YU*cos_angle_;
345
346 // projection sur la sphere
347 double thet0,phi0,theta,phia,thetb,phib;
348 PixProjToAngle(x0, y0, thet0, phi0);
349 PixProjToAngle(xa, ya, theta, phia);
350 PixProjToAngle(xb, yb, thetb, phib);
351
352 // angle solide
353 double sol= fabs((xa-x0)*(yb-y0)-(xb-x0)*(ya-y0));
354 return sol;
355}
356
357//++
358template<class T>
359void LocalMap<T>::SetOrigin(double theta0,double phi0,double angle)
360//
361// set the referential of the map (angles in degrees)
362// (default x0=siz_x/2, y0=siz_y/2)
363//--
364{
365 theta0_= theta0;
366 phi0_ = phi0;
367 angle_ = angle;
368 x0_= nSzX_/2;
369 y0_= nSzY_/2;
370 cos_angle_= cos(angle*Pi/180.);
371 sin_angle_= sin(angle*Pi/180.);
372 originFlag_= true;
373 cout << " LocalMap:: set origin 1 done" << endl;
374}
375
376//++
377template<class T>
378void LocalMap<T>::SetOrigin(double theta0,double phi0,int_4 x0,int_4 y0,double angle)
379//
380// set the referential of the map (angles in degrees)
381//--
382{
383 theta0_= theta0;
384 phi0_ = phi0;
385 angle_ = angle;
386 x0_= x0;
387 y0_= y0;
388 cos_angle_= cos(angle*Pi/180.);
389 sin_angle_= sin(angle*Pi/180.);
390 originFlag_= true;
391 cout << " LocalMap:: set origin 2 done" << endl;
392}
393
394//++
395template<class T>
396void LocalMap<T>::SetSize(double angleX,double angleY)
397//
398// angle range of tthe map (angles in degrees)
399//--
400{
401 angleX_= angleX;
402 angleY_= angleY;
403
404 // tangente de la moitie de l'ouverture angulaire totale
405 tgAngleX_= tan(0.5*angleX_*Pi/180.);
406 tgAngleY_= tan(0.5*angleY_*Pi/180.);
407
408 extensFlag_= true;
409 cout << " LocalMap:: set extension done" << endl;
410}
411
412//++
413template<class T>
414void LocalMap<T>::Project(SphericalMap<T>& sphere) const
415//
416// Projection to a spherical map
417//--
418{
419 for(int m = 0; m < nPix_; m++)
420 {
421 double theta,phi;
422 PixThetaPhi(m,theta,phi);
423 sphere(theta,phi)= pixels_(m);
424 // cout << "theta " << theta << " phi " << phi << " valeur " << sphere(theta,phi)<< endl;
425 }
426}
427// Titre Private Methods
428//++
429template<class T>
430void LocalMap<T>::InitNul()
431//
432// set some attributes to zero
433//--
434{
435 originFlag_= false;
436 extensFlag_= false;
437 cos_angle_= 1.0;
438 sin_angle_= 0.0;
439// pixels_.Reset(); Pas de reset par InitNul (en cas de share) - Reza 20/11/99 $CHECK$
440}
441
442//++
443template<class T>
444void LocalMap<T>::Getij(int_4 k,int_4& i,int_4& j) const
445//
446// Return 2 indices corresponding to the pixel number k
447//--
448{
449 i= (k+1)%nSzX_-1;
450 if(i == -1) i= nSzX_-1;
451 j= (k-i+2)/nSzX_;
452}
453
454//++
455template<class T>
456void LocalMap<T>::ReferenceToUser(double& theta,double& phi) const
457//
458// Transform a pair of coordinates (theta, phi) given in
459// reference coordinates into map coordinates
460//--
461{
462 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
463 {
464 throw "LocalMap::ReferenceToUser (theta,phi) out of range";
465 }
466
467 theta= theta0_*Pi/180.+theta-Pi*0.5;
468 if(theta < 0.)
469 {
470 theta= -theta;
471 phi += Pi;
472 }
473 else
474 {
475 if(theta > Pi)
476 {
477 theta= 2.*Pi-theta;
478 phi += Pi;
479 }
480 }
481
482 phi= phi0_*Pi/180.+phi;
483 while(phi >= 2.*Pi) phi-= 2.*Pi;
484
485 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
486 {
487 cout << " LocalMap::ReferenceToUser : erreur bizarre dans le transfert a la carte utilisateur " << endl;
488 cout << " theta= " << theta << " phi= " << phi << endl;
489 exit(0);
490 }
491}
492
493//++
494template<class T>
495void LocalMap<T>::UserToReference(double& theta,double& phi) const
496//
497// Transform a pair of coordinates (theta, phi) given in
498// map coordinates into reference coordinates
499//--
500{
501 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
502 {
503 cout<<" LocalMap::UserToReference: exceptions a mettre en place" <<endl;
504 // THROW(out_of_range("LocalMap::PIxVal Pixel index out of range"));
505 throw "LocalMap::UserToReference (theta,phi) out of range";
506 }
507
508 double phi1= phi-phi0_*Pi/180.;
509 if(phi1 < 0.) phi1+= 2.*Pi;
510
511 double theta1= theta-theta0_*Pi/180.+Pi*0.5;
512 if(theta1 < 0.)
513 {
514 theta= -theta1;
515 phi1+= Pi;
516 }
517 else
518 {
519 if(theta1 > Pi)
520 {
521 theta= 2.*Pi-theta1;
522 phi1+= Pi;
523 }
524 }
525
526 while(phi1 >= 2.*Pi) phi1-= 2.*Pi;
527 phi= phi1;
528 if(theta > Pi || theta < 0. || phi < 0. || phi >= 2*Pi)
529 {
530 cout << " LocalMap::UserToReference : erreur bizarre dans le transfert a la carte de reference " << endl;
531 cout << " theta= " << theta << " phi= " << phi << endl;
532 exit(0);
533 }
534}
535
536//++
537template<class T>
538void LocalMap<T>::PixProjToAngle(double x,double y,double& theta,double& phi) const
539//
540//
541// Given coordinates in pixel units in the REFERENCE PLANE, return
542// (theta, phi) in "absolute" referential theta=pi/2 ,phi=0.
543//--
544{
545 theta= Pi*0.5-atan(2.*y*tgAngleY_/(double)nSzY_);
546 phi= atan2(2.*x*tgAngleX_,(double)nSzX_);
547 if(phi < 0.) phi += DeuxPi;
548}
549
550//++
551template<class T>
552void LocalMap<T>::AngleProjToPix(double theta,double phi,double& x,double& y) const
553//
554// Given coordinates (theta, phi) in "absolute" referential
555// theta=pi/2 ,phi=0 return pixel indices (i,j) in the REFERENCE PLANE.
556//--
557{
558 if(phi >= Pi) phi-= DeuxPi;
559 // y=0.5*mSzY_*cot(theta)/tgAngleY_; $CHECK-REZA-04/99$
560 y= 0.5*nSzY_/tan(theta)/tgAngleY_; // ? cot = 1/tan ?
561 x= 0.5*nSzX_*tan(phi)/tgAngleX_;
562}
563
564template<class T>
565void LocalMap<T>::print(ostream& os) const
566{
567 os<<" SzX= "<<nSzX_<<", SzY= "<<nSzY_<<", NPix= "<<nPix_<<endl;
568 if(LocalMap_isDone())
569 {
570 os<<" theta0= "<<theta0_<<", phi0= "<<phi0_<<", angle= "<<angle_<<endl;
571 os<<" x0= "<<x0_<<", y0= "<<y0_<<endl;
572 os<<" cos= "<<cos_angle_<<", & sin= "<<sin_angle_<<endl;
573 os<<" angleX= "<<angleX_<<", angleY= "<<angleY_<<endl;
574 os<<" tg(angleX)= "<<tgAngleX_<<", tg(angleY)= "<<tgAngleY_<<endl;
575 }
576
577 os << " contenu de pixels : ";
578 for(int i=0; i < nPix_; i++)
579 {
580 if(i%5 == 0) os << endl;
581 os << pixels_(i) <<", ";
582 }
583 os << endl;
584}
585
586
587template<class T>
588void LocalMap<T>::recopierVariablesSimples(const LocalMap<T>& lm)
589{
590 nSzX_= lm.nSzX_;
591 nSzY_= lm.nSzY_;
592 nPix_= lm.nPix_;
593 originFlag_= lm.originFlag_;
594 extensFlag_= lm.extensFlag_;
595 x0_= lm.x0_;
596 y0_= lm.y0_;
597 theta0_= lm.theta0_;
598 phi0_= lm.phi0_;
599 angle_= lm.angle_;
600 cos_angle_= lm.cos_angle_;
601 sin_angle_= lm.sin_angle_;
602 angleX_= lm.angleX_;
603 angleY_= lm.angleY_;
604 tgAngleX_= lm.tgAngleX_;
605 tgAngleY_= lm.tgAngleY_;
606}
607
608
609//++
610// Titre class FIO_LocalMap
611// Delegated objects for persitance management
612//--
613
614
615#ifdef __CXX_PRAGMA_TEMPLATES__
616#pragma define_template LocalMap<r_8>
617#pragma define_template LocalMap<r_4>
618#pragma define_template LocalMap< complex<r_8> >
619#pragma define_template LocalMap< complex<r_4> >
620#endif
621#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
622template class LocalMap<r_8>;
623template class LocalMap<r_4>;
624template class LocalMap< complex<r_8> >;
625template class LocalMap< complex<r_4> >;
626#endif
Note: See TracBrowser for help on using the repository browser.