3D Vertices (Points)
Site Map Feedback

Download:

Ode to a Node:
What's the Point?
Up gCoord Points Vectors PV Planes

g3Point and g2Point

g3Point and g2Point are for holding vertices and have public cartesian coordinates, Spherical to Cartesian conversion, distance between points and OnViewPlane which is valuable when rendering.
The operator bool() in g3D and g2D makes it easy to compare the Vertex with the Origin.
  bool    IsOrigin() const;
  void    Move(double dx, double dy, double dz);
  g3Point OnViewPlane(g3Vector& Axis) const; // Returns the Projected point to a plane through the Global Origin having the given Normal Axis.
  static  g3Point FromSpherical(double Radius, double Theta, double Phi);
  void    ToSpherical(double& Radius, double& Theta, double& Phi) const;
  double  GetRadius() const;
  double  GetTheta () const;
  double  GetPhi   () const;
  double  DistanceTo(const g3Point& E) const; // For signed Distances use g3PV::GetDisplacement();

g3D and g2D Base Classes

There are lots of studies about the use of Vector and Vertex classes in graphics libraries.
Most agree that there is only a need for a Vector class and a Vertex can live in a Vector class.
A base class is used to hold the coordinates, and extended to create separate Vector and Point (Vertex) objects.
g2D and g3D are structs which simply hold two or three gCoords.
The default constructor doesn't initialise the gCoords. This behaviour is used throughout the Primitives because huge arrays of points are often used and don't need to be zeroed.
struct g2D { // 2 Doubles (Used as a 2 Dimensional Point or Vector)
  gCoord x,y;
  g2D() {}
  g2D(gCoord x, gCoord y) : x(x), y(y) {}
  g2D(double* DD)                      {x=*DD++; y=*DD;}
  void Clear()                         {x=y=0;}
  g2D& operator*=(const gCoord& c)     {x*=c; y*=c; return *this;}
  g2D& operator/=(const gCoord& c)     {x/=c; y/=c; return *this;}
  bool operator==(const g2D& DD) const {return Compare(DD)==0;}
  bool operator!=(const g2D& DD) const {return Compare(DD)!=0;}
       operator bool()           const {return bool(x)||bool(y);}
  int Compare(const g2D& DD)     const { // For Sorting
    int    Result =          x.Compare(DD.x);
    return Result ? Result : y.Compare(DD.y);
  }
};
To create a g3D, d2D could have been used as its base class, but there are no advantages.
Every function would need redefining anyway, and, for example:
struct g3D : g2D { // 3 Doubles (Used as x 3 Dimensional Point or Vector)
  gCoord z;
  g3D() {}
  g3D(gCoord x, gCoord y, gCoord z) : g2D(x,y), z(z) {}
  g3D(double* DDD) : g2D(DDD)      {z=*++DDD;}
  void Clear()                     {g2D::Clear(); z=0;}
  g3D& operator*=(const gCoord& c) {g2D::operator*=(c); z*=c; return *this;}
the Clear() function is more clearly written as x=y=z=0; as are most of the others.
So 3D objects are not derived from 2D objects.
Since there is now a danger in the redundancy that a change in one class is not also changed in the other,

Note the ToArray(double*) method which copies the data out to an array.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (1671) Last modified: Fri, 15 May 2009 15:26:35 +0100