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