Skip to content

Saving and Loading Trajectories to File

Jaci R edited this page Mar 28, 2016 · 1 revision

If you want to save a trajectory to file, it's very simple.

There are two kinds of files supported, binary files and CSV (comma-separated values) files. Binary Files are more space efficient, and usually carry the .traj file extension, while CSV files are human readable and can be imported into programs such as Microsoft Excel and Tableau for viewing.

Both the Java and C variants of the library have support for file exporting and importing.

C

Saving

Binary File:

FILE *fp = fopen("myfile.traj", "wb");
pathfinder_serialize(fp, trajectory, trajectory_length);
fclose(fp);

CSV File:

FILE *fp = fopen("myfile.csv", "w");
pathfinder_serialize_csv(fp, trajectory, trajectory_length);
fclose(fp);

Loading

Binary File:

FILE *fp = fopen("myfile.traj", "rb");
Segment trajectory[1024];     // Over allocate as we don't know the length yet
int length = pathfinder_deserialize(fp, trajectory);
fclose(fp);

CSV File:

FILE *fp = fopen("myfile.csv", "r");
Segment trajectory[1024];     // Over allocate as we don't know the length yet
int length = pathfinder_deserialize_csv(fp, trajectory);
fclose(fp);

Java

Saving

Binary File:

File myFile = new File("myfile.traj");
Pathfinder.writeToFile(myFile, trajectory);

CSV File:

File myFile = new File("myfile.csv");
Pathfinder.writeToCSV(myFile, trajectory);

Loading

Binary File:

File myFile = new File("myfile.traj");
Trajectory trajectory = Pathfinder.readFromFile(myFile);

CSV File:

File myFile = new File("myfile.csv");
Trajectory trajectory = Pathfinder.readFromCSV(myFile);
Clone this wiki locally