00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef VRS_ALGORITHM_H
00021 #define VRS_ALGORITHM_H
00022
00023 #include <vrs/matrix.h>
00024 #include <vrs/vector.h>
00025 #include <vrs/bounds.h>
00026 #include <vrs/container/packedvertexiterator.h>
00027
00028 namespace VRS {
00029
00030 class Disc;
00031 class PolygonSet;
00032 class Ray;
00033 class Sphere;
00034 class Facet;
00035
00036
00037
00039 template<unsigned int DIM_X, unsigned int DIM_Y, unsigned int DIM_Z>
00040 class VRS_CORE_API AlgorithmImpl {
00041 public:
00044 static SO<PolygonSet> convexHull3D(
00045 SO<Iterator<Vector> > points
00046 );
00047 static SO<Iterator<Vector> > convexHull2D( SO<Iterator<Vector> > points );
00048 static SO<Iterator<Vector> > convexHull2D(
00049 SO<Iterator<Vector> > points,
00050 const Vector& planeNormal,
00051 const Vector& onPlane = Vector::origin
00052 );
00056 static Vector projectOntoPlane(
00057 const Vector& point,
00058 const Vector& planeNormal,
00059 const Vector& onPlane = Vector(0,0,0)
00060 );
00061 static SO<Iterator<Vector> > projectOntoPlane(
00062 SO<Iterator<Vector> > points,
00063 const Vector& planeNormal,
00064 const Vector& onPlane = Vector(0,0,0)
00065 );
00066
00067 static bool intersectPlanes(
00068 const Vector& planeNormal1, const Vector& onPlane1,
00069 const Vector& planeNormal2, const Vector& onPlane2,
00070 const Vector& planeNormal3, const Vector& onPlane3,
00071 Vector& intersectionPoint
00072 );
00079 static bool linePlaneIntersection(
00080 const Vector& planeNormal,
00081 const Vector& onPlane,
00082 const Vector& a,
00083 const Vector& b,
00084 Vector& intersectionPoint,
00085 bool checkSection = true
00086 );
00097 static bool rayTriangleIntersection(
00098 const Ray& ray,
00099 const Vector& t1, const Vector& t2, const Vector& t3,
00100 Vector& intersectionPoint
00101 );
00105 static double signedDistance(const Vector& planeNormal, const Vector& onPlane, const Vector& p);
00109 static SO<Iterator<Vector> > transform(SO<Iterator<Vector> > points, const Matrix& matrix);
00112 static SO<PackedVertexIterator> translate(
00113 SO<Iterator<Vector> > vertices,
00114 const Vector& offset
00115 );
00120 static Bounds boundingBox(SO<Iterator<Vector> > vertices);
00123 static bool pointInPolygon(SO<Iterator<Vector> > polygon, const Vector& p);
00127 static bool polygonInPolygon(SO<Iterator<Vector> > polygon1, SO<Iterator<Vector> > polygon2);
00132 static Vector projectOntoLine(const Vector& p, const Vector& a, const Vector& b, double edgeLengthHint = -1.0);
00138 static Vector nearestPointOnSegment(const Vector& p, const Vector& a, const Vector& b, double edgeLengthHint = -1.0);
00143 static double pointLineDistance(const Vector& p, const Vector& a, const Vector& b, double edgeLengthHint = -1.0);
00148 static bool collinear(
00149 const Vector& a1,
00150 const Vector& b1,
00151 const Vector& a2,
00152 const Vector& b2,
00153 double errorTolerance = 1.e-10
00154 );
00160 static bool parallel(
00161 const Vector& a1,
00162 const Vector& b1,
00163 const Vector& a2,
00164 const Vector& b2,
00165 double cosMaxAngle = 1. - 1.e-10
00166 );
00171 static Vector normal(SO<Iterator<Vector> > polygon);
00176 static double signedArea(SO<Iterator<Vector> > polygon);
00182 static bool counterclockwise(SO<Iterator<Vector> > polygon);
00185 static SO<Iterator<Vector> > orientLoop(SO<Iterator<Vector> > loop, bool ccw = true);
00192 static Vector bisectorRay(const Vector& a, const Vector& b, const Vector& c);
00200 static bool intersectLines(
00201 const Vector& a1,
00202 const Vector& b1,
00203 const Vector& a2,
00204 const Vector& b2,
00205 Vector& intersectionPoint,
00206 bool ignoreSection = true,
00207 double cosMaxAngle = 1. - 1.e-10
00208 );
00218
00219
00220
00221
00222 enum LineSection {
00223 SCR_BEFORE_A,
00224 SCR_AT_A,
00225 SCR_BETWEEN_AB,
00226 SCR_AT_B,
00227 SCR_AFTER_B,
00228 };
00229 static LineSection CheckLineSection(const Vector& a, const Vector& b, const Vector& p, double tolerance = -1.0);
00230
00231 static double calculateAngleCCW(const Vector& a, const Vector& b, const Vector& c);
00236
00237
00238
00239 static double calculateAngleCCW(const Vector& a, const Vector& b);
00240
00243 static Vector rotateCCW(const Vector& a, double angle);
00244
00247 static double forceAngleToRange(double angle);
00248
00251 static bool anglesAreCCWOrdered(double alpha, double beta, double gamma);
00252
00253 static Vector findPerpendicularVector(const Vector& v);
00257 static SO<Iterator<Vector> > boundingRectangle(SO<Iterator<Vector> > polygon);
00258 static SO<Iterator<Vector> > boundingRectangle(const Vector& a, const Vector& b, const Vector& c);
00265 static SO<Iterator<SO<Iterator<SO<Iterator<Vector > > > > > >
00266 clipPolygons(SO<Iterator<SO<Iterator<Vector> > > > sp,
00267 SO<Iterator<SO<Iterator<Vector> > > > cp);
00275
00276
00277
00278
00279 static SO<Iterator<Vector> > clipConvexPolygons(
00280 SO<Iterator<Vector> > poly1,
00281 SO<Iterator<Vector> > poly2
00282 );
00283
00286 static Vector nearestPoint(const Vector& point, const Bounds& bounds);
00287
00290 static Vector furthestPoint(const Vector& point, const Bounds& bounds);
00291
00296 static SO<Iterator<Vector> > expandLoop(SO<Iterator<Vector> > loop, double delta);
00297
00300 static Vector centerOfLoop(SO<Iterator<Vector> > loop);
00301
00310 static Matrix quadrilateralToQuadTransform(
00311 const Vector& t0,
00312 const Vector& t1,
00313 const Vector& t2,
00314 const Vector& t3,
00315 const Vector& min = Vector(-1.0, -1.0),
00316 const Vector& max = Vector( 1.0, 1.0)
00317 );
00318
00321 static Matrix rotateToXZPlane(const Vector& planeNormal);
00322
00327 static Vector getEdgeNormal(const Vector& a, const Vector& b, const Vector& c, bool xyPlane, bool normalized = true);
00328
00332 static void growTriangle(Vector& a, Vector& b, Vector& c, int pixels, int atlasSize, bool xyPlane);
00333
00335 static bool intersectionTest(
00336 const Vector& a1, const Vector& b1, const Vector& c1,
00337 const Vector& a2, const Vector& b2, const Vector& c2,
00338 bool xyPlane
00339 );
00340
00344 static bool computeVertexTf(
00345 const Vector& a1, const Vector& a2, const Vector& a3,
00346 const Vector& b1, const Vector& b2, const Vector& b3,
00347 Matrix& result
00348 );
00349
00356 static Vector polar2Cartesian(const Vector& p, const Vector& up = Vector(0,1,0));
00357 static Vector cartesian2Polar(const Vector& p, const Vector& up = Vector(0,1,0));
00358
00374 static SO<Iterator<Vector> > generatePoissonDiskDistribution(SO<Facet> polygon, double radius);
00375 static SO<Iterator<Vector> > generatePoissonDiskDistribution(const Bounds& bounds, double radius, bool tiling = false);
00376
00377 static SO<Iterator<Vector> > singleStepPoissonDiskDistribution(SO<Facet> polygon, double radius, bool clear = false);
00378 static SO<Iterator<Vector> > singleStepPoissonDiskDistribution(const Bounds& bounds, double radius, bool tiling, bool clear = false);
00379
00380
00384 static bool IntersectCircles(
00385 const VRS::Vector& m0, double radius0,
00386 const VRS::Vector& m1, double radius1,
00387 VRS::Vector& firstIntersection,
00388 VRS::Vector& secondIntersection
00389 );
00390
00395 static bool LineSphereIntersection(
00396 const Vector& m, double r,
00397 const Vector& a, const Vector& b,
00398 Vector& firstIntersection,
00399 Vector& secondIntersection
00400 );
00401 };
00402
00403 typedef AlgorithmImpl<0,2,1> AlgorithmXZY;
00404 typedef AlgorithmImpl<0,1,2> AlgorithmXYZ;
00405 typedef AlgorithmXZY Algorithm;
00406
00407 }
00408
00409 #endif // VRS_ALGORITHM_H