1 /*
2     This file is a part of the RepSnapper project.
3     Copyright (C) 2010  Kulitorum
4     Copyright (C) 2012  martin.dieringer@gmx.de
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 
21 #pragma once
22 #include "stdafx.h"
23 
24 #include <vector>
25 #include <list>
26 #include <iostream>
27 
28 #include "poly.h"
29 #include "gcode/gcodestate.h"
30 #include "printlines.h"
31 
32 #include <cairomm/cairomm.h>
33 
34 //
35 // A Layer containing and maintaining all polygons to be printed
36 //
37 class Layer
38 {
39 
40 public:
41   Layer();
42   Layer(Layer * previous, int layerno=-1, double thick=0., uint skins=1);
43 
44   ~Layer();
45 
46   int LayerNo;
47   double thickness;
48   double Z;
getZ()49   double getZ() const {return Z;}
setZ(double z)50   void setZ(double z){Z=z;}
setSkins(uint skins_)51   void setSkins(uint skins_){skins = skins_;}
52 
getPrevious()53   Layer * getPrevious() const {return previous;};
setPrevious(Layer * prevlayer)54   void setPrevious(Layer * prevlayer){previous = prevlayer;};
55 
getMin()56   Vector2d getMin() const {return Min;};
getMax()57   Vector2d getMax() const {return Max;};
58   bool setMinMax(const Poly &poly);
59   bool setMinMax(const vector<Poly> &polys);
60 
61   bool pointInPolygons(const Vector2d &p) const;
62 
63   Vector2d getRandomPolygonPoint() const;
64   Vector2d getFarthestPolygonPoint(const Vector2d &from) const;
65     /* void setBBox(Vector2d min, Vector2d max); */
66   /* void setBBox(vector<Vector2d> minmax); */
67   /* void setBBox(Vector3d min, Vector3d max); */
68 
69   // ClipperLib::Paths getClipperPolygons(const vector<Poly> polygons,
70   // 					  bool reverse=true) const;
71   vector<Poly> getMergedPolygons(const vector<Poly> &polys);
72   //ClipperLib::Paths getMergedPolygons(const ClipperLib::Paths cpolys) const;
73   void mergeFullPolygons(bool bridge);
74   void mergeSupportPolygons();
75   // vector<Poly> getFillPolygons(const vector<Poly> polys, long dist) const;
76 
77   void CalcInfill (const Settings &settings);
78   void CalcRaftInfill (const vector<Poly> &polys,
79 		       double extrusionfactor, double infilldistance,
80 		       double rotation);
81 
82   vector<double> getBridgeRotations(const vector<Poly> &poly) const;
83   void calcBridgeAngles(const Layer *layerbelow);
84 
85   static void FindThinpolys(const vector<Poly> &polys, double extrwidth,
86 			    vector<Poly> &thickpolys, vector<Poly> &thinpolys);
87 
88   void MakeShells(const Settings &settings);
89   // uint shellcount, double extrudedWidth, double shelloffset,
90   // bool makeskirt, double skirtdistance, double infilloverlap);
91   /* vector<Poly> ShrinkedPolys(const vector<Poly> poly, */
92   /* 			     double distance,  */
93   /* 			     ClipperLib::JoinType join_type = ClipperLib::jtMiter); */
94   void calcConvexHull();
95   void MakeSkirt(double distance, bool single=true);
96 
GetPolygons()97   vector<Poly> GetPolygons() const { return polygons; };
98   vector<ExPoly>  GetExPolygons() const;
99   void SetPolygons(vector<Poly> &polys) ;
100   /* void SetPolygons(const Matrix4d &T, const Shape &shape, double z); */
GetFillPolygons()101   vector<Poly> GetFillPolygons() const { return fillPolygons; }
GetFullFillPolygons()102   vector<Poly> GetFullFillPolygons() const { return fullFillPolygons; }
GetBridgePolygons()103   vector<ExPoly> GetBridgePolygons() const { return bridgePolygons; }
GetSkinFullPolygons()104   vector<Poly> GetSkinFullPolygons() const { return skinFullFillPolygons; }
GetSupportPolygons()105   vector<Poly> GetSupportPolygons() const { return supportPolygons; }
GetToSupportPolygons()106   vector<Poly> GetToSupportPolygons() const { return toSupportPolygons; }
GetDecorPolygons()107   vector<Poly> GetDecorPolygons() const { return decorPolygons; }
GetShellPolygons()108   vector< vector<Poly> >  GetShellPolygons() const {return shellPolygons; }
109   vector<Poly>  GetShellPolygonsCirc(int number) const;
GetSkirtPolygons()110   vector<Poly>  GetSkirtPolygons() const {return skirtPolygons; };
111   const vector<Poly> * GetInnerShell() const;
112   const vector<Poly> * GetOuterShell() const;
GetHullPolygon()113   Poly GetHullPolygon() const {return hullPolygon;};
114 
115   vector<Poly> getOverhangs() const;
116 
117 
118   void setFullFillPolygons(const vector<Poly> &polys);
119   void addFullFillPolygons(const vector<Poly> &polys);
120   void addFullPolygons(const vector<Poly> &fullpolys, bool decor=false);
121   void addFullPolygons(const vector<ExPoly> &expolys, bool decor=false);
122   void setBridgePolygons(const vector<ExPoly> &polys);
123   void addBridgePolygons(const vector<ExPoly> &polys);
124   void setBridgeAngles(const vector<double> &angles);
125   void makeSkinPolygons();
126   void setNormalFillPolygons(const vector<Poly> &polys);
127   void setSupportPolygons(const vector<Poly> &polys);
128   void setSkirtPolygons(const vector<Poly> &poly);
129   void setDecorPolygons(const vector<Poly> &polys);
130 
131   /* void getOrderedPrintLines(const vector<Poly> polys,  */
132   /* 			    Vector2d &startPoint, */
133   /* 			    vector<printline> &lines, */
134   /* 			    double linewidth,double linewidthratio,double optratio) const; */
135 
136 
137   void MakePrintlines (Vector3d &start,
138 		       vector<PLine3> &plines,
139 		       double offsetZ,
140 		       Settings &settings) const;
141 
142   void MakeGCode (Vector3d &start,
143 		  GCodeState &gc_state,
144 		  double offsetZ,
145 		  Settings &settings) const;
146 
147   string info() const ;
148 
149   void Draw(const Settings &settings);
150 
151   void DrawRulers(const Vector2d &point);
152 
153   void Clear();
154 
155   void addPolygons(vector<Poly> &polys);
156   void cleanupPolygons();
157   int addShape(const Matrix4d &T, const Shape &shape, double z,
158 	       double &max_gradient, double max_supportangle);
159 
160   double area() const;
161 
162   string SVGpath(const Vector2d &trans=Vector2d::ZERO) const;
163 
164  private:
165 
166   Layer * previous;
167 
168   Vector2d Min, Max;  // Bounding box
169 
170   Infill * normalInfill;
171   Infill * fullInfill;
172   Infill * skirtInfill;
173   vector<Infill*> bridgeInfills;   // an infill for every brigde (different angles)
174   vector<Infill*> skinFullInfills;
175   Infill * supportInfill;
176   Infill * decorInfill;
177   Infill * thinInfill;             // one-line infill for thin features
178 
179   vector<Poly> polygons;		// original polygons directly from model
180   vector< vector<Poly> > shellPolygons; // all shells except innermost
181   vector<Poly> thinPolygons;            // areas thinner than 2 extrusion lines
182   vector<Poly> fillPolygons;	        // innermost shell
183   vector<Poly> fullFillPolygons;        // fully filled polygons (uncovered)
184   vector<ExPoly> bridgePolygons;        // fully filled ex-polygons with holes for bridges
185   vector<double> bridge_angles;         // angles of each bridge ex-polygon
186   vector< vector<Poly> > bridgePillars; // bridge pillars for debugging
187   vector<Poly> supportPolygons;	        // polygons to be filled with support pattern
188   vector<Poly> toSupportPolygons;       // triangles that should be supported
189   uint skins;                           // number of skin divisions
190   vector<Poly> skinPolygons;            // outer skin polygons
191   vector<Poly> skinFullFillPolygons;    // skin polygons of fully filled areas
192   Poly hullPolygon;                     // convex hull around everything
193   vector<Poly> skirtPolygons;           // skirt polygon
194   vector<Poly> decorPolygons;           // decoration polygons
195 
196   // uses too much memory
197   /* Cairo::RefPtr<Cairo::ImageSurface> raster_surface; */
198   /* Cairo::RefPtr<Cairo::Context>      raster_context; */
199 
200 };
201