Using jzy3d to plot 3D surface in Java

1.8k views Asked by At

I have a tab-delimited CSV file that has a header, and its first column are labels for each row. For example.

Label   Sample1   Sample2    Sample3    Sample4    Sample5
U.S.A.    10.1      3.2       5.6       6.9       7.3
Canada    9.8       4.5       5.7       6.8       7.9 

I use superCSV to parse this CSV file and create polygons for each point, for example: (1, 1, 10.1) [meaning first row, first column]. The points are added to a list of polygons. I then use the polygons to create a surface, but the surface is not continuous. I have attached a screenshot of my plotting

enter image description here

Part of my code is as follows:

public void init() {


        /* build a list of polygons out of the CSV file */
        List<Polygon> polygons = null;
        try {
            polygons = parseCSV("my_CSVFile.csv");
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println("size of polygons is: " + polygons.size());


        // Creates the 3d object
        Shape surface = new Shape(polygons);
        surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new org.jzy3d.colors.Color(1,1,1,1f)));
        surface.setWireframeDisplayed(true);
        surface.setWireframeColor(org.jzy3d.colors.Color.BLACK);

        chart = AWTChartComponentFactory.chart(Quality.Advanced, getCanvasType());
//        chart = new Chart();
        chart.getScene().getGraph().add(surface);
    }

    public List<Polygon> parseCSV(String csvFile) throws IOException
    {
        if (csvFile.isEmpty())
            System.exit(-1);

        File inputFile = new File(csvFile);
        String[] header = null;  /* header row */

        if (inputFile.exists())
        {
            FileReader fr = new FileReader(inputFile);
            LineNumberReader lineReader = new LineNumberReader(fr);
            String headerLine = null;

            while (lineReader.getLineNumber() == 0)
            {
                headerLine = lineReader.readLine();
            }
            lineReader.close();


            if (headerLine != null)
            {
                header = headerLine.split("\\t");
            }

        }

        ICsvListReader listReader = null;
        List<Polygon> polygons = new ArrayList<Polygon>();

        try {
            listReader = new CsvListReader(new FileReader(csvFile), CsvPreference.TAB_PREFERENCE);
            listReader.getHeader(true);

            List<String> contentList;
            int rowIndex = 1;           // excluding the header row
            while((contentList = listReader.read()) != null)
            {
                if (contentList.size() != header.length) {
                    System.out.println("contentList size is: " + contentList.size() + ", header length is: " + header.length);
                    continue;
                }

                Polygon polygon = new Polygon();
                for (int i = 1; i < contentList.size(); i++)
                {
                    if (DoubleFactory.tryParseDouble(contentList.get(i)) != -1)  /* unsuccessful double parse returns -1 */
                    {
                        polygon.add(new Point(new Coord3d(rowIndex, i, Double.parseDouble(contentList.get(i)))));
                    }
                }
                rowIndex++;
                polygons.add(polygon);
            }


        } finally {
            if(listReader != null) {
                listReader.close();
            }
        }

        return polygons;

    }

    /* inner class for parsing string to double */
    private static class DoubleFactory
    {
        public static double tryParseDouble(final String number)
        {
            double result;
            try {
                result = Double.parseDouble(number);
            } catch (NumberFormatException e) {
                result = -1;  /* default failed parsing*/
            }

            return result;
        }
    }

I need help with creating a continuous smooth 3D surface out of my CSV contents (the numbers)

2

There are 2 answers

0
Didii On

My code for creating the polygon list is as follows

List<Polygon> polygons = new ArrayList<Polygon>();
for (int i = 0; i < m_data.rows-k; i++) {
    for (int j = 0; j < m_data.columns-k; j++) {
        Polygon polygon = new Polygon();
        polygon.add(new Point(new Coord3d(i,  j,  m_data.get(i,  j  ))));
        polygon.add(new Point(new Coord3d(i+1,j,  m_data.get(i+1,j  ))));
        polygon.add(new Point(new Coord3d(i+1,j+1,m_data.get(i+1,j+1))));
        polygon.add(new Point(new Coord3d(i,  j+1,m_data.get(i,  j+1))));
        polygons.add(polygon);
    }
}

where m_data is a class variable and a matrix. As far as I know, this is the way you should create a polygon. A polygon only consisting of a single point seems rather unlikely to be correct.


Related question: Build a 3d surface plot using xyz coordinates with jzy3d

0
Ruan Caiman On

When you draw a polygon, it will connect the vertices in the order that you've added them. Look at the lines on your plot - they go from one point to the next, jumping from the end of one row to the beginning of the next.

What you want is a surface. I am not familiar with jzy3d, but there should be some built-in surface generators where you can just add all your points and ask it to return a surface. If not, you just have to connect groups of 3 (triangles) or 4 (quads) vertices and draw a polygon with them as Didii coded for you already.