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 "G4VtkPolydataPipeline.hh" 27 28 #include "G4Normal3D.hh" 29 #include "G4Point3D.hh" 30 #include "G4Polyhedron.hh" 31 #include "G4Polyline.hh" 32 #include "G4ViewParameters.hh" 33 #include "G4VtkViewer.hh" 34 #include "G4VtkVisContext.hh" 35 36 #include <vtkActor.h> 37 #include <vtkCleanPolyData.h> 38 #include <vtkLine.h> 39 #include <vtkMatrix4x4.h> 40 #include <vtkPolyDataAlgorithm.h> 41 #include <vtkPolyDataMapper.h> 42 #include <vtkPolyDataNormals.h> 43 #include <vtkProperty.h> 44 #include <vtkTriangleFilter.h> 45 46 std::size_t G4VtkPolydataPipeline::MakeHash(co 47 co 48 { 49 std::size_t hash = std::hash<G4Polyhedron>{} 50 51 std::hash_combine(hash, std::hash<G4double>{ 52 std::hash_combine(hash, std::hash<G4double>{ 53 std::hash_combine(hash, std::hash<G4double>{ 54 55 std::hash_combine(hash, std::hash<G4double>{ 56 std::hash_combine(hash, std::hash<G4double>{ 57 std::hash_combine(hash, std::hash<G4double>{ 58 59 std::hash_combine(hash, std::hash<G4double>{ 60 std::hash_combine(hash, std::hash<G4double>{ 61 std::hash_combine(hash, std::hash<G4double>{ 62 63 std::hash_combine(hash, std::hash<G4double>{ 64 std::hash_combine(hash, std::hash<G4double>{ 65 std::hash_combine(hash, std::hash<G4double>{ 66 67 return hash; 68 } 69 70 G4VtkPolydataPipeline::G4VtkPolydataPipeline(G 71 : G4VVtkPipeline(nameIn, "G4VtkPolydataPipel 72 { 73 // Set pipeline type 74 SetTypeName(G4String("G4VtkPolydataPipeline" 75 76 polydataPoints = vtkSmartPointer<vtkPoints>: 77 polydataCells = vtkSmartPointer<vtkCellArray 78 polydata = vtkSmartPointer<vtkPolyData>::New 79 80 polydata->SetPoints(polydataPoints); 81 polydata->SetPolys(polydataCells); 82 83 // clean input polydata 84 auto filterClean = vtkSmartPointer<vtkCleanP 85 filterClean->PointMergingOn(); 86 filterClean->AddInputData(polydata); 87 AddFilter(filterClean); 88 89 // ensure triangular mesh 90 auto filterTriangle = vtkSmartPointer<vtkTri 91 filterTriangle->SetInputConnection(filterCle 92 AddFilter(filterTriangle); 93 94 // calculate normals with a feature angle of 95 auto filterNormals = vtkSmartPointer<vtkPoly 96 filterNormals->SetFeatureAngle(45); 97 filterNormals->SetInputConnection(filterTria 98 AddFilter(filterNormals); 99 100 // mapper 101 mapper = vtkSmartPointer<vtkPolyDataMapper>: 102 mapper->SetInputConnection(GetFinalFilter()- 103 mapper->SetColorModeToDirectScalars(); 104 105 // add to actor 106 actor = vtkSmartPointer<vtkActor>::New(); 107 actor->SetMapper(mapper); 108 actor->SetVisibility(1); 109 110 // set actor properties from vis context 111 if (vc.fDrawingStyle == G4ViewParameters::hs 112 } 113 else if (vc.fDrawingStyle == G4ViewParameter 114 } 115 else if (vc.fDrawingStyle == G4ViewParameter 116 actor->GetProperty()->SetRepresentationToW 117 } 118 119 // add to renderer 120 vc.fViewer->renderer->AddActor(GetActor()); 121 } 122 123 void G4VtkPolydataPipeline::Enable() 124 { 125 actor->SetVisibility(1); 126 } 127 128 void G4VtkPolydataPipeline::Disable() 129 { 130 actor->SetVisibility(0); 131 } 132 133 void G4VtkPolydataPipeline::Print() 134 { 135 G4cout << "G4VtkPolydataPipeline filters ("; 136 for (const auto& f : filters) 137 G4cout << f->GetInformation() << ","; 138 G4cout << ")" << G4endl; 139 140 G4VVtkPipeline::Print(); 141 } 142 143 void G4VtkPolydataPipeline::Modified() 144 { 145 actor->Modified(); 146 polydata->Modified(); 147 mapper->Update(); 148 149 G4VVtkPipeline::Modified(); 150 } 151 152 void G4VtkPolydataPipeline::Clear() 153 { 154 renderer->RemoveActor(actor); 155 G4VVtkPipeline::Clear(); 156 } 157 158 void G4VtkPolydataPipeline::SetPolydata(const 159 { 160 G4bool notLastFace; 161 int iVert = 0; 162 do { 163 G4Point3D vertex[4]; 164 G4int edgeFlag[4]; 165 G4Normal3D normals[4]; 166 G4int nEdges; 167 notLastFace = polyhedron.GetNextFacet(nEdg 168 169 vtkSmartPointer<vtkIdList> poly = vtkSmart 170 // loop over vertices 171 for (int i = 0; i < nEdges; i++) { 172 polydataPoints->InsertNextPoint(vertex[i 173 poly->InsertNextId(iVert); 174 iVert++; 175 } 176 polydataCells->InsertNextCell(poly); 177 178 } while (notLastFace); 179 } 180 181 void G4VtkPolydataPipeline::SetPolydata(vtkPol 182 polydata->DeepCopy(polydataIn); 183 } 184 185 186 void G4VtkPolydataPipeline::SetPolydata(const 187 { 188 // Data data 189 const size_t nLines = polyline.size(); 190 191 for (size_t i = 0; i < nLines; ++i) { 192 auto id = polydataPoints->InsertNextPoint( 193 194 if (i < nLines - 1) { 195 vtkSmartPointer<vtkLine> line = vtkSmart 196 line->GetPointIds()->SetId(0, id); 197 line->GetPointIds()->SetId(1, id + 1); 198 polydataCells->InsertNextCell(line); 199 } 200 } 201 } 202 203 void G4VtkPolydataPipeline::SetPolydataData(co 204 { 205 SetPolydataData(p.x(), p.y(), p.z()); 206 } 207 208 void G4VtkPolydataPipeline::SetPolydataData(do 209 { 210 polydataPoints->InsertNextPoint(x, y, z); 211 } 212 213 void G4VtkPolydataPipeline::SetActorTransform( 214 215 216 217 { 218 // create transform 219 auto transform = vtkSmartPointer<vtkMatrix4x 220 double transformArray[16] = {r00, r01, r02, 221 r20, r21, r22, 222 transform->DeepCopy(transformArray); 223 actor->SetUserMatrix(transform); 224 } 225 226 void G4VtkPolydataPipeline::SetActorColour(G4d 227 { 228 actor->GetProperty()->SetColor(r, g, b); 229 actor->GetProperty()->SetOpacity(a); 230 } 231