Skip to content

Commit

Permalink
ASSIMP: adapt for changes in TGeoTesselated in ROOT
Browse files Browse the repository at this point in the history
  • Loading branch information
andresailer committed Jan 13, 2024
1 parent 0a2267e commit e942539
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 13 deletions.
52 changes: 52 additions & 0 deletions DDCAD/include/DDCAD/Utilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// Author : A. Sailer
//
//==========================================================================
#ifndef DDCAD_UTILITIES_H
#define DDCAD_UTILITIES_H

#include <TGeoTessellated.h>
#include <TGeoVector3.h>
/// Namespace for the AIDA detector description toolkit
namespace dd4hep {

/// Namespace for implementation details of the AIDA detector description toolkit
namespace cad {

inline std::stringstream streamFacet(TGeoFacet const& facet,
TGeoTessellated const& shape) {
using ::operator<<;
std::stringstream str;
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
str << "{";
for (int i = 0; i < facet.GetNvert(); ++i) {
str << shape.GetVertex(facet[i]);
if (i != facet.GetNvert() - 1)
str << ", ";
}
str << "}";
#else
str << facet;
#endif
return str;
}

inline std::stringstream streamVertices(ROOT::Geom::Vertex_t const& v1,
ROOT::Geom::Vertex_t const& v2,
ROOT::Geom::Vertex_t const& v3) {
using ::operator<<;
std::stringstream str;
str << "{" << v1 << ", " << v2 << ", " << v3 << "}";
return str;
}
}
}
#endif
7 changes: 3 additions & 4 deletions DDCAD/src/ASSIMPReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <DD4hep/Detector.h>
#include <DD4hep/ShapeTags.h>
#include <DDCAD/ASSIMPReader.h>
#include <DDCAD/Utilities.h>

