MeVisLab Toolbox Reference
CSOGeometry.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 
18 
19 #pragma once
20 
21 
22 #include "MLCSOIncludes.h"
23 #include "CSOTools/CSOHeapObject.h"
24 #include "CSOTools/CSOObjectHeap.h"
26 
27 
28 #include <mlRotation.h>
29 
30 ML_START_NAMESPACE
31 
32 
34 
35 class CSO;
36 class CSOList;
37 ML_REFCOUNTED_PTR(CSOList)
38 
39 
45 
46 
47 namespace CSOGeometry
48 {
50  const double maxPointDiff = 10e-6;
51 
53  {
57  };
58 
66  MLCSO_EXPORT bool computeIsInPlane(const CSO* cso, Vector3& planeNormal, double angleEpsilon=1e-3, double collinearEpsilon=1e-3);
68  MLCSO_EXPORT bool computeIsInPlane(const std::vector< Vector3 >& points, Vector3& planeNormal, double angleEpsilon = 1e-3, double collinearEpsilon = 1e-3);
70  MLCSO_EXPORT Vector3 computeNewellsNormal(const std::vector< Vector3 >& points);
75  MLCSO_EXPORT bool arePointsCollinear(const std::vector<Vector3>& points, double epsilon);
76 
79  MLCSO_EXPORT bool isSelfIntersecting(const std::vector<Vector3>& positions, std::vector<Vector3>& intersectPoints, bool isClosed);
81  MLCSO_EXPORT bool isSelfIntersecting(const std::vector<Vector3>& positions, bool isClosed);
82 
86  MLCSO_EXPORT bool isSelfIntersecting(const CSO* cso, std::vector<Vector3>& intersectPositions);
87 
89  MLCSO_EXPORT double computeSegmentSegmentDistance(const Vector3& start0, const Vector3& end0, const Vector3& start1, const Vector3& end1, Vector3& intersectionPoint, bool& isParallel);
91  MLCSO_EXPORT double computeSegmentSegmentDistance(const Vector3& start0, const Vector3& end0, const Vector3& start1, const Vector3& end1, Vector3& intersectionPoint);
92 
95  MLCSO_EXPORT int intersect2DSegments(const Vector2& start0, const Vector2& end0, const Vector2& start1, const Vector2& end1, Vector2& intersection0, Vector2& intersection1);
97  bool isInSegment(const Vector2& point, const Vector2& segmentStart, const Vector2& segmentEnd);
99  MLdouble perp(const Vector2& v0, const Vector2& v1);
100 
102  MLCSO_EXPORT bool areInSamePlane(const CSO* cso0, const CSO* cso1, MLdouble angleEpsilon = 10e-5, MLdouble minPlaneDistanceInMM = 10e-5);
103 
105  MLCSO_EXPORT bool areOverlapping(const CSO* cso0, const CSO* cso1, MLdouble epsilon = 10e-5, MLdouble minPlaneDistanceInMM = 10e-5);
106 
109  MLCSO_EXPORT void rotateAndProject(Rotation& rot, Vector3& voxelPos, bool shouldProject=true);
110 
115  MLCSO_EXPORT void level(CSO* cso);
116 
118  MLCSO_EXPORT void level(std::vector<Vector3>& positions);
119 
121  MLCSO_EXPORT void levelToPlane(CSO& cso, const Vector3& normal, const Vector3& planeAnchorPoint);
122 
125  MLCSO_EXPORT std::vector<Vector2> projectTo2D(const std::vector<Vector3>& points, const Vector3& normal, const Vector3& planeAnchorPoint);
126 
128  MLCSO_EXPORT std::vector<Vector3> backprojectTo3D(const std::vector<Vector2>& points, const Vector3& normal, const Vector3& planeAnchorPoint);
129 
135 
137  MLCSO_EXPORT void translate(CSO* cso, const Vector3& translation);
138 
142  MLCSO_EXPORT bool isPointInsidePolygon(const std::vector<Vector3>& polygonPoints, const Vector3& point);
143 
145  MLCSO_EXPORT void appendUniquePosition(std::vector<Vector3>&positions, Vector3 pos);
146 
150  MLCSO_EXPORT Vector3 getCenterOfGravity(CSO* cso, bool considerSeedPointsExtra=false);
152  MLCSO_EXPORT Vector3 getCenterOfGravity(const std::vector<Vector3>& positions);
153 
155  MLCSO_EXPORT double distanceToLine (const Vector3& point, const Vector3& linePoint1, const Vector3& linePoint2);
156 
158  MLCSO_EXPORT double squaredDistanceToLineSegment(const Vector3& point, const Vector3& linePoint1, const Vector3& linePoint2);
160  MLCSO_EXPORT double squaredDistanceToLineSegment(const Vector3& point, const Vector3& linePoint1, const Vector3& linePoint2, Vector3& resultPoint);
161 
163  MLCSO_EXPORT double squaredDistanceToLineSegment(const Vector2& point, const Vector2& linePoint1, const Vector2& linePoint2);
164 
166  MLCSO_EXPORT void computeClosestSeedPoint(CSO* cso, const Vector3& referencePoint, unsigned int &resultSeedPointIndex);
168  // \param resultPathPointIndex returns the index of the closest path point of the cso.
169  MLCSO_EXPORT void computeClosestPathPoint(CSO* cso, const Vector3& referencePoint, unsigned int &resultPathPointListIndex, unsigned int &resultPathPointIndex, Vector3 &resultPosition);
171  // \param resultPathPointIndex returns the index of the start point of the closest line segment.
172  MLCSO_EXPORT void computeClosestPoint(CSO* cso, const Vector3& referencePoint, unsigned int &resultPathPointListIndex, unsigned int &resultPathPointIndex, Vector3 &resultPosition);
174  MLCSO_EXPORT void computeClosestPoint(const std::vector<Vector3>& pointList, const Vector3& referencePoint, unsigned int& resultPointIndex, Vector3& resultPosition);
176  MLCSO_EXPORT double computeDistance(CSO* cso, const Vector3& referencePoint);
178  MLCSO_EXPORT double computeLength(const CSOPathPointIterator& startIter, const CSOPathPointIterator& endIter);
179 
181  MLCSO_EXPORT double computeDiameter(const CSO* cso);
183  MLCSO_EXPORT double computeDiameter(const std::vector<Vector3>& positions);
184 
187  MLCSO_EXPORT void thinOutCSO(CSO* cso, float quality=0.5f, ReductionMode reductionMode=REDUCTION_MODE_ANGLE_AND_LENGTH, unsigned int minPoints=5, float maxAngleLimit=1.0);
190  MLCSO_EXPORT void thinOutPointList(std::vector<Vector3>& positionList, float quality=0.5f, ReductionMode reductionMode=REDUCTION_MODE_ANGLE_AND_LENGTH, unsigned int minPoints=5, float maxAngleLimit=1.0);
191 
193  MLCSO_EXPORT bool csoIsEnclosedByCSO(CSO* toTestInnerCSO, CSO* toTestOuterCSO);
198  MLCSO_EXPORT size_t getLevelOfEmbedding(const CSOListPtr& csoList, std::vector<size_t>& result);
201  MLCSO_EXPORT size_t getLevelOfEmbedding(const std::vector<CSO*>& csos, std::vector<size_t>& result, std::vector<std::vector<size_t> >& enclosedCSOIndices);
205  MLCSO_EXPORT void groupByLevelOfEmbedding(const std::vector<CSO*>& csos, std::vector<std::vector<CSO*> >& groupedCSOs);
206 
208  MLCSO_EXPORT double computeSumOfAreas(const CSOListPtr& csoList);
211  MLCSO_EXPORT double computeVolume(const CSOListPtr& csoList, double sliceThickness);
212 
219  MLCSO_EXPORT bool isApproximatelyParallel(const CSO* cso, const Vector3& normal, double epsilon=0.1);
220 
222  template<class T>
223  inline bool isSamePosition(const T& pos1, const T& pos2)
224  {
225  static_assert(std::is_same_v<T, Vector3> || std::is_same_v<T, Vector4>, "Need Vector3 or Vector4 type");
226  return ((fabs(pos1[0] - pos2[0]) < maxPointDiff) &&
227  (fabs(pos1[1] - pos2[1]) < maxPointDiff) &&
228  (fabs(pos1[2] - pos2[2]) < maxPointDiff));
229  }
230 
232 
234  const unsigned int INVALID_INDEX = static_cast<unsigned int>(-1);
235 
237 
238  class PositionObj : public CSOHeapObject
239  {
240  public:
241  double angle;
245  };
246 
248 
250  {
258 
259  public:
265 
266  private:
267  CSOPlaneGuard& operator=(const CSOPlaneGuard&) { return *this; }
268  CSO& _cso;
269  bool _shouldPreservePlane;
270  Vector3 _planeNormal;
271  Vector3 _planeAnchorPoint;
272  };
273 }
274 
276 
277 ML_END_NAMESPACE
#define MLCSO_EXPORT
Defines export symbols for classes, so they can be used in other DLLs.
Definition: MLCSOSystem.h:23
@ T
Definition: SoKeyGrabber.h:71
CSOHeapObject: base class for objects to be used with the heap.
Definition: CSOHeapObject.h:25
MLCSO_EXPORT void disable()
disables the plane guard, so that level method is not called.
MLCSO_EXPORT CSOPlaneGuard(CSO &cso)
Small helper class that can be used to keep a CSO in its original plane.
The CSO represents a contour segmentation object.
Definition: CSO.h:44
Class to handle Rotations (internally the rotation is stored as a unit quaternion)
Definition: mlRotation.h:40
#define ML_REFCOUNTED_PTR(CLASSNAME)
Macro that defines convenience Ptr/ConstPtr typedefs to be used instead of intrusive_ptr templates.
double MLdouble
Definition: mlTypeDefs.h:223
MLCSO_EXPORT void translate(CSO *cso, const Vector3 &translation)
Translates a CSO with all its seed- and path points by the given translation vector.
MLCSO_EXPORT double computeVolume(const CSOListPtr &csoList, double sliceThickness)
Returns the sum of areas of all CSOs in csoList, multiplied by the given sliceThickness,...
MLCSO_EXPORT bool areInSamePlane(const CSO *cso0, const CSO *cso1, MLdouble angleEpsilon=10e-5, MLdouble minPlaneDistanceInMM=10e-5)
Returns whether the two given CSOs lie in the same plane.
@ REDUCTION_MODE_ANGLE_AND_LENGTH
Definition: CSOGeometry.h:56
MLCSO_EXPORT bool computeIsInPlane(const std::vector< Vector3 > &points, Vector3 &planeNormal, double angleEpsilon=1e-3, double collinearEpsilon=1e-3)
Returns whether the given positions form a contour that is in-plane.
MLCSO_EXPORT void appendUniquePosition(std::vector< Vector3 > &positions, Vector3 pos)
Appends a new point to the given vector without double insertions.
MLCSO_EXPORT void levelToPlane(CSO &cso, const Vector3 &normal, const Vector3 &planeAnchorPoint)
Projects all seed points and all path points to the plane that is defined by the given normal and pla...
MLCSO_EXPORT void computeClosestSeedPoint(CSO *cso, const Vector3 &referencePoint, unsigned int &resultSeedPointIndex)
Returns the closest point on a cso to the given reference point.
MLCSO_EXPORT double computeSumOfAreas(const CSOListPtr &csoList)
Returns the sum of areas of all CSOs in csoList. Area is counted negative if level of embedding is od...
bool isInSegment(const Vector2 &point, const Vector2 &segmentStart, const Vector2 &segmentEnd)
Helper function for intersect2DSegments.
MLCSO_EXPORT std::vector< Vector3 > backprojectTo3D(const std::vector< Vector2 > &points, const Vector3 &normal, const Vector3 &planeAnchorPoint)
Back-projects the 2D points to 3D by rotating from (0,0,1) to the normal vector and adding planeAncho...
MLdouble perp(const Vector2 &v0, const Vector2 &v1)
Helper function for intersect2DSegments.
MLCSO_EXPORT double computeSegmentSegmentDistance(const Vector3 &start0, const Vector3 &end0, const Vector3 &start1, const Vector3 &end1, Vector3 &intersectionPoint)
/deprecated
MLCSO_EXPORT void rotateAndProject(Rotation &rot, Vector3 &voxelPos, bool shouldProject=true)
Transforms and projects a voxelPos according to a rotation.
MLCSO_EXPORT void groupByLevelOfEmbedding(const std::vector< CSO * > &csos, std::vector< std::vector< CSO * > > &groupedCSOs)
Arranges the given CSOs into a vector of vectors so that CSOs embedded into each other are grouped to...
MLCSO_EXPORT std::vector< Vector2 > projectTo2D(const std::vector< Vector3 > &points, const Vector3 &normal, const Vector3 &planeAnchorPoint)
Projects the 3D points to 2D by subtracting planeAnchorPoint, rotating from the normal vector to (0,...
MLCSO_EXPORT bool isPointInsidePolygon(const std::vector< Vector3 > &polygonPoints, const Vector3 &point)
Returns whether a point is inside the polygon defined by the given list of positions.
const unsigned int INVALID_INDEX
A symbolic value that indicates an invalid index (-1).
Definition: CSOGeometry.h:234
MLCSO_EXPORT void thinOutCSO(CSO *cso, float quality=0.5f, ReductionMode reductionMode=REDUCTION_MODE_ANGLE_AND_LENGTH, unsigned int minPoints=5, float maxAngleLimit=1.0)
Thins out the given cso by removing all positions that do not contribute to the overall geometry (ang...
MLCSO_EXPORT void computeClosestPoint(const std::vector< Vector3 > &pointList, const Vector3 &referencePoint, unsigned int &resultPointIndex, Vector3 &resultPosition)
Returns the closest point on the given list of positions.
MLCSO_EXPORT double squaredDistanceToLineSegment(const Vector2 &point, const Vector2 &linePoint1, const Vector2 &linePoint2)
Computes the squared distance of point to line segment given by two other points.
MLCSO_EXPORT double distanceToLine(const Vector3 &point, const Vector3 &linePoint1, const Vector3 &linePoint2)
Computes distance of point to (infinite) line given by two other points:
MLCSO_EXPORT void level(std::vector< Vector3 > &positions)
Levels the given positions so that they all lie on the average plane of the input positions.
MLCSO_EXPORT size_t getLevelOfEmbedding(const std::vector< CSO * > &csos, std::vector< size_t > &result, std::vector< std::vector< size_t > > &enclosedCSOIndices)
Extension of getLevelOfEmbedding which also records the indices of the CSOs enclosed by each CSO.
MLCSO_EXPORT void computeClosestPathPoint(CSO *cso, const Vector3 &referencePoint, unsigned int &resultPathPointListIndex, unsigned int &resultPathPointIndex, Vector3 &resultPosition)
Returns the closest path point of a cso to the given reference point without interpolation.
MLCSO_EXPORT bool isSelfIntersecting(const CSO *cso, std::vector< Vector3 > &intersectPositions)
Returns whether the given contour intersects itself. Fills up the given intersectPositions vector.
MLCSO_EXPORT double computeDistance(CSO *cso, const Vector3 &referencePoint)
Returns the distance of the given point to the given CSO .
MLCSO_EXPORT double computeDiameter(const std::vector< Vector3 > &positions)
Computes the maximum distance between any two points in the given vector.
MLCSO_EXPORT double getLargestDistanceBetweenAllPathPoints(CSO &cso, Vector3 &point0, Vector3 &point1)
Returns the largest distance between any of the path points of the CSO.
const double maxPointDiff
Maximum distance of 2 points in each axis to be considered equal.
Definition: CSOGeometry.h:50
MLCSO_EXPORT void thinOutPointList(std::vector< Vector3 > &positionList, float quality=0.5f, ReductionMode reductionMode=REDUCTION_MODE_ANGLE_AND_LENGTH, unsigned int minPoints=5, float maxAngleLimit=1.0)
Thins out the given list of positions by removing all positions that do not contribute to the overall...
MLCSO_EXPORT bool areOverlapping(const CSO *cso0, const CSO *cso1, MLdouble epsilon=10e-5, MLdouble minPlaneDistanceInMM=10e-5)
Returns whether any path point of cso0 is within cso1 or vice versa.
bool isSamePosition(const T &pos1, const T &pos2)
Returns true if the two positions are considered equal as points of a CSO.
Definition: CSOGeometry.h:223
MLCSO_EXPORT double computeLength(const CSOPathPointIterator &startIter, const CSOPathPointIterator &endIter)
Computes the length of a CSO between two PathPointIterators.
MLCSO_EXPORT bool csoIsEnclosedByCSO(CSO *toTestInnerCSO, CSO *toTestOuterCSO)
Returns whether the first given CSO lies fully within the second given CSO without crossing.
MLCSO_EXPORT int intersect2DSegments(const Vector2 &start0, const Vector2 &end0, const Vector2 &start1, const Vector2 &end1, Vector2 &intersection0, Vector2 &intersection1)
Computes whether the two given 2D segments intersect; if they intersect, the intersection point is va...
MLCSO_EXPORT bool isApproximatelyParallel(const CSO *cso, const Vector3 &normal, double epsilon=0.1)
Returns whether the given CSO is approximately parallel to a plane given by the normal vector.
MLCSO_EXPORT Vector3 getCenterOfGravity(const std::vector< Vector3 > &positions)
Returns the center of gravity of the given positions.
MLCSO_EXPORT bool arePointsCollinear(const std::vector< Vector3 > &points, double epsilon)
Returns whether all points are collinear.
MLCSO_EXPORT Vector3 computeNewellsNormal(const std::vector< Vector3 > &points)
Returns a normal for the given list of points using Newell's method.