c3d-utils
|
00001 #ifndef UUC3D_38DHJWTOANTQ 00002 #define UUC3D_38DHJWTOANTQ 00003 00004 // vim:set tabstop=4 cindent: 00005 00006 // Author: Geert-Jan Giezeman <geert@cs.uu.nl> 00007 // Copyright: Utrecht University, 2011 00008 00013 #include "basic_io.hpp" 00014 #include <string> 00015 #include <vector> 00016 #include <boost/scoped_ptr.hpp> 00017 00021 namespace UuIcsC3d 00022 { 00023 00032 struct PreconditionError 00033 { 00034 }; 00035 00037 struct OpenError 00038 { 00042 OpenError(std::string const &filename) 00043 : m_filename(filename) {} 00047 std::string filename() const {return m_filename;} 00048 private: 00049 std::string m_filename; 00050 }; 00051 00053 struct ContentError 00054 { 00060 ContentError(std::string const &msg, std::string const &filename) 00061 : m_msg(msg), m_filename(filename) {} 00063 std::string filename() const {return m_filename;} 00065 std::string msg() const {return m_msg;} 00066 private: 00067 std::string m_msg; 00068 std::string m_filename; 00069 }; 00070 00074 } 00075 00076 namespace UuIcsC3d 00077 { 00078 00079 // typedef uint16_t unsigned16_t; 00081 typedef unsigned short unsigned16_t; 00082 00104 typedef unsigned char Block[512]; 00105 00110 struct HeaderEvent { 00112 std::string label; 00114 bool displayed; 00116 float time; 00117 }; 00118 00126 struct Header { 00128 int trajectory_count; 00131 int analog_per_frame; 00133 int first_frame; 00135 int last_frame; 00137 int max_interpolation_gap; 00139 float scaling_factor; 00141 int data_start_block; 00143 int analog_samples_per_frame; 00145 float frame_rate; 00147 bool has_label_and_range; 00149 int label_and_range_block; 00151 std::vector<HeaderEvent> events; 00152 }; 00153 00155 struct GroupParamCommon 00156 { 00158 GroupParamCommon(): m_id(0) {} 00167 GroupParamCommon(bool locked, int id, std::string const &name, 00168 std::string const &description) 00169 : m_locked(locked),m_id(id), m_name(name), 00170 m_description(description) {} 00172 int id() const 00173 { return m_id; } 00175 void set_id(int id) 00176 { m_id = id; } 00180 bool is_locked() const 00181 { return m_locked; } 00184 void set_locked(bool value) 00185 { m_locked = value; } 00187 std::string name() const 00188 { return m_name; } 00190 void set_name(std::string name) 00191 { m_name = name; } 00193 std::string description() const 00194 { return m_description; } 00196 void set_description(std::string const &description) 00197 { m_description = description; } 00198 protected: 00200 bool m_locked; 00202 int m_id; 00204 std::string m_name; 00206 std::string m_description; 00207 }; 00208 00213 struct Parameter: GroupParamCommon 00214 { 00218 enum DataType {DtChar=-1, DtByte=1, DtShort=2, DtFloat=4}; 00224 Parameter() {} 00243 Parameter(bool locked, int id, std::string name, int data_type, 00244 std::vector<int> const &dimension_size, 00245 unsigned char const*data_start, unsigned char const*data_end, 00246 std::string const &description, boost::shared_ptr<BasicIO> io); 00249 00251 DataType data_type() const { return m_data_type; } 00253 std::vector<int> bounds() const { return m_dimension_size; } 00259 unsigned char get_byte(std::vector<int> const &index) const; 00264 unsigned char get_byte(int i1) const; 00269 unsigned char get_the_byte() const; 00275 char get_char(std::vector<int> const &index) const; 00280 char get_char(int i1) const; 00285 char get_the_char() const; 00290 bool contains_string() const; 00298 std::string get_string(std::vector<int> const &index) const; 00303 std::string get_string(int i1) const; 00308 std::string get_the_string() const; 00314 int get_short(std::vector<int> const &index) const; 00319 int get_short(int i1) const; 00324 int get_the_short() const; 00330 unsigned get_ushort(std::vector<int> const &index) const; 00335 unsigned get_ushort(int i1) const; 00340 unsigned get_the_ushort() const; 00346 float get_float(std::vector<int> const &index) const; 00351 float get_float(int i1) const; 00356 float get_the_float() const; 00361 int representation_size() const; 00365 std::vector<unsigned char> const &raw_data() const 00366 { return m_data;} 00373 bool check_index(std::vector<int> const &index) const; 00379 bool check_string_index(std::vector<int> const &index) const; 00384 00389 void set_the_ushort(unsigned16_t val); 00395 void set_ushort(int i1, unsigned16_t val); 00402 void set_strings(std::vector<std::string> const &strings, char pad); 00404 private: 00405 int get_offset(int data_size, std::vector<int> const &index) const; 00406 int get_string_offset(std::vector<int> const &index) const; 00407 DataType m_data_type; 00408 std::vector<int> m_dimension_size; 00409 std::vector<unsigned char> m_data; 00410 boost::shared_ptr<BasicIO> m_io; // decoder of binary data, appropriate for input file 00411 }; 00412 00417 struct Group: GroupParamCommon 00418 { 00423 Group() {} 00428 Group(int id) 00429 : GroupParamCommon(false, id, std::string(), std::string()) {} 00433 Group(bool locked, int id, std::string name, 00434 std::string const &description) 00435 : GroupParamCommon(locked, id, name, description) {} 00439 void add_parameter(Parameter const ¶m); 00443 bool has_parameter(std::string const &name) const; 00450 Parameter const &get_parameter(std::string const &name) const; 00457 Parameter &get_parameter(std::string const &name); 00464 Parameter const *get_parameter_checked(std::string const &name) const; 00471 Parameter *get_parameter_checked(std::string const &name); 00475 std::vector<Parameter> const &the_parameters() const 00476 { return m_parameters;} 00481 int total_representation_size() const; 00486 int group_representation_size() const; 00487 private: 00488 std::vector<Parameter> m_parameters; 00489 }; 00490 00492 class Content 00493 { 00494 Header m_header; 00496 boost::shared_ptr<BasicIO> m_io; 00497 std::vector<Group> m_groups; 00498 void parse_params(std::vector<unsigned char> const &); 00499 std::string m_filename; 00500 public: 00501 Content(); 00507 void open(std::string const &filename); 00511 Header const &header() const 00512 {return m_header;}; 00516 Header &header() 00517 {return m_header;}; 00521 boost::shared_ptr<BasicIO> io() const 00522 { return m_io;} 00523 //std::string group_name(int gid) const; 00527 bool has_group(std::string const &name) const; 00534 Group const &get_group(std::string const &name) const; 00541 Group &get_group(std::string const &name); 00548 Group const *get_group_checked(std::string const &name) const; 00555 Group *get_group_checked(std::string const &name); 00559 std::vector<Group> const & the_groups() const { return m_groups;} 00563 std::string filename() const { return m_filename; } 00564 }; 00565 00568 Content open_c3d_file(char const *filename); 00571 Content open_c3d_file(std::string const &filename); 00572 00574 struct FileInfo1 { 00576 int data_startblock0; 00578 int frame_count; 00580 int points_per_frame; 00582 int analog_channels; 00584 int samples_per_frame; 00586 int frame_size; 00588 float point_scale; 00590 bool is_integer; 00591 }; 00592 00597 } 00598 00599 namespace UuIcsC3d 00600 { 00601 00608 00609 struct DataPoint3d { 00611 enum Status { Generated=1, Measured=2, Invalid=64}; 00612 private: 00613 float m_coords[3]; 00614 float m_residual; 00615 char m_mask; // (bits 0-6 can indicate a camera) 00616 Status m_type; 00617 unsigned16_t m_flags; 00618 public: 00620 typedef float const (&CTripleF)[3]; 00622 DataPoint3d(): m_residual(-1), m_mask(0), m_type(Invalid), m_flags(0) 00623 {m_coords[0]=0; m_coords[1]=0;m_coords[2]=0;} 00625 00634 void set_value(float x, float y, float z, unsigned f, 00635 Status v = Generated); 00642 void set_value(float const*in_coordinates, unsigned f, 00643 Status v = Generated); 00650 void set_value(double const*in_coordinates, unsigned f, 00651 Status v = Generated); 00653 void invalidate() { m_type = Invalid; } 00655 void set_mask(char mask) 00656 { m_mask = mask;} 00658 void set_residual(float r) 00659 { m_residual = r;} 00661 00663 00666 Status status() const {return m_type;} 00668 bool is_valid() const { return m_type==Generated || m_type==Measured; } 00670 CTripleF coordinates() const {return m_coords;} 00672 float x() const { return m_coords[0];} 00674 float y() const { return m_coords[1];} 00676 float z() const { return m_coords[2];} 00681 unsigned flags() const {return m_flags;} 00683 char mask() const {return m_mask;} 00685 float residual() const {return m_residual;} 00687 }; 00688 00690 struct FrameData { 00692 std::vector<DataPoint3d> points; 00694 std::vector< std::vector<float> > analog_data; 00695 }; 00696 00698 struct SpacePaddedString { 00700 SpacePaddedString() :m_size(0) {} 00703 SpacePaddedString(std::string const &str):m_val(str) {compute_size();} 00705 std::string complete() const {return m_val;} 00707 std::string stripped() const {return m_val.substr(0, m_size);} 00709 void set(std::string const &str) {m_val = str; compute_size(); } 00710 private: 00711 std::string m_val; 00712 std::string::size_type m_size; 00713 void compute_size(); 00714 }; 00715 00716 class C3dFile; 00717 00719 class C3dFileInfo { 00720 Content m_content; 00721 FileInfo1 m_fi1; 00722 std::vector<SpacePaddedString> m_labels; 00723 public: 00726 00728 C3dFileInfo(); 00734 C3dFileInfo(std::string const &filename); 00736 00739 00742 bool is_valid() const; 00744 int frame_count() const { return m_fi1.frame_count; } 00748 int points_per_frame() const { return m_fi1.points_per_frame; } 00752 std::vector<SpacePaddedString> point_labels() const 00753 { return m_labels; } 00755 int analog_channels() const { return m_fi1.analog_channels; } 00759 int analog_samples_per_frame() const { return m_fi1.samples_per_frame; } 00765 std::auto_ptr<C3dFile> open() const; 00767 00773 00777 void set_param_blocks(int c) ; 00781 void set_frame_count(int) ; 00785 void set_points_per_frame(int) ; 00789 void set_analog_samples_per_frame(int) ; 00793 void set_analog_channels(int) ; 00800 void set_point_labels(std::vector<std::string> const &labels, 00801 char pad = ' '); 00803 00806 00810 Content const &content() const { return m_content; } 00814 FileInfo1 const &fi1() const { return m_fi1; } 00816 }; 00817 00819 class C3dFile { 00820 C3dFile(C3dFile const &in); // not implemented: no copying 00821 struct Impl; 00822 boost::scoped_ptr<Impl> m_impl; 00823 public: 00825 C3dFile(C3dFileInfo const &fi); 00826 ~C3dFile(); 00832 void get_frame_data(FrameData &data, int frame_no) const; 00840 DataPoint3d get_point(int frame_no, int point_no) const; 00841 }; 00842 00844 bool write_with_same_header(std::string const &fn, C3dFileInfo const &fi, 00845 std::vector<FrameData> const &data); 00846 00847 // bool write(std::string const &fn, C3dFileInfo const &fi, std::vector<FrameData> const &data, UuIcsC3d::BasicIO const* encoder); 00855 bool write(std::string const &fn, C3dFileInfo fi, std::vector<FrameData> const &data, UuIcsC3d::BasicIO const* encoder); 00856 00861 } 00862 00863 #endif // UUC3D_38DHJWTOANTQ