MeshLib C++ Docs
Loading...
Searching...
No Matches
MRContour.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRPch/MRBindingMacros.h"
4#include "MRMeshFwd.h"
5#include "MRVector3.h"
6#include <cassert>
7
8namespace MR
9{
10
14
17template<typename T, typename R = T>
18R calcOrientedArea( const Contour2<T> & contour )
19{
20 if ( contour.size() < 3 )
21 return 0;
22
23 R area = 0;
24 Vector2<R> p0{ contour[0] };
25
26 for ( int i = 2; i < contour.size(); ++i )
27 {
28 Vector2<R> p1{ contour[i - 1] };
29 Vector2<R> p2{ contour[i] };
30 area += cross( p2 - p0, p1 - p0 );
31 }
32
33 return R(0.5) * area;
34}
35
39template<typename T, typename R = T>
40Vector3<R> calcOrientedArea( const Contour3<T> & contour )
41{
42 if ( contour.size() < 3 )
43 return {};
44
46 Vector3<R> p0{ contour[0] };
47
48 for ( int i = 2; i < contour.size(); ++i )
49 {
50 Vector3<R> p1{ contour[i - 1] };
51 Vector3<R> p2{ contour[i] };
52 area += cross( p1 - p0, p2 - p0 );
53 }
54
55 return R(0.5) * area;
56}
57
60template<typename V, typename R = typename V::ValueType>
61R calcLength( const Contour<V>& contour )
62{
63 R l = R( 0 );
64 for ( int i = 1; i < contour.size(); ++i )
65 l += R( ( contour[i] - contour[i - 1] ).length() );
66 return l;
67}
68
71template<typename V, typename R = typename V::ValueType>
72size_t findContourPointByLength( const Contour<V>& contour, R targetLen )
73{
74 if ( contour.empty() )
75 {
76 assert( false );
77 return size_t( -1 );
78 }
79 if ( targetLen <= 0 )
80 return 0;
81 R l = R( 0 );
82 for ( size_t i = 1; i < contour.size(); ++i )
83 {
84 assert( targetLen > l );
85 auto l1 = l + R( ( contour[i] - contour[i - 1] ).length() );
86 if ( targetLen <= l1 )
87 return 2 * targetLen < ( l + l1 ) ? i - 1 : i;
88 l = l1;
89 }
90 return contour.size() - 1;
91}
92
96template<typename To, typename From>
97MR_BIND_IGNORE To convertContour( const From & from )
98{
99 To res;
100 res.reserve( from.size() );
101 for ( const auto & p : from )
102 res.emplace_back( p );
103 return res;
104}
105
109template<typename To, typename From>
110MR_BIND_IGNORE To convertContours( const From & from )
111{
112 To res;
113 res.reserve( from.size() );
114 for ( const auto & c : from )
115 res.push_back( convertContour<typename To::value_type>( c ) );
116 return res;
117}
118
120
122MR_BIND_TEMPLATE( float calcOrientedArea( const Contour2<float> & contour ) )
123MR_BIND_TEMPLATE( double calcOrientedArea( const Contour2<double> & contour ) )
124MR_BIND_TEMPLATE( float calcLength( const Contour2<float>& contour ) )
125MR_BIND_TEMPLATE( double calcLength( const Contour2<double>& contour ) )
126MR_BIND_TEMPLATE( float calcLength( const Contour3<float>& contour ) )
127MR_BIND_TEMPLATE( double calcLength( const Contour3<double>& contour ) )
128MR_BIND_TEMPLATE( Vector3<float> calcOrientedArea( const Contour3<float> & contour ) )
129MR_BIND_TEMPLATE( Vector3<double> calcOrientedArea( const Contour3<double> & contour ) )
130
131
132template <typename From> [[nodiscard]] Contour2f convertContourTo2f( const From &from ) { return convertContour<Contour2f>( from ); }
133template <typename From> [[nodiscard]] Contour3f convertContourTo3f( const From &from ) { return convertContour<Contour3f>( from ); }
134template <typename From> [[nodiscard]] Contour2d convertContourTo2d( const From &from ) { return convertContour<Contour2d>( from ); }
135template <typename From> [[nodiscard]] Contour3d convertContourTo3d( const From &from ) { return convertContour<Contour3d>( from ); }
136
137template <typename From> [[nodiscard]] Contours2f convertContoursTo2f( const From &from ) { return convertContours<Contours2f>( from ); }
138template <typename From> [[nodiscard]] Contours3f convertContoursTo3f( const From &from ) { return convertContours<Contours3f>( from ); }
139template <typename From> [[nodiscard]] Contours2d convertContoursTo2d( const From &from ) { return convertContours<Contours2d>( from ); }
140template <typename From> [[nodiscard]] Contours3d convertContoursTo3d( const From &from ) { return convertContours<Contours3d>( from ); }
141
142MR_BIND_TEMPLATE( Contour2f convertContourTo2f( const Contour2f & from ) )
143MR_BIND_TEMPLATE( Contour2f convertContourTo2f( const Contour2d & from ) )
144MR_BIND_TEMPLATE( Contour2f convertContourTo2f( const Contour3f & from ) )
145MR_BIND_TEMPLATE( Contour2f convertContourTo2f( const Contour3d & from ) )
146MR_BIND_TEMPLATE( Contour2d convertContourTo2d( const Contour2f & from ) )
147MR_BIND_TEMPLATE( Contour2d convertContourTo2d( const Contour2d & from ) )
148MR_BIND_TEMPLATE( Contour2d convertContourTo2d( const Contour3f & from ) )
149MR_BIND_TEMPLATE( Contour2d convertContourTo2d( const Contour3d & from ) )
150MR_BIND_TEMPLATE( Contour3f convertContourTo3f( const Contour2f & from ) )
151MR_BIND_TEMPLATE( Contour3f convertContourTo3f( const Contour2d & from ) )
152MR_BIND_TEMPLATE( Contour3f convertContourTo3f( const Contour3f & from ) )
153MR_BIND_TEMPLATE( Contour3f convertContourTo3f( const Contour3d & from ) )
154MR_BIND_TEMPLATE( Contour3d convertContourTo3d( const Contour2f & from ) )
155MR_BIND_TEMPLATE( Contour3d convertContourTo3d( const Contour2d & from ) )
156MR_BIND_TEMPLATE( Contour3d convertContourTo3d( const Contour3f & from ) )
157MR_BIND_TEMPLATE( Contour3d convertContourTo3d( const Contour3d & from ) )
158
159MR_BIND_TEMPLATE( Contours2f convertContoursTo2f( const Contours2f & from ) )
160MR_BIND_TEMPLATE( Contours2f convertContoursTo2f( const Contours2d & from ) )
161MR_BIND_TEMPLATE( Contours2f convertContoursTo2f( const Contours3f & from ) )
162MR_BIND_TEMPLATE( Contours2f convertContoursTo2f( const Contours3d & from ) )
163MR_BIND_TEMPLATE( Contours2d convertContoursTo2d( const Contours2f & from ) )
164MR_BIND_TEMPLATE( Contours2d convertContoursTo2d( const Contours2d & from ) )
165MR_BIND_TEMPLATE( Contours2d convertContoursTo2d( const Contours3f & from ) )
166MR_BIND_TEMPLATE( Contours2d convertContoursTo2d( const Contours3d & from ) )
167MR_BIND_TEMPLATE( Contours3f convertContoursTo3f( const Contours2f & from ) )
168MR_BIND_TEMPLATE( Contours3f convertContoursTo3f( const Contours2d & from ) )
169MR_BIND_TEMPLATE( Contours3f convertContoursTo3f( const Contours3f & from ) )
170MR_BIND_TEMPLATE( Contours3f convertContoursTo3f( const Contours3d & from ) )
171MR_BIND_TEMPLATE( Contours3d convertContoursTo3d( const Contours2f & from ) )
172MR_BIND_TEMPLATE( Contours3d convertContoursTo3d( const Contours2d & from ) )
173MR_BIND_TEMPLATE( Contours3d convertContoursTo3d( const Contours3f & from ) )
174MR_BIND_TEMPLATE( Contours3d convertContoursTo3d( const Contours3d & from ) )
175
176}
R calcLength(const Contour< V > &contour)
Definition MRContour.h:61
MR_BIND_IGNORE To convertContour(const From &from)
Definition MRContour.h:97
MR_BIND_IGNORE To convertContours(const From &from)
Definition MRContour.h:110
size_t findContourPointByLength(const Contour< V > &contour, R targetLen)
Definition MRContour.h:72
R calcOrientedArea(const Contour2< T > &contour)
Definition MRContour.h:18
Contour
Definition MRObjectLabel.h:20
float area(const MeshTopology &topology, const VertCoords &points, FaceId f)
returns the area of given face
Definition MRMeshMath.h:168
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:13
length
Definition MRObjectDimensionsEnum.h:17
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Contour2f convertContourTo2f(const From &from)
Instantiate the templates when generating bindings.
Definition MRContour.h:132
Contours2d convertContoursTo2d(const From &from)
Definition MRContour.h:139
Contours2f convertContoursTo2f(const From &from)
Definition MRContour.h:137
Contours3f convertContoursTo3f(const From &from)
Definition MRContour.h:138
Contour3f convertContourTo3f(const From &from)
Definition MRContour.h:133
Contour2d convertContourTo2d(const From &from)
Definition MRContour.h:134
Contour3d convertContourTo3d(const From &from)
Definition MRContour.h:135
Contours3d convertContoursTo3d(const From &from)
Definition MRContour.h:140
Definition MRVector2.h:29
Definition MRVector3.h:33