I still have 2 problems:

- the first one is that when I declare a Ray instance I have to specify the type (and, at this point, I wonder if it is not better to use simple inheritance)
- the second one is that the 2D specific methods are available also for a 3D class instance, although not implemented. This is my code

```
#pragma once
#include "ofMain.h"
namespace ofxraycaster {
template<class T>
class Ray {
public:
Ray(){};
Ray(T _origin, T _direction){
origin = _origin;
direction = glm::normalize(_direction);
};
void setup(T _origin, T _direction){
origin = _origin;
direction = glm::normalize(_direction);
};
// generic methods
T getOrigin() const {
return origin;
};
void setOrigin(T _origin){
origin = _origin;
};
T getDirection() const {
return direction;
};
void setDirection(T _direction){
direction = _direction;
};
// Returns the linear-interpolation from the ray origin along its direction vector where t is in the range from 0 to infinite.
T lerp(const float t) const {
auto result = direction;
result *= t;
result += origin;
return result;
}
void draw(float radius = 20.){
ofPushStyle();
// draw origin
ofSetColor(255, 0, 0);
ofDrawSphere(origin.x, origin.y, radius);
// draw direction
ofSetColor(0,0,255);
auto end = origin + (direction * (radius*4.));
ofSetLineWidth(3);
ofDrawLine(origin,end);
ofSetLineWidth(1);
ofPopStyle();
};
// 2D specific methods
void intersectsSegment(glm::vec2 a, glm::vec2 b, glm::vec2& intersection, bool& intersects);
void intersectsPolyline(const ofPolyline& poly, glm::vec2& intersection, bool& intersects);
// 3D specific methods
// TODO intersectSphere
private:
T origin;
T direction;
};
}// end namespace
```

This is the Ray.cpp class, where i put the template specializations:

```
#include "Ray.h"
// 2D methods
template<>
void ofxraycaster::Ray<glm::vec2>::intersectsSegment(glm::vec2 a, glm::vec2 b, glm::vec2& intersection, bool& intersects){
//implementation
};
template<>
void ofxraycaster::Ray<glm::vec2>::intersectsPolyline(const ofPolyline& poly, glm::vec2& intersection, bool& intersects){
//implementation
};
```

And whenever I need to declare an instance variable for a 2D ray I use:

```
ofxraytracer::Ray<glm::vec2> ray
```

I’m interested using templates in this case because I think it is the best solution for this situation where there are a lot of things in common between a 2D and a 3D ray, and some small things that does not make any sense in 2D world, and viceversa.