[英]Using jzy3d to plot 3D surface in Java
我有一個制表符分隔的CSV文件,該文件具有標題,其第一列是每一行的標簽。 例如。
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
我使用superCSV解析此CSV文件並為每個點創建多邊形,例如:(1、1、10.1)[表示第一行,第一列]。 這些點將添加到多邊形列表中。 然后,我使用多邊形創建一個曲面,但是該曲面不是連續的。 我已附上我的繪圖屏幕截圖
我的部分代碼如下:
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;
}
}
我需要幫助以CSV內容(數字)創建連續的平滑3D曲面
我創建多邊形列表的代碼如下
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);
}
}
其中, m_data
是類變量和矩陣。 據我所知,這就是創建多邊形的方式。 僅包含一個點的多邊形似乎不太可能是正確的。
相關問題: 使用帶有jzy3d的xyz坐標構建3d表面圖
繪制多邊形時,它將按照添加頂點的順序連接頂點。 查看圖中的線條-它們從一個點到下一個,從一行的結尾跳到下一行的起點。
您想要的是表面。 我對jzy3d並不熟悉,但是應該有一些內置的表面生成器,您可以在其中添加所有點並要求它返回表面。 如果沒有,您只需要連接3個(三角形)或4個(方形)頂點的組,並按照Didii為您編寫的代碼繪制一個多邊形。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.