Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASSIMP: adapt for changes in TGeoTesselated in ROOT #1212

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions DDCAD/include/DDCAD/Utilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//==========================================================================
// 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 <vector>

#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;
}

// Determine if the facet is degenerated by calculating its determinant
inline bool facetIsDegenerated(std::vector<ROOT::Geom::Vertex_t> const& vertices){
andresailer marked this conversation as resolved.
Show resolved Hide resolved
const ROOT::Geom::Vertex_t& v1 = vertices[0];
const ROOT::Geom::Vertex_t& v2 = vertices[1];
const ROOT::Geom::Vertex_t& v3 = vertices[2];
constexpr double epsilon = 1.e-20;
// v1.x v2.x v3.x v1.x v2.x
//
// v1.y v2.y v3.y v1.y v2.y
//
// v1.z v2.z v3.z v1.z v2.z
double det = 0.0
+ v1.x() * v2.y() * v3.z()
+ v2.x() * v3.y() * v1.z()
+ v3.x() * v1.y() * v2.z()
- v1.z() * v2.y() * v3.x()
- v2.z() * v3.y() * v1.x()
- v3.z() * v1.y() * v2.x();
return det < epsilon;
}
}
}
#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
26 changes: 19 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,9 +158,13 @@ namespace {
++nskip;
continue;
}
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,31,1)
bool degenerated = dd4hep::cad::facetIsDegenerated(vertices);
#else
bool degenerated = true;
andresailer marked this conversation as resolved.
Show resolved Hide resolved
TGeoFacet f(&vertices, 3, vv0, vv1, vv2);
f.ComputeNormal(degenerated);
#endif
if ( degenerated ) {
++nskip;
continue;
Expand Down Expand Up @@ -388,7 +393,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 +406,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 +421,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
Loading