00001 package commandline.flatFileToBinaryPackage;
00002
00003 import java.io.IOException;
00004 import java.io.PrintStream;
00005 import java.util.ArrayList;
00006
00007 import commandline.CodingUtils.ByteConvertor;
00008 import commandline.CodingUtils.CodedFile;
00009 import commandline.CodingUtils.DataStructure;
00010 import commandline.ImageStuff.GeometricObjects.GenericEdge;
00011 import commandline.ImageStuff.GeometricObjects.Vertex;
00012 import commandline.NormalMeshAlgorithm.Configuration;
00013 import commandline.NormalMeshAlgorithm.ConfigurationWriter;
00014 import commandline.debuggingCode.Debug;
00015
00016
00017
00018 public abstract class AbstractFlatToBin {
00019 MyDataOutPutStream dos ;
00020 DataStructure breathFirstDatastructure;
00021 CodedFile treeStructure;
00022 protected int verticalOffsetBits, verticalOffsetGain, normalOffsetBits, normalOffsetGain,
00023 tesselationBits, tesselationGain,treeGain, treeBits,basemeshBits,metaBits;
00024
00025 public AbstractFlatToBin(Configuration configuration, DataStructure dataStructure,CodedFile treeStucture,MyDataOutPutStream dos) {
00026 this.dos=dos;
00027 if(Debug.VERBOSE) Debug.print("write configuration to outputstream");
00028 ConfigurationWriter configurationWriter= new ConfigurationWriter(configuration,dos);
00029 configurationWriter.write();
00030 breathFirstDatastructure=dataStructure;
00031 this.treeStructure=treeStucture;
00032 verticalOffsetBits=0;verticalOffsetGain=0;normalOffsetBits=0; normalOffsetGain=0;
00033 tesselationBits=0; tesselationGain=0;treeBits=0; treeGain=0;basemeshBits=0;metaBits=0;
00034 if(!Debug.DISTIBUTION) Debug.print("NEW(): " + this.getClass().getName()+ " created");
00035 }
00036 public final void convert() throws IOException{
00037 try{
00038 writeBaseMesh();
00039 writeTreeStructure();
00040 writeTesselations();
00041 writeOffsets();
00042 dos.flush();
00043 dos.close();
00044 }catch(IOException exception){
00045 exception.printStackTrace();
00046 }
00047 if(!Debug.DISTIBUTION) Debug.print("Starting to convert readeble file to binary file...done");
00048
00049
00050 }
00051
00056 private void writeBaseMesh() throws IOException{
00057
00058 if(!Debug.DISTIBUTION) Debug.print("* writing number of basemeshvertices (short)" + breathFirstDatastructure.getNbrBaseVertices());
00059 basemeshBits+= writeMyShort(breathFirstDatastructure.getNbrBaseVertices());
00060
00061 if(!Debug.DISTIBUTION) Debug.print("* writing vertex coordinates (short's)");
00062 for(int i =0;i!=breathFirstDatastructure.getNbrBaseVertices();i++){
00063 Vertex v= breathFirstDatastructure.getBasemeshVertices().get(i);
00064 basemeshBits+= writeMyShort(v.getX());
00065 basemeshBits+= writeMyShort(v.getY());
00066 basemeshBits+= writeMyShort((int)v.getZ());
00067 }
00068 }
00069 protected void writeTesselations(){
00070
00071 metaBits+= writeMyShort(breathFirstDatastructure.getTesselationFile().getCodeWoorden().size());
00072
00073
00074 int nbrOfTesselationcodes=breathFirstDatastructure.getTesselationFile().getCodeWoorden().size();
00075 ArrayList<String> charTesselation=new ArrayList<String>(nbrOfTesselationcodes*2);
00076 for(String s: breathFirstDatastructure.getTesselationFile().getCodeWoorden()){
00077 if(s.equals("00")){charTesselation.add("0");charTesselation.add("0");}
00078 if(s.equals("01")){charTesselation.add("0");charTesselation.add("1");}
00079 if(s.equals("10")){charTesselation.add("1");charTesselation.add("0");}
00080 if(s.equals("11")){charTesselation.add("1");charTesselation.add("1");}
00081 }
00082
00083 try{
00084 tesselationBits+=writeArrayListOfBitStringsToBytes(charTesselation);
00085 }catch(IOException exception){
00086 Debug.print("Error writing Tesselation");
00087 }
00088 }
00089
00090
00095 private void writeTreeStructure() throws IOException{
00096
00097 metaBits+= writeMyShort(treeStructure.getCodeWoorden().size());
00098 if(!Debug.DISTIBUTION) Debug.print("* writing number of treebits = " + treeStructure.getCodeWoorden().size());
00099
00100 if(!Debug.DISTIBUTION) Debug.print("* writing treebits");
00101 treeBits+=writeArrayListOfBitStringsToBytes(treeStructure.getCodeWoorden());
00102 }
00103
00104 protected void writeOffsets(){
00105
00106
00107 for(GenericEdge parent:breathFirstDatastructure.getParentCommitOrderedList()){
00108 verticalOffsetBits+= writeMyShort((int)parent.getVerticalOffset());
00109 }
00110 for(GenericEdge parent:breathFirstDatastructure.getParentCommitOrderedList()){
00111 normalOffsetBits+= writeMyShort(parent.getNormalOffset());
00112 }
00113
00114 }
00115
00116
00117 public int getVerticalOffsetBits() {
00118 return verticalOffsetBits;
00119 }
00120
00121
00122 public DataStructure getBreathFirstDatastructure() {
00123 return breathFirstDatastructure;
00124 }
00125
00126
00127 public MyDataOutPutStream getDos() {
00128 return dos;
00129 }
00130
00131
00132 public int getNormalOffsetBits() {
00133 return normalOffsetBits;
00134 }
00135
00136
00137 public int getNormalOffsetGain() {
00138 return normalOffsetGain;
00139 }
00140
00141
00142 public int getTesselationBits() {
00143 return tesselationBits;
00144 }
00145
00146
00147 public int getTesselationGain() {
00148 return tesselationGain;
00149 }
00150
00151
00152 public int getTreeBits() {
00153 return treeBits;
00154 }
00155
00156
00157 public int getTreeGain() {
00158 return treeGain;
00159 }
00160
00161
00162 public CodedFile getTreeStructure() {
00163 return treeStructure;
00164 }
00165
00166
00167 public int getVerticalOffsetGain() {
00168 return verticalOffsetGain;
00169 }
00170
00171
00172 public int getBasemeshBits() {
00173 return basemeshBits;
00174 }
00175
00176
00177 public int getMetaBits() {
00178 return metaBits;
00179 }
00180
00181 public void printto(PrintStream printStream){
00182 printStream.println("-------------------------");
00183 float total_nbr_bits=this.getTreeBits()+this.getMetaBits()+this.getTesselationBits()+this.getNormalOffsetBits()+this.getVerticalOffsetBits();
00184 printStream.println("meta_bits ="+ this.getMetaBits() + " "+100*this.getMetaBits()/total_nbr_bits+"%");
00185 printStream.println("tree_bits ="+ this.getTreeBits()+ " "+ 100*this.getTreeBits()/total_nbr_bits+"%");
00186 printStream.println("tesselation_bits ="+ this.getTesselationBits()+ " "+100*this.getTesselationBits()/total_nbr_bits+"%");
00187 printStream.println("normalOffset_bits ="+ this.getNormalOffsetBits()+ " "+100*this.getNormalOffsetBits()/total_nbr_bits +"%");
00188 printStream.println("verticalOffset_bits ="+ this.getVerticalOffsetBits()+ " "+100*this.getVerticalOffsetBits()/total_nbr_bits +"%");
00189 printStream.println("-------------------------");
00190
00191 }
00192
00199 protected int writeArrayListOfBitStringsToBytes(ArrayList<String> codewoorden) throws IOException{
00200 int aantal_bits=codewoorden.size();
00201 int teller=0;
00202 char tekenreeks[]=new char[8];
00203
00204 for(int i=0;i!= codewoorden.size();i++){
00205
00206 if(codewoorden.get(i).equals("1")){
00207 tekenreeks[teller]='1';}
00208 else{tekenreeks[teller]='0';}
00209
00210 teller++;
00211 teller=teller%8;
00212 if(teller==0) {writeByteArray(new byte[]{ByteConvertor.getbyteFor(tekenreeks)});
00213 tekenreeks=new char[]{'0','0','0','0','0','0','0','0'};
00214 }
00215 }
00216 if(codewoorden.size()%8!=0)writeByteArray(new byte[]{ByteConvertor.getbyteFor(tekenreeks)});
00217
00218 return aantal_bits;
00219 }
00220
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00247 protected int writeMyShort(int getal){
00248 try{
00249 dos.writeMyShort(getal);
00250 }catch(IOException exception){
00251 Debug.print("error writing short");
00252 }
00253 return 2*ByteConvertor.BITS_PER_BYTE;
00254 }
00255
00259 protected int writeMyInt(int getal){
00260 try{
00261 dos.writeMyInt(getal);
00262 }catch(IOException exception){
00263 Debug.print("error writing int");
00264 }
00265 return 4*ByteConvertor.BITS_PER_BYTE;
00266 }
00267
00271 protected int writeMyFloat(float getal){
00272 try{
00273 dos.writeMyFloat(getal);
00274 }catch(IOException exception){
00275 Debug.print("error writing int");
00276 }
00277 return 4*ByteConvertor.BITS_PER_BYTE;
00278 }
00279
00280
00284 protected int writeByteArray(byte[] b){
00285 try{
00286 dos.write(b);
00287 }catch(IOException exception){
00288 exception.printStackTrace(System.out);
00289 }
00290 return b.length*ByteConvertor.BITS_PER_BYTE;
00291 }
00292
00293
00294 protected void printCharArray(char[] array, PrintStream printStream){
00295 printStream.println(array);
00296 }
00297
00298 protected void printStringArray(String[] bitarray, PrintStream printStream){
00300 for(String s: bitarray){
00301 printStream.print(s);
00302 }
00303 printStream.println();
00304
00305 }
00306
00307
00308
00309
00310
00311
00312 }