source: tags/release-1.1/src/Cubes/drawMomentCutout.cc @ 1391

Last change on this file since 1391 was 309, checked in by Matthew Whiting, 17 years ago

Mostly changes to fix some memory allocation/deallocation issues raised by valgrind. Minor changes to the Guide.

File size: 11.5 KB
Line 
1// -----------------------------------------------------------------------
2// drawMomentCutout.cc: Drawing a 0th-moment map and related functions.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <sstream>
30#include <cpgplot.h>
31#include <math.h>
32#include <wcs.h>
33#include <duchamp.hh>
34#include <param.hh>
35#include <fitsHeader.hh>
36#include <Cubes/cubes.hh>
37#include <Utils/utils.hh>
38#include <Utils/mycpgplot.hh>
39#include <PixelMap/Voxel.hh>
40#include <PixelMap/Object3D.hh>
41#include <vector>
42
43const int MIN_WIDTH=20;
44using namespace mycpgplot;
45using namespace PixelInfo;
46
47void Cube::drawMomentCutout(Detection &object)
48{
49  /**
50   *   A routine to draw the 0th moment for the given detection
51   *    using the flux given by the pixel array in the Cube.
52   *   The 0th moment is constructed by adding the flux of each
53   *    pixel within the full extent of the object (this may be more
54   *    pixels than were actually detected in the object)
55   *   A tick mark is also drawn to indicate angular scale (but only
56   *    if the WCS for the Cube is valid).
57   * \param object The Detection to be drawn.
58   */
59
60  if(!cpgtest())
61    duchampError("Draw Cutout","There is no PGPlot device open!\n");
62  else{
63
64    long size = (object.getXmax()-object.getXmin()+1);
65    if(size<(object.getYmax()-object.getYmin()+1))
66      size = object.getYmax()-object.getYmin()+1;
67    size += MIN_WIDTH;
68
69    long xmin = (object.getXmax()+object.getXmin())/2 - size/2 + 1;
70    long xmax = (object.getXmax()+object.getXmin())/2 + size/2;
71    long ymin = (object.getYmax()+object.getYmin())/2 - size/2 + 1;
72    long ymax = (object.getYmax()+object.getYmin())/2 + size/2;
73    long zmin = object.getZmin();
74    long zmax = object.getZmax();
75
76    bool *isGood = new bool[size*size];
77    for(int i=0;i<size*size;i++) isGood[i]=true;
78    for(int z=zmin; z<=zmax; z++){
79      for(int x=xmin; x<=xmax; x++){
80        for(int y=ymin; y<=ymax; y++){
81          isGood[(y-ymin) * size + (x-xmin)] =
82            ((x>=0)&&(x<this->axisDim[0]))    // if inside the boundaries
83            && ((y>=0)&&(y<this->axisDim[1])) // if inside the boundaries
84            && !this->isBlank(x,y,z);         // if not blank
85        }
86      }
87    }
88
89    float *image = new float[size*size];
90    for(int i=0;i<size*size;i++) image[i]=0.;
91
92    int imPos,cubePos;
93    for(int z=zmin; z<=zmax; z++){
94      for(int x=xmin; x<=xmax; x++){
95        for(int y=ymin; y<=ymax; y++){
96
97          imPos = (y-ymin) * size + (x-xmin);
98          cubePos = z*this->axisDim[0]*this->axisDim[1] +
99            y*this->axisDim[0] + x;
100
101          if(isGood[imPos]) image[imPos] += this->array[cubePos];
102
103        }
104      }
105    }
106
107    for(int i=0;i<size*size;i++){
108      // if there is some signal on this pixel, normalise by the velocity width
109      if(isGood[i]) image[i] /= float(zmax-zmin+1);
110    }
111
112    // now work out the greyscale display limits,
113    //   excluding blank pixels where necessary.
114    float z1,z2;
115    int ct=0;
116    while(!isGood[ct]) ct++; // move to first non-blank pixel
117    z1 = z2 = image[ct];
118    for(int i=1;i<size*size;i++){
119      if(isGood[i]){
120        if(image[i]<z1) z1=image[i];
121        if(image[i]>z2) z2=image[i];
122      }
123    }
124
125    // adjust the values of the blank and extra-image pixels
126    for(int i=0;i<size*size;i++)
127      if(!isGood[i]) image[i] = z1 - 1.;
128
129
130    float tr[6] = {xmin-1,1.,0.,ymin-1,0.,1.};
131
132    cpgswin(xmin-0.5,xmax-0.5,ymin-0.5,ymax-0.5);
133    cpggray(image, size, size, 1, size, 1, size, z1, z2, tr);
134
135    delete [] image;
136
137    int ci;
138    cpgqci(&ci);
139
140    // Draw the border of the BLANK region, if there is one...
141    this->plotBlankEdges();
142
143    // Draw the border of cube's pixels
144    this->drawFieldEdge();
145
146    // Draw the borders around the object
147    cpgsci(BLUE);
148    cpgsfs(OUTLINE);
149    if(this->par.drawBorders())
150      object.drawBorders(xmin,ymin);
151    else
152      cpgrect(object.getXmin()-xmin+0.5,object.getXmax()-xmin+1.5,
153              object.getYmin()-ymin+0.5,object.getYmax()-ymin+1.5);
154    /*
155      To get the borders localised correctly, we need to subtract (xmin-1)
156      from the X values. We then subtract 0.5 for the left hand border
157      (to place it on the pixel border), and add 0.5 for the right hand
158      border. Similarly for y.
159    */
160
161    if(this->head.isWCS()){
162      // Now draw a tick mark to indicate size -- 15 arcmin in length
163      //     this->drawScale(xmin+2.,ymin+2.,object.getZcentre(),0.25);
164      this->drawScale(xmin+2.,ymin+2.,object.getZcentre());
165    }
166
167    cpgsci(ci);
168
169    delete [] isGood;
170
171  }
172
173}
174
175void Cube::drawScale(float xstart, float ystart, float channel)
176{
177  /**
178   *   A routine to draw a scale bar on a (pre-existing) PGPlot image.
179   *   It uses an iterative technique to move from the given start position
180   *    (xstart,ystart) along the positive x-direction so that the length is
181   *    within 1% of the scaleLength (length in degrees), calculated
182   *    according to the pixel scale of the cube.
183   *  \param xstart X-coordinate of the start position (left-hand edge
184   *  of tick mark typically).
185   *  \param ystart Y-coordinate of the start position
186   *  \param channel Which channel to base WCS calculations on: needed
187   *  as the positions could theoretically change with channel.
188   */
189
190  if(!cpgtest())
191    duchampError("Draw Cutout","There is no PGPlot device open!\n");
192  else{
193
194    if(this->head.isWCS()){  // can only do this if the WCS is good!
195
196      enum ANGLE {ARCSEC, ARCMIN, DEGREE};
197      const std::string symbol[3] = {"\"", "'", mycpgplot::degrees };
198      const float angleScale[3] = {3600., 60., 1.};
199      //  degree, arcmin, arcsec symbols
200   
201      const int numLengths = 17;
202      const double lengths[numLengths] =
203        {0.001/3600., 0.005/3600., 0.01/3600., 0.05/3600.,
204         0.1/3600., 0.5/3600.,
205         1./3600., 5./3600., 15./3600., 30./3600.,
206         1./60., 5./60., 15./60., 30./60.,
207         1., 5., 15.};
208      const ANGLE angleType[numLengths] =
209        {ARCSEC, ARCSEC, ARCSEC, ARCSEC,
210         ARCSEC, ARCSEC, ARCSEC, ARCSEC,
211         ARCSEC, ARCSEC,
212         ARCMIN, ARCMIN, ARCMIN, ARCMIN,
213         DEGREE, DEGREE, DEGREE};
214      const float desiredRatio = 0.2;
215
216      // first, work out what is the optimum length of the scale bar,
217      //   based on the pixel scale and size of the image.
218      float pixscale = this->head.getAvPixScale();
219      double *fraction = new double[numLengths];
220      int best;
221      float x1,x2,y1,y2;
222      cpgqwin(&x1,&x2,&y1,&y2);
223      for(int i=0;i<numLengths;i++){
224        fraction[i] = (lengths[i]/pixscale) / (x2-x1);
225        if(i==0) best=0;
226        else if(fabs(fraction[i] - desiredRatio) <
227                fabs(fraction[best] - desiredRatio)) best=i;
228      }
229      delete [] fraction;
230
231      // Now work out actual pixel locations for the ends of the scale bar
232      double *pix1   = new double[3];
233      double *pix2   = new double[3];
234      double *world1 = new double[3];
235      double *world2 = new double[3];
236      pix1[0] = pix2[0] = xstart + this->par.getXOffset();
237      pix1[1] = pix2[1] = ystart + this->par.getYOffset();
238      pix1[2] = pix2[2] = channel;
239      this->head.pixToWCS(pix1,world1);
240
241      double angSep=0.;
242      double error;
243      double step=1.; // this is in pixels
244      double scaleLength = lengths[best];  // this is in degrees
245      pix2[0] = pix1[0] + scaleLength/(2.*pixscale);
246      do{
247        this->head.pixToWCS(pix2,world2);
248        angSep = angularSeparation(world1[0],world1[1],world2[0],world2[1]);
249        error = (angSep-scaleLength)/scaleLength;
250        if(error<0) error = 0 - error;
251        if(angSep>scaleLength){
252          pix2[0] -= step;
253          step /= 2.;
254        }
255        pix2[0] += step;
256      }while(error>0.05); // look for 1% change
257
258      float tickpt1 = pix1[0] - this->par.getXOffset();
259      float tickpt2 = pix2[0] - this->par.getXOffset();
260      float tickpt3 = ystart;
261      int colour;
262      cpgqci(&colour);
263      cpgsci(RED);
264      int thickness;
265      cpgqlw(&thickness);
266      cpgslw(3);
267      cpgerrx(1,&tickpt1,&tickpt2,&tickpt3,2.);
268      cpgslw(thickness);
269
270      std::stringstream text;
271      text << scaleLength * angleScale[angleType[best]]
272           << symbol[angleType[best]];
273      float size,xch,ych;
274      cpgqch(&size);
275      cpgsch(0.4);
276      cpgqcs(4,&xch,&ych); // get the character size in world coords
277      cpgptxt((tickpt1+tickpt2)/2., ystart+ych, 0, 0.5, text.str().c_str());
278      cpgsch(size);
279      cpgsci(colour);
280
281      delete [] pix1;
282      delete [] pix2;
283      delete [] world1;
284      delete [] world2;
285
286    }
287  }
288
289}
290
291void Detection::drawBorders(int xoffset, int yoffset)
292{
293  /**
294   * For a given object, draw borders around the spatial extent of the object.
295   * \param xoffset The offset from 0 of the x-axis of the plotting window
296   * \param yoffset The offset from 0 of the y-axis of the plotting window
297   */
298  if(!cpgtest())
299    duchampError("Draw Borders","There is no PGPlot device open!\n");
300  else{
301
302    float x1,x2,y1,y2;
303    cpgqwin(&x1,&x2,&y1,&y2);
304    int xsize = int(x2 - x1) + 1;
305    int ysize = int(y2 - y1) + 1;
306
307    std::vector<Voxel> voxlist = this->pixelArray.getPixelSet();
308    std::vector<bool> isObj(xsize*ysize,false);
309    for(int i=0;i<voxlist.size();i++){
310      int pos = (voxlist[i].getX()-xoffset) +
311        (voxlist[i].getY()-yoffset)*xsize;
312      if(pos<xsize*ysize) isObj[pos] = true;
313    }
314    voxlist.clear();
315   
316    cpgswin(0,xsize-1,0,ysize-1);
317    for(int x=this->getXmin(); x<=this->getXmax(); x++){
318      // for each column...
319      for(int y=1;y<ysize;y++){
320        int current = y*xsize + (x-xoffset);
321        int previous = (y-1)*xsize + (x-xoffset);
322        if((isObj[current]&&!isObj[previous])   ||
323           (!isObj[current]&&isObj[previous])){
324          cpgmove(x-xoffset+0, y+0);
325          cpgdraw(x-xoffset+1, y+0);
326        }
327      }
328    }
329    for(int y=this->getYmin(); y<=this->getYmax(); y++){
330      // now for each row...
331      for(int x=1;x<xsize;x++){
332        int current = (y-yoffset)*xsize + x;
333        int previous = (y-yoffset)*xsize + x - 1;
334        if((isObj[current]&&!isObj[previous])   ||
335           (!isObj[current]&&isObj[previous])){
336          cpgmove(x+0, y-yoffset+0);
337          cpgdraw(x+0, y-yoffset+1);
338        }
339      }
340    }
341    cpgswin(x1,x2,y1,y2);
342 
343  }   
344
345}
346
347void Cube::drawFieldEdge()
348{
349  /**
350   * Draw a border around the spatial edge of the data. Lines are
351   * drawn in yellow at 0 and the values of xdim & ydim.  There must
352   * be a PGPLOT window open, else an error message is returned.
353   */
354  if(!cpgtest())
355    duchampError("Draw Cutout","There is no PGPlot device open!\n");
356  else{
357    int ci;
358    cpgqci(&ci);
359    cpgsci(YELLOW);
360 
361    cpgmove(-0.5,-0.5);
362    cpgdraw(-0.5,this->axisDim[1]-0.5);
363    cpgdraw(this->axisDim[0]-0.5,this->axisDim[1]-0.5);
364    cpgdraw(this->axisDim[0]-0.5,-0.5);
365    cpgdraw(-0.5,-0.5);
366
367    cpgsci(ci);
368  }
369}
Note: See TracBrowser for help on using the repository browser.