1 From 853a0a438422d61cf1b9756d6949e04e5270d495 Mon Sep 17 00:00:00 2001
2 From: azv <azv@opencascade.com>
3 Date: Thu, 2 Aug 2018 11:32:16 +0300
4 Subject: [PATCH] Update PlaneGCS solver for SHAPER needs
6 * Add possibility to compile PlaneGCS as a shared library
7 * Possibility to avoid memory clearance inside the solver (make possible external memory management)
9 src/Mod/Sketcher/App/planegcs/Constraints.h | 48 ++++++++---------
10 src/Mod/Sketcher/App/planegcs/GCS.cpp | 81 +++++++++++++++++++++++------
11 src/Mod/Sketcher/App/planegcs/GCS.h | 8 +--
12 src/Mod/Sketcher/App/planegcs/Geo.h | 34 ++++++------
13 src/Mod/Sketcher/App/planegcs/Util.h | 10 ++++
14 5 files changed, 123 insertions(+), 58 deletions(-)
16 diff --git a/src/Mod/Sketcher/App/planegcs/Constraints.h b/src/Mod/Sketcher/App/planegcs/Constraints.h
17 index 559f3a2..76e4763 100644
18 --- a/src/Mod/Sketcher/App/planegcs/Constraints.h
19 +++ b/src/Mod/Sketcher/App/planegcs/Constraints.h
20 @@ -91,7 +91,7 @@ namespace GCS
21 HyperbolaNegativeMinorY = 17
25 + class PLANEGCS_EXPORT Constraint
27 _PROTECTED_UNLESS_EXTRACT_MODE_:
28 VEC_pD origpvec; // is used only as a reference for redirecting and reverting pvec
29 @@ -128,7 +128,7 @@ namespace GCS
33 - class ConstraintEqual : public Constraint
34 + class PLANEGCS_EXPORT ConstraintEqual : public Constraint
37 inline double* param1() { return pvec[0]; }
38 @@ -142,7 +142,7 @@ namespace GCS
42 - class ConstraintDifference : public Constraint
43 + class PLANEGCS_EXPORT ConstraintDifference : public Constraint
46 inline double* param1() { return pvec[0]; }
47 @@ -157,7 +157,7 @@ namespace GCS
51 - class ConstraintP2PDistance : public Constraint
52 + class PLANEGCS_EXPORT ConstraintP2PDistance : public Constraint
55 inline double* p1x() { return pvec[0]; }
56 @@ -178,7 +178,7 @@ namespace GCS
60 - class ConstraintP2PAngle : public Constraint
61 + class PLANEGCS_EXPORT ConstraintP2PAngle : public Constraint
64 inline double* p1x() { return pvec[0]; }
65 @@ -200,7 +200,7 @@ namespace GCS
69 - class ConstraintP2LDistance : public Constraint
70 + class PLANEGCS_EXPORT ConstraintP2LDistance : public Constraint
73 inline double* p0x() { return pvec[0]; }
74 @@ -224,7 +224,7 @@ namespace GCS
78 - class ConstraintPointOnLine : public Constraint
79 + class PLANEGCS_EXPORT ConstraintPointOnLine : public Constraint
82 inline double* p0x() { return pvec[0]; }
83 @@ -246,7 +246,7 @@ namespace GCS
86 // PointOnPerpBisector
87 - class ConstraintPointOnPerpBisector : public Constraint
88 + class PLANEGCS_EXPORT ConstraintPointOnPerpBisector : public Constraint
91 inline double* p0x() { return pvec[0]; }
92 @@ -268,7 +268,7 @@ namespace GCS
96 - class ConstraintParallel : public Constraint
97 + class PLANEGCS_EXPORT ConstraintParallel : public Constraint
100 inline double* l1p1x() { return pvec[0]; }
101 @@ -291,7 +291,7 @@ namespace GCS
105 - class ConstraintPerpendicular : public Constraint
106 + class PLANEGCS_EXPORT ConstraintPerpendicular : public Constraint
109 inline double* l1p1x() { return pvec[0]; }
110 @@ -315,7 +315,7 @@ namespace GCS
114 - class ConstraintL2LAngle : public Constraint
115 + class PLANEGCS_EXPORT ConstraintL2LAngle : public Constraint
118 inline double* l1p1x() { return pvec[0]; }
119 @@ -342,7 +342,7 @@ namespace GCS
123 - class ConstraintMidpointOnLine : public Constraint
124 + class PLANEGCS_EXPORT ConstraintMidpointOnLine : public Constraint
127 inline double* l1p1x() { return pvec[0]; }
128 @@ -366,7 +366,7 @@ namespace GCS
132 - class ConstraintTangentCircumf : public Constraint
133 + class PLANEGCS_EXPORT ConstraintTangentCircumf : public Constraint
136 inline double* c1x() { return pvec[0]; }
137 @@ -389,7 +389,7 @@ namespace GCS
138 virtual double grad(double *);
141 - class ConstraintPointOnEllipse : public Constraint
142 + class PLANEGCS_EXPORT ConstraintPointOnEllipse : public Constraint
145 inline double* p1x() { return pvec[0]; }
146 @@ -411,7 +411,7 @@ namespace GCS
147 virtual double grad(double *);
150 - class ConstraintEllipseTangentLine : public Constraint
151 + class PLANEGCS_EXPORT ConstraintEllipseTangentLine : public Constraint
155 @@ -426,7 +426,7 @@ namespace GCS
156 virtual double grad(double *);
159 - class ConstraintInternalAlignmentPoint2Ellipse : public Constraint
160 + class PLANEGCS_EXPORT ConstraintInternalAlignmentPoint2Ellipse : public Constraint
163 ConstraintInternalAlignmentPoint2Ellipse(Ellipse &e, Point &p1, InternalAlignmentType alignmentType);
164 @@ -442,7 +442,7 @@ namespace GCS
165 InternalAlignmentType AlignmentType;
168 - class ConstraintInternalAlignmentPoint2Hyperbola : public Constraint
169 + class PLANEGCS_EXPORT ConstraintInternalAlignmentPoint2Hyperbola : public Constraint
172 ConstraintInternalAlignmentPoint2Hyperbola(Hyperbola &e, Point &p1, InternalAlignmentType alignmentType);
173 @@ -458,7 +458,7 @@ namespace GCS
174 InternalAlignmentType AlignmentType;
177 - class ConstraintEqualMajorAxesConic : public Constraint
178 + class PLANEGCS_EXPORT ConstraintEqualMajorAxesConic : public Constraint
181 MajorRadiusConic * e1;
182 @@ -473,7 +473,7 @@ namespace GCS
183 virtual double grad(double *);
186 - class ConstraintEqualFocalDistance : public Constraint
187 + class PLANEGCS_EXPORT ConstraintEqualFocalDistance : public Constraint
191 @@ -488,7 +488,7 @@ namespace GCS
192 virtual double grad(double *);
195 - class ConstraintCurveValue : public Constraint
196 + class PLANEGCS_EXPORT ConstraintCurveValue : public Constraint
199 inline double* pcoord() { return pvec[2]; } //defines, which coordinate of point is being constrained by this constraint
200 @@ -515,7 +515,7 @@ namespace GCS
204 - class ConstraintPointOnHyperbola : public Constraint
205 + class PLANEGCS_EXPORT ConstraintPointOnHyperbola : public Constraint
208 inline double* p1x() { return pvec[0]; }
209 @@ -538,7 +538,7 @@ namespace GCS
213 - class ConstraintPointOnParabola : public Constraint
214 + class PLANEGCS_EXPORT ConstraintPointOnParabola : public Constraint
217 void errorgrad(double* err, double* grad, double *param); //error and gradient combined. Values are returned through pointers.
218 @@ -558,7 +558,7 @@ namespace GCS
219 virtual double grad(double *);
222 - class ConstraintAngleViaPoint : public Constraint
223 + class PLANEGCS_EXPORT ConstraintAngleViaPoint : public Constraint
226 inline double* angle() { return pvec[0]; };
227 @@ -583,7 +583,7 @@ namespace GCS
228 virtual double grad(double *);
231 - class ConstraintSnell : public Constraint //snell's law angles constrainer. Point needs to lie on all three curves to be constraied.
232 + class PLANEGCS_EXPORT ConstraintSnell : public Constraint //snell's law angles constrainer. Point needs to lie on all three curves to be constraied.
235 inline double* n1() { return pvec[0]; };
236 diff --git a/src/Mod/Sketcher/App/planegcs/GCS.cpp b/src/Mod/Sketcher/App/planegcs/GCS.cpp
237 index 474024a..9103653 100644
238 --- a/src/Mod/Sketcher/App/planegcs/GCS.cpp
239 +++ b/src/Mod/Sketcher/App/planegcs/GCS.cpp
240 @@ -101,11 +101,56 @@
243 #include <FCConfig.h>
244 +#ifndef _GCS_USE_STL_OUTPUT
245 #include <Base/Console.h>
248 #include <boost/graph/adjacency_list.hpp>
249 #include <boost/graph/connected_components.hpp>
253 + void Log(const char* message)
255 +#ifdef _GCS_USE_STL_OUTPUT
256 + std::cout << message << std::endl;
258 + Base::Console().Log(message);
262 + void Log(const char* format, const char* param)
264 +#ifdef _GCS_USE_STL_OUTPUT
265 + static char message[4096];
266 + sprintf(message, format, param);
267 + std::cout << message << std::endl;
269 + Base::Console().Log(format, param);
273 + void Log(const char* format, const size_t param)
275 +#ifdef _GCS_USE_STL_OUTPUT
276 + static char message[4096];
277 + sprintf(message, format, param);
278 + std::cout << message << std::endl;
280 + Base::Console().Log(format, param);
284 + void Warning(const char* message)
286 +#ifdef _GCS_USE_STL_OUTPUT
287 + std::cout << "Warning: " << message << std::endl;
289 + Base::Console().Warning(message);
294 typedef Eigen::FullPivHouseholderQR<Eigen::MatrixXd>::IntDiagSizeVectorType MatrixIndexType;
297 @@ -130,7 +175,7 @@ void LogMatrix(std::string str, Eigen::MatrixXd matrix )
299 const std::string tmp = stream.str();
301 - Base::Console().Log(tmp.c_str());
302 + GCS::Log(tmp.c_str());
306 @@ -155,7 +200,7 @@ void LogMatrix(std::string str, MatrixIndexType matrix )
308 const std::string tmp = stream.str();
310 - Base::Console().Log(tmp.c_str());
311 + GCS::Log(tmp.c_str());
315 @@ -178,7 +223,7 @@ void LogString(std::string str)
317 const std::string tmp = stream.str();
319 - Base::Console().Log(tmp.c_str());
320 + GCS::Log(tmp.c_str());
324 @@ -429,7 +474,11 @@ void System::clear()
328 +#ifdef _GCS_DO_NOT_FREE_CONSTRAINTS_MEMORY
336 @@ -486,9 +535,11 @@ void System::removeConstraint(Constraint *constr)
340 +#ifndef _GCS_DO_NOT_FREE_CONSTRAINTS_MEMORY
341 std::vector<Constraint *> constrvec;
342 constrvec.push_back(constr);
348 @@ -1477,7 +1528,7 @@ int System::solve_BFGS(SubSystem *subsys, bool /*isFine*/, bool isRedundantsolvi
349 << ", maxIter: " << maxIterNumber << "\n";
351 const std::string tmp = stream.str();
352 - Base::Console().Log(tmp.c_str());
353 + GCS::Log(tmp.c_str());
356 double divergingLim = 1e6*err + 1e12;
357 @@ -1493,7 +1544,7 @@ int System::solve_BFGS(SubSystem *subsys, bool /*isFine*/, bool isRedundantsolvi
358 << ", h_norm: " << h_norm << "\n";
360 const std::string tmp = stream.str();
361 - Base::Console().Log(tmp.c_str());
362 + GCS::Log(tmp.c_str());
366 @@ -1505,7 +1556,7 @@ int System::solve_BFGS(SubSystem *subsys, bool /*isFine*/, bool isRedundantsolvi
367 << ", divergingLim: " << divergingLim << "\n";
369 const std::string tmp = stream.str();
370 - Base::Console().Log(tmp.c_str());
371 + GCS::Log(tmp.c_str());
375 @@ -1542,7 +1593,7 @@ int System::solve_BFGS(SubSystem *subsys, bool /*isFine*/, bool isRedundantsolvi
376 << ", h_norm: " << h_norm << "\n";
378 const std::string tmp = stream.str();
379 - Base::Console().Log(tmp.c_str());
380 + GCS::Log(tmp.c_str());
384 @@ -1598,7 +1649,7 @@ int System::solve_LM(SubSystem* subsys, bool isRedundantsolving)
385 << ", maxIter: " << maxIterNumber << "\n";
387 const std::string tmp = stream.str();
388 - Base::Console().Log(tmp.c_str());
389 + GCS::Log(tmp.c_str());
393 @@ -1712,7 +1763,7 @@ int System::solve_LM(SubSystem* subsys, bool isRedundantsolving)
394 << ", h_norm: " << h_norm << "\n";
396 const std::string tmp = stream.str();
397 - Base::Console().Log(tmp.c_str());
398 + GCS::Log(tmp.c_str());
402 @@ -1757,7 +1808,7 @@ int System::solve_DL(SubSystem* subsys, bool isRedundantsolving)
403 << ", maxIter: " << maxIterNumber << "\n";
405 const std::string tmp = stream.str();
406 - Base::Console().Log(tmp.c_str());
407 + GCS::Log(tmp.c_str());
410 Eigen::VectorXd x(xsize), x_new(xsize);
411 @@ -1912,7 +1963,7 @@ int System::solve_DL(SubSystem* subsys, bool isRedundantsolving)
412 << ", err(divergingLim): " << err << "\n";
414 const std::string tmp = stream.str();
415 - Base::Console().Log(tmp.c_str());
416 + GCS::Log(tmp.c_str());
419 // count this iteration and start again
420 @@ -1926,7 +1977,7 @@ int System::solve_DL(SubSystem* subsys, bool isRedundantsolving)
421 stream << "DL: stopcode: " << stop << ((stop == 1) ? ", Success" : ", Failed") << "\n";
423 const std::string tmp = stream.str();
424 - Base::Console().Log(tmp.c_str());
425 + GCS::Log(tmp.c_str());
428 return (stop == 1) ? Success : Failed;
429 @@ -3721,7 +3772,7 @@ int System::diagnose(Algorithm alg)
430 Eigen::SparseQR<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int> > SqrJT;
432 if(qrAlgorithm==EigenSparseQR){
433 - Base::Console().Warning("SparseQR not supported by you current version of Eigen. It requires Eigen 3.2.2 or higher. Falling back to Dense QR\n");
434 + GCS::Warning("SparseQR not supported by you current version of Eigen. It requires Eigen 3.2.2 or higher. Falling back to Dense QR\n");
435 qrAlgorithm=EigenDenseQR;
438 @@ -4074,7 +4125,7 @@ int System::diagnose(Algorithm alg)
442 - Base::Console().Log("Sketcher::RedundantSolving-%s-\n",solvername.c_str());
443 + GCS::Log("Sketcher::RedundantSolving-%s-\n",solvername.c_str());
446 if (res == Success) {
447 @@ -4088,7 +4139,7 @@ int System::diagnose(Algorithm alg)
450 if(debugMode==Minimal || debugMode==IterationLevel) {
451 - Base::Console().Log("Sketcher Redundant solving: %d redundants\n",redundant.size());
452 + GCS::Log("Sketcher Redundant solving: %d redundants\n",redundant.size());
455 std::vector< std::vector<Constraint *> > conflictGroupsOrig=conflictGroups;
456 diff --git a/src/Mod/Sketcher/App/planegcs/GCS.h b/src/Mod/Sketcher/App/planegcs/GCS.h
457 index 17d57c8..ecc5b04 100644
458 --- a/src/Mod/Sketcher/App/planegcs/GCS.h
459 +++ b/src/Mod/Sketcher/App/planegcs/GCS.h
460 @@ -69,7 +69,7 @@ namespace GCS
465 + class PLANEGCS_EXPORT System
467 // This is the main class. It holds all constraints and information
468 // about partitioning into subsystems and solution strategies
469 @@ -283,9 +283,9 @@ namespace GCS
471 ///////////////////////////////////////
473 - void free(VEC_pD &doublevec);
474 - void free(std::vector<Constraint *> &constrvec);
475 - void free(std::vector<SubSystem *> &subsysvec);
476 + PLANEGCS_EXPORT void free(VEC_pD &doublevec);
477 + PLANEGCS_EXPORT void free(std::vector<Constraint *> &constrvec);
478 + PLANEGCS_EXPORT void free(std::vector<SubSystem *> &subsysvec);
482 diff --git a/src/Mod/Sketcher/App/planegcs/Geo.h b/src/Mod/Sketcher/App/planegcs/Geo.h
483 index 1cdb983..6246f3b 100644
484 --- a/src/Mod/Sketcher/App/planegcs/Geo.h
485 +++ b/src/Mod/Sketcher/App/planegcs/Geo.h
491 +#pragma warning(disable : 4251)
496 - class DependentParameters
497 + class PLANEGCS_EXPORT DependentParameters
500 DependentParameters():hasDependentParameters(false) {}
501 bool hasDependentParameters;
504 - class Point : public DependentParameters
505 + class PLANEGCS_EXPORT Point : public DependentParameters
508 Point(){x = 0; y = 0;}
509 @@ -55,7 +59,7 @@ namespace GCS
510 ///manually as well. The class also provides a bunch of methods to do math
511 ///on it (and derivatives are calculated implicitly).
514 + class PLANEGCS_EXPORT DeriVector2
517 DeriVector2(){x=0; y=0; dx=0; dy=0;}
518 @@ -95,7 +99,7 @@ namespace GCS
520 ///////////////////////////////////////
522 - class Curve: public DependentParameters //a base class for all curve-based objects (line, circle/arc, ellipse/arc)
523 + class PLANEGCS_EXPORT Curve: public DependentParameters //a base class for all curve-based objects (line, circle/arc, ellipse/arc)
527 @@ -125,7 +129,7 @@ namespace GCS
528 virtual Curve* Copy() = 0; //DeepSOIC: I haven't found a way to simply copy a curve object provided pointer to a curve object.
531 - class Line: public Curve
532 + class PLANEGCS_EXPORT Line: public Curve
536 @@ -139,7 +143,7 @@ namespace GCS
537 virtual Line* Copy();
540 - class Circle: public Curve
541 + class PLANEGCS_EXPORT Circle: public Curve
545 @@ -153,7 +157,7 @@ namespace GCS
546 virtual Circle* Copy();
549 - class Arc: public Circle
550 + class PLANEGCS_EXPORT Arc: public Circle
553 Arc(){startAngle=0;endAngle=0;rad=0;}
554 @@ -169,7 +173,7 @@ namespace GCS
558 - class MajorRadiusConic: public Curve
559 + class PLANEGCS_EXPORT MajorRadiusConic: public Curve
562 virtual ~MajorRadiusConic(){}
563 @@ -179,7 +183,7 @@ namespace GCS
564 DeriVector2 CalculateNormal(Point &p, double* derivparam = 0) = 0;
567 - class Ellipse: public MajorRadiusConic
568 + class PLANEGCS_EXPORT Ellipse: public MajorRadiusConic
571 Ellipse(){ radmin = 0;}
572 @@ -197,7 +201,7 @@ namespace GCS
573 virtual Ellipse* Copy();
576 - class ArcOfEllipse: public Ellipse
577 + class PLANEGCS_EXPORT ArcOfEllipse: public Ellipse
580 ArcOfEllipse(){startAngle=0;endAngle=0;radmin = 0;}
581 @@ -215,7 +219,7 @@ namespace GCS
582 virtual ArcOfEllipse* Copy();
585 - class Hyperbola: public MajorRadiusConic
586 + class PLANEGCS_EXPORT Hyperbola: public MajorRadiusConic
589 Hyperbola(){ radmin = 0;}
590 @@ -233,7 +237,7 @@ namespace GCS
591 virtual Hyperbola* Copy();
594 - class ArcOfHyperbola: public Hyperbola
595 + class PLANEGCS_EXPORT ArcOfHyperbola: public Hyperbola
598 ArcOfHyperbola(){startAngle=0;endAngle=0;radmin = 0;}
599 @@ -249,7 +253,7 @@ namespace GCS
600 virtual ArcOfHyperbola* Copy();
603 - class Parabola: public Curve
604 + class PLANEGCS_EXPORT Parabola: public Curve
608 @@ -263,7 +267,7 @@ namespace GCS
609 virtual Parabola* Copy();
612 - class ArcOfParabola: public Parabola
613 + class PLANEGCS_EXPORT ArcOfParabola: public Parabola
616 ArcOfParabola(){startAngle=0;endAngle=0;}
617 @@ -279,7 +283,7 @@ namespace GCS
618 virtual ArcOfParabola* Copy();
621 - class BSpline: public Curve
622 + class PLANEGCS_EXPORT BSpline: public Curve
625 BSpline(){periodic=false;degree=2;}
626 diff --git a/src/Mod/Sketcher/App/planegcs/Util.h b/src/Mod/Sketcher/App/planegcs/Util.h
627 index 8a759ba..7baafaa 100644
628 --- a/src/Mod/Sketcher/App/planegcs/Util.h
629 +++ b/src/Mod/Sketcher/App/planegcs/Util.h
630 @@ -44,4 +44,14 @@ namespace GCS
635 +# if defined PLANEGCS_EXPORTS
636 +# define PLANEGCS_EXPORT __declspec(dllexport)
638 +# define PLANEGCS_EXPORT __declspec(dllimport)
641 +# define PLANEGCS_EXPORT
644 #endif // PLANEGCS_UTIL_H