URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-09-27 Simon Nivault <simon.nivault(a)lrde.epita.fr>
Somes fixes.
* mln/core/tr_image.hh: Add routines has.
* mln/fun/x2x/rotation.hh: Fix and factor some code.
* mln/metal/mat.hh: Fix operator*.
* tests/tr_image.cc: Update, test in 3d.
---
mln/core/tr_image.hh | 15 +++++++++++
mln/fun/x2x/rotation.hh | 63 +++++++++++++++++++++++++-----------------------
mln/metal/mat.hh | 52 +++++++++++++++++++--------------------
tests/tr_image.cc | 26 ++++++++++---------
4 files changed, 88 insertions(+), 68 deletions(-)
Index: trunk/milena/tests/tr_image.cc
===================================================================
--- trunk/milena/tests/tr_image.cc (revision 1186)
+++ trunk/milena/tests/tr_image.cc (revision 1187)
@@ -32,35 +32,37 @@
#include <iostream>
-#include <mln/fun/x2x/translation.hh>
-#include <mln/core/image2d_b.hh>
+#include <mln/fun/x2x/rotation.hh>
+#include <mln/core/image3d_b.hh>
#include <mln/value/int_u8.hh>
-#include <mln/io/pgm/load.hh>
-#include <mln/io/pgm/save.hh>
#include <mln/core/tr_image.hh>
+#include <mln/debug/iota.hh>
+#include <mln/debug/println.hh>
int main()
{
using namespace mln;
using value::int_u8;
- image2d_b<int_u8> lena = io::pgm::load("../img/lena.pgm");
- image2d_b<int_u8> out(lena.domain());
+ image3d_b<int_u8> in(3, 3, 3);
+ image3d_b<int_u8> out(in.domain());
- fun::x2x::translation<2,float> rot1(make::vec(10., 10.));
+ debug::iota(in);
+ debug::println(in);
- tr_image<fun::x2x::translation<2,float>, image2d_b<int_u8> >
inter(lena, rot1);
+ fun::x2x::rotation<3,float> rot1(1.67, 0);
+ tr_image<fun::x2x::rotation<3,float>, image3d_b<int_u8> > inter(in,
rot1);
- image2d_b<int_u8>::fwd_piter p(out.domain());
+ image3d_b<int_u8>::fwd_piter p(out.domain());
for_all(p)
{
- metal::vec<2,int> vec = (image2d_b<int_u8>::point)p;
- if (inter.owns_(vec))
+ metal::vec<3,int> vec = (image3d_b<int_u8>::point)p;
+ if (inter.has(vec))
out(p) = inter(vec);
else
out(p) = 255;
}
- io::pgm::save(out, "out.pgm");
+ debug::println(out);
}
Index: trunk/milena/mln/core/tr_image.hh
===================================================================
--- trunk/milena/mln/core/tr_image.hh (revision 1186)
+++ trunk/milena/mln/core/tr_image.hh (revision 1187)
@@ -103,6 +103,11 @@
/// Test if a pixel value is accessible at \p v.
bool owns_(const mln::metal::vec<I::point::dim, float>& v) const;
+ using super_::has;
+
+ /// Test if a pixel value is belonging to image at \p v.
+ bool has(const mln::metal::vec<I::point::dim, float>& v) const;
+
/// Read-only access of pixel value at point site \p p.
/// Mutable access is only OK for reading (not writing).
using super_::operator();
@@ -160,6 +165,16 @@
}
template <typename T, typename I>
+ bool tr_image<T,I>::has(const metal::vec<I::point::dim, float>& v)
const
+ {
+ mln_point(I) p;
+ metal::vec<I::point::dim, float> v2 = this->data_->tr_.inv()(v);
+ for (unsigned i = 0; i < I::point::dim; ++i)
+ p[i] = static_cast<int>(round(v2[i]));
+ return this->data_->ima_.domain().has(p);
+ }
+
+ template <typename T, typename I>
mln_value(I)
tr_image<T,I>::operator()(const metal::vec<I::point::dim, float>& v)
const
{
Index: trunk/milena/mln/fun/x2x/rotation.hh
===================================================================
--- trunk/milena/mln/fun/x2x/rotation.hh (revision 1186)
+++ trunk/milena/mln/fun/x2x/rotation.hh (revision 1187)
@@ -68,6 +68,8 @@
void set_alpha(float alpha);
void set_dir(unsigned dir);
+ void update();
+
protected:
float alpha_;
@@ -89,23 +91,8 @@
:alpha_(alpha),
dir_(dir)
{
- const float cos_a = cos(alpha_);
- const float sin_a = sin(alpha_);
- const metal::vec<4,float> vec = make::vec(cos_a, -sin_a, sin_a, cos_a);
-
- m_ = metal::mat<n+1,n+1,C>::Id;
- unsigned k = 0;
- for (unsigned i = 0; i <= n; ++i)
- {
- if (i == dir)
- continue;
- for (unsigned j = 0; j <= n; ++j)
- {
- if (j == dir)
- continue;
- m_(i, j) = vec[k++];
- }
- }
+ mln_precondition(dir == 2 || n == 3);
+ update();
}
template <unsigned n, typename C>
@@ -139,40 +126,56 @@
void
rotation<n,C>::set_alpha(float alpha)
{
- const float cos_a = cos(alpha);
- const float sin_a = sin(alpha);
-
alpha_ = alpha;
-
- m_(0,0) = cos_a;
- m_(0,1) = -sin_a;
- m_(1,0) = sin_a;
- m_(1,1) = cos_a;
+ update();
}
template <unsigned n, typename C>
void
rotation<n,C>::set_dir(unsigned dir)
{
+ mln_precondition(dir == 2 || n == 3);
+ dir_ = dir;
+ update();
+ }
+
+ template <unsigned n, typename C>
+ void
+ rotation<n,C>::update()
+ {
const float cos_a = cos(alpha_);
const float sin_a = sin(alpha_);
- const metal::vec<4,float> vec(cos_a, -sin_a, sin_a, cos_a);
+ const metal::vec<4,float> vec = make::vec(cos_a, -sin_a, sin_a, cos_a);
m_ = metal::mat<n+1,n+1,C>::Id;
unsigned k = 0;
- for (unsigned i = 0; i <= n; ++i)
+ for (unsigned i = 0; i < n; ++i)
{
- if (i == dir)
+ if (i == dir_)
continue;
- for (unsigned j = 0; j <= n; ++j)
+ for (unsigned j = 0; j < n; ++j)
{
- if (j == dir)
+ if (j == dir_)
continue;
m_(i, j) = vec[k++];
}
}
}
+// template <typename C> FIXME : template parameter should be swapped
+// void
+// rotation<2,C>::update()
+// {
+// const float cos_a = cos(alpha_);
+// const float sin_a = sin(alpha_);
+
+// m_ = metal::mat<3,3,C>::Id;
+// m_(0, 0) = cos_a;
+// m_(0, 1) = -sin_a;
+// m_(1, 0) = sin_a;
+// m_(1, 1) = cos_a;
+// }
+
# endif // ! MLN_INCLUDE_ONLY
Index: trunk/milena/mln/metal/mat.hh
===================================================================
--- trunk/milena/mln/metal/mat.hh (revision 1186)
+++ trunk/milena/mln/metal/mat.hh (revision 1187)
@@ -110,14 +110,6 @@
// *
- template <unsigned n, unsigned m, typename T, typename U>
- mat<n,m,T>&
- operator*=(mat<n,m,T>& lhs, const U& scalar);
-
- template <unsigned n, unsigned m, typename T, typename U>
- mat<n,m,typename binary_arith_trait<T,U>::ret>
- operator*(mat<n,m,T>& lhs, const U& scalar);
-
template <unsigned n, unsigned m, unsigned o, typename T, typename U>
mat<n,m,T>&
operator*=(mat<n,o,T>& lhs, mat<o,m,U>& rhs);
@@ -126,6 +118,14 @@
mat<n,m,typename binary_arith_trait<T,U>::ret>
operator*(const mat<n,o,T>& lhs, const mat<o,m,U>& rhs);
+ template <unsigned n, unsigned m, typename T, typename U>
+ mat<n,m,T>&
+ operator*=(mat<n,m,T>& lhs, const U& scalar);
+
+ template <unsigned n, unsigned m, typename T, typename U>
+ mat<n,m,typename binary_arith_trait<T,U>::ret>
+ operator*(const U& scalar, mat<n,m,T>& lhs);
+
// /
template <unsigned n, unsigned m, typename T, typename U>
@@ -294,45 +294,45 @@
// *
- template <unsigned n, unsigned m, typename T, typename U>
+ template <unsigned n, unsigned m, unsigned o, typename T, typename U>
mat<n,m,T>&
- operator*=(mat<n,m,T>& lhs, const U& scalar)
+ operator*=(mat<n,o,T>& lhs, mat<o,m,U>& rhs)
{
- for (unsigned i = 0; i < n; ++i)
- for (unsigned j = 0; j < m; ++j)
- lhs(i, j) *= scalar;
+ lhs = lhs * rhs;
return lhs;
}
- template <unsigned n, unsigned m, typename T, typename U>
+ template <unsigned n, unsigned m, unsigned o, typename T, typename U>
mat<n,m,typename binary_arith_trait<T,U>::ret>
- operator*(mat<n,m,T>& lhs, const U& scalar)
+ operator*(const mat<n,o,T>& lhs, const mat<o,m,U>& rhs)
{
mat<n,m,typename binary_arith_trait<T,U>::ret> tmp;
for (unsigned i = 0; i < n; ++i)
for (unsigned j = 0; j < m; ++j)
- tmp[i][j] = lhs(i, j) * scalar;
+ {
+ tmp(i, j) = 0;
+ for (unsigned k = 0; k < o; ++k)
+ tmp(i, j) += lhs(i, k) * rhs(k, j);
+ }
return tmp;
}
- template <unsigned n, unsigned m, unsigned o, typename T, typename U>
+ template <unsigned n, unsigned m, typename T, typename U>
mat<n,m,T>&
- operator*=(mat<n,o,T>& lhs, mat<o,m,U>& rhs)
+ operator*=(mat<n,m,T>& lhs, const U& scalar)
{
- lhs = lhs * rhs;
+ for (unsigned i = 0; i < n; ++i)
+ for (unsigned j = 0; j < m; ++j)
+ lhs(i, j) *= scalar;
return lhs;
}
- template <unsigned n, unsigned m, unsigned o, typename T, typename U>
+ template <unsigned n, unsigned m, typename T, typename U>
mat<n,m,typename binary_arith_trait<T,U>::ret>
- operator*(const mat<n,o,T>& lhs, const mat<o,m,U>& rhs)
+ operator*(const U& scalar, mat<n,m,T>& lhs)
{
mat<n,m,typename binary_arith_trait<T,U>::ret> tmp;
for (unsigned i = 0; i < n; ++i)
for (unsigned j = 0; j < m; ++j)
- {
- tmp(i, j) = 0;
- for (unsigned k = 0; k < o; ++k)
- tmp(i, j) += lhs(i, k) * rhs(k, j);
- }
+ tmp(i, j) = scalar * lhs(i, j);
return tmp;
}