1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.fxml.builder;
  27 
  28 import javafx.scene.shape.TriangleMesh;
  29 import javafx.scene.shape.VertexFormat;
  30 import javafx.util.Builder;
  31 
  32 import java.util.Locale;
  33 import java.util.Map.Entry;
  34 import java.util.Set;
  35 import java.util.TreeMap;
  36 
  37 public class TriangleMeshBuilder extends TreeMap<String, Object> implements Builder<TriangleMesh> {
  38 
  39     private static final String VALUE_SEPARATOR_REGEX = "[,\\s]+";
  40 
  41     private float[] points;
  42     private float[] texCoords;
  43     private float[] normals;
  44     private int[] faces;
  45     private int[] faceSmoothingGroups;
  46     private VertexFormat vertexFormat;
  47 
  48     @Override
  49     public TriangleMesh build() {
  50         TriangleMesh mesh = new TriangleMesh();
  51         if (points != null) {
  52             mesh.getPoints().setAll(points);
  53         }
  54         if (texCoords != null) {
  55             mesh.getTexCoords().setAll(texCoords);
  56         }
  57         if (faces != null) {
  58             mesh.getFaces().setAll(faces);
  59         }
  60         if (faceSmoothingGroups != null) {
  61             mesh.getFaceSmoothingGroups().setAll(faceSmoothingGroups);
  62         }
  63         if (normals != null) {
  64             mesh.getNormals().setAll(normals);
  65         }
  66         if (vertexFormat != null) {
  67             mesh.setVertexFormat(vertexFormat);
  68         }
  69         return mesh;
  70     }
  71 
  72     @Override
  73     public Object put(String key, Object value) {
  74 
  75         if ("points".equalsIgnoreCase(key)) {
  76             String[] split = ((String) value).split(VALUE_SEPARATOR_REGEX);
  77             points = new float[split.length];
  78             for (int i = 0; i < split.length; ++i) {
  79                 points[i] = Float.parseFloat(split[i]);
  80             }
  81         } else if ("texcoords".equalsIgnoreCase(key)) {
  82             String[] split = ((String) value).split(VALUE_SEPARATOR_REGEX);
  83             texCoords = new float[split.length];
  84             for (int i = 0; i < split.length; ++i) {
  85                 texCoords[i] = Float.parseFloat(split[i]);
  86             }
  87         } else if ("faces".equalsIgnoreCase(key)) {
  88             String[] split = ((String) value).split(VALUE_SEPARATOR_REGEX);
  89             faces = new int[split.length];
  90             for (int i = 0; i < split.length; ++i) {
  91                 faces[i] = Integer.parseInt(split[i]);
  92             }
  93         } else if ("facesmoothinggroups".equalsIgnoreCase(key)) {
  94             String[] split = ((String) value).split(VALUE_SEPARATOR_REGEX);
  95             faceSmoothingGroups = new int[split.length];
  96             for (int i = 0; i < split.length; ++i) {
  97                 faceSmoothingGroups[i] = Integer.parseInt(split[i]);
  98             }
  99         } else if ("normals".equalsIgnoreCase(key)) {
 100             String[] split = ((String) value).split(VALUE_SEPARATOR_REGEX);
 101             normals = new float[split.length];
 102             for (int i = 0; i < split.length; ++i) {
 103                 normals[i] = Float.parseFloat(split[i]);
 104             }
 105         } else if ("vertexformat".equalsIgnoreCase(key)) {
 106             if ("point_texcoord".equalsIgnoreCase((String)value)) {
 107                 vertexFormat = VertexFormat.POINT_TEXCOORD;
 108             } else if ("point_normal_texcoord".equalsIgnoreCase((String)value)) {
 109                 vertexFormat = VertexFormat.POINT_NORMAL_TEXCOORD;
 110             }
 111         }
 112 
 113         return super.put(key.toLowerCase(Locale.ROOT), value);
 114     }
 115 
 116     @Override
 117     public Set<Entry<String, Object>> entrySet() {
 118         return super.entrySet();
 119     }
 120 
 121 }