Fade2D Documentation pages v2.17.3
Delaunay Features
Zone2.h
Go to the documentation of this file.
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the Fade2D library. The student license is free
4 // of charge and covers personal, non-commercial research. Licensees
5 // holding a commercial license may use this file in accordance with
6 // the Commercial License Agreement.
7 //
8 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
9 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
10 // FOR A PARTICULAR PURPOSE.
11 //
12 // Please contact the author if any conditions of this licensing are
13 // unclear to you.
14 //
15 // Support: https://www.geom.at/contact/
16 // Project: https://www.geom.at/fade2d/html/
17 
19 
20 #pragma once
21 
22 #include "common.h"
23 #include "freeFunctions.h"
24 #include "FadeExport.h"
25 #include "Bbox2.h"
26 #include "Edge2.h"
27 #include "Segment2.h"
28 #include "UserPredicates.h"
29 #include "MsgBase.h"
30 #include "VtkWriter.h"
31 #include "CompPolygon.h"
32 #include "PolygonClipper.h"
33 
34 
35 #if GEOM_PSEUDO3D==GEOM_TRUE
36  namespace GEOM_FADE25D {
37 #elif GEOM_PSEUDO3D==GEOM_FALSE
38  namespace GEOM_FADE2D {
39 #else
40  #error GEOM_PSEUDO3D is not defined
41 #endif
42 
43 class ZoneShooter2; // FWD
44 class ConstraintSegment2; // FWD
45 class Fade_2D; // FWD
46 
55 {
59 };
60 
61 
62 
69 enum SegmentSelectionType
70 {
71  SST_CONSTRAINT,
72  SST_INCIRCLE,
73  SST_IN_OR_ONCIRCLE
74 };
75 
76 
77 
78 class Progress; // FWD
79 class Dt2; // Fwd
80 class ConstraintGraph2; // Fwd
81 class Triangle2; // Fwd
82 class Point2; // Fwd
83 class Visualizer2; // Fwd
84 class Visualizer3; // Fwd
85 
86 
87 
98 class CLASS_DECLSPEC Zone2
99 {
100 public:
102  Zone2(Dt2* pDt_,ZoneLocation zoneLoc_);
104  Zone2(Dt2* pDt_,ZoneLocation zoneLoc_,ConstraintGraph2* pConstraintGraph_);
106  Zone2(Dt2* pDt_,ZoneLocation zoneLoc_,const std::vector<ConstraintGraph2*>& vConstraintGraphs_);
108  Zone2(Dt2* pDt_,const std::vector<ConstraintGraph2*>& vConstraintGraphs_,ZoneLocation zoneLoc_,std::vector<Point2>& vStartPoints);
110  ~Zone2();
122  bool hasOn(const Point2& p);
123 
124 
137 
138 #if GEOM_PSEUDO3D==GEOM_TRUE
145  double getMedianEdgeLength25D();
146 #endif
147 
160  bool hasOnBoundary(const Point2& p);
161 
173  Triangle2* locate(const Point2& p);
174 
211  bool shiftToZone(const Point2& q, double tolerance, Point2& result);
212 
213 
239  bool save(const char* filename);
240 
264  bool save(std::ostream& stream);
265 
266 
276  void exportZone(FadeExport& fadeExport,bool bWithCustomIndices) const;
277 
278 
279 
300  void getOffsetBoundary(double offset,std::vector<Segment2>& vOffsetBoundary,double mergeAngleDeg=10.0,double angleStepDeg=20.0) const;
301 
323  Zone2* createOffsetZone(Fade_2D* pFade,double offset,double mergeAngleDeg=10,double angleStep=20.0) const;
324 
325 
326 
357  Zone2* morphOpen(Fade_2D* pFade,double offset,double mergeAngleDeg=10.0,double angleStep=20.0) const;
358 
359 
383  Zone2* morphClose(Fade_2D* pFade,double offset,double mergeAngleDeg=10.0,double angleStep=20.0) const;
384 
385 
393  void subscribe(MsgType msgType,MsgBase* pMsg);
394 
395 
403  void unsubscribe(MsgType msgType,MsgBase* pMsg);
404 
411  ZoneLocation getZoneLocation() const;
412 
413 
428 
429 
443  void show(const char* filename,const Colorname& zoneFillColor=CTRANSPARENT,const Colorname& zoneEdgeColor=CBLACK,const Colorname& nonZoneFillColor=CTRANSPARENT,const Colorname& nonZoneEdgeColor=CTRANSPARENT,const Colorname& constraintEdgeColor=CTRANSPARENT) const;
444 
458  void show(Visualizer2* pVisualizer,const Colorname& zoneFillColor=CTRANSPARENT,const Colorname& zoneEdgeColor=CBLACK,const Colorname& nonZoneFillColor=CTRANSPARENT,const Colorname& nonZoneEdgeColor=CTRANSPARENT,const Colorname& constraintEdgeColor=CTRANSPARENT) const;
459 
460 
469  void show(const char* filename,bool bShowFull,bool bWithConstraints) const;
470 
481  void show(Visualizer2* pVisualizer,bool bShowFull,bool bWithConstraints) const;
482 
492  void showVtk(const char* filename,VtkColor zoneColor,VtkColor nonZoneColor=VTK_TRANSPARENT,VtkColor constraintColor=VTK_TRANSPARENT) const;
493 
505  void showVtk(VtkWriter* pVtk,VtkColor zoneColor,VtkColor nonZoneColor=VTK_TRANSPARENT,VtkColor constraintColor=VTK_TRANSPARENT) const;
506 
507 
508 
509 
510 
511 #if GEOM_PSEUDO3D==GEOM_TRUE
521  void showGeomview(const char* filename,const char* color) const;
531  void showGeomview(Visualizer3* pVis,const char* color) const;
532 
534  void analyzeAngles(const char* name="");
535 
547  double getVerticalWalls(double aboveAngleDegree,std::vector<Triangle2*>& vVerticalWalls);
548 
559  void smoothing(int numIterations=2,bool bWithXY=true);
560 
561 
586  void smoothing2(int numIterations,bool bWithXY,bool bWithConstraintZ);
608  void slopeValleyRidgeOptimization(OptimizationMode om=OPTMODE_BETTER);
609 
611  /*
612  * This function is deprecated but kept for backwards compatibility.
613  * Better use slopeValleyRidgeOptimization() (see above)
614  *
615  * Optimize Valleys and Ridges
616  *
617  * A Delaunay triangulation is not unique when when 2 or more triangles
618  * share a common circumcircle. As a consequence the four corners of
619  * a rectangle can be triangulated in two different ways: Either the
620  * diagonal proceeds from the lower left to the upper right corner
621  * or it connects the other two corners. Both solutions are valid and
622  * an arbitrary one is applied when points are triangulated. To improve
623  * the repeatability and for reasons of visual appearance this method
624  * unifies such diagonals such that they point from the lower left to
625  * the upper right corner (or in horizontal direction).\n
626  *
627  * Moreover a Delaunay triangulation does not take the z-value into
628  * account and thus valleys and ridges may be disturbed. The present
629  * method flips diagonals such that they point from the lower left to
630  * the upper right corner of a quad. And if the 2.5D lengths of the
631  * diagonals are significantly different, then the shorter one is
632  * applied.
633  *
634  * @param tolerance2D is 0 when only exact cases of more than 3 points
635  * on a common circumcircle shall be changed. But in practice input
636  * data can be disturbed by noise and tiny rounding errors such that
637  * grid points are not exactly on a grid. The numeric error is computed
638  * as \f$error=\frac{abs(diagonalA-diagonalB)}{max(diagonalA,diagonalB)}\f$.
639  * and \p tolerance2D is an upper threshold to allow modification despite
640  * such tiny inaccuracies.
641  * @param lowerThreshold25D is used to take also the heights of the
642  * involved points into account. For example, the points\n
643  * \n
644  * Point_2 a(0,0,0);\n
645  * Point_2 b(10,0,0);\n
646  * Point_2 c(10,10,0);\n
647  * Point_2 d(0,10,1000);\n
648  * \n
649  * can form the triangles (a,b,c) and (a,c,d) or the triangles (a,b,d)
650  * and (d,b,c) but (a,c) is obviousy the better diagonal because the
651  * points a,b,c share the same elevation while d is at z=1000.
652  * Technically spoken, the diagonal with the smaller 2.5D-length is
653  * applied if the both, the 2D error is below \p tolerance2D and the
654  * 2.5D error is above \p lowerThreshold25D. The 2.5D
655  * criterion has priority over the 2D criterion.
656  *
657  */
658  void optimizeValleysAndRidges(double tolerance2D,double lowerThreshold25D);
659 #endif
660 
661 
679  void unifyGrid(double tolerance);
680 
682  bool assignDt2(Dt2* pDt_);
683 
691  void getTriangles(std::vector<Triangle2*>& vTriangles) const;
692 
696  void getVertices(std::vector<Point2*>& vVertices_) const;
697 
698 
703  void statistics(const char* s) const;
704 
705 
713 
721  size_t getNumberOfTriangles() const;
722 
723 
727  void getConstraintGraphs(std::vector<ConstraintGraph2*>& vConstraintGraphs_) const;
728 
730  Dt2* getDelaunayTriangulation() const;
731 
738  size_t numberOfConstraintGraphs() const;
739 
741  void debug(const char* name="");
742 
746 
747  // Deprecated, replaced by getBorderEdges() but kept for
748  // backwards-compatibility.
751  void getBoundaryEdges(std::vector<Edge2>& vEdges) const;
752 
769  void getComponentPolygons(std::vector<CompPolygon>& vCompPolygons) const;
770 
771 
781  void getBoundarySegments(std::vector<Segment2>& vSegments) const;
793  double getArea2D() const;
794 
795 
796 #if GEOM_PSEUDO3D==GEOM_TRUE
804  double getArea25D() const;
805 #endif
806 
816 void getBorderEdges(std::vector<Edge2>& vBorderEdgesOut) const;
817 
826  void writeObj(const char* filename) const;
827 
828 #ifndef SKIPTHREADS
829 #if GEOM_PSEUDO3D==GEOM_TRUE
830 
842  bool writePly(const char* filename,bool bASCII=false) const;
843 
855  bool writePly(std::ostream& os,bool bASCII=false) const;
856 #endif
857 #endif
858 
859 
875  void getReplicationData( SegmentSelectionType segmentSelectionType,
876  std::vector<Point2>& vPoints,
877  std::vector<Segment2>& vInnerSegments,
878  std::vector<Segment2>& vBoundarySegments
879  ) const;
880 
881 
882 protected:
883  Zone2& operator=(const Zone2&);
884  // Optimization techniques
886  void optMode_standard_sub(std::vector<Triangle2*>& vT,std::vector<Triangle2*>& vChangedT);
888  void optMode_standard();
890  double optMode_prioq(double noEdgeBelowDih,bool bWithProgress);
892  void getEdgesForOptimization(double noEdgeBelowDegree,std::vector<Edge2>& vEdges);
894  void optMode_simulatedAnnealing();
896  void optMode_simulatedAnnealing_sub(std::vector<Edge2>& vUniqueEdges,double temperature);
898  void removeConstraintEdges(std::vector<Edge2>& vEdges) const;
900  Zone2(const Zone2&);
902  void getTriangles_RESULT(std::vector<Triangle2*>& vTriangles) const;
904  void initWorkspace(bool bInside,std::set<std::pair<Point2*,Point2*> >& sNoGrowEdges,std::vector<Triangle2*>& vWorkspace) const;
906  void bfsFromWorkspace(std::vector<Triangle2*>& vWorkspace,std::set<std::pair<Point2*,Point2*> >& sNoGrowEdges,std::vector<Triangle2*>& vTriangles) const;
908  Zone2* ctbz_treatCC(std::vector<Triangle2*>& vOneCC);
909  // Data
911  Dt2* pDt;
913  Progress* pZoneProgress;
915  ZoneShooter2* pZoneShooter;
917  ZoneLocation zoneLoc;
918  CLASS_DECLSPEC
919  friend Zone2* zoneUnion(Zone2* pZone0,Zone2* pZone1);
920  CLASS_DECLSPEC
921  friend Zone2* zoneIntersection(Zone2* pZone0,Zone2* pZone1);
922  CLASS_DECLSPEC
923  friend Zone2* zoneDifference(Zone2* pZone0,Zone2* pZone1);
924  CLASS_DECLSPEC
925  friend Zone2* zoneSymmetricDifference(Zone2* pZone0,Zone2* pZone1);
926 
933  CLASS_DECLSPEC
934  friend Zone2* peelOffIf(Zone2* pZone, UserPredicateT* pPredicate,bool bVerbose); // Depricated!
935 
936 
956  CLASS_DECLSPEC
957  friend Zone2* peelOffIf(Zone2* pZone, bool bAvoidSplit,PeelPredicateTS* pPredicate);
958 
959 
960 
961 private:
962 #ifndef __MINGW32__
963 #ifdef _WIN32
964 #pragma warning(push)
965 #pragma warning(disable:4251)
966 #endif
967 #endif
968  std::vector<ConstraintGraph2*> vConstraintGraphs;
969  std::vector<Point2> vStartPoints;
970  std::vector<Zone2*> vInputZones;
971 
972 #ifndef __MINGW32__
973 #ifdef _WIN32
974 #pragma warning(pop)
975 #endif
976 #endif
977 };
978 
979 // Free functions
997 CLASS_DECLSPEC
998 Zone2* zoneDifference(Zone2* pZone0,Zone2* pZone1);
999 
1018 CLASS_DECLSPEC
1019 Zone2* zoneIntersection(Zone2* pZone0,Zone2* pZone1);
1020 
1021 
1040 CLASS_DECLSPEC
1042 
1043 
1058 CLASS_DECLSPEC
1059 Zone2* zoneUnion(Zone2* pZone0,Zone2* pZone1);
1060 
1061 
1062 
1063 
1064 
1065 
1066 } // (namespace)
Colorname
Predefined colors for convenience.
Definition: Color.h:36
MsgType
Message types for the message system.
Definition: MsgBase.h:40
VtkColor
Enumeration of colors used by the VTKWriter class to represent various colors in the VTK file format.
Definition: VtkWriter.h:39
CLASS_DECLSPEC Zone2 * zoneUnion(Zone2 *pZone0, Zone2 *pZone1)
Computes the union of two Zone2 objects.
CLASS_DECLSPEC Zone2 * zoneIntersection(Zone2 *pZone0, Zone2 *pZone1)
Computes the intersection of two Zone2 objects.
OptimizationMode
Definition: Zone2.h:55
@ OPTMODE_STANDARD
Fastest optimization mode.
Definition: Zone2.h:56
@ OPTMODE_BEST
Best quality but quite time consuming.
Definition: Zone2.h:58
@ OPTMODE_BETTER
Considerably better quality and still fast.
Definition: Zone2.h:57
CLASS_DECLSPEC Zone2 * zoneSymmetricDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the symmetric difference between two Zone2 objects.
CLASS_DECLSPEC Zone2 * zoneDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the difference between two Zone2 objects.
A 2D axis-aligned bounding box.
Definition: Bbox2.h:39
A constraint graph is a collection of constraint edges (ConstraintSegment2).
Definition: ConstraintGraph2.h:56
A ConstraintSegment2 represents a constraint edge between two vertices of a Delaunay triangulation.
Definition: ConstraintSegment2.h:71
Fade_2D is a class that contains a Delaunay triangulation.
Definition: Fade_2D.h:124
MsgBase, a base class for message subscriber classes.
Definition: MsgBase.h:55
User-defined peel predicate.
Definition: UserPredicates.h:57
Represents a 2D point.
Definition: Point2.h:76
Represents a triangle in a triangulation.
Definition: Triangle2.h:59
User-defined predicate (deprecated)
Definition: UserPredicates.h:38
A PDF and Postscript Writer for 2D Visualization.
Definition: Visualizer2.h:60
A class for writing data to the VTK file format for visualization purposes.
Definition: VtkWriter.h:76
Represents a defined area within a triangulation.
Definition: Zone2.h:99
Zone2 * createOffsetZone(Fade_2D *pFade, double offset, double mergeAngleDeg=10, double angleStep=20.0) const
Creates an offset zone.
bool hasOnBoundary(const Point2 &p)
Checks if a point lies on the boundary of the zone.
ConstraintGraph2 * getConstraintGraph() const
Get the associated constraint.
bool save(std::ostream &stream)
Save the zone.
CLASS_DECLSPEC friend Zone2 * zoneSymmetricDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the symmetric difference between two Zone2 objects.
void show(Visualizer2 *pVisualizer, const Colorname &zoneFillColor=CTRANSPARENT, const Colorname &zoneEdgeColor=CBLACK, const Colorname &nonZoneFillColor=CTRANSPARENT, const Colorname &nonZoneEdgeColor=CTRANSPARENT, const Colorname &constraintEdgeColor=CTRANSPARENT) const
Postscript- and PDF visualization.
Bbox2 getBoundingBox() const
Compute the bounding box.
void showVtk(const char *filename, VtkColor zoneColor, VtkColor nonZoneColor=VTK_TRANSPARENT, VtkColor constraintColor=VTK_TRANSPARENT) const
VTK visualization.
CLASS_DECLSPEC friend Zone2 * zoneUnion(Zone2 *pZone0, Zone2 *pZone1)
Computes the union of two Zone2 objects.
double getArea2D() const
Get 2D Area.
void getTriangles(std::vector< Triangle2 * > &vTriangles) const
Get the triangles of the zone.
size_t numberOfConstraintGraphs() const
Get the number of ConstraintGraph2 objects.
Triangle2 * locate(const Point2 &p)
Locates the triangle containing the given point within the zone.
double getMedianEdgeLength2D()
Get the median 2D edge length.
void getComponentPolygons(std::vector< CompPolygon > &vCompPolygons) const
Get connected components.
void show(Visualizer2 *pVisualizer, bool bShowFull, bool bWithConstraints) const
Postscript- and PDF-visualization.
bool hasOn(const Point2 &p)
Checks if a point lies on the zone.
void show(const char *filename, bool bShowFull, bool bWithConstraints) const
Postscript- and PDF-visualization.
void subscribe(MsgType msgType, MsgBase *pMsg)
Registers a message receiver.
void getBoundarySegments(std::vector< Segment2 > &vSegments) const
Get the boundaries as segments.
CLASS_DECLSPEC friend Zone2 * peelOffIf(Zone2 *pZone, bool bAvoidSplit, PeelPredicateTS *pPredicate)
Peel off undesired border triangles.
Zone2 * morphClose(Fade_2D *pFade, double offset, double mergeAngleDeg=10.0, double angleStep=20.0) const
Morphological closing to bridge small gaps in the Zone2.
void getConstraintGraphs(std::vector< ConstraintGraph2 * > &vConstraintGraphs_) const
Get the associated constraint graphs.
Zone2 * morphOpen(Fade_2D *pFade, double offset, double mergeAngleDeg=10.0, double angleStep=20.0) const
Morphological opening to remove small details.
void writeObj(const char *filename) const
Write the Zone2 to an .obj file.
CLASS_DECLSPEC friend Zone2 * zoneDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the difference between two Zone2 objects.
ZoneLocation getZoneLocation() const
Get the zone location.
void getVertices(std::vector< Point2 * > &vVertices_) const
Get the vertices of the zone.
void show(const char *filename, const Colorname &zoneFillColor=CTRANSPARENT, const Colorname &zoneEdgeColor=CBLACK, const Colorname &nonZoneFillColor=CTRANSPARENT, const Colorname &nonZoneEdgeColor=CTRANSPARENT, const Colorname &constraintEdgeColor=CTRANSPARENT) const
Postscript- and PDF visualization.
void exportZone(FadeExport &fadeExport, bool bWithCustomIndices) const
Export triangles from a zone.
void showVtk(VtkWriter *pVtk, VtkColor zoneColor, VtkColor nonZoneColor=VTK_TRANSPARENT, VtkColor constraintColor=VTK_TRANSPARENT) const
VTK visualization.
void unsubscribe(MsgType msgType, MsgBase *pMsg)
Unregisters a message receiver.
bool shiftToZone(const Point2 &q, double tolerance, Point2 &result)
Finds a point close to the query that lies inside the zone.
void getOffsetBoundary(double offset, std::vector< Segment2 > &vOffsetBoundary, double mergeAngleDeg=10.0, double angleStepDeg=20.0) const
Get the offset boundary of a shape.
size_t getNumberOfTriangles() const
Get the number of triangles.
bool save(const char *filename)
Save the zone.
Zone2 * convertToBoundedZone()
Convert a zone to a bounded zone.
void unifyGrid(double tolerance)
Unify Grid.
void statistics(const char *s) const
Prints statistics.
void getBorderEdges(std::vector< Edge2 > &vBorderEdgesOut) const
Get border edges.
CLASS_DECLSPEC friend Zone2 * peelOffIf(Zone2 *pZone, UserPredicateT *pPredicate, bool bVerbose)
Peel off undesired border triangles (deprecated)
ConstraintSegment2 * getNearbyBoundaryConstraint(Point2 &p, double tolerance)
Locates the nearest boundary ConstraintSegment2 of the Zone2.
CLASS_DECLSPEC friend Zone2 * zoneIntersection(Zone2 *pZone0, Zone2 *pZone1)
Computes the intersection of two Zone2 objects.
FadeExport is a simple struct to export triangulation data.
Definition: FadeExport.h:49