AliceVision
Photogrammetric Computer Vision Framework
PointFeature.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // Copyright (c) 2012 openMVG contributors.
4 // This Source Code Form is subject to the terms of the Mozilla Public License,
5 // v. 2.0. If a copy of the MPL was not distributed with this file,
6 // You can obtain one at https://mozilla.org/MPL/2.0/.
7 
8 #pragma once
9 
10 #include "aliceVision/numeric/numeric.hpp"
11 #include <iostream>
12 #include <iterator>
13 #include <fstream>
14 #include <string>
15 #include <vector>
16 
17 namespace aliceVision {
18 namespace feature {
19 
25 {
26  friend std::ostream& operator<<(std::ostream& out, const PointFeature& obj);
27  friend std::istream& operator>>(std::istream& in, PointFeature& obj);
28 
29  public:
30  PointFeature() {}
31  PointFeature(float x, float y, float scale, float orient)
32  : _coords(x, y),
33  _scale(scale),
34  _orientation(orient)
35  {}
36 
37  float x() const { return _coords(0); }
38  float y() const { return _coords(1); }
39  const Vec2f& coords() const { return _coords; }
40  float scale() const { return _scale; }
41  float& scale() { return _scale; }
42 
43  float& x() { return _coords(0); }
44  float& y() { return _coords(1); }
45  Vec2f& coords() { return _coords; }
46 
47  float orientation() const { return _orientation; }
48  float& orientation() { return _orientation; }
49 
54  Vec2f getOrientationVector() const { return Vec2f(std::cos(orientation()), std::sin(orientation())); }
55 
60  Vec2f getScaledOrientationVector() const { return scale() * getOrientationVector(); }
61 
62  bool operator==(const PointFeature& b) const
63  {
64  return (_scale == b.scale()) && (_orientation == b.orientation()) && (x() == b.x()) && (y() == b.y());
65  }
66 
67  bool operator!=(const PointFeature& b) const { return !((*this) == b); }
68 
69  protected:
70  Vec2f _coords = {0.0f, 0.0f}; // (x, y)
71  float _scale = 0.0f; // In pixels.
72  float _orientation = 0.0f; // In radians.
73 };
74 
75 typedef std::vector<PointFeature> PointFeatures;
76 
77 // with overloaded operators:
78 inline std::ostream& operator<<(std::ostream& out, const PointFeature& obj)
79 {
80  return out << obj._coords(0) << " " << obj._coords(1) << " " << obj._scale << " " << obj._orientation;
81 }
82 
83 inline std::istream& operator>>(std::istream& in, PointFeature& obj)
84 {
85  return in >> obj._coords(0) >> obj._coords(1) >> obj._scale >> obj._orientation;
86 }
87 
89 template<typename FeaturesT>
90 inline void loadFeatsFromFile(const std::string& sfileNameFeats, FeaturesT& vec_feat)
91 {
92  vec_feat.clear();
93 
94  std::ifstream fileIn(sfileNameFeats);
95 
96  if (!fileIn.is_open())
97  throw std::runtime_error("Can't load features file, can't open '" + sfileNameFeats + "' !");
98 
99  std::copy(std::istream_iterator<typename FeaturesT::value_type>(fileIn),
100  std::istream_iterator<typename FeaturesT::value_type>(),
101  std::back_inserter(vec_feat));
102  if (fileIn.bad())
103  throw std::runtime_error("Can't load features file, '" + sfileNameFeats + "' is incorrect !");
104  fileIn.close();
105 }
106 
108 template<typename FeaturesT>
109 inline void saveFeatsToFile(const std::string& sfileNameFeats, FeaturesT& vec_feat)
110 {
111  std::ofstream file(sfileNameFeats);
112 
113  if (!file.is_open())
114  throw std::runtime_error("Can't save features file, can't open '" + sfileNameFeats + "' !");
115 
116  std::copy(vec_feat.begin(), vec_feat.end(), std::ostream_iterator<typename FeaturesT::value_type>(file, "\n"));
117 
118  if (!file.good())
119  throw std::runtime_error("Can't save features file, '" + sfileNameFeats + "' is incorrect !");
120 
121  file.close();
122 }
123 
125 template<typename FeaturesT, typename MatT>
126 void PointsToMat(const FeaturesT& vec_feats, MatT& m)
127 {
128  m.resize(2, vec_feats.size());
129  typedef typename FeaturesT::value_type ValueT; // Container type
130 
131  size_t i = 0;
132  for (typename FeaturesT::const_iterator iter = vec_feats.begin(); iter != vec_feats.end(); ++iter, ++i)
133  {
134  const ValueT& feat = *iter;
135  m.col(i) << feat.x(), feat.y();
136  }
137 }
138 
139 } // namespace feature
140 } // namespace aliceVision
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::feature::PointFeature
Definition: PointFeature.hpp:24
aliceVision::feature::PointFeature::getOrientationVector
Vec2f getOrientationVector() const
Return the orientation of the feature as an unit vector.
Definition: PointFeature.hpp:54
aliceVision::feature::PointFeature::getScaledOrientationVector
Vec2f getScaledOrientationVector() const
Return the orientation of the feature as a vector scaled to the scale of the feature.
Definition: PointFeature.hpp:60