G4DrawVoxels.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // class G4DrawVoxels
00031 //
00032 // Implementation
00033 //
00034 // Define G4DrawVoxelsDebug for debugging information on G4cout
00035 //
00036 // History:
00037 // 03/08/1999 The G4VisAttributes have been made member data for
00038 //            lifetime reasons / visualisation  L.G
00039 // 29/07/1999 first comitted version L.G.
00040 // --------------------------------------------------------------------
00041 
00042 #include "G4DrawVoxels.hh"
00043 #include "G4AffineTransform.hh"
00044 #include "G4SmartVoxelHeader.hh"
00045 #include "G4LogicalVolume.hh"
00046 #include "G4VSolid.hh"
00047 #include "G4VVisManager.hh"
00048 #include "G4Colour.hh"
00049 #include "G4TransportationManager.hh"
00050 #include "G4TouchableHistoryHandle.hh"
00051 
00052 #define voxel_width 0
00053 
00054 // Private Constructor
00055 //
00056 G4DrawVoxels::G4DrawVoxels()
00057 {
00058   fVoxelsVisAttributes[0].SetColour(G4Colour(1.,0.,0.));
00059   fVoxelsVisAttributes[1].SetColour(G4Colour(0.,1.,0.));
00060   fVoxelsVisAttributes[2].SetColour(G4Colour(0.,0.,1.));
00061   fBoundingBoxVisAttributes.SetColour(G4Colour(.3,0.,.2));
00062 }
00063 
00064 // Destructor
00065 //
00066 G4DrawVoxels::~G4DrawVoxels()
00067 {
00068 }
00069 
00070 // Methods that allow changing colors of the drawing
00071 //
00072 void G4DrawVoxels::SetVoxelsVisAttributes(G4VisAttributes& VA_voxelX,
00073                                           G4VisAttributes& VA_voxelY,
00074                                           G4VisAttributes& VA_voxelZ)
00075 {
00076   fVoxelsVisAttributes[0]=VA_voxelX;
00077   fVoxelsVisAttributes[1]=VA_voxelY;
00078   fVoxelsVisAttributes[2]=VA_voxelZ;
00079 }
00080 
00081 void G4DrawVoxels::SetBoundingBoxVisAttributes(G4VisAttributes& VA_boundingbox)
00082 {
00083   fBoundingBoxVisAttributes=VA_boundingbox;
00084 }
00085 
00086 // ***************************************************************
00087 
00088 void
00089 G4DrawVoxels::ComputeVoxelPolyhedra(const G4LogicalVolume* lv,
00090                                     const G4SmartVoxelHeader* header,
00091                                           G4VoxelLimits& limit,
00092                                           G4PlacedPolyhedronList* ppl) const
00093 {
00094   // Let's draw the selected voxelisation now !
00095  
00096    G4VSolid* solid=lv->GetSolid();
00097   
00098    G4double dx=kInfinity,dy=kInfinity,dz=kInfinity;
00099    G4double xmax=0,xmin=0,ymax=0,ymin=0,zmax=0,zmin=0;
00100    
00101    if (lv->GetNoDaughters()<=0)
00102    {
00103      return;
00104    }
00105    
00106    // Let's get the data for the voxelisation
00107 
00108    solid->CalculateExtent(kXAxis,limit,G4AffineTransform(),xmin,xmax);
00109      // G4AffineTransform() is identity
00110    solid->CalculateExtent(kYAxis,limit,G4AffineTransform(),ymin,ymax);
00111      // extents according to the axis of the local frame
00112    solid->CalculateExtent(kZAxis,limit,G4AffineTransform(),zmin,zmax);
00113    dx=xmax-xmin;
00114    dy=ymax-ymin;
00115    dz=zmax-zmin;
00116 
00117    // Preparing the colored bounding polyhedronBox for the pVolume
00118    //
00119    G4PolyhedronBox bounding_polyhedronBox(dx*0.5,dy*0.5,dz*0.5);
00120    bounding_polyhedronBox.SetVisAttributes(&fBoundingBoxVisAttributes);
00121    G4ThreeVector t_centerofBoundingBox((xmin+xmax)*0.5,
00122                                        (ymin+ymax)*0.5,
00123                                        (zmin+zmax)*0.5);
00124    
00125    ppl->push_back(G4PlacedPolyhedron(bounding_polyhedronBox,
00126                                      G4Translate3D(t_centerofBoundingBox)));
00127    
00128    G4ThreeVector t_FirstCenterofVoxelPlane;
00129    const G4VisAttributes* voxelsVisAttributes=0;
00130 
00131    G4ThreeVector unit_translation_vector;
00132    G4ThreeVector current_translation_vector;
00133    
00134    switch(header->GetAxis())
00135    {
00136      case kXAxis:
00137        dx=voxel_width;
00138        unit_translation_vector=G4ThreeVector(1,0,0);
00139        t_FirstCenterofVoxelPlane=G4ThreeVector(xmin,(ymin+ymax)*0.5,
00140                                                     (zmin+zmax)*0.5);
00141        voxelsVisAttributes=&fVoxelsVisAttributes[0];
00142        break;
00143      case kYAxis:
00144        dy=voxel_width;
00145        t_FirstCenterofVoxelPlane=G4ThreeVector((xmin+xmax)*0.5,ymin,
00146                                                (zmin+zmax)*0.5);
00147        unit_translation_vector=G4ThreeVector(0,1,0);
00148        voxelsVisAttributes=&fVoxelsVisAttributes[1];
00149        break;
00150      case kZAxis:
00151        dz=voxel_width;
00152        t_FirstCenterofVoxelPlane=G4ThreeVector((xmin+xmax)*0.5,
00153                                                (ymin+ymax)*0.5,zmin);
00154        unit_translation_vector=G4ThreeVector(0,0,1);
00155        voxelsVisAttributes=&fVoxelsVisAttributes[2];
00156        break;
00157      default:
00158        break;
00159    };
00160      
00161    G4PolyhedronBox voxel_plane(dx*0.5,dy*0.5,dz*0.5);
00162    voxel_plane.SetVisAttributes(voxelsVisAttributes);
00163    
00164    G4SmartVoxelProxy* slice=header->GetSlice(0);
00165    G4int slice_no=0,no_slices=header->GetNoSlices();
00166    G4double beginning=header->GetMinExtent(),
00167             step=(header->GetMaxExtent()-beginning)/no_slices;
00168 
00169    while (slice_no<no_slices)
00170    {    
00171      if (slice->IsHeader())
00172      {
00173        G4VoxelLimits newlimit(limit);
00174        newlimit.AddLimit(header->GetAxis(),beginning+step*slice_no,
00175        beginning+step*(slice->GetHeader()->GetMaxEquivalentSliceNo()+1));
00176        ComputeVoxelPolyhedra(lv,slice->GetHeader(),newlimit,ppl);
00177      }
00178      current_translation_vector=unit_translation_vector;
00179      current_translation_vector*=step*slice_no;
00180    
00181      ppl->push_back(G4PlacedPolyhedron(voxel_plane,
00182                     G4Translate3D(current_translation_vector
00183                                  +t_FirstCenterofVoxelPlane)));
00184      slice_no=(slice->IsHeader()
00185                ? slice->GetHeader()->GetMaxEquivalentSliceNo()+1
00186                : slice->GetNode()->GetMaxEquivalentSliceNo()+1);
00187      if (slice_no<no_slices) { slice=header->GetSlice(slice_no); }
00188    }
00189 }
00190 
00191 // ########################################################################
00192 
00193 G4PlacedPolyhedronList*
00194 G4DrawVoxels::CreatePlacedPolyhedra(const G4LogicalVolume* lv) const
00195 {
00196   G4PlacedPolyhedronList* pplist=new G4PlacedPolyhedronList;
00197   G4VoxelLimits limits;  // Working object for recursive call.
00198   ComputeVoxelPolyhedra(lv,lv->GetVoxelHeader(),limits,pplist);
00199   return pplist; //it s up to the calling program to destroy it then!
00200 }
00201 
00202 void G4DrawVoxels::DrawVoxels(const G4LogicalVolume* lv) const
00203 {   
00204    G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
00205 
00206    if (lv->GetNoDaughters()<=0)
00207    {
00208      return;
00209    }
00210 
00211    // Computing the transformation according to the world volume 
00212    // (the drawing is directly in the world volume while the axis
00213    // are relative to the mother volume of lv's daughter.)
00214 
00215    G4TouchableHistoryHandle aTouchable =
00216      G4TransportationManager::GetTransportationManager()->
00217      GetNavigatorForTracking()->CreateTouchableHistoryHandle();
00218    G4AffineTransform globTransform =
00219      aTouchable->GetHistory()->GetTopTransform().Inverse();
00220    G4Transform3D transf3D(globTransform.NetRotation(),
00221                           globTransform.NetTranslation());
00222 
00223    G4PlacedPolyhedronList* pplist=CreatePlacedPolyhedra(lv);
00224    if(pVVisManager)
00225    {
00226      // Drawing the bounding and voxel polyhedra for the pVolume
00227      //
00228      for (size_t i=0;i<pplist->size();i++)
00229      {
00230        pVVisManager->Draw((*pplist)[i].GetPolyhedron(),
00231                           (*pplist)[i].GetTransform()*transf3D);
00232      }
00233    }
00234    else
00235    {
00236      G4Exception("G4DrawVoxels::DrawVoxels()",
00237                  "GeomNav1002", JustWarning,
00238                  "Pointer to visualization manager is null!");
00239    }
00240    delete pplist;
00241 }

Generated on Mon May 27 17:48:04 2013 for Geant4 by  doxygen 1.4.7