darkprinze's picture

Apply mesh to rendered point cloud and create .stl file

From this forum members help, i successfully rendered pointcloud into GLControl. I have attached the screen shot for reference. Now, i need to apply mesh to rendered points (like connect all points with triangles and apply the color to that triangle) and generate .stl file from applied mesh (Attached the Meshlab output). I am a web application developer, and this whole graphics thing is new to me, and my client is crushing my neck. This is my code to render the point cloud. PointCloud is contains nearly 190000 scanned foot points (x, y, z) .

private void GLControl_Load(object sender, EventArgs e)
            loaded = true;
            int w = 818;
            int h = 561;           
            GL.Viewport(0, 0, w, h); 
 private void GLControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            if (!loaded)
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            var aspect_ratio = Width / (float)Height;            
            var projection = Matrix4.CreatePerspectiveFieldOfView(
                          MathHelper.PiOver4, float.Parse(aspect_ratio.ToString()), 1, 1024);         
            var modelview = Matrix4.LookAt(70, 20, 50, 0, 0, 0, 0, 50, 90);           
            GL.LoadMatrix(ref projection);
            GL.LoadMatrix(ref modelview);
            GL.Translate(x, 0, 0);
            GL.Rotate(angleX, Vector3.UnitZ); 
            int j = 0;
            foreach (Vector3 v in vertices)
                GL.Vertex3(v.X, v.Y, v.Z);
                lblCount.Content = j++;
            lblMessage.Content = "Completed";
  private void LoadVertices()
            char[] splitCharacters = new char[] { ' ' };
            TextReader file = File.OpenText(txtFilePath.Text);
            string line;
            vertices = new List<Vector3>();
            while ((line = file.ReadLine()) != null)
                line = line.Trim(splitCharacters);
                line = line.Replace("  ", " ");
                string[] parameters = line.Split(splitCharacters);
                float x = float.Parse(parameters[0]);
                float y = float.Parse(parameters[1]);
                float z = float.Parse(parameters[2]);
                vertices.Add(new Vector3(x, y, z));
            lblMessage.Content = " Success: " + vertices.Count + "";
 private void Window_Loaded(object sender, RoutedEventArgs e)
            txtFilePath.Text = "C:\\Users\\<name>\\Downloads\\Work\\all.asc";
private void glSurface_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            System.Windows.Forms.MouseEventArgs ev = (e as System.Windows.Forms.MouseEventArgs);
            _mouseStartX = ev.X;
            _mouseStartY = ev.Y;
        private void glSurface_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            System.Windows.Forms.MouseEventArgs ev = (e as System.Windows.Forms.MouseEventArgs);
            angleXS = angleX;
            angleYS = angleY;
            distanceS = distance;
        private void glSurface_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            System.Windows.Forms.MouseEventArgs ev = (e as System.Windows.Forms.MouseEventArgs);
            if (ev.Button == MouseButtons.Left)
                angleX = angleXS + (ev.X - _mouseStartX) * rotSpeed;
                angleY = angleYS + (ev.Y - _mouseStartY) * rotSpeed;
            if (ev.Button == MouseButtons.Right)
                distance = Math.Max(2.9f, distanceS + (ev.Y - _mouseStartY) / 10.0f);

I am also try to use VCG library in my application which is used in MeshLab to create the .STL file from .ASC, but it does not show any error or giving output. So, i am totally struck. Please point me into the right direction.

Inline Images
Meshed foot
Rendered Point cloud


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
darkprinze's picture

Is there any surface reconstruction algorithms such as Poisson disk sampling or Ball pivoting available in OpenTK? I will post samples if i succed with VCGLibrary.

the Fiddler's picture

Not inside OpenTK, no. You will have to combine OpenTK with a more specialized math library for this, such as Math.Net.

darkprinze's picture

Oh. So, OpenTK is only for rendering 3D objects and you can't do the meshing like in uploaded meshlab picture and create .stl file from meshes?

the Fiddler's picture

OpenTK is a low-level library that allows you to use OpenGL in your .Net application. For example, you could use OpenTK to create an application like meshlab from scratch, but it doesn't give you high-level mesh editing capabilities out of the box - that's not its purpose.

One option might be to search for a specialized math library that provides the algorithms you need, and combine that with OpenTK for rendering. It all depends on your project and how much effort you are willing to spend in it.

flopoloco's picture

Hello all.

darkprinze you could get a look at meshlab because is a quite advanced software used for reconstructing point clouds and is open source.

Also another interesting was that application example I made quite long ago, this could be more efficient to handle large amount of data (only point clouds).


darkprinze's picture

Thanks flopoloco. I looked at MeshLab and Meshlab used VCG library for reconstruction purposes. I took the required files from VCG and built the library for reconstruction(for example, convert .ply to .stl) so that render .stl file display mesh in GLControl. However, when i run the application it keeps on processing for long time. Since, its C++ code i cant do the debug to find the problem.

Meanwhile, i am trying to implement the camera rotation and zoomin and zoomout using mouse in GLControl (like in MeshLab). I am partially able to rotate the model viewer but its not so smooth. I updated the code for the reference.