CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

math2d.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 1998-2000 by Jorrit Tyberghein
00003     Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_MATH2D_H__
00021 #define __CS_MATH2D_H__
00022 
00030 #include "csextern.h"
00031 
00032 #include "csgeom/plane2.h"
00033 #include "csgeom/segment.h"
00034 #include "csgeom/vector2.h"
00035 
00036 class csBox2;
00037 class csPoly2D;
00038 
00043 class CS_CRYSTALSPACE_EXPORT csMath2
00044 {
00045 public:
00052   static int WhichSide2D (const csVector2& v,
00053                           const csVector2& s1, const csVector2& s2)
00054   {
00055     float k  = (s1.y - v.y)*(s2.x - s1.x);
00056     float k1 = (s1.x - v.x)*(s2.y - s1.y);
00057     if (k < k1) return -1;
00058     else if (k > k1) return 1;
00059     else return 0;
00060   }
00061 
00068   static int WhichSide2D (const csVector2& v,
00069                           const csSegment2& s)
00070   {
00071     return WhichSide2D (v, s.Start (), s.End ());
00072   }
00073 
00081   static int InPoly2D (const csVector2& v,
00082                        csVector2* P, int n, csBox2* bounding_box);
00083 
00089   static float Area2 (const csVector2& a,
00090                       const csVector2& b,
00091                       const csVector2& c)
00092   {
00093     return
00094       a.x * b.y - a.y * b.x +
00095       a.y * c.x - a.x * c.y +
00096       b.x * c.y - c.x * b.y;
00097   }
00098 
00104   static float Right (const csVector2& a,
00105                       const csVector2& b,
00106                       const csVector2& c)
00107   {
00108     return Area2 (a, b, c) <= -SMALL_EPSILON;
00109   }
00110 
00116   static float Left (const csVector2& a,
00117                      const csVector2& b,
00118                      const csVector2& c)
00119   {
00120     return Area2 (a, b, c) >= SMALL_EPSILON;
00121   }
00122 
00128   static bool Visible (const csVector2& p, const csPlane2& pl)
00129   { return pl.Classify (p) <= 0; }
00130 
00137   static bool PlanesEqual (const csPlane2& p1, const csPlane2& p2)
00138   {
00139     return ( ( p1.norm - p2.norm) < (float).001 ) &&
00140              (  ABS (p1.CC-p2.CC) < (float).001 );
00141   }
00142 
00148   static bool PlanesClose (const csPlane2& p1, const csPlane2& p2);
00149 };
00150 
00156 class CS_CRYSTALSPACE_EXPORT csIntersect2
00157 {
00158 public:
00165   static bool PlanePolygon (const csPlane2& plane, csPoly2D* poly,
00166         csSegment2& segment);
00167 
00173   static bool SegmentSegment (
00174     const csSegment2& a, const csSegment2& b,   // Two segments.
00175     csVector2& isect, float& dist);         // intersection point and distance
00176 
00182   static bool SegmentLine (
00183     const csSegment2& a,                // First segment.
00184     const csSegment2& b,                // A line (end is only direction)
00185     csVector2& isect, float& dist);     // intersection point and distance
00186 
00191   static bool LineLine (
00192     // Two lines (end is only direction).
00193     const csSegment2& a, const csSegment2& b,
00194     csVector2& isect);                      // intersection point
00195 
00204   static bool SegmentPlane (
00205     const csVector2& u, const csVector2& v,
00206     const csPlane2& p,                     // plane Ax+By+Cz+D=0
00207     csVector2& isect,                     // intersection point
00208     float& dist);                       // distance from u to isect
00209 
00218   static bool SegmentPlane (
00219     const csSegment2& uv,       // Segment.
00220     const csPlane2& p,                     // plane Ax+By+Cz+D=0
00221     csVector2& isect,                     // intersection point
00222     float& dist)                        // distance from u to isect
00223   {
00224     return SegmentPlane (uv.Start (), uv.End (), p, isect, dist);
00225   }
00226 
00231   static void SegmentPlaneNoTest (const csVector2& u, const csVector2& v,
00232                      const csPlane2& p, csVector2& isect, float& dist)
00233   {
00234     float x,y, denom;
00235     x = v.x-u.x;  y = v.y-u.y;
00236     denom = p.norm.x*x + p.norm.y*y;
00237     dist = -(p.norm*u + p.CC) / denom;
00238     isect.x = u.x + dist*x;  isect.y = u.y + dist*y;
00239   }
00240 
00245   static void SegmentPlaneNoTest (const csSegment2& uv,
00246                      const csPlane2& p, csVector2& isect, float& dist)
00247   {
00248     SegmentPlaneNoTest (uv.Start (), uv.End (), p, isect, dist);
00249   }
00250 
00256   static bool PlanePlane (const csPlane2& p1, const csPlane2& p2,
00257                       csVector2& isect);
00258 
00259 };
00260 
00263 #endif // __CS_MATH2D_H__

Generated for Crystal Space by doxygen 1.4.4