Ejemplo n.º 1
0
void Model::setTexCoords(const Vertices& texCoords)
{
	m_texCoords.resize(texCoords.size());
	std::memcpy(m_texCoords.data(), texCoords.data(), texCoords.size() * 2 * sizeof(float));

	if (m_texCoordsVBO)
	{
		m_texCoordsVBO.bind();
		m_texCoordsVBO.write(texCoords);
		m_texCoordsVBO.release();
	}
}
Ejemplo n.º 2
0
void InitVertices( Vertices& vertices, Indices& indices )
{
	assert( 0 == vertices.size() );
	assert( 0 == indices.size() );
	
	const float edgeSize = 100.0f;
	const unsigned nVerticesPerEdge = 201;
	for( unsigned i=0; i<nVerticesPerEdge; ++i )
	{
		for( unsigned j=0; j<nVerticesPerEdge; ++j )
		{
			vertices.push_back(
				Vertex( 0.0f, edgeSize*i/(nVerticesPerEdge-1) - edgeSize/2, edgeSize*j/(nVerticesPerEdge-1) - edgeSize/2,
				1.0f, 0.0f, 0.0f ) );
		}
	}

	for( unsigned i=0; i<nVerticesPerEdge-1; ++i )
	{
		for( unsigned j=0; j<nVerticesPerEdge-1; ++j )
		{
			indices.push_back( (i)*nVerticesPerEdge + (j) );
			indices.push_back( (i+1)*nVerticesPerEdge + (j) );
			indices.push_back( (i+1)*nVerticesPerEdge + (j+1) );

			indices.push_back( (i)*nVerticesPerEdge + (j) );
			indices.push_back( (i+1)*nVerticesPerEdge + (j+1) );
			indices.push_back( (i)*nVerticesPerEdge + (j+1) );
		}
	}
}
Ejemplo n.º 3
0
ShapeEntity::ShapeEntity(const sf::Vector2f& velocity, const sf::Vector2f& acceleration, const sf::Vector2f& maxVelocity, const sf::Vector2f& accValues, const Vertices& vertices)
	:Entity(velocity,acceleration,maxVelocity,accValues),
	shape_(),
	bounds_(){
	if (vertices.size()>0)
		setVertices(vertices);
}
Ejemplo n.º 4
0
    void RenderBridge::drawTexture(std::shared_ptr<const Texture> texture, const Vertices& vertices)
    {
        loadTexture(*texture);
        GLuint textureId = _textures.at(texture.get());

        FloatList texturesBuffer(2*vertices.size());
        FloatList verticesBuffer(3*vertices.size());
        for(const Vertex& vertex : vertices)
        {
            Point p = vertex.position*_transform;
            texturesBuffer.push_back(vertex.texture.x);
            texturesBuffer.push_back(vertex.texture.y);
            verticesBuffer.push_back(p.x);
            verticesBuffer.push_back(p.y);
            verticesBuffer.push_back(0);
        }

        glEnable(GL_TEXTURE_2D);
        glEnableClientState( GL_TEXTURE_COORD_ARRAY );
        glEnableClientState( GL_VERTEX_ARRAY );

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);       

        glBindTexture(GL_TEXTURE_2D, textureId);
        glVertexPointer(3, GL_FLOAT, 0, &verticesBuffer.front() );
        glTexCoordPointer(2, GL_FLOAT, 0, &texturesBuffer.front() );
        glDrawArrays(GL_QUADS, 0, verticesBuffer.size()/3 );

        glDisableClientState( GL_TEXTURE_COORD_ARRAY );
        glDisableClientState( GL_VERTEX_ARRAY );
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_BLEND);
        glFlush();

#ifdef MVCGAME_DEBUG_DRAW
        std::cout << ">>>>" << std::endl;
        std::cout << "GlRenderBridge::drawTexture " << textureId << std::endl;
        std::cout << "vertices " << vertices.size() << std::endl;
        GLenum err = glGetError();
        if(err != GL_NO_ERROR)
        {
            std::cout << "error " << err << std::endl;
        }        
        std::cout << "<<<<" << std::endl;
#endif        
    }
