Repository: opencv
Issue: cv::viz::Mesh::load() does not properly load a ply file containing faces other than triangles
Labels: feature,category: viz

<!--
If you have a question rather than reporting a bug please go to http://answers.opencv.org where you get much faster responses.
If you need further assistance please read [How To Contribute](https://github.com/opencv/opencv/wiki/How_to_contribute).

This is a template helping you to create an issue which can be processed as quickly as possible. This is the bug reporting section for the OpenCV library.
-->

##### System information (version)
<!-- Example
- OpenCV => 3.1
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2015
-->

- OpenCV => 3.1
- Operating System / Platform => Linux Ubuntu 16.04
- Compiler => gcc 5.4.0
- Remark => Use opencv bundled with ROS

##### Detailed description

The function `cv::viz::Mesh::load(const String & file, int type = LOAD_PLY)` does not properly load a ply file that has faces other than triangles defined. The vertices are always properly loaded, but the faces (polygons) are not. This only occurs if the faces are defined by more than three vertices.

##### Steps to reproduce

Save the following two ply files; then run the following code.

square.ply:
```
ply
format ascii 1.0
element vertex 4
property double x
property double y
property double z
element face 1
property list uchar int vertex_index
end_header
0 0 0
0 1 0
1 0 0
1 1 0
4 0 1 2 3
```
triangle.ply:
```
ply
format ascii 1.0
element vertex 3
property double x
property double y
property double z
element face 1
property list uchar int vertex_index
end_header
0 0 0
0 1 0
1 0 0
3 0 1 2 
```
code:
```
#include <opencv2/viz.hpp>
#include <iostream>

int main(int argc, char** argv)
{
  std::string path = "/path/to/your/saved/folder/";
  std::string file_name_square = path + "square.ply";
  std::string file_name_triangle = path + "triangle.ply";

  cv::viz::Mesh triangle = cv::viz::Mesh::load(file_name_triangle);
  std::cout << "Mat cloud = " << std::endl << triangle.cloud << std::endl << std::flush;
  std::cout << "Mat polygons = " << std::endl << triangle.polygons << std::endl << std::flush;

  cv::viz::Mesh square = cv::viz::Mesh::load(file_name_square);
  std::cout << "Mat cloud = " << std::endl << square.cloud << std::endl << std::flush;
  std::cout << "Mat polygons = " << std::endl << square.polygons << std::endl << std::flush;

  return 0;
}
```

The above code returns the following output:
```
Mat cloud = 
[0, 0, 0, 0, 1, 0, 1, 0, 0]
Mat polygons = 
[3, 0, 1, 2]
Mat cloud = 
[0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0]
Mat polygons = 
[4, 0, 1, 2, 3, 0, 0, 0, 0]
```
You can see that in the case of the triangle, the polygon matrix is well initialized, with 4 entries. However, for the square, the matrix has 8 entries as opposed to 5 (there are four additional 0 trailing the matrix). In both cases, the cloud matrix is correct.

In this specific case, the code does not crash because the additional entries are all zeros. However, when I use this snippet with other code, the entries are random and cause the program to crash. See an example of this below:

```
Mat cloud = 
[0, 0, 0, 0, 1, 0, 1, 0, 0]
Mat polygons = 
[3, 0, 1, 2]
Mat cloud = 
[0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0]
Mat polygons = 
[4, 0, 1, 2, 3, 0, 48, 0, 33022272]
```
Trying to create a Mesh Widget with the previous output generates a segmentation fault.