Is it worth to write classes representing 1D, 2D, 3D points using templates
template <class T>
class Point2D
{
protected:
T X, Y;
public:
Point2D(const T x, const T y) : hot smileyx), Y(y)) {}
...
};
template <class T>
class Point3D : public Point2D<T>
{
protected:
T Z;
public:
Point3D(const T x, const T y, const T z) : Point2D<T>(x,y), Z(z) {}
...
};
...or using this approach:
class Point2D
{
protected:
double X, Y;
public:
Point2D(const double x, const double y) : X(x), Y(y)) {}
...
};
class Point3D : public Point2D
{
protected:
double Z;
public:
Point3D(const double x, const double y, const double z) : Point2D(x,y), Z(z) {}
...
};
We understand the coordinates as continuous variables, so it makes sense to express them using double values. A similar situation arises when working with matrices. However in this case templates are widely used...
This class is not only for single use, but it will be part of the library... And my second part of the question. How some "measure" functions should be implemented?
template <class T>
T getDist(const Point2D <T> * p1, const Point2D<T> *p2)
{
....
}
or
double getDist(const Point2D <T> * p1, const Point2D<T> *p2)
{
....
}
Is it reasonable to write such function in general or for some specific type?
Why repeat yourself? Most of the content of those classes and functions will be the same.
Something like this works much better:
template <std::size_T N, typename T>
class Point
{
public:
Point()
{
std::fill_n(mData, N, T());
}
explicit Point(const T& pX) :
mData[0](pX)
{
// or some variant (enable_if also works)
static_assert(N == 1, "X constructor only usable in 1D");
}
explicit Point(const T& pX, const T& pY) :
mData[0](pX),
mData[1](pY),
{
static_assert(N == 2, "XY constructor only usable in 2D");
}
// incomplete, left as exercise for reader. :P
private:
T mData[N];
};
And you just use loops for the functions:
template <std::size_T N, typename T>
T getDist(const Point<N, T>& pFirst, const Point<N, T>& pSecond)
{
// generic, compiler will unroll loops
}
Based on my experience, I'd avoid like the plague creating a class hierarchy where a 3D point IS-A 2D point. It makes it far too easy for 3D points to unintentionally and silently be treated as 2D points. Better to keep coordinates of different dimensionality distinct types (as per GMan's answer) and require explicit coercion between types.
My current favourite implementation of this sort of thing is provided by Eigen. Even if it's license (LGPL) limits it's use to you, I'd suggest taking a look at it's elegantly templated vector and matrix types and learning from them.
It looks OK to me, provided that all functions are implemented correctly and efficiently. For distance measurement:
template <class T>
T getDist(const Point3D <T>& p1, const Point3D<T>& p2);
signature should be used. Distances can be in 3D also.
I've run into this situation, and had cause to allow different types for the X and Y values, where X was sometimes integral but Y always had higher precision (float or double depending on usage). Might be worth considering that upfront.
Some consideration should also be made on how the class will be used. You mentioned matrices. If you plan on solving systems using that class or anything else incolving matrix inversions (or other similar matrix operations) representing a matrix as a bunch of integers would be outlandish.
If you plan to use it as simply a table of sorts (or are only going to be adding/subtracting and multiplying the matrices) then you could template the class because a matrix full of integers would not provide weird (and incorrect) results for those operations.
Related
If I have 2 constructor overloads
calculations(double vector, double angle);
calculations(double horizontalVector, double verticalVector);
How can I ensure the compiler specifically uses one of the overloads that I choose (as each of them do different things behind the scenes)?
If you want to have overloads, the types needs to be different. One way to do this is the so called whole value idiom.
Make a struct for each parameters: a vector (watching out for potential name clashes), angle and so on.
The you will have two different constructors.
Assuming that your parameters are actually polar and cartesian coordinates rather than vectors,
calculations(double length, double angle);
calculations(double x_coordinate, double y_coordinate);
you can abstract those into types,
struct Polar { double length, angle; };
struct Cartesian {double x, y; };
and overload
calculations(const Polar& p);
calculations(const Cartesian& c);
and then
calculations c1(Polar{1,1});
calculations c2(Cartesian{1,1});
To provide different functions with same parameters you can use tag dispatching, where you provide unused parameter to do overloading.
struct Calculations {
struct UseTwoVectors {};
Calculations(double vector, double angle);
Calculations(double horizontalVector, double verticalVector, const UseTwoVectors&);
};
int main() {
Calculations c(1, 2);
Calculations d(1, 2, Calculations::UseTwoVectors());
}
Overall I do not know what these arguments represent - I suspect there is something wrong with abstraction in your code. You could take a different approach and make a class from your parameters.
struct VectorAngle {
double vector, angle;
};
struct TwoVectors {
double horizontalVector, verticalVector;
};
struct Calculations {
Calculations(const VectorAngle& v);
Calculations(const TwoVectors& v);
};
int main() {
Calculations c(VectorAngle{1, 2});
Calculations d(TwoVectors{1, 2});
};
I'm not an advanced programmer. How can I overload the [] operator for a class that has two (or more) array/vector type variables?
class X
{
protected:
std::vector<double> m_x, m_y;
public:
double& operator[](const short &i) { return ???; }
};
What should I use for ???, or how can I do it (maybe adding other definitions?) to be able to call either variable?
Additional question: will this allow other classes of type class derived : public X access m_x and m_y for writing?
UPDATE:
Thank you everyone who answered, but I'm afraid that if I draw the line then the answer to my first question is no, and to the second yes. The longer version implies either an extra struct, or class, or plain setters/getters, which I wanted to avoid by using a simple function for all.
As it stands, the current solution is a (temporary) reference to each variable, in each class to avoid the extra X:: typing (and keep code clear), since m_x would have existed, one way or another.
you can write just a function for this, like:
double &get(unsigned int whichVector, unsigned int index)
{
return (whichVector == 0 ? m_x[index] : m_y[index]);
}
or use operator():
struct A
{
std::vector<int> a1;
std::vector<int> a2;
int operator()(int vec, int index)
{
return (vec == 0 ? a1[index] : a2[index]);
}
};
A a;
auto var = a(0, 1);
but still, this is kinda strange :) probably you should just give a const ref outside, like:
const std::vector<double> &getX() const { return m_x; }
and second question: protected will be convert into private in public inheritance (child/derived will have access to these memebers)
Assuming you want m_x and m_y indexed against the same parameter and a single return value:
struct XGetter
{
double& x;
double& y;
};
XGetter operator[](const short &i) { return { m_x[i], m_y[i] }; }
And the const overload:
struct XGetterReadOnly
{
double x;
double y;
};
XGetterReadOnly operator[](const short &i) const { return { m_x[i], m_y[i] }; }
The compiler will make a good job of optimizing away the intermediate classes XGetter and XGetterReadOnly where appropriate which maybe hard to get your head round if you're a new to C++.
If using mixin doesn't make you uncomfortable you could use tag dispatching like:
#include <utility>
#include <vector>
#include <iostream>
template <size_t I>
struct IndexedVector {
std::vector<double> v;
IndexedVector():v(10){}
};
template <size_t I>
struct tag {
int i;
};
template <size_t S, class = std::make_index_sequence<S>>
struct MixinVector;
template <size_t S, size_t... Is>
struct MixinVector<S, std::index_sequence<Is...>>: IndexedVector<Is>... {
template <size_t I>
double &operator[](tag<I> i) {
return IndexedVector<I>::v[i.i];
}
};
int main() {
MixinVector<2> mv;
mv[tag<0>{0}] = 1.0;
std::cout << mv[tag<0>{0}] << std::endl;
}
To use std::index_sequence you need however compiler supporting c++14 (you could though implement it yourself in c++11). The approach is easily expandable to any number of vectors by simple MixinVector template parameter modification.
There are many broken things, either at conceptual and design level.
Are you able to point your finger simultaneously against two distinct things? No? That's why you cannot use one index to address two distinct vector retaining their distinction.
You can do many things: whatever way to "combine" two value int one is good
by a syntactic point of view:
return m_x[i]+m_y[x] or return sin(m_x[i])*cos(m_y[i]) or return whatever_complicated_expression_you_like_much
But what's the meaning of that? The point is WHY THERE ARE TWO VECTOR IN YOUR CLASS? What do you want them to represent? What do you mean (semantically) indexing them both?
Something I can do to keep their distinction is
auto operator[](int i) const
{ return std::make_pair(m_x[i],m_y[i]); }
so that you get a std::pair<double,double> whose fist and second members are m_x[i] and m_y[i] respectively.
Or ... you can return std::vector<double>{m_x[i],m_y[i]};
About your other question: Yes, inheriting as public makes the new class able to access the protected parts: that's what protected is for.
And yes, you cam R/W: public,protected and private are about visibility, not readability and writeability. That's what const is about.
But again: what does your class represent? without such information we cannot establish what make sense and what not.
Ok, stated your comment:
you need two different funcntions: one for read (double operator[](unsigned) const) and one for write (double& operator[](unsigned) const)
If you know vectors have a known length -say 200-, that you can code an idex transforamtion like i/1000 to identify the vector and i%1000 to get the index,so that 0..199 addres the first, 1000..1199 address the second 2000..2199 address the third... etc.
Or ... you can use an std::pair<unsigned,unsigend> as the index (like operator[](const std::pair<unsigned,unsigned>& i), using i.first to identify the vector, and i.second to index into it, and then call x[{1,10}], x[{3,30}] etc.
Or ... you can chain vetor together as
if(i<m_x.size()) return m_x[i]; i-=m_x:size();
if(i<m_y.size()) return m_y[i]; i-=m_y:size();
if(i<m_z.size()) return m_z[i]; i-=m_z:size();
...
so that you index them contiguously.
But you can get more algorithmic solution using an array of vectors instead of distinct vector variables
if you have std::array<std::vector<double>,N> m; instead of m_x, m_y and m_z the above code can be...
for(auto& v: m)
{
if(i<v.size()) return v[i];
i-=v.size();
}
You can return a struct has two double
struct A{
double& x;
double& y;
A(A& r) : x(r.x), y(r.y){}
A(double& x, double& y) : x(x), y(y){}
};
class X
{
protected:
std::vector<double> m_x, m_y;
public:
A operator[](const short &i) {
A result(m_x[i], m_y[i]);
return result;
}
};
Thank for editing to #marcinj
I'm trying to create classes for mathematical vectors in various dimensions, and since they have much in common I created a templated base class with the vectors size as the template parameter. I'm subclassing because I want different constructors (e.g. Vec2f(float x, float y), Vec3f(float x, float y, float z)) and additional functions like the cross product for 3 dimensional vectors.
My Problem: What should operator+ in the baseclass return? If it returns an instance of the baseclass then Vec3f + Vec3f returns Vecnf, but it should return Vec3f. Can this be achieved somehow?
Here is a code example:
template <size_t n>
class Vecnf {
public:
Vecnf operator+(Vecnf const & vec) const {
return Vecnf(*this) += vec;
}
Vecnf & operator+=(Vecnf const & vec) {
for (int i = 0; i < n; ++i) {
elements[i] += vec.elements[i];
}
return *this;
}
protected:
std::array<float, n> elements;
};
class Vec3f : public Vecnf<3> {
public:
Vec3f(float x = 0.0f, float y = 0.0f, float z = 0.0f);
Vec3f crossProd(Vec3f const & vec);
};
With this implementation, the following:
Vec3f a, b;
Vec3f c = a + b;
gives the compile time error
error: conversion from 'Vecnf<3u>' to non-scalar type 'Vec3f' requested
I'm using TDM GCC version 4.8.1 on Windows 8 Pro. I'm using c++11, so your solution can use it as well, but since I don't think it's crucial I haven't flagged the question with it. Thank you in advance :)
You could make the constructor a variadic template, thereby solving your first problem and eliminating the need to even use inheritance:
template <size_t n>
class Vecnf
{
std::array<float, n> elements;
public:
template <typename ... Args>
Vecnf(Args ... args):
elements {{static_cast<float>(args)...}}
{}
// other methods, operators etc...
};
Now you can make typedef's for the commonly used sizes if you like:
typedef Vecnf<3> Vec3f;
In my opinion, you could even improve this by adding another template parameter for the type you want to store in the array. It could default to float of course.
Also, it shouldn't be too hard to implement the cross-product in a generic way...
You can just define an operator+ on vec3f that does what you want it to. It can have a differing return type to vecnf's as long as it's a subtype of it and it will still override it.
Better solution is what #JorenHeit introduced; that actually is what you wanted, together with two typedefs. This solution is c++03 compatible though, and his isn't.
suppose I have an object like this:
class Spline {
public:
Spline(std::size_t const dim);
// Quite a few functions here. One of those:
vec operator()(double const t) const; // Returns vector of dimension d
}
Now, at most uses of this class, the dimension will already be determined at compile time, thus it would be a good idea (for performance reasons) to change the class like this:
template <std::size_t dim>
class Spline {
public:
Spline();
// Quite a few functions here. One of those:
vec::fixed<dim> operator()(double const t) const; // Returns vector of dimension d
}
(For those who wonder, vec and vec::fixed are objects defined by the armadillo linear algebra library). Now I would like to have both versions living in parallel, thus being able to choose the dimension at compile time as well as during runtime. In short, I would like to create the equivalent of vec::fixed<dim> as Spline::fixed<dim>, but without implementing all functions twice. Especially, I would have to choose the return type of all those functions depending on whether there is a template argument present or not.
Do you have any idea how I might accomplish this, especially thinking in terms of a clear and maintainable design? (In the hope that I made myself clear, which I am not totally sure about.)
Use a simple traits metastruct and specialize that.
template<std::size_t dim>
struct spline_return_traits{
typedef vec::fixed<dim> type;
};
template<>
struct spline_return_traits<0>{ // 0 is a special marker for runtime dimensions
typedef vec type;
};
template<std::size_t dim>
class Spline_impl{
typedef typename spline_return_traits<dim>::type spline_return;
public:
spline_return operator()(double const t) const;
// if <dim> is 0, then the dynamic vec will be chosen as the return type
// all your functions
};
class Spline : public Spline_impl<0>{ // default is dynamic
public:
template<int dim>
struct fixed : public Spline_impl<dim>{
};
};
Now you simple use that. :) Every operator, constructor and function of Spline_impl should be available in the subclasses. For the implementation of each function, you need to do some branching where it's a must to decide between runtime or fixed vec:
if(dim == 0){
// runtime specific stuff
}else{
// compile-time specific stuff
}
Use as:
Spline dynamic_spline;
Spline::fixed<10> fixed_10_spline;
Only problem being that the Spline class will be double the size of Spline_impl... :/ Lemme think if I can find a solution to that too.
Edit: If you don't want Spline to be double the size of Spline_impl, one possibility is to add a little verbosity and a typedef:
class Spline : public Spline_impl<0>{ // default is dynamic size
public:
template<std::size_t dim>
struct fixed{
typedef Spline_impl<dim> type;
};
};
And use as
Spline dynamic_spline;
typename Spline::fixed<10>::type fixed_10_spline;
If I understand your question correctly, you want a struct for compile time usage and runtime usage as well with preferably same name. In my opinion, you can declare the class as template and then specialize one of its instance (say size_t = 0xffffffff), which you may not be using. You can declare all your definitions for runtime usage in that instance.
For example,
template<std::size_t dim = ~0> // choose default dimension(0xffffffff) not to be used
class Spline {
public:
Spline () {}
vec::fixed<dim> operator () (double const t) const {}
};
template<>
class Spline<~0> { // specialize the default dimension for runtime use
public:
Spline (std::size_t const dim) {}
vec operator () (double const t) const {}
};
It can be used as below:
Spline<5> o5; // compile time dimensions
Spline<> o0(3); // run time dimensions (don't mention anything in template)
You can just overload it. Making a non-duplicate implementation, however, is a problem with no generic solution, unless you have more template magic that can do this.
In C++ how to tell compiler that Ogre::Vector3 IS_SAME_AS SomeOtherLIB::Vector3 ?
I feel that.. in languages like c++ which are not structural typed but there are cases when it makes sense.
Normally as game developer when working with 4+ libraries that provide sort or their own Vector3 implementation. The code is littered with ToOgre, ToThis, ToThat conversion function. Thats a lot of Float3 copying around which should not happen on first place.
Is in C++ or any other languages where we dont have to convert (copying) from one type to another which is essentially the samething. But any solution in C++ as most of the good gamedevs libs are for c/c++.
If you use templates you can define functions that take any type of argument as long as the necessary operations are defined on that type. Example:
class Foo { void quack() {} };
class Bar { void quack() {} };
class Baz {};
template<typename Duck>
void f(Duck d) {
d.quack();
}
int main() {
f(Foo()); // works
f(Bar()); // works
f(Baz()); // compile error because Baz does not have a quack method
return 0;
}
While it doesn't suit any situation, templates can give you "compile-time duck typing".
Lets say you have two vector types:
struct Vec3A {
float x, y, z;
};
struct Vec3B {
float p[3];
};
You can define function templates that hide the implementation how of to get the components:
template<class T> float get_x(const T&);
template<class T> float get_y(const T&);
template<class T> float get_z(const T&);
template<> float get_x<Vec3A>(const Vec3A& v) { return v.x; }
// ...
template<> float get_x<Vec3B>(const Vec3B& v) { return v.p[0]; }
// ...
With such helpers you can now write generic functions that work on both:
template<class T> float length(const T& t) {
return std::sqrt(std::pow(get_x(t), 2),
std::pow(get_y(t), 2),
std::pow(get_z(t), 2));
}
You can also continue by specializing utilities like length() for performance or other reasons, e.g. if a certain vector already has a member function providing you with the length:
template<> float length<Vec3C>(const Vec3C& v) {
return v.length();
}
If you are really sure in case of non-virtual structures, you can do a reinterpret_cast. However, it's better to:
do templated wrapper functions as shown by sepp2k
inherit from one of the vectors and add a conversion operator to the other vector
add a separate _cast function that does the conversion
Haxe is a highly portable language with completely optional structural subtyping:
typedef Vector3 = { x : double, y : double, z : double };
class FancyVector3 {
public var x : double, y : double, z : double;
function dot(Vector3 v) {
return x * v.x + y * v.y + z * v.z;
}
function length() {
return Math.sqrt(dot(this));
}
}
Not only is Vector3 an already usable structure, it also acts as a structural interface for other classes. Such typedef'd structs can specify function signatures as well as fields.
Haxe also has a CFFI for talking to C++ (although it still requires conversion methods), and bindings already exist for a few C++ game engines, as well as a variety of lower-level frameworks. Cross-platform engines written in pure Haxe are also being developed, targeting variously C++, Flash, and JS (both Canvas and WebGL).
This is probably not the solution you are looking for right now, but may become more interesting within a few years.