[299] | 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 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
[103] | 29 | #include <sstream> |
---|
[3] | 30 | #include <cpgplot.h> |
---|
| 31 | #include <math.h> |
---|
[393] | 32 | #include <duchamp/duchamp.hh> |
---|
| 33 | #include <duchamp/param.hh> |
---|
| 34 | #include <duchamp/fitsHeader.hh> |
---|
| 35 | #include <duchamp/Cubes/cubes.hh> |
---|
[463] | 36 | #include <duchamp/Cubes/cubeUtils.hh> |
---|
[393] | 37 | #include <duchamp/Utils/utils.hh> |
---|
| 38 | #include <duchamp/Utils/mycpgplot.hh> |
---|
| 39 | #include <duchamp/PixelMap/Voxel.hh> |
---|
| 40 | #include <duchamp/PixelMap/Object3D.hh> |
---|
[258] | 41 | #include <vector> |
---|
[3] | 42 | |
---|
[142] | 43 | const int MIN_WIDTH=20; |
---|
[146] | 44 | using namespace mycpgplot; |
---|
[258] | 45 | using namespace PixelInfo; |
---|
[142] | 46 | |
---|
[378] | 47 | namespace duchamp |
---|
[3] | 48 | { |
---|
[83] | 49 | |
---|
[378] | 50 | void Cube::drawMomentCutout(Detection &object) |
---|
| 51 | { |
---|
[528] | 52 | /// @details |
---|
| 53 | /// A routine to draw the 0th moment for the given detection |
---|
| 54 | /// using the flux given by the pixel array in the Cube. |
---|
| 55 | /// The 0th moment is constructed by adding the flux of each |
---|
| 56 | /// pixel within the full extent of the object (this may be more |
---|
| 57 | /// pixels than were actually detected in the object) |
---|
| 58 | /// A tick mark is also drawn to indicate angular scale (but only |
---|
| 59 | /// if the WCS for the Cube is valid). |
---|
| 60 | /// \param object The Detection to be drawn. |
---|
[117] | 61 | |
---|
[913] | 62 | if(!cpgtest()){ |
---|
| 63 | DUCHAMPERROR("Draw Cutout","There is no PGPlot device open."); |
---|
| 64 | } |
---|
[378] | 65 | else{ |
---|
[3] | 66 | |
---|
[931] | 67 | size_t size = (object.getXmax()-object.getXmin()+1); |
---|
| 68 | if(size<size_t(object.getYmax()-object.getYmin()+1)) |
---|
[378] | 69 | size = object.getYmax()-object.getYmin()+1; |
---|
| 70 | size += MIN_WIDTH; |
---|
[3] | 71 | |
---|
[378] | 72 | long xmin = (object.getXmax()+object.getXmin())/2 - size/2 + 1; |
---|
| 73 | long xmax = (object.getXmax()+object.getXmin())/2 + size/2; |
---|
| 74 | long ymin = (object.getYmax()+object.getYmin())/2 - size/2 + 1; |
---|
| 75 | long ymax = (object.getYmax()+object.getYmin())/2 + size/2; |
---|
| 76 | long zmin = object.getZmin(); |
---|
| 77 | long zmax = object.getZmax(); |
---|
| 78 | |
---|
| 79 | bool *isGood = new bool[size*size]; |
---|
[931] | 80 | for(size_t i=0;i<size*size;i++) isGood[i]=true; |
---|
[378] | 81 | for(int z=zmin; z<=zmax; z++){ |
---|
| 82 | for(int x=xmin; x<=xmax; x++){ |
---|
| 83 | for(int y=ymin; y<=ymax; y++){ |
---|
| 84 | isGood[(y-ymin) * size + (x-xmin)] = |
---|
[931] | 85 | ((x>=0)&&(x<int(this->axisDim[0]))) // if inside the boundaries |
---|
| 86 | && ((y>=0)&&(y<int(this->axisDim[1]))) // if inside the boundaries |
---|
[378] | 87 | && !this->isBlank(x,y,z); // if not blank |
---|
| 88 | } |
---|
[142] | 89 | } |
---|
[137] | 90 | } |
---|
| 91 | |
---|
[378] | 92 | float *image = new float[size*size]; |
---|
[931] | 93 | for(size_t i=0;i<size*size;i++) image[i]=0.; |
---|
[285] | 94 | |
---|
[966] | 95 | size_t imPos,cubePos; |
---|
[378] | 96 | for(int z=zmin; z<=zmax; z++){ |
---|
| 97 | for(int x=xmin; x<=xmax; x++){ |
---|
| 98 | for(int y=ymin; y<=ymax; y++){ |
---|
[137] | 99 | |
---|
[378] | 100 | imPos = (y-ymin) * size + (x-xmin); |
---|
| 101 | cubePos = z*this->axisDim[0]*this->axisDim[1] + |
---|
| 102 | y*this->axisDim[0] + x; |
---|
[137] | 103 | |
---|
[378] | 104 | if(isGood[imPos]) image[imPos] += this->array[cubePos]; |
---|
[137] | 105 | |
---|
[378] | 106 | } |
---|
[142] | 107 | } |
---|
[3] | 108 | } |
---|
| 109 | |
---|
[931] | 110 | for(size_t i=0;i<size*size;i++){ |
---|
[378] | 111 | // if there is some signal on this pixel, normalise by the velocity width |
---|
| 112 | if(isGood[i]) image[i] /= float(zmax-zmin+1); |
---|
| 113 | } |
---|
[3] | 114 | |
---|
[378] | 115 | // now work out the greyscale display limits, |
---|
| 116 | // excluding blank pixels where necessary. |
---|
| 117 | float z1,z2; |
---|
| 118 | int ct=0; |
---|
| 119 | while(!isGood[ct]) ct++; // move to first non-blank pixel |
---|
| 120 | z1 = z2 = image[ct]; |
---|
[931] | 121 | for(size_t i=1;i<size*size;i++){ |
---|
[378] | 122 | if(isGood[i]){ |
---|
| 123 | if(image[i]<z1) z1=image[i]; |
---|
| 124 | if(image[i]>z2) z2=image[i]; |
---|
| 125 | } |
---|
[142] | 126 | } |
---|
[3] | 127 | |
---|
[378] | 128 | // adjust the values of the blank and extra-image pixels |
---|
[931] | 129 | for(size_t i=0;i<size*size;i++) |
---|
[378] | 130 | if(!isGood[i]) image[i] = z1 - 1.; |
---|
[3] | 131 | |
---|
[285] | 132 | |
---|
[378] | 133 | float tr[6] = {xmin-1,1.,0.,ymin-1,0.,1.}; |
---|
[3] | 134 | |
---|
[378] | 135 | cpgswin(xmin-0.5,xmax-0.5,ymin-0.5,ymax-0.5); |
---|
| 136 | // cpggray(image, size, size, 1, size, 1, size, z1, z2, tr); |
---|
| 137 | cpggray(image, size, size, 1, size, 1, size, z2, z1, tr); |
---|
| 138 | cpgbox("bc",0,0,"bc",0,0); |
---|
[3] | 139 | |
---|
[378] | 140 | delete [] image; |
---|
[137] | 141 | |
---|
[378] | 142 | int ci; |
---|
| 143 | cpgqci(&ci); |
---|
[137] | 144 | |
---|
[378] | 145 | // Draw the border of the BLANK region, if there is one... |
---|
| 146 | drawBlankEdges(this->array,this->axisDim[0],this->axisDim[1],this->par); |
---|
[3] | 147 | |
---|
[378] | 148 | // Draw the border of cube's pixels |
---|
| 149 | this->drawFieldEdge(); |
---|
[3] | 150 | |
---|
[1130] | 151 | // Draw the fitted ellipse |
---|
| 152 | cpgsci(RED); |
---|
[1133] | 153 | float scale=this->head.getShapeScale(); |
---|
[1176] | 154 | cpgellipse(object.getXcentre(), object.getYcentre(), |
---|
| 155 | object.getMajorAxis()/this->head.getAvPixScale()/2./scale, object.getMinorAxis()/this->head.getAvPixScale()/2./scale, |
---|
| 156 | 90.+object.getPositionAngle()); |
---|
[1130] | 157 | |
---|
[1176] | 158 | // Draw the beam |
---|
[1177] | 159 | if(this->head.beam().isDefined()){ |
---|
| 160 | setDarkGreen(); |
---|
| 161 | cpgsci(DARKGREEN); |
---|
| 162 | cpgellipse(xmax-0.5-this->head.beam().maj(), ymin-0.5+this->head.beam().maj(), |
---|
| 163 | this->head.beam().maj()/2., this->head.beam().min()/2., this->head.beam().pa()+90.); |
---|
| 164 | } |
---|
[1176] | 165 | |
---|
[378] | 166 | // Draw the borders around the object |
---|
| 167 | cpgsci(DUCHAMP_OBJECT_OUTLINE_COLOUR); |
---|
| 168 | cpgsfs(OUTLINE); |
---|
| 169 | if(this->par.drawBorders()) |
---|
| 170 | object.drawBorders(xmin,ymin); |
---|
| 171 | else |
---|
| 172 | cpgrect(object.getXmin()-xmin+0.5,object.getXmax()-xmin+1.5, |
---|
| 173 | object.getYmin()-ymin+0.5,object.getYmax()-ymin+1.5); |
---|
| 174 | /* |
---|
| 175 | To get the borders localised correctly, we need to subtract (xmin-1) |
---|
| 176 | from the X values. We then subtract 0.5 for the left hand border |
---|
| 177 | (to place it on the pixel border), and add 0.5 for the right hand |
---|
| 178 | border. Similarly for y. |
---|
| 179 | */ |
---|
[3] | 180 | |
---|
[378] | 181 | if(this->head.isWCS()){ |
---|
| 182 | // Now draw a tick mark to indicate size -- 15 arcmin in length |
---|
| 183 | // this->drawScale(xmin+2.,ymin+2.,object.getZcentre(),0.25); |
---|
| 184 | this->drawScale(xmin+2.,ymin+2.,object.getZcentre()); |
---|
| 185 | } |
---|
[142] | 186 | |
---|
[378] | 187 | cpgsci(ci); |
---|
[142] | 188 | |
---|
[378] | 189 | delete [] isGood; |
---|
[309] | 190 | |
---|
[378] | 191 | } |
---|
| 192 | |
---|
[135] | 193 | } |
---|
[117] | 194 | |
---|
[378] | 195 | void Cube::drawScale(float xstart, float ystart, float channel) |
---|
| 196 | { |
---|
[528] | 197 | /// @details |
---|
| 198 | /// A routine to draw a scale bar on a (pre-existing) PGPlot image. |
---|
| 199 | /// It uses an iterative technique to move from the given start position |
---|
| 200 | /// (xstart,ystart) along the positive x-direction so that the length is |
---|
| 201 | /// within 1% of the scaleLength (length in degrees), calculated |
---|
| 202 | /// according to the pixel scale of the cube. |
---|
| 203 | /// \param xstart X-coordinate of the start position (left-hand edge |
---|
| 204 | /// of tick mark typically). |
---|
| 205 | /// \param ystart Y-coordinate of the start position |
---|
| 206 | /// \param channel Which channel to base WCS calculations on: needed |
---|
| 207 | /// as the positions could theoretically change with channel. |
---|
[3] | 208 | |
---|
[913] | 209 | if(!cpgtest()){ |
---|
| 210 | DUCHAMPERROR("Draw Cutout","There is no PGPlot device open."); |
---|
| 211 | } |
---|
[378] | 212 | else{ |
---|
[83] | 213 | |
---|
[378] | 214 | if(this->head.isWCS()){ // can only do this if the WCS is good! |
---|
[83] | 215 | |
---|
[378] | 216 | enum ANGLE {ARCSEC, ARCMIN, DEGREE}; |
---|
| 217 | const std::string symbol[3] = {"\"", "'", mycpgplot::degrees }; |
---|
| 218 | const float angleScale[3] = {3600., 60., 1.}; |
---|
| 219 | // degree, arcmin, arcsec symbols |
---|
[142] | 220 | |
---|
[378] | 221 | const int numLengths = 17; |
---|
| 222 | const double lengths[numLengths] = |
---|
| 223 | {0.001/3600., 0.005/3600., 0.01/3600., 0.05/3600., |
---|
| 224 | 0.1/3600., 0.5/3600., |
---|
| 225 | 1./3600., 5./3600., 15./3600., 30./3600., |
---|
| 226 | 1./60., 5./60., 15./60., 30./60., |
---|
| 227 | 1., 5., 15.}; |
---|
| 228 | const ANGLE angleType[numLengths] = |
---|
| 229 | {ARCSEC, ARCSEC, ARCSEC, ARCSEC, |
---|
| 230 | ARCSEC, ARCSEC, ARCSEC, ARCSEC, |
---|
| 231 | ARCSEC, ARCSEC, |
---|
| 232 | ARCMIN, ARCMIN, ARCMIN, ARCMIN, |
---|
| 233 | DEGREE, DEGREE, DEGREE}; |
---|
| 234 | const float desiredRatio = 0.2; |
---|
[142] | 235 | |
---|
[378] | 236 | // first, work out what is the optimum length of the scale bar, |
---|
| 237 | // based on the pixel scale and size of the image. |
---|
| 238 | float pixscale = this->head.getAvPixScale(); |
---|
| 239 | double *fraction = new double[numLengths]; |
---|
[634] | 240 | int best=0; |
---|
[378] | 241 | float x1,x2,y1,y2; |
---|
| 242 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
| 243 | for(int i=0;i<numLengths;i++){ |
---|
| 244 | fraction[i] = (lengths[i]/pixscale) / (x2-x1); |
---|
| 245 | if(i==0) best=0; |
---|
| 246 | else if(fabs(fraction[i] - desiredRatio) < |
---|
| 247 | fabs(fraction[best] - desiredRatio)) best=i; |
---|
| 248 | } |
---|
| 249 | delete [] fraction; |
---|
[83] | 250 | |
---|
[378] | 251 | // Now work out actual pixel locations for the ends of the scale bar |
---|
| 252 | double *pix1 = new double[3]; |
---|
| 253 | double *pix2 = new double[3]; |
---|
| 254 | double *world1 = new double[3]; |
---|
| 255 | double *world2 = new double[3]; |
---|
[931] | 256 | pix1[0] = pix2[0] = xstart; |
---|
| 257 | pix1[1] = pix2[1] = ystart; |
---|
[378] | 258 | pix1[2] = pix2[2] = channel; |
---|
| 259 | this->head.pixToWCS(pix1,world1); |
---|
[83] | 260 | |
---|
[378] | 261 | double angSep=0.; |
---|
| 262 | double error; |
---|
| 263 | double step=1.; // this is in pixels |
---|
| 264 | double scaleLength = lengths[best]; // this is in degrees |
---|
| 265 | pix2[0] = pix1[0] + scaleLength/(2.*pixscale); |
---|
| 266 | do{ |
---|
| 267 | this->head.pixToWCS(pix2,world2); |
---|
| 268 | angSep = angularSeparation(world1[0],world1[1],world2[0],world2[1]); |
---|
| 269 | error = (angSep-scaleLength)/scaleLength; |
---|
| 270 | if(error<0) error = 0 - error; |
---|
| 271 | if(angSep>scaleLength){ |
---|
| 272 | pix2[0] -= step; |
---|
| 273 | step /= 2.; |
---|
| 274 | } |
---|
| 275 | pix2[0] += step; |
---|
| 276 | }while(error>0.05); // look for 1% change |
---|
[83] | 277 | |
---|
[931] | 278 | float tickpt1 = pix1[0]; |
---|
| 279 | float tickpt2 = pix2[0]; |
---|
[378] | 280 | float tickpt3 = ystart; |
---|
| 281 | int colour; |
---|
| 282 | cpgqci(&colour); |
---|
| 283 | cpgsci(DUCHAMP_TICKMARK_COLOUR); |
---|
| 284 | int thickness; |
---|
| 285 | cpgqlw(&thickness); |
---|
| 286 | cpgslw(3); |
---|
| 287 | cpgerrx(1,&tickpt1,&tickpt2,&tickpt3,2.); |
---|
| 288 | cpgslw(thickness); |
---|
[83] | 289 | |
---|
[378] | 290 | std::stringstream text; |
---|
| 291 | text << scaleLength * angleScale[angleType[best]] |
---|
| 292 | << symbol[angleType[best]]; |
---|
| 293 | float size,xch,ych; |
---|
| 294 | cpgqch(&size); |
---|
| 295 | cpgsch(0.4); |
---|
| 296 | cpgqcs(4,&xch,&ych); // get the character size in world coords |
---|
| 297 | cpgptxt((tickpt1+tickpt2)/2., ystart+ych, 0, 0.5, text.str().c_str()); |
---|
| 298 | cpgsch(size); |
---|
| 299 | cpgsci(colour); |
---|
[142] | 300 | |
---|
[378] | 301 | delete [] pix1; |
---|
| 302 | delete [] pix2; |
---|
| 303 | delete [] world1; |
---|
| 304 | delete [] world2; |
---|
[142] | 305 | |
---|
[378] | 306 | } |
---|
[146] | 307 | } |
---|
[378] | 308 | |
---|
[142] | 309 | } |
---|
[431] | 310 | |
---|
[378] | 311 | void Detection::drawBorders(int xoffset, int yoffset) |
---|
| 312 | { |
---|
[528] | 313 | /// @details |
---|
| 314 | /// For a given object, draw borders around the spatial extent of the object. |
---|
| 315 | /// \param xoffset The offset from 0 of the x-axis of the plotting window |
---|
| 316 | /// \param yoffset The offset from 0 of the y-axis of the plotting window |
---|
| 317 | |
---|
[913] | 318 | if(!cpgtest()){ |
---|
| 319 | DUCHAMPERROR("Draw Borders","There is no PGPlot device open."); |
---|
| 320 | } |
---|
[378] | 321 | else{ |
---|
[83] | 322 | |
---|
[378] | 323 | float x1,x2,y1,y2; |
---|
| 324 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
| 325 | int xsize = int(x2 - x1) + 1; |
---|
| 326 | int ysize = int(y2 - y1) + 1; |
---|
[3] | 327 | |
---|
[431] | 328 | std::vector<int> vertexSet = this->getVertexSet(); |
---|
| 329 | |
---|
[378] | 330 | cpgswin(0,xsize-1,0,ysize-1); |
---|
[431] | 331 | |
---|
[913] | 332 | if(vertexSet.size()%4 != 0){ |
---|
| 333 | DUCHAMPERROR("drawBorders","Vertex set wrong size!"); |
---|
| 334 | } |
---|
[431] | 335 | else{ |
---|
[623] | 336 | for(size_t i=0;i<vertexSet.size()/4;i++){ |
---|
[431] | 337 | cpgmove(vertexSet[i*4]-xoffset,vertexSet[i*4+1]-yoffset); |
---|
| 338 | cpgdraw(vertexSet[i*4+2]-xoffset,vertexSet[i*4+3]-yoffset); |
---|
[142] | 339 | } |
---|
[3] | 340 | } |
---|
[378] | 341 | cpgswin(x1,x2,y1,y2); |
---|
[3] | 342 | |
---|
[378] | 343 | } |
---|
[142] | 344 | |
---|
[378] | 345 | } |
---|
[142] | 346 | |
---|
[378] | 347 | void Cube::drawFieldEdge() |
---|
| 348 | { |
---|
[528] | 349 | /// @details |
---|
| 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 | |
---|
[913] | 354 | if(!cpgtest()){ |
---|
| 355 | DUCHAMPERROR("Draw Cutout","There is no PGPlot device open."); |
---|
| 356 | } |
---|
[378] | 357 | else{ |
---|
| 358 | int ci; |
---|
| 359 | cpgqci(&ci); |
---|
| 360 | cpgsci(DUCHAMP_CUBE_EDGE_COLOUR); |
---|
[142] | 361 | |
---|
[378] | 362 | cpgmove(-0.5,-0.5); |
---|
| 363 | cpgdraw(-0.5,this->axisDim[1]-0.5); |
---|
| 364 | cpgdraw(this->axisDim[0]-0.5,this->axisDim[1]-0.5); |
---|
| 365 | cpgdraw(this->axisDim[0]-0.5,-0.5); |
---|
| 366 | cpgdraw(-0.5,-0.5); |
---|
[142] | 367 | |
---|
[378] | 368 | cpgsci(ci); |
---|
| 369 | } |
---|
[142] | 370 | } |
---|
[378] | 371 | |
---|
[142] | 372 | } |
---|