Example #1
0
void Reprojector::projectCloud( IplImage *image )
{
	cvZero( image );

	if( !this->projVertices.size() )
		return;

	VertexList tempList;
	tempList.resize( this->projVertices.size() );

	transform3DTo2D( this->projVertices.size(), &( this->projVertices[0] ), &( tempList[0] ), this->cxProjector, this->cyProjector, this->focalLengthProjector, false );

	unsigned short val = 0;
	unsigned short *tempPtr = NULL;

	for( VertexList::const_iterator it = tempList.begin(); it != tempList.end(); ++it )
	{
		unsigned int x = it->X;
		unsigned int y = it->Y;
		unsigned short depth = -( it->Z );

		if( x >= image->width || y >= image->height )
			continue;

		val = depth;
		tempPtr = (unsigned short*)( image->imageData ) + x + y * image->width;

		if( !( *tempPtr ) || *tempPtr > val )	//z-test
			*tempPtr = val;
	}
}
Example #2
0
//***************************************************************************************
// Name: Subdivide
// Desc: Function subdivides every input triangle into four triangles of equal area.
//***************************************************************************************
void Subdivide(VertexList& vertices, IndexList& indices)
{
	VertexList vin = vertices;
	IndexList  iin = indices;

	vertices.resize(0);
	indices.resize(0);

	//       v1
	//       *
	//      / \
	//     /   \
	//  m0*-----*m1
	//   / \   / \
	//  /   \ /   \
	// *-----*-----*
	// v0    m2     v2

	UINT numTris = (UINT)iin.size()/3;
	for(UINT i = 0; i < numTris; ++i)
	{
		D3DXVECTOR3 v0 = vin[ iin[i*3+0] ];
		D3DXVECTOR3 v1 = vin[ iin[i*3+1] ];
		D3DXVECTOR3 v2 = vin[ iin[i*3+2] ];

		D3DXVECTOR3 m0 = 0.5f*(v0 + v1);
		D3DXVECTOR3 m1 = 0.5f*(v1 + v2);
		D3DXVECTOR3 m2 = 0.5f*(v0 + v2);

		vertices.push_back(v0); // 0
		vertices.push_back(v1); // 1
		vertices.push_back(v2); // 2
		vertices.push_back(m0); // 3
		vertices.push_back(m1); // 4
		vertices.push_back(m2); // 5
 
		indices.push_back(i*6+0);
		indices.push_back(i*6+3);
		indices.push_back(i*6+5);

		indices.push_back(i*6+3);
		indices.push_back(i*6+4);
		indices.push_back(i*6+5);

		indices.push_back(i*6+5);
		indices.push_back(i*6+4);
		indices.push_back(i*6+2);

		indices.push_back(i*6+3);
		indices.push_back(i*6+1);
		indices.push_back(i*6+4);
	}
}
Example #3
0
//***************************************************************************************
// Name: BuildGeoSphere
// Desc: Function approximates a sphere by tesselating an icosahedron.
//***************************************************************************************
void BuildGeoSphere(UINT numSubdivisions, float radius, VertexList& vertices, IndexList& indices)
{
	// Put a cap on the number of subdivisions.
	numSubdivisions = Min(numSubdivisions, UINT(5));

	// Approximate a sphere by tesselating an icosahedron.

	const float X = 0.525731f; 
	const float Z = 0.850651f;

	D3DXVECTOR3 pos[12] = 
	{
		D3DXVECTOR3(-X, 0.0f, Z),  D3DXVECTOR3(X, 0.0f, Z),  
		D3DXVECTOR3(-X, 0.0f, -Z), D3DXVECTOR3(X, 0.0f, -Z),    
		D3DXVECTOR3(0.0f, Z, X),   D3DXVECTOR3(0.0f, Z, -X), 
		D3DXVECTOR3(0.0f, -Z, X),  D3DXVECTOR3(0.0f, -Z, -X),    
		D3DXVECTOR3(Z, X, 0.0f),   D3DXVECTOR3(-Z, X, 0.0f), 
		D3DXVECTOR3(Z, -X, 0.0f),  D3DXVECTOR3(-Z, -X, 0.0f)
	};

	DWORD k[60] = 
	{
		1,4,0,  4,9,0,  4,5,9,  8,5,4,  1,8,4,    
		1,10,8, 10,3,8, 8,3,5,  3,2,5,  3,7,2,    
		3,10,7, 10,6,7, 6,11,7, 6,0,11, 6,1,0, 
		10,1,6, 11,0,9, 2,11,9, 5,2,9,  11,2,7 
	};

	vertices.resize(12);
	indices.resize(60);

	for(int i = 0; i < 12; ++i)
		vertices[i] = pos[i];

	for(int i = 0; i < 60; ++i)
		indices[i] = k[i];

	for(UINT i = 0; i < numSubdivisions; ++i)
		Subdivide(vertices, indices);

	// Project vertices onto sphere and scale.
	for(size_t i = 0; i < vertices.size(); ++i)
	{
		D3DXVec3Normalize(&vertices[i], &vertices[i]);
		vertices[i] *= radius;
	}
}
Example #4
0
      void read_label (const std::string& path, VertexList& vertices, Scalar& scalar)
      {
        vertices.clear();
        scalar.resize(0);

        std::ifstream in (path.c_str(), std::ios_base::in);
        if (!in)
          throw Exception ("Error opening input file!");

        std::string line;
        std::getline (in, line);
        if (line.substr(0, 13) != "#!ascii label")
          throw Exception ("Error parsing FreeSurfer label file \"" + Path::basename (path) + "\": Bad first line identifier");
        std::getline (in, line);
        uint32_t num_vertices = 0;
        try {
          num_vertices = to<size_t> (line);
        } catch (Exception& e) {
          throw Exception (e, "Error parsing FreeSurfer label file \"" + Path::basename (path) + "\": Bad second line vertex count");
        }

        for (size_t i = 0; i != num_vertices; ++i) {
          std::getline (in, line);
          uint32_t index = std::numeric_limits<uint32_t>::max();
          default_type x = NaN, y = NaN, z = NaN, value = NaN;
          sscanf (line.c_str(), "%u %lf %lf %lf %lf", &index, &x, &y, &z, &value);
          if (index == std::numeric_limits<uint32_t>::max())
            throw Exception ("Error parsing FreeSurfer label file \"" + Path::basename (path) + "\": Malformed line");
          if (index >= scalar.size()) {
            scalar.conservativeResizeLike (Scalar::Base::Constant (index+1, NaN));
            vertices.resize (index+1, Vertex (NaN, NaN, NaN));
          }
          if (std::isfinite (scalar[index]))
            throw Exception ("Error parsing FreeSurfer label file \"" + Path::basename (path) + "\": Duplicated index (" + str(scalar[index]) + ")");
          scalar[index] = value;
          vertices[index] = Vertex (x, y, z);
        }

        if (!in.good())
          throw Exception ("Error parsing FreeSurfer label file \"" + Path::basename (path) + "\": End of file reached");
        scalar.set_name (path);
      }
