Using jzy3d to plot 3D surface in Java

350 views
Skip to first unread message

IVYtony smth

unread,
Jun 4, 2014, 5:36:53 PM6/4/14
to jz...@googlegroups.com
I have a tab-delimited CSV file that has a header, and its first column are labels for each row. For example.

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, need some help with making smooth continuous 3D surface plotting. thanks!


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;
        }
    }


IVYtony smth

unread,
Jun 4, 2014, 5:38:28 PM6/4/14
to jz...@googlegroups.com
The code is as follows:





        }<span class="pln" style="font-size: 13.600000381469727p
...

Martin Pernollet

unread,
Jun 5, 2014, 4:18:35 AM6/5/14
to Jzy3d
You might use a larger Z range to have the surface look smoother.



--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "Jzy3d".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse jzy3d+un...@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages