void Model::addBall(float radius, size_t step, size_t rstep, const glm::vec3 ¢er) { if (step < 2) { step = 2; // step < 2 => straight line } if (rstep < 3) { rstep = 2 * step; // rstep < 3 => flat polygon } float phi = 0, theta = 0, dPhi = Pi / (step), dTheta = 2.0f * Pi / rstep; step += 1; rstep += 1; reserveVertices((step) * (rstep)); reserveIndices((step) * (rstep) * 2); glm::vec3 n; size_t i, j, k = mVertices.size(); for (i = 0; i < rstep; i++, theta += dTheta) { for (j = 0, phi = 0; j < step; j++, phi += dPhi) { n.x = std::sin(phi)*std::cos(theta); n.y = std::cos(phi); n.z = std::sin(phi)*std::sin(theta); addVertex(Vertex(n, center+n*radius)); size_t p = (i + 1) % rstep; size_t q = (j + 1) % step; addQuad(k+(i*step)+j, k+(p*step)+j, k+(p*step)+q, k+(i*step)+q); } } }
std::vector<VisusIndexedData::VertexDataType>& VisusIndexedData::vertex(IndexDataType i) { if ((i < 0) || (i >= (int)nrOfVertices())) { vwarning("Index out of range: no such vertex. Return first vertex"); // We need to return something in case of a failure. We return hte // first vertex and if there is no such vertex we create one if (nrOfVertices() == 0) reserveVertices(1); return (*mVertices)[0]; } return (*mVertices)[i]; }
void Model::addPlane(const glm::vec3 &a, const glm::vec3 &b, const glm::vec3 &c, const glm::vec4 &texRect) { glm::vec3 normal = normalize(cross(c - b, a - b)); glm::vec3 d = a + c - b; glm::vec2 t0(texRect.x, texRect.y); glm::vec2 t1(texRect.x + texRect.z, texRect.y + texRect.w); switch (mPrimitive) { case GLTriangles: { reserveIndices(6); size_t n = mVertices.size(); addQuad(n+0, n+1, n+2, n+3); reserveVertices(4); addVertex(Vertex(glm::vec2(t1.x,t0.y), normal, c)); addVertex(Vertex(glm::vec2(t1.x,t1.y), normal, d)); addVertex(Vertex(glm::vec2(t0.x,t1.y), normal, a)); addVertex(Vertex(glm::vec2(t0.x,t0.y), normal, b)); break; } } }
/*! * Import surface tasselation from S.T.L. file. STL facet are added at to the * present mesh, i.e. current mesh content is not discarded. Howver no checks * are performed to ensure that no duplicated vertices or cells are created. * * If the input file is a multi-solid ASCII file, all solids will be loaded * and a different PID will be assigned to the PID of the different solids. * * \param[in] stl_name name of stl file * \param[in] isBinary flag for binary (true), of ASCII (false) stl file * \param[in] PIDOffset is the offset for the PID numbering * * \result on output returns an error flag for I/O error */ unsigned short SurfUnstructured::importSTL(const string &stl_name, const bool &isBinary, int PIDOffset) { // ====================================================================== // // VARIABLES DECLARATION // // ====================================================================== // // Parameters const unordered_map<size_t, ElementInfo::Type> ele_type{ {0, ElementInfo::UNDEFINED}, {1, ElementInfo::VERTEX}, {2, ElementInfo::LINE}, {3, ElementInfo::TRIANGLE}, {4, ElementInfo::QUAD} }; // STL Object STLObj STL(stl_name, isBinary); // ====================================================================== // // OPEN STL FILE // // ====================================================================== // STL.open("in"); if (STL.err != 0) { return STL.err; } // ====================================================================== // // LOAD ALL SOLID FROM THE STL FILE // // ====================================================================== // int pid = PIDOffset - 1; while (true) { // ====================================================================== // // LOAD SOLID FROM THE STL FILE // // ====================================================================== // int nVertex = 0; int nSimplex = 0; std::vector<std::array<double, 3>> vertexList; std::vector<std::array<double, 3>> normalList; std::vector<std::vector<int>> connectivityList; STL.loadSolid(nVertex, nSimplex, vertexList, normalList, connectivityList); if (nVertex == 0) { break; } // ====================================================================== // // PID OF THE SOLID // // ====================================================================== // ++pid; // ====================================================================== // // PREPARE MESH FOR DATA IMPORT // // ====================================================================== // reserveVertices(getVertexCount() + nVertex); reserveCells(m_nInternals + m_nGhosts + nSimplex); // ====================================================================== // // ADD VERTICES TO MESH // // ====================================================================== // vector<array<double, 3>>::const_iterator v_, ve_; std::unordered_map<long, long> vertexMap; vertexMap.reserve(nVertex); long v_counter = 0; ve_ = vertexList.cend(); for (v_ = vertexList.cbegin(); v_ != ve_; ++v_) { VertexIterator i_ = addVertex(*v_); vertexMap[v_counter] = i_->getId(); ++v_counter; } //next v_ // ====================================================================== // // ADD CELLS TO MESH // // ====================================================================== // vector<vector<int>>::const_iterator c_, ce_; vector<int>::const_iterator w_, we_; ce_ = connectivityList.cend(); for (c_ = connectivityList.cbegin(); c_ != ce_; ++c_) { // Remap STL connectivity int n_v = c_->size(); std::vector<long> connect(n_v, Vertex::NULL_ID); we_ = c_->cend(); int i = 0; for (w_ = c_->cbegin(); w_ < we_; ++w_) { connect[i] = vertexMap[*w_]; ++i; } //next w_ // Add cell CellIterator cellIterator = addCell(ele_type.at(n_v), true, connect); cellIterator->setPID(pid); } //next c_ // ====================================================================== // // Multi-body STL files are supported only in ASCII mode // // ====================================================================== // if (isBinary) { break; } } // ====================================================================== // // CLOSE STL FILE // // ====================================================================== // STL.close("in"); return 0; }