From 2df719e0fd0729db2dd292ece8d8b9eb2ee0a186 Mon Sep 17 00:00:00 2001 From: Andreas Schenk <andreas_schenk@hms.harvard.edu> Date: Fri, 3 May 2013 16:11:42 -0400 Subject: [PATCH] refactored img::Point: - replaced boundary check in operator[] with assert - replaced int data array with int variables (x,y,z) - moved some methods to header to allow inlining - used boost operator for operator implementations - exported x,y,z members to python --- modules/img/alg/src/convolute.cc | 4 +- modules/img/alg/src/correlate.cc | 2 +- modules/img/base/pymod/export_point.cc | 7 +- modules/img/base/src/point.cc | 209 +++++-------------------- modules/img/base/src/point.hh | 151 ++++++++++++------ 5 files changed, 146 insertions(+), 227 deletions(-) diff --git a/modules/img/alg/src/convolute.cc b/modules/img/alg/src/convolute.cc index f34cf7285..70543151b 100644 --- a/modules/img/alg/src/convolute.cc +++ b/modules/img/alg/src/convolute.cc @@ -37,7 +37,7 @@ struct fnc_expl_convolute_wrap_op { for(ExtentIterator it1(ex1);!it1.AtEnd(); ++it1) { T1 val(0); for(ExtentIterator it2(rhs->GetExtent());!it2.AtEnd();++it2) { - val+=lhs->Value(ex1.WrapAround(it1-it2)) * Val2Val<T2,T1>(rhs->Value(it2)); + val+=lhs->Value(ex1.WrapAround(Point(it1)-Point(it2))) * Val2Val<T2,T1>(rhs->Value(it2)); } res->Value(it1) = val; } @@ -56,7 +56,7 @@ struct fnc_expl_convolute_op { for(ExtentIterator it1(ex1);!it1.AtEnd(); ++it1) { T1 val(0); for(ExtentIterator it2(rhs->GetExtent());!it2.AtEnd();++it2) { - val+=lhs->GetCheckedValue(it1-it2) * Val2Val<T2,T1>(rhs->Value(it2)); + val+=lhs->GetCheckedValue(Point(it1)-Point(it2)) * Val2Val<T2,T1>(rhs->Value(it2)); } res->Value(it1) = val; } diff --git a/modules/img/alg/src/correlate.cc b/modules/img/alg/src/correlate.cc index 04f1b75f6..f9ba50848 100644 --- a/modules/img/alg/src/correlate.cc +++ b/modules/img/alg/src/correlate.cc @@ -46,7 +46,7 @@ struct fnc_expl_correlate_wrap_op { for(ExtentIterator it1(ex1);!it1.AtEnd(); ++it1) { T1 val(0); // default ctor sets value to zero for(ExtentIterator it2(rhs->GetExtent());!it2.AtEnd();++it2) { - val+=lhs->Value(ex1.WrapAround(it1+it2)) * Val2Val<T2,T1>(Conj(rhs->Value(it2))); + val+=lhs->Value(ex1.WrapAround(Point(it1)+Point(it2))) * Val2Val<T2,T1>(Conj(rhs->Value(it2))); } res->Value(it1) = val; } diff --git a/modules/img/base/pymod/export_point.cc b/modules/img/base/pymod/export_point.cc index 27179b3e4..53047e6e8 100644 --- a/modules/img/base/pymod/export_point.cc +++ b/modules/img/base/pymod/export_point.cc @@ -33,8 +33,8 @@ namespace ost { namespace img { int point_len(const Point& p) {return 3;} -int point_getitem(const Point& p, int i) {return p[i];} -void point_setitem(Point& p, int i, int v) {p[i]=v;} +int point_getitem(const Point& p, int i) {return p.At(i);} +void point_setitem(Point& p, int i, int v) {p.At(i)=v;} }} @@ -48,6 +48,9 @@ void export_Point() .def(init<const Vec3&>(args("vec"))) .def(init<const Vec4&>(args("vec"))) .def(init<const Point&>(args("point"))) + .def_readwrite("x", &Point::x) + .def_readwrite("y", &Point::y) + .def_readwrite("z", &Point::z) .def("Mirror",&Point::Mirror) .def("ToVec2",&Point::ToVec2) .def("ToVec3",&Point::ToVec3) diff --git a/modules/img/base/src/point.cc b/modules/img/base/src/point.cc index 3cadd17b0..94d1d2914 100644 --- a/modules/img/base/src/point.cc +++ b/modules/img/base/src/point.cc @@ -21,255 +21,118 @@ /* point - Author: Ansgar Philippsen + Authors: Ansgar Philippsen, Andreas Schenk */ #include <ost/message.hh> #include <iostream> #include <sstream> -#include <ost/img/peak.hh> #include "point.hh" #include "size.hh" #include "vecmat.hh" namespace ost { namespace img { -Point::Point(): - data_() +Point::Point(const Size& size): + x(size[0]), + y(size[1]), + z(size[2]) { - data_[0]=0; - data_[1]=0; - data_[2]=0; -} - -Point::Point(int a): - data_() -{ - data_[0]=a; - data_[1]=0; - data_[2]=0; -} - -Point::Point(int a, int b): - data_() -{ - data_[0]=a; - data_[1]=b; - data_[2]=0; -} - -Point::Point(int a, int b, int c): - data_() -{ - data_[0]=a; - data_[1]=b; - data_[2]=c; -} - -Point::Point(const Vec2& v): - data_() -{ - data_[0]=static_cast<int>(round(v[0])); - data_[1]=static_cast<int>(round(v[1])); - data_[2]=0; -} - -Point::Point(const Vec3& v): - data_() -{ - data_[0]=static_cast<int>(round(v[0])); - data_[1]=static_cast<int>(round(v[1])); - data_[2]=static_cast<int>(round(v[2])); -} - -Point::Point(const Vec4& v): - data_() -{ - if(std::abs(v[3])<1e-100) { - throw geom::OutOfRangeException("4th element of Vec4 is too close to zero for normalization"); - } else { - Real sf = 1.0/v[3]; - data_[0]=static_cast<int>(round(v[0]*sf)); - data_[1]=static_cast<int>(round(v[1]*sf)); - data_[2]=static_cast<int>(round(v[2]*sf)); - } -} - -Point::Point(const Size& s): - data_() -{ - data_[0]=s[0]; - data_[1]=s[1]; - data_[2]=s[2]; } -Point::Point(const Point &p): - data_() -{ - data_[0]=p.data_[0]; - data_[1]=p.data_[1]; - data_[2]=p.data_[2]; -} - Point Point::Mirror(int planes) { - return Point(planes & Plane::YZ ? -data_[0] : data_[0], - planes & Plane::XZ ? -data_[1] : data_[1], - planes & Plane::XY ? -data_[2] : data_[2]); -} - - -bool Point::equal(const Point &p) const -{ - return (data_[0]==p.data_[0] && - data_[1]==p.data_[1] && - data_[2]==p.data_[2]); -}; - -bool Point::less(const Point &p) const -{ - return ( data_[2]!=p.data_[2] ? data_[2]<p.data_[2] : (data_[1]!=p.data_[1] ? data_[1]<p.data_[1] : data_[0]<p.data_[0]) ); -} - -Point Point::absolute() const -{ - return Point(std::abs(data_[0]),std::abs(data_[1]),std::abs(data_[2])); + return Point(planes & Plane::YZ ? -x : x, + planes & Plane::XZ ? -y : y, + planes & Plane::XY ? -z : z); } Point& Point::operator+=(const Point& p) { - data_[0]+=p.data_[0]; - data_[1]+=p.data_[1]; - data_[2]+=p.data_[2]; + x+=p.x; + y+=p.y; + z+=p.z; return *this; } Point& Point::operator-=(const Point& p) { - data_[0]-=p.data_[0]; - data_[1]-=p.data_[1]; - data_[2]-=p.data_[2]; + x-=p.x; + y-=p.y; + z-=p.z; return *this; } Point Point::operator-() const { - return Point(-data_[0],-data_[1],-data_[2]); + return Point(-x,-y,-z); } Point& Point::operator=(const Point& p) { - data_[0]=p.data_[0]; - data_[1]=p.data_[1]; - data_[2]=p.data_[2]; + x=p.x; + y=p.y; + z=p.z; return *this; } bool Point::operator==(const Point &p) const { - return equal(p); -} - -bool Point::operator!=(const Point &p) const -{ - return !equal(p); + return (x==p.x && + y==p.y && + z==p.z); } bool Point::operator<(const Point &p) const { - return less(p); + return ( z!=p.z ? z<p.z : (y!=p.y ? y<p.y : x<p.x) ); } -bool Point::operator<=(const Point &p) const -{ - return (equal(p) || less(p)); -} - -bool Point::operator>(const Point &p) const -{ - return p.less((*this)); -} - -bool Point::operator>=(const Point &p) const -{ - return (equal(p) || p.less((*this))); -} Point& Point::operator+=(const Size& s) { - data_[0]+=s[0]; - data_[1]+=s[1]; - data_[2]+=s[2]; + x+=s[0]; + y+=s[1]; + z+=s[2]; return *this; } Point& Point::operator-=(const Size& s) { - data_[0]-=s[0]; - data_[1]-=s[1]; - data_[2]-=s[2]; + x-=s[0]; + y-=s[1]; + z-=s[2]; return *this; } Vec2 Point::ToVec2() const { - Vec2 nrvo(static_cast<Real>(data_[0]), - static_cast<Real>(data_[1])); + Vec2 nrvo(static_cast<Real>(x), + static_cast<Real>(y)); return nrvo; } Vec3 Point::ToVec3() const { - Vec3 nrvo(static_cast<Real>(data_[0]), - static_cast<Real>(data_[1]), - static_cast<Real>(data_[2])); + Vec3 nrvo(static_cast<Real>(x), + static_cast<Real>(y), + static_cast<Real>(z)); return nrvo; } Vec4 Point::ToVec4() const { - Vec4 nrvo(static_cast<Real>(data_[0]), - static_cast<Real>(data_[1]), - static_cast<Real>(data_[2]), + Vec4 nrvo(static_cast<Real>(x), + static_cast<Real>(y), + static_cast<Real>(z), 1.0); return nrvo; } -int& Point::operator[](unsigned int index) -{ - if(index>2) throw std::range_error("Point index out of range"); - return data_[index]; -} - -int Point::operator[](unsigned int index) const -{ - if(index>2) throw geom::OutOfRangeException("Point index out of range"); - return data_[index]; -} - // global funcs -Point operator+(const Point& p1, const Point& p2) -{ - Point r(p1); - r+=(p2); - return r; -} - -Point operator-(const Point& p1, const Point& p2) { - Point r(p1); - r-=(p2); - return r; -} - - - -Point absolute(const Point& p) { - return p.absolute(); -} - std::ostream& operator<<(std::ostream& os, const Point &p) { os << "(" << p[0] << "," << p[1] << "," << p[2] << ")"; diff --git a/modules/img/base/src/point.hh b/modules/img/base/src/point.hh index bc2a161e8..4e9678630 100644 --- a/modules/img/base/src/point.hh +++ b/modules/img/base/src/point.hh @@ -21,11 +21,7 @@ /* integer triplet based point - Author: Ansgar Philippsen -*/ - -/* - TODO: use boost operator + Authors: Ansgar Philippsen, Andreas Schenk */ #ifndef IMG_POINT_H @@ -33,6 +29,7 @@ #include <vector> #include <iosfwd> +#include <boost/operators.hpp> #include <ost/img/module_config.hh> #include "vecmat.hh" @@ -43,36 +40,117 @@ namespace ost { namespace img { class Size; //! class encapsulating 1D to 3D point -class DLLEXPORT_OST_IMG_BASE Point { +class DLLEXPORT_OST_IMG_BASE Point: boost::additive<Point, + boost::additive2<Point, Size, + boost::multipliable2<Point,int, + boost::less_than_comparable<Point, + boost::equality_comparable<Point> > > > >{ public: - Point(); - Point(const Point &p); + Point(): + x(), + y(), + z() + { + } + + Point(const Point &p): + x(p.x), + y(p.y), + z(p.z) + { + } + //! 1D int constructor - explicit Point(int a); + Point(int a): + x(a), + y(), + z() + { + } + //! 2D int constructor - Point(int a, int b); + Point(int a, int b): + x(a), + y(b), + z() + { + } + //! 3D int constructor - Point(int a, int b, int c); + Point(int a, int b, int c): + x(a), + y(b), + z(c) + { + } + //! conversion from Vec2 - explicit Point(const Vec2& v); + Point(const Vec2& v): + x(static_cast<int>(round(v[0]))), + y(static_cast<int>(round(v[1]))), + z() + { + } + //! conversion from Vec3 - explicit Point(const Vec3& v); + Point(const Vec3& v): + x(static_cast<int>(round(v[0]))), + y(static_cast<int>(round(v[1]))), + z(static_cast<int>(round(v[2]))) + { + } + //! conversion from Vec4 (normalization) - explicit Point(const Vec4& v); + explicit Point(const Vec4& v): + x(static_cast<int>(round(v[0]))), + y(static_cast<int>(round(v[1]))), + z(static_cast<int>(round(v[2]))) + { + if(std::abs(v[3])<1e-100) { + throw geom::OutOfRangeException("4th element of Vec4 is too close to zero for normalization"); + } else { + Real sf = 1.0/v[3]; + x*=sf; + y*=sf; + z*=sf; + } + } //! (implicit) conversion of size to point - Point(const Size& s); + Point(const Size& size); //! return mirror point according to planes Point Mirror(int planes); // operators - int operator[](unsigned int index) const; + int& operator[](unsigned int index) + { + assert(index<=2); + return (&x)[index]; + } + + int operator[](unsigned int index) const + { + assert(index<=2); + return (&x)[index]; + } + // operators + + int& At(unsigned int index) + { + if(index>2) throw std::range_error("Point index out of range"); + return (&x)[index]; + } + + int At(unsigned int index) const + { + if(index>2) throw geom::OutOfRangeException("Point index out of range"); + return (&x)[index]; + } - int& operator[](unsigned int index); Point& operator=(const Point& p); @@ -82,49 +160,24 @@ public: Point operator-() const; - bool operator==(const Point &p) const; - - bool operator!=(const Point &p) const; - - bool operator<(const Point &p) const; - - bool operator<=(const Point &p) const; - - bool operator>(const Point &p) const; - - bool operator>=(const Point &p) const; - Point& operator+=(const Size& p); Point& operator-=(const Size& p); + bool operator==(const Point &p) const; + + bool operator<(const Point &p) const; + // conversion to vectors Vec2 ToVec2() const; Vec3 ToVec3() const; Vec4 ToVec4() const; -private: - int data_[3]; - - bool equal(const Point &p) const; - bool less(const Point &p) const; - Point neg() const; - - friend Point absolute(const Point&); - - Point absolute() const; + int x; + int y; + int z; }; -DLLEXPORT_OST_IMG_BASE Point operator+(const Point& p1, const Point& p2); -DLLEXPORT_OST_IMG_BASE Point operator-(const Point& p1, const Point& p2); - - /* -Point operator+(const Point& p, const Size& s); -Point operator+(const Size& s, const Point& p); - -Point operator-(const Point& p, const Size& s); -Point operator-(const Size& s, const Point& p); - */ DLLEXPORT_OST_IMG_BASE std::ostream& operator<<(std::ostream& os, const Point &p); -- GitLab