PeriDyno 1.2.1
Loading...
Searching...
No Matches
stlreader_helper.cpp
Go to the documentation of this file.
1#include "tinyobj_helper.h"
2
3#include "ImageLoader.h"
4
5#include "stl_reader/stl_reader.h"
6
7namespace dyno
8{
9 bool loadTextureMeshFromStl(std::shared_ptr<TextureMesh> texMesh, const FilePath& fullname, bool useToCenter)
10 {
11 //tinyobj::attrib_t attrib;
12 //std::vector<tinyobj::shape_t> shapes;
13 //std::vector<tinyobj::material_t> materials;
14 //std::string err, warn;
15
16 //auto name = fullname;
17 //auto root = name.path().parent_path();
18
19 //bool result = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
20 // name.string().c_str(), root.string().c_str());
21
22 //if (!err.empty()) {
23 // std::cerr << err << std::endl;
24 // return false;
25 //}
26
27 //if (!warn.empty()) {
28 // std::cerr << warn << std::endl;
29 //}
30
31 //std::vector<dyno::Vec3f> vertices;
32 //std::vector<dyno::Vec3f> normals;
33 //std::vector<dyno::Vec2f> texCoords;
34 //std::vector<dyno::uint> shapeIds;
35
36 //std::vector<dyno::Vec3i> pIndex;
37 //std::vector<dyno::Vec3i> nIndex;
38 //std::vector<dyno::Vec3i> tIndex;
39
40 //for (int i = 0; i < attrib.vertices.size(); i += 3) {
41 // vertices.push_back({ attrib.vertices[i], attrib.vertices[i + 1], attrib.vertices[i + 2] });
42 //}
43
44 //for (int i = 0; i < attrib.normals.size(); i += 3) {
45 // normals.push_back({ attrib.normals[i], attrib.normals[i + 1], attrib.normals[i + 2] });
46 //}
47
48 //for (int i = 0; i < attrib.texcoords.size(); i += 2) {
49 // texCoords.push_back({ attrib.texcoords[i], attrib.texcoords[i + 1] });
50 //}
51
52 //shapeIds.resize(vertices.size());
53 //texMesh->shapeIds().reset();
55 //dyno::CArray2D<dyno::Vec4f> texture(1, 1);
56 //texture[0, 0] = dyno::Vec4f(1);
57
58 //auto& tMats = texMesh->materials();
59 //tMats.resize(materials.size());
60
61 //uint mId = 0;
62 //for (const auto& mtl : materials) {
63 // tMats[mId] = std::make_shared<Material>();
64 // //tMats[mId]->ambient = { mtl.ambient[0], mtl.ambient[1], mtl.ambient[2] };
65 // //tMats[mId]->diffuse = { mtl.diffuse[0], mtl.diffuse[1], mtl.diffuse[2] };
66 // //tMats[mId]->specular = { mtl.specular[0], mtl.specular[1], mtl.specular[2] };
67 // //tMats[mId]->roughness = 1.0f - mtl.shininess;
68 // tMats[mId]->baseColor = { mtl.diffuse[0], mtl.diffuse[1], mtl.diffuse[2] };
69
70 // std::shared_ptr<ImageLoader> loader = std::make_shared<ImageLoader>();
71
72 // if (!mtl.diffuse_texname.empty())
73 // {
74 // auto tex_path = (root / mtl.diffuse_texname).string();
75
76 // if (loader->loadImage(tex_path.c_str(), texture))
77 // {
78 // tMats[mId]->texColor.assign(texture);
79 // }
80 // }
81 // if (!mtl.bump_texname.empty())
82 // {
83 // auto tex_path = (root / mtl.bump_texname).string();
84
85 // if (loader->loadImage(tex_path.c_str(), texture))
86 // {
87 // tMats[mId]->texBump.assign(texture);
88 // auto texOpt = mtl.bump_texopt;
89 // tMats[mId]->bumpScale = texOpt.bump_multiplier;
90 // }
91 // }
92
93 // mId++;
94 //}
95
96
97 //auto& tShapes = texMesh->shapes();
98 //tShapes.resize(shapes.size());
99
100 //uint sId = 0;
101
102 //for (const tinyobj::shape_t& shape : shapes) {
103 // // only load triangle mesh...
104 // const auto& mesh = shape.mesh;
105 // tShapes[sId] = std::make_shared<Shape>();
106 // std::vector<TopologyModule::Triangle> vertexIndex;
107 // std::vector<TopologyModule::Triangle> normalIndex;
108 // std::vector<TopologyModule::Triangle> texCoordIndex;
109
110 // if (mesh.material_ids.size() > 0 && mesh.material_ids[0] >= 0)
111 // {
112 // tShapes[sId]->material = tMats[mesh.material_ids[0]];
113 // }
114
115 // Vec3f lo = Vec3f(REAL_MAX);
116 // Vec3f hi = Vec3f(-REAL_MAX);
117 // for (int i = 0; i < mesh.indices.size(); i += 3) {
118 // auto idx0 = mesh.indices[i];
119 // auto idx1 = mesh.indices[i + 1];
120 // auto idx2 = mesh.indices[i + 2];
121
122 // vertexIndex.push_back({ idx0.vertex_index, idx1.vertex_index, idx2.vertex_index });
123 // normalIndex.push_back({ idx0.normal_index, idx1.normal_index, idx2.normal_index });
124 // texCoordIndex.push_back({ idx0.texcoord_index, idx1.texcoord_index, idx2.texcoord_index });
125
126 // lo = lo.minimum(vertices[idx0.vertex_index]);
127 // lo = lo.minimum(vertices[idx1.vertex_index]);
128 // lo = lo.minimum(vertices[idx2.vertex_index]);
129
130 // hi = hi.maximum(vertices[idx0.vertex_index]);
131 // hi = hi.maximum(vertices[idx1.vertex_index]);
132 // hi = hi.maximum(vertices[idx2.vertex_index]);
133
134 // shapeIds[idx0.vertex_index] = sId;
135 // shapeIds[idx1.vertex_index] = sId;
136 // shapeIds[idx2.vertex_index] = sId;
137
138 // }
139 // tShapes[sId]->vertexIndex.assign(vertexIndex);
140 // tShapes[sId]->normalIndex.assign(normalIndex);
141 // tShapes[sId]->texCoordIndex.assign(texCoordIndex);
142
143 // auto shapeCenter = (lo + hi) / 2;
144 // tShapes[sId]->boundingBox = TAlignedBox3D<Real>(lo, hi);
145 // tShapes[sId]->boundingTransform = Transform3f(shapeCenter, Quat1f().toMatrix3x3());
146
147 //
148 // //Move to center
149 // if (useToCenter)
150 // {
151 // std::vector<int> indicator(vertices.size(), 0);
152 // for (int i = 0; i < mesh.indices.size(); i += 3)
153 // {
154 // auto idx0 = mesh.indices[i];
155 // auto idx1 = mesh.indices[i + 1];
156 // auto idx2 = mesh.indices[i + 2];
157
158 // if (indicator[idx0.vertex_index] == 0)
159 // {
160 // vertices[idx0.vertex_index] -= shapeCenter;
161 // indicator[idx0.vertex_index] = 1;
162 // }
163 // if (indicator[idx1.vertex_index] == 0)
164 // {
165 // vertices[idx1.vertex_index] -= shapeCenter;
166 // indicator[idx1.vertex_index] = 1;
167 // }
168 // if (indicator[idx2.vertex_index] == 0)
169 // {
170 // vertices[idx2.vertex_index] -= shapeCenter;
171 // indicator[idx2.vertex_index] = 1;
172 // }
173 // }
174 // }
175 //
176
177 // vertexIndex.clear();
178 // normalIndex.clear();
179 // texCoordIndex.clear();
180
181 // sId++;
182 //}
183
184 //texMesh->vertices().assign(vertices);
185 //texMesh->normals().assign(normals);
186 //texMesh->texCoords().assign(texCoords);
187 //texMesh->shapeIds().assign(shapeIds);
188
190 //if (shapes.size() == 1)
191 //{
192 // texMesh->shapeIds().resize(vertices.size());
193 // texMesh->shapeIds().reset();
194 //}
195
196 //vertices.clear();
197 //normals.clear();
198 //texCoords.clear();
199
200 return true;
201 }
202
203
204 bool loadStl(std::vector<Vec3f>& points, std::vector<TopologyModule::Triangle>& triangles, std::string filename, bool append)
205 {
206 if (!append)
207 {
208 points.clear();
209 triangles.clear();
210 }
211 int offset = append ? points.size() : 0;
212
213
214 std::vector<float> coords;
215 std::vector<float> normals;
216 std::vector<size_t> tris;
217 std::vector<size_t> solids;
218
219 try {
220 stl_reader::ReadStlFile(filename.c_str(), coords, normals, tris, solids);
221 const size_t numTris = tris.size() / 3;
222 }
223 catch (const std::exception& e) {
224 std::cerr << "Error reading STL file: " << e.what() << std::endl;
225 }
226
227 std::cout << "************************ Loading STL ************************ " << std::endl << std::endl;
228 std::cout << " " << " coords size = " << coords.size() << std::endl << std::endl;
229 std::cout << " " << " normal size =" << normals.size() << std::endl << std::endl;
230 std::cout << " " << " tris size = " << tris.size() << std::endl << std::endl;
231 std::cout << " " << " solids size =" << solids.size() << std::endl << std::endl;
232
233 if (solids.size() == 0) { return false; }
234
235 for (int i = 0; i < coords.size() / 3; i++)
236 {
237 points.push_back(Vec3f(coords[3 * i], coords[3 * i + 1], coords[3 * i + 2]));
238 }
239 std::cout << "************************ Loading : f ************************ " << std::endl << std::endl;
240
241 //std::cout << " " << " Triangle " << i << " size =" << myshape[i].mesh.indices.size() / 3 << std::endl << std::endl;
242
243 for (int s = 0; s < tris.size() / 3; s++)
244 {
245 //std::cout << myshape[i].mesh.indices[s].vertex_index <<" " << std::endl;
246
247 triangles.push_back(TopologyModule::Triangle(tris[3 * s] + offset, tris[3 * s + 1] + offset, tris[3 * s + 2] + offset));
248 }
249
250 std::cout << "************************ Loading completed **********************" << std::endl << std::endl;
251 return true;
252
253 }
254
255}
Vector< PointType, 3 > Triangle
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
bool loadStl(std::vector< Vec3f > &points, std::vector< TopologyModule::Triangle > &triangles, std::string filename, bool append)
bool loadTextureMeshFromStl(std::shared_ptr< TextureMesh > texMesh, const FilePath &fullname, bool useToCenter)