Ejemplo n.º 5
0
Quadrangle2 Quadrangle3::getQuadrangle2(Vertices v, const CoordSystem3& coorSystem) {
	Quadrangle2::Vertices vertices2;
	for(size_t i = 0; i < v.size(); ++i) {
		mCoordSystem.convertTo(v[i]);
		vertices2[i] = {v[i].x(), v[i].y()};
	}
	return vertices2;
}
Ejemplo n.º 6
0
int incorrect_input(std::string error = "")
{
  std::cerr << "Incorrect input file.\n"
	    << "  " << error << "\n";
  std::cerr << "So far: " << vertices.size() << " vertices, "
	    << faces.size() << " faces\n";
  return EXIT_FAILURE;
}
Ejemplo n.º 7
0
    uint32_t DirectXInputLayoutRegistry::create(const Vertices& vertices)
    {
        TB::runtimeCheck(vertices.size() > 0);

        const char* semantics[] = { "POSITION", "NORMAL", "TEXCOORD", "COLOR" };
        const DXGI_FORMAT formats[] = { DXGI_FORMAT(0), DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT, DXGI_FORMAT_R32G32B32A32_FLOAT };

        // Input layout elements
        std::vector<D3D11_INPUT_ELEMENT_DESC> elements;
        for (size_t i = 0; i < vertices.size(); i++)
        {
            const auto& stream = vertices[i];
            elements.push_back({ semantics[(int)stream.semantic], (UINT)stream.usageIndex, formats[stream.elements], (UINT)i, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
        }

        // Hash the memory bytes of the element
        uint32_t id = hash(reinterpret_cast<uint8_t*>(&elements[0]), elements.size() * sizeof(D3D11_INPUT_ELEMENT_DESC));
        auto item = mRegistry.find(id);
        if (item == mRegistry.end())
        {
            std::stringstream fakeVSCode;
            const char* memberTypes[] = { nullptr, "float", "float2", "float3", "float4" };

            // Create a fake vertex shader for the input layout
            fakeVSCode << "struct VSInput";
            fakeVSCode << "{";
            for (size_t i = 0; i < vertices.size(); i++)
            {
                const auto& stream = vertices[i];
                fakeVSCode << memberTypes[stream.elements] << " _" << i << " : " << semantics[(int)stream.semantic] << stream.usageIndex << ";";
            }
            fakeVSCode << "};";

            fakeVSCode << "float4 MainVS(VSInput input) : SV_POSITION { return float4(0, 0, 0, 1); }";

            DirectXShader fakeVS(mRenderer, DataChunk(fakeVSCode.str()), "MainVS", ShaderType::Vertex);

            ComPtr<ID3D11InputLayout> inputLayout;
            HRESULT hr = mRenderer->getDevice()->CreateInputLayout(&elements[0], (UINT)elements.size(), fakeVS.getBlob()->GetBufferPointer(), fakeVS.getBlob()->GetBufferSize(), inputLayout.getInitRef());
            TB::runtimeCheck(hr == S_OK);

            mRegistry.emplace(id, inputLayout);
        }

        return id;
    }
Ejemplo n.º 8
0
void ShapeEntity::setVertices(const Vertices& vertices){
	shape_.setPointCount(vertices.size());
	int i = 0;
	for (auto& v : vertices){
		shape_.setPoint(i, sf::Vector2f(v.x, v.y));
		++i;
	}
	adjustBounds();
	shape_.setTextureRect(static_cast<sf::IntRect>(bounds_));
}
	void expand_dyn(Vertices &R){// diff -> diff with no dyn
		S[level].i1 = S[level].i1 + S[level - 1].i1 - S[level].i2;//diff
		S[level].i2 = S[level - 1].i1;//diff
		while((int)R.size()) {
			if((int)Q.size() + R.back().d > (int)QMAX.size()){
				Q.push_back(R.back().i); Vertices Rp; cut2(R, Rp);
				if((int)Rp.size()){
					if((float)S[level].i1 / ++pk < Tlimit) degree_sort(Rp);//diff
					color_sort(Rp);
					S[level].i1++, level++;//diff
					expand_dyn(Rp);
					level--;//diff
				}
				else if((int)Q.size() > (int)QMAX.size()) QMAX = Q;
				Q.pop_back();
			}
			else return;
			R.pop_back();
		}
	}
Ejemplo n.º 10
0
Plane::Plane( D3D::GraphicDevice &device,
			  const Material& material )
	: device_(device),
	  vertexDeclaration_(device, DefaultVertexDeclaration),
	  vertexBuffer_(device),
	  indexBuffer_(device),
	  shader_(device, L"plane.vsh"),
	  material_(material)
{
	Vertices vertices;
	Indices indices;
	InitVertices( vertices, indices );

	nVertices_ = vertices.size();
	nPrimitives_ = indices.size()/3;
	vertexBuffer_.SetVertices( &vertices[0], vertices.size() );
	indexBuffer_.SetIndices( &indices[0], indices.size() );
	SetViewMatrix( UnityMatrix() );
	SetProjectiveMatrix( UnityMatrix() );
}
Ejemplo n.º 11
0
int ParticleSystem::createSphere(int numParticles, float maxspray, Vertices &vtx, Indices &ind)

{
	int i;
	Vector3f pos;
	Vector3f norm;
	Vector2f texCoord;
	Vector4f colour;
	Vector3f wander;

	vtx.resize(numParticles);
	std::cout << "   the vector's size is: " << vtx.size() << std::endl;
	std::cout << "   the vector's capacity is: " << vtx.capacity() << std::endl;
	std::cout << "   the vector's maximum size is: " << vtx.max_size() << std::endl;

	ind.resize(0);

	srand(time(0));
	float trad = 0.4; // Defines the starting point of the particles
	/* Create a set of points which will be the particles */
	/* This is similar to drawing a torus: we will sample points on the surface of the torus */

	float u, v, w, theta, phi, spray; // Work variables
	for (int i = 0; i < numParticles; i++){
			
		// Randomly select two numbers to define a point on the torus
		u = ((double) rand() / (RAND_MAX));
        v = ((double) rand() / (RAND_MAX));
            
		// Use u and v to define the point on the torus
        theta = u * 2.0f*M_PI;
		phi = v * 2.0f*M_PI;
        norm = Vector3f(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi));

		pos = Vector3f(norm.x*trad, norm.y*trad, norm.z*trad);
		colour = Vector4f(((float)i) / ((float)numParticles), 0.0f, 1.0f - (((float)i) / ((float)numParticles)), 1.0f);
		texCoord = Vector2f(0, 0); //not used for particels
		// Now sample a point on a sphere to define a direction for points to wander around
		u = ((double) rand() / (RAND_MAX));
        v = ((double) rand() / (RAND_MAX));
		w = ((double) rand() / (RAND_MAX));
			
		theta = u * 2*M_PI;
		phi = acos(2.0*v * -1.0);
		spray = maxspray*pow((float) w, (float) (1.0/3.0)); // Cubic root
		wander = Vector3f(spray*sin(theta)*sin(phi), spray*cos(theta)*sin(phi), spray*cos(phi));

		norm = wander;
		vtx[i] = Vertex(pos, colour, norm, texCoord);
	}

	return(0);

}
Ejemplo n.º 12
0
void NavMesh::Grow(Vertices& vertices, float amount) {
    
    std::vector<Vector2> normals;
    normals.resize(vertices.size(), Vector2(0,0));
    for(NavTriangle* t : triangles) {
        for (int i=0; i<3; i++) {
            if (!t->neighbors[i]) {
                int index0 = t->corners[i];
                int index1 = t->corners[i<2 ? i + 1 : 0];
                Vector2 direction = vertices[index1] - vertices[index0];
                Vector2 normal = { -direction.y, direction.x };
                normal.Normalize();
                normals[index0] += normal;
                normals[index1] += normal;
            }
        }
    }

    for (int i=0; i<vertices.size(); i++) {
        vertices[i] -= normals[i].Normalized() * amount;
    }
}
Ejemplo n.º 13
0
 void print()
 {
     std::cout << "MeshData:: " << std::endl << "Vertices : " << std::endl;
     for (int i = 0; i < m_Vertices.size(); i++)
     {
         std::cout << i << ": " << m_Vertices[i] << std::endl;
     }
     std::cout << "Indices & Normals : " << std::endl;
     for (int i = 0; i < m_Faces.size(); i++)
     {
         std::cout << i << ": " << m_Faces[i] << "\t n:" << m_Normals[i] << std::endl;
     }
 };
	void color_sort(Vertices &R){
		int j = 0, maxno = 1, min_k = max((int)QMAX.size() - (int)Q.size() + 1, 1);
		C[1].clear(), C[2].clear();
		for(int i = 0; i < (int)R.size(); i++) {
			int pi = R[i].i, k = 1;
			while(cut1(pi, C[k])) k++;
			if(k > maxno) maxno = k, C[maxno + 1].clear();
			C[k].push_back(pi);
			if(k < min_k) R[j++].i = pi;
		}
		if(j > 0) R[j - 1].d = 0;
		for(int k = min_k; k <= maxno; k++)
			for(int i = 0; i < (int)C[k].size(); i++)
				R[j].i = C[k][i], R[j++].d = k;
	}
Ejemplo n.º 15
0
Sphere::Sphere( float radius, unsigned tesselationLevel, D3D::GraphicDevice device, float freq,
			    const Material& material )
	: device_(device),
	  vertexDeclaration_(device, DefaultVertexDeclaration),
	  vertexBuffer_(device),
	  indexBuffer_(device),
	  shader_(device, L"sphere.vsh"),
	  radius_(radius),
	  tesselationLevel_(tesselationLevel),
	  freq_(freq),
	  material_(material)
{
	Vertices vertices;
	Indices indices;

	InitVertices(tesselationLevel, radius*sqrtf(2), vertices, indices);
	nVertices_ = vertices.size();
	nPrimitives_ = indices.size()/3;
	vertexBuffer_.SetVertices( &vertices[0], vertices.size() );
	indexBuffer_.SetIndices( &indices[0], indices.size() );
	SetPositionMatrix( UnityMatrix() );
	SetViewMatrix( UnityMatrix() );
	SetProjectiveMatrix( UnityMatrix() );
}
Ejemplo n.º 16
0
void MDAL::DriverFlo2D::createMesh( const std::vector<CellCenter> &cells, double half_cell_size )
{
  // Create all Faces from cell centers.
  // Vertexs must be also created, they are not stored in FLO-2D files
  // try to reuse Vertexs already created for other Faces by usage of unique_Vertexs set.
  Faces faces;
  Vertices vertices;
  std::map<Vertex, size_t, VertexCompare> unique_vertices; //vertex -> id
  size_t vertex_idx = 0;

  for ( size_t i = 0; i < cells.size(); ++i )
  {
    Face e( 4 );

    for ( size_t position = 0; position < 4; ++position )
    {
      Vertex n = createVertex( position, half_cell_size, cells[i] );
      const auto iter = unique_vertices.find( n );
      if ( iter == unique_vertices.end() )
      {
        unique_vertices[n] = vertex_idx;
        vertices.push_back( n );
        e[position] = vertex_idx;
        ++vertex_idx;
      }
      else
      {
        e[position] = iter->second;
      }
    }

    faces.push_back( e );
  }

  mMesh.reset(
    new MemoryMesh(
      name(),
      vertices.size(),
      faces.size(),
      4, //maximum quads
      computeExtent( vertices ),
      mDatFileName
    )
  );
  mMesh->faces = faces;
  mMesh->vertices = vertices;
}
Ejemplo n.º 17
0
    void writeToLog(Logging::Log* plog)
    {
        std::stringstream logmessage;

        using std::endl;
        using std::cout;

        logmessage << "MeshData:: " << std::endl << "Vertices : " << std::endl;
        for (int i = 0; i < m_Vertices.size(); i++)
        {
            logmessage << i << ": " << m_Vertices[i] << endl;
        }
        logmessage << "Indices & Normals : " << endl;
        for (int i = 0; i < m_Faces.size(); i++)
        {
            logmessage << i << ": " << m_Faces[i] << "\t n:" << m_Normals[i] << std::endl;
        }
        plog->logMessage(logmessage.str());
    };
Ejemplo n.º 18
0
/**
 * Add all the outgoing edges from the vertex v to the horizon edges.
 * Also, update the conflict graph for the newly added faces.
 */
void addCone(Arrangement &arr, Vertex *v, Edges &horizon, Vertices &vertices)
{
	int n = horizon.size();

	Edges spokes1;
	Edges spokes2;
	for (int i = 0; i < n; ++i) {
		Edge *e0 = horizon[i];

		Edge *e1 = arr.addHalfEdge(e0->tail, NULL, NULL, false);
		e1->id = edge_id++;
		Edge *e2 = arr.addHalfEdge(v, NULL, NULL, false);
		e2->id = edge_id++;
		e1->twin = e2;
		e2->twin = e1;
		spokes1.push_back(e1);
		spokes2.push_back(e2);
	}

	// order the edges around the vertex, i.e. set the next pointer correctly
	for (int i = 0; i < n; ++i) {
		Edge *e = horizon[i];
		e->twin->next = spokes1[(i + 1) % n];
		spokes1[i]->next = e;
		spokes2[i]->next = spokes2[(i - 1 + n) % n];
	}

	// Add the corresponding faces
	for (int i = 0; i < n; ++i) {
		Face *f = new Face;
		arr.faces.push_back(f);
		f->id = face_id++;
		arr.addBoundary(horizon[i], f);

		// Also, update the conflict graph for added faces.
		for (int j = 0; j < vertices.size(); ++j) {
			if (f->visible(vertices[j])) {
				f->visibleVertices.push_back(vertices[j]);
				vertices[j]->visibleFaces.push_back(f);
			}
		}
	}
}
Ejemplo n.º 19
0
bool MDAL::LoaderGdal::initVertices( Vertices &vertices )
{
  Vertex *VertexsPtr = vertices.data();
  unsigned int mXSize = meshGDALDataset()->mXSize;
  unsigned int mYSize = meshGDALDataset()->mYSize;
  const double *mGT = meshGDALDataset()->mGT;

  for ( unsigned int y = 0; y < mYSize; ++y )
  {
    for ( unsigned int x = 0; x < mXSize; ++x, ++VertexsPtr )
    {
      // VertexsPtr->setId(x + mXSize*y);
      VertexsPtr->x = mGT[0] + ( x + 0.5 ) * mGT[1] + ( y + 0.5 ) * mGT[2];
      VertexsPtr->y = mGT[3] + ( x + 0.5 ) * mGT[4] + ( y + 0.5 ) * mGT[5];
      VertexsPtr->z = 0.0;
    }
  }

  BBox extent = computeExtent( vertices );
  // we want to detect situation when there is whole earth represented in dataset
  bool is_longitude_shifted = ( extent.minX >= 0.0 ) &&
                              ( fabs( extent.minX + extent.maxX - 360.0 ) < 1.0 ) &&
                              ( extent.minY >= -90.0 ) &&
                              ( extent.maxX <= 360.0 ) &&
                              ( extent.maxX > 180.0 ) &&
                              ( extent.maxY <= 90.0 );
  if ( is_longitude_shifted )
  {
    for ( Vertices::size_type n = 0; n < vertices.size(); ++n )
    {
      if ( vertices[n].x > 180.0 )
      {
        vertices[n].x -= 360.0;
      }
    }
  }

  return is_longitude_shifted;
}
Ejemplo n.º 20
0
    void setup(size_t& vertex_count, Ogre::Vector3* vertices, size_t& index_count, unsigned long* indices, double scale)
    {
        // If we add more Objects we need an Offset for the Indices
        std::size_t indices_offset = m_Vertices.size();

        Vector3 temp;
        for (int i = 0; i < vertex_count; i++)
        {
            temp << vertices[i].x / (Ogre::Real)scale, vertices[i].y / (Ogre::Real)scale,
                vertices[i].z / (Ogre::Real)scale;
            m_Vertices.push_back(temp);
        }

        for (int i = 0; i < index_count; i += 3)
        {
            Eigen::Matrix<unsigned long, 3, 1> temp;
            temp << (unsigned long)(indices[i] + indices_offset), (unsigned long)(indices[i + 1] + indices_offset),
                (unsigned long)(indices[i + 2] + indices_offset);
            m_Faces.push_back(temp);
            // Calculate the normal to each triangle:
            // m_Normals.push_back(Ogre::Math::calculateBasicFaceNormal(m_Vertices[indices[i]],m_Vertices[indices[i+1]],m_Vertices[indices[i+2]]));
        }
    };
	void init_colors(Vertices &v){
		const int max_degree = v[0].d;
		for(int i = 0; i < (int)v.size(); i++) v[i].d = min(i, max_degree) + 1;
	}
Ejemplo n.º 22
0
int ParticleSystem::createVAO(Shader newShader, Vertices vtx, Indices ind)
{
	int rc = 0;

	GLint location;		// location of the attributes in the shader;

	shader = newShader;

	//create vertex array object
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	//create vertex buffer object
	glGenBuffers(1, &vtxVBO);
	glBindBuffer(GL_ARRAY_BUFFER, vtxVBO);
	glBufferData(GL_ARRAY_BUFFER, vtx.size() * sizeof(Vertex), vtx.data(), GL_STATIC_DRAW);

	//copy the vertex position
	location = glGetAttribLocation(shader.getProgId(), "vtxPos");
	if (location == -1) {
		rc = -1;
		goto err;
	}
	glEnableVertexAttribArray(location);
	glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, pos));

	//copy the vertex color
	location = glGetAttribLocation(shader.getProgId(), "vtxCol");
	//	if (location == -1) {
	//	rc = -2;
	//goto err;
	//}
	glEnableVertexAttribArray(location);
	glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, col));

	//copy the vertex normal
	location = glGetAttribLocation(shader.getProgId(), "vtxNorm");
	//	if (location == -1) {
	//	rc = -2;
	//goto err;
	//}
	glEnableVertexAttribArray(location);
	glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, norm));

	// copy the texture coords
	location = glGetAttribLocation(shader.getProgId(), "texCoord");
	//	if (location == -1) {
	//	rc = -2;
	//goto err;
	//}
	glEnableVertexAttribArray(location);
	glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord));

	//create index buffer
	glGenBuffers(1, &indVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indVBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, ind.size() * sizeof(GLuint), ind.data(), GL_STATIC_DRAW);
	// store the number of indices
	numIndices = vtx.size();
	//numIndices = ind.size();


	//end creation
	glBindVertexArray(0);

