ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ObjVisualization.ipp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ObjVisualization.ipp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 template <typename T>
12  m_vertexColors[m_vertices.size()] = color;
13  m_vertices.push_back(vtx.template cast<ValueType>());
14 }
15 
16 template <typename T>
19  if (color != IVisualization::ColorType{0, 0, 0}) {
20  m_lineColors[m_lines.size()] = color;
21  }
22  // not implemented
23  vertex(a, color);
24  vertex(b, color);
25  m_lines.push_back({m_vertices.size() - 2, m_vertices.size() - 1});
26 }
27 
28 template <typename T>
29 void ObjVisualization<T>::face(const std::vector<Vector3D>& vtxs,
31  if (color != IVisualization::ColorType{0, 0, 0}) {
32  m_faceColors[m_faces.size()] = color;
33  }
34  FaceType idxs;
35  idxs.reserve(vtxs.size());
36  for (const auto& vtx : vtxs) {
37  vertex(vtx, color);
38  idxs.push_back(m_vertices.size() - 1);
39  }
40  m_faces.push_back(std::move(idxs));
41 }
42 
43 template <typename T>
44 void ObjVisualization<T>::faces(const std::vector<Vector3D>& vtxs,
45  const std::vector<FaceType>& faces,
46  ColorType color) {
47  // No faces given - call the face() method
48  if (faces.empty()) {
49  face(vtxs, color);
50  } else {
51  if (color != IVisualization::ColorType{0, 0, 0}) {
52  m_faceColors[m_faces.size()] = color;
53  }
54  auto vtxoffs = m_vertices.size();
55  if (color != IVisualization::ColorType{0, 0, 0}) {
56  m_vertexColors[m_vertices.size()] = color;
57  }
58  m_vertices.insert(m_vertices.end(), vtxs.begin(), vtxs.end());
59  for (const auto& face : faces) {
60  if (face.size() == 2) {
61  m_lines.push_back({face[0] + vtxoffs, face[2] + vtxoffs});
62  } else {
63  FaceType rawFace = face;
64  std::transform(rawFace.begin(), rawFace.end(), rawFace.begin(),
65  [&](size_t& iv) { return (iv + vtxoffs); });
66  m_faces.push_back(rawFace);
67  }
68  }
69  }
70 }
71 
72 template <typename T>
73 void ObjVisualization<T>::write(const std::string& path) const {
74  std::ofstream os;
75  std::string objectpath = path;
76  if (not IVisualization::hasExtension(objectpath)) {
77  objectpath += std::string(".obj");
78  }
79  os.open(objectpath);
80  std::string mtlpath = objectpath;
81  IVisualization::replaceExtension(mtlpath, ".mtl");
82  os << "mtllib " << mtlpath << "\n";
83  std::ofstream mtlos;
84  mtlos.open(mtlpath);
85  write(os, mtlos);
86  os.close();
87  mtlos.close();
88 }
89 
90 template <typename T>
91 void ObjVisualization<T>::write(std::ostream& os) const {
92  std::stringstream sterile;
93  write(os, sterile);
94 }
95 
96 template <typename T>
97 void ObjVisualization<T>::write(std::ostream& os, std::ostream& mos) const {
98  std::map<std::string, bool> materials;
99 
100  auto mixColor = [&](const IVisualization::ColorType& color) -> std::string {
101  std::string materialName;
102  materialName = "material_";
103  materialName += std::to_string(color[0]) + std::string("_");
104  materialName += std::to_string(color[1]) + std::string("_");
105  materialName += std::to_string(color[2]);
106 
107  if (materials.find(materialName) == materials.end()) {
108  mos << "newmtl " << materialName << "\n";
109  std::vector<std::string> shadings = {"Ka", "Kd", "Ks"};
110  for (const auto& shd : shadings) {
111  mos << shd << " " << std::to_string(color[0] / 256.) << " ";
112  mos << std::to_string(color[1] / 256.) << " ";
113  mos << std::to_string(color[2] / 256.) << " "
114  << "\n";
115  }
116  mos << "\n";
117  }
118  return std::string("usemtl ") + materialName;
119  };
120 
121  size_t iv = 0;
122  IVisualization::ColorType lastVertexColor = {0, 0, 0};
123  for (const VertexType& vtx : m_vertices) {
124  if (m_vertexColors.find(iv) != m_vertexColors.end()) {
125  auto color = m_vertexColors.find(iv)->second;
126  if (color != lastVertexColor) {
127  os << mixColor(color) << "\n";
128  lastVertexColor = color;
129  }
130  }
131  os << "v " << vtx.x() << " " << vtx.y() << " " << vtx.z() << "\n";
132  ++iv;
133  }
134  size_t il = 0;
135  IVisualization::ColorType lastLineColor = {0, 0, 0};
136  for (const LineType& ln : m_lines) {
137  if (m_lineColors.find(il) != m_lineColors.end()) {
138  auto color = m_lineColors.find(il)->second;
139  if (color != lastLineColor) {
140  os << mixColor(color) << "\n";
141  lastLineColor = color;
142  }
143  }
144  os << "l " << ln.first + 1 << " " << ln.second + 1 << "\n";
145  ++il;
146  }
147  size_t is = 0;
148  IVisualization::ColorType lastFaceColor = {0, 0, 0};
149  for (const FaceType& fc : m_faces) {
150  if (m_faceColors.find(is) != m_faceColors.end()) {
151  auto color = m_faceColors.find(is)->second;
152  if (color != lastFaceColor) {
153  os << mixColor(color) << "\n";
154  lastFaceColor = color;
155  }
156  }
157  os << "f";
158  for (size_t i = 0; i < fc.size(); i++) {
159  os << " " << fc[i] + 1;
160  }
161  os << "\n";
162  ++is;
163  }
164 }
165 
166 template <typename T>
168  m_vertices.clear();
169  m_faces.clear();
170  m_lines.clear();
171  m_lineColors.clear();
172  m_vertexColors.clear();
173  m_faceColors.clear();
174 }