void RenderingEngine::Initialize()
{
    // Create vertices for full-screen quad.
    m_backgroundVertices.resize(4);
    m_backgroundVertices[0].Position = vec3(-1, -1.5, 0);
    m_backgroundVertices[0].TexCoord = vec2(0, 0);
    m_backgroundVertices[1].Position = vec3(-1, 1.5, 0);
    m_backgroundVertices[1].TexCoord = vec2(0, 1);
    m_backgroundVertices[2].Position = vec3(1, -1.5, 0);
    m_backgroundVertices[2].TexCoord = vec2(1, 0);
    m_backgroundVertices[3].Position = vec3(1, 1.5, 0);
    m_backgroundVertices[3].TexCoord = vec2(1, 1);

    if (false) {
        m_backgroundVertices[0].TexCoord *= 0.3;
        m_backgroundVertices[1].TexCoord *= 0.3;
        m_backgroundVertices[2].TexCoord *= 0.3;
        m_backgroundVertices[3].TexCoord *= 0.3;
    }
    
    // Create the line-based stick figure.
    size_t indexCount = sizeof(StickFigureIndices) / sizeof(GLushort);
    size_t lineCount = indexCount / 2;
    m_stickFigure.Indices = IndexList(StickFigureIndices, StickFigureIndices + indexCount);
    glGenBuffers(1, &m_stickFigure.IndexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_stickFigure.IndexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 m_stickFigure.Indices.size() * sizeof(GLushort),
                 &m_stickFigure.Indices[0],
                 GL_STATIC_DRAW);
    m_stickFigure.Vertices.resize(JointCount);
    
    // Create the triangle-based stick figure.
    GenerateTriangleIndices(lineCount, m_aaStickFigure.Indices);
    glGenBuffers(1, &m_aaStickFigure.IndexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_aaStickFigure.IndexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 m_aaStickFigure.Indices.size() * sizeof(GLushort),
                 &m_aaStickFigure.Indices[0],
                 GL_STATIC_DRAW);
    m_aaStickFigure.Vertices.resize(lineCount * 8);
    GenerateTriangleTexCoords(m_aaStickFigure);

    // Initialize the demo state.
    m_demoState = DemoStateAaLines;
    
    // Load up some textures.
    m_textures.Tile = CreateTexture(Tile);
    m_textures.Circle = CreateTexture(Circle);
    m_textures.BlurryCircle = CreateTexture(BlurryCircle);

    // Extract width and height from the color buffer.
    ivec2 screenSize;
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES,
                                    GL_RENDERBUFFER_WIDTH_OES, &screenSize.x);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES,
                                    GL_RENDERBUFFER_HEIGHT_OES, &screenSize.y);
    
    // Create the on-screen FBO.
    glGenFramebuffersOES(1, &m_framebuffers.Screen);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_framebuffers.Screen);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
                                 GL_RENDERBUFFER_OES, m_renderbuffers.Screen);
    
    // Set up various GL state.
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);
    glViewport(0, 0, screenSize.x, screenSize.y);
    glAlphaFunc(GL_LESS, 0.5);
    
    // Set up the transforms.
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    const float NearPlane = 5, FarPlane = 100;
    const float Scale = 0.0005;
    glFrustumf(-Scale * screenSize.x / 2, Scale * screenSize.x / 2,
               -Scale * screenSize.y / 2, Scale * screenSize.y / 2,
               NearPlane, FarPlane);
    
    glMatrixMode(GL_MODELVIEW);
}