Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-21 08:08:52

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Visualization/PlyVisualization3D.hpp"
0012 
0013 #include <fstream>
0014 
0015 namespace Acts {
0016 
0017 template <typename T>
0018 void PlyVisualization3D<T>::vertex(const Vector3& vtx, Color color) {
0019   m_vertices.emplace_back(vtx.template cast<ValueType>(), color);
0020 }
0021 
0022 template <typename T>
0023 void PlyVisualization3D<T>::face(const std::vector<Vector3>& vtxs,
0024                                  Color color) {
0025   FaceType idxs;
0026   idxs.reserve(vtxs.size());
0027   for (const auto& vtx : vtxs) {
0028     vertex(vtx, color);
0029     idxs.push_back(m_vertices.size() - 1);
0030   }
0031   m_faces.push_back(std::move(idxs));
0032 }
0033 
0034 template <typename T>
0035 void PlyVisualization3D<T>::faces(const std::vector<Vector3>& vtxs,
0036                                   const std::vector<FaceType>& /*faces*/,
0037                                   Color color) {
0038   face(vtxs, color);
0039 }
0040 
0041 template <typename T>
0042 void PlyVisualization3D<T>::line(const Vector3& a, const Vector3& b,
0043                                  Color color) {
0044   vertex(a, color);
0045   std::size_t idx_a = m_vertices.size() - 1;
0046   vertex(b, color);
0047   std::size_t idx_b = m_vertices.size() - 1;
0048   m_edges.emplace_back(std::make_pair(std::make_pair(idx_a, idx_b), color));
0049 }
0050 
0051 template <typename T>
0052 void PlyVisualization3D<T>::write(const std::filesystem::path& path) const {
0053   std::ofstream os;
0054   std::filesystem::path objectpath = path;
0055   if (!objectpath.has_extension()) {
0056     objectpath.replace_extension(std::filesystem::path("ply"));
0057   }
0058   os.open(std::filesystem::absolute(objectpath).string());
0059   write(os);
0060   os.close();
0061 }
0062 
0063 template <typename T>
0064 void PlyVisualization3D<T>::write(std::ostream& os) const {
0065   os << "ply\n";
0066   os << "format ascii 1.0\n";
0067   os << "element vertex " << m_vertices.size() << "\n";
0068   os << "property float x\n";
0069   os << "property float y\n";
0070   os << "property float z\n";
0071   os << "property uchar red\n";
0072   os << "property uchar green\n";
0073   os << "property uchar blue\n";
0074   os << "element face " << m_faces.size() << "\n";
0075   os << "property list uchar int vertex_index\n";
0076   os << "element edge " << m_edges.size() << "\n";
0077   os << "property int vertex1\n";
0078   os << "property int vertex2\n";
0079   os << "property uchar red\n";
0080   os << "property uchar green\n";
0081   os << "property uchar blue\n";
0082   os << "end_header\n";
0083 
0084   for (const std::pair<VertexType, Color>& vtx : m_vertices) {
0085     os << vtx.first.x() << " " << vtx.first.y() << " " << vtx.first.z() << " ";
0086     os << vtx.second[0] << " " << vtx.second[1] << " " << vtx.second[2] << "\n";
0087   }
0088 
0089   for (const FaceType& fc : m_faces) {
0090     os << fc.size();
0091     for (std::size_t i = 0; i < fc.size(); i++) {
0092       os << " " << fc[i];
0093     }
0094     os << "\n";
0095   }
0096 
0097   for (const std::pair<std::pair<std::size_t, std::size_t>, Color>& edge :
0098        m_edges) {
0099     std::pair<std::size_t, std::size_t> idxs = edge.first;
0100     os << idxs.first << " " << idxs.second << " ";
0101     os << edge.second[0] << " " << edge.second[1] << " " << edge.second[2]
0102        << "\n";
0103   }
0104 }
0105 
0106 template <typename T>
0107 void PlyVisualization3D<T>::clear() {
0108   m_vertices.clear();
0109   m_faces.clear();
0110   m_edges.clear();
0111 }
0112 
0113 }  // namespace Acts