openEmbroider  0.1
an open source embroidery software
OE_utils.h
1 /*
2  * Copyright (c) 2015 Tricoire Sebastien 3dsman@free.fr
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  */
21 
22 #ifndef OE_UTILS_H
23 #define OE_UTILS_H
24 
25 #include <math.h>
26 #include <vector>
27 
28 struct vector_2d {
29  float x, y;
30  vector_2d( float _x, float _y) {
31  x = _x;
32  y = _y;
33  }
34  vector_2d() {}
35 
36  inline vector_2d operator *(const vector_2d& pt) const{
37  return {pt.x*x,pt.y*y};
38  }
39  inline vector_2d operator *( const float val) {
40  return {val*x,val*y};
41  }
42  inline vector_2d operator /(const vector_2d& pt) const{
43  return {x/pt.x,y/pt.y};
44  }
45  inline vector_2d operator /( const float val) {
46  return {x/val,y/val};
47  }
48  inline vector_2d operator +(const vector_2d pt) const{
49  return {pt.x+x,pt.y+y};
50  }
51  inline vector_2d operator +(const float val) const{
52  return {val+x,val+y};
53  }
54  inline vector_2d operator -(const vector_2d pt) const{
55  return {x-pt.x,y-pt.y};
56  }
57  inline vector_2d operator -(const float val) const{
58  return {x-val,y-val};
59  }
60 
63  void normalize() {
64  float length = 1/len() ;
65  x *= length;
66  y *= length;
67  }
68 
69 
72  void turnRight() {
73  float tmpx = x;
74  x = -y;
75  y = tmpx;
76  }
77 
78  void turnLeft() {
79  float tmpx = x;
80  x = y;
81  y = -tmpx;
82  }
83 
87  float len() {
88  return sqrt(x*x+y*y);
89  }
90 };
91 
92 inline std::vector<vector_2d> normals(std::vector<vector_2d> segments)
93 {
94  vector_2d tmpNormal;
95  vector_2d normal;
96  std::vector<vector_2d> out;
97  for(unsigned i=0; i<segments.size()-1; i++)
98  {
99  normal = segments.at(i+1)-segments.at(i);
100  normal.turnRight();
101  if (i==0)
102  tmpNormal = normal;
103  tmpNormal =tmpNormal + normal;
104  tmpNormal.normalize();
105  out.push_back( tmpNormal);
106 
107  tmpNormal = normal;
108  }
109  tmpNormal.normalize();
110  out.push_back( tmpNormal);
111  return out;
112 }
113 
114 inline std::vector<vector_2d> subd(vector_2d pt1, vector_2d pt2, float maxlen)
115 {
116  std::vector<vector_2d> out;
117  vector_2d tmpvec = pt2 - pt1;
118  float len = tmpvec.len();
119  if (!len) return out;
120  if (len<maxlen)
121  {
122  out.push_back( pt2);
123  return out;
124  }
125 
126  int nbsub = ceilf(len / maxlen);
127  len /= (float)nbsub;
128  tmpvec.normalize();
129  tmpvec = tmpvec*len;
130  for (;nbsub>0;nbsub--)
131  {
132  pt1 = pt1 + tmpvec;
133  out.push_back( pt1);
134  }
135  return out;
136 }
137 
138 
139 /* inline vector_2d operator *( const vector_2d vect, const float val) {
140  return {val*vect.x,val*vect.y};
141 }*/
142  inline vector_2d operator *( const float val, const vector_2d vect) {
143  return {val*vect.x,val*vect.y};
144 }
145  /*inline vector_2d operator -( const vector_2d vect1, const vector_2d vect2) {
146  return {vect1.x-vect2.x,vect1.y-vect2.y};
147 }*/
148  inline vector_2d operator -( const float val, const vector_2d vect) {
149  return {val-vect.x,val-vect.y};
150 }
151  inline vector_2d operator -( const vector_2d vect, const float val) {
152  return {vect.x-val , vect.y-val};
153 }
154 
155 static inline float minf(float a, float b) { return a < b ? a : b; }
156 static inline float maxf(float a, float b) { return a > b ? a : b; }
157 
158 #endif // OE_UTILS_H
float len()
parse the xml file to load params
Definition: OE_utils.h:87
void normalize()
normalise the vector (his length become 1)
Definition: OE_utils.h:63
void turnRight()
turn the vector 90° to the right
Definition: OE_utils.h:72
Definition: OE_utils.h:28