12 m_vertexColors[m_vertices.size()] =
color;
13 m_vertices.push_back(vtx.template cast<ValueType>());
20 m_lineColors[m_lines.size()] =
color;
25 m_lines.push_back({m_vertices.size() - 2, m_vertices.size() - 1});
32 m_faceColors[m_faces.size()] =
color;
35 idxs.reserve(vtxs.size());
36 for (
const auto& vtx : vtxs) {
38 idxs.push_back(m_vertices.size() - 1);
40 m_faces.push_back(std::move(idxs));
45 const std::vector<FaceType>& faces,
52 m_faceColors[m_faces.size()] =
color;
54 auto vtxoffs = m_vertices.size();
56 m_vertexColors[m_vertices.size()] =
color;
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});
63 FaceType rawFace = face;
65 [&](
size_t& iv) {
return (iv + vtxoffs); });
66 m_faces.push_back(rawFace);
75 std::string objectpath = path;
76 if (not IVisualization::hasExtension(objectpath)) {
77 objectpath += std::string(
".obj");
80 std::string mtlpath = objectpath;
81 IVisualization::replaceExtension(mtlpath,
".mtl");
82 os <<
"mtllib " << mtlpath <<
"\n";
91 void ObjVisualization<T>::write(std::ostream& os)
const {
92 std::stringstream sterile;
97 void ObjVisualization<T>::write(std::ostream& os, std::ostream& mos)
const {
100 auto mixColor = [&](
const IVisualization::ColorType&
color) -> std::string {
101 std::string materialName;
102 materialName =
"material_";
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) {
118 return std::string(
"usemtl ") + materialName;
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;
131 os <<
"v " << vtx.x() <<
" " << vtx.y() <<
" " << vtx.z() <<
"\n";
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;
144 os <<
"l " << ln.first + 1 <<
" " << ln.second + 1 <<
"\n";
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;
158 for (
size_t i = 0; i < fc.size(); i++) {
159 os <<
" " << fc[i] + 1;
166 template <
typename T>
171 m_lineColors.clear();
172 m_vertexColors.clear();
173 m_faceColors.clear();