bool RigTriangulate(const Rig& rig, std::vector<Rig>& finalTriangles) { Vector2dVector a; for(int i = 0; i < rig.size(); i++) { a.push_back(Vector2d(rig[i].x,rig[i].y)); } Vector2dVector result; bool success = Triangulate::Process(a,result); // print out the results. int tcount = result.size()/3; for (int i=0; i<tcount; i++) { const Vector2d &p1 = result[i*3+0]; const Vector2d &p2 = result[i*3+1]; const Vector2d &p3 = result[i*3+2]; Rig triangle; triangle.push_back(ccp(p1.GetX(),p1.GetY())); triangle.push_back(ccp(p2.GetX(),p2.GetY())); triangle.push_back(ccp(p3.GetX(),p3.GetY())); finalTriangles.push_back(triangle); } return success; }
LDEfloat Triangulate::Area(const Vector2dVector &contour) { int n = contour.size(); LDEfloat A=0.0f; for(int p=n-1,q=0; q<n; p=q++) { A+= contour[p].x*contour[q].y - contour[q].x*contour[p].y; } return A*0.5f; }
double Triangulate::Area(const Vector2dVector &contour) { int n = contour.size(); double A=0.0; for(int p=n-1,q=0; q<n; p=q++) { A+= contour[p].GetX()*contour[q].GetY() - contour[q].GetX()*contour[p].GetY(); } return A*0.5; }
float Triangulate::Area(const Vector2dVector &contour) { int n = (int)contour.size(); float A=0.0f; for(int p=n-1,q=0; q<n; p=q++) { A+= contour[p].GetX()*contour[q].GetY() - contour[q].GetX()*contour[p].GetY(); } return A*0.5f; }
void CPhysics::drawSquare(b2Vec2* points,b2Vec2 center,float angle,int VertexCount, float Camera_Z) { Vector2dVector a; for (int i = 0; i < VertexCount; i++) { a.push_back(Vector2d(points[i].x,points[i].y)); } Vector2dVector result; Triangulate::Process(a,result); int tcount = result.size()/3; glColor3f(0.7,0.3,0.7); //glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(center.x *M2P,center.y*M2P,Camera_Z); glRotatef(angle*180.0/M_PI,0,0,1); glBegin(GL_TRIANGLES); for(int i=0;i<tcount;i++){ if (i == 0) glColor3f(1,0,1); else if (i == 1) glColor3f(1,0,0); else if (i == 2) glColor3f(0,1,0); else if (i == 3) glColor3f(0,0,1); else if (i == 4) glColor3f(0,1,1); else if (i == 5) glColor3f(1,0,1); glVertex2f(result[i*3+0].GetX()*M2P,result[i*3+0].GetY()*M2P); glVertex2f(result[i*3+1].GetX()*M2P,result[i*3+1].GetY()*M2P); glVertex2f(result[i*3+2].GetX()*M2P,result[i*3+2].GetY()*M2P); } glEnd(); glPopMatrix(); glColor3f(1,1,1); }
void ModuleBuildings::addBuildingRoof(Building* building, float height, float elevation){ //create && fill vector a with polygon corners Vector2dVector a; bool first = true; for(auto corner : building->getCorners()) { if (first) { first=false; continue; } a.push_back( Vector2d(corner[0], corner[1])); } Vector2dVector result; // allocate an STL vector to hold the answer. Triangulate::Process(a,result); // Invoke the triangulator to triangulate this polygon. //create roof height = (float)height * Config::get()->BUILDING_FLOOR_HEIGHT + elevation; int tcount = result.size()/3; for (int i=0; i<tcount; i++) { const Vector2d &p1 = result[i*3+0]; const Vector2d &p2 = result[i*3+1]; const Vector2d &p3 = result[i*3+2]; //create triangle for (int j=0; j<3; j++) { r_geo_d->inds->addValue(r_geo_d->inds->size()); r_geo_d->norms->addValue(Vec3f(0, 1, 0)); } r_geo_d->pos->addValue(Vec3f(p1.GetX(), height, p1.GetY())); r_geo_d->pos->addValue(Vec3f(p2.GetX(), height, p2.GetY())); r_geo_d->pos->addValue(Vec3f(p3.GetX(), height, p3.GetY())); //r_geo_d->texs->addValue(Vec2f(p1.GetX()/factor, p1.GetY()/factor)); //use only border color of texture //r_geo_d->texs->addValue(Vec2f(p2.GetX()/factor, p2.GetY()/factor)); //use only border color of texture //r_geo_d->texs->addValue(Vec2f(p3.GetX()/factor, p3.GetY()/factor)); //use only border color of texture float mx = (p1.GetX() + p2.GetX() + p3.GetX() )/3.0; float my = (p1.GetY() + p2.GetY() + p3.GetY() )/3.0; r_geo_d->texs->addValue(convRTC(p1.GetX(), p1.GetY(), Vec2f(mx,my))); //use only border color of texture r_geo_d->texs->addValue(convRTC(p2.GetX(), p2.GetY(), Vec2f(mx,my))); //use only border color of texture r_geo_d->texs->addValue(convRTC(p3.GetX(), p3.GetY(), Vec2f(mx,my))); //use only border color of texture //r_geo_d->texs->addValue(Vec2f(0.0, 0.9)); //use only border color of texture //r_geo_d->texs->addValue(Vec2f(0.1, 0.9)); //use only border color of texture //r_geo_d->texs->addValue(Vec2f(0.0, 1.0)); //use only border color of texture } }
void PRFilledPolygon::setPoints(Vector2dVector &points) { CC_SAFE_FREE(areaTrianglePoints); CC_SAFE_FREE(textureCoordinates); Vector2dVector triangulatedPoints = PRRatcliffTriangulator::triangulateVertices(points); areaTrianglePointCount = triangulatedPoints.size(); areaTrianglePoints = (CCPoint*) malloc(sizeof(CCPoint) * areaTrianglePointCount); textureCoordinates = (CCPoint*) malloc(sizeof(CCPoint) * areaTrianglePointCount); for (int i = 0; i < areaTrianglePointCount; i++) { Vector2d v = (Vector2d)triangulatedPoints.at(i); areaTrianglePoints[i] = CCPointMake(v.GetX(), v.GetY()); } calculateTextureCoordinates(); }
void EasyPolygon::setPoints(Vector2dVector &points) { if (points.size() <= 0) { areaTrianglePointCount = 0; return; } CC_SAFE_FREE(areaTrianglePoints); CC_SAFE_FREE(textureCoordinates); /*Vector2dVector triangulatedPoints = PRRatcliffTriangulator::triangulateVertices(points); areaTrianglePointCount = (int)triangulatedPoints.size(); areaTrianglePoints = (Point*) malloc(sizeof(Point) * areaTrianglePointCount); textureCoordinates = (Point*) malloc(sizeof(Point) * areaTrianglePointCount); for (int i = 0; i < areaTrianglePointCount; i++) { Vector2d v = (Vector2d)triangulatedPoints.at(i); areaTrianglePoints[i] = Point(v.GetX(), v.GetY()); // printf("x = %f, y = %f\n", v.GetX, v.GetY); }*/ areaTrianglePointCount = 6; areaTrianglePoints = (Point*) malloc(sizeof(Point) * areaTrianglePointCount); textureCoordinates = (Point*) malloc(sizeof(Point) * areaTrianglePointCount); Vector2d v = points.at(0); areaTrianglePoints[0] = Point(v.GetX(), v.GetY()); v = points.at(3); areaTrianglePoints[1] = Point(v.GetX(), v.GetY()); v = points.at(2); areaTrianglePoints[2] = Point(v.GetX(), v.GetY()); v = points.at(2); areaTrianglePoints[3] = Point(v.GetX(), v.GetY()); v = points.at(1); areaTrianglePoints[4] = Point(v.GetX(), v.GetY()); v = points.at(0); areaTrianglePoints[5] = Point(v.GetX(), v.GetY()); calculateTextureCoordinates(); }
NS_CC_BEGIN Vector2dVector PRRatcliffTriangulator::triangulateVertices ( Vector2dVector aVertices ) { Vector2dVector* pInputPointsForTriangulation = new Vector2dVector; for ( KDuint uIndex = 0; uIndex < aVertices.size ( ); uIndex++ ) { Vector2d tValue = (Vector2d) aVertices.at ( uIndex ); pInputPointsForTriangulation->push_back ( tValue ); } // Triangulate results Vector2dVector aTriangulatedPoints; Triangulate::Process ( *pInputPointsForTriangulation, aTriangulatedPoints ); delete pInputPointsForTriangulation; return aTriangulatedPoints; }
Trampolim::Trampolim(float x, float y) { moveangle=0.0f; realangle=0.0f; type = O_TRAMPOLIN; pX=x; pY=y; sm = ScreenManager::getInstance(); /**********codigo Box2D***************/ //define o tipo e a posição bodydef.position.Set(x,y); bodydef.type = b2_kinematicBody; //cria o corpo usando as definições body=NULL; b2World * world = ((Level*)sm->getCurrentScreen())->getWorld(); body = world->CreateBody(&bodydef); b2FixtureDef fd; fd.density = 1.0f; fd.friction = 0.3f; fd.restitution = 0.9f; //uso do triangulate.cpp -> pega num poligono e divide em triangulos Vector2dVector a; a.push_back(b2Vec2(-2.0f, 0.0f)); a.push_back(b2Vec2(0.0f, 0.0f)); a.push_back(b2Vec2(2.0f, 0.0f)); a.push_back(b2Vec2(2.0f, 2.0f)); a.push_back(b2Vec2(1.442f, 1.636f)); a.push_back(b2Vec2(1.676f, 1.36f )); a.push_back(b2Vec2(0.0f, 1.251f )); a.push_back(b2Vec2(-0.67f, 1.36f )); a.push_back(b2Vec2(-1.442f, 1.636f)); a.push_back(b2Vec2(-2.0f, 2.0f)); Vector2dVector result; // Invoke the triangulator to triangulate this polygon. Triangulate::Process(a,result); // print out the results. int tcount = result.size()/3; for (int i=0; i<tcount; i++) { b2Vec2 vect[3]; vect[0] = result[i*3+0]; vect[1] = result[i*3+1]; vect[2] = result[i*3+2]; b2PolygonShape shape; shape.Set(vect,3); fd.shape = (b2Shape*)&shape; body->CreateFixture(&fd); printf("Triangle %d => (%f,%f) (%f,%f) (%f,%f)\n",i+1,vect[0].x,vect[0].y,vect[1].x,vect[1].y,vect[2].x,vect[2].y); } //usa o userdata para guardar um ponteiro no objecto body do Box2D (usado nas colisões) body->SetUserData(this); /**************************************/ /***********codigo CLanlib***************/ //criar a sprite sprite = sm->getSprite("trampolim"); sprite->set_linear_filter(true); }
bool Triangulate::Process(const Vector2dVector &contour,Vector2dVector &result) { /* allocate and initialize list of Vertices in polygon */ int n = contour.size(); if ( n < 3 ) return false; int *V = new int[n]; /* we want a counter-clockwise polygon in V */ if ( 0.0 < Area(contour) ) for (int v=0; v<n; v++) V[v] = v; else for(int v=0; v<n; v++) V[v] = (n-1)-v; int nv = n; /* remove nv-2 Vertices, creating 1 triangle every time */ int count = 2*nv; /* error detection */ for(int m=0, v=nv-1; nv>2; ) { /* if we loop, it is probably a non-simple polygon */ if (0 >= (count--)) { //** Triangulate: ERROR - probable bad polygon! return false; } /* three consecutive vertices in current polygon, <u,v,w> */ int u = v ; if (nv <= u) u = 0; /* previous */ v = u+1; if (nv <= v) v = 0; /* new v */ int w = v+1; if (nv <= w) w = 0; /* next */ if ( Snip(contour,u,v,w,nv,V) ) { int a,b,c,s,t; /* true names of the vertices */ a = V[u]; b = V[v]; c = V[w]; /* output Triangle */ result.push_back( contour[a] ); result.push_back( contour[b] ); result.push_back( contour[c] ); m++; /* remove v from remaining polygon */ for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--; /* resest error detection counter */ count = 2*nv; } } delete V; return true; }
bool Triangulate::Process(const Vector2dVector &contour,Vector2dVector &result) { #if defined(PRECISE_TRIANGULATION) std::vector<p2t::Point*> _polyline; for (auto _p : contour){ _polyline.push_back(new p2t::Point(_p.GetX(), _p.GetY())); } p2t::CDT* _cdt = new p2t::CDT(_polyline); _cdt->Triangulate(); std::vector<p2t::Triangle*> _triangles = _cdt->GetTriangles(); for (auto _t : _triangles){ auto _pt1 = _t->GetPoint(0); auto _pt2 = _t->GetPoint(1); auto _pt3 = _t->GetPoint(2); result.push_back(Vector2d(_pt1->x, _pt1->y)); result.push_back(Vector2d(_pt2->x, _pt2->y)); result.push_back(Vector2d(_pt3->x, _pt3->y)); } delete _cdt; for (auto _p : _polyline) delete _p; return true; #else /* allocate and initialize list of Vertices in polygon */ int n = (int)contour.size(); if ( n < 3 ) return false; int *V = new int[n]; /* we want a counter-clockwise polygon in V */ if ( 0.0f < Area(contour) ) for (int v=0; v<n; v++) V[v] = v; else for(int v=0; v<n; v++) V[v] = (n-1)-v; int nv = n; /* remove nv-2 Vertices, creating 1 triangle every time */ int count = 2*nv; /* error detection */ for(int m=0, v=nv-1; nv>2; ) { /* if we loop, it is probably a non-simple polygon */ if (0 >= (count--)) { //** Triangulate: ERROR - probable bad polygon! return false; } /* three consecutive vertices in current polygon, <u,v,w> */ int u = v ; if (nv <= u) u = 0; /* previous */ v = u+1; if (nv <= v) v = 0; /* new v */ int w = v+1; if (nv <= w) w = 0; /* next */ if ( Snip(contour,u,v,w,nv,V) ) { int a,b,c,s,t; /* true names of the vertices */ a = V[u]; b = V[v]; c = V[w]; /* output Triangle */ result.push_back( contour[a] ); result.push_back( contour[b] ); result.push_back( contour[c] ); m++; /* remove v from remaining polygon */ for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--; /* resest error detection counter */ count = 2*nv; } } //Danilo S Carvalho Alteration from delete to delete[] delete[] V; return true; #endif }