reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofPolyline.h
Go to the documentation of this file.
1// This include guard is not a pragma once on purpose
2// so some IDEs are happy include this file back form the corresponding inl
3#ifndef OF_POLYLINE_H
4#define OF_POLYLINE_H
5
6#include "ofConstants.h"
7#include "glm/fwd.hpp"
8#include <deque>
9
50
51
52
53class ofRectangle;
54
55template<class T>
57public:
58 using VertexType = T;
59
62
65
67 ofPolyline_(const std::vector<T>& verts);
68
69 static ofPolyline_ fromRectangle(const ofRectangle& rect);
70
74
76 void clear();
77
79 void addVertex( const T& p );
80
82 void addVertex( float x, float y, float z=0 );
83
104 void addVertices( const std::vector<T>& verts );
105
108 void addVertices(const T* verts, int numverts);
109
110 void insertVertex(const T &p, int index);
111 void insertVertex(float x, float y, float z, int index);
112
113
120 void removeVertex(int index);
121
124 void resize(size_t size);
125
129
131 size_t size() const;
132
146 const T& operator[] (int index) const;
147 T& operator[] (int index);
148
150 std::vector<T> & getVertices();
151 const std::vector<T> & getVertices() const;
152
153 typename std::vector<T>::iterator begin();
154 typename std::vector<T>::const_iterator begin() const;
155 typename std::vector<T>::reverse_iterator rbegin();
156 typename std::vector<T>::const_reverse_iterator rbegin() const;
157 typename std::vector<T>::iterator end();
158 typename std::vector<T>::const_iterator end() const;
159 typename std::vector<T>::reverse_iterator rend();
160 typename std::vector<T>::const_reverse_iterator rend() const;
161
165
168 void lineTo(const T & to ){ addVertex(to); }
169
172 void lineTo(float x, float y, float z=0){
173 addVertex(x,y,z);
174 }
175
190 void arc(const T & center, float radiusX, float radiusY, float angleBegin, float angleEnd, bool clockwise, int circleResolution = 20);
191
219 void arc(const T & center, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution = 20) {
220 arc(center, radiusX, radiusY, angleBegin, angleEnd, true, circleResolution);
221 }
222
231 void arc(float x, float y, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution = 20){
232 arc(T(x, y, 0.f), radiusX, radiusY, angleBegin, angleEnd, true, circleResolution);
233 }
234
243 void arc(float x, float y, float z, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution = 20){
244 arc(T(x, y, z), radiusX, radiusY, angleBegin, angleEnd, true, circleResolution);
245 }
246 void arcNegative(const T & center, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution = 20) {
247 arc(center, radiusX, radiusY, angleBegin, angleEnd, false, circleResolution);
248 }
249 void arcNegative(float x, float y, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution = 20){
250 arc(T(x,y,0.f), radiusX, radiusY, angleBegin, angleEnd, false, circleResolution);
251 }
252 void arcNegative(float x, float y, float z, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution = 20){
253 arc(T(x, y, z), radiusX, radiusY, angleBegin, angleEnd, false, circleResolution);
254 }
255
256
257
271 void curveTo( const T & to, int curveResolution = 20 );
272
275 void curveTo(float x, float y, float z = 0, int curveResolution = 20 ){
276 curveTo({x,y,z},curveResolution);
277 }
278
289 void bezierTo( const T & cp1, const T & cp2, const T & to, int curveResolution = 20);
290
294 void bezierTo(float cx1, float cy1, float cx2, float cy2, float x, float y, int curveResolution = 20){
295 bezierTo({cx1,cy1,0.f}, {cx2,cy2,0.f}, {x,y,0.f}, curveResolution);
296 }
297
301 void bezierTo(float cx1, float cy1, float cz1, float cx2, float cy2, float cz2, float x, float y, float z, int curveResolution = 20){
302 bezierTo({cx1,cy1,cz1}, {cx2,cy2,cz2}, {x,y,z}, curveResolution);
303 }
304
311 void quadBezierTo(float cx1, float cy1, float cz1, float cx2, float cy2, float cz2, float x, float y, float z, int curveResolution = 20);
312
316 void quadBezierTo( const T & p1, const T & p2, const T & p3, int curveResolution = 20 ){
317 quadBezierTo(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z,p3.x,p3.y,p3.z,curveResolution);
318 }
319
323 void quadBezierTo(float cx1, float cy1, float cx2, float cy2, float x, float y, int curveResolution = 20){
324 quadBezierTo(cx1,cy1,0,cx2,cy2,0,x,y,0,curveResolution);
325 }
326
330
338 ofPolyline_ getSmoothed(int smoothingSize, float smoothingShape = 0) const;
339
349 ofPolyline_ getResampledBySpacing(float spacing) const;
350
355 ofPolyline_ getResampledByCount(int count) const;
356
362 void simplify(float tolerance=0.3f);
363
367
368 void rotateDeg(float degrees, const glm::vec3& axis);
369 void rotateRad(float radians, const glm::vec3& axis);
370 OF_DEPRECATED_MSG("Use Deg/Rad versions.", void rotate(float degrees, const glm::vec3& axis));
371
372 void translate(const glm::vec3 & p);
373
374 void rotateDeg(float degrees, const glm::vec2& axis);
375 void rotateRad(float radians, const glm::vec2& axis);
376 OF_DEPRECATED_MSG("Use Deg/Rad versions.", void rotate(float degrees, const glm::vec2& axis));
377
378 void translate(const glm::vec2 & p);
379
383 void scale(float x, float y);
384
388
391 void close();
392
395 void setClosed( bool tf );
396 bool isClosed() const;
397
398
400 bool hasChanged();
401 void flagHasChanged();
402
406
408 static bool inside(float x, float y, const ofPolyline_ & polyline);
410 bool inside(float x, float y) const;
411
413 static bool inside(const T & p, const ofPolyline_ & polyline);
415 bool inside(const T & p) const;
416
420
424 float getPerimeter() const;
425
427 float getArea() const;
428
430 T getCentroid2D() const;
431
435 T getClosestPoint(const T& target, unsigned int* nearestIndex = nullptr) const;
436
437
441
446 float getIndexAtLength(float f) const;
447
452 float getIndexAtPercent(float f) const;
453
455 float getLengthAtIndex(int index) const;
456
459 float getLengthAtIndexInterpolated(float findex) const;
460
463 T getPointAtLength(float f) const;
464
467 T getPointAtPercent(float f) const;
468
471 T getPointAtIndexInterpolated(float findex) const;
472
474 OF_DEPRECATED_MSG("Use Deg/Rad versions.", float getAngleAtIndex(int index) const);
475
478 OF_DEPRECATED_MSG("Use Deg/Rad versions.", float getAngleAtIndexInterpolated(float findex) const);
479
481 T getRotationAtIndex(int index) const;
482
485 T getRotationAtIndexInterpolated(float findex) const;
486
488 float getDegreesAtIndex(int index) const;
489
492 float getDegreesAtIndexInterpolated(float findex) const;
493
495 float getRadiansAtIndex(int index) const;
496
499 float getRadiansAtIndexInterpolated(float findex) const;
500
502 T getTangentAtIndex(int index) const;
503
506 T getTangentAtIndexInterpolated(float findex) const;
507
509 T getNormalAtIndex(int index) const;
510
513 T getNormalAtIndexInterpolated(float findex) const;
514
516 int getWrappedIndex(int index) const;
517
518 // used for calculating the normals
519 void setRightVector(T v = T(0, 0, -1));
520 T getRightVector() const;
521
525
527 void draw() const;
528
530
531
532private:
533 void setCircleResolution(int res);
534 float wrapAngle(float angleRad);
535
536 std::vector<T> points;
537 T rightVector;
538
539 // cache
540 mutable std::vector<float> lengths; // cumulative lengths, stored per point (lengths[n] is the distance to the n'th point, zero based)
541 mutable std::vector<T> tangents; // tangent at vertex, stored per point
542 mutable std::vector<T> normals; //
543 mutable std::vector<T> rotations; // rotation axes between adjacent segments, stored per point (cross product)
544 mutable std::vector<float> angles; // angle (rad) between adjacent segments, stored per point (asin(cross product))
545 mutable T centroid2D;
546 mutable float area;
547
548
549 std::deque<T> curveVertices;
550 std::vector<T> circlePoints;
551
552 bool bClosed;
553 bool bHasChanged; // public API has access to this
554 mutable bool bCacheIsDirty; // used only internally, no public API to read
555
556 void updateCache(bool bForceUpdate = false) const;
557
558 // given an interpolated index (e.g. 5.75) return neighboring indices and interolation factor (e.g. 5, 6, 0.75)
559 void getInterpolationParams(float findex, int &i1, int &i2, float &t) const;
560
561 void calcData(int index, T &tangent, float &angle, T &rotation, T &normal) const;
562};
563
564#include "ofPolyline.inl"
565
567
573template<class T>
574bool ofInsidePoly(float x, float y, const std::vector<T>& polygon){
575 return ofPolyline_<T>::inside(x,y, ofPolyline_<T>(polygon));
576}
577
578
583template<class T>
584bool ofInsidePoly(const T& p, const std::vector<T>& poly){
585 return ofPolyline_<T>::inside(p.x,p.y, ofPolyline_<T>(poly));
586}
587
588#endif
Definition ofPolyline.h:56
float getPerimeter() const
Gets the size of the perimeter of the polyline, good for determining length of the line,...
Definition ofPolyline.inl:430
float getRadiansAtIndex(int index) const
Get angle (degrees) of the path at index.
Definition ofPolyline.inl:1012
size_t size() const
The number of points in the ofPolyline.
Definition ofPolyline.inl:111
OF_DEPRECATED_MSG("Use Deg/Rad versions.", void rotate(float degrees, const glm::vec2 &axis))
ofPolyline_ getSmoothed(int smoothingSize, float smoothingShape=0) const
Gets a smoothed version of the ofPolyline.
Definition ofPolyline.inl:470
float getIndexAtLength(float f) const
Get (interpolated) index at given length along the path.
Definition ofPolyline.inl:889
void quadBezierTo(float cx1, float cy1, float cz1, float cx2, float cy2, float cz2, float x, float y, float z, int curveResolution=20)
Adds a quadratic bezier line in 3D space from the current drawing point with the beginning indicated ...
Definition ofPolyline.inl:262
void bezierTo(float cx1, float cy1, float cx2, float cy2, float x, float y, int curveResolution=20)
Adds a cubic bezier line from the current drawing point with the 2 control points indicated by the co...
Definition ofPolyline.h:294
ofRectangle getBoundingBox() const
Get the bounding box of the polyline , taking into account all the points to determine the extents of...
Definition ofPolyline.inl:455
void arcNegative(float x, float y, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution=20)
Definition ofPolyline.h:249
void setClosed(bool tf)
Closes the ofPolyline, meaning that all the vertices will be linked and can be "walked".
Definition ofPolyline.inl:137
float getDegreesAtIndexInterpolated(float findex) const
Get angle (degrees) at interpolated index (interpolated between neighboring indices)
Definition ofPolyline.inl:1001
void quadBezierTo(const T &p1, const T &p2, const T &p3, int curveResolution=20)
Adds a quadratic bezier line in 2D space from the current drawing point with the beginning indicated ...
Definition ofPolyline.h:316
T getTangentAtIndexInterpolated(float findex) const
Get tangent vector at interpolated index (interpolated between neighboring indices)
Definition ofPolyline.inl:1056
OF_DEPRECATED_MSG("Use Deg/Rad versions.", float getAngleAtIndex(int index) const)
Get angle (degrees) of the path at index.
OF_DEPRECATED_MSG("Use Deg/Rad versions.", float getAngleAtIndexInterpolated(float findex) const)
Get angle (degrees) at interpolated index (interpolated between neighboring indices)
void addVertex(const T &p)
Adds a point using an T at the end of the ofPolyline.
Definition ofPolyline.inl:51
T getPointAtLength(float f) const
Get point long the path at a given length (e.g. f=150 => 150 units along the path)
Definition ofPolyline.inl:951
void clear()
Removes all the points from the ofPolyline.
Definition ofPolyline.inl:42
void arc(const T &center, float radiusX, float radiusY, float angleBegin, float angleEnd, bool clockwise, int circleResolution=20)
Adds an arc around the T center with the width of radiusX and the height of radiusY to the polyline.
Definition ofPolyline.inl:331
float getRadiansAtIndexInterpolated(float findex) const
Get angle (degrees) at interpolated index (interpolated between neighboring indices)
Definition ofPolyline.inl:1020
static bool inside(float x, float y, const ofPolyline_ &polyline)
Tests whether the x,y coordinates are within a closed ofPolyline.
Definition ofPolyline.inl:639
void bezierTo(const T &cp1, const T &cp2, const T &to, int curveResolution=20)
Adds a cubic bezier line from the current drawing point with the 2 control points indicated by T cp1 ...
Definition ofPolyline.inl:215
T getRotationAtIndex(int index) const
Get rotation vector at index (magnitude is sine of angle)
Definition ofPolyline.inl:1030
T getCentroid2D() const
Get the center of the area bounded by the line.
Definition ofPolyline.inl:448
bool hasChanged()
Returns whether the vertices within the line have changed.
Definition ofPolyline.inl:156
ofPolyline_()
Creates an ofPolyline.
Definition ofPolyline.inl:15
std::vector< T >::reverse_iterator rend()
Definition ofPolyline.inl:1238
void arcNegative(float x, float y, float z, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution=20)
Definition ofPolyline.h:252
void quadBezierTo(float cx1, float cy1, float cx2, float cy2, float x, float y, int curveResolution=20)
Adds a quadratic bezier line in 2D space from the current drawing point with the beginning indicated ...
Definition ofPolyline.h:323
T getNormalAtIndexInterpolated(float findex) const
Get normal vector at interpolated index (interpolated between neighboring indices)
Definition ofPolyline.inl:1074
void insertVertex(const T &p, int index)
Definition ofPolyline.inl:83
void setRightVector(T v=T(0, 0, -1))
Definition ofPolyline.inl:876
T getClosestPoint(const T &target, unsigned int *nearestIndex=nullptr) const
Gets the point on the line closest to the target. You can also optionally pass a pointer to/address o...
Definition ofPolyline.inl:583
T getRotationAtIndexInterpolated(float findex) const
Get rotation vector at interpolated index (interpolated between neighboring indices) (magnitude is si...
Definition ofPolyline.inl:1038
void flagHasChanged()
Definition ofPolyline.inl:167
float getArea() const
Gets the precise area bounded by the line.
Definition ofPolyline.inl:441
OF_DEPRECATED_MSG("Use Deg/Rad versions.", void rotate(float degrees, const glm::vec3 &axis))
float getDegreesAtIndex(int index) const
Get angle (degrees) of the path at index.
Definition ofPolyline.inl:993
bool isClosed() const
Definition ofPolyline.inl:144
void translate(const glm::vec3 &p)
Definition ofPolyline.inl:806
T VertexType
Definition ofPolyline.h:58
std::vector< T >::iterator end()
Definition ofPolyline.inl:1214
static ofPolyline_ fromRectangle(const ofRectangle &rect)
Definition ofPolyline.inl:30
void scale(float x, float y)
Change the size of the ofPolyline These changes are non-reversible, so for instance scaling by 0,...
Definition ofPolyline.inl:860
ofPolyline_ getResampledByCount(int count) const
Resamples the line based on the count passed in. The lower the count passed in, the more points will ...
Definition ofPolyline.inl:540
T getNormalAtIndex(int index) const
Get normal vector at index.
Definition ofPolyline.inl:1066
void arcNegative(const T &center, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution=20)
Definition ofPolyline.h:246
void draw() const
Draw the line using the current renderer.
Definition ofPolyline.inl:870
void arc(float x, float y, float z, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution=20)
Adds an arc around the coordinates (x,y,z) with the width of radiusX and the height of radiusY.
Definition ofPolyline.h:243
void rotateDeg(float degrees, const glm::vec3 &axis)
Definition ofPolyline.inl:821
float getLengthAtIndexInterpolated(float findex) const
Get length along path at interpolated index (e.g. f=5.75 => 75% along the path between 5th and 6th po...
Definition ofPolyline.inl:939
std::vector< T >::reverse_iterator rbegin()
Definition ofPolyline.inl:1232
const T & operator[](int index) const
Definition ofPolyline.inl:117
T getTangentAtIndex(int index) const
Get tangent vector at index.
Definition ofPolyline.inl:1048
T getRightVector() const
Definition ofPolyline.inl:883
T getPointAtIndexInterpolated(float findex) const
Get point along the path at interpolated index (e.g. f=5.75 => 75% along the path between 5th and 6th...
Definition ofPolyline.inl:967
void lineTo(const T &to)
Add a straight line from the last point added, or from 0,0 if no point is set, to the point indicated...
Definition ofPolyline.h:168
void close()
Closes the ofPolyline, meaning that all the vertices will be linked and can be "walked".
Definition ofPolyline.inl:150
ofPolyline_ getResampledBySpacing(float spacing) const
Resamples the line based on the spacing passed in. The larger the spacing, the more points will be el...
Definition ofPolyline.inl:517
void rotateRad(float radians, const glm::vec3 &axis)
Definition ofPolyline.inl:827
int getWrappedIndex(int index) const
Get wrapped index depending on whether poly is closed or not.
Definition ofPolyline.inl:1120
void simplify(float tolerance=0.3f)
Simplifies the polyline, removing un-necessary vertices.
Definition ofPolyline.inl:756
void arc(float x, float y, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution=20)
Adds an arc around the coordinates (x,y) with the width of radiusX and the height of radiusY.
Definition ofPolyline.h:231
void curveTo(float x, float y, float z=0, int curveResolution=20)
Adds a curve to the x,y,z points passed in with the optional resolution.
Definition ofPolyline.h:275
T getPointAtPercent(float f) const
Get point along the path at a given percentage (e.g. f=0.25 => 25% along the path)
Definition ofPolyline.inl:959
void bezierTo(float cx1, float cy1, float cz1, float cx2, float cy2, float cz2, float x, float y, float z, int curveResolution=20)
Adds a cubic bezier line in 3D space from the current drawing point with the 2 control points indicat...
Definition ofPolyline.h:301
void arc(const T &center, float radiusX, float radiusY, float angleBegin, float angleEnd, int circleResolution=20)
Adds an arc around the T center with the width of radiusX and the height of radiusY.
Definition ofPolyline.h:219
std::vector< T > & getVertices()
Gets a vector of vertices that the line contains.
Definition ofPolyline.inl:174
void removeVertex(int index)
Remove a vertex at a given index.
Definition ofPolyline.inl:98
std::vector< T >::iterator begin()
Definition ofPolyline.inl:1208
void curveTo(const T &to, int curveResolution=20)
Adds a curve to an T object passed in.
Definition ofPolyline.inl:279
float getLengthAtIndex(int index) const
Get length along path at index.
Definition ofPolyline.inl:931
void resize(size_t size)
Resize the number of points in the ofPolyline to the value passed in.
Definition ofPolyline.inl:130
void addVertices(const std::vector< T > &verts)
Add multiple points at the end of the ofPolyline using a vector of T objects.
Definition ofPolyline.inl:67
float getIndexAtPercent(float f) const
Get (interpolated) index at given percentage along the path.
Definition ofPolyline.inl:925
void lineTo(float x, float y, float z=0)
Add a straight line from the last point added, or from 0,0 if no point is set, to the point indicated...
Definition ofPolyline.h:172
A class representing a 2D rectangle.
Definition ofRectangle.h:87
bool ofInsidePoly(float x, float y, const std::vector< T > &polygon)
Determine if an (x,y) coordinate is within the polygon defined by a vector of glm::vec3s.
Definition ofPolyline.h:574