3D Ray C++ Source Code

The C++ code for the 3D ray implementation consists of a header file (Ray3.hpp) and a source file (Ray3.cpp). The ray has an origin and direction (defined as Vec3 vectors) and various math operations.

Header File (Ray3.hpp)

Ray3.hpp - Click here to download
/******************************************************************************/
/*                                                                            */
/* Copyright 2016-2017 Steven Dolly                                           */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License");            */
/* you may not use this file except in compliance with the License.           */
/* You may obtain a copy of the License at:                                   */
/*                                                                            */
/*     http://www.apache.org/licenses/LICENSE-2.0                             */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/*                                                                            */
/******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// Ray3.hpp                                                                   //
// 3D Ray Class Header File                                                   //
// Created June 1, 2017 (Steven Dolly)                                        //
//                                                                            //
// This header file contains a class for a double-precision three-dimensional //
// ray, with simple math operation functions.                                 //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

// Header guard
#ifndef RAY3_HPP
#define RAY3_HPP

// Standard C header files
#include <cmath>

// Custom headers
#include "Vec3.hpp"

namespace solutio
{
  class Ray3
  {
    public:
      // Default constructor (set to all zeros)
      Ray3();
      // Constructor with origin and direction setter
      Ray3(Vec3<double> o, Vec3<double> d);
      // Ray origin & direction
      Vec3<double> origin, direction;
      // Set functions
      void SetRay(Vec3<double> o, Vec3<double> d);
      // Get functions
      Vec3<double> GetPoint(double t);
      double GetLength();
  };
}

#endif

Source File (Ray3.cpp)

Ray3.cpp - Click here to download
/******************************************************************************/
/*                                                                            */
/* Copyright 2016-2017 Steven Dolly                                           */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License");            */
/* you may not use this file except in compliance with the License.           */
/* You may obtain a copy of the License at:                                   */
/*                                                                            */
/*     http://www.apache.org/licenses/LICENSE-2.0                             */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/*                                                                            */
/******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// Ray3.cpp                                                                   //
// 3D Ray Class Main File                                                     //
// Created June 2, 2017 (Steven Dolly)                                        //
//                                                                            //
// This main file contains a class for a double-precision three-dimensional   //
// ray, with simple math operation functions.                                 //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

// Class header
#include "Ray3.hpp"

namespace solutio
{
  // Default constructor (set to all zeros)
  Ray3::Ray3()
  {
    Vec3<double> o, d;
    SetRay(o, d);
  }
  // Constructor with origin and direction setter
  Ray3::Ray3(Vec3<double> o, Vec3<double> d)
  {
    SetRay(o, d);
  }
  // Set functions
  void Ray3::SetRay(Vec3<double> o, Vec3<double> d)
  {
    origin = o;
    direction = d;
  }
  // Get functions
  Vec3<double> Ray3::GetPoint(double t)
  {
    Vec3<double> point;
    point.x = origin.x + t*direction.x;
    point.y = origin.y + t*direction.y;
    point.z = origin.z + t*direction.z;
    return point;
  }
  
  double Ray3::GetLength()
  {
    return direction.Magnitude();
  }
}

Please do not hesistate to submit any questions or comments you may have about the site by using the Contact page.