18 package com.generalrobotix.ui.view.graph;
22 import java.util.zip.*;
43 private static final String
POSTFIX =
".tmp";
49 private Hashtable<String, LogHeader>
header_;
51 private Hashtable<String, RandomAccessFile>
readFile_;
66 public static void main(String[] args) {
71 "test1",
"float",
"test2",
"float[3]" 83 String tmpdir = System.getProperty(
"TEMP");
95 header_ =
new Hashtable<String, LogHeader>();
96 indexMapMap_ =
new HashMap<String, Map<String, Integer>>();
108 }
catch (IOException ex){
109 ex.printStackTrace();
110 }
catch (Exception ex){
111 ex.printStackTrace();
122 }
catch (IOException ex){
123 ex.printStackTrace();
124 }
catch (Exception ex){
125 ex.printStackTrace();
132 if (tmpdir !=
null) {
133 return tmpdir +
File.separator + objectName +
POSTFIX;
140 Enumeration elements = header_.elements();
141 if (!elements.hasMoreElements())
return "";
142 LogHeader header = (LogHeader)elements.nextElement();
153 Enumeration elements = header_.elements();
154 if (!elements.hasMoreElements())
return;
155 LogHeader header = (LogHeader)elements.nextElement();
167 LogHeader header =
new LogHeader(objectName, format);
168 header_.put(objectName, header);
176 collisionLog_ =
new CollisionLogHeader(time);
189 writeFile_ =
new Hashtable<String, DataOutputStream>();
190 for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
192 LogHeader header = (LogHeader) elements.nextElement();
193 header.totalTime_ = time.totalTime_.getUtime();
194 header.startTime_ = time.startTime_.getUtime();
195 header.timeStep_ = time.timeStep_.getUtime();
198 header.numRecords_ = 0;
200 DataOutputStream out =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(
getTempFilePath(header.objectName_))));
203 writeFile_.put(header.objectName_, out);
204 }
catch (IOException ex) {
205 for (Enumeration elms = writeFile_.elements(); elms.hasMoreElements();) {
206 DataOutputStream out = (DataOutputStream) elms.nextElement();
218 if(writeFile_ ==
null)
220 for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
221 LogHeader header = (LogHeader) elements.nextElement();
222 DataOutputStream out = (DataOutputStream) writeFile_.get(header.objectName_);
227 RandomAccessFile file =
new RandomAccessFile(
getTempFilePath(header.objectName_),
"rw");
228 header.outEndTime(file);
236 readFile_ =
new Hashtable<String, RandomAccessFile>();
237 for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
238 LogHeader header = (LogHeader) elements.nextElement();
239 RandomAccessFile file =
null;
242 }
catch (IOException ex){
243 throw new FileOpenFailException(ex.getMessage());
245 readFile_.put(header.objectName_, file);
250 if (readFile_ ==
null)
252 for (Enumeration elements = readFile_.elements(); elements.hasMoreElements();) {
253 RandomAccessFile file = (RandomAccessFile) elements.nextElement();
260 collisionOut_ =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(collisionLogPath_)));
261 collisionDatOut_ =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(collisionLogDatPath_)));
262 collisionLog_.currentPos_ = 0;
263 collisionLog_.position_.clear();
264 collisionLog_.position_.add(0);
265 collisionLog_.numRecords_ = 0;
266 collisionLog_.createLogHeader(collisionOut_);
270 collisionIn_ =
new RandomAccessFile(collisionLogPath_,
"r");
271 collisionDatIn_ =
new RandomAccessFile(collisionLogDatPath_,
"r");
275 if ( collisionDatIn_ !=
null ){
276 collisionDatIn_.close();
277 collisionDatIn_ =
null;
279 if ( collisionIn_ !=
null ){
280 collisionIn_.close();
286 if(collisionOut_ ==
null || collisionDatOut_ ==
null )
288 collisionDatOut_.close();
289 collisionOut_.close();
290 collisionDatOut_ =
null;
291 collisionOut_ =
null;
294 collisionLog_.endTime_ = time_.
getUtime();
296 RandomAccessFile file =
new RandomAccessFile(collisionLogPath_,
"rw");
297 collisionLog_.outTimesAndRecalculation(file);
298 collisionLog_.outPositions(file);
300 }
catch (FileNotFoundException ex) {
301 throw new IOException();
306 LogHeader header = (LogHeader) header_.get(objectName);
310 int size = header.dataFormat_.length / 2;
311 String[] format =
new String[
size];
312 for (
int i = 0;
i <
size;
i++) {
313 format[
i] = header.dataFormat_[
i * 2];
323 LogHeader header = (LogHeader) header_.get(objectName);
325 if (
data.length == (header.recordSize_ / LogHeader.FLOAT_DATA_SIZE)) {
327 DataOutputStream out = (DataOutputStream) writeFile_.get(objectName);
328 for (
int i = 0;
i <
data.length;
i++) {
329 out.writeFloat(
data[
i]);
332 }
catch (IOException ex) {
336 header.numRecords_++;
338 throw new LogFileOutputException(
"data length error.");
347 for (
int i = 0;
i <
data.length;
i++) {
348 collisionDatOut_.writeFloat((
float)
data[
i].normal[0]);
349 collisionDatOut_.writeFloat((
float)
data[
i].normal[1]);
350 collisionDatOut_.writeFloat((
float)
data[
i].normal[2]);
352 collisionDatOut_.writeFloat((
float)
data[
i].position[1]);
353 collisionDatOut_.writeFloat((
float)
data[
i].position[2]);
354 collisionDatOut_.writeDouble(
data[
i].idepth);
356 collisionDatOut_.flush();
358 collisionLog_.position_.add(collisionLog_.currentPos_);
359 collisionLog_.numRecords_++;
366 byte[]
buffer =
new byte[1024 * 1024];
369 for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
370 LogHeader header = (LogHeader) elements.nextElement();
371 String srcFilePath =
new String( srcDir +
File.separator + header.objectName_ + POSTFIX);
372 File srcFile =
new File(srcFilePath);
373 FileInputStream srcInStream =
new FileInputStream(srcFile);
374 srcInStream.skip(header.headerSize_);
375 leftSize = srcFile.length() - header.headerSize_;
376 header.numRecords_ += leftSize / header.recordSize_;
377 DataOutputStream destOutStream = writeFile_.get(header.objectName_);
378 while (leftSize > 0) {
379 int readSize = srcInStream.read(buffer);
380 destOutStream.write(buffer, 0, readSize);
381 leftSize -= readSize;
387 File col =
new File(srcDir +
File.separator + COLLISION_LOG_NAME);
388 DataInputStream inCol =
new DataInputStream(
new FileInputStream(col));
389 CollisionLogHeader localColLog =
new CollisionLogHeader();
392 localColLog.input(inCol);
394 ex.printStackTrace();
395 }
catch ( IOException ex){
401 collisionLog_.joinCollisionLogHeader(localColLog);
404 File colData =
new File(srcDir +
File.separator + COLLISION_LOG_DAT_NAME);
405 FileInputStream fileInStream =
new FileInputStream(colData);
406 leftSize = colData.length();
407 while (leftSize > 0) {
408 int readSize = fileInStream.read(buffer);
409 collisionDatOut_.write(buffer, 0, readSize);
410 leftSize -= readSize;
412 fileInStream.close();
420 byte[]
buffer =
new byte[1024 * 1024];
423 for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
424 LogHeader header = (LogHeader) elements.nextElement();
425 String srcFilePath =
new String( srcDir +
File.separator + header.objectName_ + POSTFIX);
426 File srcFile =
new File(srcFilePath);
427 FileInputStream srcInStream =
new FileInputStream(srcFile);
428 srcInStream.skip(header.headerSize_ + header.recordSize_ * changePos);
429 leftSize = srcFile.length() - header.headerSize_ - header.recordSize_ * changePos;
430 DataOutputStream destOutStream = writeFile_.get(header.objectName_);
431 while (leftSize > 0) {
432 int readSize = srcInStream.read(buffer);
433 destOutStream.write(buffer, 0, readSize);
434 leftSize -= readSize;
440 File col =
new File(srcDir +
File.separator + COLLISION_LOG_NAME);
441 DataInputStream inCol =
new DataInputStream(
new FileInputStream(col));
442 CollisionLogHeader localColLog =
new CollisionLogHeader();
445 localColLog.input(inCol);
447 ex.printStackTrace();
448 }
catch ( IOException ex){
454 collisionLog_.separateCollisionLogHeader(localColLog,changePos);
457 final int offset = collisionLog_.position_.get(changePos);
458 File colData =
new File(srcDir +
File.separator + COLLISION_LOG_DAT_NAME);
459 FileInputStream fileInStream =
new FileInputStream(colData);
460 fileInStream.skip( offset );
461 leftSize = colData.length() - offset;
462 while (leftSize > 0) {
463 int readSize = fileInStream.read(buffer);
464 collisionDatOut_.write(buffer, 0, readSize);
465 leftSize -= readSize;
467 fileInStream.close();
476 public void save(String fileName, String prjFileName)
throws IOException {
478 ZipOutputStream
zip =
new ZipOutputStream(
new FileOutputStream(
new File(fileName)));
481 for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
482 LogHeader header = (LogHeader) elements.nextElement();
498 }
catch (IOException ex) {
506 FileInputStream fileInStream =
new FileInputStream(file);
508 byte[]
buffer =
new byte[1024 * 1024];
511 ZipEntry zipEntry =
new ZipEntry(zipPath);
512 zip.putNextEntry(zipEntry);
514 long leftSize = file.length();
515 while (leftSize > 0) {
516 int readSize = fileInStream.read(buffer);
517 zip.write(buffer, 0, readSize);
518 leftSize -= readSize;
520 fileInStream.close();
526 if (tmpdir !=
null) {
527 String abs_path = (
new File(path)).getAbsolutePath();
528 String tmp_abs_path = (
new File(tmpdir)).getAbsolutePath();
529 if(abs_path.startsWith(tmp_abs_path)){
530 String
ret = abs_path.substring(tmp_abs_path.length());
531 if(ret.startsWith(
File.separator)){
532 ret = ret.substring(
File.separator.length());
546 ZipFile zipFile =
new ZipFile(fileName);
547 int entrySize = zipFile.size();
554 ZipInputStream
zip =
new ZipInputStream(
new FileInputStream(fileName));
555 byte[]
buffer =
new byte[1024];
556 for (
int i = 0;
i < entrySize;
i++) {
557 String entry = zip.getNextEntry().getName();
558 if(!(
new File(entry)).isAbsolute()){
559 entry = tmpdir +
File.separator + entry;
562 FileOutputStream out =
new FileOutputStream(entry);
563 while (zip.available() == 1) {
564 int readSize = zip.read(buffer, 0, 1024);
567 out.write(buffer, 0, readSize);
571 if (entry.equals(prjFile) ||
572 entry.contains(
new File(collisionLogDatPath_).getName()) ) {
578 if (entry.contains(
new File(collisionLogPath_).getName())) {
580 in =
new DataInputStream(
new FileInputStream(entry));
581 collisionLog_ =
new CollisionLogHeader();
582 collisionLog_.input(in);
590 in =
new DataInputStream(
new FileInputStream(entry));
591 LogHeader header =
new LogHeader();
593 header_.put(header.objectName_, header);
595 if (header.getVersion() <= 100) {
597 header.setFileSize(file.length());
599 header.calcUnitSize();
608 }
catch (IOException ex) {
609 ex.printStackTrace();
610 throw new FileOpenFailException();
614 public void saveCSV(String fileName, String ObjectName)
throws FileOpenFailException {
616 LogHeader header = (LogHeader) header_.get(ObjectName);
617 if (header ==
null) {
618 throw new FileOpenFailException();
620 final long nLine = (
new File(
getTempFilePath(header.objectName_)).length() - header.headerSize_) / header.recordSize_;
621 DataInputStream in =
new DataInputStream(
new FileInputStream(
getTempFilePath(header.objectName_)));
622 PrintWriter out =
new PrintWriter(
new FileWriter(fileName));
624 out.println(
"Software Version, " + String.valueOf(header.version_[0]) +
"." + String.valueOf(header.version_[1]) +
"." + String.valueOf(header.version_[2]) +
"." + String.valueOf(header.version_[3]));
625 out.println(
"Header Size[byte], " + header.headerSize_);
626 out.println(
"Simulation Total Time[s], " + (
double) header.totalTime_ / 1000000.0);
627 out.println(
"Simulation Start Time[s], " + (
double) header.startTime_ / 1000000.0);
628 out.println(
"Simulation End Time[s], " + (nLine > 0 ?
get(ObjectName, nLine - 1)[0] : 0 ));
629 out.println(
"TimeStep[s], " + (
double) header.timeStep_ / 1000000.0);
632 if(!methodStr.equals(
"")){
633 out.println(
"Integration Method, " + methodStr);
635 out.println(
"Integration Method, " + header.method_);
637 out.println(
"Record Size[byte], " + header.recordSize_);
639 for (
int i = 0;
i < header.dataFormat_.length / 2;
i++) {
640 String fmt = header.dataFormat_[
i * 2 + 1];
641 int start = fmt.indexOf(
'[') + 1;
642 int end = fmt.indexOf(
']');
645 len = Integer.parseInt(fmt.substring(start, end));
647 out.print(header.dataFormat_[
i * 2]);
648 if (
i != header.dataFormat_.length / 2 - 1)
651 for (
int j = 0; j < len; j++) {
652 out.print(header.dataFormat_[
i * 2] +
"[" + j +
"]");
653 if (!(
i == header.dataFormat_.length / 2 - 1 && j == len - 1))
659 in.skip(header.headerSize_);
660 for (
long i = 0;
i < nLine;
i++) {
661 for (
long j = 0; j < header.recordSize_ / LogHeader.FLOAT_DATA_SIZE - 1; j++) {
662 out.print(in.readFloat() +
",");
665 out.println(in.readFloat());
670 }
catch (IOException ex) {
671 ex.printStackTrace();
672 throw new FileOpenFailException();
677 Enumeration elements = header_.elements();
678 while (elements.hasMoreElements()) {
679 LogHeader header = (LogHeader) elements.nextElement();
680 if (header.numRecords_ <= recordNum) {
687 if (collisionLog_.numRecords_ <= recordNum) {
698 return header_.size();
702 LogHeader header = (LogHeader) header_.get(objectName);
703 return header.recordSize_ / LogHeader.FLOAT_DATA_SIZE;
719 if (readFile_ ==
null) {
724 int numItems = dataModelArray.length;
725 ArrayList<String> objList =
new ArrayList<String>();
726 HashMap<String, ArrayList<DataSeries>> dsListMap =
new HashMap<String, ArrayList<DataSeries>>();
727 HashMap<String, ArrayList<Object>> indexListMap =
new HashMap<String, ArrayList<Object>>();
728 HashMap<String, ArrayList<Integer>> posListMap =
new HashMap<String, ArrayList<Integer>>();
729 HashMap<String, ArrayList<Integer>> sizeListMap =
new HashMap<String, ArrayList<Integer>>();
730 for (
int i = 0;
i < numItems;
i++) {
734 if (obj ==
null || obj.equals(
"")) {
741 Object ind = ((Map) indexMapMap_.get(obj)).
get(di.
node +
"." + di.
attribute + (di.
index >= 0 ?
"." + di.
index :
""));
744 ArrayList<DataSeries> dsList = dsListMap.get(obj);
746 ArrayList<Object> indList = indexListMap.get(obj);
748 ArrayList<Integer> posList = posListMap.get(obj);
750 ArrayList<Integer> sizeList = sizeListMap.get(obj);
751 if (dsList ==
null) {
753 dsList =
new ArrayList<DataSeries>();
754 indList =
new ArrayList<Object>();
755 posList =
new ArrayList<Integer>();
756 sizeList =
new ArrayList<Integer>();
757 dsListMap.put(obj, dsList);
758 indexListMap.put(obj, indList);
759 posListMap.put(obj, posList);
760 sizeListMap.put(obj, sizeList);
766 posList.add(
new Integer(pos));
767 sizeList.add(
new Integer(size));
770 int numObjs = indexListMap.size();
771 for (
int i = 0;
i < numObjs;
i++) {
772 String
obj = (String) objList.get(
i);
773 LogHeader header = (LogHeader) header_.get(obj);
774 int itemsPerRec = header.recordSize_ / LogHeader.FLOAT_DATA_SIZE;
775 double[] record =
new double[itemsPerRec];
777 long recNo = origin + offset;
779 ArrayList dsList = (ArrayList) dsListMap.get(obj);
780 ArrayList indList = (ArrayList) indexListMap.get(obj);
781 ArrayList posList = (ArrayList) posListMap.get(obj);
782 ArrayList sizeList = (ArrayList) sizeListMap.get(obj);
783 int itemCount = dsList.size();
784 int[] posArray =
new int[itemCount];
786 for (
int j = 0; j < itemCount; j++) {
787 posArray[j] = ((Integer) posList.get(j)).intValue();
790 RandomAccessFile file = (RandomAccessFile) readFile_.get(obj);
792 synchronized (file) {
797 file.seek((
long) header.headerSize_);
798 }
else if (recNo < header.numRecords_) {
800 file.seek((
long) header.headerSize_ + header.recordSize_ * recNo);
804 for (
int rec = 0; rec < count; rec++) {
808 if (recNo < 0 || recNo >= header.numRecords_) {
811 for (
int k = 0; k < itemsPerRec; k++) {
812 record[k] = Double.NaN;
815 for (
int k = 0; k < itemsPerRec; k++) {
816 record[k] = file.readFloat();
825 data[posArray[
item]] = record[((Integer) indList.get(
item)).intValue()];
826 if (posArray[
item] < (((Integer) sizeList.get(
item)).intValue() - 1)) {
834 }
catch (IOException ex) {
835 ex.printStackTrace();
841 private HashMap<String, ArrayList<DataSeries>>
dsListMap_ =
new HashMap<String, ArrayList<DataSeries>>();
842 private HashMap<String, ArrayList<Integer>>
indexListMap_ =
new HashMap<String, ArrayList<Integer>>();
847 if(indexMapMap_ ==
null || indexMapMap_.isEmpty())
850 indexListMap_.clear();
851 for (
int i = 0;
i < dataModelArray.length;
i++) {
855 if (obj ==
null || obj.equals(
"")) {
859 if(attribute.equals(
"attitude"))
860 attribute =
"rotation";
861 Integer ind = ((Map<String, Integer>) indexMapMap_.get(obj)).
get(di.
node +
"." + attribute + (di.
index >= 0 ?
"." + di.
index :
""));
863 ArrayList<DataSeries> dsList = dsListMap_.get(obj);
864 ArrayList<Integer> indexList = indexListMap_.get(obj);
866 dsList =
new ArrayList<DataSeries>();
867 dsListMap_.put(obj, dsList);
868 indexList =
new ArrayList<Integer>();
869 indexListMap_.put(obj, indexList);
875 Iterator<String> it = dsListMap_.keySet().iterator();
876 while (it.hasNext()) {
877 String
obj = it.next();
878 ArrayList<DataSeries> dsList = dsListMap_.get(obj);
879 int dsNum = dsList.size();
880 dataPos_ =
new int[dsNum];
881 data_ =
new double[dsNum][];
882 dsSize_ =
new int[dsNum];
883 for(
int i=0;
i<dsNum;
i++){
892 public void getData(
long origin,
int offset,
int count){
893 Iterator<String> it = dsListMap_.keySet().iterator();
894 while (it.hasNext()) {
895 String
obj = it.next();
896 ArrayList<DataSeries> dsList = dsListMap_.get(obj);
897 int dsNum = dsList.size();
898 for(
int i=0;
i<dsNum;
i++){
907 private void _getData(String
obj,
long recNo,
int count, Integer[] itemIndex,
double[][]
data,
int[] dataPos,
int[] dsSize){
908 LogHeader header = (LogHeader) header_.get(obj);
909 int itemsPerRec = header.recordSize_ / LogHeader.FLOAT_DATA_SIZE;
910 double[] record =
new double[itemsPerRec];
911 RandomAccessFile file = (RandomAccessFile) readFile_.get(obj);
912 synchronized (file) {
915 for (
int rec = 0; rec < count; rec++) {
918 if (recNo < 0 || recNo >= header.numRecords_)
919 data[
item][dataPos[
item]] = Double.NaN;
921 file.seek((
long) header.headerSize_ + header.recordSize_ * recNo + LogHeader.FLOAT_DATA_SIZE * itemIndex[
item]);
922 data[
item][dataPos[
item]] = file.readFloat();
924 if (dataPos[
item] < dsSize[
item]-1) {
932 }
catch (IOException ex) {
933 ex.printStackTrace();
939 String[] format = header.dataFormat_;
940 Map<String, Integer> indexMap =
new HashMap<String, Integer>();
942 for (
int i = 0;
i < format.length / 2;
i++) {
943 if (header.getUnitSize(
i) == 0) {
944 indexMap.put(format[
i * 2],
new Integer(index));
947 for (
int j = 0; j < header.getUnitSize(
i); j++) {
948 StringBuffer attrName =
new StringBuffer(format[
i * 2]);
949 attrName.append(
'.');
951 indexMap.put(attrName.toString(),
new Integer(index));
956 indexMapMap_.put(header.objectName_, indexMap);
969 public byte[] version_;
970 public int headerSize_;
971 public long totalTime_;
972 public long startTime_;
973 public long endTime_;
974 public long timeStep_;
976 public int recordSize_;
977 public int numRecords_;
978 public byte[] reserved_;
979 public byte[] reserved_v1_0_;
982 public String objectName_;
983 public String[] dataFormat_;
985 public int[] unitSize_;
987 private static final long DEFULT_TOTAL_TIME_MSEC = 20000;
988 private static final long DEFULT_STEP_TIME_MSEC = 1;
989 private static final int VERSION_DATA_SIZE = 4;
990 private static final int INT_DATA_SIZE = 4;
991 private static final int LONG_DATA_SIZE = 8;
992 private static final int FLOAT_DATA_SIZE = 4;
993 private static final int FIXED_PART_SIZE = 64;
994 private static final int RESERVED_DATA_SIZE =
1001 private static final int RESERVED_DATA_SIZE_V1_0 =
1008 private static final int END_TIME_SEEK_POINT =
1009 (VERSION_DATA_SIZE + INT_DATA_SIZE + LONG_DATA_SIZE * 2);
1011 private static final int NUM_RECORDS_SEEK_POINT =
1012 (VERSION_DATA_SIZE + INT_DATA_SIZE * 3 + LONG_DATA_SIZE * 4);
1015 reserved_ =
new byte[RESERVED_DATA_SIZE];
1018 LogHeader(String objectName, String[] format)
1021 reserved_ =
new byte[RESERVED_DATA_SIZE];
1022 objectName_ = objectName;
1023 dataFormat_ = format;
1026 headerSize_ = FIXED_PART_SIZE;
1027 headerSize_ += objectName_.length() + 1;
1028 for (
int i = 0;
i < dataFormat_.length;
i ++) {
1029 headerSize_ += dataFormat_[
i].length() + 1;
1032 unitSize_ =
new int[dataFormat_.length / 2];
1036 for (
int i = 0;
i < dataFormat_.length / 2;
i ++) {
1037 if (!dataFormat_[
i * 2 + 1].startsWith(
"float")) {
1040 if (dataFormat_[
i * 2 + 1].equals(
"float")) {
1045 unitSize_[
i] = Integer.parseInt(
1046 dataFormat_[
i * 2 + 1].substring(
1047 dataFormat_[
i * 2 + 1].indexOf(
'[') + 1,
1048 dataFormat_[
i * 2 + 1].indexOf(
']')
1052 recordSize_ += unitSize_[
i];
1053 }
catch (NumberFormatException ex) {
1055 }
catch (StringIndexOutOfBoundsException ex) {
1060 recordSize_ *= FLOAT_DATA_SIZE;
1063 public int getVersion() {
1065 version_[0] * 1000 +
1072 public void output(DataOutputStream out)
1087 version_ =
new byte[] {0, 3, 1, 0};
1088 out.write(version_, 0, VERSION_DATA_SIZE);
1089 out.writeInt(headerSize_);
1090 out.writeLong(totalTime_);
1091 out.writeLong(startTime_);
1092 out.writeLong(endTime_);
1093 out.writeLong(timeStep_);
1094 out.writeInt(method_);
1095 out.writeInt(recordSize_);
1096 out.writeInt(numRecords_);
1097 out.write(reserved_, 0, RESERVED_DATA_SIZE);
1098 out.writeBytes(objectName_);
1100 for (
int i = 0;
i < dataFormat_.length;
i ++) {
1102 out.writeBytes(dataFormat_[
i]);
1108 public void input(DataInputStream in)
1111 version_ =
new byte[4];
1112 in.readFully(version_);
1113 if (getVersion() <= 100) {
1114 reserved_v1_0_ =
new byte[RESERVED_DATA_SIZE_V1_0];
1117 headerSize_ = in.readInt();
1119 if (getVersion() <= 100) {
1120 totalTime_ =
new Time(in.readDouble()).getUtime();
1121 startTime_ =
new Time(in.readDouble()).getUtime();
1122 endTime_ =
new Time(in.readDouble()).getUtime();
1123 timeStep_ = in.readInt();
1125 totalTime_ = in.readLong();
1126 startTime_ = in.readLong();
1127 endTime_ = in.readLong();
1128 timeStep_ = in.readLong();
1130 method_ = in.readInt();
1131 recordSize_ = in.readInt();
1133 if (getVersion() <= 100) {
1134 in.readFully(reserved_v1_0_);
1136 numRecords_ = in.readInt();
1137 in.readFully(reserved_);
1152 byte[] readBuffer =
new byte[headerSize_ - FIXED_PART_SIZE];
1153 in.readFully(readBuffer);
1157 for (ptr = 0; ptr < readBuffer.length; ptr ++) {
1158 if (readBuffer[ptr] == 0) {
1159 objectName_ =
new String(readBuffer, 0, ptr);
1167 for (
int j = ptr; j < readBuffer.length; j ++) {
1168 if (readBuffer[j] == 0) counter ++;
1172 dataFormat_ =
new String[counter];
1174 for (
int j = ptr; j < readBuffer.length; j ++) {
1175 if (readBuffer[j] == 0) {
1176 dataFormat_[counter] =
new String(readBuffer, ptr, j - ptr);
1183 public void calcUnitSize()
throws LogFileFormatException {
1184 unitSize_ =
new int[dataFormat_.length / 2];
1187 for (
int i = 0;
i < dataFormat_.length / 2;
i ++) {
1188 if (!dataFormat_[
i * 2 + 1].startsWith(
"float")) {
1189 throw new LogFileFormatException();
1191 if (dataFormat_[
i * 2 + 1].equals(
"float")) {
1195 unitSize_[
i] = Integer.parseInt(
1196 dataFormat_[
i * 2 + 1].substring(
1197 dataFormat_[
i * 2 + 1].indexOf(
'[') + 1,
1198 dataFormat_[
i * 2 + 1].indexOf(
']')
1201 }
catch (NumberFormatException ex) {
1202 throw new LogFileFormatException();
1203 }
catch (StringIndexOutOfBoundsException ex) {
1204 throw new LogFileFormatException();
1210 int getUnitSize(
int index) {
return unitSize_[index]; }
1212 public void outEndTime(RandomAccessFile file)
throws IOException {
1213 file.seek(END_TIME_SEEK_POINT);
1214 file.writeLong(endTime_);
1215 file.seek(NUM_RECORDS_SEEK_POINT);
1216 file.writeInt(numRecords_);
1223 void setFileSize(
long length) {
1224 if (recordSize_ == 0) {
1227 numRecords_ = (
int)((length - headerSize_) / recordSize_);
1235 class CollisionLogHeader {
1237 public byte[] version_;
1238 public int headerSize_;
1239 public long totalTime_;
1240 public long startTime_;
1241 public long endTime_;
1242 public long timeStep_;
1243 public byte[] reserved_;
1244 public byte[] reserved_v1_0_;
1247 public ArrayList<Integer> position_ =
new ArrayList<Integer>();
1249 public int currentPos_;
1250 public int numRecords_;
1252 private static final int VERSION_DATA_SIZE = 4;
1253 private static final int INT_DATA_SIZE = 4;
1254 private static final int LONG_DATA_SIZE = 8;
1255 private static final int FLOAT_DATA_SIZE = 4;
1256 private static final int FIXED_PART_SIZE = 64;
1257 private static final int RESERVED_DATA_SIZE =
1265 private static final int RESERVED_DATA_SIZE_V1_0 =
1273 private static final int END_TIME_SEEK_POINT =
1274 (VERSION_DATA_SIZE + INT_DATA_SIZE + LONG_DATA_SIZE * 2);
1276 private static final int TOTAL_TIME_SEEK_POINT =
1277 VERSION_DATA_SIZE + INT_DATA_SIZE;
1279 public CollisionLogHeader() {
1280 reserved_ =
new byte[RESERVED_DATA_SIZE];
1284 reserved_ =
new byte[RESERVED_DATA_SIZE];
1290 headerSize_ = FIXED_PART_SIZE + INT_DATA_SIZE;
1292 public int getVersion() {
1294 version_[0] * 1000 +
1301 public void input(DataInputStream in)
1304 version_ =
new byte[4];
1305 in.readFully(version_);
1306 if (getVersion() <= 100) {
1307 reserved_v1_0_ =
new byte[RESERVED_DATA_SIZE_V1_0];
1310 headerSize_ = in.readInt();
1312 if (getVersion() <= 100) {
1313 totalTime_ =
new Time(in.readDouble()).getUtime();
1314 startTime_ =
new Time(in.readDouble()).getUtime();
1315 endTime_ =
new Time(in.readDouble()).getUtime();
1316 timeStep_ = in.readInt();
1318 totalTime_ = in.readLong();
1319 startTime_ = in.readLong();
1320 endTime_ = in.readLong();
1321 timeStep_ = in.readLong();
1324 if (getVersion() <= 100) {
1325 in.readFully(reserved_v1_0_);
1327 in.readFully(reserved_);
1331 (
int)((endTime_ - startTime_) / timeStep_) +
1332 (((endTime_ - startTime_) % timeStep_) > timeStep_>>1 ? 1 : 0) + 1;
1333 for (
int i = 0;
i < frameSize + 1; ++
i) {
1334 position_.add(in.readInt());
1337 numRecords_ = frameSize;
1340 public void output(DataOutputStream out)
throws IOException {
1342 version_ =
new byte[] {0, 3, 1, 0};
1343 out.write(version_, 0, VERSION_DATA_SIZE);
1344 out.writeInt(headerSize_);
1345 out.writeLong(totalTime_);
1346 out.writeLong(startTime_);
1347 out.writeLong(endTime_);
1348 out.writeLong(timeStep_);
1349 out.write(reserved_, 0, RESERVED_DATA_SIZE);
1350 for (
int i = 0;
i < position_.size();
i ++) {
1351 out.writeInt(position_.get(
i));
1355 public void createLogHeader(DataOutputStream out)
throws IOException {
1357 version_ =
new byte[] {0, 3, 1, 0};
1358 out.write(version_, 0, VERSION_DATA_SIZE);
1359 out.writeInt(headerSize_);
1360 out.writeLong(totalTime_);
1361 out.writeLong(startTime_);
1362 out.writeLong(endTime_);
1363 out.writeLong(timeStep_);
1364 out.write(reserved_, 0, RESERVED_DATA_SIZE);
1365 for (
int i = 0;
i < position_.size();
i ++) {
1366 out.writeInt(position_.get(
i));
1371 public void outTimesAndRecalculation(RandomAccessFile file)
throws IOException {
1372 file.seek(TOTAL_TIME_SEEK_POINT);
1373 file.writeLong(endTime_ - startTime_);
1374 file.writeLong(startTime_);
1375 file.writeLong(endTime_);
1378 public void outPositions(RandomAccessFile file)
throws IOException {
1379 file.seek(FIXED_PART_SIZE);
1380 for (
int i = 0;
i < position_.size();
i ++) {
1381 file.writeInt(position_.get(
i));
1385 public boolean joinCollisionLogHeader(CollisionLogHeader ref){
1386 if( ref.equals(
this) ){
1388 }
else if( ref.timeStep_ != timeStep_) {
1391 endTime_ = ref.endTime_;
1392 final int size = ref.position_.size();
1393 for(
int i = 1;
i <
size; ++
i){
1394 position_.add(ref.position_.get(
i) + currentPos_);
1396 numRecords_ += size - 1;
1400 public boolean separateCollisionLogHeader(CollisionLogHeader ref,
final int changePos){
1401 if( ref.equals(
this) ){
1403 }
else if( ref.timeStep_ != timeStep_) {
1406 endTime_ = ref.endTime_;
1408 final int size = ref.position_.size();
1409 final int offsetNum = ref.position_.get(changePos);
1410 for(
int i = changePos;
i <
size; ++
i){
1411 position_.add(ref.position_.get(
i) - offsetNum);
1413 numRecords_ = position_.size() - 1;
1420 public float[]
get(String objectName,
long record)
throws IOException {
1421 if (readFile_ ==
null)
return null;
1423 LogHeader header = (LogHeader)header_.get(objectName);
1426 RandomAccessFile file = (RandomAccessFile)readFile_.get(objectName);
1428 float[]
data =
new float[header.recordSize_ / LogHeader.FLOAT_DATA_SIZE];
1431 synchronized (file) {
1432 file.seek((
long)header.headerSize_ + header.recordSize_ * record);
1433 for (
int i = 0;
i < data.length;
i ++)
1434 data[
i] = file.readFloat();
1436 }
catch (EOFException ex) {
1437 ex.printStackTrace();
1438 }
catch (IOException ex) {
1446 if (collisionLog_.position_.size() < frameNum + 2) {
1449 int size = collisionLog_.position_.get(frameNum + 1) - collisionLog_.position_.get(frameNum);
1451 Enumeration elements = header_.elements();
1452 LogHeader header = (LogHeader)elements.nextElement();
1453 int version = header.getVersion();
1454 if (version <= 110){
1459 if ((size % data_size) != 0 || size <= 0)
1463 collisionDatIn_.seek(collisionLog_.position_.get(frameNum));
1464 CollisionPoint[]
data =
new CollisionPoint[
size];
1465 for (
int i = 0;
i <
size;
i ++) {
1466 data[
i] =
new CollisionPoint();
1467 data[
i].normal =
new double[3];
1468 data[
i].normal[0] = collisionDatIn_.readFloat();
1469 data[
i].normal[1] = collisionDatIn_.readFloat();
1470 data[
i].normal[2] = collisionDatIn_.readFloat();
1471 data[
i].position =
new double[3];
1472 data[
i].position[0] = collisionDatIn_.readFloat();
1473 data[
i].position[1] = collisionDatIn_.readFloat();
1474 data[
i].position[2] = collisionDatIn_.readFloat();
1475 if (version <= 110){
1476 data[
i].idepth = 0.01;
1478 data[
i].idepth = collisionDatIn_.readDouble();
1486 return collisionLog_.position_.get(frameNum + 1) - collisionLog_.position_.get(frameNum);
1487 }
catch (IndexOutOfBoundsException ex) {
1488 ex.printStackTrace();
1494 LogHeader header = (LogHeader)header_.get(objectName);
1495 return header.numRecords_;
1503 File pf = f.getParentFile();
1504 if (!pf.isDirectory())
1506 if (!f.isDirectory())
1527 return ((Integer) (((Map) indexMapMap_.get(obj)).
get(member))).intValue();
1531 if(
this != logger ){
1541 for(
int i= 0;
i < INTEGRATION_METHOD_NAMES.length;
i++){
1542 if(methodStr.equals(INTEGRATION_METHOD_NAMES[
i])){
1549 if(methodInt < 0 || INTEGRATION_METHOD_NAMES.length <= methodInt)
1551 return INTEGRATION_METHOD_NAMES[methodInt];
static final int COLLISION_DATA_SIZE
void openCollisionLogAsRead()
HashMap< String, ArrayList< DataSeries > > dsListMap_
RandomAccessFile collisionIn_
void addLogObject(String objectName, String[] format)
#define null
our own NULL pointer
Hashtable< String, RandomAccessFile > readFile_
String getTempFilePath(String objectName)
String getIntegrationMethodStr()
final DataSeries dataSeries
void closeCollisionLogAsWrite()
RandomAccessFile collisionDatIn_
int str2IntIntegrationMethod(String methodStr)
void openCollisionLogAsWrite()
Hashtable< String, DataOutputStream > writeFile_
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
void _getData(String obj, long recNo, int count, Integer[] itemIndex, double[][] data, int[] dataPos, int[] dsSize)
void getSimulationTime(SimulationTime time)
png_bytep png_bytep png_size_t length
void putCollisionPointData(CollisionPoint[] data)
void extendTime(SimulationTime time)
void closeCollisionLogAsRead()
Map< String, Map< String, Integer > > indexMapMap_
HashMap< String, ArrayList< Integer > > indexListMap_
String _getRelativePath(String path)
DataOutputStream collisionDatOut_
CollisionLogHeader collisionLog_
CollisionPoint [] getCollisionPointData(int frameNum)
void _makeIndexMapMap(LogHeader header)
Hashtable< String, LogHeader > header_
static final String COLLISION_LOG_DAT_NAME
static final String COLLISION_LOG_NAME
void save(String fileName, String prjFileName)
static final String NONAME_OBJECT
void _addFileToZipEntry(ZipOutputStream zip, File file)
String [] getDataFormat(String objectName)
void getData(long origin, int offset, int count, DataModel[] dataModelArray)
LogManager(LogManager logger)
void initGetData(DataModel[] dataModelArray)
void initCollisionLog(SimulationTime time)
void load(String fileName, String prjFile)
void openAsWrite(SimulationTime time, String method)
void put(String objectName, float[] data)
int getDataLength(String objectName)
static final String [] INTEGRATION_METHOD_NAMES
void _initTempInstance(LogManager logger)
void setUtime(long utime)
void separateLogs(final int changePos)
boolean existRecord(int recordNum)
static void main(String[] args)
png_infop png_bytep buffer
int getRecordNum(String objectName)
int getIndex(String obj, String member)
void getData(long origin, int offset, int count)
String collisionLogDatPath_
int getCollisionPointDataSize(int frameNum)
String int2StrIntegrationMethod(int methodInt)
void setTempDir(String tmp)
output(gif_dest_ptr dinfo, int code)
static final String POSTFIX
DataOutputStream collisionOut_
void saveCSV(String fileName, String ObjectName)