Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PerspectiveVisAction.cc
Go to the documentation of this file.
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 //
28 //
29 //
30 // $Id$
31 
32 #include "PerspectiveVisAction.hh"
33 
35 #include "G4VVisManager.hh"
36 #include "G4VisAttributes.hh"
37 #include "G4Box.hh"
38 #include "G4Polyline.hh"
39 #include "G4Polyhedron.hh"
40 #include "G4Vector3D.hh"
41 #include "G4Point3D.hh"
42 #include "G4SystemOfUnits.hh"
43 
45  fpVisManager(0),
46  fOptionString("none"),
47  fScene("room-and-chair"),
48  fRoomX(2.5*m), // Half-lengths...
49  fRoomY(2.5*m),
50  fRoomZ(1.3*m),
51  fWindowX(10*cm),
52  fWindowY(75*cm),
53  fWindowZ(50*cm),
54  fWindowSillHeight(80*cm),
55  fWindowOffset(-50*cm),
56  fDoorFrameX(10*cm),
57  fDoorFrameY(50*cm),
58  fDoorFrameZ(1*m),
59  fDoorFrameOffset(1.5*m),
60  fDoorX(2*cm),
61  fDoorY(50*cm),
62  fDoorZ(1*m),
63  fChairX(20*cm), // Half overall width.
64  fChairY(20*cm), // Half overall depth.
65  fChairZ(45*cm), // Half overall height.
66  fChairSeat(20*cm), // Half height of top of seat.
67  fChairThickness(3.*cm) // Half thicknes of back, seat, legs.
68 {
70 }
71 
73 {
74  fpVisManager = G4VVisManager::GetConcreteInstance();
75  if (fpVisManager) {
76 
77  // All scenes assume upvector z and origin on "floor"...
78 
79  if (fScene == "room-and-chair" )
80  {
81  RoomAndChair();
82  }
83  }
84 }
85 
86 void PerspectiveVisAction::RoomAndChair()
87 {
88  // Simple box, size of a room, translated so origin is on xy "floor"...
89  G4VisAttributes room_visAtts(G4Colour::Red());
90  room_visAtts.SetForceWireframe(true);
91  ExtendedDraw
92  (G4Box("box",fRoomX,fRoomY,fRoomZ), room_visAtts, G4TranslateZ3D(fRoomZ));
93 
94  // Windows...
95  G4Box ("window",fWindowX,fWindowY,fWindowZ);
96  ExtendedDraw
97  (G4Box("window-x",fWindowX,fWindowY,fWindowZ),
98  room_visAtts,
99  G4Translate3D(-fRoomX - fWindowX, fWindowOffset, fWindowY + fWindowSillHeight));
100  ExtendedDraw
101  (G4Box("window-y1",fWindowX,fWindowY,fWindowZ),
102  room_visAtts,
103  G4Translate3D(0., -fRoomY - fWindowX, fWindowY + fWindowSillHeight) *
104  G4RotateZ3D(90.*deg));
105  ExtendedDraw
106  (G4Box("window-y2",fWindowX,fWindowY,fWindowZ),
107  room_visAtts,
108  G4Translate3D(0., fRoomY + fWindowX, fWindowY + fWindowSillHeight) *
109  G4RotateZ3D(-90.*deg));
110 
111  // Door...
112  ExtendedDraw
113  (G4Box("door-frame",fDoorFrameX,fDoorFrameY,fDoorFrameZ),
114  room_visAtts,
115  G4Translate3D(-fRoomX - fDoorFrameX, fDoorFrameOffset, fDoorFrameZ));
116  ExtendedDraw
117  (G4Box("door",fDoorX,fDoorY,fDoorZ),
118  room_visAtts,
119  G4Translate3D(-fRoomX - fDoorX, fDoorFrameOffset, fDoorZ) *
120  G4TranslateY3D(fDoorY) *
121  G4RotateZ3D(60.*deg) *
122  G4TranslateY3D(-fDoorY)); // Last transform operates first.
123 
124  // Chair...
125  G4VisAttributes chair_visAtts(G4Colour::Cyan());
126  G4Transform3D A = G4RotateZ3D(90.*deg); // Turn through 90 deg.
127  G4Transform3D B = G4RotateY3D(90.*deg); // Lie down.
128  G4Transform3D C = G4RotateZ3D(-20.*deg) ; // Rotate a little.
129  G4Transform3D D = G4TranslateZ3D(fChairY); // Place on floor.
130  G4Transform3D E = G4TranslateY3D(-0.5 * fRoomY); // Move over to the left...
131  G4Transform3D chair_transform = E*D*C*B*A;
132  Chair(chair_visAtts, chair_transform);
133 }
134 
135 void PerspectiveVisAction::Chair
136 (const G4VisAttributes& visAtts,
137  const G4Transform3D& transform)
138 {
139  // Origin is on floor, z = 0, and in the xy centre...
140  ExtendedDraw
141  (G4Box("chair-back",fChairX, fChairThickness, fChairZ - fChairSeat),
142  visAtts, transform *
143  G4Translate3D(0.,-fChairY + fChairThickness, fChairZ + fChairSeat));
144  ExtendedDraw
145  (G4Box("chair-seat",fChairX, fChairY, fChairThickness),
146  visAtts, transform * G4TranslateZ3D(-fChairThickness + 2.* fChairSeat));
147  for (int i = -1; i < 2; i+=2) {
148  for (int j = -1; j < 2; j+=2) {
149  ExtendedDraw
150  (G4Box("chair-leg",fChairThickness,
151  fChairThickness,
152  fChairSeat - fChairThickness),
153  visAtts, transform *
154  G4Translate3D(i * (fChairX - fChairThickness),
155  j * (fChairY - fChairThickness),
156  fChairSeat - fChairThickness));
157  }
158  }
159 }
160 
161 void PerspectiveVisAction::ExtendedDraw
162 (const G4VSolid& solid,
163  const G4VisAttributes& visAtts,
164  const G4Transform3D& transform)
165 {
166  static const G4double extender = 100.*m;
167  static const G4Vector3D x(1,0,0);
168  static const G4Vector3D y(0,1,0);
169  static const G4Vector3D z(0,0,1);
170 
171  // Draw extended edges as requested...
172  G4bool any = false, A = false, X = false, Y = false, Z = false;
173  if (fOptionString.contains("a")) {A = true; any = true;}
174  if (fOptionString.contains("x")) {X = true; any = true;}
175  if (fOptionString.contains("y")) {Y = true; any = true;}
176  if (fOptionString.contains("z")) {Z = true; any = true;}
177  if (any)
178  {
179  G4Polyhedron* polyhedron = solid.GetPolyhedron();
180  G4bool isAuxEdgeVisible = false; // How do I pick this up??? Can't.
181  G4bool notLastFace;
182  do {
183  G4int n;
184  G4Point3D nodes[4];
185  notLastFace = polyhedron->GetNextFacet(n, nodes);
186  G4bool notLastEdge;
187  do {
188  G4Point3D v1, v2;
189  G4int edgeFlag;
190  notLastEdge = polyhedron->GetNextEdge(v1, v2, edgeFlag);
191  if (isAuxEdgeVisible || edgeFlag > 0) {
192  G4Vector3D v21 = v2 - v1;
193  // Check for components of actual edge...
194  G4Vector3D v21a = v21;
195  v21a.transform(transform);
196  // G4cout << "v21a: " << v21a << G4endl;
197  using namespace std;
198  if (A ||
199  (Z && abs(v21a.z()) >
200  sqrt(v21a.x()*v21a.x()+v21a.y()*v21a.y())) ||
201  (X && abs(v21a.x()) >
202  sqrt(v21a.y()*v21a.y()+v21a.z()*v21a.z())) ||
203  (Y && abs(v21a.y()) >
204  sqrt(v21a.x()*v21a.x()+v21a.x()*v21a.z()))) {
205  G4Polyline edge;
206  edge.SetVisAttributes(G4Colour(.2,.2,.2));
207  edge.push_back(v1 - extender * v21.unit());
208  edge.push_back(v2 + extender * v21.unit());
209  fpVisManager->Draw(edge, transform);
210  }
211  }
212  } while (notLastEdge);
213  } while (notLastFace);
214  }
215 
216  // Draw actual object...
217  fpVisManager->Draw(solid, visAtts, transform);
218 }