1 /***************************************************************************
2  *   Copyright (C) 2021 by Abderrahman Taha                                *
3  *                                                                         *
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     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor,Boston, MA 02110-1301 USA             *
19  ***************************************************************************/
20 #include "../fparser/fparser.hh"
21 #include <qthread.h>
22 #include <complex.h>
23 #include <tgmath.h>
24 
25 #define PI (double(314159265) / double(100000000))
26 #define MAGIC_SCALE 1.5707963f
27 extern template class FunctionParserBase<double>; // explicit instantiation
28 
29 struct GlobalParam
30 {
31     uint TypeDrawin = 10;
32     uint TypeDrawinNormStep = 4;
33     uint PreviousSizeMinimalTopology = 0;
34     uint NbPolyMinimalTopology = 0;
35     uint NbVertexTmp = 0;
36 };
37 
38 struct ImplicitStructure
39 {
40     std::string fxyz;
41     std::string xmin;
42     std::string xmax;
43     std::string ymin;
44     std::string ymax;
45     std::string zmin;
46     std::string zmax;
47     std::string grid;
48     std::string cnd;
49 };
50 
51 extern uint OrignbX, OrignbY, OrignbZ;
52 extern uint Stack_Factor;
53 
54 struct Voxel
55 {
56     uint Edge_Points[12]; // reference to the Edge Points
57     uint Signature;       // From 0 to 255
58     uint NbEdgePoint;
59 };
60 
61 struct NoiseParemeters
62 {
63     int Octaves=4;
64     float Lacunarity=0.5f;
65     float Gain=0.5f;
66     int NoiseType=1; // 0 :Texture ; 1 : Pigments
67     FunctionParser *RgbtParser;
68     FunctionParser *VRgbtParser, *GradientParser, *NoiseParser;
69     uint Nb_vrgbts;
70     int NoiseShape;
71     int NoiseActive;
72 };
73 
74 struct ErrorMessage
75 {
76     int iErrorIndex;
77     std::string strError;
78 };
79 
80 // Any modification to this enum must be translated to the same enum in
81 // fparser.hh
82 enum EvalErrorType
83 {
84     EVAL_ERROR = 0,
85     DIVISION_BY_ZERO,
86     COTAN_FUNCT_ERROR,
87     EXP_FUNCT_ERROR,
88     ARC_TRIGONOMETRIC_FUNCT_ERROR,
89     IF_FUNCT_ERROR,
90     EVAL_NO_ERROR,
91     VAR_OVERFLOW
92 };
93 
94 enum ScriptErrorType
95 {
96     SCRIPT_NO_ERROR = 0,
97     MAXGRID_OUT_OF_RANGE,
98     MAXPT_OUT_OF_RANGE,
99     MAXTRI_OUT_OF_RANGE,
100     FXYZ_OUT_OF_RANGE,
101     FX_OUT_OF_RANGE,
102     XMAX_NBCOMPONENT_MISMATCH,
103     YMAX_NBCOMPONENT_MISMATCH,
104     ZMAX_NBCOMPONENT_MISMATCH,
105     XMIN_NBCOMPONENT_MISMATCH,
106     YMIN_NBCOMPONENT_MISMATCH,
107     ZMIN_NBCOMPONENT_MISMATCH,
108     FY_FX_MISMATCH,
109     FZ_FX_MISMATCH,
110     FW_FX_MISMATCH,
111     UMAX_NBCOMPONENT_MISMATCH,
112     VMAX_NBCOMPONENT_MISMATCH,
113     WMAX_NBCOMPONENT_MISMATCH,
114     UMIN_NBCOMPONENT_MISMATCH,
115     VMIN_NBCOMPONENT_MISMATCH,
116     WMIN_NBCOMPONENT_MISMATCH,
117     GRID_NBCOMPONENT_MISMATCH,
118     COMPONENT_NBCOMPONENT_MISMATCH,
119     CND_NBCOMPONENT_MISMATCH,
120     NBSLIDERS_OUT_OF_RANGE,
121     NBSLIDERSVALUES_OUT_OF_RANGE,
122     VERTEX_TAB_MEM_OVERFLOW,
123     TRIANGLES_TAB_MEM_OVERFLOW,
124     MINPOLY_TAB_MEM_OVERFLOW,
125     POLY_TAB_MEM_OVERFLOW,
126     CND_TAB_MEM_OVERFLOW,
127     CND_POL_MEM_OVERFLOW,
128     DEFINED_PARAM_GRID_VERTEX_TAB_OVERFLOW,
129     DEFINED_PARAM_GRID_TRIANGLE_TAB_OVERFLOW,
130     GRID_SUPERIOR_TO_GRIDMAX,
131     VECT_DIMENSION_ERROR,
132     MEM_OVERFLOW,
133     EMPTY_MANDATORY_FIELD
134 };
135 struct ComponentInfos
136 {
137     std::vector<uint> ParisoTriangle;
138     std::vector<uint> ParisoVertex;
139     std::vector<uint> NbComponentsType;
140 
141     std::vector<bool> ThereisCND;
142     std::vector<bool> ThereisRGBA;
143     std::vector<bool> ParisoCondition;
144     std::vector<bool> ShowParIsoCmp;
145     uint ParisoNbComponents = 1;
146     uint ParisoCurrentComponentIndex = 0;
147     uint NbParametricMeshLines=0;
148     uint MinParametricMeshLines=0;
149     std::vector<uint> ParametricGrid;
150 
151     std::vector<uint> NbTrianglesVerifyCND;
152     std::vector<uint> NbTrianglesNoCND;
153     std::vector<uint> NbTrianglesNotVerifyCND;
154     std::vector<uint> NbTrianglesBorderCND;
155 
156     NoiseParemeters NoiseParam[2];
157     bool Interleave = false;
158     bool pariso = false;
159     bool updateviewer = false;
160     std::vector<bool> hsv;
161 };
162 
163 struct ObjectProperties
164 {
165     float *ArrayNorVer_localPt;
166     uint *PolyIndices_localPt;
167     uint *PolyIndices_localPtMin;
168     int *TypeSurfaceTriangleListeCND;
169     uint NbPolygnNbVertexPtMin = 0;
170     uint NbPolygnNbVertexPtMinSize = 0;
171     uint PolyNumber = 0;
172     uint VertxNumber = 0;
173     uint NbBorderpoints;
174     struct ComponentInfos componentsinfos;
175     int IndexCurrentFormula = -1;
176     bool ShininessValUpdated=true;
177     bool AmbientValUpdated=true;
178     bool DiffuseValUpdated=true;
179     bool FrontSurfValUpdated=false;
180     int border = 1;
181     int anim = -1;
182     int animxyz = 1;
183     int animx = -1;
184     int animy = -1;
185     int animz = -1;
186     float animxValue = 0.0;
187     float animyValue = 0.0;
188     float animzValue = 0.0;
189     float animxValueStep = 0.0;
190     float animyValueStep = 0.0;
191     float animzValueStep = 0.0;
192 
193     int glFrontFacingSupport = 1;
194     int slider = -1;
195     int morph = -1;
196     int morphstep = -1;
197     int typedrawing = 1;
198     int mesh = 1;
199     int fill = 1;
200     int transparency = -1;
201     int boundingbox = -1;
202     bool activarecnd = true;
203     bool updategl = true;
204     int triangles = -1;
205     int infos = -1;
206     int frame = -1;
207     int infosdetails[3]={1, 1, 1};
208     int line = 1;
209     int norm = -1;
210     int plan = 1;
211     int axe = 1;
212     int attachwininfos = 1;
213     int png_ok =  1;
214     int jpg_ok = -1;
215     int bmp_ok = -1;
216     int quality_image = 50;
217     int colortype = 0;
218     int colortypeParam = 0;
219     float specReflection[4]={0.2f, 0.2f, 0.2f, 1.0f};
220     int threads[3];
221     int shininess = 110;
222     float frontcol[4]={0.9f, 0.5f, 0.1f, 1.0f};
223     float backcol[4] ={0.2f, 0.8f, 0.1f, 1.0f};
224     float frontcols[40]={0.72f, 0.5f, 0.1f, 1.0f, 0.3f, 0.8f, 0.5f, 1.0f, 0.2f, 0.5f, 0.6f, 1.0f, 0.1f, 0.3f, 0.9f, 1.0f, 0.96f, 0.71f, 0.3f, 1.0f,
225                           0.4f, 0.4f, 0.9f, 1.0f, 0.99f, 0.1f, 0.843f, 1.0f, 0.16f, 0.91f, 0.73f, 1.0f, 0.8f, 0.9f, 0.0f, 1.0f, 0.9f, 0.91f, 0.3f, 1.0f};
226     float backcols[40]={0.4f, 0.6f, 0.1f, 1.0f, 0.6f, 0.6f, 0.9f, 1.0f, 0.8f, 0.6f, 0.3f, 1.0f, 0.9f, 0.8f, 0.6f, 1.0f, 0.93f, 0.2f, 0.1f, 1.0f,
227                         0.3f, 0.72f, 0.63f, 1.0f, 0.1f, 0.5f, 0.73f, 1.0f, 0.9f, 0.5f, 0.3f, 1.0f, 0.8f, 0.8f, 0.6f, 1.0f, 0.7f, 0.9f, 0.93f, 1.0f};
228     float gridcol[4]  ={0.25f, 0.2f, 0.0f, 0.08f};
229     float groundcol[4]={0.0f, 0.0f, 0.0f, 1.0f};
230     float polyfactor= 1.0;
231     float polyunits= 1.0;
232     float RotStrength=0;
233     float oldRotx, oldRoty;
234     float zminim= -350.0f;
235     float BoxLimits[3][6];
236     int viewport[4];
237     double matrix[16];
238     double matrixInverse[16];
239     double ScalCoeff = 1.0;
240     double axe_x, axe_y, axe_z;
241     double view_rotx = 90.0, view_roty = 90.0, view_rotz = 1.0;
242     bool cndoptions[5] = {true, true, true, false, false};
243     uint vboId_ArrayNorVer_localPt;
244     uint vboId_PolyIndices_localPt;
245 };
246 
247 class CellNoise
248 {
249 public:
250     float rd[3], featurePoint[4];
251     const static uint OFFSET_BASIS = 2166136261U;
252     const static uint FNV_PRIME = 16777619U;
253 
254     float CellNoiseFunc(float, float, float, int, int type = 2, int Dist = 0);
255     float EuclidianDistanceFunc(float, float, float, float, float, float);
256     float ManhattanDistanceFunc(float, float, float, float, float, float);
257     float ChebyshevDistanceFunc(float, float, float, float, float, float);
258     int probLookup(uint);
259     void insert(float *, float);
260     int lcgRandom(int);
261     int hash(int, int, int);
262 };
263 
264 class ImprovedNoise
265 {
266 public:
267     int passes;
268     float correction;
269     ImprovedNoise(float, float, float);
270     float noise(float, float, float);
271     float fade(float);
272     float lerp(float, float, float);
273     float grad(int, float, float, float);
274     float FractalNoise3D(float, float, float, int, float, float);
275     float Marble(float, float, float, int);
276     float lookup(float, float, float);
277 };
278 
279 extern std::vector<float> NormVertexTabVector;
280 extern std::vector<uint> IndexPolyTabMinVector;
281 extern std::vector<uint> IndexPolyTabMinVector2;
282 extern std::vector<uint> IndexPolyTabVector;
283 static struct ComponentInfos *components = new (struct ComponentInfos);
284