err:
	return(rc);
}
Ejemplo n.º 23
0
void MDAL::Driver3Di::populateFacesAndVertices( Vertices &vertices, Faces &faces )
{
  assert( vertices.empty() );
  size_t faceCount = mDimensions.size( CFDimensions::Face2D );
  faces.resize( faceCount );
  size_t verticesInFace = mDimensions.size( CFDimensions::MaxVerticesInFace );
  size_t arrsize = faceCount * verticesInFace;
  std::map<std::string, size_t> xyToVertex2DId;

  // X coordinate
  int ncidX = mNcFile.getVarId( "Mesh2DContour_x" );
  double fillX = mNcFile.getFillValue( ncidX );
  std::vector<double> faceVerticesX( arrsize );
  if ( nc_get_var_double( mNcFile.handle(), ncidX, faceVerticesX.data() ) ) throw MDAL_Status::Err_UnknownFormat;

  // Y coordinate
  int ncidY = mNcFile.getVarId( "Mesh2DContour_y" );
  double fillY = mNcFile.getFillValue( ncidY );
  std::vector<double> faceVerticesY( arrsize );
  if ( nc_get_var_double( mNcFile.handle(), ncidY, faceVerticesY.data() ) ) throw MDAL_Status::Err_UnknownFormat;

  // now populate create faces and backtrack which vertices
  // are used in multiple faces
  for ( size_t faceId = 0; faceId < faceCount; ++faceId )
  {
    Face face;

    for ( size_t faceVertexId = 0; faceVertexId < verticesInFace; ++faceVertexId )
    {
      size_t arrId = faceId * verticesInFace + faceVertexId;
      Vertex vertex;
      vertex.x = faceVerticesX[arrId];
      vertex.y = faceVerticesY[arrId];
      vertex.z = 0;

      if ( MDAL::equals( vertex.x, fillX ) || MDAL::equals( vertex.y, fillY ) )
        break;


      size_t vertexId;

      std::string key = std::to_string( vertex.x ) + "," + std::to_string( vertex.y );
      const auto it = xyToVertex2DId.find( key );
      if ( it == xyToVertex2DId.end() )
      {
        // new vertex
        vertexId = vertices.size();
        xyToVertex2DId[key] = vertexId;
        vertices.push_back( vertex );
      }
      else
      {
        // existing vertex
        vertexId = it->second;
      }

      face.push_back( vertexId );

    }

    faces[faceId] = face;
  }

  // Only now we have number of vertices, since we identified vertices that
  // are used in multiple faces
  mDimensions.setDimension( CFDimensions::Vertex2D, vertices.size() );
}
Ejemplo n.º 24
0
void ConvertOutlineToTriangles(Vertices &ioOutline,const QuickVec<int> &inSubPolys)
{
   // Order polygons ...
   int subs = inSubPolys.size();
   if (subs<1)
      return;

   QuickVec<SubInfo> subInfo;
   QuickVec<EdgePoint> edges(ioOutline.size());
   int index = 0;
   int groupId = 0;

   for(int sub=0;sub<subs;sub++)
   {
      SubInfo info;

      info.p0 = sub>0?inSubPolys[sub-1]:0;
      info.size = inSubPolys[sub] - info.p0;
      if (ioOutline[info.p0] == ioOutline[info.p0+info.size-1])
         info.size--;

      if (info.size>2)
      {
         UserPoint *p = &ioOutline[info.p0];
         double area = 0.0;
         for(int i=2;i<info.size;i++)
         {
            UserPoint v_prev = p[i-1] - p[0];
            UserPoint v_next = p[i] - p[0];
            area += v_prev.Cross(v_next);
         }
         bool reverse = area < 0;
         int  parent = -1;

         for(int prev=subInfo.size()-1; prev>=0 && parent==-1; prev--)
         {
            if (subInfo[prev].contains(p[0]))
            {
               int prev_p0 = subInfo[prev].p0;
               int prev_size = subInfo[prev].size;
               int inside = PIP_MAYBE;
               for(int test_point = 0; test_point<info.size && inside==PIP_MAYBE; test_point++)
               {
                  inside =  PointInPolygon( p[test_point], &ioOutline[prev_p0], prev_size);
                  if (inside==PIP_YES)
                     parent = prev;
               }
            }
         }

         if (parent==-1 || subInfo[parent].is_internal )
         {
            info.group = groupId++;
            info.is_internal = false;
         }
         else
         {
            info.group = subInfo[parent].group;
            info.is_internal = true;
         }

         info.first = &edges[index];
         AddSubPoly(info.first,p,info.size,reverse!=info.is_internal);
         if (sub<subs-1)
            info.calcExtent();
         index += info.size;

         subInfo.push_back(info);
      }
   }

   Vertices triangles;
   for(int group=0;group<groupId;group++)
   {
      int first = -1;
      int size = 0;
      for(int sub=0;sub<subInfo.size();sub++)
      {
         SubInfo &info = subInfo[sub];
         if (info.group==group)
         {
            if (first<0)
            {
               first = sub;
               size = info.size;
            }
            else
            {
               LinkSubPolys(subInfo[first].first,info.first, info.link);
               size += info.size + 2;
            }
         }
      }
      ConvertOutlineToTriangles(subInfo[first].first, size,triangles);
   }

   ioOutline.swap(triangles);
}
Ejemplo n.º 25
0
pcl::simulation::TriangleMeshModel::TriangleMeshModel (pcl::PolygonMesh::Ptr plg)
{
  Vertices vertices;
  Indices indices;

  bool found_rgb = false;
  for (size_t i=0; i < plg->cloud.fields.size () ; ++i)
    if (plg->cloud.fields[i].name.compare ("rgb") == 0)
      found_rgb = true;

  if (found_rgb)
  {
    pcl::PointCloud<pcl::PointXYZRGB> newcloud;
    pcl::fromROSMsg (plg->cloud, newcloud);

    PCL_DEBUG("RGB Triangle mesh: ");
    PCL_DEBUG("Mesh polygons: %ld", plg->polygons.size ());
    PCL_DEBUG("Mesh points: %ld", newcloud.points.size ());

    Eigen::Vector4f tmp;
    for(size_t i=0; i< plg->polygons.size (); ++i)
    { // each triangle/polygon
      pcl::Vertices apoly_in = plg->polygons[i];
      for(size_t j = 0; j < apoly_in.vertices.size (); ++j)
      { // each point
        uint32_t pt = apoly_in.vertices[j];
        tmp = newcloud.points[pt].getVector4fMap();
        vertices.push_back (Vertex (Eigen::Vector3f (tmp (0), tmp (1), tmp (2)),
                                    Eigen::Vector3f (newcloud.points[pt].r/255.0f,
                                                     newcloud.points[pt].g/255.0f,
                                                     newcloud.points[pt].b/255.0f)));
        indices.push_back (indices.size ());
      }
    }
  }
  else
  {
    pcl::PointCloud<pcl::PointXYZ> newcloud;
    pcl::fromROSMsg (plg->cloud, newcloud);
    Eigen::Vector4f tmp;
    for(size_t i=0; i< plg->polygons.size (); i++)
    { // each triangle/polygon
      pcl::Vertices apoly_in = plg->polygons[i];
      for(size_t j=0; j< apoly_in.vertices.size (); j++)
      { // each point
        uint32_t pt = apoly_in.vertices[j];
        tmp = newcloud.points[pt].getVector4fMap();
        vertices.push_back (Vertex (Eigen::Vector3f (tmp (0), tmp (1), tmp (2)),
                                    Eigen::Vector3f (1.0, 1.0, 1.0)));
        indices.push_back (indices.size ());
      }
    }
  }

  PCL_DEBUG("Vertices: %ld", vertices.size ());
  PCL_DEBUG("Indices: %ld", indices.size ());

  glGenBuffers (1, &vbo_);
  glBindBuffer (GL_ARRAY_BUFFER, vbo_);
  glBufferData (GL_ARRAY_BUFFER, vertices.size () * sizeof (vertices[0]), &(vertices[0]), GL_STATIC_DRAW);
  glBindBuffer (GL_ARRAY_BUFFER, 0);

  glGenBuffers (1, &ibo_);
  glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, ibo_);
  glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices.size () * sizeof (indices[0]), &(indices[0]), GL_STATIC_DRAW);
  glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);

  if (indices.size () > std::numeric_limits<GLuint>::max ())
    PCL_THROW_EXCEPTION(PCLException, "Too many vertices");

  size_ = static_cast<GLuint>(indices.size ());
}
	void mcqdyn(int* maxclique, int &sz){ 
		set_degrees(V); sort(V.begin(),V.end(), desc_degree); init_colors(V);
		for(int i = 0; i < (int)V.size() + 1; i++) S[i].i1 = S[i].i2 = 0;
		expand_dyn(V); sz = (int)QMAX.size();
		for(int i = 0; i < (int)QMAX.size(); i++) maxclique[i] = QMAX[i];
	}
