static void drawVoronoiDual(std::vector<T*> &elements) { glColor4ubv((GLubyte *) & CTX::instance()->color.fg); glEnable(GL_LINE_STIPPLE); glLineStipple(1, 0x0F0F); gl2psEnable(GL2PS_LINE_STIPPLE); glBegin(GL_LINES); for(unsigned int i = 0; i < elements.size(); i++){ T *ele = elements[i]; if(!isElementVisible(ele)) continue; SPoint3 pc = ele->circumcenter(); if(ele->getDim() == 2){ for(int j = 0; j < ele->getNumEdges(); j++){ MEdge e = ele->getEdge(j); SVector3 p2p1(e.getVertex(1)->x() - e.getVertex(0)->x(), e.getVertex(1)->y() - e.getVertex(0)->y(), e.getVertex(1)->z() - e.getVertex(0)->z()); SVector3 pcp1(pc.x() - e.getVertex(0)->x(), pc.y() - e.getVertex(0)->y(), pc.z() - e.getVertex(0)->z()); double alpha = dot(pcp1,p2p1) / dot(p2p1,p2p1); SPoint3 p((1 - alpha)*e.getVertex(0)->x() + alpha * e.getVertex(1)->x(), (1 - alpha)*e.getVertex(0)->y() + alpha * e.getVertex(1)->y(), (1 - alpha)*e.getVertex(0)->z() + alpha * e.getVertex(1)->z()); glVertex3d(pc.x(), pc.y(), pc.z()); glVertex3d(p.x(), p.y(), p.z()); } } else if(ele->getDim() == 3){ for(int j = 0; j < ele->getNumFaces(); j++){ MFace f = ele->getFace(j); SPoint3 p = f.barycenter(); glVertex3d(pc.x(), pc.y(), pc.z()); glVertex3d(p.x(), p.y(), p.z()); for(int k = 0; k < f.getNumVertices(); k++){ MEdge e(f.getVertex(k), (k == f.getNumVertices() - 1) ? f.getVertex(0) : f.getVertex(k + 1)); SPoint3 pe = e.barycenter(); glVertex3d(p.x(), p.y(), p.z()); glVertex3d(pe.x(), pe.y(), pe.z()); } } } } glEnd(); glDisable(GL_LINE_STIPPLE); gl2psDisable(GL2PS_LINE_STIPPLE); }
bool Centerline::cutByDisk(SVector3 &PT, SVector3 &NORM, double &maxRad) { double a = NORM.x(); double b = NORM.y(); double c = NORM.z(); double d = -a * PT.x() - b * PT.y() - c * PT.z(); int maxStep = 20; const double EPS = 0.007; //std::set<MEdge,Less_Edge> allEdges; std::vector<MEdge> allEdges; for(unsigned int i = 0; i < triangles.size(); i++){ for ( unsigned int j= 0; j < 3; j++){ allEdges.push_back(triangles[i]->getEdge(j)); //allEdges.insert(triangles[i]->getEdge(j)); } } std::unique(allEdges.begin(), allEdges.end()); bool closedCut = false; int step = 0; while (!closedCut && step < maxStep){ double rad = 1.1*maxRad+0.05*step*maxRad; std::map<MEdge,MVertex*,Less_Edge> cutEdges; std::vector<MVertex*> cutVertices; std::vector<MTriangle*> newTris; std::set<MEdge,Less_Edge> newCut; cutEdges.clear(); cutVertices.clear(); newTris.clear(); newCut.clear(); // for (std::set<MEdge,Less_Edge>::iterator it = allEdges.begin(); // it != allEdges.end() ; ++it){ // MEdge me = *it; for (unsigned int j = 0; j < allEdges.size(); j++){ MEdge me = allEdges[j]; SVector3 P1(me.getVertex(0)->x(),me.getVertex(0)->y(), me.getVertex(0)->z()); SVector3 P2(me.getVertex(1)->x(),me.getVertex(1)->y(), me.getVertex(1)->z()); double V1 = a * P1.x() + b * P1.y() + c * P1.z() + d; double V2 = a * P2.x() + b * P2.y() + c * P2.z() + d; bool inters = (V1*V2<=0.0) ? true: false; bool inDisk = ((norm(P1-PT) < rad ) || (norm(P2-PT) < rad)) ? true : false; double rdist = -V1/(V2-V1); if (inters && rdist > EPS && rdist < 1.-EPS){ SVector3 PZ = P1+rdist*(P2-P1); if (inDisk){ MVertex *newv = new MVertex (PZ.x(), PZ.y(), PZ.z()); cutEdges.insert(std::make_pair(me,newv)); } } else if (inters && rdist <= EPS && inDisk ) cutVertices.push_back(me.getVertex(0)); else if (inters && rdist >= 1.-EPS && inDisk) cutVertices.push_back(me.getVertex(1)); } for(unsigned int i = 0; i < triangles.size(); i++){ cutTriangle(triangles[i], cutEdges,cutVertices, newTris, newCut); } if (isClosed(newCut)) { triangles.clear(); triangles = newTris; theCut.insert(newCut.begin(),newCut.end()); break; } else { step++; //if (step == maxStep) {printf("no closed cut %d \n", (int)newCut.size()); }; // // triangles = newTris; // // theCut.insert(newCut.begin(),newCut.end()); // char name[256]; // sprintf(name, "myCUT-%d.pos", step); // FILE * f2 = Fopen(name,"w"); // fprintf(f2, "View \"\"{\n"); // std::set<MEdge,Less_Edge>::iterator itp = newCut.begin(); // while (itp != newCut.end()){ // MEdge l = *itp; // fprintf(f2, "SL(%g,%g,%g,%g,%g,%g){%g,%g};\n", // l.getVertex(0)->x(), l.getVertex(0)->y(), l.getVertex(0)->z(), // l.getVertex(1)->x(), l.getVertex(1)->y(), l.getVertex(1)->z(), // 1.0,1.0); // itp++; // } // fprintf(f2,"};\n"); // fclose(f2); } } if (step < maxStep){ //printf("cutByDisk OK step =%d \n", step); return true; } else { //printf("cutByDisk not succeeded \n"); return false; } }
static int getAspectRatio(std::vector<MElement *> &elements, std::vector<std::vector<MEdge> > &boundaries) { double area3D = 0.0; for(unsigned int i = 0; i <elements.size(); ++i){ MElement *t = elements[i]; std::vector<MVertex *> v(3); for(int k = 0; k < 3; k++) v[k] = t->getVertex(k); double p0[3] = {v[0]->x(), v[0]->y(), v[0]->z()}; double p1[3] = {v[1]->x(), v[1]->y(), v[1]->z()}; double p2[3] = {v[2]->x(), v[2]->y(), v[2]->z()}; double a_3D = fabs(triangle_area(p0, p1, p2)); area3D += a_3D; } double tot_length = 0.0; for(unsigned int i = 0; i <boundaries.size(); ++i){ std::vector<MEdge> iBound = boundaries[i]; double iLength = 0.0; for( unsigned int j = 0; j <iBound.size(); ++j){ MVertex *v0 = iBound[j].getVertex(0); MVertex *v1 = iBound[j].getVertex(1); const double length = sqrt((v0->x() - v1->x()) * (v0->x() - v1->x()) + (v0->y() - v1->y()) * (v0->y() - v1->y()) + (v0->z() - v1->z()) * (v0->z() - v1->z())); iLength += length; } tot_length += iLength; } int AR = 1; if (boundaries.size() > 0){ tot_length /= boundaries.size(); AR = (int) ceil(2*3.14*area3D/(tot_length*tot_length)); } //compute AR also with Bounding box std::set<MVertex*> vs; for(unsigned int i = 0; i < elements.size(); i++){ MElement *e = elements[i]; for(int j = 0; j < e->getNumVertices(); j++){ vs.insert(e->getVertex(j)); } } SBoundingBox3d bb; std::vector<SPoint3> vertices; for (std::set<MVertex* >::iterator it = vs.begin(); it != vs.end(); it++){ SPoint3 pt((*it)->x(),(*it)->y(), (*it)->z()); vertices.push_back(pt); bb += pt; } double H = norm(SVector3(bb.max(), bb.min())); //SOrientedBoundingBox obbox = SOrientedBoundingBox::buildOBB(vertices); //double H = obbox.getMaxSize(); double D = H; if (boundaries.size() > 0 ) D = 10e4; for (unsigned int i = 0; i < boundaries.size(); i++){ std::set<MVertex*> vb; std::vector<MEdge> iBound = boundaries[i]; for (unsigned int j = 0; j < iBound.size(); j++){ MEdge e = iBound[j]; vb.insert(e.getVertex(0)); vb.insert(e.getVertex(1)); } std::vector<SPoint3> vBounds; SBoundingBox3d bb; for (std::set<MVertex* >::iterator it = vb.begin(); it != vb.end(); it++){ SPoint3 pt((*it)->x(),(*it)->y(), (*it)->z()); vBounds.push_back(pt); bb +=pt; } double iD = norm(SVector3(bb.max(), bb.min())); D = std::min(D, iD); //SOrientedBoundingBox obboxD = SOrientedBoundingBox::buildOBB(vBounds); //D = std::max(D, obboxD.getMaxSize()); } int AR2 = (int)ceil(H/D); return std::max(AR, AR2); }