/// Open Asset Importer Library
#include "assimp/scene.h"
Expand Down Expand Up @@ -62,8 +63,7 @@ ASSIMPReader::readShapes(const std::string& source, double unit_length) const
if ( dump_facets ) {
for( size_t i=0, n=shape->GetNfacets(); i < n; ++i ) {
const auto& facet = shape->GetFacet(i);
std::stringstream str;
str << facet;
std::stringstream str = dd4hep::cad::streamFacet(facet, shape);
printout(ALWAYS,"ASSIMPReader","++ Facet %4ld : %s",
i, str.str().c_str());
}
Expand Down Expand Up @@ -162,8 +162,7 @@ ASSIMPReader::readVolumes(const std::string& source, double unit_length) const
if ( dump_facets ) {
for( size_t i=0, n=shape->GetNfacets(); i < n; ++i ) {
const auto& facet = shape->GetFacet(i);
std::stringstream str;
str << facet;
std::stringstream str = dd4hep::cad::streamFacet(facet, shape);
printout(ALWAYS,"ASSIMPReader","++ Facet %4ld : %s",
i, str.str().c_str());
}
Expand Down
24 changes: 17 additions & 7 deletions DDCAD/src/ASSIMPWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
#include <DD4hep/Detector.h>
#include <DD4hep/Printout.h>
#include <DDCAD/ASSIMPWriter.h>
#include <DDCAD/Utilities.h>

/// Open Asset Importer Library
#include "assimp/postprocess.h"
#include "assimp/Exporter.hpp"
#include "assimp/scene.h"

/// ROOT include files
#include <TBuffer3D.h>
#include <TBuffer3DTypes.h>
#include <TClass.h>
#include <TGeoBoolNode.h>
#include <TGeoMatrix.h>
#include <TBuffer3D.h>
#include <TClass.h>
#include <CsgOps.h>

/// C/C++ include files
Expand Down Expand Up @@ -157,13 +158,15 @@ namespace {
++nskip;
continue;
}
#if ROOT_VERSION_CODE < ROOT_VERSION(6,31,1)
bool degenerated = true;
TGeoFacet f(&vertices, 3, vv0, vv1, vv2);
f.ComputeNormal(degenerated);
if ( degenerated ) {
++nskip;
continue;
}
#endif
tes->AddFacet(vv0, vv1, vv2);
}
#else
Expand Down Expand Up @@ -388,7 +391,11 @@ int ASSIMPWriter::write(const std::string& file_name,
for( long j=0, nvx=0, n=tes->GetNfacets(); j < n; ++j ) {
bool degenerated = false;
const auto& facet = tes->GetFacet(j);
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
tmp = tes->FacetComputeNormal(j, degenerated);
#else
tmp = facet.ComputeNormal(degenerated);
#endif
if ( !degenerated && facet.GetNvert() > 0 ) {
aiFace& face = mesh->mFaces[mesh->mNumFaces];
double u = unit_scale;
Expand All @@ -397,7 +404,11 @@ int ASSIMPWriter::write(const std::string& file_name,
trafo->LocalToMaster(tmp.fVec, norm.fVec);
face.mNumIndices = 0;
for( long k=0; k < facet.GetNvert(); ++k ) {
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
tmp = tes->GetVertex(facet[k]);
#else
tmp = facet.GetVertex(k);
#endif
trafo->LocalToMaster(tmp.fVec, vtx.fVec);
face.mIndices[face.mNumIndices] = nvx;
mesh->mNormals[nvx] = aiVector3D(norm.x(), norm.y(), norm.z());
Expand All @@ -408,13 +419,12 @@ int ASSIMPWriter::write(const std::string& file_name,
}
++mesh->mNumFaces;
if ( dump_facets ) {
stringstream str;
const auto* id = face.mIndices;
const auto* vv = mesh->mVertices;
TGeoFacet fac(Vertex(vv[id[0]].x,vv[id[0]].y,vv[id[0]].z),
Vertex(vv[id[1]].x,vv[id[1]].y,vv[id[1]].z),
Vertex(vv[id[2]].x,vv[id[2]].y,vv[id[2]].z));
str << fac;
ROOT::Geom::Vertex_t v1(vv[id[0]].x, vv[id[0]].y, vv[id[0]].z);
ROOT::Geom::Vertex_t v2(vv[id[1]].x, vv[id[1]].y, vv[id[1]].z);
ROOT::Geom::Vertex_t v3(vv[id[2]].x, vv[id[2]].y, vv[id[2]].z);
std::stringstream str = dd4hep::cad::streamVertices(v1, v2, v3);
printout(ALWAYS,"ASSIMPWriter","++ Facet %4ld : %s", j, str.str().c_str());
}
}
Expand Down
2 changes: 1 addition & 1 deletion DDCore/src/ShapeUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ namespace dd4hep {
const TGeoFacet& f = sh->GetFacet(i);
pars.emplace_back(double(f.GetNvert()));
for(int j=0, n=f.GetNvert(); j<n; ++j) {
#if ROOT_VERSION_CODE > ROOT_VERSION(6,31,1)
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
int idx = f[j];
pars.emplace_back(double(idx));
#else
Expand Down
7 changes: 6 additions & 1 deletion DDCore/src/plugins/DetectorChecksum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,12 @@ const DetectorChecksum::entry_t& DetectorChecksum::handleSolid(Solid solid) cons
except("DetectorChecksum","+++ TGeoTessellated volume with unsupported number of vertices: %s", solid.name());
}
for (int ivertex = 0; ivertex < facet.GetNvert(); ivertex++) {
log << " vertex" << ivertex + 1 << "=\"" << nam << "_v" << facet.GetVertexIndex(ivertex) << "\"";
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
auto vertexIndex = facet[ivertex];
#else
auto vertexIndex = facet.GetVertexIndex(ivertex)
#endif
log << " vertex" << ivertex + 1 << "=\"" << nam << "_v" << vertexIndex << "\"";
}
log << " type=\"ABSOLUTE\"/>" << newline;
}
Expand Down
10 changes: 10 additions & 0 deletions DDG4/src/Geant4ShapeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,15 @@ namespace dd4hep {
for(int i=0; i<num_facet; ++i) {
const TGeoFacet& facet = sh->GetFacet(i);
int nv = facet.GetNvert();
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
const auto& v0 = sh->GetVertex(facet[0]);
const auto& v1 = sh->GetVertex(facet[1]);
const auto& v2 = sh->GetVertex(facet[2]);
#else
const auto& v0 = sh->GetVertex(facet.GetVertexIndex(0));
const auto& v1 = sh->GetVertex(facet.GetVertexIndex(1));
const auto& v2 = sh->GetVertex(facet.GetVertexIndex(2));
#endif
G4VFacet* g4f = 0;
if ( nv == 3 ) {
g4f = new G4TriangularFacet(G4ThreeVector(v0.x() * CM_2_MM, v0.y() * CM_2_MM, v0.z() * CM_2_MM),
Expand All @@ -270,7 +276,11 @@ namespace dd4hep {
ABSOLUTE);
}
else if ( nv == 4 ) {
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
const auto& v3 = sh->GetVertex(facet[3]);
#else
const auto& v3 = sh->GetVertex(facet.GetVertexIndex(3));
#endif
g4f = new G4QuadrangularFacet(G4ThreeVector(v0.x() * CM_2_MM, v0.y() * CM_2_MM, v0.z() * CM_2_MM),
G4ThreeVector(v1.x() * CM_2_MM, v1.y() * CM_2_MM, v1.z() * CM_2_MM),
G4ThreeVector(v2.x() * CM_2_MM, v2.y() * CM_2_MM, v2.z() * CM_2_MM),
Expand Down

0 comments on commit e942539

Please sign in to comment.