Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why vectors don't have useful, basic algebraic functions?  (Read 7689 times)

0 Members and 1 Guest are viewing this topic.

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Why vectors don't have useful, basic algebraic functions?
« on: May 08, 2009, 04:49:06 pm »
Like to compute a scalar/vectorial product, it's length, normalization.

What's the reason?

Thanks
-Martín

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Why vectors don't have useful, basic algebraic functions?
« Reply #1 on: May 08, 2009, 06:57:52 pm »
The topic has already been discussed here:
http://www.sfml-dev.org/forum/viewtopic.php?t=651

But my opinion isn't exactly the same any more. Although I don't really need the mentioned functions, I can understand blankthemuffin's post. In contrast to some others he had good arguments. The four functions wouldn't really be a problem, if they were implemented in good design (not as member functions, to show a very bad example :)). I can imagine some cases where they might be useful.

Nevertheless, the crucial point is to set the right limit, so that SFML won't tend to a linear algebra library. As I said, I think blankthemuffin's suggestions would be a good compromise.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Why vectors don't have useful, basic algebraic functions?
« Reply #2 on: May 08, 2009, 07:22:06 pm »
It takes about 15 minutes to implement that yourself and one could always use his own vector class.
Listen to my band: pencilcase.bandcamp.com

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Why vectors don't have useful, basic algebraic functions?
« Reply #3 on: June 05, 2009, 10:04:29 pm »
Sorry for the repetition then. I didn't see the one mentioned.

I think that functionality is not really "wild".
Every other library ever has vectors with the basic functionality.
It's not code bloat, it's just what you would expect.

I know it can be done in 15 minutes, but I don't really understand the reason to be so tight with the functionality.

It's not like having a "length()" function would break the purity of the library.

I mean... I'm starting to believe that the "Simple" in SFML is more related to "Basic" than to "Simplicity (of use)".

Regards
-Martín

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Why vectors don't have useful, basic algebraic functions?
« Reply #4 on: June 05, 2009, 10:18:55 pm »
There's no need to argue about that, it's in the roadmap for SFML 2.0... ;)
Laurent Gomila - SFML developer

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Why vectors don't have useful, basic algebraic functions?
« Reply #5 on: June 05, 2009, 10:27:38 pm »
Yey! :D

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Why vectors don't have useful, basic algebraic functions?
« Reply #6 on: July 06, 2009, 05:21:10 pm »
But there is a problem about implementing those functions, namely the support of mathematical operations for certain types. For example, the norm (length) of a vector requires the square root to be defined for the type. What should Norm(IntVector) return? The same applies for the UnitVector (as it divides a vector by its norm).

I propose, an angle function should be added as well, which tells us about the direction of a vector. The vector [1, 0] corresponds to 0°, [0, 1] would be 90° and so on. But here, the problem again comes up because the required function atan2() to compute the angle isn't supported for all types. I wouldn't force the user to provide a set of functions named sqrt, atan2 and so on. Remember, sqrt and atan2 are actually in namespace std and according to the C++ standard, programmers may not extend std except for template specializations. So, you don't know which namespace to use.

Instead, I suggest the solution about traits. Traits are class templates, which define some types, static functions or constants, and which can be specialized for every type. This could look like:
Code: [Select]
// General, empty template
template <typename T>
struct TrigonometricTraits
{
typedef T Type;
};

// template specialization for built-in float
template <>
struct TrigonometricTraits<float>
{
typedef float Type;

static Type Atan2(Type ValY, Type ValX) { return std::atan2(ValY, ValX); }
static Type Sqrt(Type Value)  { return std::sqrt(Value); }
};

Perhaps, a trait could also support functions to transform between degree and radians, here for float:
Code: [Select]
static Type RadToDegFactor()  { return 180.f / 3.14159265f; }
static Type DegToRadFactor()  { return 3.14159265f / 180.f; }

For example, if one uses boost::rational<int> as vector type, the transformation factors must be represented as fractions. The user can now specialize the TrigonometricTraits template for his own types in order to make them work with the vector functions.

