source: trunk/geant4/visualization/management/src/G4ViewParameters.cc@ 772

Last change on this file since 772 was 772, checked in by garnier, 18 years ago

r781@wl-72126: garnier | 2008-04-17 11:28:25 +0200
mise à jour

  • Property svn:mime-type set to text/cpp
File size: 18.2 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.30 2008/03/10 16:23:42 lgarnier Exp $
28// GEANT4 tag $Name: $
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 fDrawingStyle (wireframe),
42 fAuxEdgeVisible (false),
43 fRepStyle (polyhedron),
44 fCulling (true),
45 fCullInvisible (true),
46 fDensityCulling (false),
47 fVisibleDensity (0.01 * g / cm3),
48 fCullCovered (false),
49 fSection (false),
50 fSectionPlane (),
51 fCutawayMode (cutawayUnion),
52 fCutawayPlanes (),
53 fExplodeFactor (1.),
54 fNoOfSides (24),
55 fViewpointDirection (G4Vector3D (0., 0., 1.)), // On z-axis.
56 fUpVector (G4Vector3D (0., 1., 0.)), // y-axis up.
57 fFieldHalfAngle (0.), // Orthogonal projection.
58 fZoomFactor (1.),
59 fScaleFactor (G4Vector3D (1., 1., 1.)),
60 fCurrentTargetPoint (),
61 fDolly (0.),
62 fLightsMoveWithCamera (false),
63 fRelativeLightpointDirection (G4Vector3D (1., 1., 1.)),
64 fActualLightpointDirection (G4Vector3D (1., 1., 1.)),
65 fDefaultVisAttributes (),
66 fDefaultTextVisAttributes (G4Colour (0., 0., 1.)),
67 fDefaultMarker (),
68 fGlobalMarkerScale (1.),
69 fGlobalLineWidthScale (1.),
70 fMarkerNotHidden (true),
71 fWindowSizeHintX (600),
72 fWindowSizeHintY (600),
73 fAutoRefresh (false),
74 fBackgroundColour (G4Colour(0.,0.,0.)), // Black
75 fPicking (false)
76{
77 fDefaultMarker.SetScreenSize (5.);
78 // Markers are 5 pixels "overall" size, i.e., diameter.
79}
80
81G4ViewParameters::~G4ViewParameters () {}
82
83void G4ViewParameters::MultiplyScaleFactor
84(const G4Vector3D& scaleFactorMultiplier) {
85 fScaleFactor.setX(fScaleFactor.x() * scaleFactorMultiplier.x());
86 fScaleFactor.setY(fScaleFactor.y() * scaleFactorMultiplier.y());
87 fScaleFactor.setZ(fScaleFactor.z() * scaleFactorMultiplier.z());
88}
89
90G4Vector3D& G4ViewParameters::GetActualLightpointDirection () {
91 SetViewAndLights (fViewpointDirection);
92 return fActualLightpointDirection;
93}
94
95// Useful quantities - begin snippet.
96// Here Follow functions to evaluate the above algorithms as a
97// function of the radius of the Bounding Sphere of the object being
98// viewed. Call them in the order given - for efficiency, later
99// functions depend on the results of earlier ones (Store the
100// results of earlier functions in your own temporary variables -
101// see, for example, G4OpenGLView::SetView ().)
102
103G4double G4ViewParameters::GetCameraDistance (G4double radius) const {
104 G4double cameraDistance;
105 if (fFieldHalfAngle == 0.) {
106 cameraDistance = radius;
107 }
108 else {
109 cameraDistance = radius / std::sin (fFieldHalfAngle) - fDolly;
110 }
111 return cameraDistance;
112}
113
114G4double G4ViewParameters::GetNearDistance (G4double cameraDistance,
115 G4double radius) const {
116 const G4double small = 1.e-6 * radius;
117 G4double nearDistance = cameraDistance - radius;
118 if (nearDistance < small) nearDistance = small;
119 return nearDistance;
120}
121
122G4double G4ViewParameters::GetFarDistance (G4double cameraDistance,
123 G4double nearDistance,
124 G4double radius) const {
125 G4double farDistance = cameraDistance + radius;
126 if (farDistance < nearDistance) farDistance = nearDistance;
127 return farDistance;
128}
129
130G4double G4ViewParameters::GetFrontHalfHeight (G4double nearDistance,
131 G4double radius) const {
132 G4double frontHalfHeight;
133 if (fFieldHalfAngle == 0.) {
134 frontHalfHeight = radius / fZoomFactor;
135 }
136 else {
137 frontHalfHeight = nearDistance * std::tan (fFieldHalfAngle) / fZoomFactor;
138 }
139 return frontHalfHeight;
140}
141// Useful quantities - end snippet.
142
143void G4ViewParameters::AddCutawayPlane (const G4Plane3D& cutawayPlane) {
144 if (fCutawayPlanes.size () < 3 ) {
145 fCutawayPlanes.push_back (cutawayPlane);
146 }
147 else {
148 G4cout <<
149 "ERROR: G4ViewParameters::AddCutawayPlane:"
150 "\n A maximum of 3 cutaway planes supported." << G4endl;
151 }
152}
153
154void G4ViewParameters::ChangeCutawayPlane
155(size_t index, const G4Plane3D& cutawayPlane) {
156 if (index >= fCutawayPlanes.size()) {
157 G4cout <<
158 "ERROR: G4ViewParameters::ChangeCutawayPlane:"
159 "\n Plane " << index << " does not exist." << G4endl;
160 } else {
161 fCutawayPlanes[index] = cutawayPlane;
162 }
163}
164
165void G4ViewParameters::SetVisibleDensity (G4double visibleDensity) {
166 const G4double reasonableMaximum = 10.0 * g / cm3;
167 if (visibleDensity < 0) {
168 G4cout << "G4ViewParameters::SetVisibleDensity: attempt to set negative "
169 "density - ignored." << G4endl;
170 }
171 else {
172 if (visibleDensity > reasonableMaximum) {
173 G4cout << "G4ViewParameters::SetVisibleDensity: density > "
174 << G4BestUnit (reasonableMaximum, "Volumic Mass")
175 << " - did you mean this?"
176 << G4endl;
177 }
178 fVisibleDensity = visibleDensity;
179 }
180}
181
182G4int G4ViewParameters::SetNoOfSides (G4int nSides) {
183 const G4int nSidesMin = 12;
184 if (nSides < nSidesMin) {
185 nSides = nSidesMin;
186 G4cout << "G4ViewParameters::SetNoOfSides: attempt to set the"
187 "\nnumber of sides per circle < " << nSidesMin
188 << "; forced to " << nSides << G4endl;
189 }
190 fNoOfSides = nSides;
191 return fNoOfSides;
192}
193
194void G4ViewParameters::SetViewAndLights
195(const G4Vector3D& viewpointDirection) {
196
197 fViewpointDirection = viewpointDirection;
198
199 // If the requested viewpoint direction is parallel to the up
200 // vector, the orientation of the view is undefined...
201 if (fViewpointDirection.unit() * fUpVector.unit() > .9999) {
202 G4cout <<
203 "WARNING: Viewpoint direction is very close to the up vector direction."
204 "\n Consider setting the up vector to obtain definable behaviour."
205 << G4endl;
206 }
207
208 // Move the lights too if requested...
209 if (fLightsMoveWithCamera) {
210 G4Vector3D zprime = fViewpointDirection.unit ();
211 G4Vector3D xprime = (fUpVector.cross (zprime)).unit ();
212 G4Vector3D yprime = zprime.cross (xprime);
213 fActualLightpointDirection =
214 fRelativeLightpointDirection.x () * xprime +
215 fRelativeLightpointDirection.y () * yprime +
216 fRelativeLightpointDirection.x () * zprime;
217 } else {
218 fActualLightpointDirection = fRelativeLightpointDirection;
219 }
220}
221
222void G4ViewParameters::SetLightpointDirection
223(const G4Vector3D& lightpointDirection) {
224 fRelativeLightpointDirection = lightpointDirection;
225 SetViewAndLights (fViewpointDirection);
226}
227
228void G4ViewParameters::SetPan (G4double right, G4double up) {
229 G4Vector3D unitRight = (fUpVector.cross (fViewpointDirection)).unit();
230 G4Vector3D unitUp = (fViewpointDirection.cross (unitRight)).unit();
231 fCurrentTargetPoint = right * unitRight + up * unitUp;
232}
233
234void G4ViewParameters::IncrementPan (G4double right, G4double up) {
235 IncrementPan (right,up, 0);
236}
237
238void G4ViewParameters::IncrementPan (G4double right, G4double up, G4double distance) {
239 G4Vector3D unitRight = (fUpVector.cross (fViewpointDirection)).unit();
240 G4Vector3D unitUp = (fViewpointDirection.cross (unitRight)).unit();
241 fCurrentTargetPoint += right * unitRight + up * unitUp + distance * fViewpointDirection;
242}
243
244void G4ViewParameters::PrintDifferences (const G4ViewParameters& v) const {
245
246 // Put performance-sensitive parameters first.
247 if (
248 // This first to optimise spin, etc.
249 (fViewpointDirection != v.fViewpointDirection) ||
250
251 // No particular order from here on.
252 (fDrawingStyle != v.fDrawingStyle) ||
253 (fAuxEdgeVisible != v.fAuxEdgeVisible) ||
254 (fRepStyle != v.fRepStyle) ||
255 (fCulling != v.fCulling) ||
256 (fCullInvisible != v.fCullInvisible) ||
257 (fDensityCulling != v.fDensityCulling) ||
258 (fVisibleDensity != v.fVisibleDensity) ||
259 (fCullCovered != v.fCullCovered) ||
260 (fSection != v.fSection) ||
261 (fNoOfSides != v.fNoOfSides) ||
262 (fUpVector != v.fUpVector) ||
263 (fFieldHalfAngle != v.fFieldHalfAngle) ||
264 (fZoomFactor != v.fZoomFactor) ||
265 (fScaleFactor != v.fScaleFactor) ||
266 (fCurrentTargetPoint != v.fCurrentTargetPoint) ||
267 (fDolly != v.fDolly) ||
268 (fRelativeLightpointDirection != v.fRelativeLightpointDirection) ||
269 (fLightsMoveWithCamera != v.fLightsMoveWithCamera) ||
270 (fDefaultVisAttributes != v.fDefaultVisAttributes) ||
271 (fDefaultTextVisAttributes != v.fDefaultTextVisAttributes) ||
272 (fDefaultMarker != v.fDefaultMarker) ||
273 (fGlobalMarkerScale != v.fGlobalMarkerScale) ||
274 (fGlobalLineWidthScale != v.fGlobalLineWidthScale) ||
275 (fMarkerNotHidden != v.fMarkerNotHidden) ||
276 (fWindowSizeHintX != v.fWindowSizeHintX) ||
277 (fWindowSizeHintY != v.fWindowSizeHintY) ||
278 (fXGeometryString != v.fXGeometryString) ||
279 (fAutoRefresh != v.fAutoRefresh) ||
280 (fBackgroundColour != v.fBackgroundColour) ||
281 (fPicking != v.fPicking)
282 )
283 G4cout << "Difference in 1st batch." << G4endl;
284
285 if (fSection) {
286 if (!(fSectionPlane == v.fSectionPlane))
287 G4cout << "Difference in section planes batch." << G4endl;
288 }
289
290 if (IsCutaway()) {
291 if (fCutawayPlanes.size () != v.fCutawayPlanes.size ()) {
292 G4cout << "Difference in no of cutaway planes." << G4endl;
293 }
294 else {
295 for (size_t i = 0; i < fCutawayPlanes.size (); i++) {
296 if (!(fCutawayPlanes[i] == v.fCutawayPlanes[i]))
297 G4cout << "Difference in cutaway plane no. " << i << G4endl;
298 }
299 }
300 }
301
302 if (IsExplode()) {
303 if (fExplodeFactor != v.fExplodeFactor)
304 G4cout << "Difference in explode factor." << G4endl;
305 if (fExplodeCentre != v.fExplodeCentre)
306 G4cout << "Difference in explode centre." << G4endl;
307 }
308}
309
310std::ostream& operator << (std::ostream& os,
311 const G4ViewParameters::DrawingStyle& style) {
312 switch (style) {
313 case G4ViewParameters::wireframe:
314 os << "wireframe"; break;
315 case G4ViewParameters::hlr:
316 os << "hlr - hidden lines removed"; break;
317 case G4ViewParameters::hsr:
318 os << "hsr - hidden surfaces removed"; break;
319 case G4ViewParameters::hlhsr:
320 os << "hlhsr - hidden line, hidden surface removed"; break;
321 default: os << "unrecognised"; break;
322 }
323 return os;
324}
325
326std::ostream& operator << (std::ostream& os, const G4ViewParameters& v) {
327 os << "View parameters and options:";
328
329 os << "\n Drawing style: " << v.fDrawingStyle;
330
331 os << "\n Auxiliary edges: ";
332 if (!v.fAuxEdgeVisible) os << "in";
333 os << "visible";
334
335 os << "\n Representation style: ";
336 switch (v.fRepStyle) {
337 case G4ViewParameters::polyhedron:
338 os << "polyhedron"; break;
339 case G4ViewParameters::nurbs:
340 os << "nurbs"; break;
341 default: os << "unrecognised"; break;
342 }
343
344 os << "\n Culling: ";
345 if (v.fCulling) os << "on";
346 else os << "off";
347
348 os << "\n Culling invisible objects: ";
349 if (v.fCullInvisible) os << "on";
350 else os << "off";
351
352 os << "\n Density culling: ";
353 if (v.fDensityCulling) {
354 os << "on - invisible if density less than "
355 << v.fVisibleDensity / (1. * g / cm3) << " g cm^-3";
356 }
357 else os << "off";
358
359 os << "\n Culling daughters covered by opaque mothers: ";
360 if (v.fCullCovered) os << "on";
361 else os << "off";
362
363 os << "\n Section flag: ";
364 if (v.fSection) os << "true, section/cut plane: " << v.fSectionPlane;
365 else os << "false";
366
367 if (v.IsCutaway()) {
368 os << "\n Cutaway planes: ";
369 for (size_t i = 0; i < v.fCutawayPlanes.size (); i++) {
370 os << ' ' << v.fCutawayPlanes[i];
371 }
372 }
373 else {
374 os << "\n No cutaway planes";
375 }
376
377 os << "\n Explode factor: " << v.fExplodeFactor
378 << " about centre: " << v.fExplodeCentre;
379
380 os << "\n No. of sides used in circle polygon approximation: "
381 << v.fNoOfSides;
382
383 os << "\n Viewpoint direction: " << v.fViewpointDirection;
384
385 os << "\n Up vector: " << v.fUpVector;
386
387 os << "\n Field half angle: " << v.fFieldHalfAngle;
388
389 os << "\n Zoom factor: " << v.fZoomFactor;
390
391 os << "\n Scale factor: " << v.fScaleFactor;
392
393 os << "\n Current target point: " << v.fCurrentTargetPoint;
394
395 os << "\n Dolly distance: " << v.fDolly;
396
397 os << "\n Light ";
398 if (v.fLightsMoveWithCamera) os << "moves";
399 else os << "does not move";
400 os << " with camera";
401
402 os << "\n Relative lightpoint direction: "
403 << v.fRelativeLightpointDirection;
404
405 os << "\n Actual lightpoint direction: "
406 << v.fActualLightpointDirection;
407
408 os << "\n Derived parameters for standard view of object of unit radius:";
409 G4ViewParameters tempVP = v;
410 tempVP.fDolly = 0.;
411 tempVP.fZoomFactor = 1.;
412 const G4double radius = 1.;
413 const G4double cameraDistance = tempVP.GetCameraDistance (radius);
414 const G4double nearDistance =
415 tempVP.GetNearDistance (cameraDistance, radius);
416 const G4double farDistance =
417 tempVP.GetFarDistance (cameraDistance, nearDistance, radius);
418 const G4double right = tempVP.GetFrontHalfHeight (nearDistance, radius);
419 os << "\n Camera distance: " << cameraDistance;
420 os << "\n Near distance: " << nearDistance;
421 os << "\n Far distance: " << farDistance;
422 os << "\n Front half height: " << right;
423
424 os << "\n Default VisAttributes:\n " << v.fDefaultVisAttributes;
425
426 os << "\n Default TextVisAttributes:\n " << v.fDefaultTextVisAttributes;
427
428 os << "\n Default marker: " << v.fDefaultMarker;
429
430 os << "\n Global marker scale: " << v.fGlobalMarkerScale;
431
432 os << "\n Global lineWidth scale: " << v.fGlobalLineWidthScale;
433
434 os << "\n Marker ";
435 if (v.fMarkerNotHidden) os << "not ";
436 os << "hidden by surfaces.";
437
438 os << "\n Window size hint: "
439 << v.fWindowSizeHintX << 'x'<< v.fWindowSizeHintX;
440
441 os << "\n X geometry string: " << v.fXGeometryString;
442
443 os << "\n Auto refresh: ";
444 if (v.fAutoRefresh) os << "true";
445 else os << "false";
446
447 os << "\n Background colour: " << v.fBackgroundColour;
448
449 os << "\n Picking requested: ";
450 if (v.fPicking) os << "true";
451 else os << "false";
452
453 return os;
454}
455
456G4bool G4ViewParameters::operator != (const G4ViewParameters& v) const {
457
458 // Put performance-sensitive parameters first.
459 if (
460 // This first to optimise spin, etc.
461 (fViewpointDirection != v.fViewpointDirection) ||
462
463 // No particular order from here on.
464 (fDrawingStyle != v.fDrawingStyle) ||
465 (fAuxEdgeVisible != v.fAuxEdgeVisible) ||
466 (fRepStyle != v.fRepStyle) ||
467 (fCulling != v.fCulling) ||
468 (fCullInvisible != v.fCullInvisible) ||
469 (fDensityCulling != v.fDensityCulling) ||
470 (fCullCovered != v.fCullCovered) ||
471 (fSection != v.fSection) ||
472 (IsCutaway() != v.IsCutaway()) ||
473 (IsExplode() != v.IsExplode()) ||
474 (fNoOfSides != v.fNoOfSides) ||
475 (fUpVector != v.fUpVector) ||
476 (fFieldHalfAngle != v.fFieldHalfAngle) ||
477 (fZoomFactor != v.fZoomFactor) ||
478 (fScaleFactor != v.fScaleFactor) ||
479 (fCurrentTargetPoint != v.fCurrentTargetPoint) ||
480 (fDolly != v.fDolly) ||
481 (fRelativeLightpointDirection != v.fRelativeLightpointDirection) ||
482 (fLightsMoveWithCamera != v.fLightsMoveWithCamera) ||
483 (fDefaultVisAttributes != v.fDefaultVisAttributes) ||
484 (fDefaultTextVisAttributes != v.fDefaultTextVisAttributes) ||
485 (fDefaultMarker != v.fDefaultMarker) ||
486 (fGlobalMarkerScale != v.fGlobalMarkerScale) ||
487 (fGlobalLineWidthScale != v.fGlobalLineWidthScale) ||
488 (fMarkerNotHidden != v.fMarkerNotHidden) ||
489 (fWindowSizeHintX != v.fWindowSizeHintX) ||
490 (fWindowSizeHintY != v.fWindowSizeHintY) ||
491 (fXGeometryString != v.fXGeometryString) ||
492 (fAutoRefresh != v.fAutoRefresh) ||
493 (fBackgroundColour != v.fBackgroundColour) ||
494 (fPicking != v.fPicking)
495 )
496 return true;
497
498 if (fDensityCulling &&
499 (fVisibleDensity != v.fVisibleDensity)) return true;
500
501 if (fSection &&
502 (!(fSectionPlane == v.fSectionPlane))) return true;
503
504 if (IsCutaway()) {
505 if (fCutawayPlanes.size () != v.fCutawayPlanes.size ())
506 return true;
507 else {
508 for (size_t i = 0; i < fCutawayPlanes.size (); i++) {
509 if (!(fCutawayPlanes[i] == v.fCutawayPlanes[i])) return true;
510 }
511 }
512 }
513
514 if (IsExplode() &&
515 (fExplodeFactor != v.fExplodeFactor) ||
516 (fExplodeCentre != v.fExplodeCentre)) return true;
517
518 return false;
519}
Note: See TracBrowser for help on using the repository browser.