1 /*
2     This file is a part of the RepSnapper project.
3     Copyright (C) 2010  Kulitorum
4     Copyright (C) 2011-12  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 
23 #include <vector>
24 #include <list>
25 #include <iostream>
26 #include <fstream>
27 #include <string>
28 #include <sstream>
29 #include <limits>
30 #include <algorithm>
31 #include "stdafx.h"
32 #include "transform3d.h"
33 //#include "settings.h"
34 #include "triangle.h"
35 #include "slicer/geometry.h"
36 #include "poly.h"
37 
38 //#define ABS(a)	   (((a) < 0) ? -(a) : (a))
39 
40 
41 struct Segment {
SegmentSegment42   Segment(guint s, guint e) { start = s; end = e; }
43   int start;		// Vertex index of start point
44     int end;		// Vertex index of end point
SwapSegment45   void Swap() {
46     int tmp = start;
47     start = end;
48     end = tmp;
49   }
50 };
51 
52 
53 
54 #define sqr(x) ((x)*(x))
55 
56 
57 class Shape
58 {
59 public:
dimensions()60   virtual short dimensions(){return 3;};
61 
62 	Shape();
63 	/* Shape(string filename, istream &text); */
~Shape()64 	virtual ~Shape(){};
65 	Glib::ustring filename;
66 	int idx;
67 
68 	int parseASCIISTL(istream &text, uint max_triangles=0, bool readnormals=false);
69 
70 	Transform3D transform3D;
71 
72 	virtual void clear();
73 	/* void displayInfillOld(const Settings &settings, CuttingPlane &plane,  */
74 	/* 		      guint LayerNr, vector<int>& altInfillLayers); */
75 	void draw (const Settings &settings,
76 		   bool highlight=false, uint max_triangles=0);
77 	virtual void draw_geometry (uint max_triangles=0);
78 	void drawBBox() const;
79 	virtual bool getPolygonsAtZ(const Matrix4d &T, double z,
80 				    vector<Poly> &polys,
81 				    double &max_gradient,
82 				    vector<Poly> &supportpolys,
83 				    double max_supportangle,
84 				    double thickness = -1) const;
85 	// Extract a 2D polygonset from a 3D model:
86 	// void CalcLayer(const Matrix4d &T, CuttingPlane *plane) const;
87 
88     virtual vector<Vector3d> getMostUsedNormals() const;
89 	// Auto-Rotate object to have the largest area surface down for printing:
90     virtual void OptimizeRotation();
91     virtual void CalcBBox();
92 	// Rotation for manual rotate and used by OptimizeRotation:
93     virtual void Rotate(const Vector3d & axis, const double &angle);
94 	void Twist(double angle);
95 
move(Vector3d delta)96 	virtual void move(Vector3d delta){ transform3D.move(delta); };
97 
98 	void Scale(double scale_factor, bool calcbbox = true);
99 	void ScaleX(double scale_factor);
100 	void ScaleY(double scale_factor);
101 	virtual void ScaleZ(double scale_factor);
getScaleFactor()102 	double getScaleFactor() { return transform3D.get_scale(); };
getScaleFactorX()103 	double getScaleFactorX(){ return transform3D.get_scale_x(); };
getScaleFactorY()104 	double getScaleFactorY(){ return transform3D.get_scale_y(); };
getScaleFactorZ()105 	virtual double getScaleFactorZ(){ return transform3D.get_scale_z(); };
106 
107 
108 	void FitToVolume(const Vector3d &vol);
109 
110     void PlaceOnPlatform();
111 
112     Vector3d Min, Max, Center;
113 
114 
t_Min()115     Vector3d t_Min() const {return transform3D.transform * Min;}
t_Max()116     Vector3d t_Max() const {return transform3D.transform * Max;}
t_Center()117     Vector3d t_Center() const {return transform3D.transform * Center;}
118 
119     Vector3d scaledCenter() const;
120 
121     /* Poly getOutline(const Matrix4d &T, double maxlen) const;*/
122     vector<Triangle> trianglesSteeperThan(double angle) const;
123 
124     string getSTLsolid() const;
125     double volume() const;
126 
127     void invertNormals();
128     void repairNormals(double sqdistance);
129     virtual void mirror();
130     void makeHollow(double wallthickness);
131     virtual void splitshapes(vector<Shape*> &shapes, ViewProgress *progress=NULL);
132     int divideAtZ(double z, Shape *upper, Shape *lower, const Matrix4d &T) const;
133 
134 
135     int saveBinarySTL(Glib::ustring filename) const;
136 
137 
138     bool slow_drawing;
139     virtual string info() const;
140 
141     vector<Triangle> getTriangles(const Matrix4d &T=Matrix4d::IDENTITY) const;
142     void addTriangles(const vector<Triangle> &tr);
143 
144     void setTriangles(const vector<Triangle> &triangles_);
145 
size()146     uint size() const {return triangles.size();}
147 
148 protected:
149 
150     int gl_List;
151 
152 private:
153 
154     vector<Triangle> triangles;
155     //vector<Polygon2d>  polygons;  // surface polygons instead of triangles
156     void calcPolygons();
157 
158     // returns maximum gradient
159     vector<Segment> getCutlines(const Matrix4d &T, double z,
160 				vector<Vector2d> &vertices, double &max_grad,
161 				vector<Triangle> &support_triangles,
162 				double supportangle,
163 				double thickness) const;
164 
165     bool hasAdjacentTriangleTo(const Triangle &triangle,
166 			       double sqdistance = 0.05) const;
167 };
168 
169 
170 
171 bool CleanupConnectSegments(const vector<Vector2d> &vertices, vector<Segment> &lines,
172 			    bool connect_all=false);
173 bool CleanupSharedSegments(vector<Segment> &lines);
174 bool CleanupStraightLines(const vector<Vector2d> &vertices, vector<Segment> &lines);
175 
176