[831] | 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: G4DrawVoxels.cc,v 1.4 2006/06/29 18:36:34 gunter Exp $ |
---|
[1058] | 28 | // GEANT4 tag $Name: geant4-09-03-beta-cand-01 $ |
---|
[831] | 29 | // |
---|
| 30 | // |
---|
| 31 | // class G4DrawVoxels |
---|
| 32 | // |
---|
| 33 | // Implementation |
---|
| 34 | // |
---|
| 35 | // Define G4DrawVoxelsDebug for debugging information on G4cout |
---|
| 36 | // |
---|
| 37 | // History: |
---|
| 38 | // 03/08/1999 The G4VisAttributes have been made member data for |
---|
| 39 | // lifetime reasons / visualisation L.G |
---|
| 40 | // 29/07/1999 first comitted version L.G. |
---|
| 41 | // -------------------------------------------------------------------- |
---|
| 42 | |
---|
| 43 | #include "G4DrawVoxels.hh" |
---|
| 44 | #include "G4AffineTransform.hh" |
---|
| 45 | #include "G4SmartVoxelHeader.hh" |
---|
| 46 | #include "G4LogicalVolume.hh" |
---|
| 47 | #include "G4VSolid.hh" |
---|
| 48 | #include "G4VVisManager.hh" |
---|
| 49 | #include "G4Colour.hh" |
---|
| 50 | #include "G4TransportationManager.hh" |
---|
| 51 | #include "G4TouchableHistoryHandle.hh" |
---|
| 52 | |
---|
| 53 | #define voxel_width 0 |
---|
| 54 | |
---|
| 55 | // Private Constructor |
---|
| 56 | // |
---|
| 57 | G4DrawVoxels::G4DrawVoxels() |
---|
| 58 | { |
---|
| 59 | fVoxelsVisAttributes[0].SetColour(G4Colour(1.,0.,0.)); |
---|
| 60 | fVoxelsVisAttributes[1].SetColour(G4Colour(0.,1.,0.)); |
---|
| 61 | fVoxelsVisAttributes[2].SetColour(G4Colour(0.,0.,1.)); |
---|
| 62 | fBoundingBoxVisAttributes.SetColour(G4Colour(.3,0.,.2)); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | // Destructor |
---|
| 66 | // |
---|
| 67 | G4DrawVoxels::~G4DrawVoxels() |
---|
| 68 | { |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | // Methods that allow changing colors of the drawing |
---|
| 72 | // |
---|
| 73 | void G4DrawVoxels::SetVoxelsVisAttributes(G4VisAttributes& VA_voxelX, |
---|
| 74 | G4VisAttributes& VA_voxelY, |
---|
| 75 | G4VisAttributes& VA_voxelZ) |
---|
| 76 | { |
---|
| 77 | fVoxelsVisAttributes[0]=VA_voxelX; |
---|
| 78 | fVoxelsVisAttributes[1]=VA_voxelY; |
---|
| 79 | fVoxelsVisAttributes[2]=VA_voxelZ; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void G4DrawVoxels::SetBoundingBoxVisAttributes(G4VisAttributes& VA_boundingbox) |
---|
| 83 | { |
---|
| 84 | fBoundingBoxVisAttributes=VA_boundingbox; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | // *************************************************************** |
---|
| 88 | |
---|
| 89 | void |
---|
| 90 | G4DrawVoxels::ComputeVoxelPolyhedra(const G4LogicalVolume* lv, |
---|
| 91 | const G4SmartVoxelHeader* header, |
---|
| 92 | G4VoxelLimits& limit, |
---|
| 93 | G4PlacedPolyhedronList* ppl) const |
---|
| 94 | { |
---|
| 95 | // Let's draw the selected voxelisation now ! |
---|
| 96 | |
---|
| 97 | G4VSolid* solid=lv->GetSolid(); |
---|
| 98 | |
---|
| 99 | G4double dx=kInfinity,dy=kInfinity,dz=kInfinity; |
---|
| 100 | G4double xmax=0,xmin=0,ymax=0,ymin=0,zmax=0,zmin=0; |
---|
| 101 | |
---|
| 102 | if (lv->GetNoDaughters()<=0) |
---|
| 103 | { |
---|
| 104 | return; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // Let's get the data for the voxelisation |
---|
| 108 | |
---|
| 109 | solid->CalculateExtent(kXAxis,limit,G4AffineTransform(),xmin,xmax); |
---|
| 110 | // G4AffineTransform() is identity |
---|
| 111 | solid->CalculateExtent(kYAxis,limit,G4AffineTransform(),ymin,ymax); |
---|
| 112 | // extents according to the axis of the local frame |
---|
| 113 | solid->CalculateExtent(kZAxis,limit,G4AffineTransform(),zmin,zmax); |
---|
| 114 | dx=xmax-xmin; |
---|
| 115 | dy=ymax-ymin; |
---|
| 116 | dz=zmax-zmin; |
---|
| 117 | |
---|
| 118 | // Preparing the colored bounding polyhedronBox for the pVolume |
---|
| 119 | // |
---|
| 120 | G4PolyhedronBox bounding_polyhedronBox(dx*0.5,dy*0.5,dz*0.5); |
---|
| 121 | bounding_polyhedronBox.SetVisAttributes(&fBoundingBoxVisAttributes); |
---|
| 122 | G4ThreeVector t_centerofBoundingBox((xmin+xmax)*0.5, |
---|
| 123 | (ymin+ymax)*0.5, |
---|
| 124 | (zmin+zmax)*0.5); |
---|
| 125 | |
---|
| 126 | ppl->push_back(G4PlacedPolyhedron(bounding_polyhedronBox, |
---|
| 127 | G4Translate3D(t_centerofBoundingBox))); |
---|
| 128 | |
---|
| 129 | G4ThreeVector t_FirstCenterofVoxelPlane; |
---|
| 130 | const G4VisAttributes* voxelsVisAttributes=0; |
---|
| 131 | |
---|
| 132 | G4ThreeVector unit_translation_vector; |
---|
| 133 | G4ThreeVector current_translation_vector; |
---|
| 134 | |
---|
| 135 | switch(header->GetAxis()) |
---|
| 136 | { |
---|
| 137 | case kXAxis: |
---|
| 138 | dx=voxel_width; |
---|
| 139 | unit_translation_vector=G4ThreeVector(1,0,0); |
---|
| 140 | t_FirstCenterofVoxelPlane=G4ThreeVector(xmin,(ymin+ymax)*0.5, |
---|
| 141 | (zmin+zmax)*0.5); |
---|
| 142 | voxelsVisAttributes=&fVoxelsVisAttributes[0]; |
---|
| 143 | break; |
---|
| 144 | case kYAxis: |
---|
| 145 | dy=voxel_width; |
---|
| 146 | t_FirstCenterofVoxelPlane=G4ThreeVector((xmin+xmax)*0.5,ymin, |
---|
| 147 | (zmin+zmax)*0.5); |
---|
| 148 | unit_translation_vector=G4ThreeVector(0,1,0); |
---|
| 149 | voxelsVisAttributes=&fVoxelsVisAttributes[1]; |
---|
| 150 | break; |
---|
| 151 | case kZAxis: |
---|
| 152 | dz=voxel_width; |
---|
| 153 | t_FirstCenterofVoxelPlane=G4ThreeVector((xmin+xmax)*0.5, |
---|
| 154 | (ymin+ymax)*0.5,zmin); |
---|
| 155 | unit_translation_vector=G4ThreeVector(0,0,1); |
---|
| 156 | voxelsVisAttributes=&fVoxelsVisAttributes[2]; |
---|
| 157 | break; |
---|
| 158 | default: |
---|
| 159 | break; |
---|
| 160 | }; |
---|
| 161 | |
---|
| 162 | G4PolyhedronBox voxel_plane(dx*0.5,dy*0.5,dz*0.5); |
---|
| 163 | voxel_plane.SetVisAttributes(voxelsVisAttributes); |
---|
| 164 | |
---|
| 165 | G4SmartVoxelProxy* slice=header->GetSlice(0); |
---|
| 166 | G4int slice_no=0,no_slices=header->GetNoSlices(); |
---|
| 167 | G4double beginning=header->GetMinExtent(), |
---|
| 168 | step=(header->GetMaxExtent()-beginning)/no_slices; |
---|
| 169 | |
---|
| 170 | while (slice_no<no_slices) |
---|
| 171 | { |
---|
| 172 | if (slice->IsHeader()) |
---|
| 173 | { |
---|
| 174 | G4VoxelLimits newlimit(limit); |
---|
| 175 | newlimit.AddLimit(header->GetAxis(),beginning+step*slice_no, |
---|
| 176 | beginning+step*(slice->GetHeader()->GetMaxEquivalentSliceNo()+1)); |
---|
| 177 | ComputeVoxelPolyhedra(lv,slice->GetHeader(),newlimit,ppl); |
---|
| 178 | } |
---|
| 179 | current_translation_vector=unit_translation_vector; |
---|
| 180 | current_translation_vector*=step*slice_no; |
---|
| 181 | |
---|
| 182 | ppl->push_back(G4PlacedPolyhedron(voxel_plane, |
---|
| 183 | G4Translate3D(current_translation_vector |
---|
| 184 | +t_FirstCenterofVoxelPlane))); |
---|
| 185 | slice_no=(slice->IsHeader() |
---|
| 186 | ? slice->GetHeader()->GetMaxEquivalentSliceNo()+1 |
---|
| 187 | : slice->GetNode()->GetMaxEquivalentSliceNo()+1); |
---|
| 188 | if (slice_no<no_slices) { slice=header->GetSlice(slice_no); } |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | // ######################################################################## |
---|
| 193 | |
---|
| 194 | G4PlacedPolyhedronList* |
---|
| 195 | G4DrawVoxels::CreatePlacedPolyhedra(const G4LogicalVolume* lv) const |
---|
| 196 | { |
---|
| 197 | G4PlacedPolyhedronList* pplist=new G4PlacedPolyhedronList; |
---|
| 198 | G4VoxelLimits limits; // Working object for recursive call. |
---|
| 199 | ComputeVoxelPolyhedra(lv,lv->GetVoxelHeader(),limits,pplist); |
---|
| 200 | return pplist; //it s up to the calling program to destroy it then! |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void G4DrawVoxels::DrawVoxels(const G4LogicalVolume* lv) const |
---|
| 204 | { |
---|
| 205 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance(); |
---|
| 206 | |
---|
| 207 | if (lv->GetNoDaughters()<=0) |
---|
| 208 | { |
---|
| 209 | return; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // Computing the transformation according to the world volume |
---|
| 213 | // (the drawing is directly in the world volume while the axis |
---|
| 214 | // are relative to the mother volume of lv's daughter.) |
---|
| 215 | |
---|
| 216 | G4TouchableHistoryHandle aTouchable = |
---|
| 217 | G4TransportationManager::GetTransportationManager()-> |
---|
| 218 | GetNavigatorForTracking()->CreateTouchableHistoryHandle(); |
---|
| 219 | G4AffineTransform globTransform = |
---|
| 220 | aTouchable->GetHistory()->GetTopTransform().Inverse(); |
---|
| 221 | G4Transform3D transf3D(globTransform.NetRotation(), |
---|
| 222 | globTransform.NetTranslation()); |
---|
| 223 | |
---|
| 224 | G4PlacedPolyhedronList* pplist=CreatePlacedPolyhedra(lv); |
---|
| 225 | if(pVVisManager) |
---|
| 226 | { |
---|
| 227 | // Drawing the bounding and voxel polyhedra for the pVolume |
---|
| 228 | // |
---|
| 229 | for (size_t i=0;i<pplist->size();i++) |
---|
| 230 | { |
---|
| 231 | pVVisManager->Draw((*pplist)[i].GetPolyhedron(), |
---|
| 232 | (*pplist)[i].GetTransform()*transf3D); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | else |
---|
| 236 | { |
---|
| 237 | G4cerr << "ERROR - G4DrawVoxels::DrawVoxels()" << G4endl |
---|
| 238 | << " Pointer to visualization manager is null!" |
---|
| 239 | << G4endl; |
---|
| 240 | G4Exception("G4DrawVoxels::DrawVoxels()", |
---|
| 241 | "NotApplicable", JustWarning, |
---|
| 242 | "Pointer to visualization manager is null!"); |
---|
| 243 | } |
---|
| 244 | delete pplist; |
---|
| 245 | } |
---|