View Javadoc

1   /*
2    * Copyright (C) 2013 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.cuni.amis.pogamut.ut2004.t3dgenerator.elements.map;
18  
19  import cz.cuni.amis.pogamut.unreal.t3dgenerator.annotations.StaticText;
20  import cz.cuni.amis.pogamut.unreal.t3dgenerator.annotations.UnrealBean;
21  import cz.cuni.amis.pogamut.unreal.t3dgenerator.annotations.UnrealHeaderField;
22  import cz.cuni.amis.pogamut.unreal.t3dgenerator.datatypes.Vector3D;
23  import java.text.DecimalFormat;
24  import java.text.DecimalFormatSymbols;
25  import java.text.NumberFormat;
26  import java.util.Arrays;
27  import java.util.List;
28  
29  /**
30   * A polygon in a brush. It SEEMS that textureNormal, textureU and textureV are recomputed upon importing to UnrealEd
31   * from vertices. This means that the order of vertices is significant.
32   * @author Martin Cerny
33   */
34  @UnrealBean("Polygon")
35  public class Polygon {
36      @UnrealHeaderField
37      private String texture;
38      
39      @UnrealHeaderField
40      private Long flags;
41      
42      @UnrealHeaderField
43      private Integer link;
44      
45      /**
46       * The following fields are transient, because they are translated specifically for this class
47       * with the propertyText method
48       */
49      
50      private transient Vector3D textureOrigin;
51      private transient Vector3D textureNormal;
52      private transient Vector3D textureU;    
53      private transient Vector3D textureV;
54      private transient List<Vector3D> vertices;
55  
56      private static final NumberFormat numberFormat;
57      
58      static {
59          DecimalFormatSymbols symbols = new DecimalFormatSymbols();
60          symbols.setDecimalSeparator('.');
61          numberFormat = new DecimalFormat("+00000.000000;-00000.000000", symbols);
62      }
63      
64      public Polygon() {
65      }
66  
67      public Polygon(String texture, Vector3D textureOrigin, Vector3D textureNormal, Vector3D textureU, Vector3D textureV, List<Vector3D> vertices) {
68          this.texture = texture;
69          this.textureOrigin = textureOrigin;
70          this.textureNormal = textureNormal;
71          this.textureU = textureU;
72          this.textureV = textureV;
73          this.vertices = vertices;
74      }
75      
76      public Polygon(String texture, Vector3D textureOrigin, Vector3D textureNormal, Vector3D textureU, Vector3D textureV, Vector3D[] vertices) {
77          this(texture, textureOrigin, textureNormal, textureU, textureV, Arrays.asList(vertices));
78      }
79      
80      protected String translateVectorToT3d(String label, Vector3D vector){
81          //adding 0.0 eliminates negative zero, which in turn causes trouble
82          return "\t\t\t\t\t" + label + " " + numberFormat.format(vector.getX() + 0.0f) + "," + numberFormat.format(vector.getY() + 0.0f) + "," + numberFormat.format(vector.getZ() + 0.0f) + "\n";
83      }
84      
85      @StaticText
86      public String translateToT3D(){
87          StringBuilder theText = new StringBuilder();
88          theText.append(translateVectorToT3d("Origin", textureOrigin));
89          theText.append(translateVectorToT3d("Normal", textureNormal));
90          theText.append(translateVectorToT3d("TextureU", textureU));
91          theText.append(translateVectorToT3d("TextureV", textureV));
92          for(Vector3D v : vertices){
93              theText.append(translateVectorToT3d("Vertex", v));
94          }
95          return theText.toString();
96      }   
97  
98      public String getTexture() {
99          return texture;
100     }
101 
102     public void setTexture(String texture) {
103         this.texture = texture;
104     }
105 
106     public Long getFlags() {
107         return flags;
108     }
109 
110     public void setFlags(Long flags) {
111         this.flags = flags;
112     }
113 
114     public Integer getLink() {
115         return link;
116     }
117 
118     public void setLink(Integer link) {
119         this.link = link;
120     }
121 
122     public Vector3D getTextureOrigin() {
123         return textureOrigin;
124     }
125 
126     public void setTextureOrigin(Vector3D textureOrigin) {
127         this.textureOrigin = textureOrigin;
128     }
129 
130     public Vector3D getTextureNormal() {
131         return textureNormal;
132     }
133 
134     public void setTextureNormal(Vector3D textureNormal) {
135         this.textureNormal = textureNormal;
136     }
137 
138     public Vector3D getTextureU() {
139         return textureU;
140     }
141 
142     public void setTextureU(Vector3D textureU) {
143         this.textureU = textureU;
144     }
145 
146     public Vector3D getTextureV() {
147         return textureV;
148     }
149 
150     public void setTextureV(Vector3D textureV) {
151         this.textureV = textureV;
152     }
153 
154     public List<Vector3D> getVertices() {
155         return vertices;
156     }
157     
158     public void addVertex(Vector3D vertex) {
159         vertices.add(vertex);
160     }
161 }