00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef REGION_REGION_H_
00030 #define REGION_REGION_H_
00031
00032 #include <set>
00033 #include <map>
00034 #include <list>
00035 #include <string>
00036 #include <images/Images/ImageStatistics.h>
00037 #include <measures/Measures/MDirection.h>
00038 #include <display/region/RegionInfo.h>
00039 #include <display/Utilities/dtor.h>
00040 #include <display/Display/MouseToolState.h>
00041 #include <display/Utilities/VOID.h>
00042 #include <casaqt/QtUtilities/QtId.h>
00043 #include <stdexcept>
00044 #include <QObject>
00045 #include <display/Display/DisplayCoordinateSystem.h>
00046 #include <display/region/QtRegionState.qo.h>
00047 #include <display/region/RegionEnums.h>
00048
00049 #include <casa/Arrays/Vector.h>
00050
00051 extern "C" void casa_viewer_pure_virtual( const char *file, int line, const char *func );
00052 #define DISPLAY_PURE_VIRTUAL(FUNCTION,RESULT) \
00053 { casa_viewer_pure_virtual( __FILE__, __LINE__, #FUNCTION ); return RESULT; }
00054
00055 namespace casa {
00056
00057 class WorldCanvas;
00058 class PrincipalAxesDD;
00059 template <class T> class ImageInterface;
00060
00061 class ImageRegion;
00062 class DisplayData;
00063 class HistogramTab;
00064
00065 class RegionTextList;
00066 class AnnotationBase;
00067
00068 namespace viewer {
00069
00070 class ds9writer;
00071 class QtRegionDock;
00072
00073
00074 void linear_to_screen( WorldCanvas *wc_, double, double, int &, int & );
00075 void linear_to_screen( WorldCanvas *wc_, double, double, double, double, int &, int &, int &, int & );
00076
00077 void linear_to_pixel( WorldCanvas *wc_, double, double, double &, double & );
00078 void linear_to_pixel( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00079
00080 void screen_to_linear( WorldCanvas *wc_, int, int, double &, double & );
00081 void screen_to_linear( WorldCanvas *wc_, int, int, int, int, double &, double &, double &, double & );
00082
00083 void linear_to_world( WorldCanvas *wc_, double, double, double &, double & );
00084 void linear_to_world( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00085
00086 void world_to_linear( WorldCanvas *wc_, double, double, double &, double & );
00087 void world_to_linear( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double& );
00088
00089 void pixel_to_world( WorldCanvas *wc_, double, double, double &, double & );
00090 void pixel_to_world( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00091
00092 void pixel_to_linear( WorldCanvas *wc_, double, double, double &, double & );
00093 void pixel_to_linear( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00094
00095 void world_to_screen( WorldCanvas *wc_, double, double, int &, int & );
00096 void world_to_screen( WorldCanvas *wc_, double, double, double, double, int &, int &, int &, int & );
00097
00098 void world_to_pixel( WorldCanvas *wc_, double, double, int &, int & );
00099 void world_to_pixel( WorldCanvas *wc_, double, double, double, double, int &, int &, int &, int & );
00100
00101
00102 void linear_to_j2000( WorldCanvas *wc_, double, double, double &, double & );
00103 void linear_to_j2000( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00104 void linear_to_b1950( WorldCanvas *wc_, double, double, double &, double & );
00105 void linear_to_b1950( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00106 void linear_to_galactic( WorldCanvas *wc_, double, double, double &, double & );
00107 void linear_to_galactic( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00108 void linear_to_ecliptic( WorldCanvas *wc_, double, double, double &, double & );
00109 void linear_to_ecliptic( WorldCanvas *wc_, double, double, double, double, double &, double &, double &, double & );
00110
00111 void to_linear( WorldCanvas *, MDirection::Types in_type, double, double, double &, double & );
00112 void to_linear( WorldCanvas *, MDirection::Types in_type, double, double, double, double, double &, double &, double &, double & );
00113 void to_linear_offset( WorldCanvas *, MDirection::Types in_type, double, double, double &, double & );
00114
00115 void screen_offset_to_linear_offset( WorldCanvas *wc_, int, int, double &, double & );
00116 void pixel_offset_to_linear_offset( WorldCanvas *wc_, double, double, double &, double & );
00117 void linear_offset_to_pixel_offset( WorldCanvas *wc_, double, double, double &, double & );
00118
00119 MDirection::Types get_coordinate_type( const DisplayCoordinateSystem &wc );
00120
00121 class ImageRegion_state {
00122 public:
00123 ImageRegion_state( ) : count_(0) { }
00124 ImageRegion_state( ImageRegion *ir, size_t region_count ) : imageregion(ir), count_(region_count) { }
00125 ImageRegion_state( const ImageRegion_state &other ) : imageregion(other.imageregion), count_(other.count_) { }
00126 operator SHARED_PTR<ImageRegion>( ) {
00127 return imageregion;
00128 }
00129 size_t regionCount( ) const {
00130 return count_;
00131 }
00132 private:
00133 void* operator new (std::size_t) throw (std::logic_error) {
00134 throw std::logic_error("allocating an object not intended for dynamic allocation");
00135 }
00136 SHARED_PTR<ImageRegion> imageregion;
00137 size_t count_;
00138 };
00139
00140
00141
00142
00143 class Region : public QObject, public dtorNotifier {
00144 Q_OBJECT
00145 public:
00146
00147
00148 static bool refreshNeeded( int v ) {
00149 return v & region::MouseRefresh ? true : false;
00150 }
00151 static bool regionSelected( int v ) {
00152 return v & region::MouseSelected ? true : false;
00153 }
00154 static bool handleSelected( int v ) {
00155 return v & region::MouseHandle ? true : false;
00156 }
00157
00158
00159 const std::string name( ) const {
00160 return name_;
00161 }
00162
00163 std::string lineColor( ) const {
00164 return mystate->lineColor( );
00165 }
00166 std::string centerColor( ) const {
00167 return mystate->centerColor( );
00168 }
00169 int lineWidth( ) const {
00170 return mystate->lineWidth( );
00171 }
00172 region::LineStyle lineStyle( ) const {
00173 return mystate->lineStyle( );
00174 }
00175
00176 std::string textColor( ) const {
00177 return mystate->textColor( );
00178 }
00179 std::string textFont( ) const {
00180 return mystate->textFont( );
00181 }
00182 int textFontSize( ) const {
00183 return mystate->textFontSize( );
00184 }
00185 int textFontStyle( ) const {
00186 return mystate->textFontStyle( );
00187 }
00188 std::string textValue( ) const {
00189 return mystate->textValue( );
00190 }
00191 region::TextPosition textPosition( ) const {
00192 return mystate->textPosition( );
00193 }
00194 void textPositionDelta( int &x, int &y ) const {
00195 return mystate->textPositionDelta( x, y );
00196 }
00197
00198 virtual void setLabel( const std::string &l );
00199 virtual void setLabelPosition( region::TextPosition );
00200 virtual void setLabelDelta( const std::vector<int> & );
00201 virtual void setFont( const std::string &font="", int font_size=-1, int font_style=0, const std::string &font_color="" );
00202 virtual void setLine( const std::string &line_color="", region::LineStyle line_style=region::SolidLine, unsigned int line_width=1 );
00203 virtual void setAnnotation(bool);
00204
00205 void getCoordinatesAndUnits( region::Coord &c, region::Units &x_units, region::Units &y_units,
00206 std::string &width_height_units ) const;
00207 void getPositionString( std::string &x, std::string &y, std::string &angle,
00208 double &bounding_width, double &bounding_height,
00209 region::Coord coord = region::DefaultCoord,
00210 region::Units x_units = region::DefaultUnits,
00211 region::Units y_units = region::DefaultUnits,
00212 const std::string &bounding_units = "rad" ) const;
00213
00214 bool translateX( const std::string &, const std::string &, const std::string & );
00215 bool translateY( const std::string &, const std::string &, const std::string & );
00216 bool resizeX( const std::string &, const std::string &, const std::string & );
00217 bool resizeY( const std::string &, const std::string &, const std::string & );
00218
00219 int numFrames( ) const;
00220 void zRange( int &x, int &y ) const;
00221 int zIndex( ) const;
00222 bool regionVisible( ) const {
00223 return visible_;
00224 }
00225
00226 bool worldBoundingRectangle( double &, double &, const std::string & ) const;
00227
00228 virtual ~Region( );
00229
00230 Region( ) : dock_(0), id_(QtId::get_id( )), wc_(0), selected_(false), visible_(true),
00231 mouse_in_region(false), z_index_within_range(true) { }
00232 Region( const std::string &name, WorldCanvas *wc, QtRegionDock *,
00233 bool hold_signals_=false, QtRegionState *supplied_state=0,
00234 QtMouseToolNames::PointRegionSymbols sym=QtMouseToolNames::SYM_UNKNOWN );
00235
00236
00237 virtual bool degenerate( ) const;
00238
00239 void setDrawingEnv( );
00240 void resetDrawingEnv( );
00241 void setTextEnv( );
00242 void resetTextEnv( );
00243 void pushDrawingEnv( region::LineStyle ls, int thickness=-1 );
00244 void popDrawingEnv( );
00245
00246 void setDrawCenter(bool draw_center) {
00247 draw_center_=draw_center;
00248 };
00249 bool getDrawCenter() {
00250 return draw_center_;
00251 };
00252
00253
00254 void refresh( );
00255
00256 virtual region::PointInfo checkPoint( double x, double y ) const = 0;
00257
00258
00259 virtual unsigned int mouseMovement( double , double , bool )
00260 DISPLAY_PURE_VIRTUAL(Region::mouseMovement,0);
00261
00262 virtual void draw( bool other_selected );
00263
00264
00265 void invalidateCenterInfo( );
00266
00267 bool selected( ) const {
00268 return selected_;
00269 }
00270
00271
00272 virtual bool weaklySelected( ) const;
00273 virtual void weaklySelect( bool scroll_dock=true );
00274
00275 virtual void weaklyUnselect( );
00276
00277
00278 virtual void weaklyUnselectLimited( );
00279
00280
00281
00282 virtual void selectedInCanvas( );
00283 virtual bool clickWithin( double , double ) const DISPLAY_PURE_VIRTUAL(Region::clickWithin,false);
00284 virtual int clickHandle( double , double ) const DISPLAY_PURE_VIRTUAL(Region::clickHandle,0);
00285
00286 virtual bool doubleClick( double , double );
00287
00288
00289 virtual int moveHandle( int handle, double , double ) DISPLAY_PURE_VIRTUAL(Region::moveHandle,handle);
00290 virtual void move( double , double ) DISPLAY_PURE_VIRTUAL(Region::move,);
00291 virtual void resize( double , double ) = 0;
00292 virtual bool valid_translation( double dx, double dy, double width_delta, double height_delta ) = 0;
00293
00294
00295
00296 virtual void mark( bool set=true );
00297 virtual bool marked( ) const {
00298 return mystate->marked( );
00299 }
00300
00301 virtual bool mark_toggle( );
00302 size_t selected_region_count( );
00303 size_t marked_region_count( );
00304
00305 void clearMouseInRegion( ) {
00306 mouse_in_region = false;
00307 }
00308
00309
00310 virtual void status( const std::string &msg, const std::string &type="info" );
00311
00312 virtual bool markCenter( ) const {
00313 return mystate->markCenter( );
00314 }
00315
00316 virtual bool skyComponent( ) const {
00317 return mystate->skyComponent();
00318
00319 }
00320
00321 virtual void output( ds9writer &out ) const = 0;
00322
00323
00324 virtual void boundingRectangle (double &, double &, double &, double &) const
00325 DISPLAY_PURE_VIRTUAL(Region::boundingRectangle,);
00326
00327 void emitUpdate( );
00328
00329
00330 void holdSignals( ) {
00331 hold_signals++;
00332 }
00333 virtual void releaseSignals( ) {
00334 if ( --hold_signals == 0 ) process_held_signals( );
00335 if ( hold_signals < 0 ) hold_signals = 0;
00336 }
00337
00338 virtual std::list<SHARED_PTR<RegionInfo> > *statistics( ) {
00339 return generate_dds_statistics( );
00340 }
00341
00342
00343 static SHARED_PTR<viewer::Region> creatingRegion( ) {
00344 return creating_region;
00345 }
00346 static void creatingRegionBegin( SHARED_PTR<viewer::Region> r ) {
00347 creating_region = r;
00348 }
00349 static void creatingRegionEnd( ) {
00350 creating_region.reset( );
00351 }
00352
00353
00354 int getId() const {
00355 return id_;
00356 }
00357 ImageRegion *getImageRegion( DisplayData* dd ) const {
00358 return get_image_region( dd );
00359 }
00360
00361
00362
00363
00364 virtual region::RegionTypes type( ) const = 0;
00365
00366 virtual void pixelCenter( double &, double & ) const = 0;
00367
00368
00369
00370
00371
00372
00373 QtRegionState *state( ) {
00374 return mystate;
00375 }
00376
00377
00378
00379 int markerScale( ) const {
00380 return mystate->markerScale( );
00381 }
00382 void setMarkerScale( int v ) {
00383 mystate->setMarkerScale(v);
00384 }
00385
00386
00387 void updateStateInfo( bool region_modified, region::RegionChanges );
00388 void refresh_state_gui( );
00389
00390 void clearStatistics( );
00391 void statisticsUpdateNeeded( ) {
00392 statistics_update_needed = true;
00393 }
00394
00395
00396 int &colorIndex( );
00397
00398 std::pair<int,int> &tabState( );
00399
00400 std::map<std::string,int> &coordState( );
00401
00402
00403
00404 void selectedCountUpdateNeeded( );
00405
00406
00407 QString getSaveDir( );
00408 void putSaveDir( QString );
00409 QString getLoadDir( );
00410 void putLoadDir( QString );
00411
00412 virtual bool setMarker( QtMouseToolNames::PointRegionSymbols ) {
00413 return false;
00414 }
00415
00416 virtual AnnotationBase *annotation( ) const DISPLAY_PURE_VIRTUAL(Region::annotation,0);
00417
00418 static Quantum< ::casa::Vector<double> > convert_angle( double x, const std::string &xunits, double y, const std::string &yunits,
00419 MDirection::Types original_coordsys, MDirection::Types new_coordsys, const std::string &new_units="rad" );
00420
00421 signals:
00422 void selectionChanged(viewer::Region*,bool);
00423
00424 void regionCreated( int, const QString &shape, const QString &name, const QList<double> &world_x,
00425 const QList<double> &world_y, const QList<int> &pixel_x, const QList<int> &pixel_y,
00426 const QString &linecolor, const QString &text, const QString &font, int fontsize, int fontstyle );
00427 void regionUpdate( int, viewer::region::RegionChanges, const QList<double> &world_x, const QList<double> &world_y,
00428 const QList<int> &pixel_x, const QList<int> &pixel_y );
00429 void regionChange( viewer::Region *, std::string );
00430
00431
00432 void regionUpdateResponse( int, const QString &shape, const QString &name,
00433 const QList<double> &world_x, const QList<double> &world_y,
00434 const QList<int> &pixel_x, const QList<int> &pixel_y,
00435 const QString &linecolor, const QString &text, const QString &font, int fontsize, int fontstyle );
00436 void showHistogramTool();
00437
00438 protected slots:
00439 void refresh_canvas_event( );
00440 void refresh_statistics_event( bool );
00441 void refresh_position_event( bool );
00442
00443 void translate_x( const QString &, const QString &, const QString & );
00444 void translate_y( const QString &, const QString &, const QString & );
00445 void resize_x( const QString &, const QString &, const QString & );
00446 void resize_y( const QString &, const QString &, const QString & );
00447 void updateCenterInfo( );
00448 void adjustCorners( double, double, double, double);
00449 void refresh_zrange_event(int,int);
00450
00451
00452 void revoke_region( );
00453
00454 void revoke_region(QtRegionState*);
00455
00456 void reload_statistics_event( );
00457
00458 void output(std::list<QtRegionState*>,RegionTextList&);
00459 void output(std::list<QtRegionState*>,ds9writer&);
00460
00461 protected:
00462
00463 static const int SEXAGPREC;
00464
00465 QtRegionDock *dock_;
00466 QtRegionState *mystate;
00467 HistogramTab* histogram;
00468
00469 bool statistics_update_needed;
00470 bool position_visible;
00471 bool position_update_needed;
00472
00473 int id_;
00474 bool statistics_visible;
00475
00476 int hold_signals;
00477 std::map<region::RegionChanges,bool> held_signals;
00478
00479 void fetch_details( region::RegionTypes &type, QList<int> &pixelx,
00480 QList<int> &pixely, QList<double> &worldx, QList<double> &worldy );
00481 virtual void fetch_region_details( region::RegionTypes &type,
00482 std::vector<std::pair<int,int> > &pixel_pts,
00483 std::vector<std::pair<double,double> > &world_pts ) const {
00484 type = region::NonRegion;
00485 pixel_pts.clear( );
00486 world_pts.clear( );
00487 DISPLAY_PURE_VIRTUAL(Region::fetch_region_details,);
00488 }
00489
00490 void signal_region_change( region::RegionChanges );
00491
00492
00493
00494 protected:
00495 void initHistogram();
00496 virtual std::list<SHARED_PTR<RegionInfo> > *generate_dds_statistics( );
00497
00498
00499
00500 virtual void generate_nonimage_statistics( DisplayData*, std::list<SHARED_PTR<RegionInfo> > * ) { }
00501
00502
00503
00504 virtual RegionInfo *newInfoObject( ImageInterface<Float> *, PrincipalAxesDD * ) {
00505 return 0;
00506 }
00507
00508 virtual ImageRegion *get_image_region( DisplayData* ) const
00509 DISPLAY_PURE_VIRTUAL(Region::get_image_region,0);
00510
00511 virtual const std::set<Region*> &get_selected_regions( );
00512 virtual ImageRegion_state get_image_selected_region( DisplayData* );
00513
00514 virtual std::list<SHARED_PTR<RegionInfo> > *generate_dds_centers( ) = 0;
00515
00516 static Int getAxisIndex( ImageInterface<Float> *image, std::string axtype );
00517
00518 inline double linear_average( double a, double b ) const {
00519 return (a + b) / 2.0;
00520 }
00521 RegionInfo::center_t *getLayerCenter( PrincipalAxesDD *padd, SHARED_PTR<ImageInterface<Float> > image, ImageRegion& imgReg);
00522 RegionInfo::stats_t *getLayerStats( PrincipalAxesDD *padd, ImageInterface<Float> *image, ImageRegion& imgReg );
00523
00524 region::Units current_xunits( ) const;
00525 region::Units current_yunits( ) const;
00526 region::Coord current_region_coordsys( ) const;
00527 MDirection::Types current_casa_coordsys( ) const;
00528
00529 virtual void drawRegion( bool ) = 0;
00530 virtual void drawText( );
00531
00532 virtual void setCenter(double &, double &, double &, double &) DISPLAY_PURE_VIRTUAL(Region::setCenter,);
00533
00534 virtual void drawCenter(double &x, double &y );
00535 virtual void drawCenter(double &x, double &y, double &deltx, double &delty);
00536
00537
00538 virtual bool within_drawing_area( );
00539
00540 region::LineStyle current_ls;
00541 typedef std::pair<region::LineStyle,int> ls_ele;
00542 std::list<ls_ele> ls_stack;
00543 WorldCanvas *wc_;
00544
00545 int last_z_index;
00546 bool selected_;
00547
00548 bool visible_;
00549
00550
00551
00552 bool complete;
00553
00554
00555
00556 bool mouse_in_region;
00557
00558
00559 static SHARED_PTR<Region> creating_region;
00560
00561 private slots:
00562
00563
00564 void update_histogram_event();
00565
00566 private:
00567
00568 void init( );
00569
00570 void updateHistogramRegion();
00571 void clearHistograms();
00572 void process_held_signals( );
00573 void clear_signal_cache( );
00574 bool z_index_within_range;
00575 void set_line_style(const ls_ele&);
00576 bool draw_center_;
00577 std::string name_;
00578 };
00579
00580
00581
00582 class PointMarkerState : public VOID {
00583 public:
00584 PointMarkerState( QtMouseToolNames::PointRegionSymbols t, int s ) : VOID("viewer.PointMarkerState"), type_(t), scale_(s) { }
00585 QtMouseToolNames::PointRegionSymbols type( ) const {
00586 return type_;
00587 }
00588 int scale( ) const {
00589 return scale_;
00590 }
00591 private:
00592 QtMouseToolNames::PointRegionSymbols type_;
00593 int scale_;
00594 };
00595 }
00596 }
00597
00598 #endif