Fade2D Documentation pages v2.16.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 // not clear to you.
14 //
15 // Author: Bernhard Kornberger, bkorn (at) geom.at
16 // http://www.geom.at
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 #if GEOM_PSEUDO3D==GEOM_TRUE
35  namespace GEOM_FADE25D {
36 #elif GEOM_PSEUDO3D==GEOM_FALSE
37  namespace GEOM_FADE2D {
38 #else
39  #error GEOM_PSEUDO3D is not defined
40 #endif
41 
42 class ZoneShooter; // FWD
43 class ConstraintSegment2; // FWD
44 
53 {
57 };
58 
59 class Progress; // FWD
60 class Dt2; // Fwd
61 class ConstraintGraph2; // Fwd
62 class Triangle2; // Fwd
63 class Point2; // Fwd
64 class Visualizer2; // Fwd
65 class Visualizer3; // Fwd
66 
67 //
69 // *
70 // * The CompPolygon struct holds a connected component of triangles.
71 // * Thereby two triangles count as connected if they share a common
72 // * edge. It also holds an ordered vector of polygon edges and
73 // * ordered vectors of hole edges.
74 // *
75 // * @remark An edge is represented by a triangle and an opposite
76 // * index. Its orientation is always counterclockwise (CCW) around
77 // * the triangle. Thus outer polygon edges are counterclockwise
78 // * while hole polygons (if any) are clockwise (CW) oriented.
79 // */
80 //struct CLASS_DECLSPEC CompPolygon
81 //{
82 // CompPolygon();
83 // CompPolygon(const CompPolygon& other);
84 // CompPolygon& operator=(const CompPolygon& other);
85 // ~CompPolygon();
86 // // Data
87 // std::vector<Triangle2*>* pvCC; ///< Connected component of triangles (connection along edges, not just vertices)
88 // std::vector<Edge2>* pvOuterPolygon; ///< Ordered outer polygon
89 // std::vector<std::vector<Edge2> >* pvvHolePolygons; ///< Ordered hole polygons
90 //};
91 
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();
111 
123  bool hasOn(const Point2& p);
124 
136  bool hasOnBoundary(const Point2& p);
137 
149  Triangle2* locate(const Point2& p);
150 
179  bool shiftToZone(const Point2& from, double tolerance, Point2& result);
180 
181 
201  bool save(const char* filename);
202 
221  bool save(std::ostream& stream);
222 
223 
230  void exportZone(FadeExport& fadeExport,bool bWithCustomIndices) const;
231 
245  void getOffsetBoundary(double offset,std::vector<Segment2>& vOffsetBoundary,double mergeAngleDeg=10.0,double angleStepDeg=20.0);
246 
252  void subscribe(MsgType msgType,MsgBase* pMsg);
258  void unsubscribe(MsgType msgType,MsgBase* pMsg);
269  ZoneLocation getZoneLocation() const;
270 
284 
292  void show(const char* filename,bool bShowFull,bool bWithConstraints) const;
293 
301  void show(Visualizer2* pVisualizer,bool bShowFull,bool bWithConstraints) const;
302 
303 
304 
312  void showVtk(const char* filename,VtkColor zoneColor,VtkColor nonZoneColor=VTK_TRANSPARENT,VtkColor constraintColor=VTK_TRANSPARENT) const;
313 
314 
324  void showVtk(VtkWriter* pVtk,VtkColor zoneColor,VtkColor nonZoneColor=VTK_TRANSPARENT,VtkColor constraintColor=VTK_TRANSPARENT) const;
325 
326 
327 
328 #if GEOM_PSEUDO3D==GEOM_TRUE
335  void showGeomview(const char* filename,const char* color) const;
342  void showGeomview(Visualizer3* pVis,const char* color) const;
343 
345  void analyzeAngles(const char* name="");
346 
364  void smoothing(int numIterations=2,bool bWithXY=true);
365 
386  void smoothing2(int numIterations,bool bWithXY,bool bWithConstraintZ);
387 
408  void slopeValleyRidgeOptimization(OptimizationMode om=OPTMODE_BETTER);
409 
411  /*
412  * This function is deprecated but kept for backwards compatibility.
413  * Better use slopeValleyRidgeOptimization() (see above)
414  *
415  * Optimize Valleys and Ridges
416  *
417  * A Delaunay triangulation is not unique when when 2 or more triangles
418  * share a common circumcircle. As a consequence the four corners of
419  * a rectangle can be triangulated in two different ways: Either the
420  * diagonal proceeds from the lower left to the upper right corner
421  * or it connects the other two corners. Both solutions are valid and
422  * an arbitrary one is applied when points are triangulated. To improve
423  * the repeatability and for reasons of visual appearance this method
424  * unifies such diagonals such that they point from the lower left to
425  * the upper right corner (or in horizontal direction).\n
426  *
427  * Moreover a Delaunay triangulation does not take the z-value into
428  * account and thus valleys and ridges may be disturbed. The present
429  * method flips diagonals such that they point from the lower left to
430  * the upper right corner of a quad. And if the 2.5D lengths of the
431  * diagonals are significantly different, then the shorter one is
432  * applied.
433  *
434  * @param tolerance2D is 0 when only exact cases of more than 3 points
435  * on a common circumcircle shall be changed. But in practice input
436  * data can be disturbed by noise and tiny rounding errors such that
437  * grid points are not exactly on a grid. The numeric error is computed
438  * as \f$error=\frac{abs(diagonalA-diagonalB)}{max(diagonalA,diagonalB)}\f$.
439  * and \p tolerance2D is an upper threshold to allow modification despite
440  * such tiny inaccuracies.
441  * @param lowerThreshold25D is used to take also the heights of the
442  * involved points into account. For example, the points\n
443  * \n
444  * Point_2 a(0,0,0);\n
445  * Point_2 b(10,0,0);\n
446  * Point_2 c(10,10,0);\n
447  * Point_2 d(0,10,1000);\n
448  * \n
449  * can form the triangles (a,b,c) and (a,c,d) or the triangles (a,b,d)
450  * and (d,b,c) but (a,c) is obviousy the better diagonal because the
451  * points a,b,c share the same elevation while d is at z=1000.
452  * Technically spoken, the diagonal with the smaller 2.5D-length is
453  * applied if the both, the 2D error is below \p tolerance2D and the
454  * 2.5D error is above \p lowerThreshold25D. The 2.5D
455  * criterion has priority over the 2D criterion.
456  *
457  */
458  void optimizeValleysAndRidges(double tolerance2D,double lowerThreshold25D);
459 #endif
460 
483  void unifyGrid(double tolerance);
484 
486  bool assignDt2(Dt2* pDt_);
487 
498  void getTriangles(std::vector<Triangle2*>& vTriangles_) const;
499 
503  void getVertices(std::vector<Point2*>& vVertices_) const;
504 
505 
510  void statistics(const char* s) const;
511 
512 
518 
523  size_t getNumberOfTriangles() const;
524 
525 
529  void getConstraintGraphs(std::vector<ConstraintGraph2*>& vConstraintGraphs_) const;
530 
532  Dt2* getDelaunayTriangulation() const;
533 
538  size_t numberOfConstraintGraphs() const;
539 
542  void debug(const char* name="");
543 
547 
548  // Deprecated, replaced by getBorderEdges() but kept for
549  // backwards-compatibility.
552  void getBoundaryEdges(std::vector<Edge2>& vEdges) const;
553 
564  void getComponentPolygons(std::vector<CompPolygon>& vCompPolygons) const;
565 
571  void getBoundarySegments(std::vector<Segment2>& vSegments) const;
572 
585  double getArea2D() const;
586 
587 
588 #if GEOM_PSEUDO3D==GEOM_TRUE
596  double getArea25D() const;
597 #endif
598 
602  void getBorderEdges(std::vector<Edge2>& vBorderEdgesOut) const;
603 
610  void writeObj(const char* outFilename) const;
611 
612 #ifndef SKIPTHREADS
613 #if GEOM_PSEUDO3D==GEOM_TRUE
621  bool writePly(const char* filename,bool bASCII=false) const;
629  bool writePly(std::ostream& os,bool bASCII=false) const;
630 #endif
631 #endif
632 
633 
634 
635 protected:
636  Zone2& operator=(const Zone2&);
637  // Optimization techniques
639  void optMode_standard_sub(std::vector<Triangle2*>& vT,std::vector<Triangle2*>& vChangedT);
641  void optMode_standard();
643  double optMode_prioq(double noEdgeBelowDih,bool bWithProgress);
645  void getEdgesForOptimization(double noEdgeBelowDegree,std::vector<Edge2>& vEdges);
647  void optMode_simulatedAnnealing();
649  void optMode_simulatedAnnealing_sub(std::vector<Edge2>& vUniqueEdges,double temperature);
651  void removeConstraintEdges(std::vector<Edge2>& vEdges) const;
653  Zone2(const Zone2&);
655  void getTriangles_RESULT(std::vector<Triangle2*>& vTriangles) const;
657  void initWorkspace(bool bInside,std::set<std::pair<Point2*,Point2*> >& sNoGrowEdges,std::vector<Triangle2*>& vWorkspace) const;
659  void bfsFromWorkspace(std::vector<Triangle2*>& vWorkspace,std::set<std::pair<Point2*,Point2*> >& sNoGrowEdges,std::vector<Triangle2*>& vTriangles) const;
661  Zone2* ctbz_treatCC(std::vector<Triangle2*>& vOneCC);
662  // Data
664  Dt2* pDt;
666  Progress* pZoneProgress;
668  ZoneShooter* pZoneShooter;
670  ZoneLocation zoneLoc;
671  CLASS_DECLSPEC
672  friend Zone2* zoneUnion(Zone2* pZone0,Zone2* pZone1);
673  CLASS_DECLSPEC
674  friend Zone2* zoneIntersection(Zone2* pZone0,Zone2* pZone1);
675  CLASS_DECLSPEC
676  friend Zone2* zoneDifference(Zone2* pZone0,Zone2* pZone1);
677  CLASS_DECLSPEC
678  friend Zone2* zoneSymmetricDifference(Zone2* pZone0,Zone2* pZone1);
679 
691  CLASS_DECLSPEC
692  friend Zone2* peelOffIf(Zone2* pZone, UserPredicateT* pPredicate,bool bVerbose); // Depricated!
693 
705  CLASS_DECLSPEC
706  friend Zone2* peelOffIf(Zone2* pZone, bool bAvoidSplit,PeelPredicateTS* pPredicate);
707 
708 
709 
710 private:
711 #ifndef __MINGW32__
712 #ifdef _WIN32
713 #pragma warning(push)
714 #pragma warning(disable:4251)
715 #endif
716 #endif
717  std::vector<ConstraintGraph2*> vConstraintGraphs;
718  std::vector<Point2> vStartPoints;
719  std::vector<Zone2*> vInputZones;
720 
721 #ifndef __MINGW32__
722 #ifdef _WIN32
723 #pragma warning(pop)
724 #endif
725 #endif
726 };
727 
728 // Free functions
740 CLASS_DECLSPEC
741 Zone2* zoneUnion(Zone2* pZone0,Zone2* pZone1);
742 
743 
756 CLASS_DECLSPEC
757 Zone2* zoneIntersection(Zone2* pZone0,Zone2* pZone1);
758 
772 CLASS_DECLSPEC
773 Zone2* zoneDifference(Zone2* pZone0,Zone2* pZone1);
774 
788 CLASS_DECLSPEC
790 
791 
792 
793 
794 } // (namespace)
VtkColor
Enumeration of colors used by the VTKWriter.
Definition: VtkWriter.h:39
Zone2 * zoneSymmetricDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the symmetric difference between two Zone2 objects.
Zone2 * zoneDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the difference between two Zone2 objects.
Zone2 * zoneUnion(Zone2 *pZone0, Zone2 *pZone1)
Computes the union of two Zone2 objects.
Zone2 * zoneIntersection(Zone2 *pZone0, Zone2 *pZone1)
Computes the intersection of two Zone2 objects.
OptimizationMode
Definition: Zone2.h:53
@ OPTMODE_STANDARD
Fastest optimization mode.
Definition: Zone2.h:54
@ OPTMODE_BEST
Best quality but quite time consuming.
Definition: Zone2.h:56
@ OPTMODE_BETTER
Considerably better quality and still fast.
Definition: Zone2.h:55
Bbox2 is an axis aligned 2D bounding box.
Definition: Bbox2.h:37
ConstraintGraph2 is a set of Constraint Edges (ConstraintSegment2)
Definition: ConstraintGraph2.h:52
A ConstraintSegment2 represents a Constraint Edge.
Definition: ConstraintSegment2.h:69
MsgBase, a base class for message subscriber classes.
Definition: MsgBase.h:47
User-defined peel predicate.
Definition: UserPredicates.h:60
Point.
Definition: Point2.h:53
Triangle.
Definition: Triangle2.h:60
User-defined predicate (deprecated)
Definition: UserPredicates.h:39
Visualizer2 is a PDF- and Postscript writer.
Definition: Visualizer2.h:55
A writer for the VTK file format.
Definition: VtkWriter.h:62
‍**
Definition: Zone2.h:99
bool hasOnBoundary(const Point2 &p)
Checks if the given point lies on the boundary of the zone.
friend Zone2 * peelOffIf(Zone2 *pZone, UserPredicateT *pPredicate, bool bVerbose)
Peel off border triangles (deprecated)
ConstraintGraph2 * getConstraintGraph() const
Get the associated constraint.
bool save(std::ostream &stream)
Save the zone.
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.
double getArea2D() const
Get 2D Area.
void writeObj(const char *outFilename) const
Write the zone to *.obj Writes the triangles of the present Zone2 to an *.obj file (The *....
size_t numberOfConstraintGraphs() const
Get a the number of ConstraintGraph2 objects.
bool shiftToZone(const Point2 &from, double tolerance, Point2 &result)
Finds a point close to the input point that lies inside the zone.
friend Zone2 * zoneIntersection(Zone2 *pZone0, Zone2 *pZone1)
Computes the intersection of two Zone2 objects.
void getOffsetBoundary(double offset, std::vector< Segment2 > &vOffsetBoundary, double mergeAngleDeg=10.0, double angleStepDeg=20.0)
Get the boundary of an offset shape.
void getTriangles(std::vector< Triangle2 * > &vTriangles_) const
Get the triangles of the zone.
Triangle2 * locate(const Point2 &p)
Locates the triangle containing the given point within the zone.
void getComponentPolygons(std::vector< CompPolygon > &vCompPolygons) const
Get connected components and their boundary polygons.
void show(Visualizer2 *pVisualizer, bool bShowFull, bool bWithConstraints) const
Postscript- and PDF-visualization.
bool hasOn(const Point2 &p)
Checks if the given point lies on the zone or its boundary.
void show(const char *filename, bool bShowFull, bool bWithConstraints) const
Postscript- and PDF-visualization.
void subscribe(MsgType msgType, MsgBase *pMsg)
Register a message receiver.
void getBoundarySegments(std::vector< Segment2 > &vSegments) const
Compute the boundary segments.
void getConstraintGraphs(std::vector< ConstraintGraph2 * > &vConstraintGraphs_) const
Get the associated constraint graphs.
friend Zone2 * zoneSymmetricDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the symmetric 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 exportZone(FadeExport &fadeExport, bool bWithCustomIndices) const
Export triangles from a zone.
friend Zone2 * zoneUnion(Zone2 *pZone0, Zone2 *pZone1)
Computes the union of two Zone2 objects.
void showVtk(VtkWriter *pVtk, VtkColor zoneColor, VtkColor nonZoneColor=VTK_TRANSPARENT, VtkColor constraintColor=VTK_TRANSPARENT) const
VTK visualization.
void unsubscribe(MsgType msgType, MsgBase *pMsg)
Unregister a message receiver.
void debug(const char *name="")
Development function.
size_t getNumberOfTriangles() const
Get the number of triangles.
friend Zone2 * peelOffIf(Zone2 *pZone, bool bAvoidSplit, PeelPredicateTS *pPredicate)
Peel off border triangles.
bool save(const char *filename)
Save the zone.
Zone2 * convertToBoundedZone()
Convert a zone to a bounded zone.
void unifyGrid(double tolerance)
void statistics(const char *s) const
void getBorderEdges(std::vector< Edge2 > &vBorderEdgesOut) const
Get border edges.
ConstraintSegment2 * getNearbyBoundaryConstraint(Point2 &p, double tolerance)
Locates the nearest boundary ConstraintSegment2 of the zone within a specified distance.
friend Zone2 * zoneDifference(Zone2 *pZone0, Zone2 *pZone1)
Computes the difference between two Zone2 objects.
FadeExport is a simple struct to export triangulation data.
Definition: FadeExport.h:48