Commit f0705dc23ccde2cde898c0fe378c42ef7f205dd6

sammy 2008-05-01T10:54:28

* Inline most FTPoint methods and operators. This will probably make the code smaller instead of bigger, because most of what they do will be optimised out by the compiler. * Get rid of the weird GetNormal() method and reimplement it using the ^ operator (vector product) and Normalise() method instead.

diff --git a/src/FTGL/FTPoint.h b/src/FTGL/FTPoint.h
index 09a861c..a95e858 100644
--- a/src/FTGL/FTPoint.h
+++ b/src/FTGL/FTPoint.h
@@ -43,7 +43,7 @@ class FTGL_EXPORT FTPoint
         /**
          * Default constructor. Point is set to zero.
          */
-        FTPoint()
+        inline FTPoint()
         {
             values[0] = 0;
             values[1] = 0;
@@ -57,8 +57,8 @@ class FTGL_EXPORT FTPoint
          * @param y Second component
          * @param z Third component
          */
-        FTPoint(const FTGL_DOUBLE x, const FTGL_DOUBLE y,
-                const FTGL_DOUBLE z = 0)
+        inline FTPoint(const FTGL_DOUBLE x, const FTGL_DOUBLE y,
+                       const FTGL_DOUBLE z = 0)
         {
             values[0] = x;
             values[1] = y;
@@ -66,11 +66,11 @@ class FTGL_EXPORT FTPoint
         }
 
         /**
-         * Constructor. This converts an FT_Vector to an FT_Point
+         * Constructor. This converts an FT_Vector to an FTPoint
          *
          * @param ft_vector A freetype vector
          */
-        FTPoint(const FT_Vector& ft_vector)
+        inline FTPoint(const FT_Vector& ft_vector)
         {
             values[0] = ft_vector.x;
             values[1] = ft_vector.y;
@@ -78,14 +78,12 @@ class FTGL_EXPORT FTPoint
         }
 
         /**
-         * Calculate the normal vector to 2 points. This is 2D and ignores
-         * the z component. The normal will be normalised
+         * Normalise a point's coordinates. If the coordinates are zero,
+         * the point is left untouched.
          *
-         * @param a
-         * @param b
-         * @return
+         * @return A vector of norm one.
          */
-        static FTPoint GetNormal(const FTPoint &a, const FTPoint &b);
+        FTPoint Normalise();
 
 
         /**
@@ -94,7 +92,7 @@ class FTGL_EXPORT FTPoint
          * @param point
          * @return this plus point.
          */
-        FTPoint& operator += (const FTPoint& point)
+        inline FTPoint& operator += (const FTPoint& point)
         {
             values[0] += point.values[0];
             values[1] += point.values[1];
@@ -109,7 +107,7 @@ class FTGL_EXPORT FTPoint
          * @param point
          * @return this plus point.
          */
-        FTPoint operator + (const FTPoint& point)
+        inline FTPoint operator + (const FTPoint& point)
         {
             FTPoint temp;
             temp.values[0] = values[0] + point.values[0];
@@ -125,7 +123,7 @@ class FTGL_EXPORT FTPoint
          * @param point
          * @return this minus point.
          */
-        FTPoint& operator -= (const FTPoint& point)
+        inline FTPoint& operator -= (const FTPoint& point)
         {
             values[0] -= point.values[0];
             values[1] -= point.values[1];
@@ -140,7 +138,7 @@ class FTGL_EXPORT FTPoint
          * @param point
          * @return this minus point.
          */
-        FTPoint operator - (const FTPoint& point)
+        inline FTPoint operator - (const FTPoint& point)
         {
             FTPoint temp;
             temp.values[0] = values[0] - point.values[0];
@@ -156,7 +154,7 @@ class FTGL_EXPORT FTPoint
          * @param multiplier
          * @return <code>this</code> multiplied by <code>multiplier</code>.
          */
-        FTPoint operator * (double multiplier)
+        inline FTPoint operator * (double multiplier)
         {
             FTPoint temp;
             temp.values[0] = values[0] * multiplier;
@@ -174,11 +172,33 @@ class FTGL_EXPORT FTPoint
          * @param multiplier
          * @return <code>multiplier</code> multiplied by <code>point</code>.
          */
-        friend FTPoint operator * (double multiplier, FTPoint& point);
+        inline friend FTPoint operator * (double multiplier, FTPoint& point)
+        {
+            return point * multiplier;
+        }
+
+
+        /**
+         * Operator ^  Vector product
+         *
+         * @param point Second point
+         * @return this vector point.
+         */
+        inline FTPoint operator ^ (const FTPoint& point)
+        {
+            FTPoint temp;
+            temp.values[0] = values[1] * point.values[2]
+                           - values[2] * point.values[1];
+            temp.values[1] = values[2] * point.values[3]
+                           - values[3] * point.values[2];
+            temp.values[2] = values[3] * point.values[0]
+                           - values[0] * point.values[3];
+            return temp;
+        }
 
 
         /**
-         * Operator == Tests for eqaulity
+         * Operator == Tests for equality
          *
          * @param a
          * @param b
@@ -186,6 +206,7 @@ class FTGL_EXPORT FTPoint
          */
         friend bool operator == (const FTPoint &a, const FTPoint &b);
 
+
         /**
          * Operator != Tests for non equality
          *
@@ -199,7 +220,7 @@ class FTGL_EXPORT FTPoint
         /**
          * Cast to FTGL_DOUBLE*
          */
-        operator const FTGL_DOUBLE*() const
+        inline operator const FTGL_DOUBLE*() const
         {
             return values;
         }
@@ -208,17 +229,17 @@ class FTGL_EXPORT FTPoint
         /**
          * Setters
          */
-        void X(FTGL_DOUBLE x) { values[0] = x; };
-        void Y(FTGL_DOUBLE y) { values[1] = y; };
-        void Z(FTGL_DOUBLE z) { values[2] = z; };
+        inline void X(FTGL_DOUBLE x) { values[0] = x; };
+        inline void Y(FTGL_DOUBLE y) { values[1] = y; };
+        inline void Z(FTGL_DOUBLE z) { values[2] = z; };
 
 
         /**
          * Getters
          */
-        FTGL_DOUBLE X() const { return values[0]; };
-        FTGL_DOUBLE Y() const { return values[1]; };
-        FTGL_DOUBLE Z() const { return values[2]; };
+        inline FTGL_DOUBLE X() const { return values[0]; };
+        inline FTGL_DOUBLE Y() const { return values[1]; };
+        inline FTGL_DOUBLE Z() const { return values[2]; };
 
     private:
         /**
diff --git a/src/FTGlyph/FTExtrudeGlyph.cpp b/src/FTGlyph/FTExtrudeGlyph.cpp
index e8b9318..3929213 100644
--- a/src/FTGlyph/FTExtrudeGlyph.cpp
+++ b/src/FTGlyph/FTExtrudeGlyph.cpp
@@ -230,16 +230,16 @@ void FTExtrudeGlyphImpl::RenderSide()
                 unsigned int next = (cur == n - 1) ? 0 : cur + 1;
 
                 FTPoint frontPt = contour->FrontPoint(cur);
+                FTPoint nextPt = contour->FrontPoint(next);
                 FTPoint backPt = contour->BackPoint(cur);
 
-                FTPoint normal = FTPoint::GetNormal(frontPt, contour->FrontPoint(next));
+                FTPoint normal = FTPoint(0.f, 0.f, 1.f) ^ (frontPt - nextPt);
                 if(normal != FTPoint(0.0f, 0.0f, 0.0f))
                 {
-                    glNormal3dv(static_cast<const FTGL_DOUBLE*>(normal));
+                    glNormal3dv(static_cast<const FTGL_DOUBLE*>(normal.Normalise()));
                 }
 
-                glTexCoord2f(frontPt.X() / hscale,
-                             frontPt.Y() / vscale);
+                glTexCoord2f(frontPt.X() / hscale, frontPt.Y() / vscale);
 
                 if(contourFlag & ft_outline_reverse_fill)
                 {
diff --git a/src/FTPoint.cpp b/src/FTPoint.cpp
index dbb9a2b..6fd38ee 100644
--- a/src/FTPoint.cpp
+++ b/src/FTPoint.cpp
@@ -34,35 +34,24 @@ bool operator == (const FTPoint &a, const FTPoint &b)
     return((a.values[0] == b.values[0]) && (a.values[1] == b.values[1]) && (a.values[2] == b.values[2]));
 }
 
+
 bool operator != (const FTPoint &a, const FTPoint &b)
 {
     return((a.values[0] != b.values[0]) || (a.values[1] != b.values[1]) || (a.values[2] != b.values[2]));
 }
 
 
-FTPoint operator*(double multiplier, FTPoint& point)
-{
-    return point * multiplier;
-}
-
-FTPoint FTPoint::GetNormal(const FTPoint &a, const FTPoint &b)
+FTPoint FTPoint::Normalise()
 {
-    float vectorX = a.X() - b.X();
-    float vectorY = a.Y() - b.Y();
-
-    float length = sqrt(vectorX * vectorX + vectorY * vectorY);
-
-    if(length > 0.01f)
-    {
-        length = 1 / length;
-    }
-    else
+    float norm = sqrt(values[0] * values[0]
+                       + values[1] * values[1]
+                       + values[2] * values[2]);
+    if(norm == 0.f)
     {
-        length = 0.0f;
+        return *this;
     }
 
-    return FTPoint(-vectorY * length,
-                    vectorX * length,
-                    0.0f);
+    FTPoint temp(values[0] / norm, values[1] / norm, values[2] / norm);
+    return temp;
 }