void CRichModel::CreateEdgesFromVertsAndFaces() { //printf("vert %d face %d" , GetNumOfVerts() , GetNumOfFaces() ); m_Edges.reserve(2 * (GetNumOfVerts() + GetNumOfFaces() - 2)); map<pair<int, int>, int> pondOfUndeterminedEdges; int szFaces = GetNumOfFaces(); for (int i = 0; i < szFaces; ++i) { int threeIndices[3]; for (int j = 0; j < 3; ++j) { int post = (j + 1) % 3; int pre = (j + 2) % 3; int leftVert = Face(i)[pre]; int rightVert = Face(i)[j]; map<pair<int, int>, int>::const_iterator it = pondOfUndeterminedEdges.find(make_pair(leftVert, rightVert)); if (it != pondOfUndeterminedEdges.end()) { int posInEdgeList = it->second; if (m_Edges[posInEdgeList].indexOfOppositeVert != -1) { printf( "Repeated edges!"); continue; } threeIndices[j] = posInEdgeList; m_Edges[posInEdgeList].indexOfOppositeVert = Face(i)[post]; m_Edges[posInEdgeList].indexOfFrontFace = i; } else { CEdge edge; edge.indexOfLeftVert = leftVert; edge.indexOfRightVert = rightVert; edge.indexOfFrontFace = i; edge.indexOfOppositeVert = Face(i)[post]; edge.indexOfReverseEdge = (int)m_Edges.size() + 1; edge.length = (Vert(leftVert) - Vert(rightVert)).Len(); m_Edges.push_back(edge); pondOfUndeterminedEdges[make_pair(leftVert, rightVert)] = threeIndices[j] = (int)m_Edges.size() - 1; edge.indexOfLeftVert = rightVert; edge.indexOfRightVert = leftVert; edge.indexOfReverseEdge = (int)m_Edges.size() - 1; edge.indexOfOppositeVert = -1; m_Edges.push_back(edge); pondOfUndeterminedEdges[make_pair(rightVert, leftVert)] = (int)m_Edges.size() - 1; } } for (int j = 0; j < 3; ++j) { m_Edges[threeIndices[j]].indexOfLeftEdge = Edge(threeIndices[(j + 2) % 3]).indexOfReverseEdge; m_Edges[threeIndices[j]].indexOfRightEdge = Edge(threeIndices[(j + 1) % 3]).indexOfReverseEdge; } } m_Edges.swap(vector<CEdge>(m_Edges)); }
void CBaseModel::DrawWireframe() const { glPushMatrix(); glBegin(GL_LINES); for(int i = 0; i < (int) GetNumOfFaces();++i){ for(int j = 0; j < 3; ++j){ Vert(Face(i)[j]).Show(); Vert(Face(i)[(j+1)%3]).Show(); } } glEnd(); glPopMatrix(); }
std::vector<int> CPlanarGraph::ExtractDeepestFace(bool flagSmallFaceFirst) { std::vector<int> faceIndices; int deepestFaceIdx = -1; int deepestFaceSize = 0; int smallestFaceIdx = -1; int smallestFaceSize = std::numeric_limits<int>::max(); for ( int i=0; i<GetNumOfFaces(); i++ ) { CGraphFace& face = GetFace(i); std::vector<int>& indices = face.GetIndices(); bool flagAllNodesUnvisited = true; for ( int j=0; j<face.GetFaceSize(); j++ ) { if ( GetNode(indices[j]).GetFlagVisited() == true ) { flagAllNodesUnvisited = false; break; } } if ( flagAllNodesUnvisited == false ) { continue; } if ( face.GetFaceSize() > deepestFaceSize ) { deepestFaceIdx = i; deepestFaceSize = face.GetFaceSize(); } if ( face.GetFaceSize() < smallestFaceSize ) { smallestFaceIdx = i; smallestFaceSize = face.GetFaceSize(); } } if ( deepestFaceIdx >= 0 ) { faceIndices = GetFace(deepestFaceIdx).GetIndices(); } if ( flagSmallFaceFirst == true && smallestFaceIdx >= 0 ) { faceIndices = GetFace(smallestFaceIdx).GetIndices(); } return faceIndices; }
//void CBaseModel::RenderFaces(GLenum mode , const vector<int>& faces , // const map<int,Point2D> // const TexturedFaces& textured_faces ) const //{ // glPushMatrix(); // GLint shadeModel; // glGetIntegerv(GL_SHADE_MODEL, &shadeModel); // // const vector<int>& faces = textured_faces.textured_faces; // const map<int, Point2D> textured_coordinates = textured_faces.texture_coordinate; // for(vector<int>::const_iterator itr = faces.begin(); itr != faces.end();++itr){ // int face_id = *itr; // glBegin(GL_TRIANGLES); // for (int j = 0; j < 3; ++j) // { // CPoint3D pt = Vert(Face(face_id)[j]) + Normal(Face(face_id)[j]) * RateOfNormalShift / m_scale * 1.0; // const CPoint3D &normal = Normal(Face(face_id)[j]); // glNormal3f((float)normal.x, (float)normal.y, (float)normal.z); // const Point2D p_tex = textured_coordinates.find(Face(face_id)[j])->second; // glTexCoord2d( p_tex.x , p_tex.y); // glVertex3f((float)pt.x, (float)pt.y, (float)pt.z); // } // glEnd(); // } // glPopMatrix(); // //} // void CBaseModel::Render(GLenum mode) const { if(mode == GL_SELECT){ glPushMatrix(); for (int i = 0; i < GetNumOfFaces(); ++i) { glPushName(i); glBegin(GL_POLYGON); for (int j = 0; j < 3; ++j) { const CPoint3D &pt = Vert(Face(i)[j]); glVertex3f((float)pt.x, (float)pt.y, (float)pt.z); } glEnd(); glPopName(); } glEnd(); glPopMatrix(); if(false){ glPointSize(8); glPushMatrix(); for(int i = 0; i < (int)GetNumOfVerts();++i){ glPushName(i); glBegin(GL_POINTS); Vert(i).Show(); glEnd(); glPopName(); } glPopMatrix(); } //glPushMatrix(); ////GLint shadeModel; ////glGetIntegerv(GL_SHADE_MODEL, &shadeModel); //for (int i = 0; i < GetNumOfFaces(); ++i) //{ // glPushName(i); // glBegin(GL_POLYGON); // for (int j = 0; j < 3; ++j) // { // const CPoint3D &pt = Vert(Face(i)[j]); // glVertex3f((float)pt.x, (float)pt.y, (float)pt.z); // } // glEnd(); // glPopName(); //} //glPopMatrix(); }else{ //glDisable(GL_LIGHTING); //GLenum mode = GL_RENDER; glPushMatrix(); //glTranslated(-m_center.x, -m_center.y, -m_center.z); //glScaled(m_scale, m_scale, m_scale); GLint shadeModel; double alpha = 0.9; double alpha_small = 0.9; glGetIntegerv(GL_SHADE_MODEL, &shadeModel); for (int i = 0; i < GetNumOfFaces(); ++i) { //if( mode == GL_SELECT ) // glPushName(i); glBegin(GL_TRIANGLES); //glColor3f(1.0,1.0,1.0); for (int j = 0; j < 3; ++j) { //const CPoint3D &pt = Vert(Face(i)[j]); CPoint3D pt = Vert(Face(i)[j]) + Normal(Face(i)[j]) * RateOfNormalShift / m_scale * -1.0; const CPoint3D &normal = Normal(Face(i)[j]); //glTexCoord2d( distance_field[Face(i)[j]], distance_field[Face(i)[j]] ); glNormal3f((float)normal.x, (float)normal.y, (float)normal.z); if (!m_vertex_colors.empty()) { if(m_vertex_colors[Face(i)[j]].Len() < 0.1){ (m_vertex_colors[Face(i)[j]] * alpha_small + color_white * (1-alpha_small)).SetColor(); }else{ (m_vertex_colors[Face(i)[j]] * alpha + color_white * (1-alpha)).SetColor(); } } glVertex3f((float)pt.x, (float)pt.y, (float)pt.z); } glEnd(); //if (mode == GL_SELECT) // glPopName(); } glPopMatrix(); //glEnable(GL_LIGHTING); } }
void CBaseModel::AdjustScaleAndComputeNormalsToVerts() { if (m_Verts.empty()) return; m_NormalsToVerts.resize(m_Verts.size(), CPoint3D(0, 0, 0)); CPoint3D center(0, 0, 0); double sumArea(0); CPoint3D sumNormal(0, 0, 0); double deta(0); for (int i = 0; i < (int)m_Faces.size(); ++i) { CPoint3D normal = VectorCross(Vert(Face(i)[0]), Vert(Face(i)[1]), Vert(Face(i)[2])); double area = normal.Len(); CPoint3D gravity3 = Vert(Face(i)[0]) + Vert(Face(i)[1]) + Vert(Face(i)[2]); center += area * gravity3; sumArea += area; sumNormal += normal; deta += gravity3 ^ normal; normal.x /= area; normal.y /= area; normal.z /= area; for (int j = 0; j < 3; ++j) { m_NormalsToVerts[Face(i)[j]] += normal; } } center /= sumArea * 3; fprintf(stderr,"center %lf %lf %lf\n" , center.x , center.y , center.z ); deta -= 3 * (center ^ sumNormal); if (true)//deta > 0) { for (int i = 0; i < GetNumOfVerts(); ++i) { if (fabs(m_NormalsToVerts[i].x) + fabs(m_NormalsToVerts[i].y) + fabs(m_NormalsToVerts[i].z) >= FLT_EPSILON) { m_NormalsToVerts[i].Normalize(); } } } else { for (int i = 0; i < GetNumOfFaces(); ++i) { int temp = m_Faces[i][0]; m_Faces[i][0] = m_Faces[i][1]; m_Faces[i][1] = temp; } for (int i = 0; i < GetNumOfVerts(); ++i) { if (fabs(m_NormalsToVerts[i].x) + fabs(m_NormalsToVerts[i].y) + fabs(m_NormalsToVerts[i].z) >= FLT_EPSILON) { double len = m_NormalsToVerts[i].Len(); m_NormalsToVerts[i].x /= -len; m_NormalsToVerts[i].y /= -len; m_NormalsToVerts[i].z /= -len; } } } CPoint3D ptUp(m_Verts[0]); CPoint3D ptDown(m_Verts[0]); for (int i = 1; i < GetNumOfVerts(); ++i) { if (m_Verts[i].x > ptUp.x) ptUp.x = m_Verts[i].x; else if (m_Verts[i].x < ptDown.x) ptDown.x = m_Verts[i].x; if (m_Verts[i].y > ptUp.y) ptUp.y = m_Verts[i].y; else if (m_Verts[i].y < ptDown.y) ptDown.y = m_Verts[i].y; if (m_Verts[i].z > ptUp.z) ptUp.z = m_Verts[i].z; else if (m_Verts[i].z < ptDown.z) ptDown.z = m_Verts[i].z; } double maxEdgeLenOfBoundingBox = -1; if (ptUp.x - ptDown.x > maxEdgeLenOfBoundingBox) maxEdgeLenOfBoundingBox = ptUp.x - ptDown.x; if (ptUp.y - ptDown.y > maxEdgeLenOfBoundingBox) maxEdgeLenOfBoundingBox = ptUp.y - ptDown.y; if (ptUp.z - ptDown.z > maxEdgeLenOfBoundingBox) maxEdgeLenOfBoundingBox = ptUp.z - ptDown.z; m_scale = 2.0 / maxEdgeLenOfBoundingBox; m_center = center; m_ptUp = ptUp; m_ptDown = ptDown; m_ptUp = (m_ptUp - center) * m_scale; m_ptDown = (m_ptUp - m_ptDown) * m_scale; //for (int i = 0; i < (int)m_Verts.size(); ++i) //{ // m_Verts[i] = (m_Verts[i] - center) * m_scale; //} m_scale = 1; m_center = CPoint3D(0, 0, 0); }