LogManager.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
18 package com.generalrobotix.ui.view.graph;
19 
20 import java.util.*;
21 import java.io.*;
22 import java.util.zip.*;
23 
24 import jp.go.aist.hrp.simulator.CollisionPoint;
25 
38 public class LogManager {
39  //--------------------------------------------------------------------
40  // 定数
41  public static final String COLLISION_LOG_NAME = "CollisionData.col";
42  public static final String COLLISION_LOG_DAT_NAME = "CollisionData.dat";
43  private static final String POSTFIX = ".tmp";
44  private static final int COLLISION_DATA_SIZE = 6 * 4 + 1 * 8;
45  private static final String NONAME_OBJECT = "_noname";
46 
47  //--------------------------------------------------------------------
48  // インスタンス変数
49  private Hashtable<String, LogHeader> header_;
50  private Hashtable<String, DataOutputStream> writeFile_;
51  private Hashtable<String, RandomAccessFile> readFile_;
52  private Map<String, Map<String, Integer> > indexMapMap_;
53  private CollisionLogHeader collisionLog_;
54  private Time time_;
55  private DataOutputStream collisionOut_ = null;
56  private RandomAccessFile collisionIn_ = null;
57  private DataOutputStream collisionDatOut_ = null;
58  private RandomAccessFile collisionDatIn_ = null;
59  private String collisionLogPath_ = new String(COLLISION_LOG_NAME);
60  private String collisionLogDatPath_ = new String(COLLISION_LOG_DAT_NAME);
61 
62  private String tmpdir;
63 
64  //--------------------------------------------------------------------
65  // 公開メソッド
66  public static void main(String[] args) {
67  LogManager log = new LogManager();
68  log.init();
69  try {
70  log.addLogObject("test", new String[] {
71  "test1", "float", "test2", "float[3]"
72  });
73  } catch (LogFileFormatException ex) {
74  ex.printStackTrace();
75  }
76  }
77 
78  public LogManager(){
79 
80  }
81 
82  public LogManager(LogManager logger) {
83  String tmpdir = System.getProperty("TEMP");
84  if (logger != null) {
85  if(tmpdir != null)
86  setTempDir(tmpdir);
87  _initTempInstance(logger);
88  }
89  }
90 
94  public void init() {
95  header_ = new Hashtable<String, LogHeader>();
96  indexMapMap_ = new HashMap<String, Map<String, Integer>>();
97  time_ = new Time();
98  closeReads();
99  }
100 
104  public void closeReads() {
105  try{
106  closeAsRead();
108  } catch (IOException ex){
109  ex.printStackTrace();
110  } catch (Exception ex){
111  ex.printStackTrace();
112  }
113  }
114 
118  public void closeWrites() {
119  try{
120  closeAsWrite();
122  } catch (IOException ex){
123  ex.printStackTrace();
124  } catch (Exception ex){
125  ex.printStackTrace();
126  }
127  }
128 
129 
130  private String getTempFilePath(String objectName) {
131  // String tmpdir = System.getProperty("TEMP");
132  if (tmpdir != null) {
133  return tmpdir + File.separator + objectName + POSTFIX;
134  } else {
135  return objectName + POSTFIX;
136  }
137  }
138 
139  public String getIntegrationMethodStr(){
140  Enumeration elements = header_.elements();
141  if (!elements.hasMoreElements()) return "";
142  LogHeader header = (LogHeader)elements.nextElement();
143  return int2StrIntegrationMethod(header.method_);
144  }
145 
149  public void getSimulationTime(SimulationTime time) {
150  // ヘッダのシミュレーション時間に関する項目はすべてのオブジェクトに
151  // 共通のはずだから、1つだけヘッダ情報を取り出してSimulationTime
152  // に変換
153  Enumeration elements = header_.elements();
154  if (!elements.hasMoreElements()) return;
155  LogHeader header = (LogHeader)elements.nextElement();
156  //time.totalTime_.setUtime(header.totalTime_);
157  time.totalTime_.setUtime(header.endTime_ - header.startTime_);
158  time.startTime_.setUtime(header.startTime_);
159  time.timeStep_.setUtime(header.timeStep_);
160  }
161 
166  public void addLogObject(String objectName, String[] format) throws LogFileFormatException {
167  LogHeader header = new LogHeader(objectName, format);
168  header_.put(objectName, header);
169  _makeIndexMapMap(header);
170  }
171 
175  public void initCollisionLog(SimulationTime time) {
176  collisionLog_ = new CollisionLogHeader(time);
177  }
178 
179  public void extendTime(SimulationTime time){
180  collisionLog_.totalTime_ = time.totalTime_.getUtime();
181  }
182 
188  public void openAsWrite(SimulationTime time, String method) throws IOException {
189  writeFile_ = new Hashtable<String, DataOutputStream>();
190  for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
191  try {
192  LogHeader header = (LogHeader) elements.nextElement();
193  header.totalTime_ = time.totalTime_.getUtime();
194  header.startTime_ = time.startTime_.getUtime();
195  header.timeStep_ = time.timeStep_.getUtime();
196  header.endTime_ = 0;
197  header.method_ = str2IntIntegrationMethod(method);
198  header.numRecords_ = 0;
199  // ヘッダの書込み
200  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getTempFilePath(header.objectName_))));
201 
202  header.output(out);
203  writeFile_.put(header.objectName_, out);
204  } catch (IOException ex) {
205  for (Enumeration elms = writeFile_.elements(); elms.hasMoreElements();) {
206  DataOutputStream out = (DataOutputStream) elms.nextElement();
207  out.close();
208  }
209  throw ex;
210  }
211  }
212  }
213 
217  public double closeAsWrite() throws IOException {
218  if(writeFile_ == null)
219  return 0;
220  for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
221  LogHeader header = (LogHeader) elements.nextElement();
222  DataOutputStream out = (DataOutputStream) writeFile_.get(header.objectName_);
223  out.close();
224 
225  // ヘッダに終了時間を書き込む
226  header.endTime_ = time_.getUtime();
227  RandomAccessFile file = new RandomAccessFile(getTempFilePath(header.objectName_), "rw");
228  header.outEndTime(file); // 終了時間までシーク
229  file.close();
230  }
231  writeFile_ = null;
232  return time_.getDouble();
233  }
234 
235  public void openAsRead() throws IOException, FileOpenFailException {
236  readFile_ = new Hashtable<String, RandomAccessFile>();
237  for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
238  LogHeader header = (LogHeader) elements.nextElement();
239  RandomAccessFile file = null;
240  try{
241  file = new RandomAccessFile(getTempFilePath(header.objectName_), "r");
242  }catch (IOException ex){
243  throw new FileOpenFailException(ex.getMessage());
244  }
245  readFile_.put(header.objectName_, file);
246  }
247  }
248 
249  public void closeAsRead() throws IOException {
250  if (readFile_ == null)
251  return;
252  for (Enumeration elements = readFile_.elements(); elements.hasMoreElements();) {
253  RandomAccessFile file = (RandomAccessFile) elements.nextElement();
254  file.close();
255  }
256  readFile_ = null;
257  }
258 
259  public void openCollisionLogAsWrite() throws IOException {
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_);
267  }
268 
269  public void openCollisionLogAsRead() throws IOException, FileNotFoundException {
270  collisionIn_ = new RandomAccessFile(collisionLogPath_, "r");
271  collisionDatIn_ = new RandomAccessFile(collisionLogDatPath_, "r");
272  }
273 
274  public void closeCollisionLogAsRead() throws IOException {
275  if ( collisionDatIn_ != null ){
276  collisionDatIn_.close();
277  collisionDatIn_ = null;
278  }
279  if ( collisionIn_ != null ){
280  collisionIn_.close();
281  collisionIn_ = null;
282  }
283  }
284 
285  public void closeCollisionLogAsWrite() throws IOException {
286  if(collisionOut_ == null || collisionDatOut_ == null )
287  return;
288  collisionDatOut_.close();
289  collisionOut_.close();
290  collisionDatOut_ = null;
291  collisionOut_ = null;
292 
293  // recordSize_を書き込む
294  collisionLog_.endTime_ = time_.getUtime();
295  try {
296  RandomAccessFile file = new RandomAccessFile(collisionLogPath_, "rw");
297  collisionLog_.outTimesAndRecalculation(file);
298  collisionLog_.outPositions(file);
299  file.close();
300  } catch (FileNotFoundException ex) {
301  throw new IOException();
302  }
303  }
304 
305  public String[] getDataFormat(String objectName) {
306  LogHeader header = (LogHeader) header_.get(objectName);
307  if (header == null)
308  return null;
309 
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];
314  }
315  return format;
316  }
317 
318  public void setTime(Time time) {
319  time_.set(time);
320  }
321 
322  public void put(String objectName, float[] data) throws LogFileOutputException, IOException {
323  LogHeader header = (LogHeader) header_.get(objectName);
324 
325  if (data.length == (header.recordSize_ / LogHeader.FLOAT_DATA_SIZE)) {
326  try {
327  DataOutputStream out = (DataOutputStream) writeFile_.get(objectName);
328  for (int i = 0; i < data.length; i++) {
329  out.writeFloat(data[i]);
330  }
331  out.flush();
332  } catch (IOException ex) {
333  closeAsWrite();
334  throw ex;
335  }
336  header.numRecords_++;
337  } else {
338  throw new LogFileOutputException("data length error.");
339  }
340  }
341 
342  public void putCollisionPointData(CollisionPoint[] data) throws IOException {
343  // int frameNum = (int)(time_.getUtime() / collisionLog_.timeStep_);
344  // System.out.println("putCollisionPointData(): frameNum=" +
345  // frameNum+":"+time_.getUtime()+":"+collisionLog_.timeStep_);
346 
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]);
351  collisionDatOut_.writeFloat((float) data[i].position[0]);
352  collisionDatOut_.writeFloat((float) data[i].position[1]);
353  collisionDatOut_.writeFloat((float) data[i].position[2]);
354  collisionDatOut_.writeDouble(data[i].idepth);
355  }
356  collisionDatOut_.flush();
357  collisionLog_.currentPos_ += data.length * COLLISION_DATA_SIZE;
358  collisionLog_.position_.add(collisionLog_.currentPos_);
359  collisionLog_.numRecords_++;
360  }
361 
362  public void jointLogs() throws IOException {
363  String srcDir = tmpdir;
364 
365  long leftSize = 0;
366  byte[] buffer = new byte[1024 * 1024];
367 
368  // 各モデルログファイルの結合処理
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;
382  }
383  srcInStream.close();
384  }
385 
386  // collisionLogの結合
387  File col = new File(srcDir + File.separator + COLLISION_LOG_NAME);
388  DataInputStream inCol = new DataInputStream(new FileInputStream(col));
389  CollisionLogHeader localColLog = new CollisionLogHeader();
390 
391  try{
392  localColLog.input(inCol);
393  } catch ( LogFileFormatException ex){
394  ex.printStackTrace();
395  } catch ( IOException ex){
396  throw ex;
397  } finally {
398  inCol.close();
399  }
400 
401  collisionLog_.joinCollisionLogHeader(localColLog);
402 
403  // collisionLogDatの結合
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;
411  }
412  fileInStream.close();
413  }
414 
415 
416  public void separateLogs(final int changePos) throws IOException {
417  String srcDir = tmpdir;
418 
419  long leftSize = 0;
420  byte[] buffer = new byte[1024 * 1024];
421 
422  // 各モデルログファイルの分離処理
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;
435  }
436  srcInStream.close();
437  }
438 
439  // this_.collisionLogの分離
440  File col = new File(srcDir + File.separator + COLLISION_LOG_NAME);
441  DataInputStream inCol = new DataInputStream(new FileInputStream(col));
442  CollisionLogHeader localColLog = new CollisionLogHeader();
443 
444  try{
445  localColLog.input(inCol);
446  } catch ( LogFileFormatException ex){
447  ex.printStackTrace();
448  } catch ( IOException ex){
449  throw ex;
450  } finally {
451  inCol.close();
452  }
453 
454  collisionLog_.separateCollisionLogHeader(localColLog,changePos);
455 
456  // this_.collisionLogDatの分離
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;
466  }
467  fileInStream.close();
468  }
469 
476  public void save(String fileName, String prjFileName) throws IOException {
477  try {
478  ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(new File(fileName)));
479 
480  // 各ログファイルを追加
481  for (Enumeration elements = header_.elements(); elements.hasMoreElements();) {
482  LogHeader header = (LogHeader) elements.nextElement();
483  _addFileToZipEntry(zip, new File( getTempFilePath(header.objectName_) ) );
484  zip.closeEntry();
485  // logFile.delete();
486  }
487 
488  // プロジェクトファイルの追加
489  _addFileToZipEntry(zip, new File(prjFileName) );
490 
491  // 干渉情報ログを追加
492  _addFileToZipEntry(zip, new File(collisionLogPath_) );
493  _addFileToZipEntry(zip, new File(collisionLogDatPath_) );
494 
495  zip.flush();
496  zip.closeEntry();
497  zip.close();
498  } catch (IOException ex) {
499  throw ex;
500  }
501  }
502 
503  private void _addFileToZipEntry(ZipOutputStream zip, File file)
504  throws IOException{
505  if (file.exists()) {
506  FileInputStream fileInStream = new FileInputStream(file);
507 
508  byte[] buffer = new byte[1024 * 1024];
509 
510  String zipPath = _getRelativePath(file.getPath());
511  ZipEntry zipEntry = new ZipEntry(zipPath);
512  zip.putNextEntry(zipEntry);
513 
514  long leftSize = file.length();
515  while (leftSize > 0) {
516  int readSize = fileInStream.read(buffer);
517  zip.write(buffer, 0, readSize);
518  leftSize -= readSize;
519  }
520  fileInStream.close();
521  }
522  }
523 
524  private String _getRelativePath(String path)
525  {
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());
533  }
534  return(ret);
535  }
536  }
537  return path;
538  }
539 
540  public void load(String fileName, String prjFile) throws FileOpenFailException, LogFileFormatException {
541  init();
542 
543  // zipファイルのエントリ数を取得、プロジェクトファイルがあるかどうか
544  // チェックする。
545  try {
546  ZipFile zipFile = new ZipFile(fileName);
547  int entrySize = zipFile.size();
548  // TODO temporary comment for GRXUI
549  /*
550  * if (zipFile.getEntry(prjFile) == null) { throw new
551  * LogFileFormatException(); } zipFile.close();
552  */
553  // zipを展開する
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;
560  }
561 
562  FileOutputStream out = new FileOutputStream(entry);
563  while (zip.available() == 1) {
564  int readSize = zip.read(buffer, 0, 1024);
565  if (readSize < 0)
566  break;
567  out.write(buffer, 0, readSize);
568  }
569  out.close();
570 
571  if (entry.equals(prjFile) ||
572  entry.contains(new File(collisionLogDatPath_).getName()) ) {
573  continue;
574  }
575 
576  DataInputStream in;
577 
578  if (entry.contains(new File(collisionLogPath_).getName())) {
579  try {
580  in = new DataInputStream(new FileInputStream(entry));
581  collisionLog_ = new CollisionLogHeader();
582  collisionLog_.input(in);
583  in.close();
584  } catch (LogFileFormatException ex) {
585  zip.close();
586  throw ex;
587  }
588  } else {
589  try {
590  in = new DataInputStream(new FileInputStream(entry));
591  LogHeader header = new LogHeader();
592  header.input(in);
593  header_.put(header.objectName_, header);
594  in.close();
595  if (header.getVersion() <= 100) {
596  File file = new File(entry);
597  header.setFileSize(file.length());
598  }
599  header.calcUnitSize();
600  _makeIndexMapMap(header);
601  } catch (LogFileFormatException ex) {
602  zip.close();
603  throw ex;
604  }
605  }
606  }
607  zip.close();
608  } catch (IOException ex) {
609  ex.printStackTrace();
610  throw new FileOpenFailException();
611  }
612  }
613 
614  public void saveCSV(String fileName, String ObjectName) throws FileOpenFailException {
615  try {
616  LogHeader header = (LogHeader) header_.get(ObjectName);
617  if (header == null) {
618  throw new FileOpenFailException();
619  }
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));
623 
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);
630 
631  String methodStr = int2StrIntegrationMethod(header.method_);
632  if(!methodStr.equals("")){
633  out.println("Integration Method, " + methodStr);
634  } else {
635  out.println("Integration Method, " + header.method_);
636  }
637  out.println("Record Size[byte], " + header.recordSize_);
638 
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(']');
643  int len = 1;
644  if (start > 0)
645  len = Integer.parseInt(fmt.substring(start, end));
646  if (len == 1) {
647  out.print(header.dataFormat_[i * 2]);
648  if (i != header.dataFormat_.length / 2 - 1)
649  out.print(",");
650  } else {
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))
654  out.print(",");
655  }
656  }
657  }
658  out.println();
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() + ",");
663  }
664  // 最後の一個
665  out.println(in.readFloat());
666  }
667  out.close();
668  in.close();
669 
670  } catch (IOException ex) {
671  ex.printStackTrace();
672  throw new FileOpenFailException();
673  }
674  }
675 
676  public boolean existRecord(int recordNum) {
677  Enumeration elements = header_.elements();
678  while (elements.hasMoreElements()) {
679  LogHeader header = (LogHeader) elements.nextElement();
680  if (header.numRecords_ <= recordNum) {
681  // System.out.println("object=" + header.objectName_ + "
682  // numRecords=" + header.numRecords_);
683  return false;
684  }
685  }
686 
687  if (collisionLog_.numRecords_ <= recordNum) {
688  // System.out.println("recordNum=" + recordNum);
689  // System.out.println("numRecords=" + collisionLog_.numRecords_);
690  return false;
691  // return true;
692  } else {
693  return true;
694  }
695  }
696 
697  public int getLogObjectNum() {
698  return header_.size();
699  }
700 
701  public int getDataLength(String objectName) {
702  LogHeader header = (LogHeader) header_.get(objectName);
703  return header.recordSize_ / LogHeader.FLOAT_DATA_SIZE;
704  }
705 
718  public void getData(long origin, int offset, int count, DataModel[] dataModelArray) {
719  if (readFile_ == null) {
720  return;
721  }
722 
723  // 読み出し準備
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++) { // アイテム数分ループ
731  DataItem di = dataModelArray[i].dataItem; // データアイテム
732  DataSeries ds = dataModelArray[i].dataSeries; // データ系列
733  String obj = di.object; // オブジェクト名
734  if (obj == null || obj.equals("")) { // オブジェクト名なし?
735  obj = NONAME_OBJECT; // 無名オブジェクト
736  }
737 
738  // 添字取得(node.attribute.index)
739  // System.out.println(i+":"+obj+":"+di.node+":"+di.attribute+" ::
740  // "+indexMapMap_);
741  Object ind = ((Map) indexMapMap_.get(obj)).get(di.node + "." + di.attribute + (di.index >= 0 ? "." + di.index : ""));
742 
743  // データ系列リスト取得
744  ArrayList<DataSeries> dsList = dsListMap.get(obj);
745  // 添字リスト取得
746  ArrayList<Object> indList = indexListMap.get(obj);
747  // 配列書込位置リスト取得
748  ArrayList<Integer> posList = posListMap.get(obj);
749  // 配列長リスト取得
750  ArrayList<Integer> sizeList = sizeListMap.get(obj);
751  if (dsList == null) { // 初めてのオブジェクト?
752  objList.add(obj); // オブジェクトリストに追加
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); // 配列長リストマップに追加
761  }
762  int size = ds.getSize(); // データ系列サイズ取得
763  int pos = (ds.getHeadPos() + offset) % size; // 初期書込位置決定
764  dsList.add(ds); // データ系列リストに追加
765  indList.add(ind); // 添字リストに追加
766  posList.add(new Integer(pos)); // 配列書込位置リストに追加
767  sizeList.add(new Integer(size)); // 配列長リストに追加
768  }
769  // データ読み出し
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]; // レコードバッファ
776  double[] data; // データバッファ
777  long recNo = origin + offset; // レコード番号
778  // リスト
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]; // 配列書込位置配列
785  // 配列書込位置リストを配列にコピー
786  for (int j = 0; j < itemCount; j++) {
787  posArray[j] = ((Integer) posList.get(j)).intValue();
788  }
789  // ファイル
790  RandomAccessFile file = (RandomAccessFile) readFile_.get(obj);
791  // System.out.println("obj=" + obj);
792  synchronized (file) {
793  try {
794  // 開始レコードが先頭レコードより前か後ろか
795  if (recNo < 0) {
796  // 先頭レコードまでシーク
797  file.seek((long) header.headerSize_);
798  } else if (recNo < header.numRecords_) {
799  // 当該レコードまでシーク
800  file.seek((long) header.headerSize_ + header.recordSize_ * recNo);
801  }
802 
803  // レコード数分ループ
804  for (int rec = 0; rec < count; rec++) {
805  // レコード読み出し
806 
807  // レコード範囲外?
808  if (recNo < 0 || recNo >= header.numRecords_) {
809  // System.out.println("record = NaN, numRecords=" +
810  // header.numRecords_);
811  for (int k = 0; k < itemsPerRec; k++) {
812  record[k] = Double.NaN;
813  }
814  } else {
815  for (int k = 0; k < itemsPerRec; k++) {
816  record[k] = file.readFloat();
817  }
818  }
819 
820  // アイテム数分ループ
821  for (int item = 0; item < itemCount; item++) {
822  // data = ((DataSeries)dsList.get(item)).getData();
823  DataSeries ds = (DataSeries) dsList.get(item);
824  data = ds.getData();
825  data[posArray[item]] = record[((Integer) indList.get(item)).intValue()];
826  if (posArray[item] < (((Integer) sizeList.get(item)).intValue() - 1)) {
827  posArray[item]++;
828  } else {
829  posArray[item] = 0;
830  }
831  }
832  recNo++;
833  }
834  } catch (IOException ex) {
835  ex.printStackTrace();
836  }
837  }
838  }
839  }
840 
841  private HashMap<String, ArrayList<DataSeries>> dsListMap_ = new HashMap<String, ArrayList<DataSeries>>(); // データ系列リストのマップ
842  private HashMap<String, ArrayList<Integer>> indexListMap_ = new HashMap<String, ArrayList<Integer>>();
843  private int[] dataPos_ = null;
844  private double[][] data_ = null;
845  private int[] dsSize_ = null;
846  public void initGetData(DataModel[] dataModelArray){
847  if(indexMapMap_ == null || indexMapMap_.isEmpty())
848  return;
849  dsListMap_.clear();
850  indexListMap_.clear();
851  for (int i = 0; i < dataModelArray.length; i++) { // アイテム数分ループ
852  DataItem di = dataModelArray[i].dataItem; // データアイテム
853  DataSeries ds = dataModelArray[i].dataSeries; // データ系列
854  String obj = di.object; // オブジェクト名
855  if (obj == null || obj.equals("")) { // オブジェクト名なし?
856  obj = NONAME_OBJECT; // 無名オブジェクト
857  }
858  String attribute = di.attribute;
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 : ""));
862 
863  ArrayList<DataSeries> dsList = dsListMap_.get(obj);
864  ArrayList<Integer> indexList = indexListMap_.get(obj);
865  if(dsList==null){
866  dsList = new ArrayList<DataSeries>(); // データ系列リスト生成
867  dsListMap_.put(obj, dsList); // データ系列リストマップに追加
868  indexList = new ArrayList<Integer>();
869  indexListMap_.put(obj, indexList);
870  }
871  dsList.add(ds);
872  indexList.add(ind);
873  }
874 
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++){
884  DataSeries ds = dsList.get(i);
885  int size = ds.getSize(); // データ系列サイズ取得
886  data_[i] = ds.getData();
887  dsSize_[i] = size;
888  }
889  }
890  }
891 
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++){
899  DataSeries ds = dsList.get(i);
900  int pos = (ds.getHeadPos() + offset) % dsSize_[i]; // 初期書込位置決定
901  dataPos_[i] = pos;
902  }
903  _getData(obj, origin+offset, count, indexListMap_.get(obj).toArray(new Integer[0]), data_, dataPos_, dsSize_);
904  }
905  }
906 
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) {
913  try {
914  // レコード数分ループ
915  for (int rec = 0; rec < count; rec++) {
916  // アイテム数分ループ
917  for (int item = 0; item < itemIndex.length; item++) {
918  if (recNo < 0 || recNo >= header.numRecords_)
919  data[item][dataPos[item]] = Double.NaN;
920  else{
921  file.seek((long) header.headerSize_ + header.recordSize_ * recNo + LogHeader.FLOAT_DATA_SIZE * itemIndex[item]);
922  data[item][dataPos[item]] = file.readFloat();
923  }
924  if (dataPos[item] < dsSize[item]-1) {
925  dataPos[item]++;
926  } else {
927  dataPos[item] = 0;
928  }
929  }
930  recNo++;
931  }
932  }catch (IOException ex) {
933  ex.printStackTrace();
934  }
935  }
936  }
937 
938  private void _makeIndexMapMap(LogHeader header) {
939  String[] format = header.dataFormat_;
940  Map<String, Integer> indexMap = new HashMap<String, Integer>();
941  int index = 0;
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));
945  index++;
946  } else {
947  for (int j = 0; j < header.getUnitSize(i); j++) {
948  StringBuffer attrName = new StringBuffer(format[i * 2]);
949  attrName.append('.');
950  attrName.append(j);
951  indexMap.put(attrName.toString(), new Integer(index));
952  index++;
953  }
954  }
955  }
956  indexMapMap_.put(header.objectName_, indexMap);
957  }
958 
959  // --------------------------------------------------------------------
960  // Inner Class
967  class LogHeader {
968  // 固定長ヘッダ部
969  public byte[] version_; // ソフトウェアバージョン
970  public int headerSize_; // ヘッダサイズ
971  public long totalTime_; // シミュレーション総時間
972  public long startTime_; // シミュレーション開始時間
973  public long endTime_; // シミュレーション終了時間
974  public long timeStep_; // ステップタイム (usec)
975  public int method_; // 積分法
976  public int recordSize_; // 1レコード当りのデータ量(byte数)
977  public int numRecords_; // 総レコード数
978  public byte[] reserved_; // リザーブド
979  public byte[] reserved_v1_0_; // リザーブド(version 1.0)
980 
981  // 可変長ヘッダ部
982  public String objectName_; // オブジェクト名
983  public String[] dataFormat_; // データフォーマット
984 
985  public int[] unitSize_; //
986 
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 =
995  FIXED_PART_SIZE - (
996  VERSION_DATA_SIZE +
997  INT_DATA_SIZE * 4 +
998  LONG_DATA_SIZE * 4
999  );
1000 
1001  private static final int RESERVED_DATA_SIZE_V1_0 =
1002  FIXED_PART_SIZE - (
1003  VERSION_DATA_SIZE +
1004  INT_DATA_SIZE * 4 +
1005  FLOAT_DATA_SIZE * 3
1006  );
1007 
1008  private static final int END_TIME_SEEK_POINT =
1009  (VERSION_DATA_SIZE + INT_DATA_SIZE + LONG_DATA_SIZE * 2);
1010 
1011  private static final int NUM_RECORDS_SEEK_POINT =
1012  (VERSION_DATA_SIZE + INT_DATA_SIZE * 3 + LONG_DATA_SIZE * 4);
1013 
1014  LogHeader() {
1015  reserved_ = new byte[RESERVED_DATA_SIZE];
1016  }
1017 
1018  LogHeader(String objectName, String[] format)
1019  throws LogFileFormatException
1020  {
1021  reserved_ = new byte[RESERVED_DATA_SIZE];
1022  objectName_ = objectName;
1023  dataFormat_ = format;
1024 
1025  // ヘッダサイズの計算
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;
1030  }
1031 
1032  unitSize_ = new int[dataFormat_.length / 2];
1033 
1034  // 1レコード当りのデータ量を計算
1035  recordSize_ = 0;
1036  for (int i = 0; i < dataFormat_.length / 2; i ++) {
1037  if (!dataFormat_[i * 2 + 1].startsWith("float")) {
1038  throw new LogFileFormatException();
1039  }
1040  if (dataFormat_[i * 2 + 1].equals("float")) {
1041  unitSize_[i] = 0;
1042  recordSize_ ++;
1043  } else {
1044  try {
1045  unitSize_[i] = Integer.parseInt(
1046  dataFormat_[i * 2 + 1].substring(
1047  dataFormat_[i * 2 + 1].indexOf('[') + 1,
1048  dataFormat_[i * 2 + 1].indexOf(']')
1049  )
1050  );
1051 
1052  recordSize_ += unitSize_[i];
1053  } catch (NumberFormatException ex) {
1054  throw new LogFileFormatException();
1055  } catch (StringIndexOutOfBoundsException ex) {
1056  throw new LogFileFormatException();
1057  }
1058  }
1059  }
1060  recordSize_ *= FLOAT_DATA_SIZE;
1061  }
1062 
1063  public int getVersion() {
1064  return (
1065  version_[0] * 1000 +
1066  version_[1] * 100 +
1067  version_[2] * 10 +
1068  version_[3]
1069  );
1070  }
1071 
1072  public void output(DataOutputStream out)
1073  throws IOException
1074  {
1075  // for Debug
1076  /*
1077  System.out.println("Log header for export");
1078  System.out.println("Header Size: " + headerSize_);
1079  System.out.println("Total Time[us]: " + totalTime_);
1080  System.out.println("Start Time: " + startTime_);
1081  System.out.println("End Time: " + endTime_);
1082  System.out.println("method: " + method_);
1083  System.out.println("recordSize[byte]: " + recordSize_);
1084  System.out.println("numRecords: " + numRecords_);
1085  */
1086 
1087  version_ = new byte[] {0, 3, 1, 0}; // version 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_);
1099  out.writeByte(0);
1100  for (int i = 0; i < dataFormat_.length; i ++) {
1101  //if (i % 2 == 0) System.out.print(" " + dataFormat_[i]);
1102  out.writeBytes(dataFormat_[i]);
1103  out.writeByte(0);
1104  }
1105  //System.out.println("");
1106  }
1107 
1108  public void input(DataInputStream in)
1109  throws LogFileFormatException, IOException
1110  {
1111  version_ = new byte[4];
1112  in.readFully(version_);
1113  if (getVersion() <= 100) {
1114  reserved_v1_0_ = new byte[RESERVED_DATA_SIZE_V1_0];
1115  }
1116 
1117  headerSize_ = in.readInt();
1118 
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();
1124  } else {
1125  totalTime_ = in.readLong();
1126  startTime_ = in.readLong();
1127  endTime_ = in.readLong();
1128  timeStep_ = in.readLong();
1129  }
1130  method_ = in.readInt();
1131  recordSize_ = in.readInt();
1132 
1133  if (getVersion() <= 100) {
1134  in.readFully(reserved_v1_0_);
1135  } else {
1136  numRecords_ = in.readInt();
1137  in.readFully(reserved_);
1138  }
1139 
1140  // for Debug
1141  /*
1142  System.out.println("Log header");
1143  System.out.println("Header Size: " + headerSize_);
1144  System.out.println("Total Time[us]: " + totalTime_);
1145  System.out.println("Start Time: " + startTime_);
1146  System.out.println("End Time: " + endTime_);
1147  System.out.println("method: " + method_);
1148  System.out.println("recordSize[byte]: " + recordSize_);
1149  System.out.println("numRecords: " + numRecords_);
1150  */
1151 
1152  byte[] readBuffer = new byte[headerSize_ - FIXED_PART_SIZE];
1153  in.readFully(readBuffer);
1154 
1155  // オブジェクト名を取得
1156  int ptr;
1157  for (ptr = 0; ptr < readBuffer.length; ptr ++) {
1158  if (readBuffer[ptr] == 0) {
1159  objectName_ = new String(readBuffer, 0, ptr);
1160  ptr ++;
1161  break;
1162  }
1163  }
1164 
1165  // フォーマットストリングの数をカウント
1166  int counter = 0;
1167  for (int j = ptr; j < readBuffer.length; j ++) {
1168  if (readBuffer[j] == 0) counter ++;
1169  }
1170 
1171  // フォーマットストリングの取得
1172  dataFormat_ = new String[counter];
1173  counter = 0;
1174  for (int j = ptr; j < readBuffer.length; j ++) {
1175  if (readBuffer[j] == 0) {
1176  dataFormat_[counter] = new String(readBuffer, ptr, j - ptr);
1177  counter ++;
1178  ptr = j + 1;
1179  }
1180  }
1181  }
1182 
1183  public void calcUnitSize() throws LogFileFormatException {
1184  unitSize_ = new int[dataFormat_.length / 2];
1185 
1186  // 1レコード当りのデータ量を計算
1187  for (int i = 0; i < dataFormat_.length / 2; i ++) {
1188  if (!dataFormat_[i * 2 + 1].startsWith("float")) {
1189  throw new LogFileFormatException();
1190  }
1191  if (dataFormat_[i * 2 + 1].equals("float")) {
1192  unitSize_[i] = 0;
1193  } else {
1194  try {
1195  unitSize_[i] = Integer.parseInt(
1196  dataFormat_[i * 2 + 1].substring(
1197  dataFormat_[i * 2 + 1].indexOf('[') + 1,
1198  dataFormat_[i * 2 + 1].indexOf(']')
1199  )
1200  );
1201  } catch (NumberFormatException ex) {
1202  throw new LogFileFormatException();
1203  } catch (StringIndexOutOfBoundsException ex) {
1204  throw new LogFileFormatException();
1205  }
1206  }
1207  }
1208  }
1209 
1210  int getUnitSize(int index) { return unitSize_[index]; }
1211 
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_);
1217  //System.out.println("outEndTime(): numRecords="+numRecords_);
1218  }
1219 
1223  void setFileSize(long length) {
1224  if (recordSize_ == 0) {
1225  numRecords_ = 0;
1226  } else {
1227  numRecords_ = (int)((length - headerSize_) / recordSize_);
1228  }
1229  }
1230  }
1231 
1235  class CollisionLogHeader {
1236  // 固定長ヘッダ部
1237  public byte[] version_; // ソフトウェアバージョン
1238  public int headerSize_; // ヘッダサイズ
1239  public long totalTime_; // シミュレーション総時間
1240  public long startTime_; // シミュレーション開始時間
1241  public long endTime_; // シミュレーション終了時間
1242  public long timeStep_; // ステップタイム (usec)
1243  public byte[] reserved_; // リザーブド
1244  public byte[] reserved_v1_0_; // リザーブド(version 1.0)
1245 
1246  // 可変長ヘッダ部
1247  public ArrayList<Integer> position_ = new ArrayList<Integer>(); // シークオフセット値テーブル
1248 
1249  public int currentPos_;
1250  public int numRecords_; // 総レコード数
1251 
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 =
1258  FIXED_PART_SIZE -
1259  (
1260  VERSION_DATA_SIZE +
1261  INT_DATA_SIZE +
1262  LONG_DATA_SIZE * 4
1263  );
1264 
1265  private static final int RESERVED_DATA_SIZE_V1_0 =
1266  FIXED_PART_SIZE -
1267  (
1268  VERSION_DATA_SIZE +
1269  INT_DATA_SIZE * 2 +
1270  FLOAT_DATA_SIZE * 3
1271  );
1272 
1273  private static final int END_TIME_SEEK_POINT =
1274  (VERSION_DATA_SIZE + INT_DATA_SIZE + LONG_DATA_SIZE * 2);
1275 
1276  private static final int TOTAL_TIME_SEEK_POINT =
1277  VERSION_DATA_SIZE + INT_DATA_SIZE;
1278 
1279  public CollisionLogHeader() {
1280  reserved_ = new byte[RESERVED_DATA_SIZE];
1281  }
1282 
1283  public CollisionLogHeader(SimulationTime time) {
1284  reserved_ = new byte[RESERVED_DATA_SIZE];
1285  totalTime_ = time.totalTime_.getUtime();
1286  startTime_ = time.startTime_.getUtime();
1287  endTime_ = 0;
1288  timeStep_ = time.timeStep_.getUtime();
1289  position_.clear();
1290  headerSize_ = FIXED_PART_SIZE + INT_DATA_SIZE;
1291  }
1292  public int getVersion() {
1293  return (
1294  version_[0] * 1000 +
1295  version_[1] * 100 +
1296  version_[2] * 10 +
1297  version_[3]
1298  );
1299  }
1300 
1301  public void input(DataInputStream in)
1302  throws LogFileFormatException, IOException
1303  {
1304  version_ = new byte[4];
1305  in.readFully(version_);
1306  if (getVersion() <= 100) {
1307  reserved_v1_0_ = new byte[RESERVED_DATA_SIZE_V1_0];
1308  }
1309 
1310  headerSize_ = in.readInt();
1311 
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();
1317  } else {
1318  totalTime_ = in.readLong();
1319  startTime_ = in.readLong();
1320  endTime_ = in.readLong();
1321  timeStep_ = in.readLong();
1322  }
1323 
1324  if (getVersion() <= 100) {
1325  in.readFully(reserved_v1_0_);
1326  } else {
1327  in.readFully(reserved_);
1328  }
1329 
1330  int frameSize =
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());
1335  }
1336 
1337  numRecords_ = frameSize;
1338  }
1339 
1340  public void output(DataOutputStream out) throws IOException {
1341  //System.out.println("StartTime=" + startTime_ + ", endTime=" + endTime_);
1342  version_ = new byte[] {0, 3, 1, 0}; // version 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));
1352  }
1353  }
1354 
1355  public void createLogHeader(DataOutputStream out) throws IOException {
1356  //System.out.println("StartTime=" + startTime_ + ", endTime=" + endTime_);
1357  version_ = new byte[] {0, 3, 1, 0}; // version 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));
1367  }
1368  out.flush();
1369  }
1370 
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_);
1376  }
1377 
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));
1382  }
1383  }
1384 
1385  public boolean joinCollisionLogHeader(CollisionLogHeader ref){
1386  if( ref.equals(this) ){
1387  return false;
1388  } else if( ref.timeStep_ != timeStep_) {
1389  return false;
1390  }
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_);
1395  }
1396  numRecords_ += size - 1;
1397  return true;
1398  }
1399 
1400  public boolean separateCollisionLogHeader(CollisionLogHeader ref, final int changePos){
1401  if( ref.equals(this) ){
1402  return false;
1403  } else if( ref.timeStep_ != timeStep_) {
1404  return false;
1405  }
1406  endTime_ = ref.endTime_;
1407  time_.setUtime(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);
1412  }
1413  numRecords_ = position_.size() - 1;
1414  return true;
1415  }
1416  }
1417 
1418 
1419 // added by GRX 20070207
1420  public float[] get(String objectName, long record) throws IOException {
1421  if (readFile_ == null) return null;
1422 
1423  LogHeader header = (LogHeader)header_.get(objectName);
1424  if (header == null) return null;
1425 
1426  RandomAccessFile file = (RandomAccessFile)readFile_.get(objectName);
1427 
1428  float[] data = new float[header.recordSize_ / LogHeader.FLOAT_DATA_SIZE];
1429 
1430  try {
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();
1435  }
1436  } catch (EOFException ex) {
1437  ex.printStackTrace();
1438  } catch (IOException ex) {
1439  closeAsRead();
1440  throw ex;
1441  }
1442  return data;
1443  }
1444 
1445  public CollisionPoint[] getCollisionPointData(int frameNum) throws IOException {
1446  if (collisionLog_.position_.size() < frameNum + 2) {
1447  return null;
1448  }
1449  int size = collisionLog_.position_.get(frameNum + 1) - collisionLog_.position_.get(frameNum);
1450  int data_size=0;
1451  Enumeration elements = header_.elements();
1452  LogHeader header = (LogHeader)elements.nextElement();
1453  int version = header.getVersion();
1454  if (version <= 110){
1455  data_size = 6 * 4;
1456  }else{
1457  data_size = COLLISION_DATA_SIZE;
1458  }
1459  if ((size % data_size) != 0 || size <= 0)
1460  return null;
1461  size /= data_size;
1462 
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;
1477  }else{
1478  data[i].idepth = collisionDatIn_.readDouble();
1479  }
1480  }
1481  return data;
1482  }
1483 
1484  public int getCollisionPointDataSize(int frameNum){
1485  try {
1486  return collisionLog_.position_.get(frameNum + 1) - collisionLog_.position_.get(frameNum);
1487  } catch (IndexOutOfBoundsException ex) {
1488  ex.printStackTrace();
1489  }
1490  return -1;
1491  }
1492 
1493  public int getRecordNum(String objectName) {
1494  LogHeader header = (LogHeader)header_.get(objectName);
1495  return header.numRecords_;
1496  }
1497 
1498 // added by GRX 20070416
1499  public void setTempDir(String tmp) {
1500  tmpdir = tmp;
1501 
1502  File f = new File(tmpdir);
1503  File pf = f.getParentFile();
1504  if (!pf.isDirectory())
1505  pf.mkdir();
1506  if (!f.isDirectory())
1507  f.mkdir();
1508 
1509  collisionLogPath_ = tmpdir+File.separator+COLLISION_LOG_NAME;
1510  collisionLogDatPath_ = tmpdir+File.separator+COLLISION_LOG_DAT_NAME;
1511  }
1512 
1513  public String getTempDir() {
1514  return tmpdir;
1515  }
1516 
1526  public int getIndex(String obj,String member){
1527  return ((Integer) (((Map) indexMapMap_.get(obj)).get(member))).intValue();
1528  }
1529 
1530  private void _initTempInstance(LogManager logger){
1531  if( this != logger ){
1532  header_ = logger.header_;
1533  indexMapMap_ = logger.indexMapMap_;
1534  collisionLog_ = logger.collisionLog_;
1535  time_ = new Time();
1536  }
1537  }
1538 
1539  private static final String[] INTEGRATION_METHOD_NAMES = { "RUNGE_KUTTA", "EULER" }; //$NON-NLS-1$ //$NON-NLS-2$
1540  private int str2IntIntegrationMethod(String methodStr){
1541  for(int i= 0; i < INTEGRATION_METHOD_NAMES.length; i++){
1542  if(methodStr.equals(INTEGRATION_METHOD_NAMES[i])){
1543  return i;
1544  }
1545  }
1546  return -1;
1547  }
1548  private String int2StrIntegrationMethod(int methodInt){
1549  if(methodInt < 0 || INTEGRATION_METHOD_NAMES.length <= methodInt)
1550  return "";
1551  return INTEGRATION_METHOD_NAMES[methodInt];
1552  }
1553 }
HashMap< String, ArrayList< DataSeries > > dsListMap_
def zip(a, b)
void addLogObject(String objectName, String[] format)
#define null
our own NULL pointer
Definition: IceTypes.h:57
Hashtable< String, RandomAccessFile > readFile_
Definition: LogManager.java:51
String getTempFilePath(String objectName)
png_voidp ptr
Definition: png.h:2063
png_uint_32 size
Definition: png.h:1521
int version
Definition: jpeglib.h:901
Hashtable< String, DataOutputStream > writeFile_
Definition: LogManager.java:50
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
Definition: png.h:1541
png_uint_32 i
Definition: png.h:2735
void putCollisionPointData(CollisionPoint[] data)
Map< String, Map< String, Integer > > indexMapMap_
Definition: LogManager.java:52
HashMap< String, ArrayList< Integer > > indexListMap_
CollisionPoint[] getCollisionPointData(int frameNum)
Hashtable< String, LogHeader > header_
Definition: LogManager.java:49
void save(String fileName, String prjFileName)
int method
Definition: png.h:1847
def j(str, encoding="cp932")
list index
void _addFileToZipEntry(ZipOutputStream zip, File file)
String[] getDataFormat(String objectName)
void getData(long origin, int offset, int count, DataModel[] dataModelArray)
void initGetData(DataModel[] dataModelArray)
void initCollisionLog(SimulationTime time)
void load(String fileName, String prjFile)
png_size_t start
Definition: png.h:1496
void openAsWrite(SimulationTime time, String method)
void put(String objectName, float[] data)
typedef int
Definition: png.h:1113
void separateLogs(final int changePos)
png_infop png_bytep buffer
Definition: png.h:2042
int getIndex(String obj, String member)
void getData(long origin, int offset, int count)
JSAMPIMAGE data
Definition: jpeglib.h:945
output(gif_dest_ptr dinfo, int code)
Definition: wrgif.c:105
void saveCSV(String fileName, String ObjectName)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:24