Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 26 #include <vtkPoints.h> 27 #include <vtkCellArray.h> 28 #include <vtkCharArray.h> 29 #include <vtkUnstructuredGrid.h> 30 #include <vtkDataSetMapper.h> 31 #include <vtkUnstructuredGridVolumeMapper.h> 32 #include <vtkUnstructuredGridVolumeRayCastMapp 33 #include <vtkUnstructuredGridVolumeZSweepMappe 34 #include <vtkOpenGLProjectedTetrahedraMapper.h 35 #include <vtkActor.h> 36 #include <vtkColorTransferFunction.h> 37 #include <vtkPiecewiseFunction.h> 38 #include <vtkLookupTable.h> 39 #include <vtkVolumeProperty.h> 40 #include <vtkVolume.h> 41 #include <vtkNew.h> 42 #include <vtkTetra.h> 43 #include <vtkVoxel.h> 44 // #include <vtkCleanUnstructuredGrid.h> 45 #include <vtkDataSetTriangleFilter.h> 46 #include <vtkClipDataSet.h> 47 48 #include "G4VtkUnstructuredGridPipeline.hh" 49 #include "G4VtkVisContext.hh" 50 #include "G4VtkViewer.hh" 51 52 #include "G4Mesh.hh" 53 #include "G4LogicalVolume.hh" 54 #include "G4VNestedParameterisation.hh" 55 #include "G4ModelingParameters.hh" 56 #include "G4PhysicalVolumeModel.hh" 57 #include "G4PseudoScene.hh" 58 #include "G4Transform3D.hh" 59 #include "G4VSolid.hh" 60 #include "G4Tet.hh" 61 #include "G4Material.hh" 62 63 G4VtkUnstructuredGridPipeline::G4VtkUnstructur 64 G4VVtkPipeline(nameIn, "G4VtkUnstructuredPip 65 66 points = vtkSmartPointer<vtkPoints>::Ne 67 68 pointColourValues = vtkSmartPointer<vtkDoubl 69 cellColourValues = vtkSmartPointer<vtkDoubl 70 pointColourValues->SetNumberOfComponents(4); 71 cellColourValues->SetNumberOfComponents(4); 72 73 pointColourIndices = vtkSmartPointer<vtkDoub 74 cellColourIndices = vtkSmartPointer<vtkDoub 75 pointColourIndices->SetNumberOfComponents(1) 76 cellColourIndices->SetNumberOfComponents(1); 77 78 colourLUT = vtkSmartPointer<vtkDiscretizable 79 colourLUT->DiscretizeOn(); 80 81 unstructuredGrid = vtkSmartPointer<vtkUnstru 82 unstructuredGrid->SetPoints(points); 83 unstructuredGrid->GetPointData()->SetScalars 84 unstructuredGrid->GetCellData()->SetScalars( 85 86 // clean filter 87 #if 0 88 clean = vtkSmartPointer<vtkStaticCleanUnstru 89 clean->SetInputData(unstructuredGrid); 90 clean->ToleranceIsAbsoluteOff(); 91 clean->SetTolerance(1e-6); 92 #endif 93 94 // Clip filter 95 clip = vtkSmartPointer<vtkClipDataSet>::New( 96 vtkNew<vtkPlane> plane; 97 clip->SetClipFunction(plane); 98 clip->SetInputData(unstructuredGrid); 99 100 // Triangle filter 101 auto tri = vtkSmartPointer<vtkDataSetTriangl 102 tri->SetInputData(unstructuredGrid); 103 104 // create dataset mapper 105 mapper = vtkSmartPointer<vtkDataSetMapper>:: 106 mapper->SetScalarModeToUseCellData(); 107 mapper->SetColorModeToDirectScalars(); 108 mapper->SetInputData(unstructuredGrid); 109 //mapper->SetInputConnection(clip->GetOutput 110 //mapper->SetInputConnection(tri->GetOutputP 111 112 // create volume mapper 113 volumeMapper = vtkSmartPointer<vtkUnstructur 114 volumeMapper->SetScalarModeToUseCellData(); 115 volumeMapper->SetInputConnection(tri->GetOut 116 117 // create volume properties 118 auto volumeProp = vtkSmartPointer<vtkVolumeP 119 volumeProp->SetColor(colourLUT); 120 121 // create actor 122 actor = vtkSmartPointer<vtkActor>::New(); 123 actor->SetMapper(mapper); 124 actor->SetVisibility(1); 125 126 // create volume 127 volume = vtkSmartPointer<vtkVolume>::New(); 128 volume->SetMapper(volumeMapper); 129 //volume->SetProperty(volumeProp); 130 volume->SetVisibility(1); 131 132 // add to renderer 133 vc.fViewer->renderer->AddActor(actor); 134 //vc.fViewer->renderer->AddVolume(volume); 135 } 136 137 void G4VtkUnstructuredGridPipeline::PseudoScen 138 if (fpPVModel->GetCurrentDepth() == fDepth) 139 // Need to know it's a tet !!!! or impleme 140 try { 141 const G4Tet& tet = dynamic_cast<const G4 142 const auto* pVisAtts = fpPVModel->GetCur 143 const auto& colour = pVisAtts->GetColour 144 145 G4ThreeVector p0, p1, p2, p3; 146 tet.GetVertices(p0, p1, p2, p3); 147 148 G4int idx[4] = {-1, -1, -1, -1}; 149 150 if (iCell > 0 && iCell < 100000000) { 151 #if 0 152 G4ThreeVector pts[4] = {p0, p1, p2, p3 153 for (G4int i = 0; i < 4; i++) { 154 auto h = MakeHash(pts[i]); 155 if (fpPointMap.find(h) == fpPointMap 156 fpPointMap.insert(std::make_pair(h 157 fpPoints->InsertNextPoint(pts[i].x 158 fpPointVector.push_back(pts[i]); 159 idx[i] = iPoint; 160 iPoint++; 161 } 162 else { 163 idx[i] = fpPointMap[h]; 164 } 165 } 166 #endif 167 168 #if 1 169 fpPoints->InsertNextPoint(p0.x(), p0.y 170 fpPoints->InsertNextPoint(p1.x(), p1.y 171 fpPoints->InsertNextPoint(p2.x(), p2.y 172 fpPoints->InsertNextPoint(p3.x(), p3.y 173 iPoint += 4; 174 175 idx[0] = 4 * iCellAdd; 176 idx[1] = 4 * iCellAdd + 1; 177 idx[2] = 4 * iCellAdd + 2; 178 idx[3] = 4 * iCellAdd + 3; 179 #endif 180 181 vtkNew<vtkTetra> tetra; 182 tetra->GetPointIds()->SetId(0, idx[0]) 183 tetra->GetPointIds()->SetId(1, idx[1]) 184 tetra->GetPointIds()->SetId(2, idx[2]) 185 tetra->GetPointIds()->SetId(3, idx[3]) 186 187 fpGrid->InsertNextCell(tetra->GetCellT 188 189 auto hash = MakeHash(colour); 190 unsigned short int iColour = 0; 191 if (fpColourMap.find(hash) == fpColour 192 iColour = fpColourMap.size(); 193 fpColourMap.insert(std::make_pair(ha 194 double c[4] = {colour.GetRed(), colo 195 fpColourLUT->SetIndexedColorRGBA(iCo 196 } 197 else { 198 iColour = fpColourMap[hash]; 199 } 200 201 fpCellColourValues->InsertNextTuple4(c 202 fpCellColourIndices->InsertNextTuple1( 203 204 iCellAdd++; 205 } 206 iCell++; 207 } 208 catch (const std::bad_cast&) { 209 G4ExceptionDescription ed; 210 ed << "Called for a mesh that is not a t 211 G4Exception("PseudoSceneForTetVertices", 212 } 213 } 214 } 215 216 void G4VtkUnstructuredGridPipeline::PseudoScen 217 if (fpPVModel->GetCurrentDepth() == fDepth) 218 try { 219 const auto* pVisAtts = fpPVModel->GetCur 220 const auto& colour = pVisAtts->GetColour 221 const G4ThreeVector& position = fpCurren 222 223 fpPoints->InsertNextPoint(position.x()-b 224 fpPoints->InsertNextPoint(position.x()-b 225 fpPoints->InsertNextPoint(position.x()+b 226 fpPoints->InsertNextPoint(position.x()+b 227 228 fpPoints->InsertNextPoint(position.x()-b 229 fpPoints->InsertNextPoint(position.x()-b 230 fpPoints->InsertNextPoint(position.x()+b 231 fpPoints->InsertNextPoint(position.x()+b 232 233 vtkNew<vtkVoxel> voxel; 234 voxel->GetPointIds()->SetId(0, 8*iCell); 235 voxel->GetPointIds()->SetId(1, 8*iCell+1 236 voxel->GetPointIds()->SetId(2, 8*iCell+2 237 voxel->GetPointIds()->SetId(3, 8*iCell+3 238 voxel->GetPointIds()->SetId(4, 8*iCell+4 239 voxel->GetPointIds()->SetId(5, 8*iCell+5 240 voxel->GetPointIds()->SetId(6, 8*iCell+6 241 voxel->GetPointIds()->SetId(7, 8*iCell+7 242 243 fpGrid->InsertNextCell(voxel->GetCellTyp 244 245 double cols[] = {colour.GetRed(), 246 colour.GetGreen(), 247 colour.GetBlue(), 248 colour.GetAlpha()}; 249 fpCellColourValues->InsertNextTuple(cols 250 //fpCellColourIndices->InsertNextTuple1( 251 252 253 iCell++; 254 255 } 256 catch (const std::bad_cast&) { 257 G4ExceptionDescription ed; 258 ed << "Called for a mesh that is not a c 259 G4Exception("PseudoSceneForCubicalVertic 260 } 261 } 262 } 263 264 void G4VtkUnstructuredGridPipeline::SetUnstruc 265 266 // Modelling parameters 267 G4ModelingParameters tmpMP; 268 tmpMP.SetCulling(true); // This 269 tmpMP.SetCullingInvisible(true); // ... o 270 271 // Physical volume model 272 const auto& container = mesh.GetContainerVol 273 const G4bool useFullExtent = true; // To av 274 G4PhysicalVolumeModel tmpPVModel(container, 275 G4PhysicalV 276 G4Transform 277 &tmpMP, 278 useFullExte 279 280 // Graphics scene 281 if(mesh.GetMeshType() == G4Mesh::tetrahedron 282 PseudoSceneForTetCells pseudoScene(&tmpPVM 283 pointCo 284 pointCo 285 colourL 286 tmpPVModel.DescribeYourselfTo(pseudoScene) 287 //G4cout << pseudoScene.GetNumberOfPoints( 288 //pseudoScene.DumpColourMap(); 289 } 290 else if(mesh.GetMeshType() == G4Mesh::nested 291 PseudoSceneForCubicalCells pseudoScene(&tm 292 poi 293 poi 294 col 295 tmpPVModel.DescribeYourselfTo(pseudoScene) 296 } 297 else if(mesh.GetMeshType() == G4Mesh::rectan 298 PseudoSceneForCubicalCells pseudoScene(&tm 299 poi 300 poi 301 col 302 tmpPVModel.DescribeYourselfTo(pseudoScene) 303 } 304 305 }