Ocean
Triangulation.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_SEGMENTATION_TRIANGULATION_H
9 #define META_OCEAN_CV_SEGMENTATION_TRIANGULATION_H
10 
13 
15 #include "ocean/cv/PixelPosition.h"
16 
18 
19 #include <vector>
20 
21 namespace Ocean
22 {
23 
24 namespace CV
25 {
26 
27 namespace Segmentation
28 {
29 
30 /**
31  * This class implements functions allowing for triangulation.
32  * @ingroup cvsegmentation
33  */
34 class OCEAN_CV_SEGMENTATION_EXPORT Triangulation
35 {
36  public:
37 
38  /**
39  * This class holds coordinate index of a triangle.
40  */
41  class OCEAN_CV_SEGMENTATION_EXPORT IndexTriangle
42  {
43  public:
44 
45  /**
46  * Creates a new index triangle object with invalid indices.
47  */
48  inline IndexTriangle();
49 
50  /**
51  * Creates a new index triangle object with three valid indices.
52  * @param first First coordinate index, with range [0, infinity)
53  * @param second Second coordinate index, with range [0, infinity)
54  * @param third Third coordinate index, with range [0, infinity)
55  */
56  inline IndexTriangle(const unsigned int first, const unsigned int second, const unsigned int third);
57 
58  /**
59  * Returns whether this object is valid, which means that all three coordinate indices are different.
60  * @return True, if so
61  */
62  inline bool isValid() const;
63 
64  /**
65  * Returns the coordinate indices for the triangle.
66  * @param index The corner index for which the coordinate index will be returned, with range [0, 2]
67  * @return The triangle's coordinate indices
68  */
69  inline unsigned int operator[](const unsigned int index) const;
70 
71  protected:
72 
73  /// The three coordinate indices of the triangle.
74  unsigned int indices_[3];
75  };
76 
77  /**
78  * Definition of a vector holding index triangle objects.
79  */
80  typedef std::vector<IndexTriangle> IndexTriangles;
81 
82  private:
83 
84  /**
85  * Definition of a pair combining a pixel position index and a boolean state.
86  */
87  typedef std::pair<unsigned int, bool> PixelPositionPair;
88 
89  /**
90  * Definition of a vector holding pixel position pairs.
91  */
92  typedef std::vector<PixelPositionPair> PixelPositionPairs;
93 
94  public:
95 
96  /**
97  * Triangulates a given (sparse or dense) contour.
98  * Due to performance reasons and due to irregular coordinates a sparse contours is recommended.
99  * @param pixelContour The contour to be triangulated
100  * @param forceTriangulation True, to return a triangulation even if some of the coordinates does not allow a valid triangulation
101  * @param triangulationForced Optional resulting state whether the triangulation needed to be forced
102  * @return Resulting triangulation
103  * @tparam T The data type of the pixel contour, can be 'int' or 'unsigned int'
104  */
105  template <typename T>
106  static IndexTriangles triangulate(const PixelContourT<T>& pixelContour, const bool forceTriangulation = false, bool* triangulationForced = nullptr);
107 
108  /**
109  * Converts a given set of pixel coordinates and a corresponding triangulation into triangle objects.
110  * @param coordinates Set of pixel coordinates
111  * @param triangulation Corresponding triangulation
112  * @return Resulting triangles
113  * @tparam T The data type of the pixel coordinates, can be "int" or "unsigned int"
114  */
115  template <typename T>
116  static inline std::vector<Advanced::PixelTriangleT<T>> triangulation2triangles(const std::vector<PixelPositionT<T>>& coordinates, const IndexTriangles& triangulation);
117 };
118 
120 {
121  indices_[0] = (unsigned int)(-1);
122  indices_[1] = (unsigned int)(-1);
123  indices_[2] = (unsigned int)(-1);
124 }
125 
126 inline Triangulation::IndexTriangle::IndexTriangle(const unsigned int first, const unsigned int second, const unsigned int third)
127 {
128  indices_[0] = first;
129  indices_[1] = second;
130  indices_[2] = third;
131 }
132 
134 {
135  return indices_[0] != indices_[1] && indices_[0] != indices_[2] && indices_[1] != indices_[2];
136 }
137 
138 inline unsigned int Triangulation::IndexTriangle::operator[](const unsigned int index) const
139 {
140  ocean_assert(index < 3u);
141  return indices_[index];
142 }
143 
144 template <typename T>
145 inline std::vector<Advanced::PixelTriangleT<T>> Triangulation::triangulation2triangles(const std::vector<PixelPositionT<T>>& coordinates, const IndexTriangles& triangulation)
146 {
148  result.reserve(triangulation.size());
149 
150  for (IndexTriangles::const_iterator i = triangulation.begin(); i != triangulation.end(); ++i)
151  {
152  ocean_assert((*i)[0] < coordinates.size());
153  ocean_assert((*i)[1] < coordinates.size());
154  ocean_assert((*i)[2] < coordinates.size());
155 
156  result.push_back(Advanced::PixelTriangleT<T>(coordinates[(*i)[0]], coordinates[(*i)[1]], coordinates[(*i)[2]]));
157  }
158 
159  return result;
160 }
161 
162 }
163 
164 }
165 
166 }
167 
168 #endif // META_OCEAN_CV_SEGMENTATION_TRIANGULATION_H
This class implements a 2D triangle with pixel precision.
Definition: PixelTriangle.h:70
This class implements a 2D pixel position with pixel precision.
Definition: PixelPosition.h:65
This class implements a contour with pixel accuracy.
Definition: PixelContour.h:70
This class holds coordinate index of a triangle.
Definition: Triangulation.h:42
unsigned int operator[](const unsigned int index) const
Returns the coordinate indices for the triangle.
Definition: Triangulation.h:138
bool isValid() const
Returns whether this object is valid, which means that all three coordinate indices are different.
Definition: Triangulation.h:133
IndexTriangle()
Creates a new index triangle object with invalid indices.
Definition: Triangulation.h:119
unsigned int indices_[3]
The three coordinate indices of the triangle.
Definition: Triangulation.h:74
This class implements functions allowing for triangulation.
Definition: Triangulation.h:35
static std::vector< Advanced::PixelTriangleT< T > > triangulation2triangles(const std::vector< PixelPositionT< T >> &coordinates, const IndexTriangles &triangulation)
Converts a given set of pixel coordinates and a corresponding triangulation into triangle objects.
Definition: Triangulation.h:145
std::vector< IndexTriangle > IndexTriangles
Definition of a vector holding index triangle objects.
Definition: Triangulation.h:80
std::pair< unsigned int, bool > PixelPositionPair
Definition of a pair combining a pixel position index and a boolean state.
Definition: Triangulation.h:87
static IndexTriangles triangulate(const PixelContourT< T > &pixelContour, const bool forceTriangulation=false, bool *triangulationForced=nullptr)
Triangulates a given (sparse or dense) contour.
std::vector< PixelPositionPair > PixelPositionPairs
Definition of a vector holding pixel position pairs.
Definition: Triangulation.h:92
std::vector< PixelTriangle > PixelTriangles
Definition of a vector holding pixel triangles (with positive coordinate values).
Definition: PixelTriangle.h:53
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15