Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-19 09:23:43

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2020 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 http://mozilla.org/MPL/2.0/.
0008 
0009 template <typename T>
0010 void ObjVisualization3D<T>::vertex(const Vector3& vtx, ColorRGB color) {
0011   m_vertexColors[m_vertices.size()] = color;
0012   m_vertices.push_back(vtx.template cast<ValueType>());
0013 }
0014 
0015 template <typename T>
0016 void ObjVisualization3D<T>::line(const Vector3& a, const Vector3& b,
0017                                  ColorRGB color) {
0018   if (color != ColorRGB{0, 0, 0}) {
0019     m_lineColors[m_lines.size()] = color;
0020   }
0021   // not implemented
0022   vertex(a, color);
0023   vertex(b, color);
0024   m_lines.push_back({m_vertices.size() - 2, m_vertices.size() - 1});
0025 }
0026 
0027 template <typename T>
0028 void ObjVisualization3D<T>::face(const std::vector<Vector3>& vtxs,
0029                                  ColorRGB color) {
0030   if (color != ColorRGB{0, 0, 0}) {
0031     m_faceColors[m_faces.size()] = color;
0032   }
0033   FaceType idxs;
0034   idxs.reserve(vtxs.size());
0035   for (const auto& vtx : vtxs) {
0036     vertex(vtx, color);
0037     idxs.push_back(m_vertices.size() - 1);
0038   }
0039   m_faces.push_back(std::move(idxs));
0040 }
0041 
0042 template <typename T>
0043 void ObjVisualization3D<T>::faces(const std::vector<Vector3>& vtxs,
0044                                   const std::vector<FaceType>& faces,
0045                                   ColorRGB color) {
0046   // No faces given - call the face() method
0047   if (faces.empty()) {
0048     face(vtxs, color);
0049   } else {
0050     if (color != ColorRGB{0, 0, 0}) {
0051       m_faceColors[m_faces.size()] = color;
0052     }
0053     auto vtxoffs = m_vertices.size();
0054     if (color != ColorRGB{0, 0, 0}) {
0055       m_vertexColors[m_vertices.size()] = color;
0056     }
0057     m_vertices.insert(m_vertices.end(), vtxs.begin(), vtxs.end());
0058     for (const auto& face : faces) {
0059       if (face.size() == 2) {
0060         m_lines.push_back({face[0] + vtxoffs, face[2] + vtxoffs});
0061       } else {
0062         FaceType rawFace = face;
0063         std::transform(rawFace.begin(), rawFace.end(), rawFace.begin(),
0064                        [&](std::size_t& iv) { return (iv + vtxoffs); });
0065         m_faces.push_back(rawFace);
0066       }
0067     }
0068   }
0069 }
0070 
0071 template <typename T>
0072 void ObjVisualization3D<T>::write(const std::string& path) const {
0073   std::ofstream os;
0074   std::string objectpath = path;
0075   if (!IVisualization3D::hasExtension(objectpath)) {
0076     objectpath += std::string(".obj");
0077   }
0078   os.open(objectpath);
0079   std::string mtlpath = objectpath;
0080   IVisualization3D::replaceExtension(mtlpath, ".mtl");
0081   os << "mtllib " << mtlpath << "\n";
0082   std::ofstream mtlos;
0083   mtlos.open(mtlpath);
0084   write(os, mtlos);
0085   os.close();
0086   mtlos.close();
0087 }
0088 
0089 template <typename T>
0090 void ObjVisualization3D<T>::write(std::ostream& os) const {
0091   std::stringstream sterile;
0092   write(os, sterile);
0093 }
0094 
0095 template <typename T>
0096 void ObjVisualization3D<T>::write(std::ostream& os, std::ostream& mos) const {
0097   std::map<std::string, bool> materials;
0098 
0099   auto mixColor = [&](const ColorRGB& color) -> std::string {
0100     std::string materialName;
0101     materialName = "material_";
0102     materialName += std::to_string(color[0]) + std::string("_");
0103     materialName += std::to_string(color[1]) + std::string("_");
0104     materialName += std::to_string(color[2]);
0105 
0106     if (materials.find(materialName) == materials.end()) {
0107       mos << "newmtl " << materialName << "\n";
0108       std::vector<std::string> shadings = {"Ka", "Kd", "Ks"};
0109       for (const auto& shd : shadings) {
0110         mos << shd << " " << std::to_string(color[0] / 256.) << " ";
0111         mos << std::to_string(color[1] / 256.) << " ";
0112         mos << std::to_string(color[2] / 256.) << " "
0113             << "\n";
0114       }
0115       mos << "\n";
0116     }
0117     return std::string("usemtl ") + materialName;
0118   };
0119 
0120   std::size_t iv = 0;
0121   ColorRGB lastVertexColor = {0, 0, 0};
0122   for (const VertexType& vtx : m_vertices) {
0123     if (m_vertexColors.find(iv) != m_vertexColors.end()) {
0124       auto color = m_vertexColors.find(iv)->second;
0125       if (color != lastVertexColor) {
0126         os << mixColor(color) << "\n";
0127         lastVertexColor = color;
0128       }
0129     }
0130 
0131     os << "v " << std::setprecision(m_outputPrecision)
0132        << m_outputScalor * vtx.x() << " " << m_outputScalor * vtx.y() << " "
0133        << m_outputScalor * vtx.z() << "\n";
0134     ++iv;
0135   }
0136   std::size_t il = 0;
0137   ColorRGB lastLineColor = {0, 0, 0};
0138   for (const LineType& ln : m_lines) {
0139     if (m_lineColors.find(il) != m_lineColors.end()) {
0140       auto color = m_lineColors.find(il)->second;
0141       if (color != lastLineColor) {
0142         os << mixColor(color) << "\n";
0143         lastLineColor = color;
0144       }
0145     }
0146     os << "l " << ln.first + 1 << " " << ln.second + 1 << "\n";
0147     ++il;
0148   }
0149   std::size_t is = 0;
0150   ColorRGB lastFaceColor = {0, 0, 0};
0151   for (const FaceType& fc : m_faces) {
0152     if (m_faceColors.find(is) != m_faceColors.end()) {
0153       auto color = m_faceColors.find(is)->second;
0154       if (color != lastFaceColor) {
0155         os << mixColor(color) << "\n";
0156         lastFaceColor = color;
0157       }
0158     }
0159     os << "f";
0160     for (std::size_t i = 0; i < fc.size(); i++) {
0161       os << " " << fc[i] + 1;
0162     }
0163     os << "\n";
0164     ++is;
0165   }
0166 }
0167 
0168 template <typename T>
0169 void ObjVisualization3D<T>::clear() {
0170   m_vertices.clear();
0171   m_faces.clear();
0172   m_lines.clear();
0173   m_lineColors.clear();
0174   m_vertexColors.clear();
0175   m_faceColors.clear();
0176 }