Ejemplo n.º 27
0
void InitVertices(	unsigned recursionDepth, float edgeSize,
					Vertices &pyramidVertices,
					Indices &pyramidIndices)
{
	assert( 0 == pyramidIndices.size() );
	assert( 0 == pyramidVertices.size() );

	std::vector<Node> nodes;

	const float alpha = atanf( sqrtf(2.0f) );
	const float cosAlpha = cosf(alpha);
	const float sinAlpha = sinf(alpha);

	//top right triangle
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f, -edgeSize/ 2,
										sinAlpha,  cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f,  edgeSize/ 2,
										sinAlpha,  cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex(  0.0f,  edgeSize*sqrtf(2.0f)/2, 0.0f,
										sinAlpha,  cosAlpha,  0.0f ) );

	//top back triangle
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f,  edgeSize/ 2,
										0.0f,  cosAlpha,  sinAlpha ) );
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f,  edgeSize/ 2,
										0.0f,  cosAlpha,  sinAlpha ) );
	pyramidVertices.push_back( Vertex(  0.0f,  edgeSize*sqrtf(2.0f)/2, 0.0f,
										0.0f,  cosAlpha,  sinAlpha ) );

	//top left triangle
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f,  edgeSize/ 2,
									   -sinAlpha,  cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f, -edgeSize/ 2,
									   -sinAlpha,  cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex(  0.0f,  edgeSize*sqrtf(2.0f)/2, 0.0f,
									   -sinAlpha,  cosAlpha,  0.0f ) );

	//top fron triangle
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f, -edgeSize/ 2,
										0.0f,  cosAlpha, -sinAlpha ) );
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f, -edgeSize/ 2,
										0.0f,  cosAlpha, -sinAlpha ) );
	pyramidVertices.push_back( Vertex(  0.0f,  edgeSize*sqrtf(2.0f)/2, 0.0f,
										0.0f,  cosAlpha, -sinAlpha ) );



	//bottom right triangle
	pyramidVertices.push_back( Vertex(  0.0f, -edgeSize*sqrtf(2.0f)/2, 0.0f,
										sinAlpha, -cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f, -edgeSize/ 2,
										sinAlpha, -cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f,  edgeSize/ 2,
										sinAlpha, -cosAlpha,  0.0f ) );

	//bottom back triangle
	pyramidVertices.push_back( Vertex(  0.0f, -edgeSize*sqrtf(2.0f)/2, 0.0f,
										0.0f, -cosAlpha,  sinAlpha ) );
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f,  edgeSize/ 2,
										0.0f, -cosAlpha,  sinAlpha ) );
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f,  edgeSize/ 2,
										0.0f, -cosAlpha,  sinAlpha ) );

	//bottom left triangle
	pyramidVertices.push_back( Vertex(  0.0f, -edgeSize*sqrtf(2.0f)/2, 0.0f,
									   -sinAlpha, -cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f,  edgeSize/ 2,
									   -sinAlpha, -cosAlpha,  0.0f ) );
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f, -edgeSize/ 2,
									   -sinAlpha, -cosAlpha,  0.0f ) );

	//bottom fron triangle
	pyramidVertices.push_back( Vertex(  0.0f, -edgeSize*sqrtf(2.0f)/2, 0.0f,
										0.0f, -cosAlpha, -sinAlpha ) );
	pyramidVertices.push_back( Vertex( -edgeSize/2, 0.0f, -edgeSize/ 2,
										0.0f, -cosAlpha, -sinAlpha ) );
	pyramidVertices.push_back( Vertex(  edgeSize/2, 0.0f, -edgeSize/ 2,
										0.0f, -cosAlpha, -sinAlpha ) );


	for( Index index=0; index<pyramidVertices.size(); ++index )
	{
		nodes.push_back( Node(NULL, NULL, index) );
	}

	for( unsigned i=0; i<nodes.size(); i+=3 )
	{
		Tessellation( nodes[i], nodes[i+1], nodes[i+2], pyramidVertices, pyramidIndices, 0, recursionDepth );
	}		
}
Ejemplo n.º 28
0
int main(int argc, char** argv)
{
  if(argc != 3)
    return usage(argv[0]);
  mark_tag vertex_index(1), vertex_x(2), vertex_y(3), vertex_z(4);

  sregex tface_re = bos >> *space >> "TFACE" >> *space >> eos;

  sregex vertex_re = bos >> *space >> "VRTX" 
			 >> +space >> (vertex_index=+_d)
			 >> +space >> (vertex_x=+(digit|'-'|'+'|'.'))
			 >> +space >> (vertex_y=+(digit|'-'|'+'|'.'))
			 >> +space >> (vertex_z=+(digit|'-'|'+'|'.'))
			 >> eos;

  sregex triangle_re = bos >> *space >> "TRGL"
			   >> +space >> (s1=+digit)
			   >> +space >> (s2=+digit)
			   >> +space >> (s3=+digit)
			   >> eos;
  sregex end_re = bos >> *space >> "END" >> *space >> eos;

  std::ifstream input(argv[1]);
  std::ofstream output(argv[2]);

  if(!input) {
    std::cerr << "Cannot read \"" << argv[1] << "\"!\n";
    return EXIT_FAILURE;
  }
  if(!output) {
    std::cerr << "Cannot write to \"" << argv[2] << "\"!\n";
    return EXIT_FAILURE;
  }

  std::string line;
  std::getline(input, line);
  smatch results;
  while(input && ! regex_match(line, tface_re)) // search line "TFACE"
  {
    std::getline(input, line);
  }
  std::getline(input, line);
  while(input && regex_match(line, results, vertex_re)) {
    vertices.push_back(boost::make_tuple(results[vertex_x],
					 results[vertex_y],
					 results[vertex_z]));
    std::getline(input, line);
  }
  while(input && regex_match(line, results, triangle_re)) {
    std::stringstream s;
    int i, j, k;
    s << results[1] << " " << results[2] << " " << results[3];
    s >> i >> j >> k;
    faces.push_back(boost::make_tuple(i, j, k));
    std::getline(input, line);
  }
  if(!input || !regex_match(line, end_re))
    return incorrect_input("premature end of file!");

  output << "OFF " << vertices.size() << " " << faces.size() << " " << "0\n";
  for(Vertices::const_iterator vit = vertices.begin(), vend = vertices.end();
      vit != vend; ++vit)
    output << boost::get<0>(*vit) << " "
	   << boost::get<1>(*vit) << " "
	   << boost::get<2>(*vit) << "\n";
  for(Faces::const_iterator fit = faces.begin(), fend = faces.end();
      fit != fend; ++fit)
    output << "3 " << boost::get<0>(*fit)
	   << " " << boost::get<1>(*fit)
	   << " " << boost::get<2>(*fit) << "\n";
  if(output)
    return EXIT_SUCCESS;
  else
    return EXIT_FAILURE;
};
	void cut2(const Vertices &A, Vertices &B){
		for(int i = 0; i < (int)A.size() - 1; i++)
			if(e[A.back().i][A[i].i])
				B.push_back(A[i].i);
	}
	void set_degrees(Vertices &v){
		for(int i = 0, j; i < (int)v.size(); i++)
			for(v[i].d = j = 0; j < int(v.size()); j++)
				v[i].d += e[v[i].i][v[j].i];
	}