A vector function might be implemented like the following:
Code: [Select]
template <typename T>
T Angle(const sf::Vector2<T>& Vector)
{
return ToDegree<T>(TrigonometricTraits<T>::Atan2(Vector.y, Vector.x));
}

Where ToDegree() is the mentioned transformation function:
Code: [Select]
template <typename T>
T ToDegree(T Radian)
{
return Radian * TrigonometricTraits<T>::RadToDegFactor();
}

This may look scaring, but such an implementation is highly generic and flexible. For types which don't support the operations in a useful way (like int), the template is not specialized, resulting in a compile time error.

What do you think about this solution?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Why vectors don't have useful, basic algebraic functions?
« Reply #7 on: July 06, 2009, 05:31:46 pm »
I think that people would get confused by getting a compile error with one specialization of the template, and not with the other. And yes, trust me, there will be people trying to get the norm of a Vector2i ;)

The only conclusion that I can draw from this discussion is that too many people want too many different things. Until something is agreed by everybody, I still won't implement anything into SFML ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Why vectors don't have useful, basic algebraic functions?
« Reply #8 on: July 06, 2009, 05:37:54 pm »
Hm, okay. For me, it's not too tragic; I solved the problem in the mentioned way, so I don't really need the functions directly inside SFML.

Laurent, if you change your opinion and are not sure how to implement this, could you start a discussion again? ;)

By the way, if some people are interested in my implementation, they can contact me. Although I think everyone has his own vector functions... :)

PS: Concerning the confusion about the template error: Maybe beginners are confused, but normally not readers of tutorials. And in C++09, the template confusion can be reduced by concepts. But this is probably remote future...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Why vectors don't have useful, basic algebraic functions?
« Reply #9 on: July 06, 2009, 05:57:34 pm »
Quote
Laurent, if you change your opinion and are not sure how to implement this, could you start a discussion again?

I know how to write and use traits, don't worry ;)

Quote
Concerning the confusion about the template error: Maybe beginners are confused, but normally not readers of tutorials

Of course. But I love keeping things simple, clean and obvious :)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Why vectors don't have useful, basic algebraic functions?
« Reply #10 on: July 06, 2009, 06:26:04 pm »
Quote from: "Laurent"
I know how to write and use traits, don't worry ;)
Sorry, I didn't mean you wouldn't know that. I'd just like to know what other users think about this, or if there are better suggestions (in case the topic becomes relevant again).

Quote from: "Laurent"
Of course. But I love keeping things simple, clean and obvious :)
You're right, an implementation would probably be too confusing. But then you shouldn't forget to remove this for now from the roadmap... ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Why vectors don't have useful, basic algebraic functions?
« Reply #11 on: July 06, 2009, 10:30:49 pm »
You could do some checks with static "compile-time" asserts (like BOOST_STATIC_ASSERT) if you're worried about verbosity of information in compiler errors.

Default template makes an static assertion
Specialized template does the actual stuff.

---------------

But I don't think flexibility is a real issue here. There are better libs for doing math.
I think simplicity should be more important, but without lacking funcionality.

That is: Simple interface and simple implementation. Not as abstract to be totally flexible, somewhat specific to improve library code readability.

But also simplicity for the user, because if he has to define 10 simple operations by hand... they might be simple, but it takes time.

Also I'm against everything being so "private" in classes. That limits the users ability to enhance the library (not talking about vector now). Because it disables user extensions, one must hack the library code.

----------------

The functions I've used so far for vectors are:

const:
vector from angle (cos/sin)
angle from vector (atan2)
squared norm
norm (sqrt)
scalar and vectorial products (although "dot" and "cross" names are more appealing)

non const:
normalize

-------------------

I think that angles should be consistent with the rest of the API. Even if stdc math functions return radians, they should be translated to degrees anyway (as SetRotation/Rotate in graphics take degrees).
Forgetting about changing between formats in just one line can be a real pain in the balls. Using always degrees diminishes these cases.