| |
 | Posted by Patrick Down in reply to elud | Permalink Reply |
|
Patrick Down 
| elud@verat.netstrip wrote in news:oprxnnvbw97mnt56@news.digitalmars.com:
> I get error when using structs (instancied from templates) as function
> return values.
>
I think this bug make be a little bit more subtle. I have a similar library that compiles just fine for me when I use it as a library.
E:\Programming\D\utilities>dmd -unittest main.d mathlib.d mathlib.d(16): identifier 'Vector3D' is not defined
E:\Programming\D\utilities>dmd -c mathlib.d
Notice there is no error when compiling just to an obj.
module utilities.mathlib;
import math;
template vectorlib(CTYPE)
{
struct Vector3D
{
CTYPE x = 0;
CTYPE y = 0;
CTYPE z = 0;
static Vector3D make(CTYPE c1,CTYPE c2,CTYPE c3)
{
Vector3D rtn;
rtn.x = c1;
rtn.y = c2;
rtn.z = c3;
return rtn;
}
Vector3D neg()
{
return Vector3D.make(-x,-y,-z);
}
Vector3D add(Vector3D rhs)
{
return Vector3D.make(x + rhs.x, y + rhs.y, z + rhs.z);
}
Vector3D sub(Vector3D rhs)
{
return Vector3D.make(x - rhs.x, y - rhs.y, z - rhs.z);
}
Vector3D sub_r(Vector3D lhs)
{
return Vector3D.make(lhs.x - x, lhs.y - y, lhs.z - z);
}
Vector3D mul(CTYPE rhs)
{
return Vector3D.make(x*rhs,y*rhs,z*rhs);
}
Vector3D div(CTYPE rhs)
{
return Vector3D.make(x/rhs,y/rhs,z/rhs);
}
Vector3D div_r(CTYPE rhs)
{
assert(false);
return Vector3D.make(0,0,0);
}
Vector3D addass(Vector3D rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Vector3D subass(Vector3D rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
Vector3D mulass(CTYPE rhs)
{
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
Vector3D divass(CTYPE rhs)
{
x /= rhs;
y /= rhs;
z /= rhs;
return *this;
}
CTYPE length2()
{
return x*x + y*y + z*z;
}
CTYPE length()
{
return sqrt(length2());
}
}
Vector3D normalize(Vector3D vec)
{
CTYPE len = vec.length();
return Vector3D.make(vec.x/len,vec.y/len,vec.z/len);
}
Vector3D cross(Vector3D vec1,Vector3D vec2)
{
return Vector3D.make( vec1.y*vec2.z - vec1.z*vec2.y,
vec1.z*vec2.x - vec1.x*vec2.z,
vec1.x*vec2.y - vec1.y*vec2.x);
}
CTYPE dot(Vector3D vec1,Vector3D vec2)
{
return vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z ;
}
CTYPE distance(Vector3D vec1,Vector3D vec2)
{
return (vec1 - vec2).length();
}
}
unittest
{
alias instance vectorlib(double).Vector3D vector;
alias instance vectorlib(double).cross cross;
alias instance vectorlib(double).dot dot;
alias instance vectorlib(double).distance distance;
vector xAxis = vector.make(1,0,0);
vector yAxis = vector.make(0,1,0);
vector zAxis = vector.make(0,0,1);
vector comp = xAxis + yAxis + zAxis;
assert(comp.x == 1 && comp.y == 1 && comp.z == 1);
vector scaled = comp*5;
assert(scaled.x == 5 && scaled.y == 5 && scaled.z == 5);
vector leng = xAxis*2 + yAxis*2 + zAxis;
assert(leng.length() == 3.0);
vector xyCross = cross(xAxis,yAxis);
assert(xyCross.z > 0 && xyCross.x == 0 && xyCross.y == 0);
vector yzCross = cross(yAxis,zAxis);
assert(yzCross.x > 0 && yzCross.z == 0 && yzCross.y == 0);
vector xzCross = cross(zAxis,xAxis);
assert(xzCross.y > 0 && xzCross.x == 0 && xzCross.z == 0);
assert(dot(xAxis,yAxis) == 0);
assert(distance(xAxis,xAxis+yAxis) == 1.0);
}
|