From c6b8b167cac2dca626c2b80974bcca50489366bc Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 22 Sep 2013 10:15:41 -0700 Subject: [PATCH] Add operator<< overloads for Vector3, Ray, and Color --- src/basics.cc | 26 ++++++++++++++++++++++++++ src/basics.h | 7 +++++++ 2 files changed, 33 insertions(+) diff --git a/src/basics.cc b/src/basics.cc index 8dd91d4..eac030c 100644 --- a/src/basics.cc +++ b/src/basics.cc @@ -242,6 +242,15 @@ operator*(const float &lhs, const Vector3 &rhs) return rhs * lhs; } + +std::ostream & +operator<<(std::ostream &os, const Vector3 &v) +{ + // Stream the vector like this: + os << "<" << v.x << ", " << v.y << ", " << v.z << ">"; + return os; +} + #pragma mark - Rays /* @@ -276,6 +285,14 @@ Ray::parameterize(const float t) return origin + t * direction; } + +std::ostream & +operator<<(std::ostream &os, const Ray &r) +{ + os << "[Ray " << r.origin << " " << r.direction << "]"; + return os; +} + #pragma mark - Colors const Color Color::Black = Color(); @@ -482,3 +499,12 @@ operator*(const float &lhs, const Color &rhs) { return rhs * lhs; } + + +std::ostream & +operator<<(std::ostream &os, const Color &c) +{ + // Stream colors like this: + os << "<" << c.red << ", " << c.green << ", " << c.blue << ", " << c.alpha << ">"; + return os; +} diff --git a/src/basics.h b/src/basics.h index ab6b16a..04d6d38 100644 --- a/src/basics.h +++ b/src/basics.h @@ -12,6 +12,9 @@ #ifndef __BASICS_H__ #define __BASICS_H__ +#include + + struct Vector3 { Vector3(); @@ -46,6 +49,7 @@ struct Vector3 }; const Vector3 operator*(const float &lhs, const Vector3 &rhs); +std::ostream &operator<<(std::ostream &os, const Vector3 &v); struct Ray @@ -58,6 +62,8 @@ struct Ray Vector3 origin, direction; }; +std::ostream &operator<<(std::ostream &os, const Ray &r); + struct Color { @@ -96,5 +102,6 @@ struct Color }; const Color operator*(const float &lhs, const Color &rhs); +std::ostream &operator<<(std::ostream &os, const Color &c); #endif