00001 #ifndef ReadQuake_HEADER 00002 #define ReadQuake_HEADER 00003 00004 #include <assert.h> 00005 #include <vector> 00006 #include <geometry/Point.h> 00007 00008 class QuakeReader { 00009 public: 00010 QuakeReader(const char *filename); 00011 ~QuakeReader(); 00012 00013 typedef size_t Dimension; 00014 typedef size_t ID; 00015 00016 struct point { 00017 ID id; 00018 std::vector<double> coords; 00019 std::vector<double> attributes; 00020 bool isBdry; 00021 }; 00022 00023 struct element { 00024 ID id; 00025 Dimension dim; 00026 std::vector<ID> subElements; // IDs of dim-1 elements 00027 std::vector<std::pair<Dimension,ID> > orphans; // <dim, id> of lower-d elements 00028 bool isBdry; 00029 }; 00030 00031 struct point_iterator { 00032 void operator ++ () { 00033 id_++; 00034 assert(id_ <= source_->afterLastID()); 00035 } 00036 int operator- (const point_iterator& other) const { 00037 return id_ - other.id_; 00038 } 00039 bool operator!= (const point_iterator& other) const { 00040 return id_ != other.id_; 00041 } 00042 point operator*() const { 00043 return source_->get_point(id_); 00044 } 00045 00046 protected: 00047 friend class QuakeReader; 00048 point_iterator(const QuakeReader *r, ID ii) 00049 : source_(r), id_(ii) { } 00050 const QuakeReader *source_; 00051 ID id_; 00052 }; 00053 00054 typedef std::vector<element>::const_iterator element_iterator; 00055 00056 Dimension dim() const { return dim_; } 00057 bool hasConstraints() const; 00058 00059 size_t npoints() const { return npoints_; } 00060 ID firstID() const { return firstID_; } 00061 ID afterLastID() const { return firstID_ + npoints(); } 00062 point_iterator begin_points() const; 00063 point_iterator end_points() const; 00064 point_iterator try_get_point(ID) const; 00065 point get_point(ID) const; 00066 // Dimension dim() const 00067 const double *get_x(ID) const; 00068 size_t nPointAttrs() const { return nattrs_; } 00069 const double *get_attrs(ID) const; 00070 bool isBdry(ID) const; 00071 00072 template <size_t d> struct PointIterator : point_iterator { 00073 typedef point_iterator super; 00074 PointIterator(const point_iterator& it): super(it) { } 00075 void operator ++ () { super::operator++(); } 00076 int operator- (const PointIterator<d>& other) const { 00077 return super::operator-(other); 00078 } 00079 bool operator != (const PointIterator<d>& other) const { 00080 return super::operator!=(other); 00081 } 00082 Geometry::Point<d> operator*() const { 00083 return Geometry::Point<d>(source_->get_x(id_), d); 00084 } 00085 }; 00086 00087 template <size_t d> PointIterator<d> beginPoints() const { 00088 return PointIterator<d>(begin_points()); 00089 } 00090 template <size_t d> PointIterator<d> endPoints() const { 00091 return PointIterator<d>(end_points()); 00092 } 00093 00094 size_t nelements(Dimension) const; 00095 element_iterator begin_elements(Dimension) const; 00096 element_iterator end_elements(Dimension) const; 00097 element_iterator try_get_element(std::pair<Dimension,ID>) const; 00098 const element& get_element(std::pair<Dimension,ID> p) const; 00099 00100 private: 00101 Dimension dim_; 00102 00103 /* We're very careful with the allocation here, because it turns out the 00104 * algorithm is so fast that I/O matters to the runtime. */ 00105 ID firstID_; 00106 size_t npoints_; 00107 size_t nattrs_; 00108 double *pointX_; 00109 double *pointAttrs_; 00110 bool *pointIsBdry_; 00111 00112 std::vector<std::vector<element> > elements_; 00113 00114 struct NodeHeader { 00115 int n; 00116 int d; 00117 int nattr; 00118 int bdry; 00119 }; 00120 void allocatePoints(const NodeHeader&); 00121 }; 00122 00123 #endif
1.4.6