source: trunk/source/visualization/management/src/G4ViewParameters.cc @ 896

Last change on this file since 896 was 896, checked in by garnier, 16 years ago

Modif pour XGeometry complet. Avec debug

  • Property svn:mime-type set to text/cpp
File size: 23.4 KB
Line 
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: G4ViewParameters.cc,v 1.31 2008/04/04 13:48:53 allison Exp $
28// GEANT4 tag $Name: HEAD $
29//
30//
31// John Allison  19th July 1996
32// View parameters and options.
33
34#include "G4ViewParameters.hh"
35
36#include "G4VisManager.hh"
37#include "G4UnitsTable.hh"
38#include "G4ios.hh"
39
40G4ViewParameters::G4ViewParameters ():
41  fNoValue(0x0000),
42  fXValue(0x0001),
43  fYValue(0x0002),
44  fWidthValue(0x0004),
45  fHeightValue(0x0008),
46  fAllValues(0x000F),
47  fXNegative(0x0010),
48  fYNegative(0x0020),
49  fDrawingStyle (wireframe),
50  fAuxEdgeVisible (false),
51  fRepStyle (polyhedron),
52  fCulling (true),
53  fCullInvisible (true),
54  fDensityCulling (false),
55  fVisibleDensity (0.01 * g / cm3),
56  fCullCovered (false),
57  fSection (false),
58  fSectionPlane (),
59  fCutawayMode (cutawayUnion),
60  fCutawayPlanes (),
61  fExplodeFactor (1.),
62  fNoOfSides (24),
63  fViewpointDirection (G4Vector3D (0., 0., 1.)),  // On z-axis.
64  fUpVector (G4Vector3D (0., 1., 0.)),            // y-axis up.
65  fFieldHalfAngle (0.),                           // Orthogonal projection.
66  fZoomFactor (1.),
67  fScaleFactor (G4Vector3D (1., 1., 1.)),
68  fCurrentTargetPoint (),
69  fDolly (0.),
70  fLightsMoveWithCamera (false),
71  fRelativeLightpointDirection (G4Vector3D (1., 1., 1.)),
72  fActualLightpointDirection (G4Vector3D (1., 1., 1.)),
73  fDefaultVisAttributes (),
74  fDefaultTextVisAttributes (G4Colour (0., 0., 1.)),
75  fDefaultMarker (),
76  fGlobalMarkerScale (1.),
77  fGlobalLineWidthScale (1.),
78  fMarkerNotHidden (true),
79  fWindowSizeHintX (600),
80  fWindowSizeHintY (600),
81  fWindowLocationHintX(0),
82  fWindowLocationHintY(0),
83  fWindowLocationHintXNegative(true),
84  fWindowLocationHintYNegative(false),
85  fAutoRefresh (false),
86  fBackgroundColour (G4Colour(0.,0.,0.)),         // Black
87  fPicking (false)
88{
89#ifdef G4DEBUG
90  printf("G4ViewParameters::Create \n");
91#endif
92  fDefaultMarker.SetScreenSize (5.);
93  // Markers are 5 pixels "overall" size, i.e., diameter.
94}
95
96G4ViewParameters::~G4ViewParameters () {}
97
98void G4ViewParameters::MultiplyScaleFactor
99(const G4Vector3D& scaleFactorMultiplier) {
100  fScaleFactor.setX(fScaleFactor.x() * scaleFactorMultiplier.x());
101  fScaleFactor.setY(fScaleFactor.y() * scaleFactorMultiplier.y());
102  fScaleFactor.setZ(fScaleFactor.z() * scaleFactorMultiplier.z());
103}
104
105G4Vector3D& G4ViewParameters::GetActualLightpointDirection () {
106  SetViewAndLights (fViewpointDirection);
107  return fActualLightpointDirection;
108}
109
110// Useful quantities - begin snippet.
111// Here Follow functions to evaluate the above algorithms as a
112// function of the radius of the Bounding Sphere of the object being
113// viewed.  Call them in the order given - for efficiency, later
114// functions depend on the results of earlier ones (Store the
115// results of earlier functions in your own temporary variables -
116// see, for example, G4OpenGLView::SetView ().)
117
118G4double G4ViewParameters::GetCameraDistance (G4double radius) const {
119  G4double cameraDistance;
120  if (fFieldHalfAngle == 0.) {
121    cameraDistance = radius;
122  }
123  else {
124    cameraDistance = radius / std::sin (fFieldHalfAngle) - fDolly;
125  }
126  return cameraDistance;
127}
128
129G4double G4ViewParameters::GetNearDistance (G4double cameraDistance,
130                                            G4double radius) const {
131  const G4double small = 1.e-6 * radius;
132  G4double nearDistance = cameraDistance - radius;
133  if (nearDistance < small) nearDistance = small;
134  return nearDistance;
135}
136
137G4double G4ViewParameters::GetFarDistance (G4double cameraDistance,
138                                           G4double nearDistance,
139                                           G4double radius) const {
140  G4double farDistance = cameraDistance + radius;
141  if (farDistance < nearDistance) farDistance = nearDistance;
142  return farDistance;
143}
144
145G4double G4ViewParameters::GetFrontHalfHeight (G4double nearDistance,
146                                               G4double radius) const {
147  G4double frontHalfHeight;
148  if (fFieldHalfAngle == 0.) {
149    frontHalfHeight = radius / fZoomFactor;
150  }
151  else {
152    frontHalfHeight = nearDistance * std::tan (fFieldHalfAngle) / fZoomFactor;
153  }
154  return frontHalfHeight;
155}
156// Useful quantities - end snippet.
157
158void G4ViewParameters::AddCutawayPlane (const G4Plane3D& cutawayPlane) {
159  if (fCutawayPlanes.size () < 3 ) {
160    fCutawayPlanes.push_back (cutawayPlane);
161  }
162  else {
163    G4cout <<
164      "ERROR: G4ViewParameters::AddCutawayPlane:"
165      "\n  A maximum of 3 cutaway planes supported." << G4endl;
166  }
167}
168
169void G4ViewParameters::ChangeCutawayPlane
170(size_t index, const G4Plane3D& cutawayPlane) {
171  if (index >= fCutawayPlanes.size()) {
172    G4cout <<
173      "ERROR: G4ViewParameters::ChangeCutawayPlane:"
174      "\n  Plane " << index << " does not exist." << G4endl;
175  } else {
176    fCutawayPlanes[index] = cutawayPlane;
177  }
178}
179
180void G4ViewParameters::SetVisibleDensity (G4double visibleDensity) {
181  const G4double reasonableMaximum = 10.0 * g / cm3;
182  if (visibleDensity < 0) {
183    G4cout << "G4ViewParameters::SetVisibleDensity: attempt to set negative "
184      "density - ignored." << G4endl;
185  }
186  else {
187    if (visibleDensity > reasonableMaximum) {
188      G4cout << "G4ViewParameters::SetVisibleDensity: density > "
189             << G4BestUnit (reasonableMaximum, "Volumic Mass")
190             << " - did you mean this?"
191             << G4endl;
192    }
193    fVisibleDensity = visibleDensity;
194  }
195}
196
197G4int G4ViewParameters::SetNoOfSides (G4int nSides) {
198  const G4int  nSidesMin = 12;
199  if (nSides < nSidesMin) {
200    nSides = nSidesMin;
201    G4cout << "G4ViewParameters::SetNoOfSides: attempt to set the"
202      "\nnumber of sides per circle < " << nSidesMin
203         << "; forced to " << nSides << G4endl;
204  }
205  fNoOfSides = nSides;
206  return fNoOfSides;
207}
208
209void G4ViewParameters::SetViewAndLights
210(const G4Vector3D& viewpointDirection) {
211
212  fViewpointDirection = viewpointDirection;
213
214  // If the requested viewpoint direction is parallel to the up
215  // vector, the orientation of the view is undefined...
216  if (fViewpointDirection.unit() * fUpVector.unit() > .9999) {
217    G4cout <<
218      "WARNING: Viewpoint direction is very close to the up vector direction."
219      "\n  Consider setting the up vector to obtain definable behaviour."
220           << G4endl;
221  }
222
223  // Move the lights too if requested...
224  if (fLightsMoveWithCamera) {
225    G4Vector3D zprime = fViewpointDirection.unit ();
226    G4Vector3D xprime = (fUpVector.cross (zprime)).unit ();
227    G4Vector3D yprime = zprime.cross (xprime);
228    fActualLightpointDirection =
229      fRelativeLightpointDirection.x () * xprime +
230      fRelativeLightpointDirection.y () * yprime +
231      fRelativeLightpointDirection.x () * zprime;     
232  } else {
233    fActualLightpointDirection = fRelativeLightpointDirection;
234  }
235}
236
237void G4ViewParameters::SetLightpointDirection
238(const G4Vector3D& lightpointDirection) {
239  fRelativeLightpointDirection = lightpointDirection;
240  SetViewAndLights (fViewpointDirection);
241}
242
243void G4ViewParameters::SetPan (G4double right, G4double up) {
244  G4Vector3D unitRight = (fUpVector.cross (fViewpointDirection)).unit();
245  G4Vector3D unitUp    = (fViewpointDirection.cross (unitRight)).unit();
246  fCurrentTargetPoint  = right * unitRight + up * unitUp;
247}
248
249void G4ViewParameters::IncrementPan (G4double right, G4double up) {
250  IncrementPan (right,up, 0);
251}
252
253void G4ViewParameters::IncrementPan (G4double right, G4double up, G4double distance) {
254  G4Vector3D unitRight = (fUpVector.cross (fViewpointDirection)).unit();
255  G4Vector3D unitUp    = (fViewpointDirection.cross (unitRight)).unit();
256  fCurrentTargetPoint += right * unitRight + up * unitUp + distance * fViewpointDirection;
257}
258
259void G4ViewParameters::PrintDifferences (const G4ViewParameters& v) const {
260
261  // Put performance-sensitive parameters first.
262  if (
263      // This first to optimise spin, etc.
264      (fViewpointDirection   != v.fViewpointDirection)   ||
265
266      // No particular order from here on.
267      (fDrawingStyle         != v.fDrawingStyle)         ||
268      (fAuxEdgeVisible       != v.fAuxEdgeVisible)       ||
269      (fRepStyle             != v.fRepStyle)             ||
270      (fCulling              != v.fCulling)              ||
271      (fCullInvisible        != v.fCullInvisible)        ||
272      (fDensityCulling       != v.fDensityCulling)       ||
273      (fVisibleDensity       != v.fVisibleDensity)       ||
274      (fCullCovered          != v.fCullCovered)          ||
275      (fSection              != v.fSection)              ||
276      (fNoOfSides            != v.fNoOfSides)            ||
277      (fUpVector             != v.fUpVector)             ||
278      (fFieldHalfAngle       != v.fFieldHalfAngle)       ||
279      (fZoomFactor           != v.fZoomFactor)           ||
280      (fScaleFactor          != v.fScaleFactor)          ||
281      (fCurrentTargetPoint   != v.fCurrentTargetPoint)   ||
282      (fDolly                != v.fDolly)                ||
283      (fRelativeLightpointDirection != v.fRelativeLightpointDirection)  ||
284      (fLightsMoveWithCamera != v.fLightsMoveWithCamera) ||
285      (fDefaultVisAttributes != v.fDefaultVisAttributes) ||
286      (fDefaultTextVisAttributes != v.fDefaultTextVisAttributes) ||
287      (fDefaultMarker        != v.fDefaultMarker)        ||
288      (fGlobalMarkerScale    != v.fGlobalMarkerScale)    ||
289      (fGlobalLineWidthScale != v.fGlobalLineWidthScale) ||
290      (fMarkerNotHidden      != v.fMarkerNotHidden)      ||
291      (fWindowSizeHintX      != v.fWindowSizeHintX)      ||
292      (fWindowSizeHintY      != v.fWindowSizeHintY)      ||
293      (fXGeometryString      != v.fXGeometryString)      ||
294      (fAutoRefresh          != v.fAutoRefresh)          ||
295      (fBackgroundColour     != v.fBackgroundColour)     ||
296      (fPicking              != v.fPicking)
297      )
298    G4cout << "Difference in 1st batch." << G4endl;
299
300  if (fSection) {
301    if (!(fSectionPlane == v.fSectionPlane))
302      G4cout << "Difference in section planes batch." << G4endl;
303  }
304
305  if (IsCutaway()) {
306    if (fCutawayPlanes.size () != v.fCutawayPlanes.size ()) {
307      G4cout << "Difference in no of cutaway planes." << G4endl;
308    }
309    else {
310      for (size_t i = 0; i < fCutawayPlanes.size (); i++) {
311        if (!(fCutawayPlanes[i] == v.fCutawayPlanes[i]))
312          G4cout << "Difference in cutaway plane no. " << i << G4endl;
313      }
314    }
315  }
316
317  if (IsExplode()) {
318    if (fExplodeFactor != v.fExplodeFactor)
319      G4cout << "Difference in explode factor." << G4endl;
320    if (fExplodeCentre != v.fExplodeCentre)
321      G4cout << "Difference in explode centre." << G4endl;
322  }
323}
324
325std::ostream& operator << (std::ostream& os,
326                             const G4ViewParameters::DrawingStyle& style) {
327  switch (style) {
328  case G4ViewParameters::wireframe:
329    os << "wireframe"; break;
330  case G4ViewParameters::hlr:
331    os << "hlr - hidden lines removed"; break;
332  case G4ViewParameters::hsr:
333    os << "hsr - hidden surfaces removed"; break;
334  case G4ViewParameters::hlhsr:
335    os << "hlhsr - hidden line, hidden surface removed"; break;
336  default: os << "unrecognised"; break;
337  }
338  return os;
339}
340
341std::ostream& operator << (std::ostream& os, const G4ViewParameters& v) {
342  os << "View parameters and options:";
343
344  os << "\n  Drawing style: " << v.fDrawingStyle;
345
346  os << "\n  Auxiliary edges: ";
347  if (!v.fAuxEdgeVisible) os << "in";
348  os << "visible";
349
350  os << "\n  Representation style: ";
351  switch (v.fRepStyle) {
352  case G4ViewParameters::polyhedron:
353    os << "polyhedron"; break;
354  case G4ViewParameters::nurbs:
355    os << "nurbs"; break;
356  default: os << "unrecognised"; break;
357  }
358
359  os << "\n  Culling: ";
360  if (v.fCulling) os << "on";
361  else            os << "off";
362
363  os << "\n  Culling invisible objects: ";
364  if (v.fCullInvisible) os << "on";
365  else                  os << "off";
366
367  os << "\n  Density culling: ";
368  if (v.fDensityCulling) {
369    os << "on - invisible if density less than "
370       << v.fVisibleDensity / (1. * g / cm3) << " g cm^-3";
371  }
372  else os << "off";
373
374  os << "\n  Culling daughters covered by opaque mothers: ";
375  if (v.fCullCovered) os << "on";
376  else                os << "off";
377
378  os << "\n  Section flag: ";
379  if (v.fSection) os << "true, section/cut plane: " << v.fSectionPlane;
380  else            os << "false";
381
382  if (v.IsCutaway()) {
383    os << "\n  Cutaway planes: ";
384    for (size_t i = 0; i < v.fCutawayPlanes.size (); i++) {
385      os << ' ' << v.fCutawayPlanes[i];
386    }
387  }
388  else {
389    os << "\n  No cutaway planes";
390  }
391
392  os << "\n  Explode factor: " << v.fExplodeFactor
393     << " about centre: " << v.fExplodeCentre;
394
395  os << "\n  No. of sides used in circle polygon approximation: "
396     << v.fNoOfSides;
397
398  os << "\n  Viewpoint direction:  " << v.fViewpointDirection;
399
400  os << "\n  Up vector:            " << v.fUpVector;
401
402  os << "\n  Field half angle:     " << v.fFieldHalfAngle;
403
404  os << "\n  Zoom factor:          " << v.fZoomFactor;
405
406  os << "\n  Scale factor:         " << v.fScaleFactor;
407
408  os << "\n  Current target point: " << v.fCurrentTargetPoint;
409
410  os << "\n  Dolly distance:       " << v.fDolly;
411
412  os << "\n  Light ";
413  if (v.fLightsMoveWithCamera) os << "moves";
414  else                         os << "does not move";
415  os << " with camera";
416
417  os << "\n  Relative lightpoint direction: "
418     << v.fRelativeLightpointDirection;
419
420  os << "\n  Actual lightpoint direction: "
421     << v.fActualLightpointDirection;
422
423  os << "\n  Derived parameters for standard view of object of unit radius:";
424  G4ViewParameters tempVP = v;
425  tempVP.fDolly = 0.;
426  tempVP.fZoomFactor = 1.;
427  const G4double radius = 1.;
428  const G4double cameraDistance = tempVP.GetCameraDistance (radius);
429  const G4double nearDistance =
430    tempVP.GetNearDistance (cameraDistance, radius);
431  const G4double farDistance =
432    tempVP.GetFarDistance  (cameraDistance, nearDistance, radius);
433  const G4double right  = tempVP.GetFrontHalfHeight (nearDistance, radius);
434  os << "\n    Camera distance:   " << cameraDistance;
435  os << "\n    Near distance:     " << nearDistance;
436  os << "\n    Far distance:      " << farDistance;
437  os << "\n    Front half height: " << right;
438
439  os << "\n  Default VisAttributes:\n  " << v.fDefaultVisAttributes;
440
441  os << "\n  Default TextVisAttributes:\n  " << v.fDefaultTextVisAttributes;
442
443  os << "\n  Default marker: " << v.fDefaultMarker;
444
445  os << "\n  Global marker scale: " << v.fGlobalMarkerScale;
446
447  os << "\n  Global lineWidth scale: " << v.fGlobalLineWidthScale;
448
449  os << "\n  Marker ";
450  if (v.fMarkerNotHidden) os << "not ";
451  os << "hidden by surfaces.";
452
453  os << "\n  Window size hint: "
454     << v.fWindowSizeHintX << 'x'<< v.fWindowSizeHintX;
455
456  os << "\n  X geometry string: " << v.fXGeometryString;
457
458  os << "\n  Auto refresh: ";
459  if (v.fAutoRefresh) os << "true";
460  else os << "false";
461
462  os << "\n  Background colour: " << v.fBackgroundColour;
463
464  os << "\n  Picking requested: ";
465  if (v.fPicking) os << "true";
466  else os << "false";
467
468  return os;
469}
470
471G4bool G4ViewParameters::operator != (const G4ViewParameters& v) const {
472
473  // Put performance-sensitive parameters first.
474  if (
475      // This first to optimise spin, etc.
476      (fViewpointDirection   != v.fViewpointDirection)   ||
477
478      // No particular order from here on.
479      (fDrawingStyle         != v.fDrawingStyle)         ||
480      (fAuxEdgeVisible       != v.fAuxEdgeVisible)       ||
481      (fRepStyle             != v.fRepStyle)             ||
482      (fCulling              != v.fCulling)              ||
483      (fCullInvisible        != v.fCullInvisible)        ||
484      (fDensityCulling       != v.fDensityCulling)       ||
485      (fCullCovered          != v.fCullCovered)          ||
486      (fSection              != v.fSection)              ||
487      (IsCutaway()           != v.IsCutaway())           ||
488      (IsExplode()           != v.IsExplode())           ||
489      (fNoOfSides            != v.fNoOfSides)            ||
490      (fUpVector             != v.fUpVector)             ||
491      (fFieldHalfAngle       != v.fFieldHalfAngle)       ||
492      (fZoomFactor           != v.fZoomFactor)           ||
493      (fScaleFactor          != v.fScaleFactor)          ||
494      (fCurrentTargetPoint   != v.fCurrentTargetPoint)   ||
495      (fDolly                != v.fDolly)                ||
496      (fRelativeLightpointDirection != v.fRelativeLightpointDirection)  ||
497      (fLightsMoveWithCamera != v.fLightsMoveWithCamera) ||
498      (fDefaultVisAttributes != v.fDefaultVisAttributes) ||
499      (fDefaultTextVisAttributes != v.fDefaultTextVisAttributes) ||
500      (fDefaultMarker        != v.fDefaultMarker)        ||
501      (fGlobalMarkerScale    != v.fGlobalMarkerScale)    ||
502      (fGlobalLineWidthScale != v.fGlobalLineWidthScale) ||
503      (fMarkerNotHidden      != v.fMarkerNotHidden)      ||
504      (fWindowSizeHintX      != v.fWindowSizeHintX)      ||
505      (fWindowSizeHintY      != v.fWindowSizeHintY)      ||
506      (fXGeometryString      != v.fXGeometryString)      ||
507      (fAutoRefresh          != v.fAutoRefresh)          ||
508      (fBackgroundColour     != v.fBackgroundColour)     ||
509      (fPicking              != v.fPicking)
510      )
511    return true;
512
513  if (fDensityCulling &&
514      (fVisibleDensity != v.fVisibleDensity)) return true;
515
516  if (fSection &&
517      (!(fSectionPlane == v.fSectionPlane))) return true;
518
519  if (IsCutaway()) {
520    if (fCutawayPlanes.size () != v.fCutawayPlanes.size ())
521      return true;
522    else {
523      for (size_t i = 0; i < fCutawayPlanes.size (); i++) {
524        if (!(fCutawayPlanes[i] == v.fCutawayPlanes[i])) return true;
525      }
526    }
527  }
528
529  if (IsExplode() &&
530      ((fExplodeFactor != v.fExplodeFactor) ||
531       (fExplodeCentre != v.fExplodeCentre))) return true;
532
533  return false;
534}
535
536
537void G4ViewParameters::SetXGeometryString (const G4String& geomString) {
538
539
540  G4int x,y = 0;
541  unsigned int w,h = 0;
542  G4int m = ParseGeometry( geomString, &x, &y, &w, &h );
543
544  // Check errors
545  if ( ((m & fYValue) == 0) ||
546       ((m & fXValue) == 0) ||
547       ((m & fHeightValue) == 0 ) ||
548       ((m & fWidthValue)  == 0 )) {
549    G4cout << "ERROR: Unrecognised geometry string \""
550           << geomString
551           << "\".  Using default"
552           << G4endl;
553  } else {
554    // Set the string
555    fXGeometryString = geomString;
556
557    // Set values
558    fWindowSizeHintX = w;
559    fWindowSizeHintY = h;
560    fWindowLocationHintX = x;
561    fWindowLocationHintY = y;
562    if ( (m & fXNegative) ) {
563      fWindowLocationHintXNegative = true;
564    } else {
565      fWindowLocationHintXNegative = false;
566    }
567    if ( (m & fYNegative) ) {
568      fWindowLocationHintYNegative = true;
569    } else {
570      fWindowLocationHintYNegative = false;
571    }
572
573  }
574
575}
576
577G4int G4ViewParameters::GetWindowAbsoluteLocationHintX (G4int sizeX ) const {
578#ifdef G4DEBUG
579  printf("G4ViewParameters::GetWindowLocationHintX () :: %d\n",fWindowLocationHintX);
580#endif
581  if ( fWindowLocationHintXNegative ) {
582    return  sizeX  + fWindowLocationHintX - fWindowSizeHintX;
583  }
584  return fWindowLocationHintX;
585}
586
587G4int G4ViewParameters::GetWindowAbsoluteLocationHintY (G4int sizeY ) const {
588#ifdef G4DEBUG
589  printf("G4ViewParameters::GetWindowLocationHintY () :: %d\n",fWindowLocationHintY);
590#endif
591  if (  fWindowLocationHintYNegative ) {
592    return  sizeY  + fWindowLocationHintY - fWindowSizeHintY;
593  }
594  return fWindowLocationHintY;
595}
596
597/* Keep from :
598 * ftp://ftp.trolltech.com/qt/source/qt-embedded-free-3.0.6.tar.gz/qt-embedded-free-3.0.6/src/kernel/qapplication_qws.cpp
599 *
600 *    ParseGeometry parses strings of the form
601 *   "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
602 *   width, height, xoffset, and yoffset are unsigned integers.
603 *   Example:  "=80x24+300-49"
604 *   The equal sign is optional.
605 *   It returns a bitmask that indicates which of the four values
606 *   were actually found in the string. For each value found,
607 *   the corresponding argument is updated;  for each value
608 *   not found, the corresponding argument is left unchanged.
609 */
610
611int G4ViewParameters::ParseGeometry (
612 const char *string,
613 G4int *x,
614 G4int *y,
615 unsigned int *width,
616 unsigned int *height)
617{
618
619  G4int mask = fNoValue;
620  register char *strind;
621  unsigned int tempWidth, tempHeight;
622  G4int tempX, tempY;
623  char *nextCharacter;
624  if ( (string == NULL) || (*string == '\0')) {
625    return(mask);
626  }
627  if (*string == '=')
628    string++;  /* ignore possible '=' at beg of geometry spec */
629  strind = (char *)string;
630  if (*strind != '+' && *strind != '-' && *strind != 'x') {
631    tempWidth = ReadInteger(strind, &nextCharacter);
632    if (strind == nextCharacter)
633      return (0);
634    strind = nextCharacter;
635    mask |= fWidthValue;
636  }
637  if (*strind == 'x' || *strind == 'X') {
638    strind++;
639    tempHeight = ReadInteger(strind, &nextCharacter);
640    if (strind == nextCharacter)
641      return (0);
642    strind = nextCharacter;
643    mask |= fHeightValue;
644  }
645
646  if ((*strind == '+') || (*strind == '-')) {
647    if (*strind == '-') {
648      strind++;
649      tempX = -ReadInteger(strind, &nextCharacter);
650      if (strind == nextCharacter)
651        return (0);
652      strind = nextCharacter;
653      mask |= fXNegative;
654
655    }
656    else
657      { strind++;
658        tempX = ReadInteger(strind, &nextCharacter);
659        if (strind == nextCharacter)
660          return(0);
661        strind = nextCharacter;
662      }
663    mask |= fXValue;
664    if ((*strind == '+') || (*strind == '-')) {
665      if (*strind == '-') {
666        strind++;
667        tempY = -ReadInteger(strind, &nextCharacter);
668        if (strind == nextCharacter)
669          return(0);
670        strind = nextCharacter;
671        mask |= fYNegative;
672      }
673      else
674        {
675          strind++;
676          tempY = ReadInteger(strind, &nextCharacter);
677          if (strind == nextCharacter)
678            return(0);
679          strind = nextCharacter;
680        }
681      mask |= fYValue;
682    }
683  }
684  /* If strind isn't at the end of the string the it's an invalid
685     geometry specification. */
686  if (*strind != '\0') return (0);
687  if (mask & fXValue)
688    *x = tempX;
689  if (mask & fYValue)
690    *y = tempY;
691  if (mask & fWidthValue)
692    *width = tempWidth;
693  if (mask & fHeightValue)
694    *height = tempHeight;
695  return (mask);
696}
697
698/* Keep from :
699 * ftp://ftp.trolltech.com/qt/source/qt-embedded-free-3.0.6.tar.gz/qt-embedded-free-3.0.6/src/kernel/qapplication_qws.cpp
700 *
701 */
702G4int G4ViewParameters::ReadInteger(char *string, char **NextString)
703{
704    register G4int Result = 0;
705    G4int Sign = 1;
706
707    if (*string == '+')
708        string++;
709    else if (*string == '-')
710    {
711        string++;
712        Sign = -1;
713    }
714    for (; (*string >= '0') && (*string <= '9'); string++)
715    {
716        Result = (Result * 10) + (*string - '0');
717    }
718    *NextString = string;
719    if (Sign >= 0)
720        return (Result);
721    else
722        return (-Result);
723}
724   
Note: See TracBrowser for help on using the repository browser.