void intersectedFaces(vertex_t *v, face_set_t &f) const {
        std::vector<face_t *> intersected_faces;
        std::vector<edge_t *> intersected_edges;
        std::vector<vertex_t *> intersected_vertices;

        collect(v, &intersected_vertices, &intersected_edges, &intersected_faces);

        for (unsigned i = 0; i < intersected_vertices.size(); ++i) {
          facesForVertex(intersected_vertices[i], f);
        }
        for (unsigned i = 0; i < intersected_edges.size(); ++i) {
          facesForEdge(intersected_edges[i], f);
        }
        f.insert(intersected_faces.begin(), intersected_faces.end());
      }
      void intersectedFaces(const carve::poly::Polyhedron::edge_t *e, face_set_t &f) const {
        std::vector<const carve::poly::Polyhedron::face_t *> intersected_faces;
        std::vector<const carve::poly::Polyhedron::edge_t *> intersected_edges;
        std::vector<const carve::poly::Polyhedron::vertex_t *> intersected_vertices;

        collect(e, &intersected_vertices, &intersected_edges, &intersected_faces);

        for (unsigned i = 0; i < intersected_vertices.size(); ++i) {
          facesForVertex(intersected_vertices[i], f);
        }
        for (unsigned i = 0; i < intersected_edges.size(); ++i) {
          facesForEdge(intersected_edges[i], f);
        }
        f.insert(intersected_faces.begin(), intersected_faces.end());
      }
      void facesForObject(const IObj &obj, face_set_t &faces) const {
        switch (obj.obtype) {
        case IObj::OBTYPE_VERTEX:
          facesForVertex(obj.vertex, faces);
          break;

        case IObj::OBTYPE_EDGE:
          facesForEdge(obj.edge, faces);
          break;

        case  IObj::OBTYPE_FACE:
          facesForFace(obj.face, faces);
          break;

        default:
          break;
        }
      }
Example #4
0
	bool Mesh::load(const std::string& path, NormalType nt)
	{
		std::vector<tinyobj::material_t> materials;
		std::vector<tinyobj::shape_t> shapes;
		std::string err;
		LoadObj(shapes, materials, err, path.c_str());
		if (shapes.empty()) return false;
		if (shapes[0].mesh.indices.empty()) return false;
		if (shapes[0].mesh.normals.empty()) nt = FLAT;
		bool hasTexCoord = !shapes[0].mesh.texcoords.empty();
		std::vector<std::vector<int>> facesForVertex(shapes[0].mesh.positions.size() / 3);
		std::vector<std::vector<int>> numInFaceForVertex(shapes[0].mesh.positions.size() / 3);
		for (size_t i = 0; i < shapes[0].mesh.indices.size() / 3; ++i) {
			Vertex a, b, c;
			float *tmp;
			tmp = &shapes[0].mesh.positions[shapes[0].mesh.indices[i * 3] * 3];
			a.position = glm::vec3(tmp[0], tmp[1], tmp[2]);
			tmp = &shapes[0].mesh.positions[shapes[0].mesh.indices[i * 3 + 1] * 3];
			b.position = glm::vec3(tmp[0], tmp[1], tmp[2]);
			tmp = &shapes[0].mesh.positions[shapes[0].mesh.indices[i * 3 + 2] * 3];
			c.position = glm::vec3(tmp[0], tmp[1], tmp[2]);
			if (nt != FLAT) {
				tmp = &shapes[0].mesh.normals[shapes[0].mesh.indices[i * 3] * 3];
				a.normal = glm::normalize(glm::vec3(tmp[0], tmp[1], tmp[2]));
				tmp = &shapes[0].mesh.normals[shapes[0].mesh.indices[i * 3 + 1] * 3];
				b.normal = glm::normalize(glm::vec3(tmp[0], tmp[1], tmp[2]));
				tmp = &shapes[0].mesh.normals[shapes[0].mesh.indices[i * 3 + 2] * 3];
				c.normal = glm::normalize(glm::vec3(tmp[0], tmp[1], tmp[2]));
			}

			if (hasTexCoord) {
				tmp = &shapes[0].mesh.texcoords[shapes[0].mesh.indices[i * 3] * 2];
				a.texCoord = glm::vec2(tmp[0], tmp[1]);
				tmp = &shapes[0].mesh.texcoords[shapes[0].mesh.indices[i * 3 + 1] * 2];
				b.texCoord = glm::vec2(tmp[0], tmp[1]);
				tmp = &shapes[0].mesh.texcoords[shapes[0].mesh.indices[i * 3 + 2] * 2];
				c.texCoord = glm::vec2(tmp[0], tmp[1]);
			}
			m_triangles.push_back(new Triangle(a, b, c, *m_material, hasTexCoord, nt != FLAT));
			if (nt == CONSISTENT) {
				facesForVertex[shapes[0].mesh.indices[i * 3]].push_back(i);
				facesForVertex[shapes[0].mesh.indices[i * 3 + 1]].push_back(i);
				facesForVertex[shapes[0].mesh.indices[i * 3 + 2]].push_back(i);
				numInFaceForVertex[shapes[0].mesh.indices[i * 3]].push_back(0);
				numInFaceForVertex[shapes[0].mesh.indices[i * 3 + 1]].push_back(1);
				numInFaceForVertex[shapes[0].mesh.indices[i * 3 + 2]].push_back(2);
			}
		}

		if (nt == CONSISTENT) {
			for (int i = 0; i < facesForVertex.size(); ++i) {
				float *tmp = &shapes[0].mesh.normals[i * 3];
				glm::vec3 vertNormal(tmp[0], tmp[1], tmp[2]);
				vertNormal = glm::normalize(vertNormal);
				float minCos = 2.0f;
				for (int facenum : facesForVertex[i]) {
					float curCos = glm::dot(vertNormal, m_triangles[facenum]->getFaceNormal());
					if (curCos < minCos) minCos = curCos;
				}
				for (int j = 0; j < facesForVertex[i].size(); ++j) {
					int faceNum = facesForVertex[i][j];
					int vertNum = numInFaceForVertex[i][j];
					Vertex v = m_triangles[faceNum]->getVertex(vertNum);
					v.alpha = glm::acos(minCos) * (1.0f + 0.03632f * (1 - minCos) * (1 - minCos));
					m_triangles[faceNum]->setVertex(vertNum, v);
				}
			}
			for (auto& t:m_triangles) {
				t->commitTransformations();
			}
		}
		return true;
	}