Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // >> 27 // $Id: G4RTXScanner.cc,v 1.5 2007/05/22 17:10:42 allison Exp $ >> 28 // GEANT4 tag $Name: geant4-09-00 $ 27 // 29 // 28 // 30 // 29 31 >> 32 #ifdef G4VIS_BUILD_RAYTRACERX_DRIVER >> 33 30 #include "G4RTXScanner.hh" 34 #include "G4RTXScanner.hh" 31 35 32 #include "G4TheRayTracer.hh" 36 #include "G4TheRayTracer.hh" 33 #include "G4RayTracerXViewer.hh" 37 #include "G4RayTracerXViewer.hh" 34 #include "G4ViewParameters.hh" 38 #include "G4ViewParameters.hh" 35 #include <X11/Xlib.h> 39 #include <X11/Xlib.h> 36 #include <X11/Xutil.h> 40 #include <X11/Xutil.h> 37 #include <X11/Xatom.h> 41 #include <X11/Xatom.h> 38 42 39 #define G4warn G4cout << 40 << 41 extern "C" { 43 extern "C" { 42 Bool G4RayTracerXScannerWaitForNotify (Displ 44 Bool G4RayTracerXScannerWaitForNotify (Display*, XEvent* e, char* arg) { 43 return (e->type == MapNotify) && (e->xmap. 45 return (e->type == MapNotify) && (e->xmap.window == (Window) arg); 44 } 46 } 45 } 47 } 46 48 47 G4RTXScanner::G4RTXScanner(): 49 G4RTXScanner::G4RTXScanner(): 48 G4VRTScanner(), theNRow(0), theNColumn(0), t << 50 theNRow(0), theNColumn(0), theIRow(0), theIColumn(0) 49 ,theIRow(0), theIColumn(0) << 50 ,display(0), win(0), scmap(0) << 51 { 51 { 52 theGSName = "RayTracerX"; 52 theGSName = "RayTracerX"; 53 theGSNickname = "RayTracerX"; 53 theGSNickname = "RayTracerX"; 54 } 54 } 55 55 56 G4RTXScanner::~G4RTXScanner() {} << 57 << 58 const G4String& G4RTXScanner::GetGSName() cons 56 const G4String& G4RTXScanner::GetGSName() const 59 {return theGSName;} 57 {return theGSName;} 60 58 61 const G4String& G4RTXScanner::GetGSNickname() 59 const G4String& G4RTXScanner::GetGSNickname() const 62 {return theGSNickname;} 60 {return theGSNickname;} 63 61 64 void G4RTXScanner::Initialize(G4int nRow, G4in 62 void G4RTXScanner::Initialize(G4int nRow, G4int nColumn) { 65 theNRow = nRow; 63 theNRow = nRow; 66 theNColumn = nColumn; 64 theNColumn = nColumn; 67 G4int nMax = std::max (nRow, nColumn); 65 G4int nMax = std::max (nRow, nColumn); 68 theStep = 1; 66 theStep = 1; 69 if (nMax > 3) { 67 if (nMax > 3) { 70 for (;;) { 68 for (;;) { 71 theStep *= 3; 69 theStep *= 3; 72 if (theStep > nMax) break; 70 if (theStep > nMax) break; 73 } 71 } 74 } 72 } 75 theIRow = theStep / 2; 73 theIRow = theStep / 2; 76 theIColumn = theStep / 2 - theStep; 74 theIColumn = theStep / 2 - theStep; 77 } 75 } 78 76 79 G4bool G4RTXScanner::Coords(G4int& iRow, G4int 77 G4bool G4RTXScanner::Coords(G4int& iRow, G4int& iColumn) 80 { 78 { 81 // Increment column... 79 // Increment column... 82 theIColumn += theStep; 80 theIColumn += theStep; 83 81 84 // Skip coordinates covered in the previous 82 // Skip coordinates covered in the previous scan... 85 if ((theIColumn + (3 * theStep) / 2 + 1)%(3 83 if ((theIColumn + (3 * theStep) / 2 + 1)%(3 * theStep) == 0 && 86 (theIRow + (3 * theStep) / 2 + 1)%(3 * t 84 (theIRow + (3 * theStep) / 2 + 1)%(3 * theStep) == 0) 87 theIColumn += theStep; 85 theIColumn += theStep; 88 86 89 // If necessary, increment row... 87 // If necessary, increment row... 90 if (theIColumn >= theNColumn) { 88 if (theIColumn >= theNColumn) { 91 theIColumn = theStep / 2; 89 theIColumn = theStep / 2; 92 theIRow += theStep; 90 theIRow += theStep; 93 } 91 } 94 92 95 // Return if finished... 93 // Return if finished... 96 if (theIRow >= theNRow && theStep <= 1) retu 94 if (theIRow >= theNRow && theStep <= 1) return false; 97 95 98 // Start next scan if necessary... 96 // Start next scan if necessary... 99 if (theIRow >= theNRow) { 97 if (theIRow >= theNRow) { 100 theStep /= 3; 98 theStep /= 3; 101 theIRow = theStep / 2; 99 theIRow = theStep / 2; 102 theIColumn = theStep / 2; 100 theIColumn = theStep / 2; 103 } 101 } 104 102 105 // Return current row and column... 103 // Return current row and column... 106 iRow = theIRow; 104 iRow = theIRow; 107 iColumn = theIColumn; 105 iColumn = theIColumn; 108 return true; 106 return true; 109 } 107 } 110 108 111 G4bool G4RTXScanner::GetXWindow(const G4String 109 G4bool G4RTXScanner::GetXWindow(const G4String& name, G4ViewParameters& vp) 112 { 110 { 113 display = XOpenDisplay(0); // Use display d 111 display = XOpenDisplay(0); // Use display defined by DISPLAY environment. 114 if (!display) { 112 if (!display) { 115 G4warn << "G4RTXScanner::Initialize(): can << 113 G4cerr << "G4RTXScanner::Initialize(): cannot get display." 116 << G4endl; 114 << G4endl; 117 return false; 115 return false; 118 } 116 } 119 117 120 int screen_num = DefaultScreen(display); 118 int screen_num = DefaultScreen(display); 121 119 122 // Window size and position... 120 // Window size and position... 123 int xOffset = 0, yOffset = 0; 121 int xOffset = 0, yOffset = 0; 124 XSizeHints* size_hints = XAllocSizeHints(); << 125 unsigned int width, height; 122 unsigned int width, height; >> 123 XSizeHints* size_hints = XAllocSizeHints(); 126 const G4String& XGeometryString = vp.GetXGeo 124 const G4String& XGeometryString = vp.GetXGeometryString(); 127 if (!XGeometryString.empty()) { 125 if (!XGeometryString.empty()) { 128 G4int geometryResultMask = XParseGeometry 126 G4int geometryResultMask = XParseGeometry 129 ((char*)XGeometryString.c_str(), 127 ((char*)XGeometryString.c_str(), 130 &xOffset, &yOffset, &width, &height); 128 &xOffset, &yOffset, &width, &height); 131 if (geometryResultMask & (WidthValue | Hei 129 if (geometryResultMask & (WidthValue | HeightValue)) { 132 if (geometryResultMask & XValue) { 130 if (geometryResultMask & XValue) { 133 if (geometryResultMask & XNegative) { 131 if (geometryResultMask & XNegative) { 134 xOffset = DisplayWidth(display, screen_num 132 xOffset = DisplayWidth(display, screen_num) + xOffset - width; 135 } 133 } 136 size_hints->flags |= PPosition; 134 size_hints->flags |= PPosition; 137 size_hints->x = xOffset; 135 size_hints->x = xOffset; 138 } 136 } 139 if (geometryResultMask & YValue) { 137 if (geometryResultMask & YValue) { 140 if (geometryResultMask & YNegative) { 138 if (geometryResultMask & YNegative) { 141 yOffset = DisplayHeight(display, screen_nu 139 yOffset = DisplayHeight(display, screen_num) + yOffset - height; 142 } 140 } 143 size_hints->flags |= PPosition; 141 size_hints->flags |= PPosition; 144 size_hints->y = yOffset; 142 size_hints->y = yOffset; 145 } 143 } 146 } else { 144 } else { 147 G4warn << "ERROR: Geometry string \"" << 145 G4cout << "ERROR: Geometry string \"" 148 << XGeometryString 146 << XGeometryString 149 << "\" invalid. Using \"600x600\"." 147 << "\" invalid. Using \"600x600\"." 150 << G4endl; 148 << G4endl; 151 width = 600; 149 width = 600; 152 height = 600; 150 height = 600; 153 } 151 } 154 } else { << 155 G4warn << "ERROR: Geometry string \"" << 156 << XGeometryString << 157 << "\" is empty. Using \"600x600\"." << 158 << G4endl; << 159 width = 600; << 160 height = 600; << 161 } 152 } 162 size_hints->width = width; 153 size_hints->width = width; 163 size_hints->height = height; 154 size_hints->height = height; 164 size_hints->flags |= PSize; 155 size_hints->flags |= PSize; 165 156 166 win = XCreateSimpleWindow 157 win = XCreateSimpleWindow 167 (display, RootWindow(display, screen_num), 158 (display, RootWindow(display, screen_num), 168 xOffset, yOffset, width, height, 159 xOffset, yOffset, width, height, 169 0, // Bor 160 0, // Border width. 170 WhitePixel(display, screen_num), // Bor 161 WhitePixel(display, screen_num), // Border colour. 171 BlackPixel(display, screen_num)); // Bac 162 BlackPixel(display, screen_num)); // Background colour. 172 163 173 XGCValues values; 164 XGCValues values; 174 gc = XCreateGC(display, win, 0, &values); 165 gc = XCreateGC(display, win, 0, &values); 175 166 176 int nMaps; 167 int nMaps; 177 Status status = XGetRGBColormaps 168 Status status = XGetRGBColormaps 178 (display, RootWindow(display, screen_num), 169 (display, RootWindow(display, screen_num), 179 &scmap, &nMaps, XA_RGB_BEST_MAP); 170 &scmap, &nMaps, XA_RGB_BEST_MAP); 180 if (!status) { 171 if (!status) { 181 system("xstdcmap -best"); // ...and try a 172 system("xstdcmap -best"); // ...and try again... 182 status = XGetRGBColormaps << 173 Status status = XGetRGBColormaps 183 (display, RootWindow(display, screen_num 174 (display, RootWindow(display, screen_num), 184 &scmap, &nMaps, XA_RGB_BEST_MAP); 175 &scmap, &nMaps, XA_RGB_BEST_MAP); 185 if (!status) { 176 if (!status) { 186 G4warn << << 177 G4cerr << 187 "G4RTXScanner::Initialize(): cannot get colo 178 "G4RTXScanner::Initialize(): cannot get color map." 188 "\n Perhaps your system does not support XA << 179 "\n Perhaps your system does not support RGB_BEST_MAP." 189 << G4endl; 180 << G4endl; 190 return false; 181 return false; 191 } 182 } 192 } 183 } 193 if (!scmap->colormap) { 184 if (!scmap->colormap) { 194 G4warn << "G4RTXScanner::Initialize(): col << 185 G4cerr << "G4RTXScanner::Initialize(): color map empty." 195 << G4endl; 186 << G4endl; 196 return false; 187 return false; 197 } 188 } 198 189 199 XWMHints* wm_hints = XAllocWMHints(); 190 XWMHints* wm_hints = XAllocWMHints(); 200 XClassHint* class_hint = XAllocClassHint(); 191 XClassHint* class_hint = XAllocClassHint(); 201 const char* window_name = name.c_str(); 192 const char* window_name = name.c_str(); 202 XTextProperty windowName; 193 XTextProperty windowName; 203 XStringListToTextProperty((char**)&window_na 194 XStringListToTextProperty((char**)&window_name, 1, &windowName); 204 195 205 XSetWMProperties(display, win, &windowName, 196 XSetWMProperties(display, win, &windowName, &windowName, 206 0, 0, size_hints, wm_hints, class_hint) 197 0, 0, size_hints, wm_hints, class_hint); 207 198 208 XMapWindow(display, win); 199 XMapWindow(display, win); 209 200 210 // Wait for window to appear (wait for an "m 201 // Wait for window to appear (wait for an "map notify" event). 211 XSelectInput(display, win, StructureNotifyMa 202 XSelectInput(display, win, StructureNotifyMask); 212 XEvent event; 203 XEvent event; 213 XIfEvent (display, &event, G4RayTracerXScann 204 XIfEvent (display, &event, G4RayTracerXScannerWaitForNotify, (char*) win); 214 205 215 return true; 206 return true; 216 } 207 } 217 208 218 void G4RTXScanner::Draw 209 void G4RTXScanner::Draw 219 (unsigned char red, unsigned char green, unsig 210 (unsigned char red, unsigned char green, unsigned char blue) 220 // Draw coloured square at current position. 211 // Draw coloured square at current position. 221 { 212 { 222 unsigned long pixel_value = scmap->base_pixe 213 unsigned long pixel_value = scmap->base_pixel + 223 ((unsigned long) ((red * scmap->red_max) / 214 ((unsigned long) ((red * scmap->red_max) / 256.) * scmap->red_mult) + 224 ((unsigned long) ((green * scmap->green_ma 215 ((unsigned long) ((green * scmap->green_max) / 256.) * scmap->green_mult) + 225 ((unsigned long) ((blue * scmap->blue_max) 216 ((unsigned long) ((blue * scmap->blue_max) / 256.) * scmap->blue_mult); 226 XSetForeground(display, gc, pixel_value); 217 XSetForeground(display, gc, pixel_value); 227 218 228 if (theStep > 1) { 219 if (theStep > 1) { 229 XFillRectangle(display, win, gc, 220 XFillRectangle(display, win, gc, 230 theIColumn - theStep / 2, 221 theIColumn - theStep / 2, 231 theIRow - theStep / 2, 222 theIRow - theStep / 2, 232 theStep, theStep); 223 theStep, theStep); 233 } else { 224 } else { 234 XDrawPoint(display, win, gc, theIColumn, t 225 XDrawPoint(display, win, gc, theIColumn, theIRow); 235 } 226 } 236 227 237 XFlush(display); 228 XFlush(display); 238 } 229 } >> 230 >> 231 #endif 239 232