1 /* 2 This file is a part of the RepSnapper project. 3 Copyright (C) 2011-12 martin.dieringer@gmx.de 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with this program; if not, write to the Free Software Foundation, Inc., 17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 */ 19 20 #pragma once 21 22 #include "stdafx.h" 23 #include "geometry.h" 24 25 class Poly 26 { 27 double z; 28 double extrusionfactor; 29 30 // vector<Poly*> holes; 31 mutable bool holecalculated; 32 mutable bool hole; // this polygon is a hole 33 bool closed; 34 35 public: 36 Poly(); 37 Poly(double z, double extrusionfactor=1.); 38 Poly(const Poly &p, double z); 39 /* Poly(double z, */ 40 /* const ClipperLib::Path cpoly, bool reverse=false); */ 41 ~Poly(); 42 setClosed(bool c)43 void setClosed(bool c) { closed = c; }; isClosed()44 bool isClosed() const { return closed; }; 45 46 Vector2d operator[](int i) const { 47 if (i >= 0 && i < (int)vertices.size()) 48 return vertices[i]; 49 else return vertices[(vertices.size()+i)%vertices.size()]; 50 }; 51 nextVertex(uint i)52 uint nextVertex(uint i) const {return (i+1)%vertices.size();}; 53 54 /* Poly Shrinked(double distance) const; */ 55 /* Poly Shrinked(vector<Vector2d> *vertices, double distance); */ 56 57 // simplify douglas-peucker 58 void cleanup(double maxerror); 59 reverse()60 void reverse() {std::reverse(vertices.begin(),vertices.end());holecalculated = false;}; 61 clear()62 void clear(){vertices.clear(); holecalculated = false;}; 63 64 void transform(const Matrix4d &T); 65 void transform(const Matrix3d &T); 66 void mirrorX(const Vector3d ¢er); 67 68 //vector< vector<Vector2d> > intersect(Poly &poly1, Poly &poly2) const; 69 70 bool vertexInside(const Vector2d &point, double maxoffset=0.0001) const; 71 bool vertexInside2(const Vector2d &point, double maxoffset=0.0001) const; 72 bool isInside(const Poly &poly, double maxoffset=0.0001) const; 73 uint nearestDistanceSqTo(const Vector2d &p, double &mindist) const; 74 void nearestIndices(const Poly &p2, int &thisindex, int &otherindex) const; 75 double shortestConnectionSq(const Poly &p2, Vector2d &start, Vector2d &end) const; 76 double angleAtVertex(uint i) const; 77 vector<Vector2d> getCenterline() const; 78 79 uint getFarthestIndex(uint &thisindex) const; 80 uint getFarthestIndex(const Vector2d &from) const; 81 82 void rotate(const Vector2d ¢er, double angle); 83 void move(const Vector2d &delta); 84 85 void calcHole() const; // calc center and whether this is a hole 86 bool isHole() const; 87 88 vector<Vector2d> getMinMax() const; 89 vector<Intersection> lineIntersections(const Vector2d &P1, const Vector2d &P2, 90 double maxerr=0.0001) const; 91 92 // ClipperLib::Paths getOffsetClipperPolygons(double dist) const ; 93 // ClipperLib::Path getClipperPolygon(bool reverse=false) const; 94 95 Vector2d const &getVertexCircular(int pointindex) const; // 2d point at index 96 Vector3d getVertexCircular3(int pointindex) const; // 3d point at index 97 vector<Vector2d> getVertexRangeCircular(int from, int to) const; 98 99 vector<Vector2d> vertices; // vertices 100 void addVertex(const Vector2d &v, bool front=false); 101 void addVertexUnique(const Vector2d &v, bool front=false); 102 void addVertex(double x, double y, bool front=false); 103 void addVertexUnique(double x, double y, bool front=false); 104 105 106 mutable Vector2d center; 107 Vector2d getCenter() const; getZ()108 double getZ() const {return z;} setZ(double z)109 void setZ(double z) {this->z = z;}; getExtrusionFactor()110 double getExtrusionFactor() const{return extrusionfactor;}; setExtrusionFactor(double e)111 void setExtrusionFactor(double e){extrusionfactor = e;}; 112 double getLayerNo() const; 113 114 void draw(int gl_type, bool randomized=true) const; 115 void draw(int gl_type, double z, bool randomized=true) const; // draw at given z 116 void drawVertexNumbers() const; 117 void drawVertexAngles() const; 118 void drawLineNumbers() const; 119 void draw_as_surface() const; 120 121 void makeLines(vector<Vector2d> &lines, Vector2d &startPoint) const; 122 void makeLines(vector<Vector3d> &lines, Vector2d &startPoint) const; 123 void makeLines(vector<Vector2d> &lines,uint startindex=0) const; 124 void makeLines(vector<Vector3d> &lines,uint startindex=0) const; 125 126 double getLinelengthSq(uint startindex) const; 127 double averageLinelengthSq() const; 128 double totalLineLength() const; 129 130 vector<Vector2d> getPathAround(const Vector2d &from, const Vector2d &to) const; 131 132 int getTriangulation(vector<Triangle> &triangles) const ; 133 size()134 uint size() const {return vertices.size(); }; front()135 Vector2d front() {return vertices.front(); }; back()136 Vector2d back() {return vertices.back(); }; push_back(Vector2d v)137 void push_back (Vector2d v) { 138 vertices.push_back(v); holecalculated = false;}; push_front(Vector2d v)139 void push_front(Vector2d v) { 140 vertices.insert(vertices.begin(),v); holecalculated = false;}; 141 142 string info() const; 143 144 string SVGpolygon(string style="fill: black") const; 145 string SVGpath(const Vector2d &trans=Vector2d::ZERO) const; 146 147 string gnuplot_path(const Vector2d &trans=Vector2d::ZERO) const; // for debugging 148 149 150 static void move(vector<Poly> &polys, const Vector2d &trans); 151 152 }; 153 154 155 //////////////////////////////////////////////////////////////////// 156 157 class ExPoly 158 { 159 public: ExPoly()160 ExPoly(){}; ~ExPoly()161 ~ExPoly(){}; 162 Poly outer; 163 vector<Poly> holes; 164 165 void clear(); getZ()166 double getZ() const {return outer.getZ();} 167 168 void draw(int gl_type, bool randomized=false) const; 169 void draw(int gl_type, double z, bool randomized=false) const; // draw at given z 170 void drawVertexNumbers() const; 171 void drawLineNumbers() const; 172 173 void cleanup(double maxerror); 174 175 }; 176 177 178 //////////////////////////////////////////////////////////////////// 179 180 void draw_poly (const Poly &poly, int gl_type, int linewidth, int pointsize, 181 const float *rgb, float a, bool randomized = false); 182 void draw_polys(const vector <Poly> &polys, int gl_type, int linewidth, int pointsize, 183 const float *rgb, float a, bool randomized = false); 184 void draw_polys(const vector< vector <Poly> > &polys, 185 int gl_type, int linewidth, int pointsize, 186 const float *rgb, float a, bool randomized = false); 187 void draw_poly (const ExPoly &expoly, int gl_type, int linewidth, int pointsize, 188 const float *rgb, float a, bool randomized = false); 189 void draw_polys(const vector <ExPoly> &expolys, int gl_type, int linewidth, int pointsize, 190 const float *rgb, float a, bool randomized = false); 191 192 void draw_polys_surface(const vector <Poly> &polys, 193 const Vector2d &Min, const Vector2d &Max, 194 double z, 195 double cleandist, 196 const float *rgb, float a); 197 void draw_polys_surface(const vector< vector<Poly> > &polys, 198 const Vector2d &Min, const Vector2d &Max, 199 double z, 200 double cleandist, 201 const float *rgb, float a); 202 void draw_polys_surface(const vector <ExPoly> &expolys, 203 const Vector2d &Min, const Vector2d &Max, 204 double z, 205 double cleandist, 206 const float *rgb, float a); 207 208 void clearpolys(vector<Poly> &polys); 209 void clearpolys(vector<ExPoly> &polys); 210 void clearpolys(vector< vector<Poly> > &polys); 211