void InitGL() { glClearColor( 1.0,1.0,1.0,1.0 ); glClearDepth(1.0); GLenum err = glewInit(); if (GLEW_OK != err) printf( "%s\n", glewGetErrorString(err) ); if ( GLEW_ARB_vertex_buffer_object ) printf( "VBO extersion is supported!\n"); if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader && GL_EXT_geometry_shader4) printf("Ready for GLSL - vertex, fragment, and geometry units.\n"); else { printf("Not totally ready :( \n"); } glClearColor( 1.0,1.0,1.0,1.0); glClearDepth(1.0); vboVertex = -1; vboList = -1; if(test_mode == 1) initVBO(); else if(test_mode == 2) { if(test_type == 1) DLpoint = createPoint(); else if(test_type == 2) DLedge = createEdge(); else if(test_type == 3) DLtriangle = createTriangle(); } }
void init(Context &ctx) { ctx.program = loadShaderProgram(shaderDir() + "triangle.vert", shaderDir() + "triangle.frag"); createTriangle(ctx); }
int main () { Point v1, v2, v3, currentPoint; InitGraphics(); createTriangle(v1, v2, v3); currentPoint = randPoint(v1, v2, v3); iterate(v1, v2, v3, currentPoint); return 0; }
/** Tesselation "vertex" callback. data points to a buffer that contains two ints: - Vertex index - facevarying variable index */ void onTessVertex(int* data) { // Store the first two vertices... if (v0==0) { v0 = data; return; } if (v1==0) { v1 = data; return; } // Here we have three vertices and can create a triangle... switch(geomtype) { // Individual triangles? case GL_TRIANGLES: createTriangle(v0, v1, data); v0 = v1; v1 = data; return; // Triangle strip? case GL_TRIANGLE_STRIP: if (tricount%2==0) createTriangle(v0, v1, data); else createTriangle(v1, v0, data); v0 = v1; v1 = data; return; // Triangle fan? case GL_TRIANGLE_FAN: createTriangle(v0, v1, data); v1 = data; return; default: throw EValueError("Unknown triangle mode"); }; }
int main(int argc, char** argv) { // start GL context and O/S window using the GLFW helper library if (!glfwInit ()) { fprintf (stderr, "ERROR: could not start GLFW3\n"); return 1; } GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL); if (!window) { fprintf (stderr, "ERROR: could not open window with GLFW3\n"); glfwTerminate(); return 1; } glfwMakeContextCurrent (window); glewInit (); printMachineSpecification(); // tell GL to only draw onto a pixel if the shape is closer to the viewer glEnable (GL_DEPTH_TEST); // enable depth-testing glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer" glShadeModel(GL_SMOOTH); float points[] = { 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f }; GLuint vao; createTriangle(&vao, points); GLuint shader_programme; createShader(&shader_programme, fragment_shader, vertex_shader); while (!glfwWindowShouldClose (window)) { // wipe the drawing surface clear glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawTriangle(vao, shader_programme); // update other events like input handling glfwPollEvents (); // put the stuff we've been drawing onto the display glfwSwapBuffers (window); } glfwTerminate(); return 0; }
///* void PointQueryScene::onTouchEnded(Touch *touch, Event *event) { auto location = touch->getLocation(); auto p = CCRANDOM_0_1(); if (p < 1.0f / 3.0f) { addChild(createBall(location, CCRANDOM_0_1() * 10 + 30)); } else if (p < 2.0f / 3.0f) { addChild(createBox(location, Size(CCRANDOM_0_1() * 20 + 30, CCRANDOM_0_1() * 20 + 30))); } else { addChild(createTriangle(location, Size(CCRANDOM_0_1() * 20 + 30, CCRANDOM_0_1() * 20 + 30))); } }
void construct (int iCPX, bool A_CPX_exists, bool B_CPX_exists, int iA_CPX, int iB_CPX, int iA, int iB, vector<FrontMember>& frontList, vector<Edge>& edges, vector<Triangle>& triangles, TriangleADT& triangleADT, EdgeADT& edgeADT, int newGridId, vector<Point>& points, CircleADT& circleADT, int iFrontEdge) { //int iFrontEdge = frontList.front().edge; if (!A_CPX_exists) { Edge tmpEdge = createEdge (iA, iCPX, newGridId, true); addToEdgeList (tmpEdge, iA, iCPX, edges, edgeADT, points); iA_CPX = edges.size() - 1; addToFrontList (iA_CPX, frontList, points, edges); Point cntPoint; cntPoint.belonging = newGridId; cntPoint.dim = 0.5 * (points[tmpEdge.t[0]].dim + points[tmpEdge.t[1]].dim); //addToPointList (cntPoint, edgeCenters, edgeCenterADT); } else { eraseExistingEdgeFromFrontList (iA_CPX, frontList); } if (!B_CPX_exists) { Edge tmpEdge = createEdge (iB, iCPX, newGridId, true); addToEdgeList (tmpEdge, iB, iCPX, edges, edgeADT, points); iB_CPX = edges.size() - 1; addToFrontList (iB_CPX, frontList, points, edges); Point cntPoint; cntPoint.belonging = newGridId; cntPoint.dim = 0.5 * (points[tmpEdge.t[0]].dim + points[tmpEdge.t[1]].dim); //addToPointList (cntPoint, edgeCenters, edgeCenterADT); } else { eraseExistingEdgeFromFrontList (iB_CPX, frontList); } Triangle tmpTriangle = createTriangle (iFrontEdge, iA_CPX, iB_CPX, edges, points); addToTriangleList (triangles, tmpTriangle, triangleADT, points, circleADT, edges, frontList); eraseFromFrontList (frontList, iFrontEdge); //sortFrontList (frontList, points, edges); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- int main(int argc, char **argv) { if (argc != 2) { std::cout << "Nees 1 argument which is an output filename." << std::endl; } std::string outputFile(argv[1]); size_t width = Detail::TRI_WIDTH; size_t height = Detail::TRI_HEIGHT; unsigned char* image = createTriangle(width, height); int err = writeOutputAsTiff(image, width, height, outputFile, std::string("Crystal Orientation Color Legend")); freeMemory(image); return err; }
void DigiSpeMeasuringPoint::getTriangle(TiXmlElement* elem) { const char* a = elem->Attribute("a"); const char* b = elem->Attribute("b"); const char* c = elem->Attribute("c"); if (!a) { LogManager::getSingleton().logMessage("ERROR: DigiSpe: No 'a' attribute in a Triangle tag!", LML_CRITICAL); return; } if (!b) { LogManager::getSingleton().logMessage("ERROR: DigiSpe: No 'b' attribute in a Triangle tag!", LML_CRITICAL); return; } if (!c) { LogManager::getSingleton().logMessage("ERROR: DigiSpe: No 'c' attribute in a Triangle tag!", LML_CRITICAL); return; } Sample* sa = getSampleByName(a, false); Sample* sb = getSampleByName(b, false); Sample* sc = getSampleByName(c, false); if (!sa || !sb || !sc) { // Save triangle data for a second try (when samples from other measuring points have been imported) TriangleData data; data.a = a; data.b = b; data.c = c; brokenTriangleData.push_back(data); } else createTriangle(sa, sb, sc); }
void QgsColorWheel::paintEvent( QPaintEvent *event ) { Q_UNUSED( event ); QPainter painter( this ); //draw a frame QStyleOptionFrameV3 option = QStyleOptionFrameV3(); option.initFrom( this ); option.state = this->hasFocus() ? QStyle::State_Active : QStyle::State_None; style()->drawPrimitive( QStyle::PE_Frame, &option, &painter ); if ( !mWidgetImage || !mWheelImage || !mTriangleImage ) { createImages( size() ); } //draw everything in an image mWidgetImage->fill( Qt::transparent ); QPainter imagePainter( mWidgetImage ); imagePainter.setRenderHint( QPainter::Antialiasing ); if ( mWheelDirty ) { //need to redraw the wheel image createWheel(); } //draw wheel centered on widget QPointF center = QPointF( width() / 2.0, height() / 2.0 ); imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage ); //draw hue marker int h = hue(); double length = mWheelImage->width() / 2.0; QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() ); hueMarkerLine.setAngle( h ); imagePainter.save(); //use sourceIn mode for nicer antialiasing imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn ); QPen pen; pen.setWidth( 2 ); //adapt pen color for hue pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white ); imagePainter.setPen( pen ); imagePainter.drawLine( hueMarkerLine ); imagePainter.restore(); //draw triangle if ( mTriangleDirty ) { createTriangle(); } imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage ); //draw current color marker double triangleRadius = length - mWheelThickness - 1; //adapted from equations at https://github.com/timjb/colortriangle/blob/master/colortriangle.js by Tim Baumann double lightness = mCurrentColor.lightnessF(); double hueRadians = ( h * M_PI / 180.0 ); double hx = cos( hueRadians ) * triangleRadius; double hy = -sin( hueRadians ) * triangleRadius; double sx = -cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double sy = -sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double vx = -cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double vy = sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double mx = ( sx + vx ) / 2.0; double my = ( sy + vy ) / 2.0; double a = ( 1 - 2.0 * fabs( lightness - 0.5 ) ) * mCurrentColor.hslSaturationF(); double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a; double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a; //adapt pen color for lightness pen.setColor( lightness > 0.7 ? Qt::black : Qt::white ); imagePainter.setPen( pen ); imagePainter.setBrush( Qt::NoBrush ); imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 ); imagePainter.end(); //draw image onto widget painter.drawImage( QPoint( 0, 0 ), *mWidgetImage ); painter.end(); }
// // Retriangulates a hole within the mesh. The hole is specified through an edgeloop(closed sequence of edges). // In addition an optional number of points can be specified which will be included in the triangulation. // void MeshEx::retriangulateHole( std::vector<MeshEx::Edge *> &boundaryEdges, std::map<MeshEx::Vertex *, math::Vec2f> &boundaryVertexProjections, std::vector<std::pair<math::Vec3f, math::Vec2f> > &interiorPoints ) { std::map<Vertex_handle, MeshEx::Vertex*> vertexMap; // used to map cgal vertex_handles to vertices std::vector<MeshEx::Edge *> edges; // this vector will hold all edges which were involved (for faster edge search) // algorithm: // - prepare data // - find all boundary vertices // - prepare CGAL constrained triangulation // - insert boundary vertices into triangulation and build mapping from Triangulation vertices to MeshEx::Vertices // - use the boundary edges as constrained edges // - insert points into triangulation from interiorPoints and build mapping from Triangulation vertices to MeshEx::Vertices // - extract triangulation results // - ? // prepare algorithm ---------------------------------------------------------- /* // obsolete since we get the boundary vertices with the boundaryVertexProjections // find boundary vertices for( std::vector<MeshEx::Edge *>::iterator it = boundaryEdges.begin(); it != boundaryEdges.end(); ++it ) { MeshEx::Edge *e = *it; boundaryVertices.push_back( e->v1 ); boundaryVertices.push_back( e->v2 ); } // remove duplicate entries std::sort( boundaryVertices.begin(), boundaryVertices.end() ); boundaryVertices.erase( std::unique( boundaryVertices.begin(), boundaryVertices.end() ), boundaryVertices.end() ); */ // algorithm ------------------------------------------------------------------ Triangulation t; // constrain triangulation with the boundary edges // iterate over all boundary vertices for( std::map<MeshEx::Vertex *, math::Vec2f>::iterator it = boundaryVertexProjections.begin(); it != boundaryVertexProjections.end(); ++it ) { MeshEx::Vertex *v = it->first; // add boundary vertex to triangulation Vertex_handle vh = t.insert( Point( it->second.x, it->second.y ) ); // we dont need to create the vertex vertexMap[vh] = v; } // iterate over all boundary edges for( std::vector<MeshEx::Edge *>::iterator it = boundaryEdges.begin(); it != boundaryEdges.end(); ++it ) { MeshEx::Edge *e = *it; Vertex_handle v1, v2; bool v1_found = false; bool v2_found = false; // find vertex_handles for the given edge vertices for( std::map<Vertex_handle, MeshEx::Vertex*>::iterator vmit = vertexMap.begin(); vmit != vertexMap.end(); ++vmit ) { if( e->v1 == vmit->second ) { v1 = vmit->first; v1_found = true; } if( e->v2 == vmit->second ) { v2 = vmit->first; v2_found = true; } } // add constrainedge to the triangulation t.insert_constraint( v1, v2 ); // add edge to the list of created/existing edges edges.push_back( e ); } // add additional and optional interior points for( std::vector<std::pair<math::Vec3f, math::Vec2f> >::iterator it = interiorPoints.begin(); it != interiorPoints.end(); ++it ) { // update triangulation Vertex_handle v = t.insert( Point( it->second.x, it->second.y ) ); // insertion may return a vertex which already exists (when the position is the same) if( vertexMap.find( v ) == vertexMap.end() ) // create according MeshEx::Vertex and keep mapping to the CGAL vertices vertexMap[v] = createVertex( it->first ); } // extract results and create triangles ---------------------------------------- // now we have the triangulation of the convex hull of the whole problem, now we // have to find the faces which are inside the polygon - we mark each face with a // segment(inside or outside) property and by finding a face which is adjacent to // a infinite face, we find the segment which is outside // we employ some floodfilling scheme for( Triangulation::Finite_faces_iterator it = t.finite_faces_begin(); it != t.finite_faces_end(); ++it ) // reset info to -1 it->info() = -1; int outsideSegment = -1; findOutsideSegment( t, t.finite_faces_begin(), 0, -1, outsideSegment ); if( outsideSegment == -1 ) printf( "error : outsideSegment not found during triangulation\n" ); for( Triangulation::Finite_faces_iterator it = t.finite_faces_begin(); it != t.finite_faces_end(); ++it ) if( (it->info() == -1) && (!t.is_infinite(it)) ) printf( "triangle not touched!\n" ); // iterate over all faces of the triangulation and create edges/triangles for( Triangulation::Finite_faces_iterator it = t.finite_faces_begin(); it != t.finite_faces_end(); ++it ) { Face_handle fh = it; // we are only interested in interior triangles if( fh->info() == outsideSegment ) continue; MeshEx::Vertex *v0, *v1, *v2; v0 = vertexMap[ fh->vertex(0) ]; v1 = vertexMap[ fh->vertex(1) ]; v2 = vertexMap[ fh->vertex(2) ]; MeshEx::Edge *e0, *e1, *e2; e0 = e1 = e2 = 0; // look for the edges in the edge vector for( std::vector<MeshEx::Edge*>::iterator eit = edges.begin(); eit != edges.end(); ++eit ) { MeshEx::Edge *e = *eit; if( e->contains(v0) && e->contains(v1) ) e0 = e; else if( e->contains(v1) && e->contains(v2) ) e1 = e; else if( e->contains(v2) && e->contains(v0) ) e2 = e; } // create the edges which could not be found if( !e0 ) { e0 = createEdge( v0, v1 ); edges.push_back(e0); } if( !e1 ) { e1 = createEdge( v1, v2 ); edges.push_back(e1); } if( !e2 ) { e2 = createEdge( v2, v0 ); edges.push_back(e2); } // create triangle MeshEx::Triangle *tri = createTriangle( v0, v1, v2, e0, e1, e2 ); } }
// // removes the given vertex and retriangulates the hole which would be made // // The method returns true if the vertex has been removed and false if the vertex has been retained. // bool MeshEx::removeVertexAndReTriangulateNeighbourhood( Vertex *v ) { std::vector<MeshEx::Edge *> boundaryEdges; // boundary edges which form the polygon which has to be triangulated std::vector<MeshEx::Edge *> criticalEdges; // edges which are not only connected to other vertices of the vertexring through boundary edges std::map<MeshEx::Vertex *, EdgeInfoHelper> boundaryRing; // maps incomming and outgoing edge to each vertex of the boundary vertex-ring // gather and prepare information ------------------------------------------------ for( std::vector<Triangle *>::iterator it = v->triangleRing.begin(); it != v->triangleRing.end(); ++it ) { Triangle *t = (*it); Edge *boundaryEdge = t->getOtherEdge( v ); boundaryRing[boundaryEdge->v1].registerEdge( boundaryEdge ); boundaryRing[boundaryEdge->v2].registerEdge( boundaryEdge ); boundaryEdges.push_back( boundaryEdge ); } // align the edges so that for each vertex e1 is the incomming and e2 is the outgoing edge Vertex *first = boundaryRing.begin()->first; Vertex *current = first; Vertex *next = 0; Vertex *prev = 0; do { // we have to be sure that each boundaryRing Vertex has an incomming and outgoing edge if( !boundaryRing[current].e1 || !boundaryRing[current].e2 ) { printf( "error : edge ring around vertex not closed boundary vertex has less than 2 edges (polygon hole?)\n" ); //++g_iterations; return false; } boundaryRing[current].setVertex( current ); next = boundaryRing[current].next; if( boundaryRing[next].e1 != boundaryRing[current].e2 ) boundaryRing[next].swap(); current = next; }while( current != first ); // we have to collect all edges going out from the vertex-ring vertices which are connected to // other vertices of the vertex ring - these will later be used for consistency check #2 (see chapter 4.4 of the paper) // for each vertex of the vertex Ring V for( std::map<Vertex *, EdgeInfoHelper>::iterator it = boundaryRing.begin(); it != boundaryRing.end(); ++it ) { Vertex *rv = it->first; // current vertex of the vertex ring // for each triangle referencing rv... for( std::vector<Triangle *>::iterator tit = rv->triangleRing.begin(); tit != rv->triangleRing.end(); ++tit ) { Triangle *rv_tri = *tit; // ... which doesnt belong to the triangleRing of v if( std::find( v->triangleRing.begin(), v->triangleRing.end(), rv_tri ) == v->triangleRing.end() ) { // collect the edges containing rv... for( size_t i=0; i<3; ++i ) if( rv_tri->e[i]->contains(rv) ) // ...and dont belong to the edgeRing list if( std::find( boundaryEdges.begin(), boundaryEdges.end(), rv_tri->e[i] ) == boundaryEdges.end() ) // store the edge if the node on the other side references another vertex of the vertex-ring if( boundaryRing.find( rv_tri->e[i]->getOtherVertex(rv) ) != boundaryRing.end() ) criticalEdges.push_back( rv_tri->e[i] ); } } } // remove duplicate entries std::sort( criticalEdges.begin(), criticalEdges.end() ); criticalEdges.erase( std::unique(criticalEdges.begin(), criticalEdges.end()), criticalEdges.end() ); // Now we will project the neighbourhood of v onto a plane so that we can employ the greedy // triangulation. For this we will have to find a projection of the neighbourhood of v so that // no edges intersect on that plane. If they would, then the greedy triangulation would introduce // folds which means that the topology would be destroyed. // Looking for a working projection may lead to a number of projection-trials. For the first try // we will take the plane to be the plane defined by the normal of v and (its distance to the origin). // This will do the job most of the time. math::Vec3f normal; // direction of the projection plane math::Vec3f base1; // base vector which builds the 2d-coordinate system math::Vec3f base2; // base vector which builds the 2d-coordinate system float distance; // distance of the plane to the origin CGAL::Orientation boundaryPolygonOrientation; // orientation (clockwise/counterclockwise of the boundary polyon) distance = -math::dotProduct( v->position, normal ); size_t trialCount = 13; // only one trial for now size_t currentTrial = 0; bool success = true; do { // we assume that we will be successfull success = true; switch(currentTrial) { case 0: // first trial: we take the plane defined by the normal of v normal = v->normal; break; // for all other trials we will try one of 12 different directions case 1:normal = math::normalize( math::Vec3f( .8507f, .4472f, .2764f ));break; case 2:normal = math::normalize( math::Vec3f( -.8507f, .4472f, .2764f ));break; case 3:normal = math::normalize( math::Vec3f( .8507f, -.4472f, -.2764f ));break; case 4:normal = math::normalize( math::Vec3f( -.8507f, -.4472f, -.2764f ));break; case 5:normal = math::normalize( math::Vec3f( .5257f, -.4472f, .7236f ));break; case 6:normal = math::normalize( math::Vec3f( .5257f, .4472f, -.7236f ));break; case 7:normal = math::normalize( math::Vec3f( -.5257f, -.4472f, .7236f ));break; case 8:normal = math::normalize( math::Vec3f( -.5257f, .4472f, -.7236f ));break; case 9:normal = math::normalize( math::Vec3f( .0f, .4472f, .8944f ));break; case 10:normal = math::normalize( math::Vec3f( .0f, -1.0f, .0f ));break; case 11:normal = math::normalize( math::Vec3f( .0f, 1.0f, .0f ));break; case 12:normal = math::normalize( math::Vec3f( .0f, -.4472f, -.8944f ));break; }; // compute the basis of the 2d-coordinate system of the plane defined by current normal base1 = math::normalize( math::projectPointOnPlane( normal, distance, boundaryRing.begin()->first->position ) - v->position ); base2 = math::normalize( math::crossProduct( normal, base1 ) ); // project neighbours into the given plane for( std::map<Vertex *, EdgeInfoHelper>::iterator it = boundaryRing.begin(); it != boundaryRing.end(); ++it ) it->second.projected = _get2d( base1, base2, math::projectPointOnPlane( normal, distance, it->first->position ) ); // topologic constistency check #1 (see chapter 4.4 of the paper) // now do the consistency check: test if all edges dont intersect and dont lie over each other // if any edge between the projected vertices intersect -> success = false // test each projected edge of the edge ring against each other edge for( std::vector<Edge *>::iterator it1 = boundaryEdges.begin(); it1 != boundaryEdges.end() - 1; ++it1 ) { for( std::vector<Edge *>::iterator it2 = it1+1; it2 != boundaryEdges.end(); ++it2 ) { Edge *e1 = *it1; Edge *e2 = *it2; math::Vec3f intersectionPoint; // skip if the 2 lines share a common vertex if( e2->contains(e1->v1) || e2->contains(e1->v2) ) continue; math::Vec2f e1_v1_projected = boundaryRing[e1->v1].projected; math::Vec2f e1_v2_projected = boundaryRing[e1->v2].projected; math::Vec2f e2_v1_projected = boundaryRing[e2->v1].projected; math::Vec2f e2_v2_projected = boundaryRing[e2->v2].projected; CGAL::Segment_2<K> line_1( Point( e1_v1_projected.x, e1_v1_projected.y ), Point(e1_v2_projected.x, e1_v2_projected.y) ); CGAL::Segment_2<K> line_2( Point( e2_v1_projected.x, e2_v1_projected.y ), Point(e2_v2_projected.x, e2_v2_projected.y) ); if( CGAL::do_intersect( line_1, line_2 ) ) { success = false; break; } } if( !success ) break; } if( success ) { // // now we do the topology consistency check #2 (see chapter 4.4 of the paper) // this is done by checking all critical edges whether they cross the interior of the // polygon boundary which is formed by the projected vertex ring vertices // to do this we create the 2 polygongs which can be made by using the edge as a divider // the line crosses the interior if both polgons have a counterclockwise orientation // for( std::vector<Edge *>::iterator it = criticalEdges.begin(); it != criticalEdges.end(); ++it ) { Edge *criticalEdge = *it; // setup left polygon // start from v1 and go to v2 Polygon leftPoly; prev = 0; current = criticalEdge->v1; do { leftPoly.push_back( current, boundaryRing[current].projected ); prev = current; current = boundaryRing[current].next; }while( current != criticalEdge->v2 ); leftPoly.push_back( current, boundaryRing[current].projected ); // setup right polygon // start from v2 and go to v1 Polygon rightPoly; prev = 0; current = criticalEdge->v2; do { rightPoly.push_back( current, boundaryRing[current].projected ); prev = current; current = boundaryRing[current].next; }while( current != criticalEdge->v1 ); rightPoly.push_back( current, boundaryRing[current].projected ); // if orientations of both polygons are the same then the critical edge crosses the bounding polygon interior if( leftPoly.orientation() == rightPoly.orientation() ) { printf( "info : unable to triangulate since critical edge(es) would cross the polygon interior - vertex is not removed\n" ); success = false; break; } } } }while( (++currentTrial < trialCount)&&(!success) ); if( !success ) { // we dont remove the vertex since we didnt managed to find a planar // projection of the neighbourhood which would not destroy the topology printf( "info : unable to find planar projection for vertex remove and retriangulation - vertex is not removed\n" ); //++g_iterations; return false; } boundaryPolygonOrientation = CGAL::orientation( Point_3( v->position ), Point_3( v->position + base1 ), Point_3( v->position + base2 ), Point_3( v->position + normal ) ); // execute the result ------------------------------------------------------------ // remove vertex v and the triangle ring around v removeVertex( v ); // compute squared distances // compute the squared distances of all point pairs std::vector<DistanceHelper> sqDistances; // sorted distances of each pair of points for( std::map<Vertex *, EdgeInfoHelper>::iterator it1 = boundaryRing.begin(); it1 != boundaryRing.end(); ++it1 ) for( std::map<Vertex *, EdgeInfoHelper>::iterator it2 = it1; it2 != boundaryRing.end(); ++it2 ) { Vertex *v1 = it1->first; Vertex *v2 = it2->first; if( v1 == v2 ) continue; // we dont want to add pairs, which would build a boundary edge, since those // already exist -> simply check for next or prev vertex in boundaryRing if( (boundaryRing[v1].next == v2)||(boundaryRing[v1].prev == v2) ) continue; sqDistances.push_back( DistanceHelper( it1->first, it2->first ) ); } // sort std::sort( sqDistances.begin(), sqDistances.end() ); // std::list<Polygon *> polygons; // polygons which have to be triangulated std::vector<Edge *> edges( boundaryEdges.begin(), boundaryEdges.end() ); // created edges // create polygon which is made by the boundary edges and put it on polygon list Polygon *boundaryPolygon = new Polygon(); first = boundaryRing.begin()->first; current = first; prev = 0; do { boundaryPolygon->push_back( current, boundaryRing[current].projected ); prev = current; current = boundaryRing[current].next; }while( current != first ); if( boundaryPolygon->orientation() != boundaryPolygonOrientation ) boundaryPolygon->flip(); polygons.push_back( boundaryPolygon ); // for each entry in the sqDistance list // find the polygon on the polygon list which contains h->v1 and h->v2 // check if edge is outside the polygon by checking the orientations of the left and right sides (build left and right polygons) // check if edge intersects with any other existing edge which dont have a point in h->v1 or h->v2 // if checks pass: // create edge -> assign it to the left and right polygons | add it to the list of edges // split the polygon by removing the polygon from polygon list and putting the left and right polygons on the list if it is not a triangle // // if the any polygon remains on the polygon list, throw an error that the triangulation has created a hole in the mesh //std::vector<Edge *> edges( edgeRing.begin(), edgeRing.end() ); // when the mother polygon is already a triangle, then we have sqDistances.size() == 0 // and we have to handle the polygon if( boundaryPolygon->isTriangle() ) { createTriangle( boundaryPolygon->vertices[0], boundaryPolygon->vertices[1], boundaryPolygon->vertices[2], edges ); polygons.clear(); delete boundaryPolygon; //++g_iterations; return true; } for( std::vector<DistanceHelper>::iterator it=sqDistances.begin(); it != sqDistances.end(); ++it ) { DistanceHelper *h = &(*it); Polygon *poly = 0; // polygon which contains both vertices of h std::list<Polygon *>::iterator pit; // find the polygon which contains both vertices of h for( pit = polygons.begin(); pit != polygons.end(); ++pit ) { Polygon *p = *pit; if( p->contains( h->v1 ) && p->contains( h->v2 ) ) { poly = p; break; } } // if no polygon could be found which contains both vertices of h, then // we can skip this one if( !poly ) continue; // test for intersection with all existing edges bool intersection = false; for( std::vector<Edge *>::iterator eit = edges.begin(); eit != edges.end(); ++eit) { Edge *e = (*eit); math::Vec3f intersectionPoint; // ---- test for intersection of the edges projected into 2d plane using cgal ---- // skip if the 2 lines share a common vertex if( e->contains(h->v1) || e->contains(h->v2) ) continue; CGAL::Segment_2<K> line_1( Point( boundaryRing[e->v1].projected.x, boundaryRing[e->v1].projected.y ), Point(boundaryRing[e->v2].projected.x, boundaryRing[e->v2].projected.y) ); CGAL::Segment_2<K> line_2( Point( boundaryRing[h->v1].projected.x, boundaryRing[h->v1].projected.y ), Point(boundaryRing[h->v2].projected.x, boundaryRing[h->v2].projected.y) ); if( CGAL::do_intersect( line_1, line_2 ) ) { intersection = true; break; } } // if there was an intersection, then we can skip this one // since a non-compatible edge would be introduced if( intersection ) continue; // no intersection, get the 2 polygons which would be created from dividing the mother polyon along the edge Polygon *left, *right; left = right = 0; poly->split( left, right, h->v1, h->v2 ); // if the 2 polygons have the same orientation, then we can be sure that // the edge goes through the interior of the polygon if( left->orientation() == right->orientation() ) { // remove the current polygon from the list of polygons polygons.erase( pit ); delete poly; // create the edge h->v1 -> h->v2 Edge *edge = createEdge( h->v1, h->v2 ); edges.push_back( edge ); // and add the left and righ polygon to the list if they are not triangles if( left->isTriangle() ) { // add triangle to the mesh createTriangle( left->vertices[0], left->vertices[1], left->vertices[2], edges ); // remove polygon helper structure delete left; }else polygons.push_back( left ); if( right->isTriangle() ) { // add triangle to the mesh createTriangle( right->vertices[0], right->vertices[1], right->vertices[2], edges ); // remove polygon helper structure delete right; }else polygons.push_back( right ); }else { delete left; delete right; } } if( !polygons.empty() ) printf( "error : hole created during retriangulation\n" ); for( std::list<Polygon *>::iterator pit = polygons.begin(); pit != polygons.end(); ++pit ) delete *pit; polygons.clear(); //++g_iterations; return true; }
Scene createScene(char* filename){ Scene scene = {}; scene.max_depth = 10; scene.num_samples = 100; scene.image = createImage(500, 500); Vector position = {278, 273, -800}; Vector look_at = {278, 273, 0}; Vector up = {0, 1, 0}; scene.camera = createCamera(position, look_at, up, 39, (float)(scene.image.width/scene.image.height)); scene.num_materials = 4; scene.materials = malloc(scene.num_materials * sizeof(Material)); scene.materials[0] = createMaterial(createColor(.95, .25, .25), .6, .4, false); scene.materials[1] = createMaterial(createColor(.25, .95, .25), .6, .4, false); scene.materials[2] = createMaterial(createColor(.95, .95, .85), .6, .4, false); scene.materials[3] = createMaterial(createColor(.9, .9, .9), .6, .4, true); scene.num_triangles = 32; scene.triangles = malloc(scene.num_triangles * sizeof(Triangle)); // Right Wall scene.triangles[0] = createTriangle(&scene.materials[1], createVector(0.0, 0.0, 559.2), createVector(0.0, 0.0, 0.0), createVector(0.0, 548.8, 0.0)); scene.triangles[1] = createTriangle(&scene.materials[1], createVector(0.0, 0.0, 559.2), createVector(0.0, 548.8, 0.0), createVector(0.0, 548.8, 559.2)); // Floor scene.triangles[2] = createTriangle(&scene.materials[2], createVector(556.0, 0.0, 0.0), createVector(0.0, 0.0, 0.0), createVector(0.0, 0.0, 559.2)); scene.triangles[3] = createTriangle(&scene.materials[2], createVector(556.0, 0.0, 0.0), createVector(0.0, 0.0, 559.2), createVector(556.0, 0.0, 559.2)); // Left Wall scene.triangles[4] = createTriangle(&scene.materials[0], createVector(556.0, 0.0, 0.0), createVector(556.0, 0.0, 559.2), createVector(556.0, 548.8, 559.2)); scene.triangles[5] = createTriangle(&scene.materials[0], createVector(556.0, 0.0, 0.0), createVector(556.0, 548.8, 559.2), createVector(556.0, 548.8, 0.0)); // Ceiling scene.triangles[6] = createTriangle(&scene.materials[2], createVector(556.0, 548.8, 0.0), createVector(0.0, 548.8, 559.2), createVector(0.0, 548.8, 0.0)); scene.triangles[7] = createTriangle(&scene.materials[2], createVector(556.0, 548.8, 0.0), createVector(556.0, 548.8, 559.2), createVector(0.0, 548.8, 559.2)); // Backwall scene.triangles[8] = createTriangle(&scene.materials[2], createVector(556.0, 0.0, 559.2), createVector(0.0, 0.0, 559.2), createVector(0.0, 548.8, 559.2)); scene.triangles[9] = createTriangle(&scene.materials[2], createVector(556.0, 0.0, 559.2), createVector(0.0, 548.8, 559.2), createVector(556.0, 548.8, 559.2)); // Short Block scene.triangles[10] = createTriangle(&scene.materials[2], createVector(130.0, 165.0, 65.0), createVector(82.0, 165.0, 225.0), createVector(240.0, 165.0, 272.0)); scene.triangles[11] = createTriangle(&scene.materials[2], createVector(130.0, 165.0, 65.0), createVector(240.0, 165.0, 272.0), createVector(290.0, 165.0, 114.0)); scene.triangles[12] = createTriangle(&scene.materials[2], createVector(290.0, 0.0, 114.0), createVector(290.0, 165.0, 114.0), createVector(240.0, 165.0, 272.0)); scene.triangles[13] = createTriangle(&scene.materials[2], createVector(290.0, 0.0, 114.0), createVector(240.0, 165.0, 272.0), createVector(240.0, 0.0, 272.0)); scene.triangles[14] = createTriangle(&scene.materials[2], createVector(130.0, 0.0, 65.0), createVector(130.0, 165.0, 65.0), createVector(290.0, 165.0, 114.0)); scene.triangles[15] = createTriangle(&scene.materials[2], createVector(130.0, 0.0, 65.0), createVector(290.0, 165.0, 114.0), createVector(290.0, 0.0, 114.0)); scene.triangles[16] = createTriangle(&scene.materials[2], createVector(82.0, 0.0, 225.0), createVector(82.0, 165.0, 225.0), createVector(130.0, 165.0, 65.0)); scene.triangles[17] = createTriangle(&scene.materials[2], createVector(82.0, 0.0, 225.0), createVector(130.0, 165.0, 65.0), createVector(130.0, 0.0, 65.0)); scene.triangles[18] = createTriangle(&scene.materials[2], createVector(240.0, 0.0, 272.0), createVector(240.0, 165.0, 272.0), createVector(82.0, 165.0, 225.0)); scene.triangles[19] = createTriangle(&scene.materials[2], createVector(240.0, 0.0, 272.0), createVector(82.0, 165.0, 225.0), createVector(82.0, 0.0, 225.0)); // Tall Block scene.triangles[20] = createTriangle(&scene.materials[2], createVector(423.0, 330.0, 247.0), createVector(265.0, 330.0, 296.0), createVector(314.0, 330.0, 456.0)); scene.triangles[21] = createTriangle(&scene.materials[2], createVector(423.0, 330.0, 247.0), createVector(314.0, 330.0, 456.0), createVector(472.0, 330.0, 406.0)); scene.triangles[22] = createTriangle(&scene.materials[2], createVector(423.0, 0.0, 247.0), createVector(423.0, 330.0, 247.0), createVector(472.0, 330.0, 406.0)); scene.triangles[23] = createTriangle(&scene.materials[2], createVector(423.0, 0.0, 247.0), createVector(472.0, 330.0, 406.0), createVector(472.0, 0.0, 406.0)); scene.triangles[24] = createTriangle(&scene.materials[2], createVector(472.0, 0.0, 406.0), createVector(472.0, 330.0, 406.0), createVector(314.0, 330.0, 456.0)); scene.triangles[25] = createTriangle(&scene.materials[2], createVector(472.0, 0.0, 406.0), createVector(314.0, 330.0, 456.0), createVector(314.0, 0.0, 456.0)); scene.triangles[26] = createTriangle(&scene.materials[2], createVector(314.0, 0.0, 456.0), createVector(314.0, 330.0, 456.0), createVector(265.0, 330.0, 296.0)); scene.triangles[27] = createTriangle(&scene.materials[2], createVector(314.0, 0.0, 456.0), createVector(265.0, 330.0, 296.0), createVector(265.0, 0.0, 296.0)); scene.triangles[28] = createTriangle(&scene.materials[2], createVector(265.0, 0.0, 296.0), createVector(265.0, 330.0, 296.0), createVector(423.0, 330.0, 247.0)); scene.triangles[29] = createTriangle(&scene.materials[2], createVector(265.0, 0.0, 296.0), createVector(423.0, 330.0, 247.0), createVector(423.0, 0.0, 247.0)); // Area Light scene.triangles[30] = createTriangle(&scene.materials[3], createVector(343.0, 548.7, 227.0), createVector(343.0, 548.7, 332.0), createVector(213.0, 548.7, 332.0)); scene.triangles[31] = createTriangle(&scene.materials[3], createVector(343.0, 548.7, 227.0), createVector(213.0, 548.7, 332.0), createVector(213.0, 548.7, 227.0)); scene.num_lights = 2; scene.lights = malloc(scene.num_lights * sizeof(Triangle)); scene.lights[0] = createTriangle(&scene.materials[3], createVector(343.0, 548.7, 227.0), createVector(343.0, 548.7, 332.0), createVector(213.0, 548.7, 332.0)); scene.lights[1] = createTriangle(&scene.materials[3], createVector(343.0, 548.7, 227.0), createVector(213.0, 548.7, 332.0), createVector(213.0, 548.7, 227.0)); return scene; }
void initialize (GLfloat xa, GLfloat ya, GLfloat xb, GLfloat yb, GLfloat xc, GLfloat yc) { x1 = xa; x2 = xb; x3 = xc; y1 = ya; y2 = yb; y3 = yc; createTriangle(x1, y1, x2, y2, x3, y3); }
// Initializes the rendering. void init(void) { glClearColor(0.0, 0.0, 0.0, 1.0); createTriangleShader(); createTriangle(); }
void AI(void) { //Makes appropriate moves for the computer by analysing certain factors if(won || lost) { return; } working=true; turn=0; strphase="Defense Phase"; PrintPhase(); for(int i=0;i<NCountry;i++) { if(all[i].ownership==1 || all[i].nos==1) continue; int sum=0; for(int j=0;j<all[i].non;j++) { if(all[all[i].neigh[j]].ownership==0) continue; if(getNOppNeighbours(all[i].neigh[j])>all[all[i].neigh[j]].nos+6 ) { multipleAttackAI(all[i].neigh[j]); AI(); return; } if(all[i].nos>all[all[i].neigh[j]].nos+4 && willBeBlocked(i,all[i].neigh[j])==false) { attack(i,all[i].neigh[j]); //attack if no. of soldiers is more by 4 in the attacker country and comp is not getting blocked //after the attack if(aimode==false) { AI(); } return; } sum+=all[all[i].neigh[j]].nos; } //sum contains total number of opponent soldiers in the neighbourhood if(sum!=0 && (((float)all[i].nos)/sum)>=0.5 ) { for(int j=0;j<all[i].non;j++) { if(all[all[i].neigh[j]].ownership==1 && all[i].nos>=all[all[i].neigh[j]].nos && willBeBlocked(i,all[i].neigh[j])==false) { attack(i,all[i].neigh[j]); if(aimode==false) { AI(); } return; } } } } refresh(); strphase="Reinforcement Phase"; PrintPhase(); int reinf=reinforcement(0); // refresh2(); aireinforce(reinf); refresh2(); usleep(100000); //transfers int co,ci; // country pair with maximum sum of no. of opponent soldiers as neighbours of ci -(minus) that for co if(getMaxNCountry(co,ci,1) && co>=0 && co<=42 && ci>=0 && ci<=42 && all[co].nos>1) { // get country pair with maximum (sum of no. of opponent soldiers as neighbours of ci -(minus) that for co) // and transfer maximum possible sodiers from co to ci PrintLeft("Transferring soldiers from " + all[co].name+ " to " + all[ci].name); highlight(co); flush(); usleep(300000); highlight(ci); flush(); usleep(300000); createTriangle(Red,co,ci); flush(); sleep(1); transfer(co,ci,all[co].nos-1); refresh(); } working=false; turn=1; focus=1; c1=-1;c2=-1; strphase="Attack Phase"; PrintLeft("Computer's turn is over. Your chance... "); //being ready for player 1's attack phase showmsg.msg="Computer's Reinforcement Phase is over. ^ It's Your turn...^ Attack Phase Begins "; PrintRein(); showmsg.show(); PrintPhase(); return; }
//-------------------------------------------------------------- void ofxMtlMapping2D::update() { ofxMtlMapping2DControls::mapping2DControls()->update(); // ---- save mapping to xml if(ofxMtlMapping2DControls::mapping2DControls()->saveMapping()) { ofxMtlMapping2DControls::mapping2DControls()->resetSaveMapping(); saveShapesList(); } // ---- load mapping from xml if(ofxMtlMapping2DControls::mapping2DControls()->loadMapping()) { ofxMtlMapping2DControls::mapping2DControls()->resetLoadMapping(); loadShapesList(); } // ---- // Editing or not !? if(!ofxMtlMapping2DControls::mapping2DControls()->editShapes()) return; // ---- // Create a new shape if(ofxMtlMapping2DControls::mapping2DControls()->createNewQuad()) { ofxMtlMapping2DControls::mapping2DControls()->resetCreateNewShape(); createQuad(1020/2, 720/2); return; } if(ofxMtlMapping2DControls::mapping2DControls()->createNewGrid()) { ofxMtlMapping2DControls::mapping2DControls()->resetCreateNewShape(); createGrid(1020/2, 720/2); return; } if(ofxMtlMapping2DControls::mapping2DControls()->createNewTriangle()) { ofxMtlMapping2DControls::mapping2DControls()->resetCreateNewShape(); createTriangle(1020/2, 720/2); return; } if(ofxMtlMapping2DControls::mapping2DControls()->createNewMask()) { ofxMtlMapping2DControls::mapping2DControls()->resetCreateNewShape(); createMask(1020/2, 720/2); return; } // ---- // Selected shape with UI if(ofxMtlMapping2DControls::mapping2DControls()->selectedShapeChanged()) { ofxMtlMapping2DControls::mapping2DControls()->resetSelectedShapeChangedFlag(); list<ofxMtlMapping2DShape*>::iterator it = iteratorForShapeWithId(ofxMtlMapping2DControls::mapping2DControls()->selectedShapeId()); if(it != ofxMtlMapping2DShapes::pmShapes.end()) { ofxMtlMapping2DShape* shape = *it; shape->setAsActiveShape(true); // Put active shape at the top of the list ofxMtlMapping2DShapes::pmShapes.push_front(shape); ofxMtlMapping2DShapes::pmShapes.erase(it); } } // ---- // We changed of mode - Output / Input if(ofxMtlMapping2DControls::mapping2DControls()->mappingModeChanged()) { ofxMtlMapping2DControls::mapping2DControls()->resetMappingChangedFlag(); // ---- OUTPUT MODE if(ofxMtlMapping2DControls::mapping2DControls()->mappingMode() == MAPPING_MODE_OUTPUT) { list<ofxMtlMapping2DShape*>::iterator it; for (it=ofxMtlMapping2DShapes::pmShapes.begin(); it!=ofxMtlMapping2DShapes::pmShapes.end(); it++) { ofxMtlMapping2DShape* shape = *it; shape->enable(); if(shape->inputPolygon) { // If this Shape is textured and has an 'inputPolygon' shape->inputPolygon->setAsIdle(); } } // ---- INPUT MODE } else if (ofxMtlMapping2DControls::mapping2DControls()->mappingMode() == MAPPING_MODE_INPUT) { list<ofxMtlMapping2DShape*>::iterator it; for (it=ofxMtlMapping2DShapes::pmShapes.begin(); it!=ofxMtlMapping2DShapes::pmShapes.end(); it++) { ofxMtlMapping2DShape* shape = *it; shape->setAsIdle(); shape->inputPolygon->enable(); } } } // ---- // Update the Shapes list<ofxMtlMapping2DShape*>::iterator it; for (it=ofxMtlMapping2DShapes::pmShapes.begin(); it!=ofxMtlMapping2DShapes::pmShapes.end(); it++) { ofxMtlMapping2DShape* shape = *it; shape->update(); } }
void LodOutsideMarker::initHull() { mHull.clear(); mHull.reserve(mVertexListOrig.size()); mOutsideData.clear(); OutsideDataList::iterator itOut, itOutEnd; mOutsideData.resize(mVertexListOrig.size()); itOut = mOutsideData.begin(); itOutEnd = mOutsideData.end(); for (; itOut != itOutEnd; itOut++) { // reset output variables itOut->isOuterWallVertex = false; itOut->isInsideHull = false; } // We need to find 4 vertices, which are on the convex hull to start the algorithm. CHVertex* vertex[4] = { NULL, NULL, NULL, NULL }; { // Get 1. vertex: minimum y vertex Real miny = std::numeric_limits<Real>::max(); LodData::VertexList::iterator v, vEnd; v = mVertexListOrig.begin(); vEnd = mVertexListOrig.end(); for (; v != vEnd; v++) { Vector3& pos = v->position; if (pos.y < miny) { miny = pos.y; vertex[0] = &*v; } } assert(vertex[0]); // Vertex not found! } { // Get 2. vertex: furthest from 1. vertex Real maxdist = 0.0; LodData::VertexList::iterator v, vEnd; v = mVertexListOrig.begin(); vEnd = mVertexListOrig.end(); for (; v != vEnd; v++) { Real dist = vertex[0]->position.squaredDistance(v->position); if (dist > maxdist) { maxdist = dist; vertex[1] = &*v; } } assert(vertex[1]); // Vertex not found! } { // Get 3. vertex: furthest from 1. vertex and 2. vertex Real maxdist = 0.0; LodData::VertexList::iterator v, vEnd; v = mVertexListOrig.begin(); vEnd = mVertexListOrig.end(); for (; v != vEnd; v++) { Real dist = getPointToLineSqraredDistance(vertex[0], vertex[1], &*v); if (dist > maxdist) { maxdist = dist; vertex[2] = &*v; } } assert(vertex[2]); // Vertex not found! } { // Get 4. vertex: furthest from 1-2-3 triangle Real maxdist = 0.0f; Plane plane(vertex[0]->position, vertex[1]->position, vertex[2]->position); plane.normalise(); LodData::VertexList::iterator v, vEnd; v = mVertexListOrig.begin(); vEnd = mVertexListOrig.end(); for (; v != vEnd; v++) { Real dist = std::abs(plane.getDistance(v->position)); if (dist > maxdist) { maxdist = dist; vertex[3] = &*v; } } assert(vertex[3]); // Vertex not found! } // Volume should be bigger than 0, so that we can guarantee that the centroid point is inside the hull assert(getTetrahedronVolume(vertex[0], vertex[1], vertex[2], vertex[3]) > mEpsilon); // Centroid = (a + b + c + d) / 4 mCentroid = vertex[0]->position + vertex[1]->position + vertex[2]->position + vertex[3]->position; mCentroid /= 4.0f; // Mark vertices, so that they will not be processed again. getOutsideData(vertex[0])->isInsideHull = true; getOutsideData(vertex[1])->isInsideHull = true; getOutsideData(vertex[2])->isInsideHull = true; getOutsideData(vertex[3])->isInsideHull = true; // Create the triangles createTriangle(vertex[0], vertex[1], vertex[2]); createTriangle(vertex[0], vertex[1], vertex[3]); createTriangle(vertex[0], vertex[2], vertex[3]); createTriangle(vertex[1], vertex[2], vertex[3]); }