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